選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Fop.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.accessibility.AccessibilityUtil;
  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. 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. * Get the FOUserAgent instance associated with the rendering run represented by this instance.
  69. * @return the user agent
  70. */
  71. public FOUserAgent getUserAgent() {
  72. return foUserAgent;
  73. }
  74. /**
  75. * Creates a DefaultHandler object used to generate the document.
  76. * Note this object implements the ContentHandler interface.
  77. * For processing with a Transformer object, this DefaultHandler object
  78. * can be used in the SAXResult constructor.
  79. * Alternatively, for processing with a SAXParser, this object can be
  80. * used as the DefaultHandler argument to its parse() methods.
  81. *
  82. * @throws FOPException if setting up the DefaultHandler fails
  83. */
  84. private void createDefaultHandler() throws FOPException {
  85. this.foTreeBuilder = new FOTreeBuilder(outputFormat, foUserAgent, stream);
  86. }
  87. /**
  88. * Returns the DefaultHandler object that will receive the SAX stream containing the
  89. * FO document to be rendered.
  90. * @return the SAX DefaultHandler for handling the SAX events.
  91. * @throws FOPException if setting up the DefaultHandler fails
  92. */
  93. public DefaultHandler getDefaultHandler() throws FOPException {
  94. if (foTreeBuilder == null) {
  95. createDefaultHandler();
  96. }
  97. if (this.foUserAgent.isAccessibilityEnabled()) {
  98. return AccessibilityUtil.decorateDefaultHandler(this.foTreeBuilder, foUserAgent);
  99. } else {
  100. return this.foTreeBuilder;
  101. }
  102. }
  103. /**
  104. * Returns the results of the rendering process. Information includes
  105. * the total number of pages generated and the number of pages per
  106. * page-sequence. Call this method only after the rendering process is
  107. * finished. Note that the results are only available for output formats
  108. * which make use of FOP's layout engine (PDF, PS, etc.).
  109. * @return the results of the rendering process, or null for flow-oriented
  110. * output formats like RTF and MIF.
  111. */
  112. public FormattingResults getResults() {
  113. if (foTreeBuilder == null) {
  114. throw new IllegalStateException(
  115. "Results are only available after calling getDefaultHandler().");
  116. } else {
  117. return foTreeBuilder.getResults();
  118. }
  119. }
  120. }