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

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