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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  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: text file */
  36. private static final int TXT_OUTPUT = 6;
  37. /* System buffers */
  38. private static final int BUFFER_FILE = 7;
  39. /* System buffers */
  40. private static final int AREA_OUTPUT = 8;
  41. /* use debug mode*/
  42. Boolean errorDump = new Boolean(false);
  43. /* show configuration information */
  44. Boolean dumpConfiguration = new Boolean(false);
  45. /*suppress any progress information */
  46. Boolean quiet = new Boolean(false);
  47. /* for area tree XML output, only down to block area level */
  48. Boolean suppressLowLevelAreas = new Boolean(false);
  49. /* name of user configuration file*/
  50. File userConfigFile = null;
  51. /* name of input fo file */
  52. File fofile = null;
  53. /* name of xsltfile (xslt transformation as input)*/
  54. File xsltfile = null;
  55. /* name of xml file (xslt transformation as input)*/
  56. File xmlfile = null;
  57. /* name of output file */
  58. File outfile = null;
  59. /* name of buffer file */
  60. File bufferFile = null;
  61. /* input mode */
  62. int inputmode = NOT_SET;
  63. /*output mode */
  64. int outputmode = NOT_SET;
  65. /* buffer mode */
  66. int buffermode = NOT_SET;
  67. /* language for user information */
  68. String language = null;
  69. private java.util.Hashtable rendererOptions;
  70. public CommandLineOptions (String [] args)
  71. throws FOPException, FileNotFoundException
  72. {
  73. boolean optionsParsed = true;
  74. rendererOptions = new java.util.Hashtable();
  75. try {
  76. optionsParsed = parseOptions(args);
  77. if (optionsParsed) {
  78. checkSettings ();
  79. if (errorDump != null && errorDump.booleanValue()) {
  80. debug();
  81. }
  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[])
  99. throws FOPException
  100. {
  101. for (int i = 0; i < args.length; i++) {
  102. if (args[i].equals("-d") || args[i].equals("--full-error-dump")) {
  103. errorDump = new Boolean(true);
  104. } else if (args[i].equals("-x") || args[i].equals("--dump-config")) {
  105. dumpConfiguration = new Boolean(true);
  106. } else if (args[i].equals("-q") || args[i].equals("--quiet")) {
  107. quiet = new Boolean(true);
  108. } else if (args[i].equals("-c")) {
  109. if ((i + 1 == args.length) ||
  110. (args[i + 1].charAt(0) == '-')) {
  111. throw new FOPException("if you use '-c', you must specify the name of the configuration file");
  112. } else {
  113. userConfigFile = new File (args[i + 1]);
  114. i++;
  115. }
  116. } else if (args[i].equals("-l")) {
  117. if ((i + 1 == args.length) ||
  118. (args[i + 1].charAt(0) == '-')) {
  119. throw new FOPException("if you use '-l', you must specify a language");
  120. } else {
  121. language = args[i + 1];
  122. i++;
  123. }
  124. } else if (args[i].equals("-s")) {
  125. suppressLowLevelAreas = new Boolean(true);
  126. } else if (args[i].equals("-fo")) {
  127. inputmode = FO_INPUT;
  128. if ((i + 1 == args.length) ||
  129. (args[i + 1].charAt(0) == '-')) {
  130. throw new FOPException("you must specify the fo file for the '-fo' option");
  131. } else {
  132. fofile = new File (args[i + 1]);
  133. i++;
  134. }
  135. } else if (args[i].equals("-xsl")) {
  136. inputmode = XSLT_INPUT;
  137. if ((i + 1 == args.length) ||
  138. (args[i + 1].charAt(0) == '-')) {
  139. throw new FOPException("you must specify the stylesheet file for the '-xsl' option");
  140. } else {
  141. xsltfile = new File(args[i + 1]);
  142. i++;
  143. }
  144. } else if (args[i].equals("-xml")) {
  145. inputmode = XSLT_INPUT;
  146. if ((i + 1 == args.length) ||
  147. (args[i + 1].charAt(0) == '-')) {
  148. throw new FOPException("you must specify the input file for the '-xml' option");
  149. } else {
  150. xmlfile = new File(args[i + 1]);
  151. i++;
  152. }
  153. } else if (args[i].equals("-awt")) {
  154. setOutputMode(AWT_OUTPUT);
  155. } else if (args[i].equals("-pdf")) {
  156. setOutputMode(PDF_OUTPUT);
  157. if ((i + 1 == args.length) ||
  158. (args[i + 1].charAt(0) == '-')) {
  159. throw new FOPException("you must specify the pdf output file");
  160. } else {
  161. outfile = new File (args[i + 1]);
  162. i++;
  163. }
  164. } else if (args[i].equals("-mif")) {
  165. setOutputMode(MIF_OUTPUT);
  166. if ((i + 1 == args.length) ||
  167. (args[i + 1].charAt(0) == '-')) {
  168. throw new FOPException("you must specify the mif output file");
  169. } else {
  170. outfile = new File(args[i + 1]);
  171. i++;
  172. }
  173. } else if (args[i].equals("-print")) {
  174. setOutputMode(PRINT_OUTPUT);
  175. //show print help
  176. if (i + 1 < args.length) {
  177. if (args[i + 1].equals("help")) {
  178. printUsagePrintOutput();
  179. return false;
  180. }
  181. }
  182. } else if (args[i].equals("-pcl")) {
  183. setOutputMode(PCL_OUTPUT);
  184. if ((i + 1 == args.length) ||
  185. (args[i + 1].charAt(0) == '-')) {
  186. throw new FOPException("you must specify the pdf output file");
  187. } else {
  188. outfile = new File (args[i + 1]);
  189. i++;
  190. }
  191. } else if (args[i].equals("-txt")) {
  192. setOutputMode(TXT_OUTPUT);
  193. if ((i + 1 == args.length) ||
  194. (args[i + 1].charAt(0) == '-')) {
  195. throw new FOPException("you must specify the text output file");
  196. } else {
  197. outfile = new File (args[i + 1]);
  198. i++;
  199. }
  200. } else if (args[i].charAt(0) != '-') {
  201. if (inputmode == NOT_SET) {
  202. inputmode = FO_INPUT;
  203. fofile = new File (args[i]);
  204. } else if (outputmode == NOT_SET) {
  205. outputmode = PDF_OUTPUT;
  206. outfile = new File(args[i]);
  207. } else {
  208. throw new FOPException("Don't know what to do with " + args[i]);
  209. }
  210. } else if (args[i].equals("-buf")) {
  211. if (buffermode == NOT_SET) {
  212. buffermode = BUFFER_FILE;
  213. } else {
  214. MessageHandler.errorln("ERROR: you can only set one buffer method");
  215. printUsage();
  216. }
  217. if ((i + 1 == args.length) ||
  218. (args[i + 1].charAt(0) == '-')) {
  219. MessageHandler.errorln("ERROR: you must specify the buffer output file");
  220. printUsage();
  221. } else {
  222. bufferFile = new File (args[i + 1]);
  223. i++;
  224. }
  225. } else if (args[i].equals("-at")) {
  226. setOutputMode(AREA_OUTPUT);
  227. if ((i + 1 == args.length) ||
  228. (args[i + 1].charAt(0) == '-')) {
  229. throw new FOPException("you must specify the area-tree output file");
  230. } else {
  231. outfile = new File (args[i + 1]);
  232. i++;
  233. }
  234. } else {
  235. printUsage();
  236. return false;
  237. }
  238. }
  239. return true;
  240. } //end parseOptions
  241. private void setOutputMode(int mode)
  242. throws FOPException
  243. {
  244. if (outputmode == NOT_SET) {
  245. outputmode = mode;
  246. } else {
  247. throw new FOPException("you can only set one output method");
  248. }
  249. }
  250. /**
  251. * checks whether all necessary information has been given in a consistent way
  252. */
  253. private void checkSettings ()
  254. throws FOPException, FileNotFoundException
  255. {
  256. if (inputmode == NOT_SET) {
  257. throw new FOPException("No input file specified");
  258. }
  259. if (outputmode == NOT_SET) {
  260. throw new FOPException("No output file specified");
  261. }
  262. if (inputmode == XSLT_INPUT) {
  263. //check whether xml *and* xslt file have been set
  264. if (xmlfile == null) {
  265. throw new FOPException("XML file must be specified for the tranform mode");
  266. }
  267. if (xsltfile == null) {
  268. throw new FOPException("XSLT file must be specified for the tranform mode");
  269. }
  270. //warning if fofile has been set in xslt mode
  271. if (fofile != null) {
  272. MessageHandler.errorln(
  273. "WARNING: Can't use fo file with transform mode! Ignoring.\n" +
  274. "Your input is "+
  275. "\n xmlfile: " + xmlfile.getAbsolutePath() +
  276. "\nxsltfile: " + xsltfile.getAbsolutePath() +
  277. "\n fofile: " + fofile.getAbsolutePath());
  278. }
  279. if (!xmlfile.exists()) {
  280. throw new FileNotFoundException("xml file " +
  281. xmlfile.getAbsolutePath() + " not found ");
  282. }
  283. if (!xsltfile.exists()) {
  284. throw new FileNotFoundException("xsl file " +
  285. xsltfile.getAbsolutePath() + " not found ");
  286. }
  287. } else if (inputmode == FO_INPUT) {
  288. if (xmlfile != null || xsltfile != null) {
  289. MessageHandler.errorln("WARNING: fo input mode, but xmlfile or xslt file are set:");
  290. MessageHandler.errorln("xml file: " + xmlfile.toString());
  291. MessageHandler.errorln("xslt file: " + xsltfile.toString());
  292. }
  293. if (!fofile.exists()) {
  294. throw new FileNotFoundException("fo file " +
  295. fofile.getAbsolutePath() + " not found ");
  296. }
  297. }
  298. }// end checkSettings
  299. /**
  300. * returns the chosen renderer, throws FOPException
  301. */
  302. public int getRenderer () throws FOPException {
  303. switch (outputmode) {
  304. case NOT_SET:
  305. throw new FOPException("Renderer has not been set!");
  306. case PDF_OUTPUT:
  307. return Driver.RENDER_PDF;
  308. case AWT_OUTPUT:
  309. return Driver.RENDER_AWT;
  310. case MIF_OUTPUT:
  311. return Driver.RENDER_MIF;
  312. case PRINT_OUTPUT:
  313. return Driver.RENDER_PRINT;
  314. case PCL_OUTPUT:
  315. return Driver.RENDER_PCL;
  316. case TXT_OUTPUT:
  317. return Driver.RENDER_TXT;
  318. case AREA_OUTPUT:
  319. rendererOptions.put("fineDetail", isCoarseAreaXml());
  320. return Driver.RENDER_XML;
  321. default:
  322. throw new FOPException("Invalid Renderer setting!");
  323. }
  324. }
  325. /**
  326. *
  327. **/
  328. public InputHandler getInputHandler () {
  329. switch (inputmode) {
  330. case FO_INPUT:
  331. return new FOInputHandler(fofile);
  332. case XSLT_INPUT:
  333. return new XSLTInputHandler(xmlfile, xsltfile);
  334. default:
  335. return new FOInputHandler(fofile);
  336. }
  337. }
  338. public java.util.Hashtable getRendererOptions() {
  339. return rendererOptions;
  340. }
  341. public Starter getStarter()
  342. throws FOPException
  343. {
  344. switch (outputmode) {
  345. case PDF_OUTPUT:
  346. return new CommandLineStarter(this);
  347. case AWT_OUTPUT:
  348. try {
  349. return((Starter) Class.forName(
  350. "org.apache.fop.apps.AWTStarter").
  351. getConstructor(
  352. new Class[]{CommandLineOptions.class}).
  353. newInstance(new Object[]{this}));
  354. } catch (Exception e) {
  355. if (e instanceof FOPException) {
  356. throw (FOPException)e;
  357. }
  358. throw new FOPException("AWTStarter could not be loaded.",e);
  359. }
  360. case MIF_OUTPUT:
  361. return new CommandLineStarter(this);
  362. case PRINT_OUTPUT:
  363. try {
  364. return((Starter) Class.forName(
  365. "org.apache.fop.apps.PrintStarter").
  366. getConstructor(
  367. new Class[]{CommandLineOptions.class}).
  368. newInstance(new Object[]{this}));
  369. } catch (Exception e) {
  370. if (e instanceof FOPException) {
  371. throw (FOPException)e;
  372. }
  373. throw new FOPException("PrintStarter could not be loaded.",e);
  374. }
  375. case AREA_OUTPUT:
  376. return new CommandLineStarter(this);
  377. default:
  378. return new CommandLineStarter(this);
  379. }
  380. }
  381. public int getInputMode() {
  382. return inputmode;
  383. }
  384. public int getOutputMode() {
  385. return outputmode;
  386. }
  387. public File getFOFile() {
  388. return fofile;
  389. }
  390. public File getXMLFile() {
  391. return xmlfile;
  392. }
  393. public File getXSLFile() {
  394. return xsltfile;
  395. }
  396. public File getOutputFile() {
  397. return outfile;
  398. }
  399. public File getUserConfigFile () {
  400. return userConfigFile;
  401. }
  402. public String getLanguage() {
  403. return language;
  404. }
  405. public File getBufferFile() {
  406. return bufferFile;
  407. }
  408. public Boolean isQuiet() {
  409. return quiet;
  410. }
  411. public Boolean dumpConfiguration() {
  412. return dumpConfiguration;
  413. }
  414. public Boolean isDebugMode() {
  415. return errorDump;
  416. }
  417. public Boolean isCoarseAreaXml() {
  418. return suppressLowLevelAreas;
  419. }
  420. /**
  421. * return either the fofile or the xmlfile
  422. */
  423. public File getInputFile() {
  424. switch (inputmode) {
  425. case FO_INPUT:
  426. return fofile;
  427. case XSLT_INPUT:
  428. return xmlfile;
  429. default:
  430. return fofile;
  431. }
  432. }
  433. /**
  434. * shows the commandline syntax including a summary of all available options and some examples
  435. */
  436. public static void printUsage() {
  437. MessageHandler.errorln(
  438. "\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] [-awt|-pdf|-mif|-pcl|-txt|-at|-print] <outfile>\n" +
  439. " [OPTIONS] \n" + " -d debug mode \n" +
  440. " -x dump configuration settings \n" +
  441. " -q quiet mode \n" +
  442. " -c cfg.xml use additional configuration file cfg.xml\n" +
  443. " -l lang the language to use for user information \n" +
  444. " -s for area tree XML, down to block areas only\n\n" +
  445. " [INPUT] \n" +
  446. " infile xsl:fo input file (the same as the next) \n" +
  447. " -fo infile xsl:fo input file \n" +
  448. " -xml infile xml input file, must be used together with -xsl \n" +
  449. " -xsl stylesheet xslt stylesheet \n \n" +
  450. " [OUTPUT] \n" +
  451. " outfile input will be rendered as pdf file into outfile \n" +
  452. " -pdf outfile input will be rendered as pdf file (outfile req'd) \n" +
  453. " -awt input will be displayed on screen \n" +
  454. " -mif outfile input will be rendered as mif file (outfile req'd)\n" +
  455. " -pcl outfile input will be rendered as pcl 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 TXT_OUTPUT:
  530. MessageHandler.logln("txt");
  531. MessageHandler.logln("output file: " + outfile.toString());
  532. break;
  533. default:
  534. MessageHandler.logln("unknown input type");
  535. }
  536. MessageHandler.logln("OPTIONS");
  537. if (userConfigFile != null) {
  538. MessageHandler.logln("user configuration file: " +
  539. userConfigFile.toString());
  540. } else {
  541. MessageHandler.logln("no user configuration file is used [default]");
  542. }
  543. if (errorDump != null) {
  544. MessageHandler.logln("debug mode on");
  545. } else {
  546. MessageHandler.logln("debug mode off [default]");
  547. }
  548. if (dumpConfiguration != null) {
  549. MessageHandler.logln("dump configuration");
  550. } else {
  551. MessageHandler.logln("don't dump configuration [default]");
  552. }
  553. if (quiet != null) {
  554. MessageHandler.logln("quiet mode on");
  555. } else {
  556. MessageHandler.logln("quiet mode off [default]");
  557. }
  558. }
  559. //debug: create class and output all settings
  560. public static void main (String args[]) {
  561. /*
  562. for (int i = 0; i < args.length; i++) {
  563. MessageHandler.logln(">"+args[i]+"<");
  564. }*/
  565. try {
  566. CommandLineOptions options = new CommandLineOptions (args);
  567. }
  568. catch (Exception e) {
  569. e.printStackTrace();
  570. }
  571. //options.debug();
  572. }
  573. }