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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright 1999-2006 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.FOTreeBuilder;
  24. /**
  25. * Primary class that activates the FOP process for embedded usage.
  26. * <P>
  27. * JAXP is the standard method of embedding FOP in Java programs.
  28. * Please check our
  29. * <a href="http://xmlgraphics.apache.org/fop/trunk/embedding.html">embedding page</a>
  30. * for samples (these are also available within the distribution in
  31. * FOP_DIR\examples\embedding)
  32. * <P>
  33. * Methods within FOUserAgent are available to customize portions of the
  34. * process. For example, a specific Renderer object can be specified,
  35. * also ElementMappings which determine elements in the FO that can be
  36. * processed) can be added.
  37. * <P>
  38. * At the moment, it is recommended not to reuse an instance of this
  39. * class for more than one rendering run.
  40. */
  41. public class Fop {
  42. // desired output format: MIME type such as "application/pdf", "application/postscript" etc.
  43. private String outputFormat = null;
  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. // FOTreeBuilder object to maintain reference for access to results
  49. private FOTreeBuilder foTreeBuilder = null;
  50. /**
  51. * Constructor for use with already-created FOUserAgents. It uses MIME types to select the
  52. * output format (ex. "application/pdf" for PDF).
  53. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  54. * @param ua FOUserAgent object
  55. * @param stream the output stream
  56. * @throws FOPException if setting up the DefaultHandler fails
  57. * @deprecated End-users should use {@link FopFactory#newFop(String, FOUserAgent, OutputStream)}
  58. * instead! This constructor will become invisible with FOP 1.0.
  59. */
  60. public Fop(String outputFormat, FOUserAgent ua, OutputStream stream) throws FOPException {
  61. this.outputFormat = outputFormat;
  62. foUserAgent = ua;
  63. if (foUserAgent == null) {
  64. foUserAgent = FopFactory.newInstance().newFOUserAgent();
  65. }
  66. this.stream = stream;
  67. createDefaultHandler();
  68. }
  69. /**
  70. * Constructor for use with already-created FOUserAgents. It uses MIME types to select the
  71. * output format (ex. "application/pdf" for PDF).
  72. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  73. * @param ua FOUserAgent object
  74. * @throws FOPException if setting up the DefaultHandler fails
  75. * @deprecated End-users should use {@link FopFactory#newFop(String, FOUserAgent)} instead!
  76. * This constructor will become invisible with FOP 1.0.
  77. */
  78. public Fop(String outputFormat, FOUserAgent ua) throws FOPException {
  79. this.outputFormat = outputFormat;
  80. foUserAgent = ua;
  81. if (foUserAgent == null) {
  82. foUserAgent = FopFactory.newInstance().newFOUserAgent();
  83. }
  84. createDefaultHandler();
  85. }
  86. /**
  87. * Constructor for FOP with a default FOUserAgent. It uses MIME types to select the
  88. * output format (ex. "application/pdf" for PDF).
  89. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  90. * @deprecated End-users should use {@link FopFactory#newFop(String)} instead!
  91. * This constructor will become invisible with FOP 1.0.
  92. */
  93. public Fop(String outputFormat) {
  94. this.outputFormat = outputFormat;
  95. foUserAgent = FopFactory.newInstance().newFOUserAgent();
  96. }
  97. /**
  98. * Get the FOUserAgent instance for this process
  99. * @return the user agent
  100. */
  101. public FOUserAgent getUserAgent() {
  102. return foUserAgent;
  103. }
  104. /**
  105. * Set the OutputStream to use to output the result of the Render
  106. * (if applicable)
  107. * @param stream the stream to output the result of rendering to
  108. * @deprecated Use one of the factory methods on {@link FopFactory} with an OutputStream
  109. * parameter instead. This method will be removed with FOP 1.0.
  110. */
  111. public void setOutputStream(OutputStream stream) {
  112. this.stream = stream;
  113. }
  114. /**
  115. * Creates a DefaultHandler object used to generate the document.
  116. * Note this object implements the ContentHandler interface.
  117. * For processing with a Transformer object, this DefaultHandler object
  118. * can be used in the SAXResult constructor.
  119. * Alternatively, for processing with a SAXParser, this object can be
  120. * used as the DefaultHandler argument to its parse() methods.
  121. *
  122. * @throws FOPException if setting up the DefaultHandler fails
  123. */
  124. private void createDefaultHandler() throws FOPException {
  125. this.foTreeBuilder = new FOTreeBuilder(outputFormat, foUserAgent, stream);
  126. }
  127. /**
  128. * Returns the DefaultHandler object used to generate the document.
  129. * Checking for null and the exception is only for the deprecated constructor.
  130. * @return the SAX DefaultHandler for handling the SAX events.
  131. * @throws FOPException if setting up the DefaultHandler fails
  132. */
  133. public DefaultHandler getDefaultHandler() throws FOPException {
  134. if (foTreeBuilder == null) {
  135. createDefaultHandler();
  136. }
  137. return this.foTreeBuilder;
  138. }
  139. /**
  140. * Returns the results of the rendering process. Information includes
  141. * the total number of pages generated and the number of pages per
  142. * page-sequence. Call this method only after the rendering process is
  143. * finished. Note that the results are only available for output formats
  144. * which make use of FOP's layout engine (PDF, PS, etc.).
  145. * @return the results of the rendering process, or null for flow-oriented
  146. * output formats like RTF and MIF.
  147. */
  148. public FormattingResults getResults() {
  149. if (foTreeBuilder == null) {
  150. throw new IllegalStateException(
  151. "Results are only available after calling getDefaultHandler().");
  152. } else {
  153. return foTreeBuilder.getResults();
  154. }
  155. }
  156. /**
  157. * Get the version of FOP
  158. * @return the version string
  159. * @deprecated Use {@link org.apache.fop.Version#getVersion()} instead!
  160. * This method will be removed with FOP 1.0.
  161. */
  162. public static String getVersion() {
  163. return org.apache.fop.Version.getVersion();
  164. }
  165. }