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

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