1/*
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3 */
4/*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements.  See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License.  You may obtain a copy of the License at
11 *
12 *      http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21
22package com.sun.org.apache.xalan.internal.xsltc.trax;
23
24import java.io.IOException;
25
26import javax.xml.XMLConstants;
27import javax.xml.parsers.FactoryConfigurationError;
28import javax.xml.parsers.ParserConfigurationException;
29import javax.xml.parsers.SAXParser;
30import javax.xml.parsers.SAXParserFactory;
31import javax.xml.transform.ErrorListener;
32import javax.xml.transform.Templates;
33import javax.xml.transform.Transformer;
34import javax.xml.transform.TransformerConfigurationException;
35import javax.xml.transform.sax.SAXResult;
36
37import com.sun.org.apache.xml.internal.utils.XMLReaderManager;
38
39import org.xml.sax.ContentHandler;
40import org.xml.sax.InputSource;
41import org.xml.sax.SAXException;
42import org.xml.sax.XMLReader;
43import org.xml.sax.helpers.XMLFilterImpl;
44import org.xml.sax.helpers.XMLReaderFactory;
45
46/**
47 * skeleton extension of XMLFilterImpl for now.
48 * @author Santiago Pericas-Geertsen
49 * @author G. Todd Miller
50 */
51@SuppressWarnings("deprecation") //org.xml.sax.helpers.XMLReaderFactory
52public class TrAXFilter extends XMLFilterImpl {
53    private Templates              _templates;
54    private TransformerImpl        _transformer;
55    private TransformerHandlerImpl _transformerHandler;
56    private boolean _useServicesMechanism = true;
57
58    public TrAXFilter(Templates templates)  throws
59        TransformerConfigurationException
60    {
61        _templates = templates;
62        _transformer = (TransformerImpl) templates.newTransformer();
63        _transformerHandler = new TransformerHandlerImpl(_transformer);
64        _useServicesMechanism = _transformer.useServicesMechnism();
65    }
66
67    public Transformer getTransformer() {
68        return _transformer;
69    }
70
71    private void createParent() throws SAXException {
72        XMLReader parent = null;
73        try {
74            SAXParserFactory pfactory = SAXParserFactory.newInstance();
75            pfactory.setNamespaceAware(true);
76
77            if (_transformer.isSecureProcessing()) {
78                try {
79                    pfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
80                }
81                catch (SAXException e) {}
82            }
83
84            SAXParser saxparser = pfactory.newSAXParser();
85            parent = saxparser.getXMLReader();
86        }
87        catch (ParserConfigurationException e) {
88            throw new SAXException(e);
89        }
90        catch (FactoryConfigurationError e) {
91            throw new SAXException(e.toString());
92        }
93
94        if (parent == null) {
95            parent = XMLReaderFactory.createXMLReader();
96        }
97
98        // make this XMLReader the parent of this filter
99        setParent(parent);
100    }
101
102    public void parse (InputSource input) throws SAXException, IOException
103    {
104        XMLReader managedReader = null;
105
106        try {
107            if (getParent() == null) {
108                try {
109                    managedReader = XMLReaderManager.getInstance(_useServicesMechanism)
110                                                    .getXMLReader();
111                    setParent(managedReader);
112                } catch (SAXException  e) {
113                    throw new SAXException(e.toString());
114                }
115            }
116
117            // call parse on the parent
118            getParent().parse(input);
119        } finally {
120            if (managedReader != null) {
121                XMLReaderManager.getInstance(_useServicesMechanism).releaseXMLReader(managedReader);
122            }
123        }
124    }
125
126    public void parse (String systemId) throws SAXException, IOException
127    {
128        parse(new InputSource(systemId));
129    }
130
131    public void setContentHandler (ContentHandler handler)
132    {
133        _transformerHandler.setResult(new SAXResult(handler));
134        if (getParent() == null) {
135                try {
136                    createParent();
137                }
138                catch (SAXException  e) {
139                   return;
140                }
141        }
142        getParent().setContentHandler(_transformerHandler);
143    }
144
145    public void setErrorListener (ErrorListener handler) { }
146}
147