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 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.apps;
  8. // Imported TraX classes
  9. import javax.xml.transform.TransformerFactory;
  10. import javax.xml.transform.stream.StreamSource;
  11. import javax.xml.transform.sax.SAXResult;
  12. import javax.xml.transform.sax.SAXSource;
  13. import javax.xml.transform.sax.SAXTransformerFactory;
  14. // Imported SAX classes
  15. import org.xml.sax.InputSource;
  16. import org.xml.sax.SAXException;
  17. import org.xml.sax.XMLReader;
  18. import org.xml.sax.XMLFilter;
  19. // Imported java.io classes
  20. import java.io.InputStream;
  21. import java.io.IOException;
  22. import java.io.File;
  23. /**
  24. * XSLTInputHandler basically takes an xmlfile and transforms it with an xsltfile
  25. * and the resulting xsl:fo document is input for Fop.
  26. */
  27. public class TraxInputHandler extends InputHandler {
  28. File xmlfile, xsltfile;
  29. public TraxInputHandler(File xmlfile, File xsltfile) {
  30. this.xmlfile = xmlfile;
  31. this.xsltfile = xsltfile;
  32. }
  33. /**
  34. * overwrites the method of the super class to return the xmlfile
  35. */
  36. public InputSource getInputSource() {
  37. return fileInputSource(xmlfile);
  38. }
  39. /**
  40. * overwrites this method of the super class and returns an XMLFilter instead of a
  41. * simple XMLReader which allows chaining of transformations
  42. *
  43. */
  44. public XMLReader getParser() throws FOPException {
  45. return this.getXMLFilter(xmlfile, xsltfile);
  46. }
  47. /**
  48. * Creates from the transformer an instance of an XMLFilter which
  49. * then can be used in a chain with the XMLReader passed to Driver. This way
  50. * during the conversion of the xml file + xslt stylesheet the resulting
  51. * data is fed into Fop. This should help to avoid memory problems
  52. * @param xmlfile The xmlfile containing the text data
  53. * @param xsltfile An xslt stylesheet
  54. * @return XMLFilter an XMLFilter which can be chained together with other XMLReaders or XMLFilters
  55. */
  56. public static XMLFilter getXMLFilter(File xmlfile,
  57. File xsltfile) throws FOPException {
  58. try {
  59. // Instantiate a TransformerFactory.
  60. TransformerFactory tFactory = TransformerFactory.newInstance();
  61. // Determine whether the TransformerFactory supports The use uf SAXSource
  62. // and SAXResult
  63. if (tFactory.getFeature(SAXSource.FEATURE)
  64. && tFactory.getFeature(SAXResult.FEATURE)) {
  65. // Cast the TransformerFactory to SAXTransformerFactory.
  66. SAXTransformerFactory saxTFactory =
  67. ((SAXTransformerFactory)tFactory);
  68. // Create an XMLFilter for each stylesheet.
  69. XMLFilter xmlfilter =
  70. saxTFactory.newXMLFilter(new StreamSource(xsltfile));
  71. // Create an XMLReader.
  72. XMLReader parser = createParser();
  73. if (parser == null) {
  74. throw new FOPException("Unable to create SAX parser");
  75. }
  76. // xmlFilter1 uses the XMLReader as its reader.
  77. xmlfilter.setParent(parser);
  78. return xmlfilter;
  79. } else {
  80. throw new FOPException("Your parser doesn't support the features SAXSource and SAXResult."
  81. + "\nMake sure you are using a xsl parser which supports TrAX");
  82. }
  83. } catch (Exception ex) {
  84. if (ex instanceof FOPException) {
  85. throw (FOPException)ex;
  86. }
  87. throw new FOPException(ex);
  88. }
  89. }
  90. }