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

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