286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2001-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: TrAXFilter.java,v 1.2.4.1 2005/09/06 12:23:19 pvedula Exp $
286N/A */
286N/A
286N/A
286N/Apackage com.sun.org.apache.xalan.internal.xsltc.trax;
286N/A
286N/Aimport java.io.IOException;
286N/A
286N/Aimport javax.xml.XMLConstants;
286N/Aimport javax.xml.parsers.FactoryConfigurationError;
286N/Aimport javax.xml.parsers.ParserConfigurationException;
286N/Aimport javax.xml.parsers.SAXParser;
286N/Aimport javax.xml.parsers.SAXParserFactory;
286N/Aimport javax.xml.transform.ErrorListener;
286N/Aimport javax.xml.transform.Templates;
286N/Aimport javax.xml.transform.Transformer;
286N/Aimport javax.xml.transform.TransformerConfigurationException;
286N/Aimport javax.xml.transform.sax.SAXResult;
286N/A
286N/Aimport com.sun.org.apache.xml.internal.utils.XMLReaderManager;
286N/A
286N/Aimport org.xml.sax.ContentHandler;
286N/Aimport org.xml.sax.InputSource;
286N/Aimport org.xml.sax.SAXException;
286N/Aimport org.xml.sax.XMLReader;
286N/Aimport org.xml.sax.helpers.XMLFilterImpl;
286N/Aimport org.xml.sax.helpers.XMLReaderFactory;
286N/A
286N/A/**
286N/A * skeleton extension of XMLFilterImpl for now.
286N/A * @author Santiago Pericas-Geertsen
286N/A * @author G. Todd Miller
286N/A */
286N/Apublic class TrAXFilter extends XMLFilterImpl {
286N/A private Templates _templates;
286N/A private TransformerImpl _transformer;
286N/A private TransformerHandlerImpl _transformerHandler;
286N/A private boolean _useServicesMechanism = true;
286N/A
286N/A public TrAXFilter(Templates templates) throws
286N/A TransformerConfigurationException
286N/A {
286N/A _templates = templates;
286N/A _transformer = (TransformerImpl) templates.newTransformer();
286N/A _transformerHandler = new TransformerHandlerImpl(_transformer);
286N/A _useServicesMechanism = _transformer.useServicesMechnism();
286N/A }
286N/A
286N/A public Transformer getTransformer() {
286N/A return _transformer;
286N/A }
286N/A
286N/A private void createParent() throws SAXException {
286N/A XMLReader parent = null;
286N/A try {
286N/A SAXParserFactory pfactory = SAXParserFactory.newInstance();
286N/A pfactory.setNamespaceAware(true);
286N/A
286N/A if (_transformer.isSecureProcessing()) {
286N/A try {
286N/A pfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
286N/A }
286N/A catch (SAXException e) {}
286N/A }
286N/A
286N/A SAXParser saxparser = pfactory.newSAXParser();
286N/A parent = saxparser.getXMLReader();
286N/A }
286N/A catch (ParserConfigurationException e) {
286N/A throw new SAXException(e);
286N/A }
286N/A catch (FactoryConfigurationError e) {
286N/A throw new SAXException(e.toString());
286N/A }
286N/A
286N/A if (parent == null) {
286N/A parent = XMLReaderFactory.createXMLReader();
286N/A }
286N/A
286N/A // make this XMLReader the parent of this filter
286N/A setParent(parent);
286N/A }
286N/A
286N/A public void parse (InputSource input) throws SAXException, IOException
286N/A {
286N/A XMLReader managedReader = null;
286N/A
286N/A try {
286N/A if (getParent() == null) {
286N/A try {
286N/A managedReader = XMLReaderManager.getInstance(_useServicesMechanism)
286N/A .getXMLReader();
286N/A setParent(managedReader);
286N/A } catch (SAXException e) {
286N/A throw new SAXException(e.toString());
286N/A }
286N/A }
286N/A
286N/A // call parse on the parent
286N/A getParent().parse(input);
286N/A } finally {
286N/A if (managedReader != null) {
286N/A XMLReaderManager.getInstance(_useServicesMechanism).releaseXMLReader(managedReader);
286N/A }
286N/A }
286N/A }
286N/A
286N/A public void parse (String systemId) throws SAXException, IOException
286N/A {
286N/A parse(new InputSource(systemId));
286N/A }
286N/A
286N/A public void setContentHandler (ContentHandler handler)
286N/A {
286N/A _transformerHandler.setResult(new SAXResult(handler));
286N/A if (getParent() == null) {
286N/A try {
286N/A createParent();
286N/A }
286N/A catch (SAXException e) {
286N/A return;
286N/A }
286N/A }
286N/A getParent().setContentHandler(_transformerHandler);
286N/A }
286N/A
286N/A public void setErrorListener (ErrorListener handler) { }
286N/A}