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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.cli;
  19. // java
  20. import java.io.File;
  21. import java.io.FileNotFoundException;
  22. import java.io.IOException;
  23. import java.io.PrintStream;
  24. import java.util.Locale;
  25. import java.util.Map;
  26. import java.util.Vector;
  27. import javax.swing.UIManager;
  28. import org.xml.sax.SAXException;
  29. import org.apache.commons.logging.Log;
  30. import org.apache.commons.logging.LogFactory;
  31. import org.apache.fop.Version;
  32. import org.apache.fop.accessibility.Accessibility;
  33. import org.apache.fop.apps.FOPException;
  34. import org.apache.fop.apps.FOUserAgent;
  35. import org.apache.fop.apps.FopFactory;
  36. import org.apache.fop.apps.MimeConstants;
  37. import org.apache.fop.fonts.FontManager;
  38. import org.apache.fop.pdf.PDFAMode;
  39. import org.apache.fop.pdf.PDFEncryptionManager;
  40. import org.apache.fop.pdf.PDFEncryptionParams;
  41. import org.apache.fop.pdf.PDFXMode;
  42. import org.apache.fop.render.Renderer;
  43. import org.apache.fop.render.awt.AWTRenderer;
  44. import org.apache.fop.render.intermediate.IFContext;
  45. import org.apache.fop.render.intermediate.IFDocumentHandler;
  46. import org.apache.fop.render.intermediate.IFSerializer;
  47. import org.apache.fop.render.pdf.PDFConfigurationConstants;
  48. import org.apache.fop.render.print.PagesMode;
  49. import org.apache.fop.render.print.PrintRenderer;
  50. import org.apache.fop.render.xml.XMLRenderer;
  51. import org.apache.fop.util.CommandLineLogger;
  52. /**
  53. * Options parses the commandline arguments
  54. */
  55. public class CommandLineOptions {
  56. /** Used to indicate that only the result of the XSL transformation should be output */
  57. public static final int RENDER_NONE = -1;
  58. /* These following constants are used to describe the input (either .FO, .XML/.XSL or
  59. * intermediate format)
  60. */
  61. /** (input) not set */
  62. public static final int NOT_SET = 0;
  63. /** input: fo file */
  64. public static final int FO_INPUT = 1;
  65. /** input: xml+xsl file */
  66. public static final int XSLT_INPUT = 2;
  67. /** input: Area Tree XML file */
  68. public static final int AREATREE_INPUT = 3;
  69. /** input: Intermediate Format XML file */
  70. public static final int IF_INPUT = 4;
  71. /** input: Image file */
  72. public static final int IMAGE_INPUT = 5;
  73. /* show configuration information */
  74. private Boolean showConfiguration = Boolean.FALSE;
  75. /* for area tree XML output, only down to block area level */
  76. private Boolean suppressLowLevelAreas = Boolean.FALSE;
  77. /* user configuration file */
  78. private File userConfigFile = null;
  79. /* input fo file */
  80. private File foFile = null;
  81. /* xsltfile (xslt transformation as input) */
  82. private File xsltFile = null;
  83. /* xml file (xslt transformation as input) */
  84. private File xmlFile = null;
  85. /* area tree input file */
  86. private File areatreeFile = null;
  87. /* intermediate format input file */
  88. private File ifFile = null;
  89. /* area tree input file */
  90. private File imageFile = null;
  91. /* output file */
  92. private File outFile = null;
  93. /* input mode */
  94. private int inputMode = NOT_SET;
  95. /* output mode */
  96. private String outputMode = null;
  97. /* true if System.in (stdin) should be used for the input file */
  98. private boolean useStdIn = false;
  99. /* true if System.out (stdout) should be used for the output file */
  100. private boolean useStdOut = false;
  101. /* true if a catalog resolver should be used for entity and uri resolution */
  102. private boolean useCatalogResolver = false;
  103. /* rendering options (for the user agent) */
  104. private Map renderingOptions = new java.util.HashMap();
  105. /* target resolution (for the user agent) */
  106. private int targetResolution = 0;
  107. /* control memory-conservation policy */
  108. private boolean conserveMemoryPolicy = false;
  109. private FopFactory factory = FopFactory.newInstance();
  110. private FOUserAgent foUserAgent;
  111. private InputHandler inputHandler;
  112. private Log log;
  113. private Vector xsltParams = null;
  114. private String mimicRenderer = null;
  115. private boolean flushCache = false;
  116. /**
  117. * Construct a command line option object.
  118. */
  119. public CommandLineOptions() {
  120. LogFactory logFactory = LogFactory.getFactory();
  121. // Enable the simple command line logging when no other logger is
  122. // defined.
  123. if (System.getProperty("org.apache.commons.logging.Log") == null) {
  124. logFactory.setAttribute("org.apache.commons.logging.Log",
  125. CommandLineLogger.class.getName());
  126. setLogLevel("info");
  127. }
  128. log = LogFactory.getLog("FOP");
  129. }
  130. /**
  131. * Parses the command line arguments.
  132. *
  133. * @param args the command line arguments.
  134. * @throws FOPException for general errors
  135. * @throws IOException if the the configuration file could not be loaded
  136. * @return true if the processing can continue, false to abort
  137. */
  138. public boolean parse(String[] args)
  139. throws FOPException, IOException {
  140. boolean optionsParsed = true;
  141. try {
  142. optionsParsed = parseOptions(args);
  143. if (optionsParsed) {
  144. if (showConfiguration == Boolean.TRUE) {
  145. dumpConfiguration();
  146. }
  147. checkSettings();
  148. setUserConfig();
  149. if (flushCache) {
  150. flushCache();
  151. }
  152. //Factory config is set up, now we can create the user agent
  153. foUserAgent = factory.newFOUserAgent();
  154. foUserAgent.getRendererOptions().putAll(renderingOptions);
  155. if (targetResolution != 0) {
  156. foUserAgent.setTargetResolution(targetResolution);
  157. }
  158. addXSLTParameter("fop-output-format", getOutputFormat());
  159. addXSLTParameter("fop-version", Version.getVersion());
  160. foUserAgent.setConserveMemoryPolicy(conserveMemoryPolicy);
  161. } else {
  162. return false;
  163. }
  164. } catch (FOPException e) {
  165. printUsage(System.err);
  166. throw e;
  167. } catch (java.io.FileNotFoundException e) {
  168. printUsage(System.err);
  169. throw e;
  170. }
  171. inputHandler = createInputHandler();
  172. if (MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputMode)) {
  173. //set the system look&feel for the preview dialog
  174. try {
  175. UIManager.setLookAndFeel(
  176. UIManager.getSystemLookAndFeelClassName());
  177. } catch (Exception e) {
  178. System.err.println("Couldn't set system look & feel!");
  179. }
  180. AWTRenderer renderer = new AWTRenderer(true);
  181. renderer.setRenderable(inputHandler); //set before user agent!
  182. renderer.setUserAgent(foUserAgent);
  183. foUserAgent.setRendererOverride(renderer);
  184. } else if (MimeConstants.MIME_FOP_AREA_TREE.equals(outputMode)
  185. && mimicRenderer != null) {
  186. // render from FO to Intermediate Format
  187. Renderer targetRenderer = foUserAgent.getRendererFactory().createRenderer(
  188. foUserAgent, mimicRenderer);
  189. XMLRenderer xmlRenderer = new XMLRenderer();
  190. xmlRenderer.setUserAgent(foUserAgent);
  191. //Tell the XMLRenderer to mimic the target renderer
  192. xmlRenderer.mimicRenderer(targetRenderer);
  193. //Make sure the prepared XMLRenderer is used
  194. foUserAgent.setRendererOverride(xmlRenderer);
  195. } else if (MimeConstants.MIME_FOP_IF.equals(outputMode)
  196. && mimicRenderer != null) {
  197. // render from FO to Intermediate Format
  198. IFSerializer serializer = new IFSerializer();
  199. serializer.setContext(new IFContext(foUserAgent));
  200. IFDocumentHandler targetHandler
  201. = foUserAgent.getRendererFactory().createDocumentHandler(
  202. foUserAgent, mimicRenderer);
  203. serializer.mimicDocumentHandler(targetHandler);
  204. //Make sure the prepared serializer is used
  205. foUserAgent.setDocumentHandlerOverride(serializer);
  206. }
  207. return true;
  208. }
  209. /**
  210. * @return the InputHandler instance defined by the command-line options.
  211. */
  212. public InputHandler getInputHandler() {
  213. return inputHandler;
  214. }
  215. /**
  216. * Get the logger.
  217. * @return the logger
  218. */
  219. public Log getLogger() {
  220. return log;
  221. }
  222. private void addXSLTParameter(String name, String value) {
  223. if (xsltParams == null) {
  224. xsltParams = new Vector();
  225. }
  226. xsltParams.addElement(name);
  227. xsltParams.addElement(value);
  228. }
  229. /**
  230. * Parses the command line arguments.
  231. *
  232. * @return true if processing can continue, false if it should stop (nothing to do)
  233. * @exception FOPException if there was an error in the format of the options
  234. */
  235. private boolean parseOptions(String[] args) throws FOPException {
  236. // do not throw an exception for no args
  237. if (args.length == 0) {
  238. printVersion();
  239. printUsage(System.out);
  240. return false;
  241. }
  242. for (int i = 0; i < args.length; i++) {
  243. if (args[i].equals("-x")
  244. || args[i].equals("--dump-config")) {
  245. showConfiguration = Boolean.TRUE;
  246. } else if (args[i].equals("-c")) {
  247. i = i + parseConfigurationOption(args, i);
  248. } else if (args[i].equals("-l")) {
  249. i = i + parseLanguageOption(args, i);
  250. } else if (args[i].equals("-s")) {
  251. suppressLowLevelAreas = Boolean.TRUE;
  252. } else if (args[i].equals("-d")) {
  253. setLogOption("debug", "debug");
  254. } else if (args[i].equals("-r")) {
  255. factory.setStrictValidation(false);
  256. } else if (args[i].equals("-conserve")) {
  257. conserveMemoryPolicy = true;
  258. } else if (args[i].equals("-flush")) {
  259. flushCache = true;
  260. } else if (args[i].equals("-cache")) {
  261. parseCacheOption(args, i);
  262. } else if (args[i].equals("-dpi")) {
  263. i = i + parseResolution(args, i);
  264. } else if (args[i].equals("-q") || args[i].equals("--quiet")) {
  265. setLogOption("quiet", "error");
  266. } else if (args[i].equals("-fo")) {
  267. i = i + parseFOInputOption(args, i);
  268. } else if (args[i].equals("-xsl")) {
  269. i = i + parseXSLInputOption(args, i);
  270. } else if (args[i].equals("-xml")) {
  271. i = i + parseXMLInputOption(args, i);
  272. } else if (args[i].equals("-atin")) {
  273. i = i + parseAreaTreeInputOption(args, i);
  274. } else if (args[i].equals("-ifin")) {
  275. i = i + parseIFInputOption(args, i);
  276. } else if (args[i].equals("-imagein")) {
  277. i = i + parseImageInputOption(args, i);
  278. } else if (args[i].equals("-awt")) {
  279. i = i + parseAWTOutputOption(args, i);
  280. } else if (args[i].equals("-pdf")) {
  281. i = i + parsePDFOutputOption(args, i, null);
  282. } else if (args[i].equals("-pdfa1b")) {
  283. i = i + parsePDFOutputOption(args, i, "PDF/A-1b");
  284. } else if (args[i].equals("-mif")) {
  285. i = i + parseMIFOutputOption(args, i);
  286. } else if (args[i].equals("-rtf")) {
  287. i = i + parseRTFOutputOption(args, i);
  288. } else if (args[i].equals("-tiff")) {
  289. i = i + parseTIFFOutputOption(args, i);
  290. } else if (args[i].equals("-png")) {
  291. i = i + parsePNGOutputOption(args, i);
  292. } else if (args[i].equals("-print")) {
  293. // show print help
  294. if (i + 1 < args.length) {
  295. if (args[i + 1].equals("help")) {
  296. printUsagePrintOutput();
  297. return false;
  298. }
  299. }
  300. i = i + parsePrintOutputOption(args, i);
  301. } else if (args[i].equals("-copies")) {
  302. i = i + parseCopiesOption(args, i);
  303. } else if (args[i].equals("-pcl")) {
  304. i = i + parsePCLOutputOption(args, i);
  305. } else if (args[i].equals("-ps")) {
  306. i = i + parsePostscriptOutputOption(args, i);
  307. } else if (args[i].equals("-txt")) {
  308. i = i + parseTextOutputOption(args, i);
  309. } else if (args[i].equals("-svg")) {
  310. i = i + parseSVGOutputOption(args, i);
  311. } else if (args[i].equals("-afp")) {
  312. i = i + parseAFPOutputOption(args, i);
  313. } else if (args[i].equals("-foout")) {
  314. i = i + parseFOOutputOption(args, i);
  315. } else if (args[i].equals("-out")) {
  316. i = i + parseCustomOutputOption(args, i);
  317. } else if (args[i].equals("-at")) {
  318. i = i + parseAreaTreeOption(args, i);
  319. } else if (args[i].equals("-if")) {
  320. i = i + parseIntermediateFormatOption(args, i);
  321. } else if (args[i].equals("-a")) {
  322. this.renderingOptions.put(Accessibility.ACCESSIBILITY, Boolean.TRUE);
  323. } else if (args[i].equals("-v")) {
  324. /* Currently just print the version */
  325. printVersion();
  326. } else if (args[i].equals("-param")) {
  327. if (i + 2 < args.length) {
  328. String name = args[++i];
  329. String expression = args[++i];
  330. addXSLTParameter(name, expression);
  331. } else {
  332. throw new FOPException("invalid param usage: use -param <name> <value>");
  333. }
  334. } else if (args[i].equals("-catalog")) {
  335. useCatalogResolver = true;
  336. } else if (args[i].equals("-o")) {
  337. i = i + parsePDFOwnerPassword(args, i);
  338. } else if (args[i].equals("-u")) {
  339. i = i + parsePDFUserPassword(args, i);
  340. } else if (args[i].equals("-pdfprofile")) {
  341. i = i + parsePDFProfile(args, i);
  342. } else if (args[i].equals("-noprint")) {
  343. getPDFEncryptionParams().setAllowPrint(false);
  344. } else if (args[i].equals("-nocopy")) {
  345. getPDFEncryptionParams().setAllowCopyContent(false);
  346. } else if (args[i].equals("-noedit")) {
  347. getPDFEncryptionParams().setAllowEditContent(false);
  348. } else if (args[i].equals("-noannotations")) {
  349. getPDFEncryptionParams().setAllowEditAnnotations(false);
  350. } else if (args[i].equals("-version")) {
  351. printVersion();
  352. return false;
  353. } else if (!isOption(args[i])) {
  354. i = i + parseUnknownOption(args, i);
  355. } else {
  356. printUsage(System.err);
  357. System.exit(1);
  358. }
  359. }
  360. return true;
  361. } // end parseOptions
  362. private int parseCacheOption(String[] args, int i) throws FOPException {
  363. if ((i + 1 == args.length)
  364. || (isOption(args[i + 1]))) {
  365. throw new FOPException("if you use '-cache', you must specify "
  366. + "the name of the font cache file");
  367. } else {
  368. factory.getFontManager().setCacheFile(new File(args[i + 1]));
  369. return 1;
  370. }
  371. }
  372. private int parseConfigurationOption(String[] args, int i) throws FOPException {
  373. if ((i + 1 == args.length)
  374. || (isOption(args[i + 1]))) {
  375. throw new FOPException("if you use '-c', you must specify "
  376. + "the name of the configuration file");
  377. } else {
  378. userConfigFile = new File(args[i + 1]);
  379. return 1;
  380. }
  381. }
  382. private int parseLanguageOption(String[] args, int i) throws FOPException {
  383. if ((i + 1 == args.length)
  384. || (isOption(args[i + 1]))) {
  385. throw new FOPException("if you use '-l', you must specify a language");
  386. } else {
  387. Locale.setDefault(new Locale(args[i + 1], ""));
  388. return 1;
  389. }
  390. }
  391. private int parseResolution(String[] args, int i) throws FOPException {
  392. if ((i + 1 == args.length)
  393. || (isOption(args[i + 1]))) {
  394. throw new FOPException(
  395. "if you use '-dpi', you must specify a resolution (dots per inch)");
  396. } else {
  397. this.targetResolution = Integer.parseInt(args[i + 1]);
  398. return 1;
  399. }
  400. }
  401. private int parseFOInputOption(String[] args, int i) throws FOPException {
  402. setInputFormat(FO_INPUT);
  403. if ((i + 1 == args.length)
  404. || (isOption(args[i + 1]))) {
  405. throw new FOPException("you must specify the fo file for the '-fo' option");
  406. } else {
  407. String filename = args[i + 1];
  408. if (isSystemInOutFile(filename)) {
  409. this.useStdIn = true;
  410. } else {
  411. foFile = new File(filename);
  412. }
  413. return 1;
  414. }
  415. }
  416. private int parseXSLInputOption(String[] args, int i) throws FOPException {
  417. setInputFormat(XSLT_INPUT);
  418. if ((i + 1 == args.length)
  419. || (isOption(args[i + 1]))) {
  420. throw new FOPException("you must specify the stylesheet "
  421. + "file for the '-xsl' option");
  422. } else {
  423. xsltFile = new File(args[i + 1]);
  424. return 1;
  425. }
  426. }
  427. private int parseXMLInputOption(String[] args, int i) throws FOPException {
  428. setInputFormat(XSLT_INPUT);
  429. if ((i + 1 == args.length)
  430. || (isOption(args[i + 1]))) {
  431. throw new FOPException("you must specify the input file "
  432. + "for the '-xml' option");
  433. } else {
  434. String filename = args[i + 1];
  435. if (isSystemInOutFile(filename)) {
  436. this.useStdIn = true;
  437. } else {
  438. xmlFile = new File(filename);
  439. }
  440. return 1;
  441. }
  442. }
  443. private int parseAWTOutputOption(String[] args, int i) throws FOPException {
  444. setOutputMode(MimeConstants.MIME_FOP_AWT_PREVIEW);
  445. return 0;
  446. }
  447. private int parsePDFOutputOption(String[] args, int i, String pdfAMode) throws FOPException {
  448. setOutputMode(MimeConstants.MIME_PDF);
  449. if ((i + 1 == args.length)
  450. || (isOption(args[i + 1]))) {
  451. throw new FOPException("you must specify the PDF output file");
  452. } else {
  453. setOutputFile(args[i + 1]);
  454. if (pdfAMode != null) {
  455. if (renderingOptions.get("pdf-a-mode") != null) {
  456. throw new FOPException("PDF/A mode already set");
  457. }
  458. renderingOptions.put("pdf-a-mode", pdfAMode);
  459. }
  460. return 1;
  461. }
  462. }
  463. private void setOutputFile(String filename) {
  464. if (isSystemInOutFile(filename)) {
  465. this.useStdOut = true;
  466. } else {
  467. outFile = new File(filename);
  468. }
  469. }
  470. /**
  471. * Checks whether the given argument is the next option or the specification of
  472. * stdin/stdout.
  473. *
  474. * TODO this is very ad-hoc and should be better handled. Consider the adoption of
  475. * Apache Commons CLI.
  476. *
  477. * @param arg an argument
  478. * @return true if the argument is an option ("-something"), false otherwise
  479. */
  480. private boolean isOption(String arg) {
  481. return arg.length() > 1 && arg.startsWith("-");
  482. }
  483. private boolean isSystemInOutFile(String filename) {
  484. return "-".equals(filename);
  485. }
  486. private int parseMIFOutputOption(String[] args, int i) throws FOPException {
  487. setOutputMode(MimeConstants.MIME_MIF);
  488. if ((i + 1 == args.length)
  489. || (isOption(args[i + 1]))) {
  490. throw new FOPException("you must specify the MIF output file");
  491. } else {
  492. setOutputFile(args[i + 1]);
  493. return 1;
  494. }
  495. }
  496. private int parseRTFOutputOption(String[] args, int i) throws FOPException {
  497. setOutputMode(MimeConstants.MIME_RTF);
  498. if ((i + 1 == args.length)
  499. || (isOption(args[i + 1]))) {
  500. throw new FOPException("you must specify the RTF output file");
  501. } else {
  502. setOutputFile(args[i + 1]);
  503. return 1;
  504. }
  505. }
  506. private int parseTIFFOutputOption(String[] args, int i) throws FOPException {
  507. setOutputMode(MimeConstants.MIME_TIFF);
  508. if ((i + 1 == args.length)
  509. || (isOption(args[i + 1]))) {
  510. throw new FOPException("you must specify the TIFF output file");
  511. } else {
  512. setOutputFile(args[i + 1]);
  513. return 1;
  514. }
  515. }
  516. private int parsePNGOutputOption(String[] args, int i) throws FOPException {
  517. setOutputMode(MimeConstants.MIME_PNG);
  518. if ((i + 1 == args.length)
  519. || (isOption(args[i + 1]))) {
  520. throw new FOPException("you must specify the PNG output file");
  521. } else {
  522. setOutputFile(args[i + 1]);
  523. return 1;
  524. }
  525. }
  526. private int parsePrintOutputOption(String[] args, int i) throws FOPException {
  527. setOutputMode(MimeConstants.MIME_FOP_PRINT);
  528. if ((i + 1 <= args.length)
  529. && (args[i + 1].charAt(0) != '-')) {
  530. String arg = args[i + 1];
  531. String[] parts = arg.split(",");
  532. for (int j = 0; j < parts.length; j++) {
  533. String s = parts[j];
  534. if (s.matches("\\d+")) {
  535. renderingOptions.put(PrintRenderer.START_PAGE, new Integer(s));
  536. } else if (s.matches("\\d+-\\d+")) {
  537. String[] startend = s.split("-");
  538. renderingOptions.put(PrintRenderer.START_PAGE, new Integer(startend[0]));
  539. renderingOptions.put(PrintRenderer.END_PAGE, new Integer(startend[1]));
  540. } else {
  541. PagesMode mode = PagesMode.byName(s);
  542. renderingOptions.put(PrintRenderer.PAGES_MODE, mode);
  543. }
  544. }
  545. return 1;
  546. } else {
  547. return 0;
  548. }
  549. }
  550. private int parseCopiesOption(String[] args, int i) throws FOPException {
  551. if ((i + 1 == args.length)
  552. || (isOption(args[i + 1]))) {
  553. throw new FOPException("you must specify the number of copies");
  554. } else {
  555. renderingOptions.put(PrintRenderer.COPIES, new Integer(args[i + 1]));
  556. return 1;
  557. }
  558. }
  559. private int parsePCLOutputOption(String[] args, int i) throws FOPException {
  560. setOutputMode(MimeConstants.MIME_PCL);
  561. if ((i + 1 == args.length)
  562. || (isOption(args[i + 1]))) {
  563. throw new FOPException("you must specify the PDF output file");
  564. } else {
  565. setOutputFile(args[i + 1]);
  566. return 1;
  567. }
  568. }
  569. private int parsePostscriptOutputOption(String[] args, int i) throws FOPException {
  570. setOutputMode(MimeConstants.MIME_POSTSCRIPT);
  571. if ((i + 1 == args.length)
  572. || (isOption(args[i + 1]))) {
  573. throw new FOPException("you must specify the PostScript output file");
  574. } else {
  575. setOutputFile(args[i + 1]);
  576. return 1;
  577. }
  578. }
  579. private int parseTextOutputOption(String[] args, int i) throws FOPException {
  580. setOutputMode(MimeConstants.MIME_PLAIN_TEXT);
  581. if ((i + 1 == args.length)
  582. || (isOption(args[i + 1]))) {
  583. throw new FOPException("you must specify the text output file");
  584. } else {
  585. setOutputFile(args[i + 1]);
  586. return 1;
  587. }
  588. }
  589. private int parseSVGOutputOption(String[] args, int i) throws FOPException {
  590. setOutputMode(MimeConstants.MIME_SVG);
  591. if ((i + 1 == args.length)
  592. || (isOption(args[i + 1]))) {
  593. throw new FOPException("you must specify the SVG output file");
  594. } else {
  595. setOutputFile(args[i + 1]);
  596. return 1;
  597. }
  598. }
  599. private int parseAFPOutputOption(String[] args, int i) throws FOPException {
  600. setOutputMode(MimeConstants.MIME_AFP);
  601. if ((i + 1 == args.length)
  602. || (isOption(args[i + 1]))) {
  603. throw new FOPException("you must specify the AFP output file");
  604. } else {
  605. setOutputFile(args[i + 1]);
  606. return 1;
  607. }
  608. }
  609. private int parseFOOutputOption(String[] args, int i) throws FOPException {
  610. setOutputMode(MimeConstants.MIME_XSL_FO);
  611. if ((i + 1 == args.length)
  612. || (isOption(args[i + 1]))) {
  613. throw new FOPException("you must specify the FO output file");
  614. } else {
  615. setOutputFile(args[i + 1]);
  616. return 1;
  617. }
  618. }
  619. private int parseCustomOutputOption(String[] args, int i) throws FOPException {
  620. String mime = null;
  621. if ((i + 1 < args.length)
  622. || (args[i + 1].charAt(0) != '-')) {
  623. mime = args[i + 1];
  624. if ("list".equals(mime)) {
  625. String[] mimes = factory.getRendererFactory().listSupportedMimeTypes();
  626. System.out.println("Supported MIME types:");
  627. for (int j = 0; j < mimes.length; j++) {
  628. System.out.println(" " + mimes[j]);
  629. }
  630. System.exit(0);
  631. }
  632. }
  633. if ((i + 2 >= args.length)
  634. || (isOption(args[i + 1]))
  635. || (isOption(args[i + 2]))) {
  636. throw new FOPException("you must specify the output format and the output file");
  637. } else {
  638. setOutputMode(mime);
  639. setOutputFile(args[i + 2]);
  640. return 2;
  641. }
  642. }
  643. private int parseUnknownOption(String[] args, int i) throws FOPException {
  644. if (inputMode == NOT_SET) {
  645. inputMode = FO_INPUT;
  646. String filename = args[i];
  647. if (isSystemInOutFile(filename)) {
  648. this.useStdIn = true;
  649. } else {
  650. foFile = new File(filename);
  651. }
  652. } else if (outputMode == null) {
  653. outputMode = MimeConstants.MIME_PDF;
  654. setOutputFile(args[i]);
  655. } else {
  656. throw new FOPException("Don't know what to do with "
  657. + args[i]);
  658. }
  659. return 0;
  660. }
  661. private int parseAreaTreeOption(String[] args, int i) throws FOPException {
  662. setOutputMode(MimeConstants.MIME_FOP_AREA_TREE);
  663. if ((i + 1 == args.length)
  664. || (isOption(args[i + 1]))) {
  665. throw new FOPException("you must specify the area-tree output file");
  666. } else if ((i + 2 == args.length)
  667. || (isOption(args[i + 2]))) {
  668. // only output file is specified
  669. setOutputFile(args[i + 1]);
  670. return 1;
  671. } else {
  672. // mimic format and output file have been specified
  673. mimicRenderer = args[i + 1];
  674. setOutputFile(args[i + 2]);
  675. return 2;
  676. }
  677. }
  678. private int parseIntermediateFormatOption(String[] args, int i) throws FOPException {
  679. setOutputMode(MimeConstants.MIME_FOP_IF);
  680. if ((i + 1 == args.length)
  681. || (args[i + 1].charAt(0) == '-')) {
  682. throw new FOPException("you must specify the intermediate format output file");
  683. } else if ((i + 2 == args.length)
  684. || (args[i + 2].charAt(0) == '-')) {
  685. // only output file is specified
  686. setOutputFile(args[i + 1]);
  687. return 1;
  688. } else {
  689. // mimic format and output file have been specified
  690. mimicRenderer = args[i + 1];
  691. setOutputFile(args[i + 2]);
  692. return 2;
  693. }
  694. }
  695. private int parseAreaTreeInputOption(String[] args, int i) throws FOPException {
  696. setInputFormat(AREATREE_INPUT);
  697. if ((i + 1 == args.length)
  698. || (isOption(args[i + 1]))) {
  699. throw new FOPException("you must specify the Area Tree file for the '-atin' option");
  700. } else {
  701. String filename = args[i + 1];
  702. if (isSystemInOutFile(filename)) {
  703. this.useStdIn = true;
  704. } else {
  705. areatreeFile = new File(filename);
  706. }
  707. return 1;
  708. }
  709. }
  710. private int parseIFInputOption(String[] args, int i) throws FOPException {
  711. setInputFormat(IF_INPUT);
  712. if ((i + 1 == args.length)
  713. || (isOption(args[i + 1]))) {
  714. throw new FOPException("you must specify the intermediate file for the '-ifin' option");
  715. } else {
  716. String filename = args[i + 1];
  717. if (isSystemInOutFile(filename)) {
  718. this.useStdIn = true;
  719. } else {
  720. ifFile = new File(filename);
  721. }
  722. return 1;
  723. }
  724. }
  725. private int parseImageInputOption(String[] args, int i) throws FOPException {
  726. setInputFormat(IMAGE_INPUT);
  727. if ((i + 1 == args.length)
  728. || (isOption(args[i + 1]))) {
  729. throw new FOPException("you must specify the image file for the '-imagein' option");
  730. } else {
  731. String filename = args[i + 1];
  732. if (isSystemInOutFile(filename)) {
  733. this.useStdIn = true;
  734. } else {
  735. imageFile = new File(filename);
  736. }
  737. return 1;
  738. }
  739. }
  740. private PDFEncryptionParams getPDFEncryptionParams() throws FOPException {
  741. PDFEncryptionParams params = (PDFEncryptionParams)renderingOptions.get(
  742. PDFConfigurationConstants.ENCRYPTION_PARAMS);
  743. if (params == null) {
  744. if (!PDFEncryptionManager.checkAvailableAlgorithms()) {
  745. throw new FOPException("PDF encryption requested but it is not available."
  746. + " Please make sure MD5 and RC4 algorithms are available.");
  747. }
  748. params = new PDFEncryptionParams();
  749. renderingOptions.put(PDFConfigurationConstants.ENCRYPTION_PARAMS, params);
  750. }
  751. return params;
  752. }
  753. private int parsePDFOwnerPassword(String[] args, int i) throws FOPException {
  754. if ((i + 1 == args.length)
  755. || (isOption(args[i + 1]))) {
  756. getPDFEncryptionParams().setOwnerPassword("");
  757. return 0;
  758. } else {
  759. getPDFEncryptionParams().setOwnerPassword(args[i + 1]);
  760. return 1;
  761. }
  762. }
  763. private int parsePDFUserPassword(String[] args, int i) throws FOPException {
  764. if ((i + 1 == args.length)
  765. || (isOption(args[i + 1]))) {
  766. getPDFEncryptionParams().setUserPassword("");
  767. return 0;
  768. } else {
  769. getPDFEncryptionParams().setUserPassword(args[i + 1]);
  770. return 1;
  771. }
  772. }
  773. private int parsePDFProfile(String[] args, int i) throws FOPException {
  774. if ((i + 1 == args.length)
  775. || (isOption(args[i + 1]))) {
  776. throw new FOPException("You must specify a PDF profile");
  777. } else {
  778. String profile = args[i + 1];
  779. PDFAMode pdfAMode = PDFAMode.valueOf(profile);
  780. if (pdfAMode != null && pdfAMode != PDFAMode.DISABLED) {
  781. if (renderingOptions.get("pdf-a-mode") != null) {
  782. throw new FOPException("PDF/A mode already set");
  783. }
  784. renderingOptions.put("pdf-a-mode", pdfAMode.getName());
  785. return 1;
  786. } else {
  787. PDFXMode pdfXMode = PDFXMode.valueOf(profile);
  788. if (pdfXMode != null && pdfXMode != PDFXMode.DISABLED) {
  789. if (renderingOptions.get("pdf-x-mode") != null) {
  790. throw new FOPException("PDF/X mode already set");
  791. }
  792. renderingOptions.put("pdf-x-mode", pdfXMode.getName());
  793. return 1;
  794. }
  795. }
  796. throw new FOPException("Unsupported PDF profile: " + profile);
  797. }
  798. }
  799. private void setOutputMode(String mime) throws FOPException {
  800. if (outputMode == null) {
  801. outputMode = mime;
  802. } else {
  803. throw new FOPException("you can only set one output method");
  804. }
  805. }
  806. private void setLogOption (String option, String level) {
  807. if (log instanceof CommandLineLogger
  808. || System.getProperty("org.apache.commons.logging.Log") == null) {
  809. setLogLevel(level);
  810. } else if (log != null) {
  811. log.warn("The option " + option + " can only be used");
  812. log.warn("with FOP's command line logger,");
  813. log.warn("which is the default on the command line.");
  814. log.warn("Configure other loggers using Java system properties.");
  815. }
  816. }
  817. private void setLogLevel(String level) {
  818. // Set the level for future loggers.
  819. LogFactory.getFactory().setAttribute("level", level);
  820. if (log instanceof CommandLineLogger) {
  821. // Set the level for the logger created already.
  822. ((CommandLineLogger) log).setLogLevel(level);
  823. }
  824. }
  825. private void setInputFormat(int format) throws FOPException {
  826. if (inputMode == NOT_SET || inputMode == format) {
  827. inputMode = format;
  828. } else {
  829. throw new FOPException("Only one input mode can be specified!");
  830. }
  831. }
  832. /**
  833. * checks whether all necessary information has been given in a consistent way
  834. */
  835. private void checkSettings() throws FOPException, FileNotFoundException {
  836. if (inputMode == NOT_SET) {
  837. throw new FOPException("No input file specified");
  838. }
  839. if (outputMode == null) {
  840. throw new FOPException("No output file specified");
  841. }
  842. if ((outputMode.equals(MimeConstants.MIME_FOP_AWT_PREVIEW)
  843. || outputMode.equals(MimeConstants.MIME_FOP_PRINT))
  844. && outFile != null) {
  845. throw new FOPException("Output file may not be specified "
  846. + "for AWT or PRINT output");
  847. }
  848. if (inputMode == XSLT_INPUT) {
  849. // check whether xml *and* xslt file have been set
  850. if (xmlFile == null && !this.useStdIn) {
  851. throw new FOPException("XML file must be specified for the transform mode");
  852. }
  853. if (xsltFile == null) {
  854. throw new FOPException("XSLT file must be specified for the transform mode");
  855. }
  856. // warning if fofile has been set in xslt mode
  857. if (foFile != null) {
  858. log.warn("Can't use fo file with transform mode! Ignoring.\n"
  859. + "Your input is " + "\n xmlfile: "
  860. + xmlFile.getAbsolutePath()
  861. + "\nxsltfile: "
  862. + xsltFile.getAbsolutePath()
  863. + "\n fofile: "
  864. + foFile.getAbsolutePath());
  865. }
  866. if (xmlFile != null && !xmlFile.exists()) {
  867. throw new FileNotFoundException("Error: xml file "
  868. + xmlFile.getAbsolutePath()
  869. + " not found ");
  870. }
  871. if (!xsltFile.exists()) {
  872. throw new FileNotFoundException("Error: xsl file "
  873. + xsltFile.getAbsolutePath()
  874. + " not found ");
  875. }
  876. } else if (inputMode == FO_INPUT) {
  877. if (outputMode.equals(MimeConstants.MIME_XSL_FO)) {
  878. throw new FOPException(
  879. "FO output mode is only available if you use -xml and -xsl");
  880. }
  881. if (foFile != null && !foFile.exists()) {
  882. throw new FileNotFoundException("Error: fo file "
  883. + foFile.getAbsolutePath()
  884. + " not found ");
  885. }
  886. } else if (inputMode == AREATREE_INPUT) {
  887. if (outputMode.equals(MimeConstants.MIME_XSL_FO)) {
  888. throw new FOPException(
  889. "FO output mode is only available if you use -xml and -xsl");
  890. } else if (outputMode.equals(MimeConstants.MIME_FOP_AREA_TREE)) {
  891. throw new FOPException(
  892. "Area Tree Output is not available if Area Tree is used as input!");
  893. }
  894. if (areatreeFile != null && !areatreeFile.exists()) {
  895. throw new FileNotFoundException("Error: area tree file "
  896. + areatreeFile.getAbsolutePath()
  897. + " not found ");
  898. }
  899. } else if (inputMode == IF_INPUT) {
  900. if (outputMode.equals(MimeConstants.MIME_XSL_FO)) {
  901. throw new FOPException(
  902. "FO output mode is only available if you use -xml and -xsl");
  903. } else if (outputMode.equals(MimeConstants.MIME_FOP_AREA_TREE)) {
  904. throw new FOPException(
  905. "Area Tree Output is not available if Intermediate Format"
  906. + " is used as input!");
  907. } else if (outputMode.equals(MimeConstants.MIME_FOP_IF)) {
  908. throw new FOPException(
  909. "Intermediate Output is not available if Intermediate Format"
  910. + " is used as input!");
  911. }
  912. if (ifFile != null && !ifFile.exists()) {
  913. throw new FileNotFoundException("Error: intermediate format file "
  914. + ifFile.getAbsolutePath()
  915. + " not found ");
  916. }
  917. } else if (inputMode == IMAGE_INPUT) {
  918. if (outputMode.equals(MimeConstants.MIME_XSL_FO)) {
  919. throw new FOPException(
  920. "FO output mode is only available if you use -xml and -xsl");
  921. }
  922. if (imageFile != null && !imageFile.exists()) {
  923. throw new FileNotFoundException("Error: image file "
  924. + imageFile.getAbsolutePath()
  925. + " not found ");
  926. }
  927. }
  928. } // end checkSettings
  929. /**
  930. * Sets the user configuration.
  931. * @throws FOPException if creating the user configuration fails
  932. * @throws IOException
  933. */
  934. private void setUserConfig() throws FOPException, IOException {
  935. if (userConfigFile == null) {
  936. return;
  937. }
  938. try {
  939. factory.setUserConfig(userConfigFile);
  940. } catch (SAXException e) {
  941. throw new FOPException(e);
  942. }
  943. }
  944. /**
  945. * @return the chosen output format (MIME type)
  946. * @throws FOPException for invalid output formats
  947. */
  948. protected String getOutputFormat() throws FOPException {
  949. if (outputMode == null) {
  950. throw new FOPException("Renderer has not been set!");
  951. }
  952. if (outputMode.equals(MimeConstants.MIME_FOP_AREA_TREE)) {
  953. renderingOptions.put("fineDetail", isCoarseAreaXml());
  954. }
  955. return outputMode;
  956. }
  957. /**
  958. * Create an InputHandler object based on command-line parameters
  959. * @return a new InputHandler instance
  960. * @throws IllegalArgumentException if invalid/missing parameters
  961. */
  962. private InputHandler createInputHandler() {
  963. switch (inputMode) {
  964. case FO_INPUT:
  965. return new InputHandler(foFile);
  966. case AREATREE_INPUT:
  967. return new AreaTreeInputHandler(areatreeFile);
  968. case IF_INPUT:
  969. return new IFInputHandler(ifFile);
  970. case XSLT_INPUT:
  971. InputHandler handler = new InputHandler(xmlFile, xsltFile, xsltParams);
  972. if (useCatalogResolver) {
  973. handler.createCatalogResolver(foUserAgent);
  974. }
  975. return handler;
  976. case IMAGE_INPUT:
  977. return new ImageInputHandler(imageFile, xsltFile, xsltParams);
  978. default:
  979. throw new IllegalArgumentException("Error creating InputHandler object.");
  980. }
  981. }
  982. /**
  983. * Get the FOUserAgent for this Command-Line run
  984. * @return FOUserAgent instance
  985. */
  986. protected FOUserAgent getFOUserAgent() {
  987. return foUserAgent;
  988. }
  989. /**
  990. * Returns the XSL-FO file if set.
  991. * @return the XSL-FO file, null if not set
  992. */
  993. public File getFoFile() {
  994. return foFile;
  995. }
  996. /**
  997. * Returns the input XML file if set.
  998. * @return the input XML file, null if not set
  999. */
  1000. public File getXmlFile() {
  1001. return xmlFile;
  1002. }
  1003. /**
  1004. * Returns the stylesheet to be used for transformation to XSL-FO.
  1005. * @return stylesheet
  1006. */
  1007. public File getXslFile() {
  1008. return xsltFile;
  1009. }
  1010. /**
  1011. * Returns the output file
  1012. * @return the output file
  1013. */
  1014. public File getOutputFile() {
  1015. return outFile;
  1016. }
  1017. /**
  1018. * Returns the user configuration file to be used.
  1019. * @return the userconfig.xml file
  1020. */
  1021. public File getUserConfigFile() {
  1022. return userConfigFile;
  1023. }
  1024. /**
  1025. * Indicates whether the XML renderer should generate coarse area XML
  1026. * @return true if coarse area XML is desired
  1027. */
  1028. public Boolean isCoarseAreaXml() {
  1029. return suppressLowLevelAreas;
  1030. }
  1031. /**
  1032. * Indicates whether input comes from standard input (stdin).
  1033. * @return true if input comes from standard input (stdin)
  1034. */
  1035. public boolean isInputFromStdIn() {
  1036. return this.useStdIn;
  1037. }
  1038. /**
  1039. * Indicates whether output is sent to standard output (stdout).
  1040. * @return true if output is sent to standard output (stdout)
  1041. */
  1042. public boolean isOutputToStdOut() {
  1043. return this.useStdOut;
  1044. }
  1045. /**
  1046. * Returns the input file.
  1047. * @return either the fofile or the xmlfile
  1048. */
  1049. public File getInputFile() {
  1050. switch (inputMode) {
  1051. case FO_INPUT:
  1052. return foFile;
  1053. case XSLT_INPUT:
  1054. return xmlFile;
  1055. default:
  1056. return foFile;
  1057. }
  1058. }
  1059. private static void printVersion() {
  1060. System.out.println("FOP Version " + Version.getVersion());
  1061. }
  1062. /**
  1063. * Shows the command line syntax including a summary of all available options and some
  1064. * examples.
  1065. *
  1066. * @param out the stream to which the message must be printed
  1067. */
  1068. public static void printUsage(PrintStream out) {
  1069. out.println(
  1070. "\nUSAGE\nfop [options] [-fo|-xml] infile [-xsl file] "
  1071. + "[-awt|-pdf|-mif|-rtf|-tiff|-png|-pcl|-ps|-txt|-at [mime]|-print] <outfile>\n"
  1072. + " [OPTIONS] \n"
  1073. + " -version print FOP version and exit\n"
  1074. + " -d debug mode \n"
  1075. + " -x dump configuration settings \n"
  1076. + " -q quiet mode \n"
  1077. + " -c cfg.xml use additional configuration file cfg.xml\n"
  1078. + " -l lang the language to use for user information \n"
  1079. + " -r relaxed/less strict validation (where available)\n"
  1080. + " -dpi xxx target resolution in dots per inch (dpi) where xxx is a number\n"
  1081. + " -s for area tree XML, down to block areas only\n"
  1082. + " -v run in verbose mode (currently simply print FOP version"
  1083. + " and continue)\n\n"
  1084. + " -o [password] PDF file will be encrypted with option owner password\n"
  1085. + " -u [password] PDF file will be encrypted with option user password\n"
  1086. + " -noprint PDF file will be encrypted without printing permission\n"
  1087. + " -nocopy PDF file will be encrypted without copy content permission\n"
  1088. + " -noedit PDF file will be encrypted without edit content permission\n"
  1089. + " -noannotations PDF file will be encrypted without edit annotation permission\n"
  1090. + " -a enables accessibility features (Tagged PDF etc., default off)\n"
  1091. + " -pdfprofile prof PDF file will be generated with the specified profile\n"
  1092. + " (Examples for prof: PDF/A-1b or PDF/X-3:2003)\n\n"
  1093. + " -conserve enable memory-conservation policy (trades memory-consumption"
  1094. + " for disk I/O)\n"
  1095. + " (Note: currently only influences whether the area tree is"
  1096. + " serialized.)\n\n"
  1097. + " -cache specifies a file/directory path location"
  1098. + " for the font cache file\n"
  1099. + " -flush flushes the current font cache file\n\n"
  1100. + " [INPUT] \n"
  1101. + " infile xsl:fo input file (the same as the next) \n"
  1102. + " (use '-' for infile to pipe input from stdin)\n"
  1103. + " -fo infile xsl:fo input file \n"
  1104. + " -xml infile xml input file, must be used together with -xsl \n"
  1105. + " -atin infile area tree input file \n"
  1106. + " -ifin infile intermediate format input file \n"
  1107. + " -imagein infile image input file (piping through stdin not supported)\n"
  1108. + " -xsl stylesheet xslt stylesheet \n \n"
  1109. + " -param name value <value> to use for parameter <name> in xslt stylesheet\n"
  1110. + " (repeat '-param name value' for each parameter)\n \n"
  1111. + " -catalog use catalog resolver for input XML and XSLT files\n"
  1112. + " [OUTPUT] \n"
  1113. + " outfile input will be rendered as PDF into outfile\n"
  1114. + " (use '-' for outfile to pipe output to stdout)\n"
  1115. + " -pdf outfile input will be rendered as PDF (outfile req'd)\n"
  1116. + " -pdfa1b outfile input will be rendered as PDF/A-1b compliant PDF\n"
  1117. + " (outfile req'd, same as \"-pdf outfile -pdfprofile PDF/A-1b\")\n"
  1118. + " -awt input will be displayed on screen \n"
  1119. + " -rtf outfile input will be rendered as RTF (outfile req'd)\n"
  1120. + " -pcl outfile input will be rendered as PCL (outfile req'd) \n"
  1121. + " -ps outfile input will be rendered as PostScript (outfile req'd) \n"
  1122. + " -afp outfile input will be rendered as AFP (outfile req'd)\n"
  1123. + " -tiff outfile input will be rendered as TIFF (outfile req'd)\n"
  1124. + " -png outfile input will be rendered as PNG (outfile req'd)\n"
  1125. + " -txt outfile input will be rendered as plain text (outfile req'd) \n"
  1126. + " -at [mime] out representation of area tree as XML (outfile req'd) \n"
  1127. + " specify optional mime output to allow the AT to be converted\n"
  1128. + " to final format later\n"
  1129. + " -if [mime] out representation of document in intermediate format XML"
  1130. + " (outfile req'd)\n"
  1131. + " specify optional mime output to allow the IF to be converted\n"
  1132. + " to final format later\n"
  1133. + " -print input file will be rendered and sent to the printer \n"
  1134. + " see options with \"-print help\" \n"
  1135. + " -out mime outfile input will be rendered using the given MIME type\n"
  1136. + " (outfile req'd) Example: \"-out application/pdf D:\\out.pdf\"\n"
  1137. + " (Tip: \"-out list\" prints the list of supported MIME types"
  1138. + " and exits)\n"
  1139. //+ " -mif outfile input will be rendered as MIF (FrameMaker) (outfile req'd)\n"
  1140. //+ " Experimental feature - requires additional fop-sandbox.jar.\n"
  1141. + " -svg outfile input will be rendered as an SVG slides file (outfile req'd) \n"
  1142. + " Experimental feature - requires additional fop-sandbox.jar.\n"
  1143. + "\n"
  1144. + " -foout outfile input will only be XSL transformed. The intermediate \n"
  1145. + " XSL-FO file is saved and no rendering is performed. \n"
  1146. + " (Only available if you use -xml and -xsl parameters)\n\n"
  1147. + "\n"
  1148. + " [Examples]\n" + " fop foo.fo foo.pdf \n"
  1149. + " fop -fo foo.fo -pdf foo.pdf (does the same as the previous line)\n"
  1150. + " fop -xml foo.xml -xsl foo.xsl -pdf foo.pdf\n"
  1151. + " fop -xml foo.xml -xsl foo.xsl -foout foo.fo\n"
  1152. + " fop -xml - -xsl foo.xsl -pdf -\n"
  1153. + " fop foo.fo -mif foo.mif\n"
  1154. + " fop foo.fo -rtf foo.rtf\n"
  1155. + " fop foo.fo -print\n"
  1156. + " fop foo.fo -awt\n");
  1157. }
  1158. /**
  1159. * shows the options for print output
  1160. */
  1161. private void printUsagePrintOutput() {
  1162. System.err.println("USAGE: -print [from[-to][,even|odd]] [-copies numCopies]\n\n"
  1163. + "Example:\n"
  1164. + "all pages: fop infile.fo -print\n"
  1165. + "all pages with two copies: fop infile.fo -print -copies 2\n"
  1166. + "all pages starting with page 7: fop infile.fo -print 7\n"
  1167. + "pages 2 to 3: fop infile.fo -print 2-3\n"
  1168. + "only even page between 10 and 20: fop infile.fo -print 10-20,even\n");
  1169. }
  1170. /**
  1171. * Outputs all commandline settings
  1172. */
  1173. private void dumpConfiguration() {
  1174. log.info("Input mode: ");
  1175. switch (inputMode) {
  1176. case NOT_SET:
  1177. log.info("not set");
  1178. break;
  1179. case FO_INPUT:
  1180. log.info("FO ");
  1181. if (isInputFromStdIn()) {
  1182. log.info("fo input file: from stdin");
  1183. } else {
  1184. log.info("fo input file: " + foFile.toString());
  1185. }
  1186. break;
  1187. case XSLT_INPUT:
  1188. log.info("xslt transformation");
  1189. if (isInputFromStdIn()) {
  1190. log.info("xml input file: from stdin");
  1191. } else {
  1192. log.info("xml input file: " + xmlFile.toString());
  1193. }
  1194. log.info("xslt stylesheet: " + xsltFile.toString());
  1195. break;
  1196. case AREATREE_INPUT:
  1197. log.info("AT ");
  1198. if (isInputFromStdIn()) {
  1199. log.info("area tree input file: from stdin");
  1200. } else {
  1201. log.info("area tree input file: " + areatreeFile.toString());
  1202. }
  1203. break;
  1204. case IF_INPUT:
  1205. log.info("IF ");
  1206. if (isInputFromStdIn()) {
  1207. log.info("intermediate input file: from stdin");
  1208. } else {
  1209. log.info("intermediate input file: " + ifFile.toString());
  1210. }
  1211. break;
  1212. case IMAGE_INPUT:
  1213. log.info("Image ");
  1214. if (isInputFromStdIn()) {
  1215. log.info("image input file: from stdin");
  1216. } else {
  1217. log.info("image input file: " + imageFile.toString());
  1218. }
  1219. break;
  1220. default:
  1221. log.info("unknown input type");
  1222. }
  1223. log.info("Output mode: ");
  1224. if (outputMode == null) {
  1225. log.info("not set");
  1226. } else if (MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputMode)) {
  1227. log.info("awt on screen");
  1228. if (outFile != null) {
  1229. log.error("awt mode, but outfile is set:");
  1230. log.error("out file: " + outFile.toString());
  1231. }
  1232. } else if (MimeConstants.MIME_FOP_PRINT.equals(outputMode)) {
  1233. log.info("print directly");
  1234. if (outFile != null) {
  1235. log.error("print mode, but outfile is set:");
  1236. log.error("out file: " + outFile.toString());
  1237. }
  1238. } else if (MimeConstants.MIME_FOP_AREA_TREE.equals(outputMode)) {
  1239. log.info("area tree");
  1240. if (mimicRenderer != null) {
  1241. log.info("mimic renderer: " + mimicRenderer);
  1242. }
  1243. if (isOutputToStdOut()) {
  1244. log.info("output file: to stdout");
  1245. } else {
  1246. log.info("output file: " + outFile.toString());
  1247. }
  1248. } else if (MimeConstants.MIME_FOP_IF.equals(outputMode)) {
  1249. log.info("intermediate format");
  1250. log.info("output file: " + outFile.toString());
  1251. } else {
  1252. log.info(outputMode);
  1253. if (isOutputToStdOut()) {
  1254. log.info("output file: to stdout");
  1255. } else {
  1256. log.info("output file: " + outFile.toString());
  1257. }
  1258. }
  1259. log.info("OPTIONS");
  1260. if (userConfigFile != null) {
  1261. log.info("user configuration file: "
  1262. + userConfigFile.toString());
  1263. } else {
  1264. log.info("no user configuration file is used [default]");
  1265. }
  1266. }
  1267. private void flushCache() throws FOPException {
  1268. FontManager fontManager = factory.getFontManager();
  1269. File cacheFile = fontManager.getCacheFile();
  1270. if (!fontManager.deleteCache()) {
  1271. System.err.println("Failed to flush the font cache file '"
  1272. + cacheFile + "'.");
  1273. System.exit(1);
  1274. }
  1275. }
  1276. }