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