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

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