Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

XSLTInputHandler.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. import java.lang.reflect.*;
  8. // Imported SAX classes
  9. import org.xml.sax.InputSource;
  10. import org.xml.sax.SAXException;
  11. import org.xml.sax.XMLReader;
  12. // Imported java.io classes
  13. import java.io.*;
  14. // FOP
  15. import org.apache.fop.messaging.MessageHandler;
  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(), xsltfile.getCanonicalPath(), writer);
  47. writer.flush();
  48. writer.close();
  49. if (tmpFile != null) {
  50. reader = new FileReader(tmpFile);
  51. } else {
  52. // create a input source containing the xsl:fo file which can be fed to Fop
  53. reader = new StringReader(writer.toString());
  54. }
  55. return new InputSource(reader);
  56. }
  57. catch (Exception ex) {
  58. ex.printStackTrace();
  59. return null;
  60. }
  61. }
  62. else {
  63. return fileInputSource(xmlfile);
  64. }
  65. }
  66. /**
  67. * This looks to see if the Trax api is supported and uses that to
  68. * get an XMLFilter. Otherwise, it falls back to using DOM documents
  69. *
  70. */
  71. public XMLReader getParser()
  72. throws FOPException
  73. {
  74. XMLReader result = null;
  75. try {
  76. // try trax first
  77. Class transformer = Class.forName("javax.xml.transform.Transformer");
  78. transformer = Class.forName("org.apache.fop.apps.TraxInputHandler");
  79. Class[] argTypes = { File.class, File.class };
  80. Method getFilterMethod = transformer.getMethod("getXMLFilter",argTypes);
  81. File[] args = {xmlfile, xsltfile};
  82. Object obj = getFilterMethod.invoke(null,args);
  83. if (obj instanceof XMLReader) {
  84. result = (XMLReader)obj;
  85. }
  86. }
  87. catch (ClassNotFoundException ex){
  88. throw new FOPException(ex);
  89. }
  90. catch (InvocationTargetException ex) {
  91. throw new FOPException(ex);
  92. }
  93. catch (IllegalAccessException ex) {
  94. throw new FOPException(ex);
  95. }
  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. }