KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.xml.impl;
18
19 import java.io.IOException JavaDoc;
20
21 import org.apache.avalon.framework.activity.Initializable;
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.avalon.framework.thread.ThreadSafe;
24 import org.apache.xerces.dom.DocumentImpl;
25 import org.apache.xerces.parsers.DOMParser;
26 import org.apache.xerces.parsers.SAXParser;
27 import org.w3c.dom.Document JavaDoc;
28 import org.xml.sax.ContentHandler JavaDoc;
29 import org.xml.sax.ErrorHandler JavaDoc;
30 import org.xml.sax.InputSource JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.SAXParseException JavaDoc;
33 import org.xml.sax.ext.LexicalHandler JavaDoc;
34
35 /**
36  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
37  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:20 $
38  */

39 public final class XercesParser
40     extends AbstractLogEnabled
41     implements org.apache.excalibur.xml.sax.SAXParser, org.apache.excalibur.xml.dom.DOMParser,
42                 ErrorHandler JavaDoc, ThreadSafe, Initializable
43 {
44     public void initialize()
45         throws Exception JavaDoc
46     {
47         final String JavaDoc message =
48             "WARNING: XercesParser has been deprecated in favour of " +
49             "JaxpParser. Please use JaxpParser unless it is incompatible" +
50             "with your environment";
51         getLogger().warn( message );
52     }
53
54     public void parse( final InputSource JavaDoc in,
55                        final ContentHandler JavaDoc consumer )
56         throws SAXException JavaDoc, IOException JavaDoc
57     {
58         if( consumer instanceof LexicalHandler JavaDoc )
59         {
60             parse( in, consumer, (LexicalHandler JavaDoc)consumer );
61         }
62         else
63         {
64             parse( in, consumer, null );
65         }
66     }
67
68     /**
69      * Parse the {@link InputSource} and send
70      * SAX events to the content handler and
71      * the lexical handler.
72      */

73     public void parse( final InputSource JavaDoc in,
74                        final ContentHandler JavaDoc contentHandler,
75                        final LexicalHandler JavaDoc lexicalHandler )
76         throws SAXException JavaDoc, IOException JavaDoc
77     {
78         final SAXParser parser = createSAXParser();
79
80         if( null != lexicalHandler )
81         {
82             parser.setProperty( "http://xml.org/sax/properties/lexical-handler",
83                                 lexicalHandler );
84         }
85         parser.setErrorHandler( this );
86         parser.setContentHandler( contentHandler );
87         parser.parse( in );
88     }
89
90     /**
91      * Parses a new Document object from the given {@link InputSource}.
92      */

93     public Document JavaDoc parseDocument( final InputSource JavaDoc input )
94         throws SAXException JavaDoc, IOException JavaDoc
95     {
96         try
97         {
98             final DOMParser parser = new DOMParser();
99             parser.setFeature( "http://xml.org/sax/features/validation", false );
100             parser.setFeature( "http://xml.org/sax/features/namespaces", true );
101             parser.setFeature( "http://xml.org/sax/features/namespace-prefixes",
102                                true );
103
104             parser.parse( input );
105
106             return parser.getDocument();
107         }
108         catch( final Exception JavaDoc e )
109         {
110             final String JavaDoc message = "Could not build DocumentBuilder";
111             getLogger().error( message, e );
112             return null;
113         }
114     }
115
116     /**
117      * Return a new {@link Document}.
118      */

119     public Document JavaDoc createDocument()
120         throws SAXException JavaDoc
121     {
122         return new DocumentImpl();
123     }
124
125     /**
126      * Receive notification of a recoverable error.
127      */

128     public void error( final SAXParseException JavaDoc spe )
129         throws SAXException JavaDoc
130     {
131         final String JavaDoc message =
132             "Error parsing " + spe.getSystemId() + " (line " +
133             spe.getLineNumber() + " col. " + spe.getColumnNumber() +
134             "): " + spe.getMessage();
135         throw new SAXException JavaDoc( message, spe );
136     }
137
138     /**
139      * Receive notification of a fatal error.
140      */

141     public void fatalError( final SAXParseException JavaDoc spe )
142         throws SAXException JavaDoc
143     {
144         final String JavaDoc message =
145             "Fatal error parsing " + spe.getSystemId() + " (line " +
146             spe.getLineNumber() + " col. " + spe.getColumnNumber() +
147             "): " + spe.getMessage();
148         throw new SAXException JavaDoc( message, spe );
149     }
150
151     /**
152      * Receive notification of a warning.
153      */

154     public void warning( final SAXParseException JavaDoc spe )
155         throws SAXException JavaDoc
156     {
157         final String JavaDoc message =
158             "Warning parsing " + spe.getSystemId() + " (line " +
159             spe.getLineNumber() + " col. " + spe.getColumnNumber() +
160             "): " + spe.getMessage();
161         throw new SAXException JavaDoc( message, spe );
162     }
163
164     /**
165      * Utility method to create a SAXParser.
166      *
167      * @return new SAXParser
168      * @throws SAXException if unable to create parser
169      */

170     private SAXParser createSAXParser()
171         throws SAXException JavaDoc
172     {
173         final SAXParser parser = new SAXParser();
174         parser.setFeature( "http://xml.org/sax/features/validation", false );
175         parser.setFeature( "http://xml.org/sax/features/namespaces", true );
176         parser.setFeature( "http://xml.org/sax/features/namespace-prefixes",
177                            true );
178         return parser;
179     }
180 }
181
Popular Tags