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

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