KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
61 import java.util.Locale JavaDoc;
62
63 import com.sun.org.apache.xerces.internal.impl.Constants;
64 import com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl;
65 import com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl;
66 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
67 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
68 import com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl;
69 import com.sun.org.apache.xerces.internal.impl.dv.DTDDVFactory;
70 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
71 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
72 import com.sun.org.apache.xerces.internal.util.SymbolTable;
73 import com.sun.org.apache.xerces.internal.xni.XMLLocator;
74 import com.sun.org.apache.xerces.internal.xni.XNIException;
75 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
76 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
77 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
78 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
79 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDScanner;
80 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentScanner;
81 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
82 import com.sun.org.apache.xerces.internal.xni.parser.XMLPullParserConfiguration;
83
84 /**
85  * This is the non validating parser configuration. It extends the basic
86  * configuration with the set of following parser components:
87  * Document scanner, DTD scanner, namespace binder, document handler.
88  * <p>
89  * Xerces parser that uses this configuration is <strong>not</strong> <a HREF="http://www.w3.org/TR/REC-xml#sec-conformance">conformant</a>
90  * non-validating XML processor, since conformant non-validating processor is required
91  * to process "all the declarations they read in the internal DTD subset ... must use the information in those declarations to normalize attribute values,
92  * include the replacement text of internal entities, and supply default attribute values".
93  *
94  * @author Elena Litani, IBM
95  * @version $Id: NonValidatingConfiguration.java,v 1.12 2004/01/26 17:28:10 mrglavas Exp $
96  */

97 public class NonValidatingConfiguration
98     extends BasicParserConfiguration
99     implements XMLPullParserConfiguration {
100
101     //
102
// Constants
103
//
104

105     // feature identifiers
106

107     /** Feature identifier: warn on duplicate attribute definition. */
108     protected static final String JavaDoc WARN_ON_DUPLICATE_ATTDEF =
109         Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE;
110
111     /** Feature identifier: warn on duplicate entity definition. */
112     protected static final String JavaDoc WARN_ON_DUPLICATE_ENTITYDEF =
113         Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE;
114     
115     /** Feature identifier: warn on undeclared element definition. */
116     protected static final String JavaDoc WARN_ON_UNDECLARED_ELEMDEF =
117         Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE;
118     
119     /** Feature identifier: allow Java encodings. */
120     protected static final String JavaDoc ALLOW_JAVA_ENCODINGS =
121         Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE;
122     
123     /** Feature identifier: continue after fatal error. */
124     protected static final String JavaDoc CONTINUE_AFTER_FATAL_ERROR =
125         Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE;
126
127     /** Feature identifier: load external DTD. */
128     protected static final String JavaDoc LOAD_EXTERNAL_DTD =
129         Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE;
130
131     /** Feature identifier: notify built-in refereces. */
132     protected static final String JavaDoc NOTIFY_BUILTIN_REFS =
133         Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_BUILTIN_REFS_FEATURE;
134     
135     /** Feature identifier: notify character refereces. */
136     protected static final String JavaDoc NOTIFY_CHAR_REFS =
137         Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_CHAR_REFS_FEATURE;
138     
139
140     /** Feature identifier: expose schema normalized value */
141     protected static final String JavaDoc NORMALIZE_DATA =
142     Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE;
143
144
145     /** Feature identifier: send element default value via characters() */
146     protected static final String JavaDoc SCHEMA_ELEMENT_DEFAULT =
147     Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_ELEMENT_DEFAULT;
148
149     // property identifiers
150

151     /** Property identifier: error reporter. */
152     protected static final String JavaDoc ERROR_REPORTER =
153         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
154
155     /** Property identifier: entity manager. */
156     protected static final String JavaDoc ENTITY_MANAGER =
157         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
158     
159     /** Property identifier document scanner: */
160     protected static final String JavaDoc DOCUMENT_SCANNER =
161         Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_SCANNER_PROPERTY;
162
163     /** Property identifier: DTD scanner. */
164     protected static final String JavaDoc DTD_SCANNER =
165         Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_SCANNER_PROPERTY;
166
167     /** Property identifier: grammar pool. */
168     protected static final String JavaDoc XMLGRAMMAR_POOL =
169         Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
170     
171     /** Property identifier: DTD validator. */
172     protected static final String JavaDoc DTD_VALIDATOR =
173         Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_VALIDATOR_PROPERTY;
174
175     /** Property identifier: namespace binder. */
176     protected static final String JavaDoc NAMESPACE_BINDER =
177         Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_BINDER_PROPERTY;
178
179     /** Property identifier: datatype validator factory. */
180     protected static final String JavaDoc DATATYPE_VALIDATOR_FACTORY =
181         Constants.XERCES_PROPERTY_PREFIX + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY;
182
183     protected static final String JavaDoc VALIDATION_MANAGER =
184         Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY;
185
186     /** Property identifier: XML Schema validator. */
187     protected static final String JavaDoc SCHEMA_VALIDATOR =
188         Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY;
189
190
191     // debugging
192

193     /** Set to true and recompile to print exception stack trace. */
194     private static final boolean PRINT_EXCEPTION_STACK_TRACE = false;
195
196     //
197
// Data
198
//
199

200     // components (non-configurable)
201

202     /** Grammar pool. */
203     protected XMLGrammarPool fGrammarPool;
204
205     /** Datatype validator factory. */
206     protected DTDDVFactory fDatatypeValidatorFactory;
207
208     // components (configurable)
209

210     /** Error reporter. */
211     protected XMLErrorReporter fErrorReporter;
212
213     /** Entity manager. */
214     protected XMLEntityManager fEntityManager;
215
216     /** Document scanner. */
217     protected XMLDocumentScanner fScanner;
218
219     /** Input Source */
220     protected XMLInputSource fInputSource;
221
222     /** DTD scanner. */
223     protected XMLDTDScanner fDTDScanner;
224
225
226     protected ValidationManager fValidationManager;
227
228     // private data
229

230     /** Document scanner that does namespace binding. */
231     private XMLNSDocumentScannerImpl fNamespaceScanner;
232
233     /** Default Xerces implementation of scanner*/
234     private XMLDocumentScannerImpl fNonNSScanner;
235     
236     
237     /** fConfigUpdated is set to true if there has been any change to the configuration settings,
238      * i.e a feature or a property was changed.
239      */

240     protected boolean fConfigUpdated = false;
241
242
243     // state
244

245     /** Locator */
246     protected XMLLocator fLocator;
247
248     /**
249      * True if a parse is in progress. This state is needed because
250      * some features/properties cannot be set while parsing (e.g.
251      * validation and namespaces).
252      */

253     protected boolean fParseInProgress = false;
254
255     //
256
// Constructors
257
//
258

259     /** Default constructor. */
260     public NonValidatingConfiguration() {
261         this(null, null, null);
262     } // <init>()
263

264     /**
265      * Constructs a parser configuration using the specified symbol table.
266      *
267      * @param symbolTable The symbol table to use.
268      */

269     public NonValidatingConfiguration(SymbolTable symbolTable) {
270         this(symbolTable, null, null);
271     } // <init>(SymbolTable)
272

273     /**
274      * Constructs a parser configuration using the specified symbol table and
275      * grammar pool.
276      * <p>
277      * <strong>REVISIT:</strong>
278      * Grammar pool will be updated when the new validation engine is
279      * implemented.
280      *
281      * @param symbolTable The symbol table to use.
282      * @param grammarPool The grammar pool to use.
283      */

284     public NonValidatingConfiguration(SymbolTable symbolTable,
285                                        XMLGrammarPool grammarPool) {
286         this(symbolTable, grammarPool, null);
287     } // <init>(SymbolTable,XMLGrammarPool)
288

289     /**
290      * Constructs a parser configuration using the specified symbol table,
291      * grammar pool, and parent settings.
292      * <p>
293      * <strong>REVISIT:</strong>
294      * Grammar pool will be updated when the new validation engine is
295      * implemented.
296      *
297      * @param symbolTable The symbol table to use.
298      * @param grammarPool The grammar pool to use.
299      * @param parentSettings The parent settings.
300      */

301     public NonValidatingConfiguration(SymbolTable symbolTable,
302                                        XMLGrammarPool grammarPool,
303                                        XMLComponentManager parentSettings) {
304         super(symbolTable, parentSettings);
305
306         // add default recognized features
307
final String JavaDoc[] recognizedFeatures = {
308             PARSER_SETTINGS,
309             NAMESPACES,
310             //WARN_ON_DUPLICATE_ATTDEF, // from XMLDTDScannerImpl
311
//WARN_ON_UNDECLARED_ELEMDEF, // from XMLDTDScannerImpl
312
//ALLOW_JAVA_ENCODINGS, // from XMLEntityManager
313
CONTINUE_AFTER_FATAL_ERROR,
314             //LOAD_EXTERNAL_DTD, // from XMLDTDScannerImpl
315
//NOTIFY_BUILTIN_REFS, // from XMLDocumentFragmentScannerImpl
316
//NOTIFY_CHAR_REFS, // from XMLDocumentFragmentScannerImpl
317
//WARN_ON_DUPLICATE_ENTITYDEF // from XMLEntityManager
318
};
319         addRecognizedFeatures(recognizedFeatures);
320
321         // set state for default features
322
//setFeature(WARN_ON_DUPLICATE_ATTDEF, false); // from XMLDTDScannerImpl
323
//setFeature(WARN_ON_UNDECLARED_ELEMDEF, false); // from XMLDTDScannerImpl
324
//setFeature(ALLOW_JAVA_ENCODINGS, false); // from XMLEntityManager
325
fFeatures.put(CONTINUE_AFTER_FATAL_ERROR, Boolean.FALSE);
326         fFeatures.put(PARSER_SETTINGS, Boolean.TRUE);
327         fFeatures.put(NAMESPACES, Boolean.TRUE);
328         //setFeature(LOAD_EXTERNAL_DTD, true); // from XMLDTDScannerImpl
329
//setFeature(NOTIFY_BUILTIN_REFS, false); // from XMLDocumentFragmentScannerImpl
330
//setFeature(NOTIFY_CHAR_REFS, false); // from XMLDocumentFragmentScannerImpl
331
//setFeature(WARN_ON_DUPLICATE_ENTITYDEF, false); // from XMLEntityManager
332

333         // add default recognized properties
334
final String JavaDoc[] recognizedProperties = {
335             ERROR_REPORTER,
336             ENTITY_MANAGER,
337             DOCUMENT_SCANNER,
338             DTD_SCANNER,
339             DTD_VALIDATOR,
340             NAMESPACE_BINDER,
341             XMLGRAMMAR_POOL,
342             DATATYPE_VALIDATOR_FACTORY,
343             VALIDATION_MANAGER
344         };
345         addRecognizedProperties(recognizedProperties);
346     
347         fGrammarPool = grammarPool;
348         if(fGrammarPool != null){
349             fProperties.put(XMLGRAMMAR_POOL, fGrammarPool);
350         }
351
352         fEntityManager = createEntityManager();
353         fProperties.put(ENTITY_MANAGER, fEntityManager);
354         addComponent(fEntityManager);
355
356         fErrorReporter = createErrorReporter();
357         fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner());
358         fProperties.put(ERROR_REPORTER, fErrorReporter);
359         addComponent(fErrorReporter);
360
361         // this configuration delays creation of the scanner
362
// till it is known if namespace processing should be performed
363

364         fDTDScanner = createDTDScanner();
365         if (fDTDScanner != null) {
366             fProperties.put(DTD_SCANNER, fDTDScanner);
367             if (fDTDScanner instanceof XMLComponent) {
368                 addComponent((XMLComponent)fDTDScanner);
369             }
370         }
371
372         fDatatypeValidatorFactory = createDatatypeValidatorFactory();
373         if (fDatatypeValidatorFactory != null) {
374             fProperties.put(DATATYPE_VALIDATOR_FACTORY,
375                         fDatatypeValidatorFactory);
376         }
377         fValidationManager = createValidationManager();
378
379         if (fValidationManager != null) {
380             fProperties.put(VALIDATION_MANAGER, fValidationManager);
381         }
382         // add message formatters
383
if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) {
384             XMLMessageFormatter xmft = new XMLMessageFormatter();
385             fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft);
386             fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft);
387         }
388         
389         fConfigUpdated = false;
390
391         // set locale
392
try {
393             setLocale(Locale.getDefault());
394         }
395         catch (XNIException e) {
396             // do nothing
397
// REVISIT: What is the right thing to do? -Ac
398
}
399
400     } // <init>(SymbolTable,XMLGrammarPool)
401

402     //
403
// Public methods
404
//
405
public void setFeature(String JavaDoc featureId, boolean state)
406         throws XMLConfigurationException {
407         fConfigUpdated = true;
408         super.setFeature(featureId, state);
409     }
410     
411     public void setProperty(String JavaDoc propertyId, Object JavaDoc value)
412         throws XMLConfigurationException {
413         fConfigUpdated = true;
414         super.setProperty(propertyId, value);
415         }
416     /**
417      * Set the locale to use for messages.
418      *
419      * @param locale The locale object to use for localization of messages.
420      *
421      * @exception XNIException Thrown if the parser does not support the
422      * specified locale.
423      */

424     public void setLocale(Locale JavaDoc locale) throws XNIException {
425         super.setLocale(locale);
426         fErrorReporter.setLocale(locale);
427     } // setLocale(Locale)
428

429     public boolean getFeature(String JavaDoc featureId)
430         throws XMLConfigurationException {
431             // make this feature special
432
if (featureId.equals(PARSER_SETTINGS)){
433             return fConfigUpdated;
434         }
435         return super.getFeature(featureId);
436
437     } // getFeature(String):boolean
438
//
439
// XMLPullParserConfiguration methods
440
//
441

442     // parsing
443

444     /**
445      * Sets the input source for the document to parse.
446      *
447      * @param inputSource The document's input source.
448      *
449      * @exception XMLConfigurationException Thrown if there is a
450      * configuration error when initializing the
451      * parser.
452      * @exception IOException Thrown on I/O error.
453      *
454      * @see #parse(boolean)
455      */

456     public void setInputSource(XMLInputSource inputSource)
457         throws XMLConfigurationException, IOException JavaDoc {
458         
459         // REVISIT: this method used to reset all the components and
460
// construct the pipeline. Now reset() is called
461
// in parse (boolean) just before we parse the document
462
// Should this method still throw exceptions..?
463

464         fInputSource = inputSource;
465         
466     } // setInputSource(XMLInputSource)
467

468     /**
469      * Parses the document in a pull parsing fashion.
470      *
471      * @param complete True if the pull parser should parse the
472      * remaining document completely.
473      *
474      * @return True if there is more document to parse.
475      *
476      * @exception XNIException Any XNI exception, possibly wrapping
477      * another exception.
478      * @exception IOException An IO exception from the parser, possibly
479      * from a byte stream or character stream
480      * supplied by the parser.
481      *
482      * @see #setInputSource
483      */

484     public boolean parse(boolean complete) throws XNIException, IOException JavaDoc {
485         //
486
// reset and configure pipeline and set InputSource.
487
if (fInputSource !=null) {
488             try {
489                 // resets and sets the pipeline.
490
reset();
491                 fScanner.setInputSource(fInputSource);
492                 fInputSource = null;
493             }
494             catch (XNIException ex) {
495                 if (PRINT_EXCEPTION_STACK_TRACE)
496                     ex.printStackTrace();
497                 throw ex;
498             }
499             catch (IOException JavaDoc ex) {
500                 if (PRINT_EXCEPTION_STACK_TRACE)
501                     ex.printStackTrace();
502                 throw ex;
503             }
504             catch (RuntimeException JavaDoc ex) {
505                 if (PRINT_EXCEPTION_STACK_TRACE)
506                     ex.printStackTrace();
507                 throw ex;
508             }
509             catch (Exception JavaDoc ex) {
510                 if (PRINT_EXCEPTION_STACK_TRACE)
511                     ex.printStackTrace();
512                 throw new XNIException(ex);
513             }
514         }
515
516         try {
517             return fScanner.scanDocument(complete);
518         }
519         catch (XNIException ex) {
520             if (PRINT_EXCEPTION_STACK_TRACE)
521                 ex.printStackTrace();
522             throw ex;
523         }
524         catch (IOException JavaDoc ex) {
525             if (PRINT_EXCEPTION_STACK_TRACE)
526                 ex.printStackTrace();
527             throw ex;
528         }
529         catch (RuntimeException JavaDoc ex) {
530             if (PRINT_EXCEPTION_STACK_TRACE)
531                 ex.printStackTrace();
532             throw ex;
533         }
534         catch (Exception JavaDoc ex) {
535             if (PRINT_EXCEPTION_STACK_TRACE)
536                 ex.printStackTrace();
537             throw new XNIException(ex);
538         }
539
540     } // parse(boolean):boolean
541

542     /**
543      * If the application decides to terminate parsing before the xml document
544      * is fully parsed, the application should call this method to free any
545      * resource allocated during parsing. For example, close all opened streams.
546      */

547     public void cleanup() {
548         fEntityManager.closeReaders();
549     }
550     
551     //
552
// XMLParserConfiguration methods
553
//
554

555     /**
556      * Parses the specified input source.
557      *
558      * @param source The input source.
559      *
560      * @exception XNIException Throws exception on XNI error.
561      * @exception java.io.IOException Throws exception on i/o error.
562      */

563     public void parse(XMLInputSource source) throws XNIException, IOException JavaDoc {
564
565         if (fParseInProgress) {
566             // REVISIT - need to add new error message
567
throw new XNIException("FWK005 parse may not be called while parsing.");
568         }
569         fParseInProgress = true;
570
571         try {
572             setInputSource(source);
573             parse(true);
574         }
575         catch (XNIException ex) {
576             if (PRINT_EXCEPTION_STACK_TRACE)
577                 ex.printStackTrace();
578             throw ex;
579         }
580         catch (IOException JavaDoc ex) {
581             if (PRINT_EXCEPTION_STACK_TRACE)
582                 ex.printStackTrace();
583             throw ex;
584         }
585         catch (RuntimeException JavaDoc ex) {
586             if (PRINT_EXCEPTION_STACK_TRACE)
587                 ex.printStackTrace();
588             throw ex;
589         }
590         catch (Exception JavaDoc ex) {
591             if (PRINT_EXCEPTION_STACK_TRACE)
592                 ex.printStackTrace();
593             throw new XNIException(ex);
594         }
595         finally {
596             fParseInProgress = false;
597             // close all streams opened by xerces
598
this.cleanup();
599         }
600
601     } // parse(InputSource)
602

603     //
604
// Protected methods
605
//
606

607     /**
608      * Reset all components before parsing.
609      *
610      * @throws XNIException Thrown if an error occurs during initialization.
611      */

612     protected void reset() throws XNIException {
613
614         if (fValidationManager != null)
615             fValidationManager.reset();
616         // configure the pipeline and initialize the components
617
configurePipeline();
618         super.reset();
619
620     } // reset()
621

622     /** Configures the pipeline. */
623     protected void configurePipeline() {
624         // create appropriate scanner
625
// and register it as one of the components.
626
if (fFeatures.get(NAMESPACES) == Boolean.TRUE) {
627             if (fNamespaceScanner == null) {
628                 fNamespaceScanner = new XMLNSDocumentScannerImpl();
629                 addComponent((XMLComponent)fNamespaceScanner);
630             }
631             fProperties.put(DOCUMENT_SCANNER, fNamespaceScanner);
632             fNamespaceScanner.setDTDValidator(null);
633             fScanner = fNamespaceScanner;
634         }
635         else {
636             if (fNonNSScanner == null) {
637                 fNonNSScanner = new XMLDocumentScannerImpl();
638                 addComponent((XMLComponent)fNonNSScanner);
639             }
640             fProperties.put(DOCUMENT_SCANNER, fNonNSScanner);
641             fScanner = fNonNSScanner;
642         }
643
644         fScanner.setDocumentHandler(fDocumentHandler);
645         fLastComponent = fScanner;
646         // setup dtd pipeline
647
if (fDTDScanner != null) {
648                 fDTDScanner.setDTDHandler(fDTDHandler);
649                 fDTDScanner.setDTDContentModelHandler(fDTDContentModelHandler);
650         }
651
652
653     } // configurePipeline()
654

655     // features and properties
656

657     /**
658      * Check a feature. If feature is know and supported, this method simply
659      * returns. Otherwise, the appropriate exception is thrown.
660      *
661      * @param featureId The unique identifier (URI) of the feature.
662      *
663      * @throws XMLConfigurationException Thrown for configuration error.
664      * In general, components should
665      * only throw this exception if
666      * it is <strong>really</strong>
667      * a critical error.
668      */

669     protected void checkFeature(String JavaDoc featureId)
670         throws XMLConfigurationException {
671
672         //
673
// Xerces Features
674
//
675

676         if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
677             final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
678             
679             //
680
// http://apache.org/xml/features/validation/dynamic
681
// Allows the parser to validate a document only when it
682
// contains a grammar. Validation is turned on/off based
683
// on each document instance, automatically.
684
//
685
if (suffixLength == Constants.DYNAMIC_VALIDATION_FEATURE.length() &&
686                 featureId.endsWith(Constants.DYNAMIC_VALIDATION_FEATURE)) {
687                 return;
688             }
689             //
690
// http://apache.org/xml/features/validation/default-attribute-values
691
//
692
if (suffixLength == Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE.length() &&
693                 featureId.endsWith(Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE)) {
694                 // REVISIT
695
short type = XMLConfigurationException.NOT_SUPPORTED;
696                 throw new XMLConfigurationException(type, featureId);
697             }
698             //
699
// http://apache.org/xml/features/validation/default-attribute-values
700
//
701
if (suffixLength == Constants.VALIDATE_CONTENT_MODELS_FEATURE.length() &&
702                 featureId.endsWith(Constants.VALIDATE_CONTENT_MODELS_FEATURE)) {
703                 // REVISIT
704
short type = XMLConfigurationException.NOT_SUPPORTED;
705                 throw new XMLConfigurationException(type, featureId);
706             }
707             //
708
// http://apache.org/xml/features/validation/nonvalidating/load-dtd-grammar
709
//
710
if (suffixLength == Constants.LOAD_DTD_GRAMMAR_FEATURE.length() &&
711                 featureId.endsWith(Constants.LOAD_DTD_GRAMMAR_FEATURE)) {
712                 return;
713             }
714             //
715
// http://apache.org/xml/features/validation/nonvalidating/load-external-dtd
716
//
717
if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() &&
718                 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) {
719                 return;
720             }
721
722             //
723
// http://apache.org/xml/features/validation/default-attribute-values
724
//
725
if (suffixLength == Constants.VALIDATE_DATATYPES_FEATURE.length() &&
726                 featureId.endsWith(Constants.VALIDATE_DATATYPES_FEATURE)) {
727                 short type = XMLConfigurationException.NOT_SUPPORTED;
728                 throw new XMLConfigurationException(type, featureId);
729             }
730         }
731
732         //
733
// Not recognized
734
//
735

736         super.checkFeature(featureId);
737
738     } // checkFeature(String)
739

740     /**
741      * Check a property. If the property is know and supported, this method
742      * simply returns. Otherwise, the appropriate exception is thrown.
743      *
744      * @param propertyId The unique identifier (URI) of the property
745      * being set.
746      *
747      * @throws XMLConfigurationException Thrown for configuration error.
748      * In general, components should
749      * only throw this exception if
750      * it is <strong>really</strong>
751      * a critical error.
752      */

753     protected void checkProperty(String JavaDoc propertyId)
754         throws XMLConfigurationException {
755
756         //
757
// Xerces Properties
758
//
759

760         if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
761             final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length();
762             
763             if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() &&
764                 propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) {
765                 return;
766             }
767         }
768
769         if (propertyId.startsWith(Constants.JAXP_PROPERTY_PREFIX)) {
770             final int suffixLength = propertyId.length() - Constants.JAXP_PROPERTY_PREFIX.length();
771
772             if (suffixLength == Constants.SCHEMA_SOURCE.length() &&
773                 propertyId.endsWith(Constants.SCHEMA_SOURCE)) {
774                 return;
775             }
776         }
777
778         //
779
// Not recognized
780
//
781

782         super.checkProperty(propertyId);
783
784     } // checkProperty(String)
785

786     // factory methods
787

788     /** Creates an entity manager. */
789     protected XMLEntityManager createEntityManager() {
790         return new XMLEntityManager();
791     } // createEntityManager():XMLEntityManager
792

793     /** Creates an error reporter. */
794     protected XMLErrorReporter createErrorReporter() {
795         return new XMLErrorReporter();
796     } // createErrorReporter():XMLErrorReporter
797

798     /** Create a document scanner. */
799     protected XMLDocumentScanner createDocumentScanner() {
800         return null;
801     } // createDocumentScanner():XMLDocumentScanner
802

803     /** Create a DTD scanner. */
804     protected XMLDTDScanner createDTDScanner() {
805         return new XMLDTDScannerImpl();
806     } // createDTDScanner():XMLDTDScanner
807

808     /** Create a datatype validator factory. */
809     protected DTDDVFactory createDatatypeValidatorFactory() {
810         return DTDDVFactory.getInstance();
811     } // createDatatypeValidatorFactory():DatatypeValidatorFactory
812
protected ValidationManager createValidationManager(){
813         return new ValidationManager();
814     }
815
816 } // class NonValidatingConfiguration
817
Popular Tags