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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /* $Id$
  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: postscript file */
  36. private static final int PS_OUTPUT = 6;
  37. /* output: text file */
  38. private static final int TXT_OUTPUT = 7;
  39. /* System buffers */
  40. private static final int BUFFER_FILE = 8;
  41. /* System buffers */
  42. private static final int AREA_OUTPUT = 9;
  43. /* use debug mode*/
  44. Boolean errorDump = new Boolean(false);
  45. /* show configuration information */
  46. Boolean dumpConfiguration = new Boolean(false);
  47. /*suppress any progress information */
  48. Boolean quiet = new Boolean(false);
  49. /* for area tree XML output, only down to block area level */
  50. Boolean suppressLowLevelAreas = new Boolean(false);
  51. /* name of user configuration file*/
  52. File userConfigFile = null;
  53. /* name of input fo file */
  54. File fofile = null;
  55. /* name of xsltfile (xslt transformation as input)*/
  56. File xsltfile = null;
  57. /* name of xml file (xslt transformation as input)*/
  58. File xmlfile = null;
  59. /* name of output file */
  60. File outfile = null;
  61. /* name of buffer file */
  62. File bufferFile = null;
  63. /* input mode */
  64. int inputmode = NOT_SET;
  65. /*output mode */
  66. int outputmode = NOT_SET;
  67. /* buffer mode */
  68. int buffermode = NOT_SET;
  69. /* language for user information */
  70. String language = null;
  71. private java.util.Hashtable rendererOptions;
  72. public CommandLineOptions (String [] args) throws FOPException,
  73. FileNotFoundException {
  74. boolean optionsParsed = true;
  75. rendererOptions = new java.util.Hashtable();
  76. try {
  77. optionsParsed = parseOptions(args);
  78. if (optionsParsed) {
  79. checkSettings ();
  80. if (errorDump != null && errorDump.booleanValue()) {
  81. debug();
  82. }
  83. }
  84. } catch (FOPException e) {
  85. printUsage();
  86. throw e;
  87. }
  88. catch (java.io.FileNotFoundException e) {
  89. printUsage();
  90. throw e;
  91. }
  92. }
  93. /**
  94. * parses the commandline arguments
  95. * @return true if parse was successful and procesing can continue, false if processing should stop
  96. * @exception FOPException if there was an error in the format of the options
  97. */
  98. private boolean parseOptions (String args[]) throws FOPException {
  99. for (int i = 0; i < args.length; i++) {
  100. if (args[i].equals("-d") || args[i].equals("--full-error-dump")) {
  101. errorDump = new Boolean(true);
  102. } else if (args[i].equals("-x") || args[i].equals("--dump-config")) {
  103. dumpConfiguration = new Boolean(true);
  104. } else if (args[i].equals("-q") || args[i].equals("--quiet")) {
  105. quiet = new Boolean(true);
  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 = new 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].charAt(0) != '-') {
  208. if (inputmode == NOT_SET) {
  209. inputmode = FO_INPUT;
  210. fofile = new File (args[i]);
  211. } else if (outputmode == NOT_SET) {
  212. outputmode = PDF_OUTPUT;
  213. outfile = new File(args[i]);
  214. } else {
  215. throw new FOPException(
  216. "Don't know what to do with " + args[i]);
  217. }
  218. } else if (args[i].equals("-buf")) {
  219. if (buffermode == NOT_SET) {
  220. buffermode = BUFFER_FILE;
  221. } else {
  222. MessageHandler.errorln("ERROR: you can only set one buffer method");
  223. printUsage();
  224. }
  225. if ((i + 1 == args.length) ||
  226. (args[i + 1].charAt(0) == '-')) {
  227. MessageHandler.errorln("ERROR: you must specify the buffer output file");
  228. printUsage();
  229. } else {
  230. bufferFile = new File (args[i + 1]);
  231. i++;
  232. }
  233. } else if (args[i].equals("-at")) {
  234. setOutputMode(AREA_OUTPUT);
  235. if ((i + 1 == args.length) ||
  236. (args[i + 1].charAt(0) == '-')) {
  237. throw new FOPException("you must specify the area-tree output file");
  238. } else {
  239. outfile = new File (args[i + 1]);
  240. i++;
  241. }
  242. } else {
  243. printUsage();
  244. return false;
  245. }
  246. }
  247. return true;
  248. } //end parseOptions
  249. private void setOutputMode(int mode) throws FOPException {
  250. if (outputmode == NOT_SET) {
  251. outputmode = mode;
  252. } else {
  253. throw new FOPException("you can only set one output method");
  254. }
  255. }
  256. /**
  257. * checks whether all necessary information has been given in a consistent way
  258. */
  259. private void checkSettings () throws FOPException,
  260. FileNotFoundException {
  261. if (inputmode == NOT_SET) {
  262. throw new FOPException("No input file specified");
  263. }
  264. if (outputmode == NOT_SET) {
  265. throw new FOPException("No output file specified");
  266. }
  267. if (inputmode == XSLT_INPUT) {
  268. //check whether xml *and* xslt file have been set
  269. if (xmlfile == null) {
  270. throw new FOPException("XML file must be specified for the tranform mode");
  271. }
  272. if (xsltfile == null) {
  273. throw new FOPException("XSLT file must be specified for the tranform mode");
  274. }
  275. //warning if fofile has been set in xslt mode
  276. if (fofile != null) {
  277. MessageHandler.errorln(
  278. "WARNING: Can't use fo file with transform mode! Ignoring.\n" +
  279. "Your input is "+ "\n xmlfile: " +
  280. xmlfile.getAbsolutePath() + "\nxsltfile: " +
  281. xsltfile.getAbsolutePath() + "\n fofile: " +
  282. fofile.getAbsolutePath());
  283. }
  284. if (!xmlfile.exists()) {
  285. throw new FileNotFoundException("xml file " +
  286. xmlfile.getAbsolutePath() + " not found ");
  287. }
  288. if (!xsltfile.exists()) {
  289. throw new FileNotFoundException("xsl file " +
  290. xsltfile.getAbsolutePath() + " not found ");
  291. }
  292. } else if (inputmode == FO_INPUT) {
  293. if (xmlfile != null || xsltfile != null) {
  294. MessageHandler.errorln("WARNING: fo input mode, but xmlfile or xslt file are set:");
  295. MessageHandler.errorln("xml file: " + xmlfile.toString());
  296. MessageHandler.errorln("xslt file: " + xsltfile.toString());
  297. }
  298. if (!fofile.exists()) {
  299. throw new FileNotFoundException("fo file " +
  300. fofile.getAbsolutePath() + " 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 AREA_OUTPUT:
  326. rendererOptions.put("fineDetail", isCoarseAreaXml());
  327. return Driver.RENDER_XML;
  328. default:
  329. throw new FOPException("Invalid Renderer setting!");
  330. }
  331. }
  332. /**
  333. *
  334. **/
  335. public InputHandler getInputHandler () {
  336. switch (inputmode) {
  337. case FO_INPUT:
  338. return new FOInputHandler(fofile);
  339. case XSLT_INPUT:
  340. return new XSLTInputHandler(xmlfile, xsltfile);
  341. default:
  342. return new FOInputHandler(fofile);
  343. }
  344. }
  345. public java.util.Hashtable getRendererOptions() {
  346. return rendererOptions;
  347. }
  348. public Starter getStarter() throws FOPException {
  349. switch (outputmode) {
  350. case AWT_OUTPUT:
  351. try {
  352. return((Starter) Class.forName(
  353. "org.apache.fop.apps.AWTStarter").
  354. getConstructor(
  355. new Class[]{CommandLineOptions.class}).
  356. newInstance(new Object[]{this}));
  357. } catch (Exception e) {
  358. if (e instanceof FOPException) {
  359. throw (FOPException) e;
  360. }
  361. throw new FOPException("AWTStarter could not be loaded.",e);
  362. }
  363. case PRINT_OUTPUT:
  364. try {
  365. return((Starter) Class.forName(
  366. "org.apache.fop.apps.PrintStarter").
  367. getConstructor(
  368. new Class[]{CommandLineOptions.class}).
  369. newInstance(new Object[]{this}));
  370. } catch (Exception e) {
  371. if (e instanceof FOPException) {
  372. throw (FOPException) e;
  373. }
  374. throw new FOPException("PrintStarter could not be loaded.",e);
  375. }
  376. default:
  377. return new CommandLineStarter(this);
  378. }
  379. }
  380. public int getInputMode() {
  381. return inputmode;
  382. }
  383. public int getOutputMode() {
  384. return outputmode;
  385. }
  386. public File getFOFile() {
  387. return fofile;
  388. }
  389. public File getXMLFile() {
  390. return xmlfile;
  391. }
  392. public File getXSLFile() {
  393. return xsltfile;
  394. }
  395. public File getOutputFile() {
  396. return outfile;
  397. }
  398. public File getUserConfigFile () {
  399. return userConfigFile;
  400. }
  401. public String getLanguage() {
  402. return language;
  403. }
  404. public File getBufferFile() {
  405. return bufferFile;
  406. }
  407. public Boolean isQuiet() {
  408. return quiet;
  409. }
  410. public Boolean dumpConfiguration() {
  411. return dumpConfiguration;
  412. }
  413. public Boolean isDebugMode() {
  414. return errorDump;
  415. }
  416. public Boolean isCoarseAreaXml() {
  417. return suppressLowLevelAreas;
  418. }
  419. /**
  420. * return either the fofile or the xmlfile
  421. */
  422. public File getInputFile() {
  423. switch (inputmode) {
  424. case FO_INPUT:
  425. return fofile;
  426. case XSLT_INPUT:
  427. return xmlfile;
  428. default:
  429. return fofile;
  430. }
  431. }
  432. /**
  433. * shows the commandline syntax including a summary of all available options and some examples
  434. */
  435. public static void printUsage() {
  436. MessageHandler.errorln(
  437. "\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] [-awt|-pdf|-mif|-pcl|-ps|-txt|-at|-print] <outfile>\n" +
  438. " [OPTIONS] \n" + " -d debug mode \n" +
  439. " -x dump configuration settings \n" +
  440. " -q quiet mode \n" +
  441. " -c cfg.xml use additional configuration file cfg.xml\n" +
  442. " -l lang the language to use for user information \n" +
  443. " -s for area tree XML, down to block areas only\n\n" +
  444. " [INPUT] \n" +
  445. " infile xsl:fo input file (the same as the next) \n" +
  446. " -fo infile xsl:fo input file \n" +
  447. " -xml infile xml input file, must be used together with -xsl \n" +
  448. " -xsl stylesheet xslt stylesheet \n \n" +
  449. " [OUTPUT] \n" +
  450. " outfile input will be rendered as pdf file into outfile \n" +
  451. " -pdf outfile input will be rendered as pdf file (outfile req'd) \n" +
  452. " -awt input will be displayed on screen \n" +
  453. " -mif outfile input will be rendered as mif file (outfile req'd)\n" +
  454. " -pcl outfile input will be rendered as pcl file (outfile req'd) \n" +
  455. " -ps outfile input will be rendered as PostScript file (outfile req'd) \n" +
  456. " -txt outfile input will be rendered as text file (outfile req'd) \n" +
  457. " -at outfile representation of area tree as XML (outfile req'd) \n" +
  458. " -print input file will be rendered and sent to the printer \n" +
  459. " see options with \"-print help\" \n\n" +
  460. " [Examples]\n" + " Fop foo.fo foo.pdf \n" +
  461. " Fop -fo foo.fo -pdf foo.pdf (does the same as the previous line)\n" +
  462. " Fop -xsl foo.xsl -xml foo.xml -pdf foo.pdf\n" +
  463. " Fop foo.fo -mif foo.mif\n" +
  464. " Fop foo.fo -print or Fop -print foo.fo \n" + " Fop foo.fo -awt \n");
  465. }
  466. /**
  467. * shows the options for print output
  468. */
  469. public void printUsagePrintOutput() {
  470. MessageHandler.errorln(
  471. "USAGE: -print [-Dstart=i] [-Dend=i] [-Dcopies=i] [-Deven=true|false] " +
  472. " org.apache.fop.apps.Fop (..) -print \n" +
  473. "Example:\n" + "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. System.out.print("Input mode: ");
  480. switch (inputmode) {
  481. case NOT_SET:
  482. MessageHandler.logln("not set");
  483. break;
  484. case FO_INPUT:
  485. MessageHandler.logln("FO ");
  486. MessageHandler.logln("fo input file: " + fofile.toString());
  487. break;
  488. case XSLT_INPUT:
  489. MessageHandler.logln("xslt transformation");
  490. MessageHandler.logln("xml input file: " +
  491. xmlfile.toString());
  492. MessageHandler.logln("xslt stylesheet: " +
  493. xsltfile.toString());
  494. break;
  495. default:
  496. MessageHandler.logln("unknown input type");
  497. }
  498. System.out.print("Output mode: ");
  499. switch (outputmode) {
  500. case NOT_SET:
  501. MessageHandler.logln("not set");
  502. break;
  503. case PDF_OUTPUT:
  504. MessageHandler.logln("pdf");
  505. MessageHandler.logln("output file: " + outfile.toString());
  506. break;
  507. case AWT_OUTPUT:
  508. MessageHandler.logln("awt on screen");
  509. if (outfile != null) {
  510. MessageHandler.logln("ERROR: awt mode, but outfile is set:");
  511. MessageHandler.logln("out file: " + outfile.toString());
  512. }
  513. break;
  514. case MIF_OUTPUT:
  515. MessageHandler.logln("mif");
  516. MessageHandler.logln("output file: " + outfile.toString());
  517. break;
  518. case PRINT_OUTPUT:
  519. MessageHandler.logln("print directly");
  520. if (outfile != null) {
  521. MessageHandler.logln("ERROR: print mode, but outfile is set:");
  522. MessageHandler.logln("out file: " + outfile.toString());
  523. }
  524. break;
  525. case PCL_OUTPUT:
  526. MessageHandler.logln("pcl");
  527. MessageHandler.logln("output file: " + outfile.toString());
  528. break;
  529. case PS_OUTPUT:
  530. MessageHandler.logln("PostScript");
  531. MessageHandler.logln("output file: " + outfile.toString());
  532. break;
  533. case TXT_OUTPUT:
  534. MessageHandler.logln("txt");
  535. MessageHandler.logln("output file: " + outfile.toString());
  536. break;
  537. default:
  538. MessageHandler.logln("unknown input type");
  539. }
  540. MessageHandler.logln("OPTIONS");
  541. if (userConfigFile != null) {
  542. MessageHandler.logln("user configuration file: " +
  543. userConfigFile.toString());
  544. } else {
  545. MessageHandler.logln("no user configuration file is used [default]");
  546. }
  547. if (errorDump != null) {
  548. MessageHandler.logln("debug mode on");
  549. } else {
  550. MessageHandler.logln("debug mode off [default]");
  551. }
  552. if (dumpConfiguration != null) {
  553. MessageHandler.logln("dump configuration");
  554. } else {
  555. MessageHandler.logln("don't dump configuration [default]");
  556. }
  557. if (quiet != null) {
  558. MessageHandler.logln("quiet mode on");
  559. } else {
  560. MessageHandler.logln("quiet mode off [default]");
  561. }
  562. }
  563. //debug: create class and output all settings
  564. public static void main (String args[]) {
  565. /*
  566. for (int i = 0; i < args.length; i++) {
  567. MessageHandler.logln(">"+args[i]+"<");
  568. }*/
  569. try {
  570. CommandLineOptions options = new CommandLineOptions (args);
  571. } catch (Exception e) {
  572. e.printStackTrace();
  573. }
  574. //options.debug();
  575. }
  576. }