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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * @throws IllegalArgumentException if an unsupported renderer type was requested.
  59. */
  60. public Fop(String outputFormat, FOUserAgent ua) {
  61. this.outputFormat = outputFormat;
  62. foUserAgent = ua;
  63. if (foUserAgent == null) {
  64. foUserAgent = new FOUserAgent();
  65. }
  66. }
  67. /**
  68. * Constructor for FOP with a default FOUserAgent. It uses MIME types to select the
  69. * output format (ex. "application/pdf" for PDF).
  70. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  71. * @throws IllegalArgumentException if an unsupported renderer type was requested.
  72. */
  73. public Fop(String outputFormat) {
  74. this(outputFormat, null);
  75. }
  76. /**
  77. * Constructor for use with already-created FOUserAgents
  78. * @param renderType the type of renderer to use. Must be one of
  79. * <ul>
  80. * <li>Fop.RENDER_PDF</li>
  81. * <li>Fop.RENDER_AWT</li>
  82. * <li>Fop.RENDER_PRINT</li>
  83. * <li>Fop.RENDER_MIF</li>
  84. * <li>Fop.RENDER_XML</li>
  85. * <li>Fop.RENDER_PCL</li>
  86. * <li>Fop.RENDER_PS</li>
  87. * <li>Fop.RENDER_TXT</li>
  88. * <li>Fop.RENDER_SVG</li>
  89. * <li>Fop.RENDER_RTF</li>
  90. * <li>Fop.RENDER_TIFF</li>
  91. * <li>Fop.RENDER_PNG</li>
  92. * </ul>
  93. * @param ua FOUserAgent object
  94. * @throws IllegalArgumentException if an unsupported renderer type was requested.
  95. * @deprecated Use {@link org.apache.fop.apps.Fop#Fop(java.lang.String, FOUserAgent)} instead!
  96. * This constructor will be removed.
  97. */
  98. public Fop(int renderType, FOUserAgent ua) {
  99. this(getMimeTypeForRenderType(renderType), ua);
  100. }
  101. /**
  102. * Constructor that creates a default FOUserAgent
  103. * @see org.apache.fop.apps.Fop#Fop(int, FOUserAgent)
  104. * @deprecated Use {@link org.apache.fop.apps.Fop#Fop(java.lang.String)} instead!
  105. * This constructor will be removed.
  106. */
  107. public Fop(int renderType) {
  108. this(renderType, null);
  109. }
  110. private static String getMimeTypeForRenderType(int renderType) {
  111. switch(renderType) {
  112. case Constants.RENDER_PDF: return MimeConstants.MIME_PDF;
  113. case Constants.RENDER_PS: return MimeConstants.MIME_POSTSCRIPT;
  114. case Constants.RENDER_PCL: return MimeConstants.MIME_PCL;
  115. case Constants.RENDER_MIF: return MimeConstants.MIME_MIF;
  116. case Constants.RENDER_RTF: return MimeConstants.MIME_RTF;
  117. case Constants.RENDER_SVG: return MimeConstants.MIME_SVG;
  118. case Constants.RENDER_TXT: return MimeConstants.MIME_PLAIN_TEXT;
  119. //Bitmap formats
  120. case Constants.RENDER_PNG: return MimeConstants.MIME_PNG;
  121. case Constants.RENDER_TIFF: return MimeConstants.MIME_TIFF;
  122. //Area tree XML: FOP-specific
  123. case Constants.RENDER_XML: return MimeConstants.MIME_FOP_AREA_TREE;
  124. //Non-standard pseudo MIME types
  125. case Constants.RENDER_AWT: return MimeConstants.MIME_FOP_AWT_PREVIEW;
  126. case Constants.RENDER_PRINT: return MimeConstants.MIME_FOP_PRINT;
  127. default:
  128. throw new IllegalArgumentException("Illegal renderType value: " + renderType);
  129. }
  130. }
  131. /**
  132. * Get the FOUserAgent instance for this process
  133. * @return the user agent
  134. */
  135. public FOUserAgent getUserAgent() {
  136. return foUserAgent;
  137. }
  138. /**
  139. * Set the OutputStream to use to output the result of the Render
  140. * (if applicable)
  141. * @param stream the stream to output the result of rendering to
  142. */
  143. public void setOutputStream(OutputStream stream) {
  144. this.stream = stream;
  145. }
  146. /**
  147. * Returns a DefaultHandler object used to generate the document.
  148. * Note this object implements the ContentHandler interface.
  149. * For processing with a Transformer object, this DefaultHandler object
  150. * can be used in the SAXResult constructor.
  151. * Alternatively, for processing with a SAXParser, this object can be
  152. * used as the DefaultHandler argument to its parse() methods.
  153. *
  154. * @return a SAX DefaultHandler for handling the SAX events.
  155. * @throws FOPException if setting up the DefaultHandler fails
  156. */
  157. public DefaultHandler getDefaultHandler() throws FOPException {
  158. if (foTreeBuilder == null) {
  159. this.foTreeBuilder = new FOTreeBuilder(outputFormat, foUserAgent, stream);
  160. }
  161. return this.foTreeBuilder;
  162. }
  163. /**
  164. * Returns the results of the rendering process. Information includes
  165. * the total number of pages generated and the number of pages per
  166. * page-sequence. Call this method only after the rendering process is
  167. * finished. Note that the results are only available for output formats
  168. * which make use of FOP's layout engine (PDF, PS, etc.).
  169. * @return the results of the rendering process, or null for flow-oriented
  170. * output formats like RTF and MIF.
  171. */
  172. public FormattingResults getResults() {
  173. if (foTreeBuilder == null) {
  174. throw new IllegalStateException(
  175. "Results are only available after calling getDefaultHandler().");
  176. } else {
  177. return foTreeBuilder.getResults();
  178. }
  179. }
  180. /**
  181. * Get the version of FOP
  182. * @return the version string
  183. * @deprecated Use {@link org.apache.fop.Version#getVersion()} instead!
  184. */
  185. public static String getVersion() {
  186. return org.apache.fop.Version.getVersion();
  187. }
  188. }