KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > xml > XercesParser


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.xml;
9
10 import org.apache.avalon.framework.logger.AbstractLogEnabled;
11 import org.apache.avalon.framework.thread.SingleThreaded;
12 import org.apache.xerces.parsers.DOMParser;
13 import org.apache.xerces.parsers.SAXParser;
14 import org.w3c.dom.Document JavaDoc;
15 import org.xml.sax.ContentHandler JavaDoc;
16 import org.xml.sax.ErrorHandler JavaDoc;
17 import org.xml.sax.InputSource JavaDoc;
18 import org.xml.sax.SAXException JavaDoc;
19 import org.xml.sax.SAXParseException JavaDoc;
20 import org.xml.sax.ext.LexicalHandler JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 /**
24  *
25  * @author <a HREF="mailto:fumagalli@exoffice.com">Pierpaolo Fumagalli</a>
26  * (Apache Software Foundation, Exoffice Technologies)
27  * @version CVS $Revision: 1.5 $ $Date: 2001/12/11 09:53:38 $
28  */

29 public class XercesParser
30 extends AbstractLogEnabled
31 implements Parser, ErrorHandler JavaDoc, SingleThreaded {
32
33     /** the SAX Parser */
34     final SAXParser parser;
35
36     public XercesParser ()
37     throws SAXException JavaDoc {
38         this.parser = new SAXParser();
39
40         this.parser.setFeature("http://xml.org/sax/features/validation",false);
41         this.parser.setFeature("http://xml.org/sax/features/namespaces",true);
42         this.parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
43                           true);
44     }
45
46     public void parse(InputSource JavaDoc in, ContentHandler JavaDoc consumer)
47     throws SAXException JavaDoc, IOException JavaDoc {
48         if (consumer instanceof XMLConsumer
49             || consumer instanceof LexicalHandler JavaDoc) {
50             this.parser.setProperty("http://xml.org/sax/properties/lexical-handler",
51                               (LexicalHandler JavaDoc)consumer);
52         }
53         this.parser.setErrorHandler(this);
54         this.parser.setContentHandler(consumer);
55         this.parser.parse(in);
56     }
57
58     /**
59      * Parses a new Document object from the given InputSource.
60      */

61     public Document JavaDoc parseDocument(InputSource JavaDoc input) throws SAXException JavaDoc, IOException JavaDoc {
62         DOMParser parser = null;
63
64         try {
65             parser = new DOMParser();
66
67             parser.setFeature("http://xml.org/sax/features/validation",false);
68             parser.setFeature("http://xml.org/sax/features/namespaces",true);
69             parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
70                               true);
71
72             parser.parse(input);
73         } catch (Exception JavaDoc pce) {
74             getLogger().error("Could not build DocumentBuilder", pce);
75             return null;
76         }
77
78         return parser.getDocument();
79     }
80
81     /**
82      * Receive notification of a recoverable error.
83      */

84     public void error(SAXParseException JavaDoc e)
85     throws SAXException JavaDoc {
86         throw new SAXException JavaDoc("Error parsing "+e.getSystemId()+" (line "+
87                                e.getLineNumber()+" col. "+e.getColumnNumber()+
88                                "): "+e.getMessage(),e);
89     }
90
91     /**
92      * Receive notification of a fatal error.
93      */

94     public void fatalError(SAXParseException JavaDoc e)
95     throws SAXException JavaDoc {
96         throw new SAXException JavaDoc("Fatal error parsing "+e.getSystemId()+" (line "+
97                                e.getLineNumber()+" col. "+e.getColumnNumber()+
98                                "): "+e.getMessage(),e);
99     }
100
101     /**
102      * Receive notification of a warning.
103      */

104     public void warning(SAXParseException JavaDoc e)
105     throws SAXException JavaDoc {
106         throw new SAXException JavaDoc("Warning parsing "+e.getSystemId()+" (line "+
107                                e.getLineNumber()+" col. "+e.getColumnNumber()+
108                                "): "+e.getMessage(),e);
109     }
110 }
111
Popular Tags