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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. */
  58. public Fop(String outputFormat, FOUserAgent ua, OutputStream stream) throws FOPException {
  59. this.outputFormat = outputFormat;
  60. foUserAgent = ua;
  61. if (foUserAgent == null) {
  62. foUserAgent = FopFactory.newInstance().newFOUserAgent();
  63. }
  64. this.stream = stream;
  65. createDefaultHandler();
  66. }
  67. /**
  68. * Constructor for use with already-created FOUserAgents. 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. * @param ua FOUserAgent object
  72. * @throws FOPException if setting up the DefaultHandler fails
  73. */
  74. public Fop(String outputFormat, FOUserAgent ua) throws FOPException {
  75. this.outputFormat = outputFormat;
  76. foUserAgent = ua;
  77. if (foUserAgent == null) {
  78. foUserAgent = FopFactory.newInstance().newFOUserAgent();
  79. }
  80. createDefaultHandler();
  81. }
  82. /**
  83. * Constructor for FOP with a default FOUserAgent. It uses MIME types to select the
  84. * output format (ex. "application/pdf" for PDF).
  85. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  86. * @deprecated Use a constructor with an FOUserAgent instead!
  87. */
  88. public Fop(String outputFormat) {
  89. this.outputFormat = outputFormat;
  90. foUserAgent = FopFactory.newInstance().newFOUserAgent();
  91. }
  92. /**
  93. * Get the FOUserAgent instance for this process
  94. * @return the user agent
  95. */
  96. public FOUserAgent getUserAgent() {
  97. return foUserAgent;
  98. }
  99. /**
  100. * Set the OutputStream to use to output the result of the Render
  101. * (if applicable)
  102. * @param stream the stream to output the result of rendering to
  103. * @deprecated Set the output stream in the constructor
  104. */
  105. public void setOutputStream(OutputStream stream) {
  106. this.stream = stream;
  107. }
  108. /**
  109. * Creates a DefaultHandler object used to generate the document.
  110. * Note this object implements the ContentHandler interface.
  111. * For processing with a Transformer object, this DefaultHandler object
  112. * can be used in the SAXResult constructor.
  113. * Alternatively, for processing with a SAXParser, this object can be
  114. * used as the DefaultHandler argument to its parse() methods.
  115. *
  116. * @throws FOPException if setting up the DefaultHandler fails
  117. */
  118. private void createDefaultHandler() throws FOPException {
  119. this.foTreeBuilder = new FOTreeBuilder(outputFormat, foUserAgent, stream);
  120. }
  121. /**
  122. * Returns the DefaultHandler object used to generate the document.
  123. * Checking for null and the exception is only for the deprecated constructor.
  124. * @return the SAX DefaultHandler for handling the SAX events.
  125. * @throws FOPException if setting up the DefaultHandler fails
  126. */
  127. public DefaultHandler getDefaultHandler() throws FOPException {
  128. if (foTreeBuilder == null) {
  129. createDefaultHandler();
  130. }
  131. return this.foTreeBuilder;
  132. }
  133. /**
  134. * Returns the results of the rendering process. Information includes
  135. * the total number of pages generated and the number of pages per
  136. * page-sequence. Call this method only after the rendering process is
  137. * finished. Note that the results are only available for output formats
  138. * which make use of FOP's layout engine (PDF, PS, etc.).
  139. * @return the results of the rendering process, or null for flow-oriented
  140. * output formats like RTF and MIF.
  141. */
  142. public FormattingResults getResults() {
  143. if (foTreeBuilder == null) {
  144. throw new IllegalStateException(
  145. "Results are only available after calling getDefaultHandler().");
  146. } else {
  147. return foTreeBuilder.getResults();
  148. }
  149. }
  150. /**
  151. * Get the version of FOP
  152. * @return the version string
  153. * @deprecated Use {@link org.apache.fop.Version#getVersion()} instead!
  154. */
  155. public static String getVersion() {
  156. return org.apache.fop.Version.getVersion();
  157. }
  158. }