Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CommandLineOptions.java 55KB

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