KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
6  * 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
62 import com.sun.org.apache.xerces.internal.impl.Constants;
63 import com.sun.org.apache.xerces.internal.util.EntityResolverWrapper;
64 import com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper;
65 import com.sun.org.apache.xerces.internal.util.SymbolTable;
66 import com.sun.org.apache.xerces.internal.xni.XNIException;
67 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
68 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
69 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
70 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;
71 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
72 import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException;
73 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration;
74 import org.w3c.dom.Node JavaDoc;
75 import org.xml.sax.EntityResolver JavaDoc;
76 import org.xml.sax.ErrorHandler JavaDoc;
77 import org.xml.sax.InputSource JavaDoc;
78 import org.xml.sax.SAXException JavaDoc;
79 import org.xml.sax.SAXNotRecognizedException JavaDoc;
80 import org.xml.sax.SAXNotSupportedException JavaDoc;
81 import org.xml.sax.SAXParseException JavaDoc;
82 import org.xml.sax.helpers.LocatorImpl JavaDoc;
83 import com.sun.org.apache.xerces.internal.util.EntityResolver2Wrapper;
84 import org.xml.sax.ext.EntityResolver2 JavaDoc;
85
86 /**
87  * This is the main Xerces DOM parser class. It uses the abstract DOM
88  * parser with a document scanner, a dtd scanner, and a validator, as
89  * well as a grammar pool.
90  *
91  * @author Arnaud Le Hors, IBM
92  * @author Andy Clark, IBM
93  *
94  * @version $Id: DOMParser.java,v 1.69 2004/02/17 07:14:49 neeraj Exp $
95  */

96 public class DOMParser
97 extends AbstractDOMParser {
98     
99     //
100
// Constants
101
//
102

103     // properties
104

105     /** Property identifier: symbol table. */
106     protected static final String JavaDoc SYMBOL_TABLE =
107     Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
108     
109     /** Property identifier: XML grammar pool. */
110     protected static final String JavaDoc XMLGRAMMAR_POOL =
111     Constants.XERCES_PROPERTY_PREFIX+Constants.XMLGRAMMAR_POOL_PROPERTY;
112     
113     /** Recognized properties. */
114     private static final String JavaDoc[] RECOGNIZED_PROPERTIES = {
115         SYMBOL_TABLE,
116         XMLGRAMMAR_POOL,
117     };
118     
119     //
120
// Constructors
121
//
122

123     /**
124      * Constructs a DOM parser using the specified parser configuration.
125      */

126     public DOMParser(XMLParserConfiguration config) {
127         super(config);
128     } // <init>(XMLParserConfiguration)
129

130     /**
131      * Constructs a DOM parser using the dtd/xml schema parser configuration.
132      */

133     public DOMParser() {
134         this(null, null);
135     } // <init>()
136

137     /**
138      * Constructs a DOM parser using the specified symbol table.
139      */

140     public DOMParser(SymbolTable symbolTable) {
141         this(symbolTable, null);
142     } // <init>(SymbolTable)
143

144     
145     /**
146      * Constructs a DOM parser using the specified symbol table and
147      * grammar pool.
148      */

149     public DOMParser(SymbolTable symbolTable, XMLGrammarPool grammarPool) {
150         super((XMLParserConfiguration)ObjectFactory.createObject(
151         "com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration",
152         "com.sun.org.apache.xerces.internal.parsers.XIncludeParserConfiguration"
153         ));
154         
155         // set properties
156
fConfiguration.addRecognizedProperties(RECOGNIZED_PROPERTIES);
157         if (symbolTable != null) {
158             fConfiguration.setProperty(SYMBOL_TABLE, symbolTable);
159         }
160         if (grammarPool != null) {
161             fConfiguration.setProperty(XMLGRAMMAR_POOL, grammarPool);
162         }
163         
164     } // <init>(SymbolTable,XMLGrammarPool)
165

166     //
167
// XMLReader methods
168
//
169

170     /**
171      * Parses the input source specified by the given system identifier.
172      * <p>
173      * This method is equivalent to the following:
174      * <pre>
175      * parse(new InputSource(systemId));
176      * </pre>
177      *
178      * @param source The input source.
179      *
180      * @exception org.xml.sax.SAXException Throws exception on SAX error.
181      * @exception java.io.IOException Throws exception on i/o error.
182      */

183     public void parse(String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
184         
185         // parse document
186
XMLInputSource source = new XMLInputSource(null, systemId, null);
187         try {
188             parse(source);
189         }
190         
191         // wrap XNI exceptions as SAX exceptions
192
catch (XMLParseException e) {
193             Exception JavaDoc ex = e.getException();
194             if (ex == null) {
195                 // must be a parser exception; mine it for locator info and throw
196
// a SAXParseException
197
LocatorImpl JavaDoc locatorImpl = new LocatorImpl JavaDoc();
198                 locatorImpl.setPublicId(e.getPublicId());
199                 locatorImpl.setSystemId(e.getExpandedSystemId());
200                 locatorImpl.setLineNumber(e.getLineNumber());
201                 locatorImpl.setColumnNumber(e.getColumnNumber());
202                 throw new SAXParseException JavaDoc(e.getMessage(), locatorImpl);
203             }
204             if (ex instanceof SAXException JavaDoc) {
205                 // why did we create an XMLParseException?
206
throw (SAXException JavaDoc)ex;
207             }
208             if (ex instanceof IOException JavaDoc) {
209                 throw (IOException JavaDoc)ex;
210             }
211             throw new SAXException JavaDoc(ex);
212         }
213         catch (XNIException e) {
214             e.printStackTrace();
215             Exception JavaDoc ex = e.getException();
216             if (ex == null) {
217                 throw new SAXException JavaDoc(e.getMessage());
218             }
219             if (ex instanceof SAXException JavaDoc) {
220                 throw (SAXException JavaDoc)ex;
221             }
222             if (ex instanceof IOException JavaDoc) {
223                 throw (IOException JavaDoc)ex;
224             }
225             throw new SAXException JavaDoc(ex);
226         }
227         
228     } // parse(String)
229

230     /**
231      * parse
232      *
233      * @param inputSource
234      *
235      * @exception org.xml.sax.SAXException
236      * @exception java.io.IOException
237      */

238     public void parse(InputSource inputSource)
239     throws SAXException JavaDoc, IOException JavaDoc {
240         
241         // parse document
242
try {
243             XMLInputSource xmlInputSource =
244             new XMLInputSource(inputSource.getPublicId(),
245             inputSource.getSystemId(),
246             null);
247             xmlInputSource.setByteStream(inputSource.getByteStream());
248             xmlInputSource.setCharacterStream(inputSource.getCharacterStream());
249             xmlInputSource.setEncoding(inputSource.getEncoding());
250             parse(xmlInputSource);
251         }
252         
253         // wrap XNI exceptions as SAX exceptions
254
catch (XMLParseException e) {
255             Exception JavaDoc ex = e.getException();
256             if (ex == null) {
257                 // must be a parser exception; mine it for locator info and throw
258
// a SAXParseException
259
LocatorImpl JavaDoc locatorImpl = new LocatorImpl JavaDoc();
260                 locatorImpl.setPublicId(e.getPublicId());
261                 locatorImpl.setSystemId(e.getExpandedSystemId());
262                 locatorImpl.setLineNumber(e.getLineNumber());
263                 locatorImpl.setColumnNumber(e.getColumnNumber());
264                 throw new SAXParseException JavaDoc(e.getMessage(), locatorImpl);
265             }
266             if (ex instanceof SAXException JavaDoc) {
267                 // why did we create an XMLParseException?
268
throw (SAXException JavaDoc)ex;
269             }
270             if (ex instanceof IOException JavaDoc) {
271                 throw (IOException JavaDoc)ex;
272             }
273             throw new SAXException JavaDoc(ex);
274         }
275         catch (XNIException e) {
276             Exception JavaDoc ex = e.getException();
277             if (ex == null) {
278                 throw new SAXException JavaDoc(e.getMessage());
279             }
280             if (ex instanceof SAXException JavaDoc) {
281                 throw (SAXException JavaDoc)ex;
282             }
283             if (ex instanceof IOException JavaDoc) {
284                 throw (IOException JavaDoc)ex;
285             }
286             throw new SAXException JavaDoc(ex);
287         }
288         
289     } // parse(InputSource)
290

291     /**
292      * Sets the resolver used to resolve external entities. The EntityResolver
293      * interface supports resolution of public and system identifiers.
294      *
295      * @param resolver The new entity resolver. Passing a null value will
296      * uninstall the currently installed resolver.
297      */

298     public void setEntityResolver(EntityResolver resolver) {
299         
300         try {
301             if(resolver instanceof EntityResolver2 JavaDoc){
302                 fConfiguration.setProperty(ENTITY_RESOLVER, new EntityResolver2Wrapper((EntityResolver2 JavaDoc)resolver));
303             }else{
304                 fConfiguration.setProperty(ENTITY_RESOLVER, new EntityResolverWrapper(resolver));
305             }
306         }
307         catch (XMLConfigurationException e) {
308             // do nothing
309
}
310         
311     } // setEntityResolver(EntityResolver)
312

313     /**
314      * Return the current entity resolver.
315      *
316      * @return The current entity resolver, or null if none
317      * has been registered.
318      * @see #setEntityResolver
319      */

320     public EntityResolver getEntityResolver() {
321         
322         EntityResolver entityResolver = null;
323         try {
324             XMLEntityResolver xmlEntityResolver =
325             (XMLEntityResolver)fConfiguration.getProperty(ENTITY_RESOLVER);
326             if (xmlEntityResolver != null){
327                 if(xmlEntityResolver instanceof EntityResolverWrapper) {
328                     entityResolver = ((EntityResolverWrapper)xmlEntityResolver).getEntityResolver();
329                 }else if(xmlEntityResolver instanceof EntityResolver2Wrapper){
330                     entityResolver = ((EntityResolver2Wrapper)xmlEntityResolver).getEntityResolver();
331                 }
332             }
333         }catch (XMLConfigurationException e) {
334             // do nothing
335
}
336         return entityResolver;
337         
338     } // getEntityResolver():EntityResolver
339

340     /**
341      * Allow an application to register an error event handler.
342      *
343      * <p>If the application does not register an error handler, all
344      * error events reported by the SAX parser will be silently
345      * ignored; however, normal processing may not continue. It is
346      * highly recommended that all SAX applications implement an
347      * error handler to avoid unexpected bugs.</p>
348      *
349      * <p>Applications may register a new or different handler in the
350      * middle of a parse, and the SAX parser must begin using the new
351      * handler immediately.</p>
352      *
353      * @param errorHandler The error handler.
354      * @exception java.lang.NullPointerException If the handler
355      * argument is null.
356      * @see #getErrorHandler
357      */

358     public void setErrorHandler(ErrorHandler errorHandler) {
359         
360         try {
361             fConfiguration.setProperty(ERROR_HANDLER,
362             new ErrorHandlerWrapper(errorHandler));
363         }
364         catch (XMLConfigurationException e) {
365             // do nothing
366
}
367         
368     } // setErrorHandler(ErrorHandler)
369

370     /**
371      * Return the current error handler.
372      *
373      * @return The current error handler, or null if none
374      * has been registered.
375      * @see #setErrorHandler
376      */

377     public ErrorHandler getErrorHandler() {
378         
379         ErrorHandler errorHandler = null;
380         try {
381             XMLErrorHandler xmlErrorHandler =
382             (XMLErrorHandler)fConfiguration.getProperty(ERROR_HANDLER);
383             if (xmlErrorHandler != null &&
384             xmlErrorHandler instanceof ErrorHandlerWrapper) {
385                 errorHandler = ((ErrorHandlerWrapper)xmlErrorHandler).getErrorHandler();
386             }
387         }
388         catch (XMLConfigurationException e) {
389             // do nothing
390
}
391         return errorHandler;
392         
393     } // getErrorHandler():ErrorHandler
394

395     /**
396      * Set the state of any feature in a SAX2 parser. The parser
397      * might not recognize the feature, and if it does recognize
398      * it, it might not be able to fulfill the request.
399      *
400      * @param featureId The unique identifier (URI) of the feature.
401      * @param state The requested state of the feature (true or false).
402      *
403      * @exception SAXNotRecognizedException If the
404      * requested feature is not known.
405      * @exception SAXNotSupportedException If the
406      * requested feature is known, but the requested
407      * state is not supported.
408      */

409     public void setFeature(String JavaDoc featureId, boolean state)
410     throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
411         
412         try {
413             fConfiguration.setFeature(featureId, state);
414         }
415         catch (XMLConfigurationException e) {
416             String JavaDoc message = e.getMessage();
417             if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
418                 throw new SAXNotRecognizedException JavaDoc(message);
419             }
420             else {
421                 throw new SAXNotSupportedException JavaDoc(message);
422             }
423         }
424         
425     } // setFeature(String,boolean)
426

427     /**
428      * Query the state of a feature.
429      *
430      * Query the current state of any feature in a SAX2 parser. The
431      * parser might not recognize the feature.
432      *
433      * @param featureId The unique identifier (URI) of the feature
434      * being set.
435      * @return The current state of the feature.
436      * @exception org.xml.sax.SAXNotRecognizedException If the
437      * requested feature is not known.
438      * @exception SAXNotSupportedException If the
439      * requested feature is known but not supported.
440      */

441     public boolean getFeature(String JavaDoc featureId)
442     throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
443         
444         try {
445             return fConfiguration.getFeature(featureId);
446         }
447         catch (XMLConfigurationException e) {
448             String JavaDoc message = e.getMessage();
449             if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
450                 throw new SAXNotRecognizedException JavaDoc(message);
451             }
452             else {
453                 throw new SAXNotSupportedException JavaDoc(message);
454             }
455         }
456         
457     } // getFeature(String):boolean
458

459     /**
460      * Set the value of any property in a SAX2 parser. The parser
461      * might not recognize the property, and if it does recognize
462      * it, it might not support the requested value.
463      *
464      * @param propertyId The unique identifier (URI) of the property
465      * being set.
466      * @param Object The value to which the property is being set.
467      *
468      * @exception SAXNotRecognizedException If the
469      * requested property is not known.
470      * @exception SAXNotSupportedException If the
471      * requested property is known, but the requested
472      * value is not supported.
473      */

474     public void setProperty(String JavaDoc propertyId, Object JavaDoc value)
475     throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
476         
477         try {
478             fConfiguration.setProperty(propertyId, value);
479         }
480         catch (XMLConfigurationException e) {
481             String JavaDoc message = e.getMessage();
482             if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
483                 throw new SAXNotRecognizedException JavaDoc(message);
484             }
485             else {
486                 throw new SAXNotSupportedException JavaDoc(message);
487             }
488         }
489         
490     } // setProperty(String,Object)
491

492     /**
493      * Query the value of a property.
494      *
495      * Return the current value of a property in a SAX2 parser.
496      * The parser might not recognize the property.
497      *
498      * @param propertyId The unique identifier (URI) of the property
499      * being set.
500      * @return The current value of the property.
501      * @exception org.xml.sax.SAXNotRecognizedException If the
502      * requested property is not known.
503      * @exception SAXNotSupportedException If the
504      * requested property is known but not supported.
505      */

506     public Object JavaDoc getProperty(String JavaDoc propertyId)
507     throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
508         
509         if (propertyId.equals(CURRENT_ELEMENT_NODE)) {
510             boolean deferred = false;
511             try {
512                 deferred = getFeature(DEFER_NODE_EXPANSION);
513             }
514             catch (XMLConfigurationException e){
515                 // ignore
516
}
517             if (deferred) {
518                 throw new SAXNotSupportedException JavaDoc("Current element node cannot be queried when node expansion is deferred.");
519             }
520             return (fCurrentNode!=null &&
521             fCurrentNode.getNodeType() == Node.ELEMENT_NODE)? fCurrentNode:null;
522         }
523         
524         try {
525             return fConfiguration.getProperty(propertyId);
526         }
527         catch (XMLConfigurationException e) {
528             String JavaDoc message = e.getMessage();
529             if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
530                 throw new SAXNotRecognizedException JavaDoc(message);
531             }
532             else {
533                 throw new SAXNotSupportedException JavaDoc(message);
534             }
535         }
536         
537     } // getProperty(String):Object
538

539 } // class DOMParser
540
Popular Tags