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

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