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.

CommandLine.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*-- $Id$ --
  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. // FOP
  50. import org.apache.fop.messaging.MessageHandler;
  51. import org.apache.fop.configuration.Configuration;
  52. /**
  53. * mainline class.
  54. *
  55. * Gets input and output filenames from the command line.
  56. * Creates a SAX Parser (defaulting to Xerces).
  57. *
  58. */
  59. public class CommandLine {
  60. private String foFile = null;
  61. private String pdfFile = null;
  62. private String userConfigFile = null;
  63. private String baseDir = null;
  64. private boolean dumpConfiguration = false;
  65. /** show a full dump on error */
  66. private static boolean errorDump = false;
  67. public CommandLine(String[] args) {
  68. for (int i = 0; i < args.length; i++) {
  69. if (args[i].equals("-d") || args[i].equals("--full-error-dump")) {
  70. errorDump = true;
  71. } else if (args[i].equals("-x")) {
  72. dumpConfiguration = true;
  73. } else if ((args[i].charAt(0) == '-') &&
  74. (args[i].charAt(1) == 'c')) {
  75. userConfigFile = args[i].substring(2);
  76. } else if (args[i].charAt(0) == '-') {
  77. printUsage(args[i]);
  78. } else if (foFile == null) {
  79. foFile = args[i];
  80. } else if (pdfFile == null) {
  81. pdfFile = args[i];
  82. } else {
  83. printUsage(args[i]);
  84. }
  85. }
  86. if (foFile == null || pdfFile == null) {
  87. printUsage(null);
  88. }
  89. }
  90. public void printUsage(String arg) {
  91. if (arg != null) {
  92. MessageHandler.errorln("Unknown argument: '"+arg + "'");
  93. }
  94. MessageHandler.errorln("Usage: java [-d] [-x]" +
  95. "[-cMyConfigFile] \n" +
  96. " org.apache.fop.apps.CommandLine " + "formatting-object-file pdf-file");
  97. MessageHandler.errorln("Options:\n" + " -d or --full-error-dump Show stack traces upon error");
  98. MessageHandler.errorln(" -x dump configuration information");
  99. MessageHandler.errorln(" -cMyConfigFile use configuration file MyConfigFile");
  100. System.exit(1);
  101. }
  102. public void run() {
  103. Driver driver = new Driver();
  104. if (errorDump) {
  105. driver.setErrorDump(true);
  106. }
  107. if (userConfigFile != null) {
  108. driver.loadUserconfiguration(userConfigFile);
  109. }
  110. driver.setBaseDir(foFile);
  111. if (dumpConfiguration) {
  112. Configuration.dumpConfiguration();
  113. System.exit(0);
  114. }
  115. String version = Version.getVersion();
  116. MessageHandler.logln(version);
  117. XMLReader parser = createParser();
  118. if (parser == null) {
  119. MessageHandler.errorln("ERROR: Unable to create SAX parser");
  120. System.exit(1);
  121. }
  122. // setting the parser features
  123. try {
  124. parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
  125. true);
  126. } catch (SAXException e) {
  127. MessageHandler.errorln("Error in setting up parser feature namespace-prefixes");
  128. MessageHandler.errorln("You need a parser which supports SAX version 2");
  129. if (errorDump) {
  130. e.printStackTrace();
  131. }
  132. System.exit(1);
  133. }
  134. try {
  135. driver.setRenderer("org.apache.fop.render.pdf.PDFRenderer",
  136. Version.getVersion());
  137. driver.addElementMapping("org.apache.fop.fo.StandardElementMapping");
  138. driver.addElementMapping("org.apache.fop.svg.SVGElementMapping");
  139. driver.addElementMapping("org.apache.fop.extensions.ExtensionElementMapping");
  140. driver.addPropertyList("org.apache.fop.fo.StandardPropertyListMapping");
  141. driver.addPropertyList("org.apache.fop.svg.SVGPropertyListMapping");
  142. driver.addPropertyList("org.apache.fop.extensions.ExtensionPropertyListMapping");
  143. driver.buildFOTree(parser, fileInputSource(foFile));
  144. driver.format();
  145. driver.setOutputStream(new FileOutputStream(pdfFile));
  146. driver.render();
  147. } catch (Exception e) {
  148. MessageHandler.errorln("FATAL ERROR: " + e.getMessage());
  149. if (errorDump) {
  150. e.printStackTrace();
  151. }
  152. System.exit(1);
  153. }
  154. }
  155. /**
  156. * creates a SAX parser, using the value of org.xml.sax.parser
  157. * defaulting to org.apache.xerces.parsers.SAXParser
  158. *
  159. * @return the created SAX parser
  160. */
  161. static XMLReader createParser() {
  162. String parserClassName = System.getProperty("org.xml.sax.parser");
  163. if (parserClassName == null) {
  164. parserClassName = "org.apache.xerces.parsers.SAXParser";
  165. }
  166. org.apache.fop.messaging.MessageHandler.logln(
  167. "using SAX parser " + parserClassName);
  168. try {
  169. return (XMLReader) Class.forName(
  170. parserClassName).newInstance();
  171. } catch (ClassNotFoundException e) {
  172. org.apache.fop.messaging.MessageHandler.errorln(
  173. "Could not find " + parserClassName);
  174. if (errorDump) {
  175. e.printStackTrace();
  176. }
  177. }
  178. catch (InstantiationException e) {
  179. org.apache.fop.messaging.MessageHandler.errorln(
  180. "Could not instantiate " + parserClassName);
  181. if (errorDump) {
  182. e.printStackTrace();
  183. }
  184. }
  185. catch (IllegalAccessException e) {
  186. org.apache.fop.messaging.MessageHandler.errorln(
  187. "Could not access " + parserClassName);
  188. if (errorDump) {
  189. e.printStackTrace();
  190. }
  191. }
  192. catch (ClassCastException e) {
  193. org.apache.fop.messaging.MessageHandler.errorln(
  194. parserClassName + " is not a SAX driver");
  195. if (errorDump) {
  196. e.printStackTrace();
  197. }
  198. }
  199. return null;
  200. }
  201. /**
  202. * create an InputSource from a file name
  203. *
  204. * @param filename the name of the file
  205. * @return the InputSource created
  206. */
  207. public static InputSource fileInputSource(String filename) {
  208. /* this code adapted from James Clark's in XT */
  209. File file = new File(filename);
  210. String path = file.getAbsolutePath();
  211. String fSep = System.getProperty("file.separator");
  212. if (fSep != null && fSep.length() == 1)
  213. path = path.replace(fSep.charAt(0), '/');
  214. if (path.length() > 0 && path.charAt(0) != '/')
  215. path = '/' + path;
  216. try {
  217. return new InputSource(new URL("file", null, path).toString());
  218. } catch (java.net.MalformedURLException e) {
  219. throw new Error("unexpected MalformedURLException");
  220. }
  221. }
  222. /**
  223. * mainline method
  224. *
  225. * first command line argument is input file
  226. * second command line argument is output file
  227. *
  228. * @param command line arguments
  229. */
  230. public static void main(String[] args) {
  231. CommandLine cmdLine = new CommandLine(args);
  232. cmdLine.run();
  233. }
  234. }