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.

TraxInputHandler.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * $Id: TraxInputHandler.java,v 1.6 2003/02/27 10:13:04 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.apps;
  52. // Imported java.io classes
  53. import java.io.File;
  54. // Imported TraX classes
  55. import javax.xml.transform.Source;
  56. import javax.xml.transform.Transformer;
  57. import javax.xml.transform.TransformerFactory;
  58. import javax.xml.transform.stream.StreamSource;
  59. import javax.xml.transform.sax.SAXResult;
  60. import javax.xml.transform.sax.SAXSource;
  61. import javax.xml.transform.sax.SAXTransformerFactory;
  62. // Imported SAX classes
  63. import org.xml.sax.InputSource;
  64. import org.xml.sax.XMLReader;
  65. import org.xml.sax.XMLFilter;
  66. /**
  67. * XSLTInputHandler basically takes an XML file and transforms it with an
  68. * XSLT file and the resulting XSL-FO document is input for FOP.
  69. */
  70. public class TraxInputHandler extends InputHandler {
  71. private Transformer transformer;
  72. private StreamSource xmlSource;
  73. private Source xsltSource;
  74. /**
  75. * Constructor with files as input.
  76. * @param xmlfile XML file
  77. * @param xsltfile XSLT file
  78. * @throws FOPException if initializing the Transformer fails
  79. */
  80. public TraxInputHandler(File xmlfile, File xsltfile) throws FOPException {
  81. this.xmlSource = new StreamSource(xmlfile);
  82. this.xsltSource = new StreamSource(xsltfile);
  83. initTransformer();
  84. }
  85. /**
  86. * Constructor with URIs/URLs as input.
  87. * @param xmlURL XML URL
  88. * @param xsltURL XSLT URL
  89. * @throws FOPException if initializing the Transformer fails
  90. */
  91. public TraxInputHandler(String xmlURL, String xsltURL) throws FOPException {
  92. this.xmlSource = new StreamSource(xmlURL);
  93. this.xsltSource = new StreamSource(xsltURL);
  94. initTransformer();
  95. }
  96. /**
  97. * Constructor with InputSources as input.
  98. * @param xmlSource XML InputSource
  99. * @param xsltSource XSLT InputSource
  100. * @throws FOPException if initializing the Transformer fails
  101. */
  102. public TraxInputHandler(InputSource xmlSource, InputSource xsltSource)
  103. throws FOPException {
  104. this.xmlSource = new StreamSource(xmlSource.getByteStream(),
  105. xmlSource.getSystemId());
  106. this.xsltSource = new StreamSource(xsltSource.getByteStream(),
  107. xsltSource.getSystemId());
  108. initTransformer();
  109. }
  110. private void initTransformer() throws FOPException {
  111. try {
  112. this.transformer =
  113. TransformerFactory.newInstance().newTransformer(xsltSource);
  114. } catch (Exception ex) {
  115. throw new FOPException(ex);
  116. }
  117. }
  118. /**
  119. * @see org.apache.fop.apps.InputHandler#getInputSource()
  120. */
  121. public InputSource getInputSource() {
  122. InputSource is = new InputSource();
  123. is.setByteStream(xmlSource.getInputStream());
  124. is.setSystemId(xmlSource.getSystemId());
  125. return is;
  126. }
  127. /**
  128. * Overwrites this method of the super class and returns an XMLFilter
  129. * instead of a simple XMLReader which allows chaining of transformations.
  130. * @see org.apache.fop.apps.InputHandler#getParser()
  131. */
  132. public XMLReader getParser() throws FOPException {
  133. return getXMLFilter(xsltSource);
  134. }
  135. /**
  136. * Creates from the transformer an instance of an XMLFilter which
  137. * then can be used in a chain with the XMLReader passed to Driver. This way
  138. * during the conversion of the xml file + xslt stylesheet the resulting
  139. * data is fed into Fop. This should help to avoid memory problems
  140. * @param xsltSource An xslt stylesheet
  141. * @return an XMLFilter which can be chained together with other
  142. * XMLReaders or XMLFilters
  143. * @throws FOPException if setting up the XMLFilter fails
  144. */
  145. public static XMLFilter getXMLFilter(Source xsltSource) throws FOPException {
  146. try {
  147. // Instantiate a TransformerFactory.
  148. TransformerFactory tFactory = TransformerFactory.newInstance();
  149. // Determine whether the TransformerFactory supports The use uf SAXSource
  150. // and SAXResult
  151. if (tFactory.getFeature(SAXSource.FEATURE)
  152. && tFactory.getFeature(SAXResult.FEATURE)) {
  153. // Cast the TransformerFactory to SAXTransformerFactory.
  154. SAXTransformerFactory saxTFactory =
  155. ((SAXTransformerFactory)tFactory);
  156. // Create an XMLFilter for each stylesheet.
  157. XMLFilter xmlfilter =
  158. saxTFactory.newXMLFilter(xsltSource);
  159. // Create an XMLReader.
  160. XMLReader parser = createParser();
  161. if (parser == null) {
  162. throw new FOPException("Unable to create SAX parser");
  163. }
  164. // xmlFilter1 uses the XMLReader as its reader.
  165. xmlfilter.setParent(parser);
  166. return xmlfilter;
  167. } else {
  168. throw new FOPException("Your parser doesn't support the "
  169. + "features SAXSource and SAXResult."
  170. + "\nMake sure you are using an XSLT engine which "
  171. + "supports TrAX");
  172. }
  173. } catch (FOPException fe) {
  174. throw fe;
  175. } catch (Exception ex) {
  176. throw new FOPException(ex);
  177. }
  178. }
  179. /**
  180. * @see org.apache.fop.apps.InputHandler#run(Driver)
  181. */
  182. public void run(Driver driver) throws FOPException {
  183. try {
  184. transformer.transform(xmlSource,
  185. new SAXResult(driver.getContentHandler()));
  186. } catch (Exception ex) {
  187. throw new FOPException(ex);
  188. }
  189. }
  190. /**
  191. * Sets an XSLT parameter.
  192. * @param name the name of the parameter
  193. * @param value the value of the parameter
  194. */
  195. public void setParameter(String name, Object value) {
  196. transformer.setParameter(name, value);
  197. }
  198. }