KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > parser > XercesParser


1 /*
2  * Copyright 1999-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 package org.apache.cocoon.components.parser;
17
18 import org.apache.avalon.framework.activity.Disposable;
19 import org.apache.avalon.framework.component.ComponentException;
20 import org.apache.avalon.framework.component.ComponentManager;
21 import org.apache.avalon.framework.component.Composable;
22 import org.apache.cocoon.components.resolver.Resolver;
23 import org.apache.cocoon.xml.AbstractXMLProducer;
24 import org.apache.xerces.dom.DocumentImpl;
25 import org.apache.xerces.dom.DocumentTypeImpl;
26 import org.apache.xerces.parsers.DOMParser;
27 import org.apache.xerces.parsers.SAXParser;
28 import org.w3c.dom.Document 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
34 import java.io.IOException JavaDoc;
35
36 /**
37  *
38  * @deprecated The Avalon XML Parser is now used inside Cocoon. This role
39  * will be removed in future releases.
40
41  * @author <a HREF="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
42  * (Apache Software Foundation)
43  * @version CVS $Id: XercesParser.java 30932 2004-07-29 17:35:38Z vgritsenko $
44  */

45 public class XercesParser extends AbstractXMLProducer
46 implements Parser, ErrorHandler JavaDoc, Composable, Disposable {
47
48     /** the SAX Parser */
49     final SAXParser parser;
50
51     /** the component manager */
52     protected ComponentManager manager;
53
54     /** the Entity Resolver */
55     protected Resolver resolver = null;
56
57     public XercesParser ()
58     throws SAXException JavaDoc {
59         this.parser = new SAXParser();
60
61         this.parser.setFeature("http://xml.org/sax/features/validation", false);
62         this.parser.setFeature("http://xml.org/sax/features/namespaces", true);
63     }
64
65     /**
66      * Get the Entity Resolver from the component manager
67      */

68     public void compose(ComponentManager manager) throws ComponentException {
69         this.manager = manager;
70         if (getLogger().isDebugEnabled()) {
71             getLogger().debug("Looking up " + Resolver.ROLE);
72         }
73         if ( manager.hasComponent( Resolver.ROLE ) ) {
74             this.resolver = (Resolver)manager.lookup(Resolver.ROLE);
75         }
76     }
77
78     /**
79      * Dispose
80      */

81     public void dispose() {
82         if (this.manager != null) {
83             this.manager.release( this.resolver );
84         }
85     }
86
87     public void parse(InputSource JavaDoc in)
88     throws SAXException JavaDoc, IOException JavaDoc {
89         this.parser.setProperty("http://xml.org/sax/properties/lexical-handler",
90                                 super.lexicalHandler);
91         this.parser.setErrorHandler(this);
92         this.parser.setContentHandler(super.contentHandler);
93         if(this.resolver != null)
94             this.parser.setEntityResolver(this.resolver);
95         this.parser.parse(in);
96     }
97
98     /**
99      * Create a new Document object.
100      */

101     public Document JavaDoc newDocument() {
102         return(newDocument(null,null,null));
103     }
104
105     /**
106      * Create a new Document object with a specified DOCTYPE.
107      */

108     public Document JavaDoc newDocument(String JavaDoc name) {
109         return(newDocument(name,null,null));
110     }
111
112     /**
113      * Create a new Document object with a specified DOCTYPE, public ID and
114      * system ID.
115      */

116     public Document JavaDoc newDocument(String JavaDoc name, String JavaDoc pub, String JavaDoc sys) {
117         DocumentImpl doc=new DocumentImpl();
118         if ((pub!=null)||(sys!=null)) {
119             DocumentTypeImpl dtd=new DocumentTypeImpl(doc,name,pub,sys);
120             doc.appendChild(dtd);
121         } else if (name!=null) {
122             DocumentTypeImpl dtd=new DocumentTypeImpl(doc,name);
123             doc.appendChild(dtd);
124         }
125         return(doc);
126     }
127
128     /**
129      * Parses a new Document object from the given InputSource.
130      */

131     public Document JavaDoc parseDocument(InputSource JavaDoc input) throws SAXException JavaDoc, IOException JavaDoc {
132         DOMParser parser = null;
133
134         try {
135             parser = new DOMParser();
136
137             parser.setFeature("http://xml.org/sax/features/validation",false);
138             parser.setFeature("http://xml.org/sax/features/namespaces",true);
139
140             parser.parse(input);
141         } catch (Exception JavaDoc pce) {
142             getLogger().error("Could not build DocumentBuilder", pce);
143             return null;
144         }
145
146         return parser.getDocument();
147     }
148
149     /**
150      * Receive notification of a recoverable error.
151      */

152     public void error(SAXParseException JavaDoc e)
153     throws SAXException JavaDoc {
154         throw new SAXException JavaDoc("Error parsing "+e.getSystemId()+" (line "+
155                                e.getLineNumber()+" col. "+e.getColumnNumber()+
156                                "): "+e.getMessage(),e);
157     }
158
159     /**
160      * Receive notification of a fatal error.
161      */

162     public void fatalError(SAXParseException JavaDoc e)
163     throws SAXException JavaDoc {
164         throw new SAXException JavaDoc("Fatal error parsing "+e.getSystemId()+" (line "+
165                                e.getLineNumber()+" col. "+e.getColumnNumber()+
166                                "): "+e.getMessage(),e);
167     }
168
169     /**
170      * Receive notification of a warning.
171      */

172     public void warning(SAXParseException JavaDoc e)
173     throws SAXException JavaDoc {
174         throw new SAXException JavaDoc("Warning parsing "+e.getSystemId()+" (line "+
175                                e.getLineNumber()+" col. "+e.getColumnNumber()+
176                                "): "+e.getMessage(),e);
177     }
178 }
179
Popular Tags