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 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.apps;
  18. // Java
  19. import java.io.OutputStream;
  20. // XML
  21. import org.xml.sax.helpers.DefaultHandler;
  22. // FOP
  23. import org.apache.fop.fo.Constants;
  24. import org.apache.fop.fo.FOTreeBuilder;
  25. /**
  26. * Primary class that activates the FOP process for embedded usage.
  27. * <P>
  28. * JAXP is the standard method of embedding FOP in Java programs.
  29. * Please check our
  30. * <a href="http://xmlgraphics.apache.org/fop/trunk/embedding.html">embedding page</a>
  31. * for samples (these are also available within the distribution in
  32. * FOP_DIR\examples\embedding)
  33. * <P>
  34. * Methods within FOUserAgent are available to customize portions of the
  35. * process. For example, a specific Renderer object can be specified,
  36. * also ElementMappings which determine elements in the FO that can be
  37. * processed) can be added.
  38. * <P>
  39. * At the moment, it is recommended not to reuse an instance of this
  40. * class for more than one rendering run.
  41. */
  42. public class Fop implements Constants {
  43. // desired output type: RENDER_PDF, RENDER_PS, etc.
  44. //private int renderType = NOT_SET;
  45. // desired output format: MIME type such as "application/pdf", "application/postscript" etc.
  46. private String outputFormat = null;
  47. // output stream to send results to
  48. private OutputStream stream = null;
  49. // FOUserAgent object to set processing options
  50. private FOUserAgent foUserAgent = null;
  51. // FOTreeBuilder object to maintain reference for access to results
  52. private FOTreeBuilder foTreeBuilder = null;
  53. /**
  54. * Constructor for use with already-created FOUserAgents. It uses MIME types to select the
  55. * output format (ex. "application/pdf" for PDF).
  56. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  57. * @param ua FOUserAgent object
  58. */
  59. public Fop(String outputFormat, FOUserAgent ua) {
  60. this.outputFormat = outputFormat;
  61. foUserAgent = ua;
  62. if (foUserAgent == null) {
  63. foUserAgent = new FOUserAgent();
  64. }
  65. }
  66. /**
  67. * Constructor for FOP with a default FOUserAgent. It uses MIME types to select the
  68. * output format (ex. "application/pdf" for PDF).
  69. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  70. */
  71. public Fop(String outputFormat) {
  72. this(outputFormat, null);
  73. }
  74. /**
  75. * Constructor for use with already-created FOUserAgents
  76. * @param renderType the type of renderer to use. Must be one of
  77. * <ul>
  78. * <li>Fop.RENDER_PDF</li>
  79. * <li>Fop.RENDER_AWT</li>
  80. * <li>Fop.RENDER_PRINT</li>
  81. * <li>Fop.RENDER_MIF</li>
  82. * <li>Fop.RENDER_XML</li>
  83. * <li>Fop.RENDER_PCL</li>
  84. * <li>Fop.RENDER_PS</li>
  85. * <li>Fop.RENDER_TXT</li>
  86. * <li>Fop.RENDER_SVG</li>
  87. * <li>Fop.RENDER_RTF</li>
  88. * <li>Fop.RENDER_TIFF</li>
  89. * <li>Fop.RENDER_PNG</li>
  90. * </ul>
  91. * @param ua FOUserAgent object
  92. * @deprecated Use {@link org.apache.fop.apps.Fop#Fop(java.lang.String, FOUserAgent)} instead!
  93. * This constructor will be removed.
  94. */
  95. public Fop(int renderType, FOUserAgent ua) {
  96. this(getMimeTypeForRenderType(renderType), ua);
  97. }
  98. /**
  99. * Constructor that creates a default FOUserAgent
  100. * @see org.apache.fop.apps.Fop#Fop(int, FOUserAgent)
  101. * @deprecated Use {@link org.apache.fop.apps.Fop#Fop(java.lang.String)} instead!
  102. * This constructor will be removed.
  103. */
  104. public Fop(int renderType) {
  105. this(renderType, null);
  106. }
  107. private static String getMimeTypeForRenderType(int renderType) {
  108. switch(renderType) {
  109. case Constants.RENDER_PDF: return MimeConstants.MIME_PDF;
  110. case Constants.RENDER_PS: return MimeConstants.MIME_POSTSCRIPT;
  111. case Constants.RENDER_PCL: return MimeConstants.MIME_PCL;
  112. case Constants.RENDER_MIF: return MimeConstants.MIME_MIF;
  113. case Constants.RENDER_RTF: return MimeConstants.MIME_RTF;
  114. case Constants.RENDER_SVG: return MimeConstants.MIME_SVG;
  115. case Constants.RENDER_TXT: return MimeConstants.MIME_PLAIN_TEXT;
  116. //Bitmap formats
  117. case Constants.RENDER_PNG: return MimeConstants.MIME_PNG;
  118. case Constants.RENDER_TIFF: return MimeConstants.MIME_TIFF;
  119. //Area tree XML: FOP-specific
  120. case Constants.RENDER_XML: return MimeConstants.MIME_FOP_AREA_TREE;
  121. //Non-standard pseudo MIME types
  122. case Constants.RENDER_AWT: return MimeConstants.MIME_FOP_AWT_PREVIEW;
  123. case Constants.RENDER_PRINT: return MimeConstants.MIME_FOP_PRINT;
  124. default:
  125. throw new IllegalArgumentException("Illegal renderType value: " + renderType);
  126. }
  127. }
  128. /**
  129. * Get the FOUserAgent instance for this process
  130. * @return the user agent
  131. */
  132. public FOUserAgent getUserAgent() {
  133. return foUserAgent;
  134. }
  135. /**
  136. * Set the OutputStream to use to output the result of the Render
  137. * (if applicable)
  138. * @param stream the stream to output the result of rendering to
  139. */
  140. public void setOutputStream(OutputStream stream) {
  141. this.stream = stream;
  142. }
  143. /**
  144. * Returns a DefaultHandler object used to generate the document.
  145. * Note this object implements the ContentHandler interface.
  146. * For processing with a Transformer object, this DefaultHandler object
  147. * can be used in the SAXResult constructor.
  148. * Alternatively, for processing with a SAXParser, this object can be
  149. * used as the DefaultHandler argument to its parse() methods.
  150. *
  151. * @return a SAX DefaultHandler for handling the SAX events.
  152. * @throws FOPException if setting up the DefaultHandler fails
  153. */
  154. public DefaultHandler getDefaultHandler() throws FOPException {
  155. if (foTreeBuilder == null) {
  156. this.foTreeBuilder = new FOTreeBuilder(outputFormat, foUserAgent, stream);
  157. }
  158. return this.foTreeBuilder;
  159. }
  160. /**
  161. * Returns the results of the rendering process. Information includes
  162. * the total number of pages generated and the number of pages per
  163. * page-sequence. Call this method only after the rendering process is
  164. * finished. Note that the results are only available for output formats
  165. * which make use of FOP's layout engine (PDF, PS, etc.).
  166. * @return the results of the rendering process, or null for flow-oriented
  167. * output formats like RTF and MIF.
  168. */
  169. public FormattingResults getResults() {
  170. if (foTreeBuilder == null) {
  171. throw new IllegalStateException(
  172. "Results are only available after calling getDefaultHandler().");
  173. } else {
  174. return foTreeBuilder.getResults();
  175. }
  176. }
  177. /**
  178. * Get the version of FOP
  179. * @return the version string
  180. * @deprecated Use {@link org.apache.fop.Version#getVersion()} instead!
  181. */
  182. public static String getVersion() {
  183. return org.apache.fop.Version.getVersion();
  184. }
  185. }