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.

XalanCommandLine.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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.apps;
  41. // SAX
  42. import org.xml.sax.XMLReader;
  43. import org.xml.sax.InputSource;
  44. import org.xml.sax.SAXException;
  45. import org.xml.sax.SAXParseException;
  46. // Java
  47. import java.io.*;
  48. import java.net.URL;
  49. /*
  50. // Xalan
  51. import org.apache.xalan.xpath.xml.XMLParserLiaison;
  52. import org.apache.xalan.xslt.XSLTInputSource;
  53. import org.apache.xalan.xslt.XSLTProcessor;
  54. import org.apache.xalan.xslt.XSLTProcessorFactory;
  55. import org.apache.xalan.xslt.XSLTResultTarget;
  56. */
  57. // FOP
  58. import org.apache.fop.messaging.MessageHandler;
  59. import org.apache.fop.configuration.ConfigurationReader;
  60. import org.apache.fop.configuration.Configuration;
  61. import org.apache.fop.tools.xslt.XSLTransform;
  62. /**
  63. * mainline class.
  64. *
  65. * Gets input and output filenames from the command line.
  66. * Creates a SAX Parser (defaulting to Xerces).
  67. *
  68. */
  69. public class XalanCommandLine {
  70. private String foFile = null;
  71. private String pdfFile = null;
  72. private String xsltFile = null;
  73. private String userConfigFile = null;
  74. private String baseDir = null;
  75. private boolean dumpConfiguration = false;
  76. /** show a full dump on error */ //this should be implemented here too
  77. private static boolean errorDump = false;
  78. public XalanCommandLine(String[] args) {
  79. for (int i = 0; i < args.length; i++) {
  80. if (args[i].equals("-d") || args[i].equals("--full-error-dump")) {
  81. errorDump = true;
  82. } else if (args[i].equals("-x")) {
  83. dumpConfiguration = true;
  84. } else if ((args[i].charAt(0) == '-') &&
  85. (args[i].charAt(1) == 'c')) {
  86. userConfigFile = args[i].substring(2);
  87. } else if (args[i].charAt(0) == '-') {
  88. printUsage(args[i]);
  89. } else if (foFile == null) {
  90. foFile = args[i];
  91. } else if (xsltFile == null) {
  92. xsltFile = args[i];
  93. } else if (pdfFile == null) {
  94. pdfFile = args[i];
  95. } else {
  96. printUsage(args[i]);
  97. }
  98. }
  99. if (foFile == null || pdfFile == null || xsltFile == null) {
  100. printUsage(null);
  101. }
  102. }
  103. /*
  104. * shows usage info
  105. */
  106. public void printUsage(String arg) {
  107. if (arg != null) {
  108. MessageHandler.errorln("Unkown argument: '"+ arg + "'");
  109. }
  110. MessageHandler.errorln("Usage: java [-d] [-x]" +
  111. "[-cMyConfigFile] " +
  112. "org.apache.fop.apps.XalanCommandLine " + "xml-file xslt-file pdf-file");
  113. MessageHandler.errorln("Options:\n" + " -d or --full-error-dump Show stack traces upon error");
  114. MessageHandler.errorln(" -x dump configuration information");
  115. MessageHandler.errorln("-cMyConfigFile use values in configuration file MyConfigFile instead of/additional to default");
  116. System.exit(1);
  117. }
  118. /**
  119. * creates a SAX parser, using the value of org.xml.sax.parser
  120. * defaulting to org.apache.xerces.parsers.SAXParser
  121. *
  122. * @return the created SAX parser
  123. */
  124. static XMLReader createParser() {
  125. String parserClassName = System.getProperty("org.xml.sax.parser");
  126. if (parserClassName == null) {
  127. parserClassName = "org.apache.xerces.parsers.SAXParser";
  128. }
  129. org.apache.fop.messaging.MessageHandler.logln(
  130. "using SAX parser " + parserClassName);
  131. try {
  132. return (XMLReader) Class.forName(
  133. parserClassName).newInstance();
  134. } catch (ClassNotFoundException e) {
  135. org.apache.fop.messaging.MessageHandler.errorln(
  136. "Could not find " + parserClassName);
  137. }
  138. catch (InstantiationException e) {
  139. org.apache.fop.messaging.MessageHandler.errorln(
  140. "Could not instantiate " + parserClassName);
  141. }
  142. catch (IllegalAccessException e) {
  143. org.apache.fop.messaging.MessageHandler.errorln(
  144. "Could not access " + parserClassName);
  145. }
  146. catch (ClassCastException e) {
  147. org.apache.fop.messaging.MessageHandler.errorln(
  148. parserClassName + " is not a SAX driver");
  149. }
  150. return null;
  151. }
  152. /**
  153. * create an InputSource from a file name
  154. *
  155. * @param filename the name of the file
  156. * @return the InputSource created
  157. */
  158. protected static InputSource fileInputSource(String filename) {
  159. /* this code adapted from James Clark's in XT */
  160. File file = new File(filename);
  161. String path = file.getAbsolutePath();
  162. String fSep = System.getProperty("file.separator");
  163. if (fSep != null && fSep.length() == 1)
  164. path = path.replace(fSep.charAt(0), '/');
  165. if (path.length() > 0 && path.charAt(0) != '/')
  166. path = '/' + path;
  167. try {
  168. return new InputSource(new URL("file", null, path).toString());
  169. } catch (java.net.MalformedURLException e) {
  170. throw new Error("unexpected MalformedURLException");
  171. }
  172. }
  173. /**
  174. * mainline method
  175. *
  176. * first command line argument is xml input file
  177. * second command line argument is xslt file which commands the conversion from xml to xsl:fo
  178. * third command line argument is the output file
  179. *
  180. * @param command line arguments
  181. */
  182. public void run () {
  183. Driver driver = new Driver();
  184. if (errorDump) {
  185. driver.setErrorDump(true);
  186. }
  187. if (userConfigFile != null) {
  188. driver.loadUserconfiguration(userConfigFile);
  189. }
  190. if (dumpConfiguration) {
  191. Configuration.dumpConfiguration();
  192. System.exit(0);
  193. }
  194. driver.setBaseDir(foFile);
  195. String version = Version.getVersion();
  196. MessageHandler.logln(version);
  197. XMLReader parser = createParser();
  198. if (parser == null) {
  199. MessageHandler.errorln("ERROR: Unable to create SAX parser");
  200. System.exit(1);
  201. }
  202. // setting the parser features
  203. try {
  204. parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
  205. true);
  206. } catch (SAXException e) {
  207. MessageHandler.errorln("Error in setting up parser feature namespace-prefixes");
  208. MessageHandler.errorln("You need a parser which supports SAX version 2");
  209. System.exit(1);
  210. }
  211. try {
  212. java.io.Writer writer;
  213. java.io.Reader reader;
  214. boolean usefile = false;
  215. MessageHandler.logln("transforming to xsl:fo markup");
  216. // create a Writer
  217. // the following is an ugly hack to allow processing of larger files
  218. // if xml file size is larger than 700 kb write the fo:file to disk
  219. if ((new File(foFile).length()) > 500000) {
  220. writer = new FileWriter(pdfFile + ".tmp");
  221. usefile = true;
  222. } else {
  223. writer = new StringWriter();
  224. }
  225. // Use XSLTProcessorFactory to instantiate an XSLTProcessor.
  226. // XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
  227. // Create the 3 objects the XSLTProcessor needs to perform the transformation.
  228. // Fix up the args...
  229. // XMLParserLiaison xmlPL = processor.getXMLProcessorLiaison();
  230. // URL urlTmp = xmlPL.getURLFromString(foFile, null);
  231. // MessageHandler.errorln("xml: " + urlTmp);
  232. /* XSLTInputSource xmlSource =
  233. new XSLTInputSource (urlTmp.toString());
  234. */
  235. // urlTmp = xmlPL.getURLFromString(xsltFile, null);
  236. // MessageHandler.errorln("xslt: " + urlTmp);
  237. /* XSLTInputSource xslSheet =
  238. new XSLTInputSource (urlTmp.toString());
  239. XSLTResultTarget xmlResult = new XSLTResultTarget (writer);
  240. */
  241. // Perform the transformation.
  242. // processor.process(xmlSource, xslSheet, xmlResult);
  243. XSLTransform.transform(foFile, xsltFile, writer);
  244. if (usefile) {
  245. reader = new FileReader(pdfFile + ".tmp");
  246. } else {
  247. // create a input source containing the xsl:fo file which can be fed to Fop
  248. reader = new StringReader(writer.toString());
  249. }
  250. writer.flush();
  251. writer.close();
  252. //set Driver methods to start Fop processing
  253. driver.setRenderer("org.apache.fop.render.pdf.PDFRenderer",
  254. version);
  255. driver.addElementMapping("org.apache.fop.fo.StandardElementMapping");
  256. driver.addElementMapping("org.apache.fop.svg.SVGElementMapping");
  257. driver.addElementMapping("org.apache.fop.extensions.ExtensionElementMapping");
  258. driver.addPropertyList("org.apache.fop.fo.StandardPropertyListMapping");
  259. driver.addPropertyList("org.apache.fop.svg.SVGPropertyListMapping");
  260. driver.addPropertyList("org.apache.fop.extensions.ExtensionPropertyListMapping");
  261. OutputStream stream = new BufferedOutputStream(new FileOutputStream(pdfFile));
  262. driver.setOutputStream(stream);
  263. driver.buildFOTree(parser, new InputSource(reader));
  264. reader.close();
  265. driver.format();
  266. driver.render();
  267. if (usefile) {
  268. new File (pdfFile + ".tmp").delete();
  269. }
  270. stream.flush();
  271. stream.close();
  272. }
  273. catch (Exception e) {
  274. MessageHandler.errorln("FATAL ERROR: " + e.getMessage());
  275. if (errorDump) {
  276. e.printStackTrace();
  277. }
  278. System.exit(1);
  279. }
  280. }
  281. /**
  282. * mainline method
  283. *
  284. * first command line argument is xml input file
  285. * second command line argument is xslt file
  286. * third command line argument is the target pdf file
  287. *
  288. * @param command line arguments
  289. */
  290. public static void main(String[] args) {
  291. XalanCommandLine xcmdLine = new XalanCommandLine(args);
  292. xcmdLine.run();
  293. }
  294. }