KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > parsers > StandardParserConfiguration


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2004 The Apache Software Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.parsers;
59
60 import com.sun.org.apache.xerces.internal.impl.Constants;
61 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator;
62 import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter;
63 import com.sun.org.apache.xerces.internal.util.SymbolTable;
64 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
65 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
66 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
67
68 /**
69  * This is the "standard" parser configuration. It extends the DTD
70  * configuration with the standard set of parser components.
71  * The standard set of parser components include those needed
72  * to parse and validate with DTD's, and those needed for XML
73  * Schema.</p>
74  * <p>
75  * In addition to the features and properties recognized by the base
76  * parser configuration, this class recognizes these additional
77  * features and properties:
78  * <ul>
79  * <li>Features
80  * <ul>
81  * <li>http://apache.org/xml/features/validation/schema</li>
82  * <li>http://apache.org/xml/features/validation/schema-full-checking</li>
83  * <li>http://apache.org/xml/features/validation/schema/normalized-value</li>
84  * <li>http://apache.org/xml/features/validation/schema/element-default</li>
85  * </ul>
86  * <li>Properties
87  * <ul>
88  * <li>http://apache.org/xml/properties/internal/error-reporter</li>
89  * <li>http://apache.org/xml/properties/internal/entity-manager</li>
90  * <li>http://apache.org/xml/properties/internal/document-scanner</li>
91  * <li>http://apache.org/xml/properties/internal/dtd-scanner</li>
92  * <li>http://apache.org/xml/properties/internal/grammar-pool</li>
93  * <li>http://apache.org/xml/properties/internal/validator/dtd</li>
94  * <li>http://apache.org/xml/properties/internal/datatype-validator-factory</li>
95  * </ul>
96  * </ul>
97  *
98  * @author Arnaud Le Hors, IBM
99  * @author Andy Clark, IBM
100  *
101  * @version $Id: StandardParserConfiguration.java,v 1.33 2004/01/26 17:28:10 mrglavas Exp $
102  */

103 public class StandardParserConfiguration
104     extends DTDConfiguration {
105
106     //
107
// Constants
108
//
109

110     // feature identifiers
111

112     /** Feature identifier: expose schema normalized value */
113     protected static final String JavaDoc NORMALIZE_DATA =
114     Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE;
115
116
117     /** Feature identifier: send element default value via characters() */
118     protected static final String JavaDoc SCHEMA_ELEMENT_DEFAULT =
119     Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_ELEMENT_DEFAULT;
120
121
122     /** Feature identifier: augment PSVI */
123     protected static final String JavaDoc SCHEMA_AUGMENT_PSVI =
124     Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI;
125
126
127     /** feature identifier: XML Schema validation */
128     protected static final String JavaDoc XMLSCHEMA_VALIDATION =
129     Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE;
130
131     /** feature identifier: XML Schema validation -- full checking */
132     protected static final String JavaDoc XMLSCHEMA_FULL_CHECKING =
133     Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING;
134
135     // property identifiers
136

137     /** Property identifier: XML Schema validator. */
138     protected static final String JavaDoc SCHEMA_VALIDATOR =
139         Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY;
140
141     /** Property identifier: schema location. */
142     protected static final String JavaDoc SCHEMA_LOCATION =
143     Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_LOCATION;
144
145     /** Property identifier: no namespace schema location. */
146     protected static final String JavaDoc SCHEMA_NONS_LOCATION =
147     Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_NONS_LOCATION;
148
149     //
150
// Data
151
//
152

153     // components (non-configurable)
154

155     /** XML Schema Validator. */
156     protected XMLSchemaValidator fSchemaValidator;
157
158     //
159
// Constructors
160
//
161

162     /** Default constructor. */
163     public StandardParserConfiguration() {
164         this(null, null, null);
165     } // <init>()
166

167     /**
168      * Constructs a parser configuration using the specified symbol table.
169      *
170      * @param symbolTable The symbol table to use.
171      */

172     public StandardParserConfiguration(SymbolTable symbolTable) {
173         this(symbolTable, null, null);
174     } // <init>(SymbolTable)
175

176     /**
177      * Constructs a parser configuration using the specified symbol table and
178      * grammar pool.
179      * <p>
180      * <strong>REVISIT:</strong>
181      * Grammar pool will be updated when the new validation engine is
182      * implemented.
183      *
184      * @param symbolTable The symbol table to use.
185      * @param grammarPool The grammar pool to use.
186      */

187     public StandardParserConfiguration(SymbolTable symbolTable,
188                                        XMLGrammarPool grammarPool) {
189         this(symbolTable, grammarPool, null);
190     } // <init>(SymbolTable,XMLGrammarPool)
191

192     /**
193      * Constructs a parser configuration using the specified symbol table,
194      * grammar pool, and parent settings.
195      * <p>
196      * <strong>REVISIT:</strong>
197      * Grammar pool will be updated when the new validation engine is
198      * implemented.
199      *
200      * @param symbolTable The symbol table to use.
201      * @param grammarPool The grammar pool to use.
202      * @param parentSettings The parent settings.
203      */

204     public StandardParserConfiguration(SymbolTable symbolTable,
205                                        XMLGrammarPool grammarPool,
206                                        XMLComponentManager parentSettings) {
207         super(symbolTable, grammarPool, parentSettings);
208
209         // add default recognized features
210
final String JavaDoc[] recognizedFeatures = {
211             NORMALIZE_DATA,
212             SCHEMA_ELEMENT_DEFAULT,
213             SCHEMA_AUGMENT_PSVI,
214             // NOTE: These shouldn't really be here but since the XML Schema
215
// validator is constructed dynamically, its recognized
216
// features might not have been set and it would cause a
217
// not-recognized exception to be thrown. -Ac
218
XMLSCHEMA_VALIDATION,
219             XMLSCHEMA_FULL_CHECKING,
220         };
221         addRecognizedFeatures(recognizedFeatures);
222
223         // set state for default features
224
setFeature(SCHEMA_ELEMENT_DEFAULT, true);
225         setFeature(NORMALIZE_DATA, true);
226         setFeature(SCHEMA_AUGMENT_PSVI, true);
227
228         // add default recognized properties
229

230         final String JavaDoc[] recognizedProperties = {
231             // NOTE: These shouldn't really be here but since the XML Schema
232
// validator is constructed dynamically, its recognized
233
// properties might not have been set and it would cause a
234
// not-recognized exception to be thrown. -Ac
235
SCHEMA_LOCATION,
236             SCHEMA_NONS_LOCATION,
237             };
238
239             addRecognizedProperties(recognizedProperties);
240
241     } // <init>(SymbolTable,XMLGrammarPool)
242

243     //
244
// Public methods
245
//
246

247     /** Configures the pipeline. */
248     protected void configurePipeline() {
249         super.configurePipeline();
250         if ( getFeature(XMLSCHEMA_VALIDATION )) {
251             // If schema validator was not in the pipeline insert it.
252
if (fSchemaValidator == null) {
253                 fSchemaValidator = new XMLSchemaValidator();
254             
255                 // add schema component
256
fProperties.put(SCHEMA_VALIDATOR, fSchemaValidator);
257                 addComponent(fSchemaValidator);
258                  // add schema message formatter
259
if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
260                     XSMessageFormatter xmft = new XSMessageFormatter();
261                     fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, xmft);
262                 }
263
264             }
265             fLastComponent = fSchemaValidator;
266             fNamespaceBinder.setDocumentHandler(fSchemaValidator);
267             
268             fSchemaValidator.setDocumentHandler(fDocumentHandler);
269             fSchemaValidator.setDocumentSource(fNamespaceBinder);
270         }
271
272
273     } // configurePipeline()
274

275     // features and properties
276

277     /**
278      * Check a feature. If feature is know and supported, this method simply
279      * returns. Otherwise, the appropriate exception is thrown.
280      *
281      * @param featureId The unique identifier (URI) of the feature.
282      *
283      * @throws XMLConfigurationException Thrown for configuration error.
284      * In general, components should
285      * only throw this exception if
286      * it is <strong>really</strong>
287      * a critical error.
288      */

289     protected void checkFeature(String JavaDoc featureId)
290         throws XMLConfigurationException {
291
292         //
293
// Xerces Features
294
//
295

296         if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
297             final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
298             
299             //
300
// http://apache.org/xml/features/validation/schema
301
// Lets the user turn Schema validation support on/off.
302
//
303
if (suffixLength == Constants.SCHEMA_VALIDATION_FEATURE.length() &&
304                 featureId.endsWith(Constants.SCHEMA_VALIDATION_FEATURE)) {
305                 return;
306             }
307             // activate full schema checking
308
if (suffixLength == Constants.SCHEMA_FULL_CHECKING.length() &&
309                 featureId.endsWith(Constants.SCHEMA_FULL_CHECKING)) {
310                 return;
311             }
312             // Feature identifier: expose schema normalized value
313
// http://apache.org/xml/features/validation/schema/normalized-value
314
if (suffixLength == Constants.SCHEMA_NORMALIZED_VALUE.length() &&
315                 featureId.endsWith(Constants.SCHEMA_NORMALIZED_VALUE)) {
316                 return;
317             }
318             // Feature identifier: send element default value via characters()
319
// http://apache.org/xml/features/validation/schema/element-default
320
if (suffixLength == Constants.SCHEMA_ELEMENT_DEFAULT.length() &&
321                 featureId.endsWith(Constants.SCHEMA_ELEMENT_DEFAULT)) {
322                 return;
323             }
324         }
325
326         //
327
// Not recognized
328
//
329

330         super.checkFeature(featureId);
331
332     } // checkFeature(String)
333

334     /**
335      * Check a property. If the property is know and supported, this method
336      * simply returns. Otherwise, the appropriate exception is thrown.
337      *
338      * @param propertyId The unique identifier (URI) of the property
339      * being set.
340      *
341      * @throws XMLConfigurationException Thrown for configuration error.
342      * In general, components should
343      * only throw this exception if
344      * it is <strong>really</strong>
345      * a critical error.
346      */

347     protected void checkProperty(String JavaDoc propertyId)
348         throws XMLConfigurationException {
349
350         //
351
// Xerces Properties
352
//
353

354         if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
355             final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length();
356             
357             if (suffixLength == Constants.SCHEMA_LOCATION.length() &&
358                 propertyId.endsWith(Constants.SCHEMA_LOCATION)) {
359                 return;
360             }
361             if (suffixLength == Constants.SCHEMA_NONS_LOCATION.length() &&
362                 propertyId.endsWith(Constants.SCHEMA_NONS_LOCATION)) {
363                 return;
364             }
365         }
366
367         if (propertyId.startsWith(Constants.JAXP_PROPERTY_PREFIX)) {
368             final int suffixLength = propertyId.length() - Constants.JAXP_PROPERTY_PREFIX.length();
369             
370             if (suffixLength == Constants.SCHEMA_SOURCE.length() &&
371                 propertyId.endsWith(Constants.SCHEMA_SOURCE)) {
372                 return;
373             }
374         }
375
376         //
377
// Not recognized
378
//
379

380         super.checkProperty(propertyId);
381
382     } // checkProperty(String)
383

384 } // class StandardParserConfiguration
385
Popular Tags