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.

Fop.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 1999-2004 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.apps;
  18. // Java
  19. import java.io.BufferedOutputStream;
  20. import java.io.FileOutputStream;
  21. import java.io.OutputStream;
  22. // XML
  23. import org.xml.sax.helpers.DefaultHandler;
  24. // FOP
  25. import org.apache.fop.fo.Constants;
  26. import org.apache.fop.fo.FOTreeBuilder;
  27. /**
  28. * Primary class that activates the FOP process for both command line
  29. * and embedded usage.
  30. * <P>
  31. * JAXP is the standard method of embedding FOP in Java programs.
  32. * Please check our embedding page (http://xml.apache.org/fop/embedding.html)
  33. * for samples (these are also available within the distribution in
  34. * FOP_DIR\examples\embedding)
  35. * <P>
  36. * Methods within FOUserAgent are available to customize portions of the
  37. * process. For example, a specific Renderer object can be specified,
  38. * also ElementMappings which determine elements in the FO that can be
  39. * processed) can be added.
  40. */
  41. public class Fop implements Constants {
  42. // desired output type: RENDER_PDF, RENDER_PS, etc.
  43. private int renderType = NOT_SET;
  44. // output stream to send results to
  45. private OutputStream stream = null;
  46. // FOUserAgent object to set processing options
  47. private FOUserAgent foUserAgent = null;
  48. /**
  49. * Constructor for use with already-created FOUserAgents
  50. * @param renderType the type of renderer to use. Must be one of
  51. * <ul>
  52. * <li>Fop.RENDER_PDF</li>
  53. * <li>Fop.RENDER_AWT</li>
  54. * <li>Fop.RENDER_PRINT</li>
  55. * <li>Fop.RENDER_MIF</li>
  56. * <li>Fop.RENDER_XML</li>
  57. * <li>Fop.RENDER_PCL</li>
  58. * <li>Fop.RENDER_PS</li>
  59. * <li>Fop.RENDER_TXT</li>
  60. * <li>Fop.RENDER_SVG</li>
  61. * <li>Fop.RENDER_RTF</li>
  62. * </ul>
  63. * @param ua FOUserAgent object
  64. * @throws IllegalArgumentException if an unsupported renderer type was requested.
  65. */
  66. public Fop(int renderType, FOUserAgent ua) {
  67. if (renderType < Constants.RENDER_MIN_CONST
  68. || renderType > Constants.RENDER_MAX_CONST) {
  69. throw new IllegalArgumentException(
  70. "Invalid render type #" + renderType);
  71. }
  72. this.renderType = renderType;
  73. foUserAgent = ua;
  74. if (foUserAgent == null) {
  75. foUserAgent = new FOUserAgent();
  76. }
  77. }
  78. /**
  79. * Constructor that creates a default FOUserAgent
  80. * @see org.apache.fop.apps.Fop#(int, FOUserAgent)
  81. */
  82. public Fop(int renderType) {
  83. this(renderType, new FOUserAgent());
  84. }
  85. /**
  86. * Get the FOUserAgent instance for this process
  87. * @return the user agent
  88. */
  89. public FOUserAgent getUserAgent() {
  90. return foUserAgent;
  91. }
  92. /**
  93. * Set the OutputStream to use to output the result of the Render
  94. * (if applicable)
  95. * @param stream the stream to output the result of rendering to
  96. */
  97. public void setOutputStream(OutputStream stream) {
  98. this.stream = stream;
  99. }
  100. /**
  101. * Returns a DefaultHandler object used to generate the document.
  102. * Note this object implements the ContentHandler interface.
  103. * For processing with a Transformer object, this DefaultHandler object
  104. * can be used in the SAXResult constructor.
  105. * Alternatively, for processing with a SAXParser, this object can be
  106. * used as the DefaultHandler argument to its parse() methods.
  107. *
  108. * @return a SAX DefaultHandler for handling the SAX events.
  109. * @throws FOPException if setting up the DefaultHandler fails
  110. */
  111. public DefaultHandler getDefaultHandler() throws FOPException {
  112. return new FOTreeBuilder(renderType, foUserAgent, stream);
  113. }
  114. /**
  115. * The main routine for the command line interface
  116. * @param args the command line parameters
  117. */
  118. public static void main(String[] args) {
  119. CommandLineOptions options = null;
  120. FOUserAgent foUserAgent = null;
  121. BufferedOutputStream bos = null;
  122. try {
  123. options = new CommandLineOptions(args);
  124. foUserAgent = options.getFOUserAgent();
  125. Fop fop = new Fop(options.getRenderer(), foUserAgent);
  126. try {
  127. if (options.getOutputFile() != null) {
  128. bos = new BufferedOutputStream(new FileOutputStream(
  129. options.getOutputFile()));
  130. fop.setOutputStream(bos);
  131. }
  132. foUserAgent.getInputHandler().render(fop);
  133. } finally {
  134. if (bos != null) {
  135. bos.close();
  136. }
  137. }
  138. // System.exit(0) called to close AWT/SVG-created threads, if any.
  139. // AWTRenderer closes with window shutdown, so exit() should not
  140. // be called here
  141. if (options.getOutputMode() != CommandLineOptions.RENDER_AWT) {
  142. System.exit(0);
  143. }
  144. } catch (Exception e) {
  145. if (options != null) {
  146. options.getLogger().error("Exception", e);
  147. }
  148. System.exit(1);
  149. }
  150. }
  151. /**
  152. * Get the version of FOP
  153. * @return the version string
  154. */
  155. public static String getVersion() {
  156. return "1.0dev";
  157. }
  158. }