Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. // Ant
  41. import org.apache.tools.ant.Task;
  42. import org.apache.tools.ant.BuildException;
  43. // SAX
  44. import org.xml.sax.XMLReader;
  45. import org.xml.sax.InputSource;
  46. import org.xml.sax.SAXException;
  47. import org.xml.sax.SAXParseException;
  48. // Java
  49. import java.io.FileReader;
  50. import java.io.File;
  51. import java.io.FileWriter;
  52. import java.io.PrintWriter;
  53. import java.io.IOException;
  54. import java.io.FileNotFoundException;
  55. import java.net.URL;
  56. // FOP
  57. import org.apache.fop.messaging.MessageHandler;
  58. import org.apache.fop.apps.*;
  59. /**
  60. * extension to Ant which allows usage of Fop in the
  61. * same way as org.apache.fop.apps.CommandLine (the code is adapted from this class)
  62. * Gets input and output filenames from the script file <br/>
  63. * needed libraries: Sax 2 parser (defaults to Xerces-J), Jimi for images, w3c.jar
  64. * containing org.w3c.dom.svg etc. for svg support
  65. */
  66. public class Fop {
  67. String fofile, pdffile;
  68. /**
  69. * sets the name of the input file
  70. * @param String name of the input fo file
  71. */
  72. public void setFofile(String fofile) {
  73. this.fofile = fofile;
  74. }
  75. /**
  76. * sets the name of the output file
  77. * @param String name of the output pdf file
  78. */
  79. public void setPdffile(String pdffile) {
  80. this.pdffile = pdffile;
  81. }
  82. /**
  83. * creates a SAX parser, using the value of org.xml.sax.parser
  84. * defaulting to org.apache.xerces.parsers.SAXParser
  85. *
  86. * @return the created SAX parser
  87. */
  88. static XMLReader createParser() {
  89. String parserClassName =
  90. System.getProperty("org.xml.sax.parser");
  91. if (parserClassName == null) {
  92. parserClassName = "org.apache.xerces.parsers.SAXParser";
  93. }
  94. MessageHandler.logln("using SAX parser " + parserClassName);
  95. try {
  96. return (XMLReader)
  97. Class.forName(parserClassName).newInstance();
  98. } catch (ClassNotFoundException e) {
  99. MessageHandler.errorln("Could not find " + parserClassName);
  100. } catch (InstantiationException e) {
  101. MessageHandler.errorln("Could not instantiate "
  102. + parserClassName);
  103. } catch (IllegalAccessException e) {
  104. MessageHandler.errorln("Could not access " + parserClassName);
  105. } catch (ClassCastException e) {
  106. MessageHandler.errorln(parserClassName + " is not a SAX driver");
  107. }
  108. return null;
  109. } // end: createParser
  110. /**
  111. * create an InputSource from a file name
  112. *
  113. * @param filename the name of the file
  114. * @return the InputSource created
  115. */
  116. protected static InputSource fileInputSource(String filename) {
  117. /* this code adapted from James Clark's in XT */
  118. File file = new File(filename);
  119. String path = file.getAbsolutePath();
  120. String fSep = System.getProperty("file.separator");
  121. if (fSep != null && fSep.length() == 1)
  122. path = path.replace(fSep.charAt(0), '/');
  123. if (path.length() > 0 && path.charAt(0) != '/')
  124. path = '/' + path;
  125. try {
  126. return new InputSource(new URL("file", null,
  127. path).toString());
  128. }
  129. catch (java.net.MalformedURLException e) {
  130. throw new Error("unexpected MalformedURLException");
  131. }
  132. } // end: fileInputSource
  133. /**
  134. * main method, starts execution of this task
  135. *
  136. */
  137. public void execute () throws BuildException {
  138. boolean errors = false;
  139. String version = Version.getVersion();
  140. MessageHandler.logln("=======================\nTask " + version +
  141. "\nconverting file " + fofile + " to " + pdffile);
  142. if (!(new File(fofile).exists())) {
  143. errors = true;
  144. MessageHandler.errorln("Task Fop - ERROR: Formatting objects file " + fofile + " missing.");
  145. }
  146. XMLReader parser = createParser();
  147. if (parser == null) {
  148. MessageHandler.errorln("Task Fop - ERROR: Unable to create SAX parser");
  149. errors = true;
  150. }
  151. // setting the parser features
  152. try {
  153. parser.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
  154. } catch (SAXException e) {
  155. MessageHandler.errorln("Error in setting up parser feature namespace-prefixes");
  156. MessageHandler.errorln("You need a parser which supports SAX version 2");
  157. System.exit(1);
  158. }
  159. if (!errors) {
  160. try {
  161. Driver driver = new Driver();
  162. driver.setRenderer("org.apache.fop.render.pdf.PDFRenderer", version);
  163. driver.addElementMapping("org.apache.fop.fo.StandardElementMapping");
  164. driver.addElementMapping("org.apache.fop.svg.SVGElementMapping");
  165. driver.addPropertyList("org.apache.fop.fo.StandardPropertyListMapping");
  166. driver.addPropertyList("org.apache.fop.svg.SVGPropertyListMapping");
  167. driver.setWriter(new PrintWriter(new FileWriter(pdffile)));
  168. driver.buildFOTree(parser, fileInputSource(fofile));
  169. driver.format();
  170. driver.render();
  171. } catch (Exception e) {
  172. MessageHandler.errorln("Task Fop - FATAL ERROR: " + e.getMessage());
  173. System.exit(1);
  174. }
  175. }
  176. MessageHandler.logln("=======================\n");
  177. } // end: execute
  178. }