KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import org.apache.xerces.impl.Constants;
23 import org.apache.xerces.impl.XMLDTDScannerImpl;
24 import org.apache.xerces.impl.XMLDocumentScannerImpl;
25 import org.apache.xerces.impl.XMLEntityManager;
26 import org.apache.xerces.impl.XMLErrorReporter;
27 import org.apache.xerces.impl.XMLNamespaceBinder;
28 import org.apache.xerces.impl.dtd.XMLDTDProcessor;
29 import org.apache.xerces.impl.dtd.XMLDTDValidator;
30 import org.apache.xerces.impl.dv.DTDDVFactory;
31 import org.apache.xerces.impl.msg.XMLMessageFormatter;
32 import org.apache.xerces.impl.validation.ValidationManager;
33 import org.apache.xerces.util.SymbolTable;
34 import org.apache.xerces.xni.XMLLocator;
35 import org.apache.xerces.xni.XNIException;
36 import org.apache.xerces.xni.grammars.XMLGrammarPool;
37 import org.apache.xerces.xni.parser.XMLComponent;
38 import org.apache.xerces.xni.parser.XMLComponentManager;
39 import org.apache.xerces.xni.parser.XMLConfigurationException;
40 import org.apache.xerces.xni.parser.XMLDTDScanner;
41 import org.apache.xerces.xni.parser.XMLDocumentScanner;
42 import org.apache.xerces.xni.parser.XMLInputSource;
43 import org.apache.xerces.xni.parser.XMLPullParserConfiguration;
44
45 /**
46  * This is the DTD-only parser configuration. It extends the basic
47  * configuration with a standard set of parser components appropriate
48  * to DTD-centric validation. Since
49  * the Xerces2 reference implementation document and DTD scanner
50  * implementations are capable of acting as pull parsers, this
51  * configuration implements the
52  * <code>XMLPullParserConfiguration</code> interface.
53  * <p>
54  * In addition to the features and properties recognized by the base
55  * parser configuration, this class recognizes these additional
56  * features and properties:
57  * <ul>
58  * <li>Features
59  * <ul>
60  * <li>http://apache.org/xml/features/validation/warn-on-duplicate-attdef</li>
61  * <li>http://apache.org/xml/features/validation/warn-on-undeclared-elemdef</li>
62  * <li>http://apache.org/xml/features/allow-java-encodings</li>
63  * <li>http://apache.org/xml/features/continue-after-fatal-error</li>
64  * <li>http://apache.org/xml/features/load-external-dtd</li>
65  * </ul>
66  * <li>Properties
67  * <ul>
68  * <li>http://apache.org/xml/properties/internal/error-reporter</li>
69  * <li>http://apache.org/xml/properties/internal/entity-manager</li>
70  * <li>http://apache.org/xml/properties/internal/document-scanner</li>
71  * <li>http://apache.org/xml/properties/internal/dtd-scanner</li>
72  * <li>http://apache.org/xml/properties/internal/grammar-pool</li>
73  * <li>http://apache.org/xml/properties/internal/validator/dtd</li>
74  * <li>http://apache.org/xml/properties/internal/datatype-validator-factory</li>
75  * </ul>
76  * </ul>
77  *
78  * @author Arnaud Le Hors, IBM
79  * @author Andy Clark, IBM
80  * @author Neil Graham, IBM
81  *
82  * @version $Id: DTDConfiguration.java,v 1.18 2004/02/24 23:15:57 mrglavas Exp $
83  */

84 public class DTDConfiguration
85     extends BasicParserConfiguration
86     implements XMLPullParserConfiguration {
87
88     //
89
// Constants
90
//
91

92     // feature identifiers
93

94     /** Feature identifier: warn on duplicate attribute definition. */
95     protected static final String JavaDoc WARN_ON_DUPLICATE_ATTDEF =
96         Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE;
97
98     /** Feature identifier: warn on duplicate entity definition. */
99     protected static final String JavaDoc WARN_ON_DUPLICATE_ENTITYDEF =
100         Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE;
101     
102     /** Feature identifier: warn on undeclared element definition. */
103     protected static final String JavaDoc WARN_ON_UNDECLARED_ELEMDEF =
104         Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE;
105     
106     /** Feature identifier: allow Java encodings. */
107     protected static final String JavaDoc ALLOW_JAVA_ENCODINGS =
108         Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE;
109     
110     /** Feature identifier: continue after fatal error. */
111     protected static final String JavaDoc CONTINUE_AFTER_FATAL_ERROR =
112         Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE;
113
114     /** Feature identifier: load external DTD. */
115     protected static final String JavaDoc LOAD_EXTERNAL_DTD =
116         Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE;
117
118     /** Feature identifier: notify built-in refereces. */
119     protected static final String JavaDoc NOTIFY_BUILTIN_REFS =
120         Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_BUILTIN_REFS_FEATURE;
121     
122     /** Feature identifier: notify character refereces. */
123     protected static final String JavaDoc NOTIFY_CHAR_REFS =
124         Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_CHAR_REFS_FEATURE;
125     
126
127     // property identifiers
128

129     /** Property identifier: error reporter. */
130     protected static final String JavaDoc ERROR_REPORTER =
131         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
132
133     /** Property identifier: entity manager. */
134     protected static final String JavaDoc ENTITY_MANAGER =
135         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
136     
137     /** Property identifier document scanner: */
138     protected static final String JavaDoc DOCUMENT_SCANNER =
139         Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_SCANNER_PROPERTY;
140
141     /** Property identifier: DTD scanner. */
142     protected static final String JavaDoc DTD_SCANNER =
143         Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_SCANNER_PROPERTY;
144
145     /** Property identifier: grammar pool. */
146     protected static final String JavaDoc XMLGRAMMAR_POOL =
147         Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
148     
149     /** Property identifier: DTD loader. */
150     protected static final String JavaDoc DTD_PROCESSOR =
151         Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_PROCESSOR_PROPERTY;
152
153     /** Property identifier: DTD validator. */
154     protected static final String JavaDoc DTD_VALIDATOR =
155         Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_VALIDATOR_PROPERTY;
156
157     /** Property identifier: namespace binder. */
158     protected static final String JavaDoc NAMESPACE_BINDER =
159         Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_BINDER_PROPERTY;
160
161     /** Property identifier: datatype validator factory. */
162     protected static final String JavaDoc DATATYPE_VALIDATOR_FACTORY =
163         Constants.XERCES_PROPERTY_PREFIX + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY;
164
165     protected static final String JavaDoc VALIDATION_MANAGER =
166         Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY;
167     
168     /** Property identifier: JAXP schema language / DOM schema-type. */
169     protected static final String JavaDoc JAXP_SCHEMA_LANGUAGE =
170     Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE;
171
172     /** Property identifier: JAXP schema source/ DOM schema-location. */
173     protected static final String JavaDoc JAXP_SCHEMA_SOURCE =
174     Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE;
175
176
177     // debugging
178

179     /** Set to true and recompile to print exception stack trace. */
180     protected static final boolean PRINT_EXCEPTION_STACK_TRACE = false;
181
182     //
183
// Data
184
//
185

186     // components (non-configurable)
187

188     /** Grammar pool. */
189     protected XMLGrammarPool fGrammarPool;
190
191     /** Datatype validator factory. */
192     protected DTDDVFactory fDatatypeValidatorFactory;
193
194     // components (configurable)
195

196     /** Error reporter. */
197     protected XMLErrorReporter fErrorReporter;
198
199     /** Entity manager. */
200     protected XMLEntityManager fEntityManager;
201
202     /** Document scanner. */
203     protected XMLDocumentScanner fScanner;
204
205     /** Input Source */
206     protected XMLInputSource fInputSource;
207
208     /** DTD scanner. */
209     protected XMLDTDScanner fDTDScanner;
210
211     /** DTD Processor . */
212     protected XMLDTDProcessor fDTDProcessor;
213
214     /** DTD Validator. */
215     protected XMLDTDValidator fDTDValidator;
216
217     /** Namespace binder. */
218     protected XMLNamespaceBinder fNamespaceBinder;
219
220     protected ValidationManager fValidationManager;
221     // state
222

223     /** Locator */
224     protected XMLLocator fLocator;
225
226     /**
227      * True if a parse is in progress. This state is needed because
228      * some features/properties cannot be set while parsing (e.g.
229      * validation and namespaces).
230      */

231     protected boolean fParseInProgress = false;
232
233     //
234
// Constructors
235
//
236

237     /** Default constructor. */
238     public DTDConfiguration() {
239         this(null, null, null);
240     } // <init>()
241

242     /**
243      * Constructs a parser configuration using the specified symbol table.
244      *
245      * @param symbolTable The symbol table to use.
246      */

247     public DTDConfiguration(SymbolTable symbolTable) {
248         this(symbolTable, null, null);
249     } // <init>(SymbolTable)
250

251     /**
252      * Constructs a parser configuration using the specified symbol table and
253      * grammar pool.
254      * <p>
255      * <strong>REVISIT:</strong>
256      * Grammar pool will be updated when the new validation engine is
257      * implemented.
258      *
259      * @param symbolTable The symbol table to use.
260      * @param grammarPool The grammar pool to use.
261      */

262     public DTDConfiguration(SymbolTable symbolTable,
263                                        XMLGrammarPool grammarPool) {
264         this(symbolTable, grammarPool, null);
265     } // <init>(SymbolTable,XMLGrammarPool)
266

267     /**
268      * Constructs a parser configuration using the specified symbol table,
269      * grammar pool, and parent settings.
270      * <p>
271      * <strong>REVISIT:</strong>
272      * Grammar pool will be updated when the new validation engine is
273      * implemented.
274      *
275      * @param symbolTable The symbol table to use.
276      * @param grammarPool The grammar pool to use.
277      * @param parentSettings The parent settings.
278      */

279     public DTDConfiguration(SymbolTable symbolTable,
280                                        XMLGrammarPool grammarPool,
281                                        XMLComponentManager parentSettings) {
282         super(symbolTable, parentSettings);
283
284         // add default recognized features
285
final String JavaDoc[] recognizedFeatures = {
286             //WARN_ON_DUPLICATE_ATTDEF, // from XMLDTDScannerImpl
287
//WARN_ON_UNDECLARED_ELEMDEF, // from XMLDTDScannerImpl
288
//ALLOW_JAVA_ENCODINGS, // from XMLEntityManager
289
CONTINUE_AFTER_FATAL_ERROR,
290             LOAD_EXTERNAL_DTD, // from XMLDTDScannerImpl
291
//NOTIFY_BUILTIN_REFS, // from XMLDocumentFragmentScannerImpl
292
//NOTIFY_CHAR_REFS, // from XMLDocumentFragmentScannerImpl
293
//WARN_ON_DUPLICATE_ENTITYDEF, // from XMLEntityManager
294
};
295         addRecognizedFeatures(recognizedFeatures);
296
297         // set state for default features
298
//setFeature(WARN_ON_DUPLICATE_ATTDEF, false); // from XMLDTDScannerImpl
299
//setFeature(WARN_ON_UNDECLARED_ELEMDEF, false); // from XMLDTDScannerImpl
300
//setFeature(ALLOW_JAVA_ENCODINGS, false); // from XMLEntityManager
301
setFeature(CONTINUE_AFTER_FATAL_ERROR, false);
302         setFeature(LOAD_EXTERNAL_DTD, true); // from XMLDTDScannerImpl
303
//setFeature(NOTIFY_BUILTIN_REFS, false); // from XMLDocumentFragmentScannerImpl
304
//setFeature(NOTIFY_CHAR_REFS, false); // from XMLDocumentFragmentScannerImpl
305
//setFeature(WARN_ON_DUPLICATE_ENTITYDEF, false); // from XMLEntityManager
306

307         // add default recognized properties
308
final String JavaDoc[] recognizedProperties = {
309             ERROR_REPORTER,
310             ENTITY_MANAGER,
311             DOCUMENT_SCANNER,
312             DTD_SCANNER,
313             DTD_PROCESSOR,
314             DTD_VALIDATOR,
315             NAMESPACE_BINDER,
316             XMLGRAMMAR_POOL,
317             DATATYPE_VALIDATOR_FACTORY,
318             VALIDATION_MANAGER,
319             JAXP_SCHEMA_SOURCE,
320             JAXP_SCHEMA_LANGUAGE
321         };
322         addRecognizedProperties(recognizedProperties);
323
324         fGrammarPool = grammarPool;
325         if(fGrammarPool != null){
326             setProperty(XMLGRAMMAR_POOL, fGrammarPool);
327         }
328
329         fEntityManager = createEntityManager();
330         setProperty(ENTITY_MANAGER, fEntityManager);
331         addComponent(fEntityManager);
332
333         fErrorReporter = createErrorReporter();
334         fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner());
335         setProperty(ERROR_REPORTER, fErrorReporter);
336         addComponent(fErrorReporter);
337
338         fScanner = createDocumentScanner();
339         setProperty(DOCUMENT_SCANNER, fScanner);
340         if (fScanner instanceof XMLComponent) {
341             addComponent((XMLComponent)fScanner);
342         }
343
344         fDTDScanner = createDTDScanner();
345         if (fDTDScanner != null) {
346             setProperty(DTD_SCANNER, fDTDScanner);
347             if (fDTDScanner instanceof XMLComponent) {
348                 addComponent((XMLComponent)fDTDScanner);
349             }
350         }
351
352         fDTDProcessor = createDTDProcessor();
353         if (fDTDProcessor != null) {
354             setProperty(DTD_PROCESSOR, fDTDProcessor);
355             if (fDTDProcessor instanceof XMLComponent) {
356                 addComponent((XMLComponent)fDTDProcessor);
357             }
358         }
359
360         fDTDValidator = createDTDValidator();
361         if (fDTDValidator != null) {
362             setProperty(DTD_VALIDATOR, fDTDValidator);
363             addComponent(fDTDValidator);
364         }
365
366         fNamespaceBinder = createNamespaceBinder();
367         if (fNamespaceBinder != null) {
368             setProperty(NAMESPACE_BINDER, fNamespaceBinder);
369             addComponent(fNamespaceBinder);
370         }
371         
372         fDatatypeValidatorFactory = createDatatypeValidatorFactory();
373         if (fDatatypeValidatorFactory != null) {
374             setProperty(DATATYPE_VALIDATOR_FACTORY,
375                         fDatatypeValidatorFactory);
376         }
377         fValidationManager = createValidationManager();
378
379         if (fValidationManager != null) {
380             setProperty (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         // set locale
390
try {
391             setLocale(Locale.getDefault());
392         }
393         catch (XNIException e) {
394             // do nothing
395
// REVISIT: What is the right thing to do? -Ac
396
}
397
398     } // <init>(SymbolTable,XMLGrammarPool)
399

400     //
401
// Public methods
402
//
403

404     /**
405      * Set the locale to use for messages.
406      *
407      * @param locale The locale object to use for localization of messages.
408      *
409      * @exception XNIException Thrown if the parser does not support the
410      * specified locale.
411      */

412     public void setLocale(Locale JavaDoc locale) throws XNIException {
413         super.setLocale(locale);
414         fErrorReporter.setLocale(locale);
415     } // setLocale(Locale)
416

417     //
418
// XMLPullParserConfiguration methods
419
//
420

421     // parsing
422

423     /**
424      * Sets the input source for the document to parse.
425      *
426      * @param inputSource The document's input source.
427      *
428      * @exception XMLConfigurationException Thrown if there is a
429      * configuration error when initializing the
430      * parser.
431      * @exception IOException Thrown on I/O error.
432      *
433      * @see #parse(boolean)
434      */

435     public void setInputSource(XMLInputSource inputSource)
436         throws XMLConfigurationException, IOException JavaDoc {
437         
438         // REVISIT: this method used to reset all the components and
439
// construct the pipeline. Now reset() is called
440
// in parse (boolean) just before we parse the document
441
// Should this method still throw exceptions..?
442

443         fInputSource = inputSource;
444         
445     } // setInputSource(XMLInputSource)
446

447     /**
448      * Parses the document in a pull parsing fashion.
449      *
450      * @param complete True if the pull parser should parse the
451      * remaining document completely.
452      *
453      * @return True if there is more document to parse.
454      *
455      * @exception XNIException Any XNI exception, possibly wrapping
456      * another exception.
457      * @exception IOException An IO exception from the parser, possibly
458      * from a byte stream or character stream
459      * supplied by the parser.
460      *
461      * @see #setInputSource
462      */

463     public boolean parse(boolean complete) throws XNIException, IOException JavaDoc {
464         //
465
// reset and configure pipeline and set InputSource.
466
if (fInputSource !=null) {
467             try {
468                 // resets and sets the pipeline.
469
reset();
470                 fScanner.setInputSource(fInputSource);
471                 fInputSource = null;
472             }
473             catch (XNIException ex) {
474                 if (PRINT_EXCEPTION_STACK_TRACE)
475                     ex.printStackTrace();
476                 throw ex;
477             }
478             catch (IOException JavaDoc ex) {
479                 if (PRINT_EXCEPTION_STACK_TRACE)
480                     ex.printStackTrace();
481                 throw ex;
482             }
483             catch (RuntimeException JavaDoc ex) {
484                 if (PRINT_EXCEPTION_STACK_TRACE)
485                     ex.printStackTrace();
486                 throw ex;
487             }
488             catch (Exception JavaDoc ex) {
489                 if (PRINT_EXCEPTION_STACK_TRACE)
490                     ex.printStackTrace();
491                 throw new XNIException(ex);
492             }
493         }
494
495         try {
496             return fScanner.scanDocument(complete);
497         }
498         catch (XNIException ex) {
499             if (PRINT_EXCEPTION_STACK_TRACE)
500                 ex.printStackTrace();
501             throw ex;
502         }
503         catch (IOException JavaDoc ex) {
504             if (PRINT_EXCEPTION_STACK_TRACE)
505                 ex.printStackTrace();
506             throw ex;
507         }
508         catch (RuntimeException JavaDoc ex) {
509             if (PRINT_EXCEPTION_STACK_TRACE)
510                 ex.printStackTrace();
511             throw ex;
512         }
513         catch (Exception JavaDoc ex) {
514             if (PRINT_EXCEPTION_STACK_TRACE)
515                 ex.printStackTrace();
516             throw new XNIException(ex);
517         }
518
519     } // parse(boolean):boolean
520

521     /**
522      * If the application decides to terminate parsing before the xml document
523      * is fully parsed, the application should call this method to free any
524      * resource allocated during parsing. For example, close all opened streams.
525      */

526     public void cleanup() {
527         fEntityManager.closeReaders();
528     }
529     
530     //
531
// XMLParserConfiguration methods
532
//
533

534     /**
535      * Parses the specified input source.
536      *
537      * @param source The input source.
538      *
539      * @exception XNIException Throws exception on XNI error.
540      * @exception java.io.IOException Throws exception on i/o error.
541      */

542     public void parse(XMLInputSource source) throws XNIException, IOException JavaDoc {
543
544         if (fParseInProgress) {
545             // REVISIT - need to add new error message
546
throw new XNIException("FWK005 parse may not be called while parsing.");
547         }
548         fParseInProgress = true;
549
550         try {
551             setInputSource(source);
552             parse(true);
553         }
554         catch (XNIException ex) {
555             if (PRINT_EXCEPTION_STACK_TRACE)
556                 ex.printStackTrace();
557             throw ex;
558         }
559         catch (IOException JavaDoc ex) {
560             if (PRINT_EXCEPTION_STACK_TRACE)
561                 ex.printStackTrace();
562             throw ex;
563         }
564         catch (RuntimeException JavaDoc ex) {
565             if (PRINT_EXCEPTION_STACK_TRACE)
566                 ex.printStackTrace();
567             throw ex;
568         }
569         catch (Exception JavaDoc ex) {
570             if (PRINT_EXCEPTION_STACK_TRACE)
571                 ex.printStackTrace();
572             throw new XNIException(ex);
573         }
574         finally {
575             fParseInProgress = false;
576             // close all streams opened by xerces
577
this.cleanup();
578         }
579
580     } // parse(InputSource)
581

582     //
583
// Protected methods
584
//
585

586     /**
587      * Reset all components before parsing.
588      *
589      * @throws XNIException Thrown if an error occurs during initialization.
590      */

591     protected void reset() throws XNIException {
592
593         if (fValidationManager != null)
594             fValidationManager.reset();
595         // configure the pipeline and initialize the components
596
configurePipeline();
597         super.reset();
598     } // reset()
599

600     /** Configures the pipeline. */
601     protected void configurePipeline() {
602
603         // REVISIT: This should be better designed. In other words, we
604
// need to figure out what is the best way for people to
605
// re-use *most* of the standard configuration but do
606
// things common things such as remove a component (e.g.
607
// the validator), insert a new component (e.g. XInclude),
608
// etc... -Ac
609

610         // setup document pipeline
611
if (fDTDValidator != null) {
612             fScanner.setDocumentHandler(fDTDValidator);
613             if (fFeatures.get(NAMESPACES) == Boolean.TRUE) {
614
615                 // filters
616
fDTDValidator.setDocumentHandler(fNamespaceBinder);
617                 fDTDValidator.setDocumentSource(fScanner);
618                 fNamespaceBinder.setDocumentHandler(fDocumentHandler);
619                 fNamespaceBinder.setDocumentSource(fDTDValidator);
620                 fLastComponent = fNamespaceBinder;
621             }
622             else {
623                 fDTDValidator.setDocumentHandler(fDocumentHandler);
624                 fDTDValidator.setDocumentSource(fScanner);
625                 fLastComponent = fDTDValidator;
626             }
627         }
628         else {
629             if (fFeatures.get(NAMESPACES) == Boolean.TRUE) {
630                 fScanner.setDocumentHandler(fNamespaceBinder);
631                 fNamespaceBinder.setDocumentHandler(fDocumentHandler);
632                 fNamespaceBinder.setDocumentSource(fScanner);
633                 fLastComponent = fNamespaceBinder;
634             }
635             else {
636                 fScanner.setDocumentHandler(fDocumentHandler);
637                 fLastComponent = fScanner;
638             }
639         }
640         
641         configureDTDPipeline();
642     } // configurePipeline()
643

644     protected void configureDTDPipeline (){
645         
646         // setup dtd pipeline
647
if (fDTDScanner != null) {
648             fProperties.put(DTD_SCANNER, fDTDScanner);
649             if (fDTDProcessor != null) {
650                 fProperties.put(DTD_PROCESSOR, fDTDProcessor);
651                 fDTDScanner.setDTDHandler(fDTDProcessor);
652                 fDTDProcessor.setDTDSource(fDTDScanner);
653                 fDTDProcessor.setDTDHandler(fDTDHandler);
654                 if (fDTDHandler != null) {
655                     fDTDHandler.setDTDSource(fDTDProcessor);
656                 }
657
658                 fDTDScanner.setDTDContentModelHandler(fDTDProcessor);
659                 fDTDProcessor.setDTDContentModelSource(fDTDScanner);
660                 fDTDProcessor.setDTDContentModelHandler(fDTDContentModelHandler);
661                 if (fDTDContentModelHandler != null) {
662                     fDTDContentModelHandler.setDTDContentModelSource(fDTDProcessor);
663                 }
664             }
665             else {
666                 fDTDScanner.setDTDHandler(fDTDHandler);
667                 if (fDTDHandler != null) {
668                     fDTDHandler.setDTDSource(fDTDScanner);
669                 }
670                 fDTDScanner.setDTDContentModelHandler(fDTDContentModelHandler);
671                 if (fDTDContentModelHandler != null) {
672                     fDTDContentModelHandler.setDTDContentModelSource(fDTDScanner);
673                 }
674             }
675         }
676
677
678     }
679
680     // features and properties
681

682     /**
683      * Check a feature. If feature is know and supported, this method simply
684      * returns. Otherwise, the appropriate exception is thrown.
685      *
686      * @param featureId The unique identifier (URI) of the feature.
687      *
688      * @throws XMLConfigurationException Thrown for configuration error.
689      * In general, components should
690      * only throw this exception if
691      * it is <strong>really</strong>
692      * a critical error.
693      */

694     protected void checkFeature(String JavaDoc featureId)
695         throws XMLConfigurationException {
696
697         //
698
// Xerces Features
699
//
700

701         if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
702             final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
703             
704             //
705
// http://apache.org/xml/features/validation/dynamic
706
// Allows the parser to validate a document only when it
707
// contains a grammar. Validation is turned on/off based
708
// on each document instance, automatically.
709
//
710
if (suffixLength == Constants.DYNAMIC_VALIDATION_FEATURE.length() &&
711                 featureId.endsWith(Constants.DYNAMIC_VALIDATION_FEATURE)) {
712                 return;
713             }
714
715             //
716
// http://apache.org/xml/features/validation/default-attribute-values
717
//
718
if (suffixLength == Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE.length() &&
719                 featureId.endsWith(Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE)) {
720                 // REVISIT
721
short type = XMLConfigurationException.NOT_SUPPORTED;
722                 throw new XMLConfigurationException(type, featureId);
723             }
724             //
725
// http://apache.org/xml/features/validation/default-attribute-values
726
//
727
if (suffixLength == Constants.VALIDATE_CONTENT_MODELS_FEATURE.length() &&
728                 featureId.endsWith(Constants.VALIDATE_CONTENT_MODELS_FEATURE)) {
729                 // REVISIT
730
short type = XMLConfigurationException.NOT_SUPPORTED;
731                 throw new XMLConfigurationException(type, featureId);
732             }
733             //
734
// http://apache.org/xml/features/validation/nonvalidating/load-dtd-grammar
735
//
736
if (suffixLength == Constants.LOAD_DTD_GRAMMAR_FEATURE.length() &&
737                 featureId.endsWith(Constants.LOAD_DTD_GRAMMAR_FEATURE)) {
738                 return;
739             }
740             //
741
// http://apache.org/xml/features/validation/nonvalidating/load-external-dtd
742
//
743
if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() &&
744                 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) {
745                 return;
746             }
747
748             //
749
// http://apache.org/xml/features/validation/default-attribute-values
750
//
751
if (suffixLength == Constants.VALIDATE_DATATYPES_FEATURE.length() &&
752                 featureId.endsWith(Constants.VALIDATE_DATATYPES_FEATURE)) {
753                 short type = XMLConfigurationException.NOT_SUPPORTED;
754                 throw new XMLConfigurationException(type, featureId);
755             }
756         }
757
758         //
759
// Not recognized
760
//
761

762         super.checkFeature(featureId);
763
764     } // checkFeature(String)
765

766     /**
767      * Check a property. If the property is know and supported, this method
768      * simply returns. Otherwise, the appropriate exception is thrown.
769      *
770      * @param propertyId The unique identifier (URI) of the property
771      * being set.
772      *
773      * @throws XMLConfigurationException Thrown for configuration error.
774      * In general, components should
775      * only throw this exception if
776      * it is <strong>really</strong>
777      * a critical error.
778      */

779     protected void checkProperty(String JavaDoc propertyId)
780         throws XMLConfigurationException {
781
782         //
783
// Xerces Properties
784
//
785

786         if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
787             final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length();
788
789             if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() &&
790                 propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) {
791                 return;
792             }
793         }
794
795         //
796
// Not recognized
797
//
798

799         super.checkProperty(propertyId);
800
801     } // checkProperty(String)
802

803     // factory methods
804

805     /** Creates an entity manager. */
806     protected XMLEntityManager createEntityManager() {
807         return new XMLEntityManager();
808     } // createEntityManager():XMLEntityManager
809

810     /** Creates an error reporter. */
811     protected XMLErrorReporter createErrorReporter() {
812         return new XMLErrorReporter();
813     } // createErrorReporter():XMLErrorReporter
814

815     /** Create a document scanner. */
816     protected XMLDocumentScanner createDocumentScanner() {
817         return new XMLDocumentScannerImpl();
818     } // createDocumentScanner():XMLDocumentScanner
819

820     /** Create a DTD scanner. */
821     protected XMLDTDScanner createDTDScanner() {
822         return new XMLDTDScannerImpl();
823     } // createDTDScanner():XMLDTDScanner
824

825     /** Create a DTD loader . */
826     protected XMLDTDProcessor createDTDProcessor() {
827         return new XMLDTDProcessor();
828     } // createDTDProcessor():XMLDTDProcessor
829

830     /** Create a DTD validator. */
831     protected XMLDTDValidator createDTDValidator() {
832         return new XMLDTDValidator();
833     } // createDTDValidator():XMLDTDValidator
834

835     /** Create a namespace binder. */
836     protected XMLNamespaceBinder createNamespaceBinder() {
837         return new XMLNamespaceBinder();
838     } // createNamespaceBinder():XMLNamespaceBinder
839

840     /** Create a datatype validator factory. */
841     protected DTDDVFactory createDatatypeValidatorFactory() {
842         return DTDDVFactory.getInstance();
843     } // createDatatypeValidatorFactory():DatatypeValidatorFactory
844
protected ValidationManager createValidationManager(){
845         return new ValidationManager();
846     }
847
848 } // class DTDConfiguration
849
Popular Tags