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

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