1 16 19 20 21 package org.apache.xalan.xsltc.trax; 22 23 import java.io.IOException ; 24 25 import javax.xml.parsers.FactoryConfigurationError ; 26 import javax.xml.parsers.ParserConfigurationException ; 27 import javax.xml.parsers.SAXParser ; 28 import javax.xml.parsers.SAXParserFactory ; 29 import javax.xml.transform.ErrorListener ; 30 import javax.xml.transform.Templates ; 31 import javax.xml.transform.Transformer ; 32 import javax.xml.transform.TransformerConfigurationException ; 33 import javax.xml.transform.sax.SAXResult ; 34 35 import org.apache.xml.utils.XMLReaderManager; 36 37 import org.xml.sax.ContentHandler ; 38 import org.xml.sax.InputSource ; 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.XMLReader ; 41 import org.xml.sax.helpers.XMLFilterImpl ; 42 import org.xml.sax.helpers.XMLReaderFactory ; 43 44 49 public class TrAXFilter extends XMLFilterImpl { 50 private Templates _templates; 51 private TransformerImpl _transformer; 52 private TransformerHandlerImpl _transformerHandler; 53 54 public TrAXFilter(Templates templates) throws 55 TransformerConfigurationException 56 { 57 _templates = templates; 58 _transformer = (TransformerImpl) templates.newTransformer(); 59 _transformerHandler = new TransformerHandlerImpl(_transformer); 60 } 61 62 public Transformer getTransformer() { 63 return _transformer; 64 } 65 66 private void createParent() throws SAXException { 67 XMLReader parent = null; 68 try { 69 SAXParserFactory pfactory = SAXParserFactory.newInstance(); 70 pfactory.setNamespaceAware(true); 71 SAXParser saxparser = pfactory.newSAXParser(); 72 parent = saxparser.getXMLReader(); 73 } 74 catch (ParserConfigurationException e) { 75 throw new SAXException (e); 76 } 77 catch (FactoryConfigurationError e) { 78 throw new SAXException (e.toString()); 79 } 80 81 if (parent == null) { 82 parent = XMLReaderFactory.createXMLReader(); 83 } 84 85 setParent(parent); 87 } 88 89 public void parse (InputSource input) throws SAXException , IOException 90 { 91 XMLReader managedReader = null; 92 93 try { 94 if (getParent() == null) { 95 try { 96 managedReader = XMLReaderManager.getInstance() 97 .getXMLReader(); 98 setParent(managedReader); 99 } catch (SAXException e) { 100 throw new SAXException (e.toString()); 101 } 102 } 103 104 getParent().parse(input); 106 } finally { 107 if (managedReader != null) { 108 XMLReaderManager.getInstance().releaseXMLReader(managedReader); 109 } 110 } 111 } 112 113 public void parse (String systemId) throws SAXException , IOException 114 { 115 parse(new InputSource (systemId)); 116 } 117 118 public void setContentHandler (ContentHandler handler) 119 { 120 _transformerHandler.setResult(new SAXResult (handler)); 121 if (getParent() == null) { 122 try { 123 createParent(); 124 } 125 catch (SAXException e) { 126 return; 127 } 128 } 129 getParent().setContentHandler(_transformerHandler); 130 } 131 132 public void setErrorListener (ErrorListener handler) { } 133 } 134 | Popular Tags |