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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. import java.lang.reflect.*;
  9. // Imported SAX classes
  10. import org.xml.sax.InputSource;
  11. import org.xml.sax.SAXException;
  12. import org.xml.sax.XMLReader;
  13. // Imported java.io classes
  14. import java.io.*;
  15. // FOP
  16. import org.apache.fop.tools.xslt.XSLTransform;
  17. /**
  18. * XSLTInputHandler basically takes an xmlfile and transforms it with an xsltfile
  19. * and the resulting xsl:fo document is input for Fop.
  20. */
  21. public class XSLTInputHandler extends InputHandler {
  22. File xmlfile, xsltfile;
  23. boolean useOldTransform = false;
  24. public XSLTInputHandler(File xmlfile, File xsltfile) {
  25. this.xmlfile = xmlfile;
  26. this.xsltfile = xsltfile;
  27. }
  28. /**
  29. * overwrites the method of the super class to return the xmlfile
  30. */
  31. public InputSource getInputSource() {
  32. if (useOldTransform) {
  33. try {
  34. java.io.Writer writer;
  35. java.io.Reader reader;
  36. File tmpFile = null;
  37. // create a Writer
  38. // the following is an ugly hack to allow processing of larger files
  39. // if xml file size is larger than 500 kb write the fo:file to disk
  40. if ((xmlfile.length()) > 500000) {
  41. tmpFile = new File(xmlfile.getName() + ".fo.tmp");
  42. writer = new FileWriter(tmpFile);
  43. } else {
  44. writer = new StringWriter();
  45. }
  46. XSLTransform.transform(xmlfile.getCanonicalPath(),
  47. xsltfile.getCanonicalPath(), writer);
  48. writer.flush();
  49. writer.close();
  50. if (tmpFile != null) {
  51. reader = new FileReader(tmpFile);
  52. } else {
  53. // create a input source containing the xsl:fo file which can be fed to Fop
  54. reader = new StringReader(writer.toString());
  55. }
  56. return new InputSource(reader);
  57. } catch (Exception ex) {
  58. ex.printStackTrace();
  59. return null;
  60. }
  61. } else {
  62. return fileInputSource(xmlfile);
  63. }
  64. }
  65. /**
  66. * This looks to see if the Trax api is supported and uses that to
  67. * get an XMLFilter. Otherwise, it falls back to using DOM documents
  68. *
  69. */
  70. public XMLReader getParser() throws FOPException {
  71. XMLReader result = null;
  72. try {
  73. // try trax first
  74. Class transformer =
  75. Class.forName("javax.xml.transform.Transformer");
  76. transformer =
  77. Class.forName("org.apache.fop.apps.TraxInputHandler");
  78. Class[] argTypes = {
  79. File.class, File.class
  80. };
  81. Method getFilterMethod = transformer.getMethod("getXMLFilter",
  82. argTypes);
  83. File[] args = {
  84. xmlfile, xsltfile
  85. };
  86. Object obj = getFilterMethod.invoke(null, args);
  87. if (obj instanceof XMLReader) {
  88. result = (XMLReader)obj;
  89. }
  90. } catch (ClassNotFoundException ex) {
  91. throw new FOPException(ex);
  92. } catch (InvocationTargetException ex) {
  93. throw new FOPException(ex);
  94. } catch (IllegalAccessException ex) {
  95. throw new FOPException(ex);
  96. } catch (NoSuchMethodException ex) {
  97. throw new FOPException(ex);
  98. }
  99. // otherwise, use DOM documents via our XSLTransform tool class old style
  100. if (result == null) {
  101. useOldTransform = true;
  102. result = createParser();
  103. }
  104. return result;
  105. }
  106. }