1 17 18 19 20 package org.apache.fop.cli; 21 22 import java.io.File ; 24 import java.io.OutputStream ; 25 import java.util.Vector ; 26 27 import javax.xml.transform.ErrorListener ; 29 import javax.xml.transform.Result ; 30 import javax.xml.transform.Source ; 31 import javax.xml.transform.Transformer ; 32 import javax.xml.transform.TransformerException ; 33 import javax.xml.transform.TransformerFactory ; 34 import javax.xml.transform.sax.SAXResult ; 35 import javax.xml.transform.stream.StreamResult ; 36 import javax.xml.transform.stream.StreamSource ; 37 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 import org.apache.fop.apps.FOPException; 41 import org.apache.fop.apps.FOUserAgent; 42 import org.apache.fop.apps.Fop; 43 import org.apache.fop.apps.FopFactory; 44 import org.apache.fop.render.awt.viewer.Renderable; 45 46 51 public class InputHandler implements ErrorListener , Renderable { 52 53 private File sourcefile = null; private File stylesheet = null; private Vector xsltParams = null; 57 58 protected Log log = LogFactory.getLog(InputHandler.class); 59 60 67 public InputHandler(File xmlfile, File xsltfile, Vector params) { 68 sourcefile = xmlfile; 69 stylesheet = xsltfile; 70 xsltParams = params; 71 } 72 73 77 public InputHandler(File fofile) { 78 sourcefile = fofile; 79 } 80 81 88 public void renderTo(FOUserAgent userAgent, String outputFormat, OutputStream out) 89 throws FOPException { 90 91 FopFactory factory = userAgent.getFactory(); 92 Fop fop; 93 if (out != null) { 94 fop = factory.newFop(outputFormat, userAgent, out); 95 } else { 96 fop = factory.newFop(outputFormat, userAgent); 97 } 98 99 if (fop.getUserAgent().getBaseURL() == null) { 101 String baseURL = null; 102 103 try { 104 baseURL = new File (sourcefile.getAbsolutePath()). 105 getParentFile().toURL().toExternalForm(); 106 } catch (Exception e) { 107 baseURL = ""; 108 } 109 fop.getUserAgent().setBaseURL(baseURL); 110 } 111 112 Result res = new SAXResult (fop.getDefaultHandler()); 114 115 transformTo(res); 116 } 117 118 119 public void renderTo(FOUserAgent userAgent, String outputFormat) throws FOPException { 120 renderTo(userAgent, outputFormat, null); 121 } 122 123 129 public void transformTo(OutputStream out) throws FOPException { 130 Result res = new StreamResult (out); 131 transformTo(res); 132 } 133 134 139 protected void transformTo(Result result) throws FOPException { 140 try { 141 TransformerFactory factory = TransformerFactory.newInstance(); 143 Transformer transformer; 144 145 if (stylesheet == null) { transformer = factory.newTransformer(); 147 } else { transformer = factory.newTransformer(new StreamSource ( 149 stylesheet)); 150 151 if (xsltParams != null) { 153 for (int i = 0; i < xsltParams.size(); i += 2) { 154 transformer.setParameter((String ) xsltParams.elementAt(i), 155 (String ) xsltParams.elementAt(i + 1)); 156 } 157 } 158 } 159 transformer.setErrorListener(this); 160 161 Source src = new StreamSource (sourcefile); 163 164 transformer.transform(src, result); 166 167 } catch (Exception e) { 168 throw new FOPException(e); 169 } 170 } 171 172 174 177 public void warning(TransformerException exc) { 178 log.warn(exc.toString()); 179 } 180 181 184 public void error(TransformerException exc) { 185 log.error(exc.toString()); 186 } 187 188 191 public void fatalError(TransformerException exc) 192 throws TransformerException { 193 throw exc; 194 } 195 196 } 197 | Popular Tags |