選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CommandLineOptions.java 34KB

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