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

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