KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > parser > XercesParser


1 /* $Id: XercesParser.java 155412 2005-02-26 12:58:36Z dirkv $
2  *
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.commons.digester.parser;
20
21 import java.lang.reflect.Method JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25 import javax.xml.parsers.SAXParser JavaDoc;
26 import javax.xml.parsers.SAXParserFactory JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.SAXNotRecognizedException JavaDoc;
32 import org.xml.sax.SAXNotSupportedException JavaDoc;
33
34 /**
35  * Create a <code>SAXParser</code> based on the underlying Xerces version.
36  * Currently, Xerces 2.3 and up doesn't implement schema validation the same way
37  * 2.1 was. In other to support schema validation in a portable way between
38  * parser, some features/properties need to be set.
39  *
40  * @since 1.6
41  */

42
43 public class XercesParser{
44
45     /**
46      * The Log to which all SAX event related logging calls will be made.
47      */

48     protected static Log log =
49         LogFactory.getLog("org.apache.commons.digester.Digester.sax");
50
51
52     /**
53      * The JAXP 1.2 property required to set up the schema location.
54      */

55     private static final String JavaDoc JAXP_SCHEMA_SOURCE =
56         "http://java.sun.com/xml/jaxp/properties/schemaSource";
57
58
59     /**
60      * The JAXP 1.2 property to set up the schemaLanguage used.
61      */

62     protected static String JavaDoc JAXP_SCHEMA_LANGUAGE =
63         "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
64
65
66     /**
67      * Xerces dynamic validation property
68      */

69     protected static String JavaDoc XERCES_DYNAMIC =
70         "http://apache.org/xml/features/validation/dynamic";
71
72
73     /**
74      * Xerces schema validation property
75      */

76     protected static String JavaDoc XERCES_SCHEMA =
77         "http://apache.org/xml/features/validation/schema";
78
79
80     /**
81      * A <code>float</code> representing the underlying Xerces version
82      */

83     protected static float version;
84
85
86     /**
87      * The current Xerces version.
88      */

89     protected static String JavaDoc versionNumber = null;
90
91
92     /**
93      * Return the current Xerces version.
94      * @return the current Xerces version.
95      */

96     private static String JavaDoc getXercesVersion() {
97         // If for some reason we can't get the version, set it to 1.0.
98
String JavaDoc versionNumber = "1.0";
99         try{
100             // Use reflection to avoid a build dependency with Xerces.
101
Class JavaDoc versionClass =
102                             Class.forName("org.apache.xerces.impl.Version");
103             // Will return Xerces-J 2.x.0
104
Method JavaDoc method =
105                 versionClass.getMethod("getVersion", (Class JavaDoc[])null);
106             String JavaDoc version = (String JavaDoc)method.invoke(null, (Object JavaDoc[])null);
107             versionNumber = version.substring( "Xerces-J".length() ,
108                                                version.lastIndexOf(".") );
109         } catch (Exception JavaDoc ex){
110             // Do nothing.
111
}
112         return versionNumber;
113     }
114
115
116     /**
117      * Create a <code>SAXParser</code> based on the underlying
118      * <code>Xerces</code> version.
119      * @param properties parser specific properties/features
120      * @return an XML Schema/DTD enabled <code>SAXParser</code>
121      */

122     public static SAXParser JavaDoc newSAXParser(Properties JavaDoc properties)
123             throws ParserConfigurationException JavaDoc,
124                    SAXException JavaDoc,
125                    SAXNotSupportedException JavaDoc {
126
127         SAXParserFactory JavaDoc factory =
128                         (SAXParserFactory JavaDoc)properties.get("SAXParserFactory");
129
130         if (versionNumber == null){
131             versionNumber = getXercesVersion();
132             version = new Float JavaDoc( versionNumber ).floatValue();
133         }
134
135         // Note: 2.2 is completely broken (with XML Schema).
136
if (version > 2.1) {
137
138             configureXerces(factory);
139             return factory.newSAXParser();
140         } else {
141             SAXParser JavaDoc parser = factory.newSAXParser();
142             configureOldXerces(parser,properties);
143             return parser;
144         }
145     }
146
147
148     /**
149      * Configure schema validation as recommended by the JAXP 1.2 spec.
150      * The <code>properties</code> object may contains information about
151      * the schema local and language.
152      * @param properties parser optional info
153      */

154     private static void configureOldXerces(SAXParser JavaDoc parser,
155                                            Properties JavaDoc properties)
156             throws ParserConfigurationException JavaDoc,
157                    SAXNotSupportedException JavaDoc {
158
159         String JavaDoc schemaLocation = (String JavaDoc)properties.get("schemaLocation");
160         String JavaDoc schemaLanguage = (String JavaDoc)properties.get("schemaLanguage");
161
162         try{
163             if (schemaLocation != null) {
164                 parser.setProperty(JAXP_SCHEMA_LANGUAGE, schemaLanguage);
165                 parser.setProperty(JAXP_SCHEMA_SOURCE, schemaLocation);
166             }
167         } catch (SAXNotRecognizedException JavaDoc e){
168             log.info(parser.getClass().getName() + ": "
169                                         + e.getMessage() + " not supported.");
170         }
171
172     }
173
174
175     /**
176      * Configure schema validation as recommended by the Xerces spec.
177      * Both DTD and Schema validation will be enabled simultaneously.
178      * @param factory SAXParserFactory to be configured
179      */

180     private static void configureXerces(SAXParserFactory JavaDoc factory)
181             throws ParserConfigurationException JavaDoc,
182                    SAXNotRecognizedException JavaDoc,
183                    SAXNotSupportedException JavaDoc {
184
185         factory.setFeature(XERCES_DYNAMIC, true);
186         factory.setFeature(XERCES_SCHEMA, true);
187
188     }
189 }
190
Popular Tags