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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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("-nofillinforms")) {
  351. getPDFEncryptionParams().setAllowFillInForms(false);
  352. } else if (args[i].equals("-noaccesscontent")) {
  353. getPDFEncryptionParams().setAllowAccessContent(false);
  354. } else if (args[i].equals("-noassembledoc")) {
  355. getPDFEncryptionParams().setAllowAssembleDocument(false);
  356. } else if (args[i].equals("-noprinthq")) {
  357. getPDFEncryptionParams().setAllowPrintHq(false);
  358. } else if (args[i].equals("-version")) {
  359. printVersion();
  360. return false;
  361. } else if (!isOption(args[i])) {
  362. i = i + parseUnknownOption(args, i);
  363. } else {
  364. printUsage(System.err);
  365. System.exit(1);
  366. }
  367. }
  368. return true;
  369. } // end parseOptions
  370. private int parseCacheOption(String[] args, int i) throws FOPException {
  371. if ((i + 1 == args.length)
  372. || (isOption(args[i + 1]))) {
  373. throw new FOPException("if you use '-cache', you must specify "
  374. + "the name of the font cache file");
  375. } else {
  376. factory.getFontManager().setCacheFile(new File(args[i + 1]));
  377. return 1;
  378. }
  379. }
  380. private int parseConfigurationOption(String[] args, int i) throws FOPException {
  381. if ((i + 1 == args.length)
  382. || (isOption(args[i + 1]))) {
  383. throw new FOPException("if you use '-c', you must specify "
  384. + "the name of the configuration file");
  385. } else {
  386. userConfigFile = new File(args[i + 1]);
  387. return 1;
  388. }
  389. }
  390. private int parseLanguageOption(String[] args, int i) throws FOPException {
  391. if ((i + 1 == args.length)
  392. || (isOption(args[i + 1]))) {
  393. throw new FOPException("if you use '-l', you must specify a language");
  394. } else {
  395. Locale.setDefault(new Locale(args[i + 1], ""));
  396. return 1;
  397. }
  398. }
  399. private int parseResolution(String[] args, int i) throws FOPException {
  400. if ((i + 1 == args.length)
  401. || (isOption(args[i + 1]))) {
  402. throw new FOPException(
  403. "if you use '-dpi', you must specify a resolution (dots per inch)");
  404. } else {
  405. this.targetResolution = Integer.parseInt(args[i + 1]);
  406. return 1;
  407. }
  408. }
  409. private int parseFOInputOption(String[] args, int i) throws FOPException {
  410. setInputFormat(FO_INPUT);
  411. if ((i + 1 == args.length)
  412. || (isOption(args[i + 1]))) {
  413. throw new FOPException("you must specify the fo file for the '-fo' option");
  414. } else {
  415. String filename = args[i + 1];
  416. if (isSystemInOutFile(filename)) {
  417. this.useStdIn = true;
  418. } else {
  419. fofile = new File(filename);
  420. }
  421. return 1;
  422. }
  423. }
  424. private int parseXSLInputOption(String[] args, int i) throws FOPException {
  425. setInputFormat(XSLT_INPUT);
  426. if ((i + 1 == args.length)
  427. || (isOption(args[i + 1]))) {
  428. throw new FOPException("you must specify the stylesheet "
  429. + "file for the '-xsl' option");
  430. } else {
  431. xsltfile = new File(args[i + 1]);
  432. return 1;
  433. }
  434. }
  435. private int parseXMLInputOption(String[] args, int i) throws FOPException {
  436. setInputFormat(XSLT_INPUT);
  437. if ((i + 1 == args.length)
  438. || (isOption(args[i + 1]))) {
  439. throw new FOPException("you must specify the input file "
  440. + "for the '-xml' option");
  441. } else {
  442. String filename = args[i + 1];
  443. if (isSystemInOutFile(filename)) {
  444. this.useStdIn = true;
  445. } else {
  446. xmlfile = new File(filename);
  447. }
  448. return 1;
  449. }
  450. }
  451. private int parseAWTOutputOption(String[] args, int i) throws FOPException {
  452. setOutputMode(MimeConstants.MIME_FOP_AWT_PREVIEW);
  453. return 0;
  454. }
  455. private int parsePDFOutputOption(String[] args, int i, String pdfAMode) throws FOPException {
  456. setOutputMode(MimeConstants.MIME_PDF);
  457. if ((i + 1 == args.length)
  458. || (isOption(args[i + 1]))) {
  459. throw new FOPException("you must specify the PDF output file");
  460. } else {
  461. setOutputFile(args[i + 1]);
  462. if (pdfAMode != null) {
  463. if (renderingOptions.get("pdf-a-mode") != null) {
  464. throw new FOPException("PDF/A mode already set");
  465. }
  466. renderingOptions.put("pdf-a-mode", pdfAMode);
  467. }
  468. return 1;
  469. }
  470. }
  471. private void setOutputFile(String filename) {
  472. if (isSystemInOutFile(filename)) {
  473. this.useStdOut = true;
  474. } else {
  475. outfile = new File(filename);
  476. }
  477. }
  478. /**
  479. * Checks whether the given argument is the next option or the specification of
  480. * stdin/stdout.
  481. *
  482. * TODO this is very ad-hoc and should be better handled. Consider the adoption of
  483. * Apache Commons CLI.
  484. *
  485. * @param arg an argument
  486. * @return true if the argument is an option ("-something"), false otherwise
  487. */
  488. private boolean isOption(String arg) {
  489. return arg.length() > 1 && arg.startsWith("-");
  490. }
  491. private boolean isSystemInOutFile(String filename) {
  492. return "-".equals(filename);
  493. }
  494. private int parseMIFOutputOption(String[] args, int i) throws FOPException {
  495. setOutputMode(MimeConstants.MIME_MIF);
  496. if ((i + 1 == args.length)
  497. || (isOption(args[i + 1]))) {
  498. throw new FOPException("you must specify the MIF output file");
  499. } else {
  500. setOutputFile(args[i + 1]);
  501. return 1;
  502. }
  503. }
  504. private int parseRTFOutputOption(String[] args, int i) throws FOPException {
  505. setOutputMode(MimeConstants.MIME_RTF);
  506. if ((i + 1 == args.length)
  507. || (isOption(args[i + 1]))) {
  508. throw new FOPException("you must specify the RTF output file");
  509. } else {
  510. setOutputFile(args[i + 1]);
  511. return 1;
  512. }
  513. }
  514. private int parseTIFFOutputOption(String[] args, int i) throws FOPException {
  515. setOutputMode(MimeConstants.MIME_TIFF);
  516. if ((i + 1 == args.length)
  517. || (isOption(args[i + 1]))) {
  518. throw new FOPException("you must specify the TIFF output file");
  519. } else {
  520. setOutputFile(args[i + 1]);
  521. return 1;
  522. }
  523. }
  524. private int parsePNGOutputOption(String[] args, int i) throws FOPException {
  525. setOutputMode(MimeConstants.MIME_PNG);
  526. if ((i + 1 == args.length)
  527. || (isOption(args[i + 1]))) {
  528. throw new FOPException("you must specify the PNG output file");
  529. } else {
  530. setOutputFile(args[i + 1]);
  531. return 1;
  532. }
  533. }
  534. private int parsePrintOutputOption(String[] args, int i) throws FOPException {
  535. setOutputMode(MimeConstants.MIME_FOP_PRINT);
  536. if ((i + 1 <= args.length)
  537. && (args[i + 1].charAt(0) != '-')) {
  538. String arg = args[i + 1];
  539. String[] parts = arg.split(",");
  540. for (int j = 0; j < parts.length; j++) {
  541. String s = parts[j];
  542. if (s.matches("\\d+")) {
  543. renderingOptions.put(PrintRenderer.START_PAGE, new Integer(s));
  544. } else if (s.matches("\\d+-\\d+")) {
  545. String[] startend = s.split("-");
  546. renderingOptions.put(PrintRenderer.START_PAGE, new Integer(startend[0]));
  547. renderingOptions.put(PrintRenderer.END_PAGE, new Integer(startend[1]));
  548. } else {
  549. PagesMode mode = PagesMode.byName(s);
  550. renderingOptions.put(PrintRenderer.PAGES_MODE, mode);
  551. }
  552. }
  553. return 1;
  554. } else {
  555. return 0;
  556. }
  557. }
  558. private int parseCopiesOption(String[] args, int i) throws FOPException {
  559. if ((i + 1 == args.length)
  560. || (isOption(args[i + 1]))) {
  561. throw new FOPException("you must specify the number of copies");
  562. } else {
  563. renderingOptions.put(PrintRenderer.COPIES, new Integer(args[i + 1]));
  564. return 1;
  565. }
  566. }
  567. private int parsePCLOutputOption(String[] args, int i) throws FOPException {
  568. setOutputMode(MimeConstants.MIME_PCL);
  569. if ((i + 1 == args.length)
  570. || (isOption(args[i + 1]))) {
  571. throw new FOPException("you must specify the PDF output file");
  572. } else {
  573. setOutputFile(args[i + 1]);
  574. return 1;
  575. }
  576. }
  577. private int parsePostscriptOutputOption(String[] args, int i) throws FOPException {
  578. setOutputMode(MimeConstants.MIME_POSTSCRIPT);
  579. if ((i + 1 == args.length)
  580. || (isOption(args[i + 1]))) {
  581. throw new FOPException("you must specify the PostScript output file");
  582. } else {
  583. setOutputFile(args[i + 1]);
  584. return 1;
  585. }
  586. }
  587. private int parseTextOutputOption(String[] args, int i) throws FOPException {
  588. setOutputMode(MimeConstants.MIME_PLAIN_TEXT);
  589. if ((i + 1 == args.length)
  590. || (isOption(args[i + 1]))) {
  591. throw new FOPException("you must specify the text output file");
  592. } else {
  593. setOutputFile(args[i + 1]);
  594. return 1;
  595. }
  596. }
  597. private int parseSVGOutputOption(String[] args, int i) throws FOPException {
  598. setOutputMode(MimeConstants.MIME_SVG);
  599. if ((i + 1 == args.length)
  600. || (isOption(args[i + 1]))) {
  601. throw new FOPException("you must specify the SVG output file");
  602. } else {
  603. setOutputFile(args[i + 1]);
  604. return 1;
  605. }
  606. }
  607. private int parseAFPOutputOption(String[] args, int i) throws FOPException {
  608. setOutputMode(MimeConstants.MIME_AFP);
  609. if ((i + 1 == args.length)
  610. || (isOption(args[i + 1]))) {
  611. throw new FOPException("you must specify the AFP output file");
  612. } else {
  613. setOutputFile(args[i + 1]);
  614. return 1;
  615. }
  616. }
  617. private int parseFOOutputOption(String[] args, int i) throws FOPException {
  618. setOutputMode(MimeConstants.MIME_XSL_FO);
  619. if ((i + 1 == args.length)
  620. || (isOption(args[i + 1]))) {
  621. throw new FOPException("you must specify the FO output file");
  622. } else {
  623. setOutputFile(args[i + 1]);
  624. return 1;
  625. }
  626. }
  627. private int parseCustomOutputOption(String[] args, int i) throws FOPException {
  628. String mime = null;
  629. if ((i + 1 < args.length)
  630. || (args[i + 1].charAt(0) != '-')) {
  631. mime = args[i + 1];
  632. if ("list".equals(mime)) {
  633. String[] mimes = factory.getRendererFactory().listSupportedMimeTypes();
  634. System.out.println("Supported MIME types:");
  635. for (int j = 0; j < mimes.length; j++) {
  636. System.out.println(" " + mimes[j]);
  637. }
  638. System.exit(0);
  639. }
  640. }
  641. if ((i + 2 >= args.length)
  642. || (isOption(args[i + 1]))
  643. || (isOption(args[i + 2]))) {
  644. throw new FOPException("you must specify the output format and the output file");
  645. } else {
  646. setOutputMode(mime);
  647. setOutputFile(args[i + 2]);
  648. return 2;
  649. }
  650. }
  651. private int parseUnknownOption(String[] args, int i) throws FOPException {
  652. if (inputmode == NOT_SET) {
  653. inputmode = FO_INPUT;
  654. String filename = args[i];
  655. if (isSystemInOutFile(filename)) {
  656. this.useStdIn = true;
  657. } else {
  658. fofile = new File(filename);
  659. }
  660. } else if (outputmode == null) {
  661. outputmode = MimeConstants.MIME_PDF;
  662. setOutputFile(args[i]);
  663. } else {
  664. throw new FOPException("Don't know what to do with "
  665. + args[i]);
  666. }
  667. return 0;
  668. }
  669. private int parseAreaTreeOption(String[] args, int i) throws FOPException {
  670. setOutputMode(MimeConstants.MIME_FOP_AREA_TREE);
  671. if ((i + 1 == args.length)
  672. || (isOption(args[i + 1]))) {
  673. throw new FOPException("you must specify the area-tree output file");
  674. } else if ((i + 2 == args.length)
  675. || (isOption(args[i + 2]))) {
  676. // only output file is specified
  677. setOutputFile(args[i + 1]);
  678. return 1;
  679. } else {
  680. // mimic format and output file have been specified
  681. mimicRenderer = args[i + 1];
  682. setOutputFile(args[i + 2]);
  683. return 2;
  684. }
  685. }
  686. private int parseIntermediateFormatOption(String[] args, int i) throws FOPException {
  687. setOutputMode(MimeConstants.MIME_FOP_IF);
  688. if ((i + 1 == args.length)
  689. || (args[i + 1].charAt(0) == '-')) {
  690. throw new FOPException("you must specify the intermediate format output file");
  691. } else if ((i + 2 == args.length)
  692. || (args[i + 2].charAt(0) == '-')) {
  693. // only output file is specified
  694. setOutputFile(args[i + 1]);
  695. return 1;
  696. } else {
  697. // mimic format and output file have been specified
  698. mimicRenderer = args[i + 1];
  699. setOutputFile(args[i + 2]);
  700. return 2;
  701. }
  702. }
  703. private int parseAreaTreeInputOption(String[] args, int i) throws FOPException {
  704. setInputFormat(AREATREE_INPUT);
  705. if ((i + 1 == args.length)
  706. || (isOption(args[i + 1]))) {
  707. throw new FOPException("you must specify the Area Tree file for the '-atin' option");
  708. } else {
  709. String filename = args[i + 1];
  710. if (isSystemInOutFile(filename)) {
  711. this.useStdIn = true;
  712. } else {
  713. areatreefile = new File(filename);
  714. }
  715. return 1;
  716. }
  717. }
  718. private int parseIFInputOption(String[] args, int i) throws FOPException {
  719. setInputFormat(IF_INPUT);
  720. if ((i + 1 == args.length)
  721. || (isOption(args[i + 1]))) {
  722. throw new FOPException("you must specify the intermediate file for the '-ifin' option");
  723. } else {
  724. String filename = args[i + 1];
  725. if (isSystemInOutFile(filename)) {
  726. this.useStdIn = true;
  727. } else {
  728. iffile = new File(filename);
  729. }
  730. return 1;
  731. }
  732. }
  733. private int parseImageInputOption(String[] args, int i) throws FOPException {
  734. setInputFormat(IMAGE_INPUT);
  735. if ((i + 1 == args.length)
  736. || (isOption(args[i + 1]))) {
  737. throw new FOPException("you must specify the image file for the '-imagein' option");
  738. } else {
  739. String filename = args[i + 1];
  740. if (isSystemInOutFile(filename)) {
  741. this.useStdIn = true;
  742. } else {
  743. imagefile = new File(filename);
  744. }
  745. return 1;
  746. }
  747. }
  748. private PDFEncryptionParams getPDFEncryptionParams() throws FOPException {
  749. PDFEncryptionParams params = (PDFEncryptionParams)renderingOptions.get(
  750. PDFConfigurationConstants.ENCRYPTION_PARAMS);
  751. if (params == null) {
  752. if (!PDFEncryptionManager.checkAvailableAlgorithms()) {
  753. throw new FOPException("PDF encryption requested but it is not available."
  754. + " Please make sure MD5 and RC4 algorithms are available.");
  755. }
  756. params = new PDFEncryptionParams();
  757. renderingOptions.put(PDFConfigurationConstants.ENCRYPTION_PARAMS, params);
  758. }
  759. return params;
  760. }
  761. private int parsePDFOwnerPassword(String[] args, int i) throws FOPException {
  762. if ((i + 1 == args.length)
  763. || (isOption(args[i + 1]))) {
  764. getPDFEncryptionParams().setOwnerPassword("");
  765. return 0;
  766. } else {
  767. getPDFEncryptionParams().setOwnerPassword(args[i + 1]);
  768. return 1;
  769. }
  770. }
  771. private int parsePDFUserPassword(String[] args, int i) throws FOPException {
  772. if ((i + 1 == args.length)
  773. || (isOption(args[i + 1]))) {
  774. getPDFEncryptionParams().setUserPassword("");
  775. return 0;
  776. } else {
  777. getPDFEncryptionParams().setUserPassword(args[i + 1]);
  778. return 1;
  779. }
  780. }
  781. private int parsePDFProfile(String[] args, int i) throws FOPException {
  782. if ((i + 1 == args.length)
  783. || (isOption(args[i + 1]))) {
  784. throw new FOPException("You must specify a PDF profile");
  785. } else {
  786. String profile = args[i + 1];
  787. PDFAMode pdfAMode = PDFAMode.valueOf(profile);
  788. if (pdfAMode != null && pdfAMode != PDFAMode.DISABLED) {
  789. if (renderingOptions.get("pdf-a-mode") != null) {
  790. throw new FOPException("PDF/A mode already set");
  791. }
  792. renderingOptions.put("pdf-a-mode", pdfAMode.getName());
  793. return 1;
  794. } else {
  795. PDFXMode pdfXMode = PDFXMode.valueOf(profile);
  796. if (pdfXMode != null && pdfXMode != PDFXMode.DISABLED) {
  797. if (renderingOptions.get("pdf-x-mode") != null) {
  798. throw new FOPException("PDF/X mode already set");
  799. }
  800. renderingOptions.put("pdf-x-mode", pdfXMode.getName());
  801. return 1;
  802. }
  803. }
  804. throw new FOPException("Unsupported PDF profile: " + profile);
  805. }
  806. }
  807. private void setOutputMode(String mime) throws FOPException {
  808. if (outputmode == null) {
  809. outputmode = mime;
  810. } else {
  811. throw new FOPException("you can only set one output method");
  812. }
  813. }
  814. private void setLogOption (String option, String level) {
  815. if (log instanceof CommandLineLogger
  816. || System.getProperty("org.apache.commons.logging.Log") == null) {
  817. setLogLevel(level);
  818. } else if (log != null) {
  819. log.warn("The option " + option + " can only be used");
  820. log.warn("with FOP's command line logger,");
  821. log.warn("which is the default on the command line.");
  822. log.warn("Configure other loggers using Java system properties.");
  823. }
  824. }
  825. private void setLogLevel(String level) {
  826. // Set the level for future loggers.
  827. LogFactory.getFactory().setAttribute("level", level);
  828. if (log instanceof CommandLineLogger) {
  829. // Set the level for the logger created already.
  830. ((CommandLineLogger) log).setLogLevel(level);
  831. }
  832. }
  833. private void setInputFormat(int format) throws FOPException {
  834. if (inputmode == NOT_SET || inputmode == format) {
  835. inputmode = format;
  836. } else {
  837. throw new FOPException("Only one input mode can be specified!");
  838. }
  839. }
  840. /**
  841. * checks whether all necessary information has been given in a consistent way
  842. */
  843. private void checkSettings() throws FOPException, FileNotFoundException {
  844. if (inputmode == NOT_SET) {
  845. throw new FOPException("No input file specified");
  846. }
  847. if (outputmode == null) {
  848. throw new FOPException("No output file specified");
  849. }
  850. if ((outputmode.equals(MimeConstants.MIME_FOP_AWT_PREVIEW)
  851. || outputmode.equals(MimeConstants.MIME_FOP_PRINT))
  852. && outfile != null) {
  853. throw new FOPException("Output file may not be specified "
  854. + "for AWT or PRINT output");
  855. }
  856. if (inputmode == XSLT_INPUT) {
  857. // check whether xml *and* xslt file have been set
  858. if (xmlfile == null && !this.useStdIn) {
  859. throw new FOPException("XML file must be specified for the transform mode");
  860. }
  861. if (xsltfile == null) {
  862. throw new FOPException("XSLT file must be specified for the transform mode");
  863. }
  864. // warning if fofile has been set in xslt mode
  865. if (fofile != null) {
  866. log.warn("Can't use fo file with transform mode! Ignoring.\n"
  867. + "Your input is " + "\n xmlfile: "
  868. + xmlfile.getAbsolutePath()
  869. + "\nxsltfile: "
  870. + xsltfile.getAbsolutePath()
  871. + "\n fofile: "
  872. + fofile.getAbsolutePath());
  873. }
  874. if (xmlfile != null && !xmlfile.exists()) {
  875. throw new FileNotFoundException("Error: xml file "
  876. + xmlfile.getAbsolutePath()
  877. + " not found ");
  878. }
  879. if (!xsltfile.exists()) {
  880. throw new FileNotFoundException("Error: xsl file "
  881. + xsltfile.getAbsolutePath()
  882. + " not found ");
  883. }
  884. } else if (inputmode == FO_INPUT) {
  885. if (outputmode.equals(MimeConstants.MIME_XSL_FO)) {
  886. throw new FOPException(
  887. "FO output mode is only available if you use -xml and -xsl");
  888. }
  889. if (fofile != null && !fofile.exists()) {
  890. throw new FileNotFoundException("Error: fo file "
  891. + fofile.getAbsolutePath()
  892. + " not found ");
  893. }
  894. } else if (inputmode == AREATREE_INPUT) {
  895. if (outputmode.equals(MimeConstants.MIME_XSL_FO)) {
  896. throw new FOPException(
  897. "FO output mode is only available if you use -xml and -xsl");
  898. } else if (outputmode.equals(MimeConstants.MIME_FOP_AREA_TREE)) {
  899. throw new FOPException(
  900. "Area Tree Output is not available if Area Tree is used as input!");
  901. }
  902. if (areatreefile != null && !areatreefile.exists()) {
  903. throw new FileNotFoundException("Error: area tree file "
  904. + areatreefile.getAbsolutePath()
  905. + " not found ");
  906. }
  907. } else if (inputmode == IF_INPUT) {
  908. if (outputmode.equals(MimeConstants.MIME_XSL_FO)) {
  909. throw new FOPException(
  910. "FO output mode is only available if you use -xml and -xsl");
  911. } else if (outputmode.equals(MimeConstants.MIME_FOP_AREA_TREE)) {
  912. throw new FOPException(
  913. "Area Tree Output is not available if Intermediate Format"
  914. + " is used as input!");
  915. } else if (outputmode.equals(MimeConstants.MIME_FOP_IF)) {
  916. throw new FOPException(
  917. "Intermediate Output is not available if Intermediate Format"
  918. + " is used as input!");
  919. }
  920. if (iffile != null && !iffile.exists()) {
  921. throw new FileNotFoundException("Error: intermediate format file "
  922. + iffile.getAbsolutePath()
  923. + " not found ");
  924. }
  925. } else if (inputmode == IMAGE_INPUT) {
  926. if (outputmode.equals(MimeConstants.MIME_XSL_FO)) {
  927. throw new FOPException(
  928. "FO output mode is only available if you use -xml and -xsl");
  929. }
  930. if (imagefile != null && !imagefile.exists()) {
  931. throw new FileNotFoundException("Error: image file "
  932. + imagefile.getAbsolutePath()
  933. + " not found ");
  934. }
  935. }
  936. } // end checkSettings
  937. /**
  938. * Sets the user configuration.
  939. * @throws FOPException if creating the user configuration fails
  940. * @throws IOException
  941. */
  942. private void setUserConfig() throws FOPException, IOException {
  943. if (userConfigFile == null) {
  944. return;
  945. }
  946. try {
  947. factory.setUserConfig(userConfigFile);
  948. } catch (SAXException e) {
  949. throw new FOPException(e);
  950. }
  951. }
  952. /**
  953. * @return the chosen output format (MIME type)
  954. * @throws FOPException for invalid output formats
  955. */
  956. protected String getOutputFormat() throws FOPException {
  957. if (outputmode == null) {
  958. throw new FOPException("Renderer has not been set!");
  959. }
  960. if (outputmode.equals(MimeConstants.MIME_FOP_AREA_TREE)) {
  961. renderingOptions.put("fineDetail", isCoarseAreaXml());
  962. }
  963. return outputmode;
  964. }
  965. /**
  966. * Create an InputHandler object based on command-line parameters
  967. * @return a new InputHandler instance
  968. * @throws IllegalArgumentException if invalid/missing parameters
  969. */
  970. private InputHandler createInputHandler() {
  971. switch (inputmode) {
  972. case FO_INPUT:
  973. return new InputHandler(fofile);
  974. case AREATREE_INPUT:
  975. return new AreaTreeInputHandler(areatreefile);
  976. case IF_INPUT:
  977. return new IFInputHandler(iffile);
  978. case XSLT_INPUT:
  979. InputHandler handler = new InputHandler(xmlfile, xsltfile, xsltParams);
  980. if (useCatalogResolver) {
  981. handler.createCatalogResolver(foUserAgent);
  982. }
  983. return handler;
  984. case IMAGE_INPUT:
  985. return new ImageInputHandler(imagefile, xsltfile, xsltParams);
  986. default:
  987. throw new IllegalArgumentException("Error creating InputHandler object.");
  988. }
  989. }
  990. /**
  991. * Get the FOUserAgent for this Command-Line run
  992. * @return FOUserAgent instance
  993. */
  994. protected FOUserAgent getFOUserAgent() {
  995. return foUserAgent;
  996. }
  997. /**
  998. * Returns the XSL-FO file if set.
  999. * @return the XSL-FO file, null if not set
  1000. */
  1001. public File getFOFile() {
  1002. return fofile;
  1003. }
  1004. /**
  1005. * Returns the input XML file if set.
  1006. * @return the input XML file, null if not set
  1007. */
  1008. public File getXMLFile() {
  1009. return xmlfile;
  1010. }
  1011. /**
  1012. * Returns the stylesheet to be used for transformation to XSL-FO.
  1013. * @return stylesheet
  1014. */
  1015. public File getXSLFile() {
  1016. return xsltfile;
  1017. }
  1018. /**
  1019. * Returns the output file
  1020. * @return the output file
  1021. */
  1022. public File getOutputFile() {
  1023. return outfile;
  1024. }
  1025. /**
  1026. * Returns the user configuration file to be used.
  1027. * @return the userconfig.xml file
  1028. */
  1029. public File getUserConfigFile() {
  1030. return userConfigFile;
  1031. }
  1032. /**
  1033. * Indicates whether the XML renderer should generate coarse area XML
  1034. * @return true if coarse area XML is desired
  1035. */
  1036. public Boolean isCoarseAreaXml() {
  1037. return suppressLowLevelAreas;
  1038. }
  1039. /**
  1040. * Indicates whether input comes from standard input (stdin).
  1041. * @return true if input comes from standard input (stdin)
  1042. */
  1043. public boolean isInputFromStdIn() {
  1044. return this.useStdIn;
  1045. }
  1046. /**
  1047. * Indicates whether output is sent to standard output (stdout).
  1048. * @return true if output is sent to standard output (stdout)
  1049. */
  1050. public boolean isOutputToStdOut() {
  1051. return this.useStdOut;
  1052. }
  1053. /**
  1054. * Returns the input file.
  1055. * @return either the fofile or the xmlfile
  1056. */
  1057. public File getInputFile() {
  1058. switch (inputmode) {
  1059. case FO_INPUT:
  1060. return fofile;
  1061. case XSLT_INPUT:
  1062. return xmlfile;
  1063. default:
  1064. return fofile;
  1065. }
  1066. }
  1067. private static void printVersion() {
  1068. System.out.println("FOP Version " + Version.getVersion());
  1069. }
  1070. /**
  1071. * Shows the command line syntax including a summary of all available options and some
  1072. * examples.
  1073. *
  1074. * @param out the stream to which the message must be printed
  1075. */
  1076. public static void printUsage(PrintStream out) {
  1077. out.println(
  1078. "\nUSAGE\nfop [options] [-fo|-xml] infile [-xsl file] "
  1079. + "[-awt|-pdf|-mif|-rtf|-tiff|-png|-pcl|-ps|-txt|-at [mime]|-print] <outfile>\n"
  1080. + " [OPTIONS] \n"
  1081. + " -version print FOP version and exit\n"
  1082. + " -d debug mode \n"
  1083. + " -x dump configuration settings \n"
  1084. + " -q quiet mode \n"
  1085. + " -c cfg.xml use additional configuration file cfg.xml\n"
  1086. + " -l lang the language to use for user information \n"
  1087. + " -r relaxed/less strict validation (where available)\n"
  1088. + " -dpi xxx target resolution in dots per inch (dpi) where xxx is a number\n"
  1089. + " -s for area tree XML, down to block areas only\n"
  1090. + " -v run in verbose mode (currently simply print FOP version"
  1091. + " and continue)\n\n"
  1092. + " -o [password] PDF file will be encrypted with option owner password\n"
  1093. + " -u [password] PDF file will be encrypted with option user password\n"
  1094. + " -noprint PDF file will be encrypted without printing permission\n"
  1095. + " -nocopy PDF file will be encrypted without copy content permission\n"
  1096. + " -noedit PDF file will be encrypted without edit content permission\n"
  1097. + " -noannotations PDF file will be encrypted without edit annotation permission\n"
  1098. + " -nofillinforms PDF file will be encrypted without"
  1099. + " fill in interactive form fields permission\n"
  1100. + " -noaccesscontent PDF file will be encrypted without"
  1101. + " extract text and graphics permission\n"
  1102. + " -noassembledoc PDF file will be encrypted without"
  1103. + " assemble the document permission\n"
  1104. + " -noprinthq PDF file will be encrypted without"
  1105. + " print high quality permission\n"
  1106. + " -a enables accessibility features (Tagged PDF etc., default off)\n"
  1107. + " -pdfprofile prof PDF file will be generated with the specified profile\n"
  1108. + " (Examples for prof: PDF/A-1b or PDF/X-3:2003)\n\n"
  1109. + " -conserve enable memory-conservation policy (trades memory-consumption"
  1110. + " for disk I/O)\n"
  1111. + " (Note: currently only influences whether the area tree is"
  1112. + " serialized.)\n\n"
  1113. + " -cache specifies a file/directory path location"
  1114. + " for the font cache file\n"
  1115. + " -flush flushes the current font cache file\n\n"
  1116. + " [INPUT] \n"
  1117. + " infile xsl:fo input file (the same as the next) \n"
  1118. + " (use '-' for infile to pipe input from stdin)\n"
  1119. + " -fo infile xsl:fo input file \n"
  1120. + " -xml infile xml input file, must be used together with -xsl \n"
  1121. + " -atin infile area tree input file \n"
  1122. + " -ifin infile intermediate format input file \n"
  1123. + " -imagein infile image input file (piping through stdin not supported)\n"
  1124. + " -xsl stylesheet xslt stylesheet \n \n"
  1125. + " -param name value <value> to use for parameter <name> in xslt stylesheet\n"
  1126. + " (repeat '-param name value' for each parameter)\n \n"
  1127. + " -catalog use catalog resolver for input XML and XSLT files\n"
  1128. + " [OUTPUT] \n"
  1129. + " outfile input will be rendered as PDF into outfile\n"
  1130. + " (use '-' for outfile to pipe output to stdout)\n"
  1131. + " -pdf outfile input will be rendered as PDF (outfile req'd)\n"
  1132. + " -pdfa1b outfile input will be rendered as PDF/A-1b compliant PDF\n"
  1133. + " (outfile req'd, same as \"-pdf outfile -pdfprofile PDF/A-1b\")\n"
  1134. + " -awt input will be displayed on screen \n"
  1135. + " -rtf outfile input will be rendered as RTF (outfile req'd)\n"
  1136. + " -pcl outfile input will be rendered as PCL (outfile req'd) \n"
  1137. + " -ps outfile input will be rendered as PostScript (outfile req'd) \n"
  1138. + " -afp outfile input will be rendered as AFP (outfile req'd)\n"
  1139. + " -tiff outfile input will be rendered as TIFF (outfile req'd)\n"
  1140. + " -png outfile input will be rendered as PNG (outfile req'd)\n"
  1141. + " -txt outfile input will be rendered as plain text (outfile req'd) \n"
  1142. + " -at [mime] out representation of area tree as XML (outfile req'd) \n"
  1143. + " specify optional mime output to allow the AT to be converted\n"
  1144. + " to final format later\n"
  1145. + " -if [mime] out representation of document in intermediate format XML"
  1146. + " (outfile req'd)\n"
  1147. + " specify optional mime output to allow the IF to be converted\n"
  1148. + " to final format later\n"
  1149. + " -print input file will be rendered and sent to the printer \n"
  1150. + " see options with \"-print help\" \n"
  1151. + " -out mime outfile input will be rendered using the given MIME type\n"
  1152. + " (outfile req'd) Example: \"-out application/pdf D:\\out.pdf\"\n"
  1153. + " (Tip: \"-out list\" prints the list of supported MIME types"
  1154. + " and exits)\n"
  1155. //+ " -mif outfile input will be rendered as MIF (FrameMaker) (outfile req'd)\n"
  1156. //+ " Experimental feature - requires additional fop-sandbox.jar.\n"
  1157. + " -svg outfile input will be rendered as an SVG slides file (outfile req'd) \n"
  1158. + " Experimental feature - requires additional fop-sandbox.jar.\n"
  1159. + "\n"
  1160. + " -foout outfile input will only be XSL transformed. The intermediate \n"
  1161. + " XSL-FO file is saved and no rendering is performed. \n"
  1162. + " (Only available if you use -xml and -xsl parameters)\n\n"
  1163. + "\n"
  1164. + " [Examples]\n" + " fop foo.fo foo.pdf \n"
  1165. + " fop -fo foo.fo -pdf foo.pdf (does the same as the previous line)\n"
  1166. + " fop -xml foo.xml -xsl foo.xsl -pdf foo.pdf\n"
  1167. + " fop -xml foo.xml -xsl foo.xsl -foout foo.fo\n"
  1168. + " fop -xml - -xsl foo.xsl -pdf -\n"
  1169. + " fop foo.fo -mif foo.mif\n"
  1170. + " fop foo.fo -rtf foo.rtf\n"
  1171. + " fop foo.fo -print\n"
  1172. + " fop foo.fo -awt\n");
  1173. }
  1174. /**
  1175. * shows the options for print output
  1176. */
  1177. private void printUsagePrintOutput() {
  1178. System.err.println("USAGE: -print [from[-to][,even|odd]] [-copies numCopies]\n\n"
  1179. + "Example:\n"
  1180. + "all pages: fop infile.fo -print\n"
  1181. + "all pages with two copies: fop infile.fo -print -copies 2\n"
  1182. + "all pages starting with page 7: fop infile.fo -print 7\n"
  1183. + "pages 2 to 3: fop infile.fo -print 2-3\n"
  1184. + "only even page between 10 and 20: fop infile.fo -print 10-20,even\n");
  1185. }
  1186. /**
  1187. * Outputs all commandline settings
  1188. */
  1189. private void dumpConfiguration() {
  1190. log.info("Input mode: ");
  1191. switch (inputmode) {
  1192. case NOT_SET:
  1193. log.info("not set");
  1194. break;
  1195. case FO_INPUT:
  1196. log.info("FO ");
  1197. if (isInputFromStdIn()) {
  1198. log.info("fo input file: from stdin");
  1199. } else {
  1200. log.info("fo input file: " + fofile.toString());
  1201. }
  1202. break;
  1203. case XSLT_INPUT:
  1204. log.info("xslt transformation");
  1205. if (isInputFromStdIn()) {
  1206. log.info("xml input file: from stdin");
  1207. } else {
  1208. log.info("xml input file: " + xmlfile.toString());
  1209. }
  1210. log.info("xslt stylesheet: " + xsltfile.toString());
  1211. break;
  1212. case AREATREE_INPUT:
  1213. log.info("AT ");
  1214. if (isInputFromStdIn()) {
  1215. log.info("area tree input file: from stdin");
  1216. } else {
  1217. log.info("area tree input file: " + areatreefile.toString());
  1218. }
  1219. break;
  1220. case IF_INPUT:
  1221. log.info("IF ");
  1222. if (isInputFromStdIn()) {
  1223. log.info("intermediate input file: from stdin");
  1224. } else {
  1225. log.info("intermediate input file: " + iffile.toString());
  1226. }
  1227. break;
  1228. case IMAGE_INPUT:
  1229. log.info("Image ");
  1230. if (isInputFromStdIn()) {
  1231. log.info("image input file: from stdin");
  1232. } else {
  1233. log.info("image input file: " + imagefile.toString());
  1234. }
  1235. break;
  1236. default:
  1237. log.info("unknown input type");
  1238. }
  1239. log.info("Output mode: ");
  1240. if (outputmode == null) {
  1241. log.info("not set");
  1242. } else if (MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputmode)) {
  1243. log.info("awt on screen");
  1244. if (outfile != null) {
  1245. log.error("awt mode, but outfile is set:");
  1246. log.error("out file: " + outfile.toString());
  1247. }
  1248. } else if (MimeConstants.MIME_FOP_PRINT.equals(outputmode)) {
  1249. log.info("print directly");
  1250. if (outfile != null) {
  1251. log.error("print mode, but outfile is set:");
  1252. log.error("out file: " + outfile.toString());
  1253. }
  1254. } else if (MimeConstants.MIME_FOP_AREA_TREE.equals(outputmode)) {
  1255. log.info("area tree");
  1256. if (mimicRenderer != null) {
  1257. log.info("mimic renderer: " + mimicRenderer);
  1258. }
  1259. if (isOutputToStdOut()) {
  1260. log.info("output file: to stdout");
  1261. } else {
  1262. log.info("output file: " + outfile.toString());
  1263. }
  1264. } else if (MimeConstants.MIME_FOP_IF.equals(outputmode)) {
  1265. log.info("intermediate format");
  1266. log.info("output file: " + outfile.toString());
  1267. } else {
  1268. log.info(outputmode);
  1269. if (isOutputToStdOut()) {
  1270. log.info("output file: to stdout");
  1271. } else {
  1272. log.info("output file: " + outfile.toString());
  1273. }
  1274. }
  1275. log.info("OPTIONS");
  1276. if (userConfigFile != null) {
  1277. log.info("user configuration file: "
  1278. + userConfigFile.toString());
  1279. } else {
  1280. log.info("no user configuration file is used [default]");
  1281. }
  1282. }
  1283. private void flushCache() throws FOPException {
  1284. FontManager fontManager = factory.getFontManager();
  1285. File cacheFile = fontManager.getCacheFile();
  1286. if (!fontManager.deleteCache()) {
  1287. System.err.println("Failed to flush the font cache file '"
  1288. + cacheFile + "'.");
  1289. System.exit(1);
  1290. }
  1291. }
  1292. }