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.

XSLTInputHandler.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.apps;
  18. // Imported java.io classes
  19. import java.io.File;
  20. import java.util.Vector;
  21. // Imported TraX classes
  22. import javax.xml.transform.Source;
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.TransformerFactory;
  25. import javax.xml.transform.stream.StreamSource;
  26. import javax.xml.transform.sax.SAXResult;
  27. import javax.xml.transform.sax.SAXSource;
  28. import javax.xml.transform.sax.SAXTransformerFactory;
  29. // Imported SAX classes
  30. import org.xml.sax.InputSource;
  31. import org.xml.sax.XMLReader;
  32. import org.xml.sax.XMLFilter;
  33. /**
  34. * XSLTInputHandler basically takes an XML file and transforms it with an XSLT
  35. * file and the resulting XSL-FO document is input for FOP.
  36. */
  37. public class XSLTInputHandler extends InputHandler {
  38. private StreamSource xmlSource;
  39. private Source xsltSource;
  40. private Vector xsltParams = null; // not yet implemented
  41. /**
  42. * Constructor for files as input
  43. * @param xmlfile XML file
  44. * @param xsltfile XSLT file
  45. * @param params Vector of command-line parameters (name, value,
  46. * name, value, ...) for XSL stylesheet
  47. * @throws FOPException if initializing the Transformer fails
  48. */
  49. public XSLTInputHandler(File xmlfile, File xsltfile, Vector params) throws FOPException {
  50. this.xmlSource = new StreamSource(xmlfile);
  51. this.xsltSource = new StreamSource(xsltfile);
  52. xsltParams = params;
  53. }
  54. /**
  55. * Constructor for files as input
  56. * @param xmlfile XML file
  57. * @param xsltfile XSLT file
  58. * @throws FOPException if initializing the Transformer fails
  59. * @deprecated Use JAXP instead.
  60. */
  61. public XSLTInputHandler(File xmlfile, File xsltfile) throws FOPException {
  62. this.xmlSource = new StreamSource(xmlfile);
  63. this.xsltSource = new StreamSource(xsltfile);
  64. }
  65. /**
  66. * Constructor with URIs/URLs as input.
  67. * @param xmlURL XML URL
  68. * @param xsltURL XSLT URL
  69. * @throws FOPException if initializing the Transformer fails
  70. * @deprecated Use JAXP instead.
  71. */
  72. public XSLTInputHandler(String xmlURL, String xsltURL) throws FOPException {
  73. this.xmlSource = new StreamSource(xmlURL);
  74. this.xsltSource = new StreamSource(xsltURL);
  75. }
  76. /**
  77. * Constructor with InputSources as input.
  78. * @param xmlSource XML InputSource
  79. * @param xsltSource XSLT InputSource
  80. * @throws FOPException if initializing the Transformer fails
  81. * @deprecated Use JAXP instead.
  82. */
  83. public XSLTInputHandler(InputSource xmlSource, InputSource xsltSource)
  84. throws FOPException {
  85. this.xmlSource = new StreamSource(xmlSource.getByteStream(),
  86. xmlSource.getSystemId());
  87. this.xsltSource = new StreamSource(xsltSource.getByteStream(),
  88. xsltSource.getSystemId());
  89. }
  90. /**
  91. * @see org.apache.fop.apps.InputHandler#getInputSource()
  92. */
  93. public InputSource getInputSource() {
  94. InputSource is = new InputSource();
  95. is.setByteStream(xmlSource.getInputStream());
  96. is.setSystemId(xmlSource.getSystemId());
  97. return is;
  98. }
  99. /**
  100. * Overwrites this method of the super class and returns an XMLFilter
  101. * instead of a simple XMLReader which allows chaining of transformations.
  102. * @see org.apache.fop.apps.InputHandler#getParser()
  103. */
  104. public XMLReader getParser() throws FOPException {
  105. return getXMLFilter(xsltSource, xsltParams);
  106. }
  107. /**
  108. * Creates from the transformer an instance of an XMLFilter which
  109. * then can be used in a chain with the XMLReader passed to Driver. This way
  110. * during the conversion of the xml file + xslt stylesheet the resulting
  111. * data is fed into Fop. This should help to avoid memory problems
  112. * @param xsltSource An xslt stylesheet
  113. * @return an XMLFilter which can be chained together with other
  114. * XMLReaders or XMLFilters
  115. * @throws FOPException if setting up the XMLFilter fails
  116. */
  117. public static XMLFilter getXMLFilter(Source xsltSource, Vector inParams) throws FOPException {
  118. try {
  119. // Instantiate a TransformerFactory.
  120. TransformerFactory tFactory = TransformerFactory.newInstance();
  121. // Determine whether the TransformerFactory supports The use of SAXSource
  122. // and SAXResult
  123. if (tFactory.getFeature(SAXSource.FEATURE)
  124. && tFactory.getFeature(SAXResult.FEATURE)) {
  125. // Cast the TransformerFactory to SAXTransformerFactory.
  126. SAXTransformerFactory saxTFactory =
  127. ((SAXTransformerFactory)tFactory);
  128. // Create an XMLFilter for each stylesheet.
  129. XMLFilter xmlfilter =
  130. saxTFactory.newXMLFilter(xsltSource);
  131. /* if (inParams != null) {
  132. Transformer transformer = ??? how to obtain from an XMLFilter?
  133. int nParams = inParams.size();
  134. for (int i = 0; i < nParams; i += 2) {
  135. transformer.setParameter((String) inParams.elementAt(i),
  136. (String) inParams.elementAt(i + 1));
  137. }
  138. }
  139. */
  140. // Create an XMLReader.
  141. XMLReader parser = FOFileHandler.createParser();
  142. if (parser == null) {
  143. throw new FOPException("Unable to create SAX parser");
  144. }
  145. // xmlFilter1 uses the XMLReader as its reader.
  146. xmlfilter.setParent(parser);
  147. return xmlfilter;
  148. } else {
  149. throw new FOPException("Your parser doesn't support the "
  150. + "features SAXSource and SAXResult."
  151. + "\nMake sure you are using an XSLT engine which "
  152. + "supports TrAX");
  153. }
  154. } catch (FOPException fe) {
  155. throw fe;
  156. } catch (Exception ex) {
  157. throw new FOPException(ex);
  158. }
  159. }
  160. }