KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > parsers > StandardParserConfiguration


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

16
17 package org.apache.xerces.parsers;
18
19 import org.apache.xerces.impl.Constants;
20 import org.apache.xerces.impl.xs.XMLSchemaValidator;
21 import org.apache.xerces.impl.xs.XSMessageFormatter;
22 import org.apache.xerces.util.SymbolTable;
23 import org.apache.xerces.xni.grammars.XMLGrammarPool;
24 import org.apache.xerces.xni.parser.XMLComponentManager;
25 import org.apache.xerces.xni.parser.XMLConfigurationException;
26
27 /**
28  * This is the "standard" parser configuration. It extends the DTD
29  * configuration with the standard set of parser components.
30  * The standard set of parser components include those needed
31  * to parse and validate with DTD's, and those needed for XML
32  * Schema.</p>
33  * <p>
34  * In addition to the features and properties recognized by the base
35  * parser configuration, this class recognizes these additional
36  * features and properties:
37  * <ul>
38  * <li>Features
39  * <ul>
40  * <li>http://apache.org/xml/features/validation/schema</li>
41  * <li>http://apache.org/xml/features/validation/schema-full-checking</li>
42  * <li>http://apache.org/xml/features/validation/schema/normalized-value</li>
43  * <li>http://apache.org/xml/features/validation/schema/element-default</li>
44  * </ul>
45  * <li>Properties
46  * <ul>
47  * <li>http://apache.org/xml/properties/internal/error-reporter</li>
48  * <li>http://apache.org/xml/properties/internal/entity-manager</li>
49  * <li>http://apache.org/xml/properties/internal/document-scanner</li>
50  * <li>http://apache.org/xml/properties/internal/dtd-scanner</li>
51  * <li>http://apache.org/xml/properties/internal/grammar-pool</li>
52  * <li>http://apache.org/xml/properties/internal/validator/dtd</li>
53  * <li>http://apache.org/xml/properties/internal/datatype-validator-factory</li>
54  * </ul>
55  * </ul>
56  *
57  * @author Arnaud Le Hors, IBM
58  * @author Andy Clark, IBM
59  *
60  * @version $Id: StandardParserConfiguration.java,v 1.38 2005/02/09 15:34:02 ankitp Exp $
61  */

62 public class StandardParserConfiguration
63     extends DTDConfiguration {
64
65     //
66
// Constants
67
//
68

69     // feature identifiers
70

71     /** Feature identifier: expose schema normalized value */
72     protected static final String JavaDoc NORMALIZE_DATA =
73     Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE;
74
75
76     /** Feature identifier: send element default value via characters() */
77     protected static final String JavaDoc SCHEMA_ELEMENT_DEFAULT =
78     Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_ELEMENT_DEFAULT;
79
80
81     /** Feature identifier: augment PSVI */
82     protected static final String JavaDoc SCHEMA_AUGMENT_PSVI =
83     Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI;
84
85
86     /** feature identifier: XML Schema validation */
87     protected static final String JavaDoc XMLSCHEMA_VALIDATION =
88     Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE;
89
90     /** feature identifier: XML Schema validation -- full checking */
91     protected static final String JavaDoc XMLSCHEMA_FULL_CHECKING =
92     Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING;
93     
94     /** Feature: generate synthetic annotations */
95     protected static final String JavaDoc GENERATE_SYNTHETIC_ANNOTATIONS =
96         Constants.XERCES_FEATURE_PREFIX + Constants.GENERATE_SYNTHETIC_ANNOTATIONS_FEATURE;
97     
98     /** Feature identifier: validate annotations */
99     protected static final String JavaDoc VALIDATE_ANNOTATIONS =
100         Constants.XERCES_FEATURE_PREFIX + Constants.VALIDATE_ANNOTATIONS_FEATURE;
101     
102     /** Feature identifier: honour all schemaLocations */
103     protected static final String JavaDoc HONOUR_ALL_SCHEMALOCATIONS =
104         Constants.XERCES_FEATURE_PREFIX + Constants.HONOUR_ALL_SCHEMALOCATIONS_FEATURE;
105
106     // property identifiers
107

108     /** Property identifier: XML Schema validator. */
109     protected static final String JavaDoc SCHEMA_VALIDATOR =
110         Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY;
111
112     /** Property identifier: schema location. */
113     protected static final String JavaDoc SCHEMA_LOCATION =
114     Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_LOCATION;
115
116     /** Property identifier: no namespace schema location. */
117     protected static final String JavaDoc SCHEMA_NONS_LOCATION =
118     Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_NONS_LOCATION;
119
120     //
121
// Data
122
//
123

124     // components (non-configurable)
125

126     /** XML Schema Validator. */
127     protected XMLSchemaValidator fSchemaValidator;
128
129     //
130
// Constructors
131
//
132

133     /** Default constructor. */
134     public StandardParserConfiguration() {
135         this(null, null, null);
136     } // <init>()
137

138     /**
139      * Constructs a parser configuration using the specified symbol table.
140      *
141      * @param symbolTable The symbol table to use.
142      */

143     public StandardParserConfiguration(SymbolTable symbolTable) {
144         this(symbolTable, null, null);
145     } // <init>(SymbolTable)
146

147     /**
148      * Constructs a parser configuration using the specified symbol table and
149      * grammar pool.
150      * <p>
151      * <strong>REVISIT:</strong>
152      * Grammar pool will be updated when the new validation engine is
153      * implemented.
154      *
155      * @param symbolTable The symbol table to use.
156      * @param grammarPool The grammar pool to use.
157      */

158     public StandardParserConfiguration(SymbolTable symbolTable,
159                                        XMLGrammarPool grammarPool) {
160         this(symbolTable, grammarPool, null);
161     } // <init>(SymbolTable,XMLGrammarPool)
162

163     /**
164      * Constructs a parser configuration using the specified symbol table,
165      * grammar pool, and parent settings.
166      * <p>
167      * <strong>REVISIT:</strong>
168      * Grammar pool will be updated when the new validation engine is
169      * implemented.
170      *
171      * @param symbolTable The symbol table to use.
172      * @param grammarPool The grammar pool to use.
173      * @param parentSettings The parent settings.
174      */

175     public StandardParserConfiguration(SymbolTable symbolTable,
176                                        XMLGrammarPool grammarPool,
177                                        XMLComponentManager parentSettings) {
178         super(symbolTable, grammarPool, parentSettings);
179
180         // add default recognized features
181
final String JavaDoc[] recognizedFeatures = {
182             NORMALIZE_DATA,
183             SCHEMA_ELEMENT_DEFAULT,
184             SCHEMA_AUGMENT_PSVI,
185             GENERATE_SYNTHETIC_ANNOTATIONS,
186             VALIDATE_ANNOTATIONS,
187             HONOUR_ALL_SCHEMALOCATIONS,
188             // NOTE: These shouldn't really be here but since the XML Schema
189
// validator is constructed dynamically, its recognized
190
// features might not have been set and it would cause a
191
// not-recognized exception to be thrown. -Ac
192
XMLSCHEMA_VALIDATION,
193             XMLSCHEMA_FULL_CHECKING,
194         };
195         addRecognizedFeatures(recognizedFeatures);
196
197         // set state for default features
198
setFeature(SCHEMA_ELEMENT_DEFAULT, true);
199         setFeature(NORMALIZE_DATA, true);
200         setFeature(SCHEMA_AUGMENT_PSVI, true);
201         setFeature(GENERATE_SYNTHETIC_ANNOTATIONS, false);
202         setFeature(VALIDATE_ANNOTATIONS, false);
203         setFeature(HONOUR_ALL_SCHEMALOCATIONS, false);
204
205         // add default recognized properties
206

207         final String JavaDoc[] recognizedProperties = {
208             // NOTE: These shouldn't really be here but since the XML Schema
209
// validator is constructed dynamically, its recognized
210
// properties might not have been set and it would cause a
211
// not-recognized exception to be thrown. -Ac
212
SCHEMA_LOCATION,
213             SCHEMA_NONS_LOCATION,
214             };
215
216             addRecognizedProperties(recognizedProperties);
217
218     } // <init>(SymbolTable,XMLGrammarPool)
219

220     //
221
// Public methods
222
//
223

224     /** Configures the pipeline. */
225     protected void configurePipeline() {
226         super.configurePipeline();
227         if ( getFeature(XMLSCHEMA_VALIDATION )) {
228             // If schema validator was not in the pipeline insert it.
229
if (fSchemaValidator == null) {
230                 fSchemaValidator = new XMLSchemaValidator();
231             
232                 // add schema component
233
fProperties.put(SCHEMA_VALIDATOR, fSchemaValidator);
234                 addComponent(fSchemaValidator);
235                  // add schema message formatter
236
if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
237                     XSMessageFormatter xmft = new XSMessageFormatter();
238                     fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, xmft);
239                 }
240
241             }
242             fLastComponent = fSchemaValidator;
243             fNamespaceBinder.setDocumentHandler(fSchemaValidator);
244             
245             fSchemaValidator.setDocumentHandler(fDocumentHandler);
246             fSchemaValidator.setDocumentSource(fNamespaceBinder);
247         }
248
249
250     } // configurePipeline()
251

252     // features and properties
253

254     /**
255      * Check a feature. If feature is know and supported, this method simply
256      * returns. Otherwise, the appropriate exception is thrown.
257      *
258      * @param featureId The unique identifier (URI) of the feature.
259      *
260      * @throws XMLConfigurationException Thrown for configuration error.
261      * In general, components should
262      * only throw this exception if
263      * it is <strong>really</strong>
264      * a critical error.
265      */

266     protected void checkFeature(String JavaDoc featureId)
267         throws XMLConfigurationException {
268
269         //
270
// Xerces Features
271
//
272

273         if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
274             final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
275             
276             //
277
// http://apache.org/xml/features/validation/schema
278
// Lets the user turn Schema validation support on/off.
279
//
280
if (suffixLength == Constants.SCHEMA_VALIDATION_FEATURE.length() &&
281                 featureId.endsWith(Constants.SCHEMA_VALIDATION_FEATURE)) {
282                 return;
283             }
284             // activate full schema checking
285
if (suffixLength == Constants.SCHEMA_FULL_CHECKING.length() &&
286                 featureId.endsWith(Constants.SCHEMA_FULL_CHECKING)) {
287                 return;
288             }
289             // Feature identifier: expose schema normalized value
290
// http://apache.org/xml/features/validation/schema/normalized-value
291
if (suffixLength == Constants.SCHEMA_NORMALIZED_VALUE.length() &&
292                 featureId.endsWith(Constants.SCHEMA_NORMALIZED_VALUE)) {
293                 return;
294             }
295             // Feature identifier: send element default value via characters()
296
// http://apache.org/xml/features/validation/schema/element-default
297
if (suffixLength == Constants.SCHEMA_ELEMENT_DEFAULT.length() &&
298                 featureId.endsWith(Constants.SCHEMA_ELEMENT_DEFAULT)) {
299                 return;
300             }
301         }
302
303         //
304
// Not recognized
305
//
306

307         super.checkFeature(featureId);
308
309     } // checkFeature(String)
310

311     /**
312      * Check a property. If the property is know and supported, this method
313      * simply returns. Otherwise, the appropriate exception is thrown.
314      *
315      * @param propertyId The unique identifier (URI) of the property
316      * being set.
317      *
318      * @throws XMLConfigurationException Thrown for configuration error.
319      * In general, components should
320      * only throw this exception if
321      * it is <strong>really</strong>
322      * a critical error.
323      */

324     protected void checkProperty(String JavaDoc propertyId)
325         throws XMLConfigurationException {
326
327         //
328
// Xerces Properties
329
//
330

331         if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
332             final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length();
333             
334             if (suffixLength == Constants.SCHEMA_LOCATION.length() &&
335                 propertyId.endsWith(Constants.SCHEMA_LOCATION)) {
336                 return;
337             }
338             if (suffixLength == Constants.SCHEMA_NONS_LOCATION.length() &&
339                 propertyId.endsWith(Constants.SCHEMA_NONS_LOCATION)) {
340                 return;
341             }
342         }
343
344         if (propertyId.startsWith(Constants.JAXP_PROPERTY_PREFIX)) {
345             final int suffixLength = propertyId.length() - Constants.JAXP_PROPERTY_PREFIX.length();
346             
347             if (suffixLength == Constants.SCHEMA_SOURCE.length() &&
348                 propertyId.endsWith(Constants.SCHEMA_SOURCE)) {
349                 return;
350             }
351         }
352
353         //
354
// Not recognized
355
//
356

357         super.checkProperty(propertyId);
358
359     } // checkProperty(String)
360

361 } // class StandardParserConfiguration
362
Popular Tags