您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TraxInputHandler.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 TraxInputHandler extends InputHandler {
  29. File xmlfile, xsltfile;
  30. public TraxInputHandler (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() throws FOPException {
  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. public static XMLFilter getXMLFilter (File xmlfile, File xsltfile)
  58. throws FOPException
  59. {
  60. try {
  61. // Instantiate a TransformerFactory.
  62. TransformerFactory tFactory = TransformerFactory.newInstance();
  63. // Determine whether the TransformerFactory supports The use uf SAXSource
  64. // and SAXResult
  65. if (tFactory.getFeature(SAXSource.FEATURE) &&
  66. tFactory.getFeature(SAXResult.FEATURE)) {
  67. // Cast the TransformerFactory to SAXTransformerFactory.
  68. SAXTransformerFactory saxTFactory =
  69. ((SAXTransformerFactory) tFactory);
  70. // Create an XMLFilter for each stylesheet.
  71. XMLFilter xmlfilter = saxTFactory.newXMLFilter(
  72. new StreamSource(xsltfile));
  73. // Create an XMLReader.
  74. XMLReader parser = createParser();
  75. if (parser == null) {
  76. throw new FOPException("Unable to create SAX parser");
  77. }
  78. // xmlFilter1 uses the XMLReader as its reader.
  79. xmlfilter.setParent(parser);
  80. return xmlfilter;
  81. } else {
  82. throw new FOPException(
  83. "Your parser doesn't support the features SAXSource and SAXResult." +
  84. "\nMake sure you are using a xsl parser which supports TrAX");
  85. }
  86. }
  87. catch (Exception ex) {
  88. if (ex instanceof FOPException) {
  89. throw (FOPException)ex;
  90. }
  91. throw new FOPException(ex);
  92. }
  93. }
  94. }