1. package org.apache.xalan.transformer;
  2. import java.io.IOException;
  3. import javax.xml.transform.TransformerException;
  4. import org.xml.sax.InputSource;
  5. import org.xml.sax.XMLReader;
  6. import org.xml.sax.XMLFilter;
  7. import org.xml.sax.ContentHandler;
  8. import org.xml.sax.EntityResolver;
  9. import org.xml.sax.DTDHandler;
  10. import org.xml.sax.ext.LexicalHandler;
  11. import org.xml.sax.helpers.XMLFilterImpl;
  12. import org.xml.sax.helpers.XMLReaderFactory;
  13. import javax.xml.transform.Templates;
  14. import javax.xml.transform.TransformerConfigurationException;
  15. import javax.xml.transform.ErrorListener;
  16. import org.apache.xalan.res.XSLTErrorResources;
  17. import org.apache.xalan.res.XSLMessages;
  18. public class TrAXFilter extends XMLFilterImpl
  19. {
  20. private Templates m_templates;
  21. private TransformerImpl m_transformer;
  22. /**
  23. * Construct an empty XML filter, with no parent.
  24. *
  25. * <p>This filter will have no parent: you must assign a parent
  26. * before you start a parse or do any configuration with
  27. * setFeature or setProperty.</p>
  28. *
  29. * @see org.xml.sax.XMLReader#setFeature
  30. * @see org.xml.sax.XMLReader#setProperty
  31. */
  32. public TrAXFilter (Templates templates)
  33. throws TransformerConfigurationException
  34. {
  35. m_templates = templates;
  36. m_transformer = (TransformerImpl)templates.newTransformer();
  37. }
  38. /** Set the parent reader.
  39. *
  40. * <p>This is the {@link org.xml.sax.XMLReader XMLReader} from which
  41. * this filter will obtain its events and to which it will pass its
  42. * configuration requests. The parent may itself be another filter.</p>
  43. *
  44. * <p>If there is no parent reader set, any attempt to parse
  45. * or to set or get a feature or property will fail.</p>
  46. *
  47. * @param parent The parent XML reader.
  48. * @throws java.lang.NullPointerException If the parent is null.
  49. */
  50. public void setParent (XMLReader parent)
  51. {
  52. super.setParent(parent);
  53. if(null != parent.getContentHandler())
  54. this.setContentHandler(parent.getContentHandler());
  55. // Not really sure if we should do this here, but
  56. // it seems safer in case someone calls parse() on
  57. // the parent.
  58. setupParse ();
  59. }
  60. /**
  61. * Parse a document.
  62. *
  63. * @param input The input source for the document entity.
  64. * @throws org.xml.sax.SAXException Any SAX exception, possibly
  65. * wrapping another exception.
  66. * @throws java.io.IOException An IO exception from the parser,
  67. * possibly from a byte stream or character stream
  68. * supplied by the application.
  69. * @see org.xml.sax.XMLReader#parse(org.xml.sax.InputSource)
  70. */
  71. public void parse (InputSource input)
  72. throws org.xml.sax.SAXException, IOException
  73. {
  74. if(null == getParent())
  75. {
  76. XMLReader reader=null;
  77. // Use JAXP1.1 ( if possible )
  78. try {
  79. javax.xml.parsers.SAXParserFactory factory=
  80. javax.xml.parsers.SAXParserFactory.newInstance();
  81. factory.setNamespaceAware( true );
  82. javax.xml.parsers.SAXParser jaxpParser=
  83. factory.newSAXParser();
  84. reader=jaxpParser.getXMLReader();
  85. } catch( javax.xml.parsers.ParserConfigurationException ex ) {
  86. throw new org.xml.sax.SAXException( ex );
  87. } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) {
  88. throw new org.xml.sax.SAXException( ex1.toString() );
  89. } catch( NoSuchMethodError ex2 ) {
  90. }
  91. catch (AbstractMethodError ame){}
  92. XMLReader parent;
  93. if( reader==null )
  94. parent= XMLReaderFactory.createXMLReader();
  95. else
  96. parent=reader;
  97. try
  98. {
  99. parent.setFeature("http://xml.org/sax/features/namespace-prefixes",
  100. true);
  101. // Commented out as per discussion with Thomas2.Maesing@bgs-ag.de
  102. // about bug 2124.
  103. // parent.setFeature("http://apache.org/xml/features/validation/dynamic",
  104. // true);
  105. }
  106. catch (org.xml.sax.SAXException se){}
  107. // setParent calls setupParse...
  108. setParent(parent);
  109. }
  110. else
  111. {
  112. // Make sure everything is set up.
  113. setupParse ();
  114. }
  115. if(null == m_transformer.getContentHandler())
  116. {
  117. 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!");
  118. }
  119. getParent().parse(input);
  120. Exception e = m_transformer.getExceptionThrown();
  121. if(null != e)
  122. {
  123. if(e instanceof org.xml.sax.SAXException)
  124. throw (org.xml.sax.SAXException)e;
  125. else
  126. throw new org.xml.sax.SAXException(e);
  127. }
  128. }
  129. /**
  130. * Parse a document.
  131. *
  132. * @param systemId The system identifier as a fully-qualified URI.
  133. * @throws org.xml.sax.SAXException Any SAX exception, possibly
  134. * wrapping another exception.
  135. * @throws java.io.IOException An IO exception from the parser,
  136. * possibly from a byte stream or character stream
  137. * supplied by the application.
  138. * @see org.xml.sax.XMLReader#parse(java.lang.String)
  139. */
  140. public void parse (String systemId)
  141. throws org.xml.sax.SAXException, IOException
  142. {
  143. parse(new InputSource(systemId));
  144. }
  145. /**
  146. * Set up before a parse.
  147. *
  148. * <p>Before every parse, check whether the parent is
  149. * non-null, and re-register the filter for all of the
  150. * events.</p>
  151. */
  152. private void setupParse ()
  153. {
  154. XMLReader p = getParent();
  155. if (p == null) {
  156. throw new NullPointerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_PARENT_FOR_FILTER, null)); //"No parent for filter");
  157. }
  158. ContentHandler ch = m_transformer.getInputContentHandler();
  159. // if(ch instanceof SourceTreeHandler)
  160. // ((SourceTreeHandler)ch).setUseMultiThreading(true);
  161. p.setContentHandler(ch);
  162. if(ch instanceof EntityResolver)
  163. p.setEntityResolver((EntityResolver)ch);
  164. else
  165. p.setEntityResolver(this);
  166. if(ch instanceof DTDHandler)
  167. p.setDTDHandler((DTDHandler)ch);
  168. else
  169. p.setDTDHandler(this);
  170. ErrorListener elistener = m_transformer.getErrorListener();
  171. if((null != elistener) && (elistener instanceof org.xml.sax.ErrorHandler))
  172. p.setErrorHandler((org.xml.sax.ErrorHandler)elistener);
  173. else
  174. p.setErrorHandler(this);
  175. }
  176. /**
  177. * Set the content event handler.
  178. *
  179. * @param resolver The new content handler.
  180. * @throws java.lang.NullPointerException If the handler
  181. * is null.
  182. * @see org.xml.sax.XMLReader#setContentHandler
  183. */
  184. public void setContentHandler (ContentHandler handler)
  185. {
  186. m_transformer.setContentHandler(handler);
  187. // super.setContentHandler(m_transformer.getResultTreeHandler());
  188. }
  189. public void setErrorListener (ErrorListener handler)
  190. {
  191. m_transformer.setErrorListener(handler);
  192. }
  193. }