You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputHandler.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.cli;
  18. // Imported java.io classes
  19. import java.io.File;
  20. import java.io.OutputStream;
  21. import java.util.Vector;
  22. // Imported TraX classes
  23. import javax.xml.transform.ErrorListener;
  24. import javax.xml.transform.Result;
  25. import javax.xml.transform.Source;
  26. import javax.xml.transform.Transformer;
  27. import javax.xml.transform.TransformerException;
  28. import javax.xml.transform.TransformerFactory;
  29. import javax.xml.transform.sax.SAXResult;
  30. import javax.xml.transform.stream.StreamResult;
  31. import javax.xml.transform.stream.StreamSource;
  32. import org.apache.commons.logging.Log;
  33. import org.apache.commons.logging.LogFactory;
  34. import org.apache.fop.apps.FOPException;
  35. import org.apache.fop.apps.FOUserAgent;
  36. import org.apache.fop.apps.Fop;
  37. import org.apache.fop.render.awt.viewer.Renderable;
  38. /**
  39. * Class for handling files input from command line
  40. * either with XML and XSLT files (and optionally xsl
  41. * parameters) or FO File input alone
  42. */
  43. public class InputHandler implements ErrorListener, Renderable {
  44. private File sourcefile = null; // either FO or XML/XSLT usage
  45. private File stylesheet = null; // for XML/XSLT usage
  46. private Vector xsltParams = null; // for XML/XSLT usage
  47. /** the logger */
  48. protected Log log = LogFactory.getLog(InputHandler.class);
  49. /**
  50. * Constructor for XML->XSLT->FO input
  51. * @param xmlfile XML file
  52. * @param xsltfile XSLT file
  53. * @param params Vector of command-line parameters (name, value,
  54. * name, value, ...) for XSL stylesheet, null if none
  55. */
  56. public InputHandler(File xmlfile, File xsltfile, Vector params) {
  57. sourcefile = xmlfile;
  58. stylesheet = xsltfile;
  59. xsltParams = params;
  60. }
  61. /**
  62. * Constructor for FO input
  63. * @param fofile the file to read the FO document.
  64. */
  65. public InputHandler(File fofile) {
  66. sourcefile = fofile;
  67. }
  68. /**
  69. * Generate a document, given an initialized Fop object
  70. * @param userAgent the user agent
  71. * @param outputFormat the output format to generate (MIME type, see MimeConstants)
  72. * @param out the output stream to write the generated output to (may be null if not applicable)
  73. * @throws FOPException in case of an error during processing
  74. */
  75. public void renderTo(FOUserAgent userAgent, String outputFormat, OutputStream out)
  76. throws FOPException {
  77. Fop fop;
  78. if (out != null) {
  79. fop = new Fop(outputFormat, userAgent, out);
  80. } else {
  81. fop = new Fop(outputFormat, userAgent);
  82. }
  83. // if base URL was not explicitly set in FOUserAgent, obtain here
  84. if (fop.getUserAgent().getBaseURL() == null) {
  85. String baseURL = null;
  86. try {
  87. baseURL = new File(sourcefile.getAbsolutePath()).
  88. getParentFile().toURL().toExternalForm();
  89. } catch (Exception e) {
  90. baseURL = "";
  91. }
  92. fop.getUserAgent().setBaseURL(baseURL);
  93. }
  94. // Resulting SAX events (the generated FO) must be piped through to FOP
  95. Result res = new SAXResult(fop.getDefaultHandler());
  96. transformTo(res);
  97. }
  98. /** @see org.apache.fop.render.awt.viewer.Renderable */
  99. public void renderTo(FOUserAgent userAgent, String outputFormat) throws FOPException {
  100. renderTo(userAgent, outputFormat, null);
  101. }
  102. /**
  103. * In contrast to render(Fop) this method only performs the XSLT stage and saves the
  104. * intermediate XSL-FO file to the output file.
  105. * @param out OutputStream to write the transformation result to.
  106. * @throws FOPException in case of an error during processing
  107. */
  108. public void transformTo(OutputStream out) throws FOPException {
  109. Result res = new StreamResult(out);
  110. transformTo(res);
  111. }
  112. /**
  113. * Transforms the input document to the input format expected by FOP using XSLT.
  114. * @param result the Result object where the result of the XSL transformation is sent to
  115. * @throws FOPException in case of an error during processing
  116. */
  117. protected void transformTo(Result result) throws FOPException {
  118. try {
  119. // Setup XSLT
  120. TransformerFactory factory = TransformerFactory.newInstance();
  121. Transformer transformer;
  122. if (stylesheet == null) { // FO Input
  123. transformer = factory.newTransformer();
  124. } else { // XML/XSLT input
  125. transformer = factory.newTransformer(new StreamSource(
  126. stylesheet));
  127. // Set the value of parameters, if any, defined for stylesheet
  128. if (xsltParams != null) {
  129. for (int i = 0; i < xsltParams.size(); i += 2) {
  130. transformer.setParameter((String) xsltParams.elementAt(i),
  131. (String) xsltParams.elementAt(i + 1));
  132. }
  133. }
  134. }
  135. transformer.setErrorListener(this);
  136. // Create a SAXSource from the input Source file
  137. Source src = new StreamSource(sourcefile);
  138. // Start XSLT transformation and FOP processing
  139. transformer.transform(src, result);
  140. } catch (Exception e) {
  141. throw new FOPException(e);
  142. }
  143. }
  144. // --- Implementation of the ErrorListener interface ---
  145. /**
  146. * @see javax.xml.transform.ErrorListener#warning(javax.xml.transform.TransformerException)
  147. */
  148. public void warning(TransformerException exc) {
  149. log.warn(exc.toString());
  150. }
  151. /**
  152. * @see javax.xml.transform.ErrorListener#error(javax.xml.transform.TransformerException)
  153. */
  154. public void error(TransformerException exc) {
  155. log.error(exc.toString());
  156. }
  157. /**
  158. * @see javax.xml.transform.ErrorListener#fatalError(javax.xml.transform.TransformerException)
  159. */
  160. public void fatalError(TransformerException exc)
  161. throws TransformerException {
  162. throw exc;
  163. }
  164. }