1 16 19 package org.apache.xalan.transformer; 20 21 import java.io.IOException ; 22 23 import javax.xml.transform.ErrorListener ; 24 import javax.xml.transform.Templates ; 25 import javax.xml.transform.TransformerConfigurationException ; 26 27 import org.apache.xalan.res.XSLMessages; 28 import org.apache.xalan.res.XSLTErrorResources; 29 30 import org.xml.sax.ContentHandler ; 31 import org.xml.sax.DTDHandler ; 32 import org.xml.sax.EntityResolver ; 33 import org.xml.sax.InputSource ; 34 import org.xml.sax.XMLReader ; 35 import org.xml.sax.helpers.XMLFilterImpl ; 36 import org.xml.sax.helpers.XMLReaderFactory ; 37 38 39 public class TrAXFilter extends XMLFilterImpl 40 { 41 private Templates m_templates; 42 private TransformerImpl m_transformer; 43 44 54 public TrAXFilter (Templates templates) 55 throws TransformerConfigurationException 56 { 57 m_templates = templates; 58 m_transformer = (TransformerImpl)templates.newTransformer(); 59 } 60 61 64 public TransformerImpl getTransformer() 65 { 66 return m_transformer; 67 } 68 69 81 public void setParent (XMLReader parent) 82 { 83 super.setParent(parent); 84 85 if(null != parent.getContentHandler()) 86 this.setContentHandler(parent.getContentHandler()); 87 88 setupParse (); 92 } 93 94 105 public void parse (InputSource input) 106 throws org.xml.sax.SAXException , IOException 107 { 108 if(null == getParent()) 109 { 110 XMLReader reader=null; 111 112 try { 114 javax.xml.parsers.SAXParserFactory factory= 115 javax.xml.parsers.SAXParserFactory.newInstance(); 116 factory.setNamespaceAware( true ); 117 javax.xml.parsers.SAXParser jaxpParser= 118 factory.newSAXParser(); 119 reader=jaxpParser.getXMLReader(); 120 121 } catch( javax.xml.parsers.ParserConfigurationException ex ) { 122 throw new org.xml.sax.SAXException ( ex ); 123 } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) { 124 throw new org.xml.sax.SAXException ( ex1.toString() ); 125 } catch( NoSuchMethodError ex2 ) { 126 } 127 catch (AbstractMethodError ame){} 128 129 XMLReader parent; 130 if( reader==null ) 131 parent= XMLReaderFactory.createXMLReader(); 132 else 133 parent=reader; 134 try 135 { 136 parent.setFeature("http://xml.org/sax/features/namespace-prefixes", 137 true); 138 } 139 catch (org.xml.sax.SAXException se){} 140 setParent(parent); 142 } 143 else 144 { 145 setupParse (); 147 } 148 if(null == m_transformer.getContentHandler()) 149 { 150 throw new org.xml.sax.SAXException (XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_CALL_PARSE, null)); } 152 153 getParent().parse(input); 154 Exception e = m_transformer.getExceptionThrown(); 155 if(null != e) 156 { 157 if(e instanceof org.xml.sax.SAXException ) 158 throw (org.xml.sax.SAXException )e; 159 else 160 throw new org.xml.sax.SAXException (e); 161 } 162 } 163 164 175 public void parse (String systemId) 176 throws org.xml.sax.SAXException , IOException 177 { 178 parse(new InputSource (systemId)); 179 } 180 181 182 189 private void setupParse () 190 { 191 XMLReader p = getParent(); 192 if (p == null) { 193 throw new NullPointerException (XSLMessages.createMessage(XSLTErrorResources.ER_NO_PARENT_FOR_FILTER, null)); } 195 196 ContentHandler ch = m_transformer.getInputContentHandler(); 197 p.setContentHandler(ch); 200 p.setEntityResolver(this); 201 p.setDTDHandler(this); 202 p.setErrorHandler(this); 203 } 204 205 213 public void setContentHandler (ContentHandler handler) 214 { 215 m_transformer.setContentHandler(handler); 216 } 218 219 public void setErrorListener (ErrorListener handler) 220 { 221 m_transformer.setErrorListener(handler); 222 } 223 224 } 225 | Popular Tags |