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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. ============================================================================
  3. The Apache Software License, Version 1.1
  4. ============================================================================
  5. Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modifica-
  7. tion, are permitted provided that the following conditions are met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. 3. The end-user documentation included with the redistribution, if any, must
  14. include the following acknowledgment: "This product includes software
  15. developed by the Apache Software Foundation (http://www.apache.org/)."
  16. Alternately, this acknowledgment may appear in the software itself, if
  17. and wherever such third-party acknowledgments normally appear.
  18. 4. The names "Fop" and "Apache Software Foundation" must not be used to
  19. endorse or promote products derived from this software without prior
  20. written permission. For written permission, please contact
  21. apache@apache.org.
  22. 5. Products derived from this software may not be called "Apache", nor may
  23. "Apache" appear in their name, without prior written permission of the
  24. Apache Software Foundation.
  25. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  26. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  30. DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. This software consists of voluntary contributions made by many individuals
  36. on behalf of the Apache Software Foundation and was originally created by
  37. James Tauber <jtauber@jtauber.com>. For more information on the Apache
  38. Software Foundation, please see <http://www.apache.org/>.
  39. */
  40. package org.apache.fop.tools.anttasks;
  41. // Ant
  42. import org.apache.tools.ant.Task;
  43. import org.apache.tools.ant.BuildException;
  44. // SAX
  45. import org.xml.sax.XMLReader;
  46. import org.xml.sax.InputSource;
  47. import org.xml.sax.SAXException;
  48. import org.xml.sax.SAXParseException;
  49. // Java
  50. import java.io.*;
  51. import java.net.URL;
  52. // FOP
  53. import org.apache.fop.messaging.MessageHandler;
  54. import org.apache.fop.apps.*;
  55. import org.apache.fop.configuration.Configuration;
  56. /**
  57. * extension to Ant which allows usage of Fop in the
  58. * same way as org.apache.fop.apps.CommandLine (the code is adapted from this class)
  59. * Gets input and output filenames from the script file <br/>
  60. * needed libraries: Sax 2 parser (defaults to Xerces-J), Jimi for images, w3c.jar
  61. * containing org.w3c.dom.svg etc. for svg support
  62. */
  63. public class Fop extends Starter {
  64. String fofile, pdffile;
  65. /**
  66. * sets the name of the input file
  67. * @param String name of the input fo file
  68. */
  69. public void setFofile(String fofile) {
  70. this.fofile = fofile;
  71. }
  72. /**
  73. * sets the name of the output file
  74. * @param String name of the output pdf file
  75. */
  76. public void setPdffile(String pdffile) {
  77. this.pdffile = pdffile;
  78. }
  79. public void run () {
  80. Options options = new Options();
  81. boolean errors = false;
  82. String version = Version.getVersion();
  83. File fofileF = new File (fofile);
  84. Configuration.put("baseDir",new File(fofileF.getAbsolutePath()).getParent());
  85. if (!fofileF.exists()) {
  86. errors = true;
  87. MessageHandler.errorln("Task Fop - ERROR: Formatting objects file " +
  88. fofile + " missing.");
  89. }
  90. InputHandler inputHandler = new FOInputHandler(fofileF);
  91. XMLReader parser = inputHandler.getParser();
  92. super.setParserFeatures(parser);
  93. MessageHandler.logln("=======================\nTask " +
  94. version + "\nconverting file " + fofile + " to " + pdffile);
  95. if (!errors) {
  96. try {
  97. Driver driver = new Driver(inputHandler.fileInputSource(fofileF), new FileOutputStream(pdffile));
  98. driver.setRenderer(Driver.RENDER_PDF);
  99. driver.setXMLReader(parser);
  100. driver.run();
  101. } catch (Exception e) {
  102. MessageHandler.errorln("Task Fop - FATAL ERROR: " +
  103. e.getMessage());
  104. System.exit(1);
  105. }
  106. }
  107. MessageHandler.logln("=======================\n");
  108. }
  109. /**
  110. * main method, starts execution of this task
  111. *
  112. */
  113. public void execute () throws BuildException {
  114. run();
  115. } // end: execute
  116. }