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.validation.jing;
018:
019: import java.io.IOException;
020:
021: import javax.xml.parsers.ParserConfigurationException;
022: import javax.xml.parsers.SAXParserFactory;
023:
024: import org.xml.sax.ContentHandler;
025: import org.xml.sax.DTDHandler;
026: import org.xml.sax.ErrorHandler;
027: import org.xml.sax.InputSource;
028: import org.xml.sax.SAXException;
029: import org.xml.sax.SAXNotRecognizedException;
030: import org.xml.sax.SAXNotSupportedException;
031: import org.xml.sax.XMLReader;
032:
033: import com.thaiopensource.xml.sax.DraconianErrorHandler;
034:
035: /**
036: * <p>A trivial {@link XMLReader} implementation populating and clearing the
037: * {@link Stack} of {@link InputSource}s for URI resolution kept inside
038: * a {@link JingResolver}.</p>
039: */
040: final class JingReader implements XMLReader {
041:
042: /** <p>The underlying {@link XMLReader} to use.</p> */
043: private final XMLReader reader;
044: /** <p>The {@link JingResolver} associated with this instance.</p> */
045: private final JingResolver context;
046:
047: /**
048: * <p>Create a new {@link JingReader} instance associated with the specified
049: * {@link JingResolver}.</p>
050: */
051: protected JingReader(JingResolver context) throws SAXException {
052: /*
053: * We have to look up the XMLReader using JAXP or SAX, as the SAXParser
054: * supplied by Avalon/Excalibur does not seem to work with JING.
055: */
056: try {
057: SAXParserFactory factory = SAXParserFactory.newInstance();
058: factory.setNamespaceAware(true);
059: factory.setValidating(false);
060: this .reader = factory.newSAXParser().getXMLReader();
061: this .setEntityResolver(context);
062: this .setErrorHandler(new DraconianErrorHandler());
063: this .context = context;
064: } catch (ParserConfigurationException exception) {
065: throw new SAXException("Can't create XML reader instance",
066: exception);
067: }
068: }
069:
070: public boolean getFeature(String feature)
071: throws SAXNotRecognizedException, SAXNotSupportedException {
072: return this .reader.getFeature(feature);
073: }
074:
075: public void setFeature(String feature, boolean value)
076: throws SAXNotRecognizedException, SAXNotSupportedException {
077: this .reader.setFeature(feature, value);
078: }
079:
080: public Object getProperty(String property)
081: throws SAXNotRecognizedException, SAXNotSupportedException {
082: return this .reader.getProperty(property);
083: }
084:
085: public void setProperty(String property, Object value)
086: throws SAXNotRecognizedException, SAXNotSupportedException {
087: this .reader.setProperty(property, value);
088: }
089:
090: public void setEntityResolver(org.xml.sax.EntityResolver resolver) {
091: this .reader.setEntityResolver(resolver);
092: }
093:
094: public org.xml.sax.EntityResolver getEntityResolver() {
095: return this .reader.getEntityResolver();
096: }
097:
098: public void setDTDHandler(DTDHandler handler) {
099: this .reader.setDTDHandler(handler);
100: }
101:
102: public DTDHandler getDTDHandler() {
103: return this .reader.getDTDHandler();
104: }
105:
106: public void setContentHandler(ContentHandler handler) {
107: this .reader.setContentHandler(handler);
108: }
109:
110: public ContentHandler getContentHandler() {
111: return this .reader.getContentHandler();
112: }
113:
114: public void setErrorHandler(ErrorHandler handler) {
115: this .reader.setErrorHandler(handler);
116: }
117:
118: public ErrorHandler getErrorHandler() {
119: return this .reader.getErrorHandler();
120: }
121:
122: public void parse(InputSource source) throws IOException,
123: SAXException {
124: this .context.pushInputSource(source);
125: this .reader.parse(source);
126: this .context.popInputSource();
127: }
128:
129: public void parse(String source) throws IOException, SAXException {
130: this
131: .parse(this.getEntityResolver().resolveEntity(null,
132: source));
133: }
134: }
|