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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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;
  43. // output stream to send results to
  44. private OutputStream stream;
  45. // FOUserAgent object to set processing options
  46. private final FOUserAgent foUserAgent;
  47. // FOTreeBuilder object to maintain reference for access to results
  48. private FOTreeBuilder foTreeBuilder;
  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. if (ua == null) {
  59. throw new FOPException("Cannot create a new Fop instance without a User Agent.");
  60. }
  61. this.outputFormat = outputFormat;
  62. foUserAgent = ua;
  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. * @deprecated this getter doesn't need to exist. By virtue of the fact that the client has
  71. * an instance of this object, it means they also have the {@link FOUserAgent} since this's
  72. * constructor is only used in {@link FOUserAgent}
  73. */
  74. public FOUserAgent getUserAgent() {
  75. return foUserAgent;
  76. }
  77. /**
  78. * Creates a DefaultHandler object used to generate the document.
  79. * Note this object implements the ContentHandler interface.
  80. * For processing with a Transformer object, this DefaultHandler object
  81. * can be used in the SAXResult constructor.
  82. * Alternatively, for processing with a SAXParser, this object can be
  83. * used as the DefaultHandler argument to its parse() methods.
  84. *
  85. * @throws FOPException if setting up the DefaultHandler fails
  86. */
  87. private void createDefaultHandler() throws FOPException {
  88. this.foTreeBuilder = new FOTreeBuilder(outputFormat, foUserAgent, stream);
  89. }
  90. /**
  91. * Returns the DefaultHandler object that will receive the SAX stream containing the
  92. * FO document to be rendered.
  93. * @return the SAX DefaultHandler for handling the SAX events.
  94. * @throws FOPException if setting up the DefaultHandler fails
  95. */
  96. public DefaultHandler getDefaultHandler() throws FOPException {
  97. if (foTreeBuilder == null) {
  98. createDefaultHandler();
  99. }
  100. return this.foTreeBuilder;
  101. }
  102. /**
  103. * Returns the results of the rendering process. Information includes
  104. * the total number of pages generated and the number of pages per
  105. * page-sequence. Call this method only after the rendering process is
  106. * finished. Note that the results are only available for output formats
  107. * which make use of FOP's layout engine (PDF, PS, etc.).
  108. * @return the results of the rendering process, or null for flow-oriented
  109. * output formats like RTF and MIF.
  110. */
  111. public FormattingResults getResults() {
  112. if (foTreeBuilder == null) {
  113. throw new IllegalStateException(
  114. "Results are only available after calling getDefaultHandler().");
  115. } else {
  116. return foTreeBuilder.getResults();
  117. }
  118. }
  119. }