001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: TrAXFilter.java,v 1.9 2005/06/08 13:10:08 mkwan Exp $
018: */
019:
020: package org.apache.xalan.xsltc.trax;
021:
022: import java.io.IOException;
023:
024: import javax.xml.XMLConstants;
025: import javax.xml.parsers.FactoryConfigurationError;
026: import javax.xml.parsers.ParserConfigurationException;
027: import javax.xml.parsers.SAXParser;
028: import javax.xml.parsers.SAXParserFactory;
029: import javax.xml.transform.ErrorListener;
030: import javax.xml.transform.Templates;
031: import javax.xml.transform.Transformer;
032: import javax.xml.transform.TransformerConfigurationException;
033: import javax.xml.transform.sax.SAXResult;
034:
035: import org.apache.xml.utils.XMLReaderManager;
036:
037: import org.xml.sax.ContentHandler;
038: import org.xml.sax.InputSource;
039: import org.xml.sax.SAXException;
040: import org.xml.sax.XMLReader;
041: import org.xml.sax.helpers.XMLFilterImpl;
042: import org.xml.sax.helpers.XMLReaderFactory;
043:
044: /**
045: * skeleton extension of XMLFilterImpl for now.
046: * @author Santiago Pericas-Geertsen
047: * @author G. Todd Miller
048: */
049: public class TrAXFilter extends XMLFilterImpl {
050: private Templates _templates;
051: private TransformerImpl _transformer;
052: private TransformerHandlerImpl _transformerHandler;
053:
054: public TrAXFilter(Templates templates)
055: throws TransformerConfigurationException {
056: _templates = templates;
057: _transformer = (TransformerImpl) templates.newTransformer();
058: _transformerHandler = new TransformerHandlerImpl(_transformer);
059: }
060:
061: public Transformer getTransformer() {
062: return _transformer;
063: }
064:
065: private void createParent() throws SAXException {
066: XMLReader parent = null;
067: try {
068: SAXParserFactory pfactory = SAXParserFactory.newInstance();
069: pfactory.setNamespaceAware(true);
070:
071: if (_transformer.isSecureProcessing()) {
072: try {
073: pfactory.setFeature(
074: XMLConstants.FEATURE_SECURE_PROCESSING,
075: true);
076: } catch (SAXException e) {
077: }
078: }
079:
080: SAXParser saxparser = pfactory.newSAXParser();
081: parent = saxparser.getXMLReader();
082: } catch (ParserConfigurationException e) {
083: throw new SAXException(e);
084: } catch (FactoryConfigurationError e) {
085: throw new SAXException(e.toString());
086: }
087:
088: if (parent == null) {
089: parent = XMLReaderFactory.createXMLReader();
090: }
091:
092: // make this XMLReader the parent of this filter
093: setParent(parent);
094: }
095:
096: public void parse(InputSource input) throws SAXException,
097: IOException {
098: XMLReader managedReader = null;
099:
100: try {
101: if (getParent() == null) {
102: try {
103: managedReader = XMLReaderManager.getInstance()
104: .getXMLReader();
105: setParent(managedReader);
106: } catch (SAXException e) {
107: throw new SAXException(e.toString());
108: }
109: }
110:
111: // call parse on the parent
112: getParent().parse(input);
113: } finally {
114: if (managedReader != null) {
115: XMLReaderManager.getInstance().releaseXMLReader(
116: managedReader);
117: }
118: }
119: }
120:
121: public void parse(String systemId) throws SAXException, IOException {
122: parse(new InputSource(systemId));
123: }
124:
125: public void setContentHandler(ContentHandler handler) {
126: _transformerHandler.setResult(new SAXResult(handler));
127: if (getParent() == null) {
128: try {
129: createParent();
130: } catch (SAXException e) {
131: return;
132: }
133: }
134: getParent().setContentHandler(_transformerHandler);
135: }
136:
137: public void setErrorListener(ErrorListener handler) {
138: }
139: }
|