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.

CommandLineOptions.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  3. * For details on use and redistribution please refer to the
  4. * LICENSE file included with these sources."
  5. */
  6. package org.apache.fop.apps;
  7. //java
  8. import java.util.Vector;
  9. import java.io.File;
  10. // FOP
  11. import org.apache.fop.messaging.MessageHandler;
  12. import org.apache.fop.configuration.Configuration;
  13. import org.apache.fop.apps.FOPException;
  14. /**
  15. * Options parses the commandline arguments
  16. */
  17. public class CommandLineOptions {
  18. /* input / output not set */
  19. private static final int NOT_SET = 0;
  20. /* input: fo file */
  21. private static final int FO_INPUT = 1;
  22. /* input: xml+xsl file */
  23. private static final int XSLT_INPUT = 2;
  24. /* output: pdf file */
  25. private static final int PDF_OUTPUT = 1;
  26. /* output: screen using swing */
  27. private static final int AWT_OUTPUT = 2;
  28. /* output: mif file */
  29. private static final int MIF_OUTPUT = 3;
  30. /* output: sent swing rendered file to printer */
  31. private static final int PRINT_OUTPUT = 4;
  32. /* use debug mode*/
  33. Boolean errorDump = null;
  34. /* show configuration information */
  35. Boolean dumpConfiguration = null;
  36. /*suppress any progress information */
  37. Boolean quiet = null;
  38. /* name of user configuration file*/
  39. File userConfigFile = null;
  40. /* name of input fo file */
  41. File fofile = null;
  42. /* name of xsltfile (xslt transformation as input)*/
  43. File xsltfile = null;
  44. /* name of xml file (xslt transformation as input)*/
  45. File xmlfile = null;
  46. /* name of output file */
  47. File outfile = null;
  48. /* input mode */
  49. int inputmode = NOT_SET;
  50. /*output mode */
  51. int outputmode = NOT_SET;
  52. /* language for user information */
  53. String language = null;
  54. public CommandLineOptions (String [] args) {
  55. parseOptions(args);
  56. checkSettings ();
  57. if (errorDump != null && errorDump.booleanValue()) {
  58. debug();
  59. }
  60. }
  61. /**
  62. * parses the commandline arguments
  63. */
  64. private void parseOptions (String args[]) {
  65. for (int i = 0; i < args.length; i++) {
  66. if (args[i].equals("-d") || args[i].equals("--full-error-dump")) {
  67. errorDump = new Boolean(true);
  68. } else if (args[i].equals("-x") || args[i].equals("--dump-config")) {
  69. dumpConfiguration = new Boolean(true);
  70. } else if (args[i].equals("-q") || args[i].equals("--quiet")) {
  71. quiet = new Boolean(true);
  72. } else if (args[i].equals("-c")) {
  73. if ((i + 1 == args.length) ||
  74. (args[i + 1].charAt(0) == '-')) {
  75. MessageHandler.errorln("ERROR: if you use '-c', you must specify the name of the configuration file");
  76. printUsage();
  77. } else {
  78. userConfigFile = new File (args[i + 1]);
  79. i++;
  80. }
  81. } else if (args[i].equals("-l")) {
  82. if ((i + 1 == args.length) ||
  83. (args[i + 1].charAt(0) == '-')) {
  84. MessageHandler.errorln("ERROR: if you use '-l', you must specify a language");
  85. printUsage();
  86. } else {
  87. language = args[i + 1];
  88. i++;
  89. }
  90. } else if (args[i].equals("-fo")) {
  91. inputmode = FO_INPUT;
  92. if ((i + 1 == args.length) ||
  93. (args[i + 1].charAt(0) == '-')) {
  94. MessageHandler.errorln("ERROR: you must specify the fo file");
  95. printUsage();
  96. } else {
  97. fofile = new File (args[i + 1]);
  98. i++;
  99. }
  100. } else if (args[i].equals("-xsl")) {
  101. inputmode = XSLT_INPUT;
  102. if ((i + 1 == args.length) ||
  103. (args[i + 1].charAt(0) == '-')) {
  104. MessageHandler.errorln("ERROR: you must specify the stylesheet file");
  105. printUsage();
  106. } else {
  107. xsltfile = new File(args[i + 1]);
  108. i++;
  109. }
  110. } else if (args[i].equals("-xml")) {
  111. inputmode = XSLT_INPUT;
  112. if ((i + 1 == args.length) ||
  113. (args[i + 1].charAt(0) == '-')) {
  114. MessageHandler.errorln("ERROR: you must specify the input file");
  115. printUsage();
  116. } else {
  117. xmlfile = new File(args[i + 1]);
  118. i++;
  119. }
  120. } else if (args[i].equals("-awt")) {
  121. if (outputmode == NOT_SET) {
  122. outputmode = AWT_OUTPUT;
  123. } else {
  124. MessageHandler.errorln("ERROR: you can only set one output method");
  125. printUsage();
  126. }
  127. } else if (args[i].equals("-pdf")) {
  128. if (outputmode == NOT_SET) {
  129. outputmode = PDF_OUTPUT;
  130. } else {
  131. MessageHandler.errorln("ERROR: you can only set one output method");
  132. printUsage();
  133. }
  134. if ((i + 1 == args.length) ||
  135. (args[i + 1].charAt(0) == '-')) {
  136. MessageHandler.errorln("ERROR: you must specify the pdf output file");
  137. printUsage();
  138. } else {
  139. outfile = new File (args[i + 1]);
  140. i++;
  141. }
  142. } else if (args[i].equals("-mif")) {
  143. if (outputmode == NOT_SET) {
  144. outputmode = MIF_OUTPUT;
  145. } else {
  146. MessageHandler.errorln("ERROR: you can only set one output method");
  147. printUsage();
  148. }
  149. if ((i + 1 == args.length) ||
  150. (args[i + 1].charAt(0) == '-')) {
  151. MessageHandler.errorln("ERROR: you must specify the mif output file");
  152. printUsage();
  153. } else {
  154. outfile = new File(args[i + 1]);
  155. i++;
  156. }
  157. } else if (args[i].equals("-print")) {
  158. if (outputmode == NOT_SET) {
  159. outputmode = PRINT_OUTPUT;
  160. } else {
  161. MessageHandler.errorln("ERROR: you can only set one output method");
  162. printUsage();
  163. }
  164. //show print help
  165. if (i + 1 < args.length) {
  166. if (args[i + 1].equals("help")) {
  167. printUsagePrintOutput();
  168. }
  169. }
  170. }
  171. else if (args[i].charAt(0) != '-') {
  172. if (inputmode == NOT_SET) {
  173. inputmode = FO_INPUT;
  174. fofile = new File (args[i]);
  175. } else if (outputmode == NOT_SET) {
  176. outputmode = PDF_OUTPUT;
  177. outfile = new File(args[i]);
  178. } else {
  179. MessageHandler.errorln(
  180. "ERROR: Don't know what to do with " + args[i]);
  181. printUsage();
  182. }
  183. } else {
  184. printUsage();
  185. }
  186. }
  187. } //end parseOptions
  188. /**
  189. * checks whether all necessary information has been given in a consistent way
  190. */
  191. private void checkSettings () {
  192. if (inputmode == NOT_SET) {
  193. MessageHandler.errorln("ERROR: you have to specify an input file");
  194. printUsage();
  195. }
  196. if (outputmode == NOT_SET) {
  197. MessageHandler.errorln("ERROR: you have to specify an output mode");
  198. printUsage();
  199. }
  200. if (inputmode == XSLT_INPUT) {
  201. //check whether xml *and* xslt file have been set
  202. if (xmlfile == null || xsltfile == null) {
  203. MessageHandler.errorln(
  204. "ERROR: if you want to use an xml file transformed with an xslt file as input\n" +
  205. " you must specify both files. \n" +
  206. " Your input is \nxmlfile: " +
  207. xmlfile.getAbsolutePath() + "\nxsltfile: " +
  208. xsltfile.getAbsolutePath());
  209. printUsage();
  210. }
  211. //warning if fofile has been set in xslt mode
  212. if (fofile != null) {
  213. MessageHandler.errorln(
  214. "ERROR: Can't use fo file in transformation!" +
  215. " Your input is \nxmlfile: " +
  216. xmlfile.getAbsolutePath() + "\nxsltfile: " +
  217. xsltfile.getAbsolutePath() + "\nfofile: " +
  218. fofile.getAbsolutePath());
  219. }
  220. if (!xmlfile.exists()) {
  221. MessageHandler.errorln("ERROR: xml file " +
  222. xmlfile.getAbsolutePath() + " not found ");
  223. System.exit(1);
  224. }
  225. if (!xsltfile.exists()) {
  226. MessageHandler.errorln("ERROR: xsl file " +
  227. xsltfile.getAbsolutePath() + " not found ");
  228. System.exit(1);
  229. }
  230. } else if (inputmode == FO_INPUT) {
  231. if (xmlfile != null || xsltfile != null) {
  232. MessageHandler.logln("ERROR: fo input mode, but xmlfile or xslt file are set:");
  233. MessageHandler.logln("xml file: " + xmlfile.toString());
  234. MessageHandler.logln("xslt file: " + xsltfile.toString());
  235. }
  236. if (!fofile.exists()) {
  237. MessageHandler.errorln("ERROR: fo file " +
  238. fofile.getAbsolutePath() + " not found ");
  239. System.exit(1);
  240. }
  241. }
  242. }// end checkSettings
  243. /**
  244. * returns the chosen renderer, throws FOPException
  245. */
  246. public String getRenderer () throws FOPException {
  247. switch (outputmode) {
  248. case NOT_SET:
  249. throw new FOPException("Renderer has not been set!");
  250. case PDF_OUTPUT:
  251. return "org.apache.fop.render.pdf.PDFRenderer";
  252. case AWT_OUTPUT:
  253. return "org.apache.fop.render.awt.AWTRenderer";
  254. case MIF_OUTPUT:
  255. return "org.apache.fop.render.mif.MIFRenderer";
  256. case PRINT_OUTPUT:
  257. return "org.apache.fop.render.awt.PrintRenderer";
  258. default:
  259. throw new FOPException("Invalid Renderer setting!");
  260. }
  261. }
  262. /**
  263. *
  264. **/
  265. public InputHandler getInputHandler () {
  266. switch (inputmode) {
  267. case FO_INPUT:
  268. return new FOInputHandler(fofile);
  269. case XSLT_INPUT:
  270. return new XSLTInputHandler(xmlfile,xsltfile);
  271. default:
  272. return new FOInputHandler(fofile);
  273. }
  274. }
  275. public Starter getStarter() {
  276. switch (outputmode) {
  277. case PDF_OUTPUT:
  278. return new CommandLineStarter(this);
  279. case AWT_OUTPUT:
  280. return new AWTStarter(this);
  281. case MIF_OUTPUT:
  282. return new CommandLineStarter(this);
  283. case PRINT_OUTPUT:
  284. return new PrintStarter(this);
  285. default:
  286. return new CommandLineStarter(this);
  287. }
  288. }
  289. public int getInputMode() {
  290. return inputmode;
  291. }
  292. public int getOutputMode() {
  293. return outputmode;
  294. }
  295. public File getFOFile() {
  296. return fofile;
  297. }
  298. public File getXMLFile() {
  299. return xmlfile;
  300. }
  301. public File getXSLFile() {
  302. return xsltfile;
  303. }
  304. public File getOutputFile() {
  305. return outfile;
  306. }
  307. public File getUserConfigFile () {
  308. return userConfigFile;
  309. }
  310. public String getLanguage() {
  311. return language;
  312. }
  313. public Boolean isQuiet() {
  314. return quiet;
  315. }
  316. public Boolean dumpConfiguration() {
  317. return dumpConfiguration;
  318. }
  319. public Boolean isDebugMode() {
  320. return errorDump;
  321. }
  322. /**
  323. * return either the fofile or the xmlfile
  324. */
  325. public File getInputFile() {
  326. switch (inputmode) {
  327. case FO_INPUT:
  328. return fofile;
  329. case XSLT_INPUT:
  330. return xmlfile;
  331. default:
  332. return fofile;
  333. }
  334. }
  335. /**
  336. * shows the commandline syntax including a summary of all available options and some examples
  337. */
  338. public static void printUsage() {
  339. MessageHandler.logln(
  340. "\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] [-awt|-pdf|-mif|-print] <outfile>\n" +
  341. " [OPTIONS] \n" + " -d debug mode \n" +
  342. " -x dump configuration settings \n" +
  343. " -q quiet mode \n" +
  344. " -c cfg.xml use additional configuration file cfg.xml\n" +
  345. " -l lang the language to use for user information \n\n" +
  346. " [INPUT] \n" +
  347. " infile xsl:fo input file (the same as the next) \n" +
  348. " -fo infile xsl:fo input file \n" +
  349. " -xml infile xml input file, must be used together with -xsl \n" +
  350. " -xsl stylesheet xslt stylesheet \n \n" +
  351. " [OUTPUT] \n" +
  352. " outfile input will be rendered as pdf file into outfile \n" +
  353. " -pdf outfile input will be rendered as pdf file (outfile req'd) \n" +
  354. " -awt input will be displayed on screen \n" +
  355. " -mif outfile input will be rendered as mif file (outfile req'd)\n" +
  356. " -print input file will be rendered and sent to the printer \n" +
  357. " see options with \"-print help\" \n\n" +
  358. " [Examples]\n" + " Fop foo.fo foo.pdf \n" +
  359. " Fop -fo foo.fo -pdf foo.pdf (does the same as the previous line)\n" +
  360. " Fop -xsl foo.xsl -xml foo.xml -pdf foo.pdf\n" +
  361. " Fop foo.fo -mif foo.mif\n" +
  362. " Fop foo.fo -print or Fop -print foo.fo \n" + " Fop foo.fo -awt ");
  363. System.exit(1);
  364. }
  365. /**
  366. * shows the options for print output
  367. */
  368. public void printUsagePrintOutput() {
  369. MessageHandler.errorln(
  370. "USAGE: -print [-Dstart=i] [-Dend=i] [-Dcopies=i] [-Deven=true|false] " +
  371. " org.apache.fop.apps.Fop (..) -print \n" +
  372. "Example:\n" + "java -Dstart=1 -Dend=2 org.apache.Fop.apps.Fop infile.fo -print ");
  373. System.exit(1);
  374. }
  375. /**
  376. * debug mode. outputs all commandline settings
  377. */
  378. private void debug () {
  379. System.out.print("Input mode: ");
  380. switch (inputmode) {
  381. case NOT_SET:
  382. MessageHandler.logln("not set");
  383. break;
  384. case FO_INPUT:
  385. MessageHandler.logln("FO ");
  386. MessageHandler.logln("fo input file: " + fofile.toString());
  387. break;
  388. case XSLT_INPUT:
  389. MessageHandler.logln("xslt transformation");
  390. MessageHandler.logln("xml input file: " +
  391. xmlfile.toString());
  392. MessageHandler.logln("xslt stylesheet: " +
  393. xsltfile.toString());
  394. break;
  395. default:
  396. MessageHandler.logln("unknown input type");
  397. }
  398. System.out.print("Output mode: ");
  399. switch (outputmode) {
  400. case NOT_SET:
  401. MessageHandler.logln("not set");
  402. break;
  403. case PDF_OUTPUT:
  404. MessageHandler.logln("pdf");
  405. MessageHandler.logln("output file: " + outfile.toString());
  406. break;
  407. case AWT_OUTPUT:
  408. MessageHandler.logln("awt on screen");
  409. if (outfile != null) {
  410. MessageHandler.logln("ERROR: awt mode, but outfile is set:");
  411. MessageHandler.logln("out file: " + outfile.toString());
  412. }
  413. break;
  414. case MIF_OUTPUT:
  415. MessageHandler.logln("mif");
  416. MessageHandler.logln("output file: " + outfile.toString());
  417. break;
  418. case PRINT_OUTPUT:
  419. MessageHandler.logln("print directly");
  420. if (outfile != null) {
  421. MessageHandler.logln("ERROR: print mode, but outfile is set:");
  422. MessageHandler.logln("out file: " + outfile.toString());
  423. }
  424. break;
  425. default:
  426. MessageHandler.logln("unknown input type");
  427. }
  428. MessageHandler.logln("OPTIONS");
  429. if (userConfigFile != null) {
  430. MessageHandler.logln("user configuration file: " +
  431. userConfigFile.toString());
  432. } else {
  433. MessageHandler.logln("no user configuration file is used [default]");
  434. }
  435. if (errorDump != null) {
  436. MessageHandler.logln("debug mode on");
  437. } else {
  438. MessageHandler.logln("debug mode off [default]");
  439. }
  440. if (dumpConfiguration != null) {
  441. MessageHandler.logln("dump configuration");
  442. } else {
  443. MessageHandler.logln("don't dump configuration [default]");
  444. }
  445. if (quiet != null) {
  446. MessageHandler.logln("quiet mode on");
  447. } else {
  448. MessageHandler.logln("quiet mode off [default]");
  449. }
  450. }
  451. //debug: create class and output all settings
  452. public static void main (String args[]) {
  453. /*
  454. for (int i = 0; i < args.length; i++) {
  455. MessageHandler.logln(">"+args[i]+"<");
  456. }*/
  457. CommandLineOptions options = new CommandLineOptions (args);
  458. //options.debug();
  459. }
  460. }