KickJava   Java API By Example, From Geeks To Geeks.

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


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.XMLNamespaceBinder;
69 import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDProcessor;
70 import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator;
71 import com.sun.org.apache.xerces.internal.impl.dv.DTDDVFactory;
72 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
73 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
74 import com.sun.org.apache.xerces.internal.util.SymbolTable;
75 import com.sun.org.apache.xerces.internal.xni.XMLLocator;
76 import com.sun.org.apache.xerces.internal.xni.XNIException;
77 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
78 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
79 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
80 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
81 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDScanner;
82 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentScanner;
83 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
84 import com.sun.org.apache.xerces.internal.xni.parser.XMLPullParserConfiguration;
85
86 /**
87  * This is the DTD-only parser configuration. It extends the basic
88  * configuration with a standard set of parser components appropriate
89  * to DTD-centric validation. Since
90  * the Xerces2 reference implementation document and DTD scanner
91  * implementations are capable of acting as pull parsers, this
92  * configuration implements the
93  * <code>XMLPullParserConfiguration</code> interface.
94  * <p>
95  * In addition to the features and properties recognized by the base
96  * parser configuration, this class recognizes these additional
97  * features and properties:
98  * <ul>
99  * <li>Features
100  * <ul>
101  * <li>http://apache.org/xml/features/validation/warn-on-duplicate-attdef</li>
102  * <li>http://apache.org/xml/features/validation/warn-on-undeclared-elemdef</li>
103  * <li>http://apache.org/xml/features/allow-java-encodings</li>
104  * <li>http://apache.org/xml/features/continue-after-fatal-error</li>
105  * <li>http://apache.org/xml/features/load-external-dtd</li>
106  * </ul>
107  * <li>Properties
108  * <ul>
109  * <li>http://apache.org/xml/properties/internal/error-reporter</li>
110  * <li>http://apache.org/xml/properties/internal/entity-manager</li>
111  * <li>http://apache.org/xml/properties/internal/document-scanner</li>
112  * <li>http://apache.org/xml/properties/internal/dtd-scanner</li>
113  * <li>http://apache.org/xml/properties/internal/grammar-pool</li>
114  * <li>http://apache.org/xml/properties/internal/validator/dtd</li>
115  * <li>http://apache.org/xml/properties/internal/datatype-validator-factory</li>
116  * </ul>
117  * </ul>
118  *
119  * @author Arnaud Le Hors, IBM
120  * @author Andy Clark, IBM
121  * @author Neil Graham, IBM
122  *
123  * @version $Id: DTDConfiguration.java,v 1.17 2004/01/26 17:28:10 mrglavas Exp $
124  */

125 public class DTDConfiguration
126     extends BasicParserConfiguration
127     implements XMLPullParserConfiguration {
128
129     //
130
// Constants
131
//
132

133     // feature identifiers
134

135     /** Feature identifier: warn on duplicate attribute definition. */
136     protected static final String JavaDoc WARN_ON_DUPLICATE_ATTDEF =
137         Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE;
138
139     /** Feature identifier: warn on duplicate entity definition. */
140     protected static final String JavaDoc WARN_ON_DUPLICATE_ENTITYDEF =
141         Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE;
142     
143     /** Feature identifier: warn on undeclared element definition. */
144     protected static final String JavaDoc WARN_ON_UNDECLARED_ELEMDEF =
145         Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE;
146     
147     /** Feature identifier: allow Java encodings. */
148     protected static final String JavaDoc ALLOW_JAVA_ENCODINGS =
149         Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE;
150     
151     /** Feature identifier: continue after fatal error. */
152     protected static final String JavaDoc CONTINUE_AFTER_FATAL_ERROR =
153         Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE;
154
155     /** Feature identifier: load external DTD. */
156     protected static final String JavaDoc LOAD_EXTERNAL_DTD =
157         Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE;
158
159     /** Feature identifier: notify built-in refereces. */
160     protected static final String JavaDoc NOTIFY_BUILTIN_REFS =
161         Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_BUILTIN_REFS_FEATURE;
162     
163     /** Feature identifier: notify character refereces. */
164     protected static final String JavaDoc NOTIFY_CHAR_REFS =
165         Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_CHAR_REFS_FEATURE;
166     
167
168     // property identifiers
169

170     /** Property identifier: error reporter. */
171     protected static final String JavaDoc ERROR_REPORTER =
172         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
173
174     /** Property identifier: entity manager. */
175     protected static final String JavaDoc ENTITY_MANAGER =
176         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
177     
178     /** Property identifier document scanner: */
179     protected static final String JavaDoc DOCUMENT_SCANNER =
180         Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_SCANNER_PROPERTY;
181
182     /** Property identifier: DTD scanner. */
183     protected static final String JavaDoc DTD_SCANNER =
184         Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_SCANNER_PROPERTY;
185
186     /** Property identifier: grammar pool. */
187     protected static final String JavaDoc XMLGRAMMAR_POOL =
188         Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
189     
190     /** Property identifier: DTD loader. */
191     protected static final String JavaDoc DTD_PROCESSOR =
192         Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_PROCESSOR_PROPERTY;
193
194     /** Property identifier: DTD validator. */
195     protected static final String JavaDoc DTD_VALIDATOR =
196         Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_VALIDATOR_PROPERTY;
197
198     /** Property identifier: namespace binder. */
199     protected static final String JavaDoc NAMESPACE_BINDER =
200         Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_BINDER_PROPERTY;
201
202     /** Property identifier: datatype validator factory. */
203     protected static final String JavaDoc DATATYPE_VALIDATOR_FACTORY =
204         Constants.XERCES_PROPERTY_PREFIX + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY;
205
206     protected static final String JavaDoc VALIDATION_MANAGER =
207         Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY;
208     
209     /** Property identifier: JAXP schema language / DOM schema-type. */
210     protected static final String JavaDoc JAXP_SCHEMA_LANGUAGE =
211     Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE;
212
213     /** Property identifier: JAXP schema source/ DOM schema-location. */
214     protected static final String JavaDoc JAXP_SCHEMA_SOURCE =
215     Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE;
216
217
218     // debugging
219

220     /** Set to true and recompile to print exception stack trace. */
221     protected static final boolean PRINT_EXCEPTION_STACK_TRACE = false;
222
223     //
224
// Data
225
//
226

227     // components (non-configurable)
228

229     /** Grammar pool. */
230     protected XMLGrammarPool fGrammarPool;
231
232     /** Datatype validator factory. */
233     protected DTDDVFactory fDatatypeValidatorFactory;
234
235     // components (configurable)
236

237     /** Error reporter. */
238     protected XMLErrorReporter fErrorReporter;
239
240     /** Entity manager. */
241     protected XMLEntityManager fEntityManager;
242
243     /** Document scanner. */
244     protected XMLDocumentScanner fScanner;
245
246     /** Input Source */
247     protected XMLInputSource fInputSource;
248
249     /** DTD scanner. */
250     protected XMLDTDScanner fDTDScanner;
251
252     /** DTD Processor . */
253     protected XMLDTDProcessor fDTDProcessor;
254
255     /** DTD Validator. */
256     protected XMLDTDValidator fDTDValidator;
257
258     /** Namespace binder. */
259     protected XMLNamespaceBinder fNamespaceBinder;
260
261     protected ValidationManager fValidationManager;
262     // state
263

264     /** Locator */
265     protected XMLLocator fLocator;
266
267     /**
268      * True if a parse is in progress. This state is needed because
269      * some features/properties cannot be set while parsing (e.g.
270      * validation and namespaces).
271      */

272     protected boolean fParseInProgress = false;
273
274     //
275
// Constructors
276
//
277

278     /** Default constructor. */
279     public DTDConfiguration() {
280         this(null, null, null);
281     } // <init>()
282

283     /**
284      * Constructs a parser configuration using the specified symbol table.
285      *
286      * @param symbolTable The symbol table to use.
287      */

288     public DTDConfiguration(SymbolTable symbolTable) {
289         this(symbolTable, null, null);
290     } // <init>(SymbolTable)
291

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

303     public DTDConfiguration(SymbolTable symbolTable,
304                                        XMLGrammarPool grammarPool) {
305         this(symbolTable, grammarPool, null);
306     } // <init>(SymbolTable,XMLGrammarPool)
307

308     /**
309      * Constructs a parser configuration using the specified symbol table,
310      * grammar pool, and parent settings.
311      * <p>
312      * <strong>REVISIT:</strong>
313      * Grammar pool will be updated when the new validation engine is
314      * implemented.
315      *
316      * @param symbolTable The symbol table to use.
317      * @param grammarPool The grammar pool to use.
318      * @param parentSettings The parent settings.
319      */

320     public DTDConfiguration(SymbolTable symbolTable,
321                                        XMLGrammarPool grammarPool,
322                                        XMLComponentManager parentSettings) {
323         super(symbolTable, parentSettings);
324
325         // add default recognized features
326
final String JavaDoc[] recognizedFeatures = {
327             //WARN_ON_DUPLICATE_ATTDEF, // from XMLDTDScannerImpl
328
//WARN_ON_UNDECLARED_ELEMDEF, // from XMLDTDScannerImpl
329
//ALLOW_JAVA_ENCODINGS, // from XMLEntityManager
330
CONTINUE_AFTER_FATAL_ERROR,
331             LOAD_EXTERNAL_DTD, // from XMLDTDScannerImpl
332
//NOTIFY_BUILTIN_REFS, // from XMLDocumentFragmentScannerImpl
333
//NOTIFY_CHAR_REFS, // from XMLDocumentFragmentScannerImpl
334
//WARN_ON_DUPLICATE_ENTITYDEF, // from XMLEntityManager
335
};
336         addRecognizedFeatures(recognizedFeatures);
337
338         // set state for default features
339
//setFeature(WARN_ON_DUPLICATE_ATTDEF, false); // from XMLDTDScannerImpl
340
//setFeature(WARN_ON_UNDECLARED_ELEMDEF, false); // from XMLDTDScannerImpl
341
//setFeature(ALLOW_JAVA_ENCODINGS, false); // from XMLEntityManager
342
setFeature(CONTINUE_AFTER_FATAL_ERROR, false);
343         setFeature(LOAD_EXTERNAL_DTD, true); // from XMLDTDScannerImpl
344
//setFeature(NOTIFY_BUILTIN_REFS, false); // from XMLDocumentFragmentScannerImpl
345
//setFeature(NOTIFY_CHAR_REFS, false); // from XMLDocumentFragmentScannerImpl
346
//setFeature(WARN_ON_DUPLICATE_ENTITYDEF, false); // from XMLEntityManager
347

348         // add default recognized properties
349
final String JavaDoc[] recognizedProperties = {
350             ERROR_REPORTER,
351             ENTITY_MANAGER,
352             DOCUMENT_SCANNER,
353             DTD_SCANNER,
354             DTD_PROCESSOR,
355             DTD_VALIDATOR,
356             NAMESPACE_BINDER,
357             XMLGRAMMAR_POOL,
358             DATATYPE_VALIDATOR_FACTORY,
359             VALIDATION_MANAGER,
360             JAXP_SCHEMA_SOURCE,
361             JAXP_SCHEMA_LANGUAGE
362         };
363         addRecognizedProperties(recognizedProperties);
364
365         fGrammarPool = grammarPool;
366         if(fGrammarPool != null){
367             setProperty(XMLGRAMMAR_POOL, fGrammarPool);
368         }
369
370         fEntityManager = createEntityManager();
371         setProperty(ENTITY_MANAGER, fEntityManager);
372         addComponent(fEntityManager);
373
374         fErrorReporter = createErrorReporter();
375         fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner());
376         setProperty(ERROR_REPORTER, fErrorReporter);
377         addComponent(fErrorReporter);
378
379         fScanner = createDocumentScanner();
380         setProperty(DOCUMENT_SCANNER, fScanner);
381         if (fScanner instanceof XMLComponent) {
382             addComponent((XMLComponent)fScanner);
383         }
384
385         fDTDScanner = createDTDScanner();
386         if (fDTDScanner != null) {
387             setProperty(DTD_SCANNER, fDTDScanner);
388             if (fDTDScanner instanceof XMLComponent) {
389                 addComponent((XMLComponent)fDTDScanner);
390             }
391         }
392
393         fDTDProcessor = createDTDProcessor();
394         if (fDTDProcessor != null) {
395             setProperty(DTD_PROCESSOR, fDTDProcessor);
396             if (fDTDProcessor instanceof XMLComponent) {
397                 addComponent((XMLComponent)fDTDProcessor);
398             }
399         }
400
401         fDTDValidator = createDTDValidator();
402         if (fDTDValidator != null) {
403             setProperty(DTD_VALIDATOR, fDTDValidator);
404             addComponent(fDTDValidator);
405         }
406
407         fNamespaceBinder = createNamespaceBinder();
408         if (fNamespaceBinder != null) {
409             setProperty(NAMESPACE_BINDER, fNamespaceBinder);
410             addComponent(fNamespaceBinder);
411         }
412         
413         fDatatypeValidatorFactory = createDatatypeValidatorFactory();
414         if (fDatatypeValidatorFactory != null) {
415             setProperty(DATATYPE_VALIDATOR_FACTORY,
416                         fDatatypeValidatorFactory);
417         }
418         fValidationManager = createValidationManager();
419
420         if (fValidationManager != null) {
421             setProperty (VALIDATION_MANAGER, fValidationManager);
422         }
423         // add message formatters
424
if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) {
425             XMLMessageFormatter xmft = new XMLMessageFormatter();
426             fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft);
427             fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft);
428         }
429
430         // set locale
431
try {
432             setLocale(Locale.getDefault());
433         }
434         catch (XNIException e) {
435             // do nothing
436
// REVISIT: What is the right thing to do? -Ac
437
}
438
439     } // <init>(SymbolTable,XMLGrammarPool)
440

441     //
442
// Public methods
443
//
444

445     /**
446      * Set the locale to use for messages.
447      *
448      * @param locale The locale object to use for localization of messages.
449      *
450      * @exception XNIException Thrown if the parser does not support the
451      * specified locale.
452      */

453     public void setLocale(Locale JavaDoc locale) throws XNIException {
454         super.setLocale(locale);
455         fErrorReporter.setLocale(locale);
456     } // setLocale(Locale)
457

458     //
459
// XMLPullParserConfiguration methods
460
//
461

462     // parsing
463

464     /**
465      * Sets the input source for the document to parse.
466      *
467      * @param inputSource The document's input source.
468      *
469      * @exception XMLConfigurationException Thrown if there is a
470      * configuration error when initializing the
471      * parser.
472      * @exception IOException Thrown on I/O error.
473      *
474      * @see #parse(boolean)
475      */

476     public void setInputSource(XMLInputSource inputSource)
477         throws XMLConfigurationException, IOException JavaDoc {
478         
479         // REVISIT: this method used to reset all the components and
480
// construct the pipeline. Now reset() is called
481
// in parse (boolean) just before we parse the document
482
// Should this method still throw exceptions..?
483

484         fInputSource = inputSource;
485         
486     } // setInputSource(XMLInputSource)
487

488     /**
489      * Parses the document in a pull parsing fashion.
490      *
491      * @param complete True if the pull parser should parse the
492      * remaining document completely.
493      *
494      * @return True if there is more document to parse.
495      *
496      * @exception XNIException Any XNI exception, possibly wrapping
497      * another exception.
498      * @exception IOException An IO exception from the parser, possibly
499      * from a byte stream or character stream
500      * supplied by the parser.
501      *
502      * @see #setInputSource
503      */

504     public boolean parse(boolean complete) throws XNIException, IOException JavaDoc {
505         //
506
// reset and configure pipeline and set InputSource.
507
if (fInputSource !=null) {
508             try {
509                 // resets and sets the pipeline.
510
reset();
511                 fScanner.setInputSource(fInputSource);
512                 fInputSource = null;
513             }
514             catch (XNIException ex) {
515                 if (PRINT_EXCEPTION_STACK_TRACE)
516                     ex.printStackTrace();
517                 throw ex;
518             }
519             catch (IOException JavaDoc ex) {
520                 if (PRINT_EXCEPTION_STACK_TRACE)
521                     ex.printStackTrace();
522                 throw ex;
523             }
524             catch (RuntimeException JavaDoc ex) {
525                 if (PRINT_EXCEPTION_STACK_TRACE)
526                     ex.printStackTrace();
527                 throw ex;
528             }
529             catch (Exception JavaDoc ex) {
530                 if (PRINT_EXCEPTION_STACK_TRACE)
531                     ex.printStackTrace();
532                 throw new XNIException(ex);
533             }
534         }
535
536         try {
537             return fScanner.scanDocument(complete);
538         }
539         catch (XNIException ex) {
540             if (PRINT_EXCEPTION_STACK_TRACE)
541                 ex.printStackTrace();
542             throw ex;
543         }
544         catch (IOException JavaDoc ex) {
545             if (PRINT_EXCEPTION_STACK_TRACE)
546                 ex.printStackTrace();
547             throw ex;
548         }
549         catch (RuntimeException JavaDoc ex) {
550             if (PRINT_EXCEPTION_STACK_TRACE)
551                 ex.printStackTrace();
552             throw ex;
553         }
554         catch (Exception JavaDoc ex) {
555             if (PRINT_EXCEPTION_STACK_TRACE)
556                 ex.printStackTrace();
557             throw new XNIException(ex);
558         }
559
560     } // parse(boolean):boolean
561

562     /**
563      * If the application decides to terminate parsing before the xml document
564      * is fully parsed, the application should call this method to free any
565      * resource allocated during parsing. For example, close all opened streams.
566      */

567     public void cleanup() {
568         fEntityManager.closeReaders();
569     }
570     
571     //
572
// XMLParserConfiguration methods
573
//
574

575     /**
576      * Parses the specified input source.
577      *
578      * @param source The input source.
579      *
580      * @exception XNIException Throws exception on XNI error.
581      * @exception java.io.IOException Throws exception on i/o error.
582      */

583     public void parse(XMLInputSource source) throws XNIException, IOException JavaDoc {
584
585         if (fParseInProgress) {
586             // REVISIT - need to add new error message
587
throw new XNIException("FWK005 parse may not be called while parsing.");
588         }
589         fParseInProgress = true;
590
591         try {
592             setInputSource(source);
593             parse(true);
594         }
595         catch (XNIException ex) {
596             if (PRINT_EXCEPTION_STACK_TRACE)
597                 ex.printStackTrace();
598             throw ex;
599         }
600         catch (IOException JavaDoc ex) {
601             if (PRINT_EXCEPTION_STACK_TRACE)
602                 ex.printStackTrace();
603             throw ex;
604         }
605         catch (RuntimeException JavaDoc ex) {
606             if (PRINT_EXCEPTION_STACK_TRACE)
607                 ex.printStackTrace();
608             throw ex;
609         }
610         catch (Exception JavaDoc ex) {
611             if (PRINT_EXCEPTION_STACK_TRACE)
612                 ex.printStackTrace();
613             throw new XNIException(ex);
614         }
615         finally {
616             fParseInProgress = false;
617             // close all streams opened by xerces
618
this.cleanup();
619         }
620
621     } // parse(InputSource)
622

623     //
624
// Protected methods
625
//
626

627     /**
628      * Reset all components before parsing.
629      *
630      * @throws XNIException Thrown if an error occurs during initialization.
631      */

632     protected void reset() throws XNIException {
633
634         if (fValidationManager != null)
635             fValidationManager.reset();
636         // configure the pipeline and initialize the components
637
configurePipeline();
638         super.reset();
639     } // reset()
640

641     /** Configures the pipeline. */
642     protected void configurePipeline() {
643
644         // REVISIT: This should be better designed. In other words, we
645
// need to figure out what is the best way for people to
646
// re-use *most* of the standard configuration but do
647
// things common things such as remove a component (e.g.
648
// the validator), insert a new component (e.g. XInclude),
649
// etc... -Ac
650

651         // setup document pipeline
652
if (fDTDValidator != null) {
653             fScanner.setDocumentHandler(fDTDValidator);
654             if (fFeatures.get(NAMESPACES) == Boolean.TRUE) {
655
656                 // filters
657
fDTDValidator.setDocumentHandler(fNamespaceBinder);
658                 fDTDValidator.setDocumentSource(fScanner);
659                 fNamespaceBinder.setDocumentHandler(fDocumentHandler);
660                 fNamespaceBinder.setDocumentSource(fDTDValidator);
661                 fLastComponent = fNamespaceBinder;
662             }
663             else {
664                 fDTDValidator.setDocumentHandler(fDocumentHandler);
665                 fDTDValidator.setDocumentSource(fScanner);
666                 fLastComponent = fDTDValidator;
667             }
668         }
669         else {
670             if (fFeatures.get(NAMESPACES) == Boolean.TRUE) {
671                 fScanner.setDocumentHandler(fNamespaceBinder);
672                 fNamespaceBinder.setDocumentHandler(fDocumentHandler);
673                 fNamespaceBinder.setDocumentSource(fScanner);
674                 fLastComponent = fNamespaceBinder;
675             }
676             else {
677                 fScanner.setDocumentHandler(fDocumentHandler);
678                 fLastComponent = fScanner;
679             }
680         }
681         
682         configureDTDPipeline();
683     } // configurePipeline()
684

685     protected void configureDTDPipeline (){
686         
687         // setup dtd pipeline
688
if (fDTDScanner != null) {
689             fProperties.put(DTD_SCANNER, fDTDScanner);
690             if (fDTDProcessor != null) {
691                 fProperties.put(DTD_PROCESSOR, fDTDProcessor);
692                 fDTDScanner.setDTDHandler(fDTDProcessor);
693                 fDTDProcessor.setDTDSource(fDTDScanner);
694                 fDTDProcessor.setDTDHandler(fDTDHandler);
695                 if (fDTDHandler != null) {
696                     fDTDHandler.setDTDSource(fDTDProcessor);
697                 }
698
699                 fDTDScanner.setDTDContentModelHandler(fDTDProcessor);
700                 fDTDProcessor.setDTDContentModelSource(fDTDScanner);
701                 fDTDProcessor.setDTDContentModelHandler(fDTDContentModelHandler);
702                 if (fDTDContentModelHandler != null) {
703                     fDTDContentModelHandler.setDTDContentModelSource(fDTDProcessor);
704                 }
705             }
706             else {
707                 fDTDScanner.setDTDHandler(fDTDHandler);
708                 if (fDTDHandler != null) {
709                     fDTDHandler.setDTDSource(fDTDScanner);
710                 }
711                 fDTDScanner.setDTDContentModelHandler(fDTDContentModelHandler);
712                 if (fDTDContentModelHandler != null) {
713                     fDTDContentModelHandler.setDTDContentModelSource(fDTDScanner);
714                 }
715             }
716         }
717
718
719     }
720
721     // features and properties
722

723     /**
724      * Check a feature. If feature is know and supported, this method simply
725      * returns. Otherwise, the appropriate exception is thrown.
726      *
727      * @param featureId The unique identifier (URI) of the feature.
728      *
729      * @throws XMLConfigurationException Thrown for configuration error.
730      * In general, components should
731      * only throw this exception if
732      * it is <strong>really</strong>
733      * a critical error.
734      */

735     protected void checkFeature(String JavaDoc featureId)
736         throws XMLConfigurationException {
737
738         //
739
// Xerces Features
740
//
741

742         if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
743             final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
744             
745             //
746
// http://apache.org/xml/features/validation/dynamic
747
// Allows the parser to validate a document only when it
748
// contains a grammar. Validation is turned on/off based
749
// on each document instance, automatically.
750
//
751
if (suffixLength == Constants.DYNAMIC_VALIDATION_FEATURE.length() &&
752                 featureId.endsWith(Constants.DYNAMIC_VALIDATION_FEATURE)) {
753                 return;
754             }
755
756             //
757
// http://apache.org/xml/features/validation/default-attribute-values
758
//
759
if (suffixLength == Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE.length() &&
760                 featureId.endsWith(Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE)) {
761                 // REVISIT
762
short type = XMLConfigurationException.NOT_SUPPORTED;
763                 throw new XMLConfigurationException(type, featureId);
764             }
765             //
766
// http://apache.org/xml/features/validation/default-attribute-values
767
//
768
if (suffixLength == Constants.VALIDATE_CONTENT_MODELS_FEATURE.length() &&
769                 featureId.endsWith(Constants.VALIDATE_CONTENT_MODELS_FEATURE)) {
770                 // REVISIT
771
short type = XMLConfigurationException.NOT_SUPPORTED;
772                 throw new XMLConfigurationException(type, featureId);
773             }
774             //
775
// http://apache.org/xml/features/validation/nonvalidating/load-dtd-grammar
776
//
777
if (suffixLength == Constants.LOAD_DTD_GRAMMAR_FEATURE.length() &&
778                 featureId.endsWith(Constants.LOAD_DTD_GRAMMAR_FEATURE)) {
779                 return;
780             }
781             //
782
// http://apache.org/xml/features/validation/nonvalidating/load-external-dtd
783
//
784
if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() &&
785                 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) {
786                 return;
787             }
788
789             //
790
// http://apache.org/xml/features/validation/default-attribute-values
791
//
792
if (suffixLength == Constants.VALIDATE_DATATYPES_FEATURE.length() &&
793                 featureId.endsWith(Constants.VALIDATE_DATATYPES_FEATURE)) {
794                 short type = XMLConfigurationException.NOT_SUPPORTED;
795                 throw new XMLConfigurationException(type, featureId);
796             }
797         }
798
799         //
800
// Not recognized
801
//
802

803         super.checkFeature(featureId);
804
805     } // checkFeature(String)
806

807     /**
808      * Check a property. If the property is know and supported, this method
809      * simply returns. Otherwise, the appropriate exception is thrown.
810      *
811      * @param propertyId The unique identifier (URI) of the property
812      * being set.
813      *
814      * @throws XMLConfigurationException Thrown for configuration error.
815      * In general, components should
816      * only throw this exception if
817      * it is <strong>really</strong>
818      * a critical error.
819      */

820     protected void checkProperty(String JavaDoc propertyId)
821         throws XMLConfigurationException {
822
823         //
824
// Xerces Properties
825
//
826

827         if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
828             final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length();
829
830             if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() &&
831                 propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) {
832                 return;
833             }
834         }
835
836         //
837
// Not recognized
838
//
839

840         super.checkProperty(propertyId);
841
842     } // checkProperty(String)
843

844     // factory methods
845

846     /** Creates an entity manager. */
847     protected XMLEntityManager createEntityManager() {
848         return new XMLEntityManager();
849     } // createEntityManager():XMLEntityManager
850

851     /** Creates an error reporter. */
852     protected XMLErrorReporter createErrorReporter() {
853         return new XMLErrorReporter();
854     } // createErrorReporter():XMLErrorReporter
855

856     /** Create a document scanner. */
857     protected XMLDocumentScanner createDocumentScanner() {
858         return new XMLDocumentScannerImpl();
859     } // createDocumentScanner():XMLDocumentScanner
860

861     /** Create a DTD scanner. */
862     protected XMLDTDScanner createDTDScanner() {
863         return new XMLDTDScannerImpl();
864     } // createDTDScanner():XMLDTDScanner
865

866     /** Create a DTD loader . */
867     protected XMLDTDProcessor createDTDProcessor() {
868         return new XMLDTDProcessor();
869     } // createDTDProcessor():XMLDTDProcessor
870

871     /** Create a DTD validator. */
872     protected XMLDTDValidator createDTDValidator() {
873         return new XMLDTDValidator();
874     } // createDTDValidator():XMLDTDValidator
875

876     /** Create a namespace binder. */
877     protected XMLNamespaceBinder createNamespaceBinder() {
878         return new XMLNamespaceBinder();
879     } // createNamespaceBinder():XMLNamespaceBinder
880

881     /** Create a datatype validator factory. */
882     protected DTDDVFactory createDatatypeValidatorFactory() {
883         return DTDDVFactory.getInstance();
884     } // createDatatypeValidatorFactory():DatatypeValidatorFactory
885
protected ValidationManager createValidationManager(){
886         return new ValidationManager();
887     }
888
889 } // class DTDConfiguration
890
Popular Tags