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

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