6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 2001-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* 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 |
* $Id: TrAXFilter.java,v 1.2.4.1 2005/09/06 12:23:19 pvedula Exp $
|
|
22 |
*/
|
|
23 |
|
|
24 |
|
|
25 |
package com.sun.org.apache.xalan.internal.xsltc.trax;
|
|
26 |
|
|
27 |
import java.io.IOException;
|
|
28 |
|
|
29 |
import javax.xml.XMLConstants;
|
|
30 |
import javax.xml.parsers.FactoryConfigurationError;
|
|
31 |
import javax.xml.parsers.ParserConfigurationException;
|
|
32 |
import javax.xml.parsers.SAXParser;
|
|
33 |
import javax.xml.parsers.SAXParserFactory;
|
|
34 |
import javax.xml.transform.ErrorListener;
|
|
35 |
import javax.xml.transform.Templates;
|
|
36 |
import javax.xml.transform.Transformer;
|
|
37 |
import javax.xml.transform.TransformerConfigurationException;
|
|
38 |
import javax.xml.transform.sax.SAXResult;
|
|
39 |
|
|
40 |
import com.sun.org.apache.xml.internal.utils.XMLReaderManager;
|
|
41 |
|
|
42 |
import org.xml.sax.ContentHandler;
|
|
43 |
import org.xml.sax.InputSource;
|
|
44 |
import org.xml.sax.SAXException;
|
|
45 |
import org.xml.sax.XMLReader;
|
|
46 |
import org.xml.sax.helpers.XMLFilterImpl;
|
|
47 |
import org.xml.sax.helpers.XMLReaderFactory;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* skeleton extension of XMLFilterImpl for now.
|
|
51 |
* @author Santiago Pericas-Geertsen
|
|
52 |
* @author G. Todd Miller
|
|
53 |
*/
|
|
54 |
public class TrAXFilter extends XMLFilterImpl {
|
|
55 |
private Templates _templates;
|
|
56 |
private TransformerImpl _transformer;
|
|
57 |
private TransformerHandlerImpl _transformerHandler;
|
|
58 |
|
|
59 |
public TrAXFilter(Templates templates) throws
|
|
60 |
TransformerConfigurationException
|
|
61 |
{
|
|
62 |
_templates = templates;
|
|
63 |
_transformer = (TransformerImpl) templates.newTransformer();
|
|
64 |
_transformerHandler = new TransformerHandlerImpl(_transformer);
|
|
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()
|
|
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().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 |
}
|