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.

Fop.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Ant
  2. import org.apache.tools.ant.Task;
  3. import org.apache.tools.ant.BuildException;
  4. import org.apache.fop.apps.*;
  5. // SAX
  6. import org.xml.sax.Parser;
  7. import org.xml.sax.InputSource;
  8. import org.xml.sax.SAXException;
  9. import org.xml.sax.SAXParseException;
  10. // Java
  11. import java.io.FileReader;
  12. import java.io.File;
  13. import java.io.FileWriter;
  14. import java.io.PrintWriter;
  15. import java.io.IOException;
  16. import java.io.FileNotFoundException;
  17. import java.net.URL;
  18. /* the code is adapted from Fops CommandLine class */
  19. public class Fop {
  20. String fofile, pdffile;
  21. public void setFofile(String fofile) {
  22. this.fofile = fofile;
  23. }
  24. public void setPdffile(String pdffile) {
  25. this.pdffile = pdffile;
  26. }
  27. /**
  28. * creates a SAX parser, using the value of org.xml.sax.parser
  29. * defaulting to org.apache.xerces.parsers.SAXParser
  30. *
  31. * @return the created SAX parser
  32. */
  33. static Parser createParser() {
  34. String parserClassName = System.getProperty("org.xml.sax.parser");
  35. if (parserClassName == null) {
  36. parserClassName = "org.apache.xerces.parsers.SAXParser";
  37. }
  38. System.err.println("using SAX parser " + parserClassName);
  39. try {
  40. return (Parser) Class.forName(parserClassName).newInstance();
  41. } catch (ClassNotFoundException e) {
  42. System.err.println("Could not find " + parserClassName);
  43. } catch (InstantiationException e) {
  44. System.err.println("Could not instantiate " + parserClassName);
  45. } catch (IllegalAccessException e) {
  46. System.err.println("Could not access " + parserClassName);
  47. } catch (ClassCastException e) {
  48. System.err.println(parserClassName + " is not a SAX driver");
  49. }
  50. return null;
  51. }
  52. /**
  53. * create an InputSource from a file name
  54. *
  55. * @param filename the name of the file
  56. * @return the InputSource created
  57. */
  58. protected static InputSource fileInputSource(String filename) {
  59. /* this code adapted from James Clark's in XT */
  60. File file = new File(filename);
  61. String path = file.getAbsolutePath();
  62. String fSep = System.getProperty("file.separator");
  63. if (fSep != null && fSep.length() == 1)
  64. path = path.replace(fSep.charAt(0), '/');
  65. if (path.length() > 0 && path.charAt(0) != '/')
  66. path = '/' + path;
  67. try {
  68. return new InputSource(new URL("file", null,
  69. path).toString());
  70. }
  71. catch (java.net.MalformedURLException e) {
  72. throw new Error("unexpected MalformedURLException");
  73. }
  74. }
  75. public void execute () throws BuildException {
  76. boolean errors = false;
  77. String version = Version.getVersion();
  78. System.out.println("=======================\nTask " + version +
  79. "\nconverting file " + fofile + " to " + pdffile);
  80. if (!(new File(fofile).exists())) {
  81. errors = true;
  82. System.err.println("Task Fop - ERROR: Formatting objects file " + fofile + " missing.");
  83. }
  84. Parser parser = createParser();
  85. if (parser == null) {
  86. System.err.println("Task Fop - ERROR: Unable to create SAX parser");
  87. errors = true;
  88. }
  89. if (!errors) {
  90. try {
  91. Driver driver = new Driver();
  92. driver.setRenderer("org.apache.fop.render.pdf.PDFRenderer", version);
  93. driver.addElementMapping("org.apache.fop.fo.StandardElementMapping");
  94. driver.addElementMapping("org.apache.fop.svg.SVGElementMapping");
  95. driver.setWriter(new PrintWriter(new FileWriter(pdffile)));
  96. driver.buildFOTree(parser, fileInputSource(fofile));
  97. driver.format();
  98. driver.render();
  99. } catch (Exception e) {
  100. System.err.println("Task Fop - FATAL ERROR: " + e.getMessage());
  101. System.exit(1);
  102. }
  103. }
  104. System.out.println("=======================\n");
  105. }
  106. }