KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > digester > XercesParser


1 /* $Id: XercesParser.java 467222 2006-10-24 03:17:11Z markt $
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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

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

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

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

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

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

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

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

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

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

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

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

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