001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.parser;
018:
019: import org.apache.avalon.framework.activity.Disposable;
020: import org.apache.avalon.framework.component.ComponentException;
021: import org.apache.avalon.framework.component.ComponentManager;
022: import org.apache.avalon.framework.component.Composable;
023: import org.apache.cocoon.components.resolver.Resolver;
024: import org.apache.cocoon.xml.AbstractXMLProducer;
025: import org.apache.xerces.dom.DocumentImpl;
026: import org.apache.xerces.dom.DocumentTypeImpl;
027: import org.apache.xerces.parsers.DOMParser;
028: import org.apache.xerces.parsers.SAXParser;
029: import org.w3c.dom.Document;
030: import org.xml.sax.ErrorHandler;
031: import org.xml.sax.InputSource;
032: import org.xml.sax.SAXException;
033: import org.xml.sax.SAXParseException;
034:
035: import java.io.IOException;
036:
037: /**
038: *
039: * @deprecated The Avalon XML Parser is now used inside Cocoon. This role
040: * will be removed in future releases.
041:
042: * @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
043: * (Apache Software Foundation)
044: * @version CVS $Id: XercesParser.java 433543 2006-08-22 06:22:54Z crossley $
045: */
046: public class XercesParser extends AbstractXMLProducer implements
047: Parser, ErrorHandler, Composable, Disposable {
048:
049: /** the SAX Parser */
050: final SAXParser parser;
051:
052: /** the component manager */
053: protected ComponentManager manager;
054:
055: /** the Entity Resolver */
056: protected Resolver resolver = null;
057:
058: public XercesParser() throws SAXException {
059: this .parser = new SAXParser();
060:
061: this .parser.setFeature(
062: "http://xml.org/sax/features/validation", false);
063: this .parser.setFeature(
064: "http://xml.org/sax/features/namespaces", true);
065: }
066:
067: /**
068: * Get the Entity Resolver from the component manager
069: */
070: public void compose(ComponentManager manager)
071: throws ComponentException {
072: this .manager = manager;
073: if (getLogger().isDebugEnabled()) {
074: getLogger().debug("Looking up " + Resolver.ROLE);
075: }
076: if (manager.hasComponent(Resolver.ROLE)) {
077: this .resolver = (Resolver) manager.lookup(Resolver.ROLE);
078: }
079: }
080:
081: /**
082: * Dispose
083: */
084: public void dispose() {
085: if (this .manager != null) {
086: this .manager.release(this .resolver);
087: }
088: }
089:
090: public void parse(InputSource in) throws SAXException, IOException {
091: this .parser.setProperty(
092: "http://xml.org/sax/properties/lexical-handler",
093: super .lexicalHandler);
094: this .parser.setErrorHandler(this );
095: this .parser.setContentHandler(super .contentHandler);
096: if (this .resolver != null)
097: this .parser.setEntityResolver(this .resolver);
098: this .parser.parse(in);
099: }
100:
101: /**
102: * Create a new Document object.
103: */
104: public Document newDocument() {
105: return (newDocument(null, null, null));
106: }
107:
108: /**
109: * Create a new Document object with a specified DOCTYPE.
110: */
111: public Document newDocument(String name) {
112: return (newDocument(name, null, null));
113: }
114:
115: /**
116: * Create a new Document object with a specified DOCTYPE, public ID and
117: * system ID.
118: */
119: public Document newDocument(String name, String pub, String sys) {
120: DocumentImpl doc = new DocumentImpl();
121: if ((pub != null) || (sys != null)) {
122: DocumentTypeImpl dtd = new DocumentTypeImpl(doc, name, pub,
123: sys);
124: doc.appendChild(dtd);
125: } else if (name != null) {
126: DocumentTypeImpl dtd = new DocumentTypeImpl(doc, name);
127: doc.appendChild(dtd);
128: }
129: return (doc);
130: }
131:
132: /**
133: * Parses a new Document object from the given InputSource.
134: */
135: public Document parseDocument(InputSource input)
136: throws SAXException, IOException {
137: DOMParser parser = null;
138:
139: try {
140: parser = new DOMParser();
141:
142: parser.setFeature("http://xml.org/sax/features/validation",
143: false);
144: parser.setFeature("http://xml.org/sax/features/namespaces",
145: true);
146:
147: parser.parse(input);
148: } catch (Exception pce) {
149: getLogger().error("Could not build DocumentBuilder", pce);
150: return null;
151: }
152:
153: return parser.getDocument();
154: }
155:
156: /**
157: * Receive notification of a recoverable error.
158: */
159: public void error(SAXParseException e) throws SAXException {
160: throw new SAXException("Error parsing " + e.getSystemId()
161: + " (line " + e.getLineNumber() + " col. "
162: + e.getColumnNumber() + "): " + e.getMessage(), e);
163: }
164:
165: /**
166: * Receive notification of a fatal error.
167: */
168: public void fatalError(SAXParseException e) throws SAXException {
169: throw new SAXException("Fatal error parsing " + e.getSystemId()
170: + " (line " + e.getLineNumber() + " col. "
171: + e.getColumnNumber() + "): " + e.getMessage(), e);
172: }
173:
174: /**
175: * Receive notification of a warning.
176: */
177: public void warning(SAXParseException e) throws SAXException {
178: throw new SAXException("Warning parsing " + e.getSystemId()
179: + " (line " + e.getLineNumber() + " col. "
180: + e.getColumnNumber() + "): " + e.getMessage(), e);
181: }
182: }
|