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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <a href="http://xmlgraphics.apache.org/fop/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. */
  38. public class Fop implements Constants {
  39. // desired output type: RENDER_PDF, RENDER_PS, etc.
  40. private int renderType = NOT_SET;
  41. // output stream to send results to
  42. private OutputStream stream = null;
  43. // FOUserAgent object to set processing options
  44. private FOUserAgent foUserAgent = null;
  45. // FOTreeBuilder object to maintain reference for access to results
  46. private FOTreeBuilder foTreeBuilder = null;
  47. /**
  48. * Constructor for use with already-created FOUserAgents
  49. * @param renderType the type of renderer to use. Must be one of
  50. * <ul>
  51. * <li>Fop.RENDER_PDF</li>
  52. * <li>Fop.RENDER_AWT</li>
  53. * <li>Fop.RENDER_PRINT</li>
  54. * <li>Fop.RENDER_MIF</li>
  55. * <li>Fop.RENDER_XML</li>
  56. * <li>Fop.RENDER_PCL</li>
  57. * <li>Fop.RENDER_PS</li>
  58. * <li>Fop.RENDER_TXT</li>
  59. * <li>Fop.RENDER_SVG</li>
  60. * <li>Fop.RENDER_RTF</li>
  61. * <li>Fop.RENDER_TIFF</li>
  62. * <li>Fop.RENDER_PNG</li>
  63. * </ul>
  64. * @param ua FOUserAgent object
  65. * @throws IllegalArgumentException if an unsupported renderer type was requested.
  66. */
  67. public Fop(int renderType, FOUserAgent ua) {
  68. if (renderType < Constants.RENDER_MIN_CONST
  69. || renderType > Constants.RENDER_MAX_CONST) {
  70. throw new IllegalArgumentException(
  71. "Invalid render type #" + renderType);
  72. }
  73. this.renderType = renderType;
  74. foUserAgent = ua;
  75. if (foUserAgent == null) {
  76. foUserAgent = new FOUserAgent();
  77. }
  78. }
  79. /**
  80. * Constructor that creates a default FOUserAgent
  81. * @see org.apache.fop.apps.Fop#Fop(int, FOUserAgent)
  82. */
  83. public Fop(int renderType) {
  84. this(renderType, new FOUserAgent());
  85. }
  86. /**
  87. * Get the FOUserAgent instance for this process
  88. * @return the user agent
  89. */
  90. public FOUserAgent getUserAgent() {
  91. return foUserAgent;
  92. }
  93. /**
  94. * Set the OutputStream to use to output the result of the Render
  95. * (if applicable)
  96. * @param stream the stream to output the result of rendering to
  97. */
  98. public void setOutputStream(OutputStream stream) {
  99. this.stream = stream;
  100. }
  101. /**
  102. * Returns a DefaultHandler object used to generate the document.
  103. * Note this object implements the ContentHandler interface.
  104. * For processing with a Transformer object, this DefaultHandler object
  105. * can be used in the SAXResult constructor.
  106. * Alternatively, for processing with a SAXParser, this object can be
  107. * used as the DefaultHandler argument to its parse() methods.
  108. *
  109. * @return a SAX DefaultHandler for handling the SAX events.
  110. * @throws FOPException if setting up the DefaultHandler fails
  111. */
  112. public DefaultHandler getDefaultHandler() throws FOPException {
  113. if (foTreeBuilder == null) {
  114. this.foTreeBuilder = new FOTreeBuilder(renderType, foUserAgent, stream);
  115. }
  116. return this.foTreeBuilder;
  117. }
  118. /**
  119. * Returns the results of the rendering process. Information includes
  120. * the total number of pages generated and the number of pages per
  121. * page-sequence. Call this method only after the rendering process is
  122. * finished. Note that the results are only available for output formats
  123. * which make use of FOP's layout engine (PDF, PS, etc.).
  124. * @return the results of the rendering process, or null for flow-oriented
  125. * output formats like RTF and MIF.
  126. */
  127. public FormattingResults getResults() {
  128. if (foTreeBuilder == null) {
  129. throw new IllegalStateException(
  130. "Results are only available after calling getDefaultHandler().");
  131. } else {
  132. return foTreeBuilder.getResults();
  133. }
  134. }
  135. /**
  136. * Get the version of FOP
  137. * @return the version string
  138. */
  139. public static String getVersion() {
  140. return org.apache.fop.Version.getVersion();
  141. }
  142. }