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

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