1 51 package org.apache.fop.apps; 52 53 54 import javax.xml.transform.TransformerFactory ; 56 import javax.xml.transform.Transformer ; 57 import javax.xml.transform.Source ; 58 import javax.xml.transform.stream.StreamSource ; 59 import javax.xml.transform.sax.SAXResult ; 60 import javax.xml.transform.sax.SAXSource ; 61 import javax.xml.transform.sax.SAXTransformerFactory ; 62 63 import org.xml.sax.InputSource ; 65 import org.xml.sax.XMLReader ; 66 import org.xml.sax.XMLFilter ; 67 68 import java.io.File ; 70 71 75 public class TraxInputHandler extends InputHandler { 76 private Transformer transformer; 77 private StreamSource xmlSource; 78 private Source xsltSource; 79 80 public TraxInputHandler(File xmlfile, File xsltfile) 81 throws FOPException { 82 xmlSource = new StreamSource (xmlfile); 83 xsltSource = new StreamSource (xsltfile); 84 initTransformer(); 85 } 86 87 public TraxInputHandler(String xmlURL, String xsltURL) 88 throws FOPException { 89 this.xmlSource = new StreamSource (xmlURL); 90 this.xsltSource = new StreamSource (xsltURL); 91 initTransformer(); 92 } 93 94 public TraxInputHandler(InputSource xmlSource, InputSource xsltSource) 95 throws FOPException { 96 this.xmlSource = new StreamSource (xmlSource.getByteStream(), 97 xmlSource.getSystemId()); 98 this.xsltSource = new StreamSource (xsltSource.getByteStream(), 99 xsltSource.getSystemId()); 100 initTransformer(); 101 } 102 103 private void initTransformer() throws FOPException { 104 try { 105 transformer = TransformerFactory.newInstance().newTransformer (xsltSource); 106 } 107 catch( Exception ex) { 108 throw new FOPException(ex); 109 } 110 } 111 112 117 public InputSource getInputSource() { 118 InputSource is = new InputSource (); 119 is.setByteStream(xmlSource.getInputStream()); 120 is.setSystemId(xmlSource.getSystemId()); 121 return is; 122 } 123 124 132 public XMLReader getParser() throws FOPException { 133 return this.getXMLFilter(xsltSource); 134 } 135 136 145 private static XMLFilter getXMLFilter(Source xsltSource) 146 throws FOPException { 147 try { 148 TransformerFactory tFactory = TransformerFactory.newInstance(); 150 if (tFactory.getFeature(SAXSource.FEATURE) 153 && tFactory.getFeature(SAXResult.FEATURE)) { 154 SAXTransformerFactory saxTFactory = 156 ((SAXTransformerFactory )tFactory); 157 XMLFilter xmlfilter = 159 saxTFactory.newXMLFilter(xsltSource); 160 161 XMLReader parser = createParser(); 163 if (parser == null) { 164 throw new FOPException("Unable to create SAX parser"); 165 } 166 167 xmlfilter.setParent(parser); 169 return xmlfilter; 170 } else { 171 throw new FOPException("Your parser doesn't support the features SAXSource and SAXResult." 172 + "\nMake sure you are using a xsl parser which supports TrAX"); 173 } 174 } catch (FOPException fex) { 175 throw fex; 176 } catch (Exception ex) { 177 throw new FOPException(ex); 178 } 179 } 180 181 182 191 public static XMLFilter getXMLFilter(File xmlfile, 192 File xsltfile) throws FOPException { 193 return getXMLFilter(new StreamSource (xsltfile)); 194 } 195 196 public void run(Driver driver) throws FOPException { 197 try { 198 transformer.transform(xmlSource, 199 new SAXResult (driver.getContentHandler())); 200 } catch (Exception ex) { 201 throw new FOPException(ex); 202 } 203 } 204 205 public void setParameter(String name, Object value) { 206 transformer.setParameter(name, value); 207 } 208 209 } 210 | Popular Tags |