2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 * $Id: TrAXFilter.java 468645 2006-10-28 06:57:24Z minchau $
21 package org.apache.xalan.transformer;
23 import java.io.IOException;
25 import javax.xml.XMLConstants;
26 import javax.xml.transform.ErrorListener;
27 import javax.xml.transform.Templates;
28 import javax.xml.transform.TransformerConfigurationException;
30 import org.apache.xalan.res.XSLMessages;
31 import org.apache.xalan.res.XSLTErrorResources;
33 import org.xml.sax.ContentHandler;
34 import org.xml.sax.DTDHandler;
35 import org.xml.sax.EntityResolver;
36 import org.xml.sax.InputSource;
37 import org.xml.sax.XMLReader;
38 import org.xml.sax.helpers.XMLFilterImpl;
39 import org.xml.sax.helpers.XMLReaderFactory;
42 public class TrAXFilter extends XMLFilterImpl
44 private Templates m_templates;
45 private TransformerImpl m_transformer;
48 * Construct an empty XML filter, with no parent.
50 * <p>This filter will have no parent: you must assign a parent
51 * before you start a parse or do any configuration with
52 * setFeature or setProperty.</p>
54 * @see org.xml.sax.XMLReader#setFeature
55 * @see org.xml.sax.XMLReader#setProperty
57 public TrAXFilter (Templates templates)
58 throws TransformerConfigurationException
60 m_templates = templates;
61 m_transformer = (TransformerImpl)templates.newTransformer();
65 * Return the Transformer object used for this XML filter.
67 public TransformerImpl getTransformer()
72 /** Set the parent reader.
74 * <p>This is the {@link org.xml.sax.XMLReader XMLReader} from which
75 * this filter will obtain its events and to which it will pass its
76 * configuration requests. The parent may itself be another filter.</p>
78 * <p>If there is no parent reader set, any attempt to parse
79 * or to set or get a feature or property will fail.</p>
81 * @param parent The parent XML reader.
82 * @throws java.lang.NullPointerException If the parent is null.
84 public void setParent (XMLReader parent)
86 super.setParent(parent);
88 if(null != parent.getContentHandler())
89 this.setContentHandler(parent.getContentHandler());
91 // Not really sure if we should do this here, but
92 // it seems safer in case someone calls parse() on
100 * @param input The input source for the document entity.
101 * @throws org.xml.sax.SAXException Any SAX exception, possibly
102 * wrapping another exception.
103 * @throws java.io.IOException An IO exception from the parser,
104 * possibly from a byte stream or character stream
105 * supplied by the application.
106 * @see org.xml.sax.XMLReader#parse(org.xml.sax.InputSource)
108 public void parse (InputSource input)
109 throws org.xml.sax.SAXException, IOException
111 if(null == getParent())
113 XMLReader reader=null;
115 // Use JAXP1.1 ( if possible )
117 javax.xml.parsers.SAXParserFactory factory=
118 javax.xml.parsers.SAXParserFactory.newInstance();
119 factory.setNamespaceAware( true );
121 if (m_transformer.getStylesheet().isSecureProcessing()) {
123 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
125 catch (org.xml.sax.SAXException se) {}
128 javax.xml.parsers.SAXParser jaxpParser=
129 factory.newSAXParser();
130 reader=jaxpParser.getXMLReader();
132 } catch( javax.xml.parsers.ParserConfigurationException ex ) {
133 throw new org.xml.sax.SAXException( ex );
134 } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) {
135 throw new org.xml.sax.SAXException( ex1.toString() );
136 } catch( NoSuchMethodError ex2 ) {
138 catch (AbstractMethodError ame){}
142 parent= XMLReaderFactory.createXMLReader();
147 parent.setFeature("http://xml.org/sax/features/namespace-prefixes",
150 catch (org.xml.sax.SAXException se){}
151 // setParent calls setupParse...
156 // Make sure everything is set up.
159 if(null == m_transformer.getContentHandler())
161 throw new org.xml.sax.SAXException(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_CALL_PARSE, null)); //"parse can not be called if the ContentHandler has not been set!");
164 getParent().parse(input);
165 Exception e = m_transformer.getExceptionThrown();
168 if(e instanceof org.xml.sax.SAXException)
169 throw (org.xml.sax.SAXException)e;
171 throw new org.xml.sax.SAXException(e);
178 * @param systemId The system identifier as a fully-qualified URI.
179 * @throws org.xml.sax.SAXException Any SAX exception, possibly
180 * wrapping another exception.
181 * @throws java.io.IOException An IO exception from the parser,
182 * possibly from a byte stream or character stream
183 * supplied by the application.
184 * @see org.xml.sax.XMLReader#parse(java.lang.String)
186 public void parse (String systemId)
187 throws org.xml.sax.SAXException, IOException
189 parse(new InputSource(systemId));
194 * Set up before a parse.
196 * <p>Before every parse, check whether the parent is
197 * non-null, and re-register the filter for all of the
200 private void setupParse ()
202 XMLReader p = getParent();
204 throw new NullPointerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_PARENT_FOR_FILTER, null)); //"No parent for filter");
207 ContentHandler ch = m_transformer.getInputContentHandler();
208 // if(ch instanceof SourceTreeHandler)
209 // ((SourceTreeHandler)ch).setUseMultiThreading(true);
210 p.setContentHandler(ch);
211 p.setEntityResolver(this);
212 p.setDTDHandler(this);
213 p.setErrorHandler(this);
217 * Set the content event handler.
219 * @param handler The new content handler.
220 * @throws java.lang.NullPointerException If the handler
222 * @see org.xml.sax.XMLReader#setContentHandler
224 public void setContentHandler (ContentHandler handler)
226 m_transformer.setContentHandler(handler);
227 // super.setContentHandler(m_transformer.getResultTreeHandler());
230 public void setErrorListener (ErrorListener handler)
232 m_transformer.setErrorListener(handler);