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.

FODocumentParser.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.fo;
  19. import java.io.File;
  20. import java.io.InputStream;
  21. import javax.xml.transform.Result;
  22. import javax.xml.transform.Source;
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.TransformerConfigurationException;
  25. import javax.xml.transform.TransformerException;
  26. import javax.xml.transform.TransformerFactory;
  27. import javax.xml.transform.sax.SAXResult;
  28. import javax.xml.transform.stream.StreamSource;
  29. import org.apache.fop.apps.FOPException;
  30. import org.apache.fop.apps.FOUserAgent;
  31. import org.apache.fop.apps.Fop;
  32. import org.apache.fop.apps.FopFactory;
  33. import org.apache.fop.events.EventListener;
  34. /**
  35. * Parse an FO document and run the corresponding FO events through a given
  36. * {@link FOEventHandler} instance. That instance is created using the helper
  37. * {@link FOEventHandlerFactory}.
  38. *
  39. * <p>An instance of this class may not be used in multiple threads concurrently.<p>
  40. *
  41. * <p>An instance of this class may be used multiple times if the given
  42. * {@link FOEventHandler} implementation can be used multiple times.
  43. */
  44. public final class FODocumentParser {
  45. private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
  46. private static final FopFactory FOP_FACTORY = FopFactory.newInstance(new File(".").toURI());
  47. private final FOEventHandlerFactory foEventHandlerFactory;
  48. private Fop fop;
  49. private Transformer transformer;
  50. private EventListener eventListener;
  51. /**
  52. * A factory to create custom instances of {@link FOEventHandler}.
  53. */
  54. public interface FOEventHandlerFactory {
  55. /**
  56. * Creates a new {@code FOEventHandler} instance parameterized with the given FO user agent.
  57. *
  58. * @param foUserAgent an FO user agent
  59. * @return a new {@code FOEventHandler} instance
  60. */
  61. FOEventHandler newFOEventHandler(FOUserAgent foUserAgent);
  62. }
  63. private FODocumentParser(FOEventHandlerFactory foeEventHandlerFactory) {
  64. this.foEventHandlerFactory = foeEventHandlerFactory;
  65. }
  66. /**
  67. * Creates and returns a new FO document parser. The given factory will be used to
  68. * customize the handler that will receive FO events, using the
  69. * {@link FOUserAgent#setFOEventHandlerOverride(FOEventHandler)} method.
  70. *
  71. * @param foEventHandlerFactory the factory to be used to create {@code
  72. * FOEventHandler} instances
  73. * @return a new parser
  74. */
  75. public static FODocumentParser newInstance(FOEventHandlerFactory foEventHandlerFactory) {
  76. return new FODocumentParser(foEventHandlerFactory);
  77. }
  78. /**
  79. * Sets the event listener to be used if events occurs when parsing the document.
  80. *
  81. * @param eventListener an event listener
  82. */
  83. public void setEventListener(EventListener eventListener) {
  84. this.eventListener = eventListener;
  85. }
  86. /**
  87. * Runs FOP on the given document.
  88. *
  89. * @param document XSL-FO document to parse
  90. * @throws FOPException if an error occurs when initializing FOP
  91. * @throws LoadingException if an error occurs when parsing the document
  92. */
  93. public void parse(InputStream document) throws FOPException, LoadingException {
  94. parse(document, createFOUserAgent());
  95. }
  96. /**
  97. * Runs FOP on the given document with the supplied {@link FOUserAgent}.
  98. *
  99. * @param document XSL-FO document to parse
  100. * @param foUserAgent The user agent
  101. * @throws FOPException if an error occurs when initializing FOP
  102. * @throws LoadingException if an error occurs when parsing the document
  103. */
  104. public void parse(InputStream document, FOUserAgent foUserAgent)
  105. throws FOPException, LoadingException {
  106. fop = FOP_FACTORY.newFop(foUserAgent);
  107. createTransformer();
  108. runTransformer(document);
  109. }
  110. /**
  111. * Creates a new {@link FOUserAgent}.
  112. * @return It
  113. */
  114. public FOUserAgent createFOUserAgent() {
  115. FOUserAgent userAgent = FOP_FACTORY.newFOUserAgent();
  116. FOEventHandler foEventHandler = foEventHandlerFactory.newFOEventHandler(userAgent);
  117. userAgent.setFOEventHandlerOverride(foEventHandler);
  118. if (eventListener != null) {
  119. userAgent.getEventBroadcaster().addEventListener(eventListener);
  120. }
  121. return userAgent;
  122. }
  123. private void createTransformer() {
  124. try {
  125. transformer = TRANSFORMER_FACTORY.newTransformer();
  126. } catch (TransformerConfigurationException e) {
  127. throw new RuntimeException(e);
  128. }
  129. }
  130. private void runTransformer(InputStream input) throws LoadingException, FOPException {
  131. Source source = new StreamSource(input);
  132. Result result = new SAXResult(fop.getDefaultHandler());
  133. try {
  134. transformer.transform(source, result);
  135. } catch (TransformerException e) {
  136. Throwable cause = e.getCause();
  137. throw new LoadingException(cause == null ? e : cause);
  138. }
  139. }
  140. }