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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.apps;
  19. // Java
  20. import java.io.OutputStream;
  21. import org.xml.sax.helpers.DefaultHandler;
  22. import org.apache.fop.fo.FOTreeBuilder;
  23. /**
  24. * Primary class that activates the FOP process for embedded usage.
  25. * <P>
  26. * JAXP is the standard method of embedding FOP in Java programs.
  27. * Please check our
  28. * <a href="http://xmlgraphics.apache.org/fop/trunk/embedding.html">embedding page</a>
  29. * for samples (these are also available within the distribution in
  30. * FOP_DIR\examples\embedding)
  31. * <P>
  32. * Methods within FOUserAgent are available to customize portions of the
  33. * process. For example, a specific Renderer object can be specified,
  34. * also ElementMappings which determine elements in the FO that can be
  35. * processed) can be added.
  36. * <P>
  37. * At the moment, it is recommended not to reuse an instance of this
  38. * class for more than one rendering run.
  39. */
  40. public class Fop {
  41. // desired output format: MIME type such as "application/pdf", "application/postscript" etc.
  42. private String outputFormat = null;
  43. // output stream to send results to
  44. private OutputStream stream = null;
  45. // FOUserAgent object to set processing options
  46. private FOUserAgent foUserAgent = null;
  47. // FOTreeBuilder object to maintain reference for access to results
  48. private FOTreeBuilder foTreeBuilder = null;
  49. /**
  50. * Constructor for use with already-created FOUserAgents. It uses MIME types to select the
  51. * output format (ex. "application/pdf" for PDF).
  52. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  53. * @param ua FOUserAgent object
  54. * @param stream the output stream
  55. * @throws FOPException if setting up the DefaultHandler fails
  56. */
  57. Fop(String outputFormat, FOUserAgent ua, OutputStream stream) throws FOPException {
  58. this.outputFormat = outputFormat;
  59. foUserAgent = ua;
  60. if (foUserAgent == null) {
  61. foUserAgent = FopFactory.newInstance().newFOUserAgent();
  62. }
  63. this.stream = stream;
  64. createDefaultHandler();
  65. }
  66. /**
  67. * Get the FOUserAgent instance associated with the rendering run represented by this instance.
  68. * @return the user agent
  69. */
  70. public FOUserAgent getUserAgent() {
  71. return foUserAgent;
  72. }
  73. /**
  74. * Creates a DefaultHandler object used to generate the document.
  75. * Note this object implements the ContentHandler interface.
  76. * For processing with a Transformer object, this DefaultHandler object
  77. * can be used in the SAXResult constructor.
  78. * Alternatively, for processing with a SAXParser, this object can be
  79. * used as the DefaultHandler argument to its parse() methods.
  80. *
  81. * @throws FOPException if setting up the DefaultHandler fails
  82. */
  83. private void createDefaultHandler() throws FOPException {
  84. this.foTreeBuilder = new FOTreeBuilder(outputFormat, foUserAgent, stream);
  85. }
  86. /**
  87. * Returns the DefaultHandler object that will receive the SAX stream containing the
  88. * FO document to be rendered.
  89. * @return the SAX DefaultHandler for handling the SAX events.
  90. * @throws FOPException if setting up the DefaultHandler fails
  91. */
  92. public DefaultHandler getDefaultHandler() throws FOPException {
  93. if (foTreeBuilder == null) {
  94. createDefaultHandler();
  95. }
  96. return this.foTreeBuilder;
  97. }
  98. /**
  99. * Returns the results of the rendering process. Information includes
  100. * the total number of pages generated and the number of pages per
  101. * page-sequence. Call this method only after the rendering process is
  102. * finished. Note that the results are only available for output formats
  103. * which make use of FOP's layout engine (PDF, PS, etc.).
  104. * @return the results of the rendering process, or null for flow-oriented
  105. * output formats like RTF and MIF.
  106. */
  107. public FormattingResults getResults() {
  108. if (foTreeBuilder == null) {
  109. throw new IllegalStateException(
  110. "Results are only available after calling getDefaultHandler().");
  111. } else {
  112. return foTreeBuilder.getResults();
  113. }
  114. }
  115. }