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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.cli;
  18. // java
  19. import java.io.File;
  20. import java.io.FileNotFoundException;
  21. import java.io.IOException;
  22. import java.util.Locale;
  23. import java.util.Vector;
  24. import org.apache.fop.Version;
  25. import org.apache.fop.apps.FOPException;
  26. import org.apache.fop.apps.FOUserAgent;
  27. import org.apache.fop.apps.MimeConstants;
  28. import org.apache.fop.fo.Constants;
  29. import org.apache.fop.pdf.PDFEncryptionManager;
  30. import org.apache.fop.pdf.PDFEncryptionParams;
  31. import org.apache.fop.render.awt.AWTRenderer;
  32. import org.apache.fop.util.CommandLineLogger;
  33. // commons logging
  34. import org.apache.commons.logging.Log;
  35. import org.apache.commons.logging.LogFactory;
  36. // SAX
  37. import org.xml.sax.XMLReader;
  38. import org.xml.sax.SAXException;
  39. import javax.xml.parsers.SAXParserFactory;
  40. // avalon configuration
  41. import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
  42. import org.apache.avalon.framework.configuration.Configuration;
  43. import org.apache.avalon.framework.configuration.ConfigurationException;
  44. /**
  45. * Options parses the commandline arguments
  46. */
  47. public class CommandLineOptions implements Constants {
  48. /** Used to indicate that only the result of the XSL transformation should be output */
  49. public static final int RENDER_NONE = -1;
  50. /* show configuration information */
  51. private Boolean showConfiguration = Boolean.FALSE;
  52. /* for area tree XML output, only down to block area level */
  53. private Boolean suppressLowLevelAreas = Boolean.FALSE;
  54. /* user configuration file */
  55. private File userConfigFile = null;
  56. /* input fo file */
  57. private File fofile = null;
  58. /* xsltfile (xslt transformation as input) */
  59. private File xsltfile = null;
  60. /* xml file (xslt transformation as input) */
  61. private File xmlfile = null;
  62. /* output file */
  63. private File outfile = null;
  64. /* input mode */
  65. private int inputmode = NOT_SET;
  66. /* output mode */
  67. private String outputmode = null;
  68. private FOUserAgent foUserAgent;
  69. private InputHandler inputHandler;
  70. private Log log;
  71. private Vector xsltParams = null;
  72. /**
  73. * Construct a command line option object.
  74. */
  75. public CommandLineOptions() {
  76. LogFactory logFactory = LogFactory.getFactory();
  77. // Enable the simple command line logging when no other logger is
  78. // defined.
  79. if (System.getProperty("org.apache.commons.logging.Log") == null) {
  80. logFactory.setAttribute("org.apache.commons.logging.Log",
  81. CommandLineLogger.class.getName());
  82. setLogLevel("info");
  83. }
  84. log = LogFactory.getLog("FOP");
  85. }
  86. /**
  87. * Parse the command line arguments.
  88. * @param args the command line arguments.
  89. * @throws FOPException for general errors
  90. * @throws FileNotFoundException if an input file wasn't found
  91. * @throws IOException if the the configuration file could not be loaded
  92. */
  93. public void parse(String[] args)
  94. throws FOPException, IOException {
  95. boolean optionsParsed = true;
  96. foUserAgent = new FOUserAgent();
  97. try {
  98. optionsParsed = parseOptions(args);
  99. if (optionsParsed) {
  100. if (showConfiguration == Boolean.TRUE) {
  101. dumpConfiguration();
  102. }
  103. checkSettings();
  104. createUserConfig();
  105. addXSLTParameter("fop-output-format", getOutputFormat());
  106. addXSLTParameter("fop-version", Version.getVersion());
  107. }
  108. } catch (FOPException e) {
  109. printUsage();
  110. throw e;
  111. } catch (java.io.FileNotFoundException e) {
  112. printUsage();
  113. throw e;
  114. }
  115. inputHandler = createInputHandler();
  116. if (outputmode.equals(MimeConstants.MIME_FOP_AWT_PREVIEW)) {
  117. AWTRenderer renderer = new AWTRenderer();
  118. renderer.setRenderable(inputHandler); //set before user agent!
  119. renderer.setUserAgent(foUserAgent);
  120. foUserAgent.setRendererOverride(renderer);
  121. }
  122. }
  123. /**
  124. * @return the InputHandler instance defined by the command-line options.
  125. */
  126. public InputHandler getInputHandler() {
  127. return inputHandler;
  128. }
  129. /**
  130. * Get the logger.
  131. * @return the logger
  132. */
  133. public Log getLogger() {
  134. return log;
  135. }
  136. private void addXSLTParameter(String name, String value) {
  137. if (xsltParams == null) {
  138. xsltParams = new Vector();
  139. }
  140. xsltParams.addElement(name);
  141. xsltParams.addElement(value);
  142. }
  143. /**
  144. * parses the commandline arguments
  145. * @return true if parse was successful and processing can continue, false
  146. * if processing should stop
  147. * @exception FOPException if there was an error in the format of the options
  148. */
  149. private boolean parseOptions(String[] args) throws FOPException {
  150. for (int i = 0; i < args.length; i++) {
  151. if (args[i].equals("-x")
  152. || args[i].equals("--dump-config")) {
  153. showConfiguration = Boolean.TRUE;
  154. } else if (args[i].equals("-c")) {
  155. i = i + parseConfigurationOption(args, i);
  156. } else if (args[i].equals("-l")) {
  157. i = i + parseLanguageOption(args, i);
  158. } else if (args[i].equals("-s")) {
  159. suppressLowLevelAreas = Boolean.TRUE;
  160. } else if (args[i].equals("-d")) {
  161. setLogOption("debug", "debug");
  162. } else if (args[i].equals("-r")) {
  163. foUserAgent.setStrictValidation(false);
  164. } else if (args[i].equals("-dpi")) {
  165. i = i + parseResolution(args, i);
  166. } else if (args[i].equals("-q") || args[i].equals("--quiet")) {
  167. setLogOption("quiet", "error");
  168. } else if (args[i].equals("-fo")) {
  169. i = i + parseFOInputOption(args, i);
  170. } else if (args[i].equals("-xsl")) {
  171. i = i + parseXSLInputOption(args, i);
  172. } else if (args[i].equals("-xml")) {
  173. i = i + parseXMLInputOption(args, i);
  174. } else if (args[i].equals("-awt")) {
  175. i = i + parseAWTOutputOption(args, i);
  176. } else if (args[i].equals("-pdf")) {
  177. i = i + parsePDFOutputOption(args, i);
  178. } else if (args[i].equals("-mif")) {
  179. i = i + parseMIFOutputOption(args, i);
  180. } else if (args[i].equals("-rtf")) {
  181. i = i + parseRTFOutputOption(args, i);
  182. } else if (args[i].equals("-tiff")) {
  183. i = i + parseTIFFOutputOption(args, i);
  184. } else if (args[i].equals("-png")) {
  185. i = i + parsePNGOutputOption(args, i);
  186. } else if (args[i].equals("-print")) {
  187. i = i + parsePrintOutputOption(args, i);
  188. // show print help
  189. if (i + 1 < args.length) {
  190. if (args[i + 1].equals("help")) {
  191. printUsagePrintOutput();
  192. return false;
  193. }
  194. }
  195. } else if (args[i].equals("-pcl")) {
  196. i = i + parsePCLOutputOption(args, i);
  197. } else if (args[i].equals("-ps")) {
  198. i = i + parsePostscriptOutputOption(args, i);
  199. } else if (args[i].equals("-txt")) {
  200. i = i + parseTextOutputOption(args, i);
  201. } else if (args[i].equals("-svg")) {
  202. i = i + parseSVGOutputOption(args, i);
  203. } else if (args[i].equals("-foout")) {
  204. i = i + parseFOOutputOption(args, i);
  205. } else if (args[i].equals("-out")) {
  206. i = i + parseCustomOutputOption(args, i);
  207. } else if (args[i].charAt(0) != '-') {
  208. i = i + parseUnknownOption(args, i);
  209. } else if (args[i].equals("-at")) {
  210. i = i + parseAreaTreeOption(args, i);
  211. } else if (args[i].equals("-v")) {
  212. System.out.println("FOP Version " + Version.getVersion());
  213. } else if (args[i].equals("-param")) {
  214. if (i + 2 < args.length) {
  215. String name = args[++i];
  216. String expression = args[++i];
  217. addXSLTParameter(name, expression);
  218. } else {
  219. throw new FOPException("invalid param usage: use -param <name> <value>");
  220. }
  221. } else if (args[i].equals("-o")) {
  222. i = i + parsePDFOwnerPassword(args, i);
  223. } else if (args[i].equals("-u")) {
  224. i = i + parsePDFUserPassword(args, i);
  225. } else if (args[i].equals("-noprint")) {
  226. getPDFEncryptionParams().setAllowPrint(false);
  227. } else if (args[i].equals("-nocopy")) {
  228. getPDFEncryptionParams().setAllowCopyContent(false);
  229. } else if (args[i].equals("-noedit")) {
  230. getPDFEncryptionParams().setAllowEditContent(false);
  231. } else if (args[i].equals("-noannotations")) {
  232. getPDFEncryptionParams().setAllowEditAnnotations(false);
  233. } else {
  234. printUsage();
  235. return false;
  236. }
  237. }
  238. return true;
  239. } // end parseOptions
  240. private int parseConfigurationOption(String[] args, int i) throws FOPException {
  241. if ((i + 1 == args.length)
  242. || (args[i + 1].charAt(0) == '-')) {
  243. throw new FOPException("if you use '-c', you must specify "
  244. + "the name of the configuration file");
  245. } else {
  246. userConfigFile = new File(args[i + 1]);
  247. return 1;
  248. }
  249. }
  250. private int parseLanguageOption(String[] args, int i) throws FOPException {
  251. if ((i + 1 == args.length)
  252. || (args[i + 1].charAt(0) == '-')) {
  253. throw new FOPException("if you use '-l', you must specify a language");
  254. } else {
  255. Locale.setDefault(new Locale(args[i + 1], ""));
  256. return 1;
  257. }
  258. }
  259. private int parseResolution(String[] args, int i) throws FOPException {
  260. if ((i + 1 == args.length)
  261. || (args[i + 1].charAt(0) == '-')) {
  262. throw new FOPException(
  263. "if you use '-dpi', you must specify a resolution (dots per inch)");
  264. } else {
  265. foUserAgent.setTargetResolution(Integer.parseInt(args[i + 1]));
  266. return 1;
  267. }
  268. }
  269. private int parseFOInputOption(String[] args, int i) throws FOPException {
  270. inputmode = FO_INPUT;
  271. if ((i + 1 == args.length)
  272. || (args[i + 1].charAt(0) == '-')) {
  273. throw new FOPException("you must specify the fo file for the '-fo' option");
  274. } else {
  275. fofile = new File(args[i + 1]);
  276. return 1;
  277. }
  278. }
  279. private int parseXSLInputOption(String[] args, int i) throws FOPException {
  280. inputmode = XSLT_INPUT;
  281. if ((i + 1 == args.length)
  282. || (args[i + 1].charAt(0) == '-')) {
  283. throw new FOPException("you must specify the stylesheet "
  284. + "file for the '-xsl' option");
  285. } else {
  286. xsltfile = new File(args[i + 1]);
  287. return 1;
  288. }
  289. }
  290. private int parseXMLInputOption(String[] args, int i) throws FOPException {
  291. inputmode = XSLT_INPUT;
  292. if ((i + 1 == args.length)
  293. || (args[i + 1].charAt(0) == '-')) {
  294. throw new FOPException("you must specify the input file "
  295. + "for the '-xml' option");
  296. } else {
  297. xmlfile = new File(args[i + 1]);
  298. return 1;
  299. }
  300. }
  301. private int parseAWTOutputOption(String[] args, int i) throws FOPException {
  302. setOutputMode(MimeConstants.MIME_FOP_AWT_PREVIEW);
  303. return 0;
  304. }
  305. private int parsePDFOutputOption(String[] args, int i) throws FOPException {
  306. setOutputMode(MimeConstants.MIME_PDF);
  307. if ((i + 1 == args.length)
  308. || (args[i + 1].charAt(0) == '-')) {
  309. throw new FOPException("you must specify the PDF output file");
  310. } else {
  311. outfile = new File(args[i + 1]);
  312. return 1;
  313. }
  314. }
  315. private int parseMIFOutputOption(String[] args, int i) throws FOPException {
  316. setOutputMode(MimeConstants.MIME_MIF);
  317. if ((i + 1 == args.length)
  318. || (args[i + 1].charAt(0) == '-')) {
  319. throw new FOPException("you must specify the MIF output file");
  320. } else {
  321. outfile = new File(args[i + 1]);
  322. return 1;
  323. }
  324. }
  325. private int parseRTFOutputOption(String[] args, int i) throws FOPException {
  326. setOutputMode(MimeConstants.MIME_RTF);
  327. if ((i + 1 == args.length)
  328. || (args[i + 1].charAt(0) == '-')) {
  329. throw new FOPException("you must specify the RTF output file");
  330. } else {
  331. outfile = new File(args[i + 1]);
  332. return 1;
  333. }
  334. }
  335. private int parseTIFFOutputOption(String[] args, int i) throws FOPException {
  336. setOutputMode(MimeConstants.MIME_TIFF);
  337. if ((i + 1 == args.length)
  338. || (args[i + 1].charAt(0) == '-')) {
  339. throw new FOPException("you must specify the TIFF output file");
  340. } else {
  341. outfile = new File(args[i + 1]);
  342. return 1;
  343. }
  344. }
  345. private int parsePNGOutputOption(String[] args, int i) throws FOPException {
  346. setOutputMode(MimeConstants.MIME_PNG);
  347. if ((i + 1 == args.length)
  348. || (args[i + 1].charAt(0) == '-')) {
  349. throw new FOPException("you must specify the PNG output file");
  350. } else {
  351. outfile = new File(args[i + 1]);
  352. return 1;
  353. }
  354. }
  355. private int parsePrintOutputOption(String[] args, int i) throws FOPException {
  356. setOutputMode(MimeConstants.MIME_FOP_PRINT);
  357. return 0;
  358. }
  359. private int parsePCLOutputOption(String[] args, int i) throws FOPException {
  360. setOutputMode(MimeConstants.MIME_PCL);
  361. if ((i + 1 == args.length)
  362. || (args[i + 1].charAt(0) == '-')) {
  363. throw new FOPException("you must specify the PDF output file");
  364. } else {
  365. outfile = new File(args[i + 1]);
  366. return 1;
  367. }
  368. }
  369. private int parsePostscriptOutputOption(String[] args, int i) throws FOPException {
  370. setOutputMode(MimeConstants.MIME_POSTSCRIPT);
  371. if ((i + 1 == args.length)
  372. || (args[i + 1].charAt(0) == '-')) {
  373. throw new FOPException("you must specify the PostScript output file");
  374. } else {
  375. outfile = new File(args[i + 1]);
  376. return 1;
  377. }
  378. }
  379. private int parseTextOutputOption(String[] args, int i) throws FOPException {
  380. setOutputMode(MimeConstants.MIME_PLAIN_TEXT);
  381. if ((i + 1 == args.length)
  382. || (args[i + 1].charAt(0) == '-')) {
  383. throw new FOPException("you must specify the text output file");
  384. } else {
  385. outfile = new File(args[i + 1]);
  386. return 1;
  387. }
  388. }
  389. private int parseSVGOutputOption(String[] args, int i) throws FOPException {
  390. setOutputMode(MimeConstants.MIME_SVG);
  391. if ((i + 1 == args.length)
  392. || (args[i + 1].charAt(0) == '-')) {
  393. throw new FOPException("you must specify the SVG output file");
  394. } else {
  395. outfile = new File(args[i + 1]);
  396. return 1;
  397. }
  398. }
  399. private int parseFOOutputOption(String[] args, int i) throws FOPException {
  400. setOutputMode(MimeConstants.MIME_XSL_FO);
  401. if ((i + 1 == args.length)
  402. || (args[i + 1].charAt(0) == '-')) {
  403. throw new FOPException("you must specify the FO output file");
  404. } else {
  405. outfile = new File(args[i + 1]);
  406. return 1;
  407. }
  408. }
  409. private int parseCustomOutputOption(String[] args, int i) throws FOPException {
  410. String mime = null;
  411. if ((i + 1 < args.length)
  412. || (args[i + 1].charAt(0) != '-')) {
  413. mime = args[i + 1];
  414. if ("list".equals(mime)) {
  415. String[] mimes = foUserAgent.getRendererFactory().listSupportedMimeTypes();
  416. System.out.println("Supported MIME types:");
  417. for (int j = 0; j < mimes.length; j++) {
  418. System.out.println(" " + mimes[j]);
  419. }
  420. System.exit(0);
  421. }
  422. }
  423. if ((i + 2 >= args.length)
  424. || (args[i + 1].charAt(0) == '-')
  425. || (args[i + 2].charAt(0) == '-')) {
  426. throw new FOPException("you must specify the output format and the output file");
  427. } else {
  428. setOutputMode(mime);
  429. outfile = new File(args[i + 2]);
  430. return 2;
  431. }
  432. }
  433. private int parseUnknownOption(String[] args, int i) throws FOPException {
  434. if (inputmode == NOT_SET) {
  435. inputmode = FO_INPUT;
  436. fofile = new File(args[i]);
  437. } else if (outputmode == null) {
  438. outputmode = MimeConstants.MIME_PDF;
  439. outfile = new File(args[i]);
  440. } else {
  441. throw new FOPException("Don't know what to do with "
  442. + args[i]);
  443. }
  444. return 0;
  445. }
  446. private int parseAreaTreeOption(String[] args, int i) throws FOPException {
  447. setOutputMode(MimeConstants.MIME_FOP_AREA_TREE);
  448. if ((i + 1 == args.length)
  449. || (args[i + 1].charAt(0) == '-')) {
  450. throw new FOPException("you must specify the area-tree output file");
  451. } else {
  452. outfile = new File(args[i + 1]);
  453. return 1;
  454. }
  455. }
  456. private PDFEncryptionParams getPDFEncryptionParams() throws FOPException {
  457. if (foUserAgent.getPDFEncryptionParams() == null) {
  458. if (!PDFEncryptionManager.checkAvailableAlgorithms()) {
  459. throw new FOPException("PDF encryption requested but it is not available."
  460. + " Please make sure MD5 and RC4 algorithms are available.");
  461. }
  462. foUserAgent.setPDFEncryptionParams(new PDFEncryptionParams());
  463. }
  464. return foUserAgent.getPDFEncryptionParams();
  465. }
  466. private int parsePDFOwnerPassword(String[] args, int i) throws FOPException {
  467. if ((i + 1 == args.length)
  468. || (args[i + 1].charAt(0) == '-')) {
  469. getPDFEncryptionParams().setOwnerPassword("");
  470. return 0;
  471. } else {
  472. getPDFEncryptionParams().setOwnerPassword(args[i + 1]);
  473. return 1;
  474. }
  475. }
  476. private int parsePDFUserPassword(String[] args, int i) throws FOPException {
  477. if ((i + 1 == args.length)
  478. || (args[i + 1].charAt(0) == '-')) {
  479. getPDFEncryptionParams().setUserPassword("");
  480. return 0;
  481. } else {
  482. getPDFEncryptionParams().setUserPassword(args[i + 1]);
  483. return 1;
  484. }
  485. }
  486. private void setOutputMode(String mime) throws FOPException {
  487. if (outputmode == null) {
  488. outputmode = mime;
  489. } else {
  490. throw new FOPException("you can only set one output method");
  491. }
  492. }
  493. private void setLogOption (String option, String level) {
  494. if (log instanceof CommandLineLogger
  495. || System.getProperty("org.apache.commons.logging.Log") == null) {
  496. setLogLevel(level);
  497. } else if (log != null) {
  498. log.warn("The option " + option + " can only be used");
  499. log.warn("with FOP's command line logger,");
  500. log.warn("which is the default on the command line.");
  501. log.warn("Configure other loggers using Java system properties.");
  502. }
  503. }
  504. private void setLogLevel(String level) {
  505. // Set the level for future loggers.
  506. LogFactory.getFactory().setAttribute("level", level);
  507. if (log instanceof CommandLineLogger) {
  508. // Set the level for the logger created already.
  509. ((CommandLineLogger) log).setLogLevel(level);
  510. }
  511. }
  512. /**
  513. * checks whether all necessary information has been given in a consistent way
  514. */
  515. private void checkSettings() throws FOPException, FileNotFoundException {
  516. if (inputmode == NOT_SET) {
  517. throw new FOPException("No input file specified");
  518. }
  519. if (outputmode == null) {
  520. throw new FOPException("No output file specified");
  521. }
  522. if ((outputmode.equals(MimeConstants.MIME_FOP_AWT_PREVIEW)
  523. || outputmode.equals(MimeConstants.MIME_FOP_PRINT))
  524. && outfile != null) {
  525. throw new FOPException("Output file may not be specified "
  526. + "for AWT or PRINT output");
  527. }
  528. if (inputmode == XSLT_INPUT) {
  529. // check whether xml *and* xslt file have been set
  530. if (xmlfile == null) {
  531. throw new FOPException("XML file must be specified for the transform mode");
  532. }
  533. if (xsltfile == null) {
  534. throw new FOPException("XSLT file must be specified for the transform mode");
  535. }
  536. // warning if fofile has been set in xslt mode
  537. if (fofile != null) {
  538. log.warn("Can't use fo file with transform mode! Ignoring.\n"
  539. + "Your input is " + "\n xmlfile: "
  540. + xmlfile.getAbsolutePath()
  541. + "\nxsltfile: "
  542. + xsltfile.getAbsolutePath()
  543. + "\n fofile: "
  544. + fofile.getAbsolutePath());
  545. }
  546. if (!xmlfile.exists()) {
  547. throw new FileNotFoundException("Error: xml file "
  548. + xmlfile.getAbsolutePath()
  549. + " not found ");
  550. }
  551. if (!xsltfile.exists()) {
  552. throw new FileNotFoundException("Error: xsl file "
  553. + xsltfile.getAbsolutePath()
  554. + " not found ");
  555. }
  556. } else if (inputmode == FO_INPUT) {
  557. if (outputmode.equals(MimeConstants.MIME_XSL_FO)) {
  558. throw new FOPException(
  559. "FO output mode is only available if you use -xml and -xsl");
  560. }
  561. if (xmlfile != null || xsltfile != null) {
  562. log.warn("fo input mode, but xmlfile or xslt file are set:");
  563. log.error("xml file: " + xmlfile.toString());
  564. log.error("xslt file: " + xsltfile.toString());
  565. }
  566. if (!fofile.exists()) {
  567. throw new FileNotFoundException("Error: fo file "
  568. + fofile.getAbsolutePath()
  569. + " not found ");
  570. }
  571. }
  572. } // end checkSettings
  573. /**
  574. * Create the user configuration.
  575. * @throws FOPException if creating the user configuration fails
  576. * @throws IOException
  577. */
  578. private void createUserConfig() throws FOPException, IOException {
  579. if (userConfigFile == null) {
  580. return;
  581. }
  582. XMLReader parser = createParser();
  583. DefaultConfigurationBuilder configBuilder
  584. = new DefaultConfigurationBuilder(parser);
  585. Configuration userConfig = null;
  586. try {
  587. userConfig = configBuilder.buildFromFile(userConfigFile);
  588. } catch (SAXException e) {
  589. throw new FOPException(e);
  590. } catch (ConfigurationException e) {
  591. throw new FOPException(e);
  592. }
  593. foUserAgent.setUserConfig(userConfig);
  594. }
  595. /**
  596. * @return the chosen output format (MIME type)
  597. * @throws FOPException for invalid output formats
  598. */
  599. protected String getOutputFormat() throws FOPException {
  600. if (outputmode == null) {
  601. throw new FOPException("Renderer has not been set!");
  602. }
  603. if (outputmode.equals(MimeConstants.MIME_FOP_AREA_TREE)) {
  604. foUserAgent.getRendererOptions().put("fineDetail", isCoarseAreaXml());
  605. }
  606. return outputmode;
  607. }
  608. /**
  609. * Create an InputHandler object based on command-line parameters
  610. * @return a new InputHandler instance
  611. * @throws IllegalArgumentException if invalid/missing parameters
  612. */
  613. private InputHandler createInputHandler() throws IllegalArgumentException {
  614. switch (inputmode) {
  615. case FO_INPUT:
  616. return new InputHandler(fofile);
  617. case XSLT_INPUT:
  618. return new InputHandler(xmlfile, xsltfile, xsltParams);
  619. default:
  620. throw new IllegalArgumentException("Error creating InputHandler object.");
  621. }
  622. }
  623. /**
  624. * Get the FOUserAgent for this Command-Line run
  625. * @return FOUserAgent instance
  626. */
  627. protected FOUserAgent getFOUserAgent() {
  628. return foUserAgent;
  629. }
  630. /**
  631. * Returns the XSL-FO file if set.
  632. * @return the XSL-FO file, null if not set
  633. */
  634. public File getFOFile() {
  635. return fofile;
  636. }
  637. /**
  638. * Returns the input XML file if set.
  639. * @return the input XML file, null if not set
  640. */
  641. public File getXMLFile() {
  642. return xmlfile;
  643. }
  644. /**
  645. * Returns the stylesheet to be used for transformation to XSL-FO.
  646. * @return stylesheet
  647. */
  648. public File getXSLFile() {
  649. return xsltfile;
  650. }
  651. /**
  652. * Returns the output file
  653. * @return the output file
  654. */
  655. public File getOutputFile() {
  656. return outfile;
  657. }
  658. /**
  659. * Returns the user configuration file to be used.
  660. * @return the userconfig.xml file
  661. */
  662. public File getUserConfigFile() {
  663. return userConfigFile;
  664. }
  665. /**
  666. * Indicates whether the XML renderer should generate coarse area XML
  667. * @return true if coarse area XML is desired
  668. */
  669. public Boolean isCoarseAreaXml() {
  670. return suppressLowLevelAreas;
  671. }
  672. /**
  673. * Returns the input file.
  674. * @return either the fofile or the xmlfile
  675. */
  676. public File getInputFile() {
  677. switch (inputmode) {
  678. case FO_INPUT:
  679. return fofile;
  680. case XSLT_INPUT:
  681. return xmlfile;
  682. default:
  683. return fofile;
  684. }
  685. }
  686. /**
  687. * shows the commandline syntax including a summary of all available options and some examples
  688. */
  689. public static void printUsage() {
  690. System.err.println(
  691. "\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] "
  692. + "[-awt|-pdf|-mif|-rtf|-tiff|-png|-pcl|-ps|-txt|-at|-print] <outfile>\n"
  693. + " [OPTIONS] \n"
  694. + " -d debug mode \n"
  695. + " -x dump configuration settings \n"
  696. + " -q quiet mode \n"
  697. + " -c cfg.xml use additional configuration file cfg.xml\n"
  698. + " -l lang the language to use for user information \n"
  699. + " -r relaxed/less strict validation (where available)\n"
  700. + " -dpi xxx target resolution in dots per inch (dpi) where xxx is a number\n"
  701. + " -s for area tree XML, down to block areas only\n"
  702. + " -v to show FOP version being used\n\n"
  703. + " -o [password] PDF file will be encrypted with option owner password\n"
  704. + " -u [password] PDF file will be encrypted with option user password\n"
  705. + " -noprint PDF file will be encrypted without printing permission\n"
  706. + " -nocopy PDF file will be encrypted without copy content permission\n"
  707. + " -noedit PDF file will be encrypted without edit content permission\n"
  708. + " -noannotations PDF file will be encrypted without edit annotation permission\n\n"
  709. + " [INPUT] \n"
  710. + " infile xsl:fo input file (the same as the next) \n"
  711. + " -fo infile xsl:fo input file \n"
  712. + " -xml infile xml input file, must be used together with -xsl \n"
  713. + " -xsl stylesheet xslt stylesheet \n \n"
  714. + " -param name value <value> to use for parameter <name> in xslt stylesheet\n"
  715. + " (repeat '-param name value' for each parameter)\n \n"
  716. + " [OUTPUT] \n"
  717. + " outfile input will be rendered as pdf file into outfile \n"
  718. + " -pdf outfile input will be rendered as pdf file (outfile req'd) \n"
  719. + " -awt input will be displayed on screen \n"
  720. + " -mif outfile input will be rendered as mif file (outfile req'd)\n"
  721. + " -rtf outfile input will be rendered as rtf file (outfile req'd)\n"
  722. + " -tiff outfile input will be rendered as tiff file (outfile req'd)\n"
  723. + " -png outfile input will be rendered as png file (outfile req'd)\n"
  724. + " -pcl outfile input will be rendered as pcl file (outfile req'd) \n"
  725. + " -ps outfile input will be rendered as PostScript file (outfile req'd) \n"
  726. + " -txt outfile input will be rendered as text file (outfile req'd) \n"
  727. + " -svg outfile input will be rendered as an svg slides file (outfile req'd) \n"
  728. + " -at outfile representation of area tree as XML (outfile req'd) \n"
  729. + " -print input file will be rendered and sent to the printer \n"
  730. + " see options with \"-print help\" \n"
  731. + " -out mime outfile input will be rendered using the given MIME type\n"
  732. + " (outfile req'd) Example: \"-out application/pdf D:\\out.pdf\"\n"
  733. + " (Tip: \"-out list\" prints the list of supported MIME types)\n"
  734. + "\n"
  735. + " -foout outfile input will only be XSL transformed. The intermediate \n"
  736. + " XSL-FO file is saved and no rendering is performed. \n"
  737. + " (Only available if you use -xml and -xsl parameters)\n\n"
  738. + " [Examples]\n" + " Fop foo.fo foo.pdf \n"
  739. + " Fop -fo foo.fo -pdf foo.pdf (does the same as the previous line)\n"
  740. + " Fop -xml foo.xml -xsl foo.xsl -pdf foo.pdf\n"
  741. + " Fop -xml foo.xml -xsl foo.xsl -foout foo.fo\n"
  742. + " Fop foo.fo -mif foo.mif\n"
  743. + " Fop foo.fo -rtf foo.rtf\n"
  744. + " Fop foo.fo -print or Fop -print foo.fo \n"
  745. + " Fop foo.fo -awt \n");
  746. }
  747. /**
  748. * shows the options for print output
  749. */
  750. private void printUsagePrintOutput() {
  751. System.err.println("USAGE: -print [-Dstart=i] [-Dend=i] [-Dcopies=i] [-Deven=true|false] "
  752. + " org.apache.fop.apps.Fop (..) -print \n"
  753. + "Example:\n"
  754. + "java -Dstart=1 -Dend=2 org.apache.Fop.apps.Fop infile.fo -print ");
  755. }
  756. /**
  757. * Outputs all commandline settings
  758. */
  759. private void dumpConfiguration() {
  760. log.info("Input mode: ");
  761. switch (inputmode) {
  762. case NOT_SET:
  763. log.info("not set");
  764. break;
  765. case FO_INPUT:
  766. log.info("FO ");
  767. log.info("fo input file: " + fofile.toString());
  768. break;
  769. case XSLT_INPUT:
  770. log.info("xslt transformation");
  771. log.info("xml input file: " + xmlfile.toString());
  772. log.info("xslt stylesheet: " + xsltfile.toString());
  773. break;
  774. default:
  775. log.info("unknown input type");
  776. }
  777. log.info("Output mode: ");
  778. if (outputmode == null) {
  779. log.info("not set");
  780. } else if (MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputmode)) {
  781. log.info("awt on screen");
  782. if (outfile != null) {
  783. log.error("awt mode, but outfile is set:");
  784. log.info("out file: " + outfile.toString());
  785. }
  786. } else if (MimeConstants.MIME_FOP_PRINT.equals(outputmode)) {
  787. log.info("print directly");
  788. if (outfile != null) {
  789. log.error("print mode, but outfile is set:");
  790. log.error("out file: " + outfile.toString());
  791. }
  792. } else if (MimeConstants.MIME_FOP_AREA_TREE.equals(outputmode)) {
  793. log.info("area tree");
  794. log.info("output file: " + outfile.toString());
  795. } else {
  796. log.info(outputmode);
  797. log.info("output file: " + outfile.toString());
  798. }
  799. log.info("OPTIONS");
  800. if (userConfigFile != null) {
  801. log.info("user configuration file: "
  802. + userConfigFile.toString());
  803. } else {
  804. log.info("no user configuration file is used [default]");
  805. }
  806. }
  807. /**
  808. * Creates <code>XMLReader</code> object using default
  809. * <code>SAXParserFactory</code>
  810. * @return the created <code>XMLReader</code>
  811. * @throws FOPException if the parser couldn't be created or configured for proper operation.
  812. */
  813. private XMLReader createParser() throws FOPException {
  814. try {
  815. SAXParserFactory factory = SAXParserFactory.newInstance();
  816. factory.setNamespaceAware(true);
  817. return factory.newSAXParser().getXMLReader();
  818. } catch (Exception e) {
  819. throw new FOPException("Couldn't create XMLReader", e);
  820. }
  821. }
  822. }