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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 = new Fop(outputFormat, userAgent);
  78. if (out != null) {
  79. fop.setOutputStream(out);
  80. }
  81. // if base URL was not explicitly set in FOUserAgent, obtain here
  82. if (fop.getUserAgent().getBaseURL() == null) {
  83. String baseURL = null;
  84. try {
  85. baseURL = new File(sourcefile.getAbsolutePath()).
  86. getParentFile().toURL().toExternalForm();
  87. } catch (Exception e) {
  88. baseURL = "";
  89. }
  90. fop.getUserAgent().setBaseURL(baseURL);
  91. }
  92. // Resulting SAX events (the generated FO) must be piped through to FOP
  93. Result res = new SAXResult(fop.getDefaultHandler());
  94. transformTo(res);
  95. }
  96. /** @see org.apache.fop.render.awt.viewer.Renderable */
  97. public void renderTo(FOUserAgent userAgent, String outputFormat) throws FOPException {
  98. renderTo(userAgent, outputFormat, null);
  99. }
  100. /**
  101. * In contrast to render(Fop) this method only performs the XSLT stage and saves the
  102. * intermediate XSL-FO file to the output file.
  103. * @param out OutputStream to write the transformation result to.
  104. * @throws FOPException in case of an error during processing
  105. */
  106. public void transformTo(OutputStream out) throws FOPException {
  107. Result res = new StreamResult(out);
  108. transformTo(res);
  109. }
  110. /**
  111. * Transforms the input document to the input format expected by FOP using XSLT.
  112. * @param result the Result object where the result of the XSL transformation is sent to
  113. * @throws FOPException in case of an error during processing
  114. */
  115. protected void transformTo(Result result) throws FOPException {
  116. try {
  117. // Setup XSLT
  118. TransformerFactory factory = TransformerFactory.newInstance();
  119. Transformer transformer;
  120. if (stylesheet == null) { // FO Input
  121. transformer = factory.newTransformer();
  122. } else { // XML/XSLT input
  123. transformer = factory.newTransformer(new StreamSource(
  124. stylesheet));
  125. // Set the value of parameters, if any, defined for stylesheet
  126. if (xsltParams != null) {
  127. for (int i = 0; i < xsltParams.size(); i += 2) {
  128. transformer.setParameter((String) xsltParams.elementAt(i),
  129. (String) xsltParams.elementAt(i + 1));
  130. }
  131. }
  132. }
  133. transformer.setErrorListener(this);
  134. // Create a SAXSource from the input Source file
  135. Source src = new StreamSource(sourcefile);
  136. // Start XSLT transformation and FOP processing
  137. transformer.transform(src, result);
  138. } catch (Exception e) {
  139. throw new FOPException(e);
  140. }
  141. }
  142. // --- Implementation of the ErrorListener interface ---
  143. /**
  144. * @see javax.xml.transform.ErrorListener#warning(javax.xml.transform.TransformerException)
  145. */
  146. public void warning(TransformerException exc) {
  147. log.warn(exc.toString());
  148. }
  149. /**
  150. * @see javax.xml.transform.ErrorListener#error(javax.xml.transform.TransformerException)
  151. */
  152. public void error(TransformerException exc) {
  153. log.error(exc.toString());
  154. }
  155. /**
  156. * @see javax.xml.transform.ErrorListener#fatalError(javax.xml.transform.TransformerException)
  157. */
  158. public void fatalError(TransformerException exc)
  159. throws TransformerException {
  160. throw exc;
  161. }
  162. }