KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > cli > CommandLineOptions


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
18 /* $Id: CommandLineOptions.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.cli;
21
22 // java
23
import java.io.File JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 import javax.swing.UIManager JavaDoc;
31
32 import org.apache.fop.Version;
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.pdf.PDFAMode;
38 import org.apache.fop.pdf.PDFEncryptionManager;
39 import org.apache.fop.pdf.PDFEncryptionParams;
40 import org.apache.fop.pdf.PDFXMode;
41 import org.apache.fop.render.awt.AWTRenderer;
42 import org.apache.fop.render.Renderer;
43 import org.apache.fop.render.pdf.PDFRenderer;
44 import org.apache.fop.render.xml.XMLRenderer;
45 import org.apache.fop.util.CommandLineLogger;
46
47 // commons logging
48
import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50
51 // SAX
52
import org.xml.sax.SAXException JavaDoc;
53
54 /**
55  * Options parses the commandline arguments
56  */

57 public class CommandLineOptions {
58
59     /** Used to indicate that only the result of the XSL transformation should be output */
60     public static final int RENDER_NONE = -1;
61
62     /* These following constants are used to describe the input (either .FO, .XML/.XSL or
63      * intermediate format)
64      */

65
66     /** (input) not set */
67     public static final int NOT_SET = 0;
68     /** input: fo file */
69     public static final int FO_INPUT = 1;
70     /** input: xml+xsl file */
71     public static final int XSLT_INPUT = 2;
72     /** input: Area Tree XML file */
73     public static final int AREATREE_INPUT = 3;
74
75     /* show configuration information */
76     private Boolean JavaDoc showConfiguration = Boolean.FALSE;
77     /* for area tree XML output, only down to block area level */
78     private Boolean JavaDoc suppressLowLevelAreas = Boolean.FALSE;
79     /* user configuration file */
80     private File JavaDoc userConfigFile = null;
81     /* input fo file */
82     private File JavaDoc fofile = null;
83     /* xsltfile (xslt transformation as input) */
84     private File JavaDoc xsltfile = null;
85     /* xml file (xslt transformation as input) */
86     private File JavaDoc xmlfile = null;
87     /* area tree input file */
88     private File JavaDoc areatreefile = null;
89     /* output file */
90     private File JavaDoc outfile = null;
91     /* input mode */
92     private int inputmode = NOT_SET;
93     /* output mode */
94     private String JavaDoc outputmode = null;
95     /* rendering options (for the user agent) */
96     private Map JavaDoc renderingOptions = new java.util.HashMap JavaDoc();
97     /* target resolution (for the user agent) */
98     private int targetResolution = 0;
99     
100     private FopFactory factory = FopFactory.newInstance();
101     private FOUserAgent foUserAgent;
102
103     private InputHandler inputHandler;
104
105     private Log log;
106
107     private Vector JavaDoc xsltParams = null;
108
109     private String JavaDoc mimicRenderer = null;
110
111     /**
112      * Construct a command line option object.
113      */

114     public CommandLineOptions() {
115         LogFactory logFactory = LogFactory.getFactory();
116
117         // Enable the simple command line logging when no other logger is
118
// defined.
119
if (System.getProperty("org.apache.commons.logging.Log") == null) {
120             logFactory.setAttribute("org.apache.commons.logging.Log",
121                                             CommandLineLogger.class.getName());
122             setLogLevel("info");
123         }
124
125         log = LogFactory.getLog("FOP");
126     }
127
128     /**
129      * Parse the command line arguments.
130      * @param args the command line arguments.
131      * @throws FOPException for general errors
132      * @throws FileNotFoundException if an input file wasn't found
133      * @throws IOException if the the configuration file could not be loaded
134      */

135     public void parse(String JavaDoc[] args)
136             throws FOPException, IOException JavaDoc {
137         boolean optionsParsed = true;
138
139         try {
140             optionsParsed = parseOptions(args);
141             if (optionsParsed) {
142                 if (showConfiguration == Boolean.TRUE) {
143                     dumpConfiguration();
144                 }
145                 checkSettings();
146                 createUserConfig();
147                 
148                 //Factory config is set up, now we can create the user agent
149
foUserAgent = factory.newFOUserAgent();
150                 foUserAgent.getRendererOptions().putAll(renderingOptions);
151                 if (targetResolution != 0) {
152                     foUserAgent.setTargetResolution(targetResolution);
153                 }
154                 addXSLTParameter("fop-output-format", getOutputFormat());
155                 addXSLTParameter("fop-version", Version.getVersion());
156             }
157         } catch (FOPException e) {
158             printUsage();
159             throw e;
160         } catch (java.io.FileNotFoundException JavaDoc e) {
161             printUsage();
162             throw e;
163         }
164
165         inputHandler = createInputHandler();
166
167         if (MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputmode)) {
168             //set the system look&feel for the preview dialog
169
try {
170                 UIManager.setLookAndFeel(
171                     UIManager.getSystemLookAndFeelClassName());
172             } catch (Exception JavaDoc e) {
173                 System.err.println("Couldn't set system look & feel!");
174             }
175
176             AWTRenderer renderer = new AWTRenderer(true);
177             renderer.setRenderable(inputHandler); //set before user agent!
178
renderer.setUserAgent(foUserAgent);
179             foUserAgent.setRendererOverride(renderer);
180         } else if (MimeConstants.MIME_FOP_AREA_TREE.equals(outputmode)
181                && mimicRenderer != null) {
182             // render from FO to Intermediate Format
183
Renderer targetRenderer = foUserAgent.getRendererFactory().createRenderer(
184                    foUserAgent, mimicRenderer);
185             XMLRenderer xmlRenderer = new XMLRenderer();
186             xmlRenderer.setUserAgent(foUserAgent);
187
188             //Tell the XMLRenderer to mimic the target renderer
189
xmlRenderer.mimicRenderer(targetRenderer);
190
191             //Make sure the prepared XMLRenderer is used
192
foUserAgent.setRendererOverride(xmlRenderer);
193         }
194     }
195
196     /**
197      * @return the InputHandler instance defined by the command-line options.
198      */

199     public InputHandler getInputHandler() {
200         return inputHandler;
201     }
202
203     /**
204      * Get the logger.
205      * @return the logger
206      */

207     public Log getLogger() {
208         return log;
209     }
210
211     private void addXSLTParameter(String JavaDoc name, String JavaDoc value) {
212         if (xsltParams == null) {
213             xsltParams = new Vector JavaDoc();
214         }
215         xsltParams.addElement(name);
216         xsltParams.addElement(value);
217     }
218
219     /**
220      * parses the commandline arguments
221      * @return true if parse was successful and processing can continue, false
222      * if processing should stop
223      * @exception FOPException if there was an error in the format of the options
224      */

225     private boolean parseOptions(String JavaDoc[] args) throws FOPException {
226         for (int i = 0; i < args.length; i++) {
227             if (args[i].equals("-x")
228                        || args[i].equals("--dump-config")) {
229                 showConfiguration = Boolean.TRUE;
230             } else if (args[i].equals("-c")) {
231                 i = i + parseConfigurationOption(args, i);
232             } else if (args[i].equals("-l")) {
233                 i = i + parseLanguageOption(args, i);
234             } else if (args[i].equals("-s")) {
235                 suppressLowLevelAreas = Boolean.TRUE;
236             } else if (args[i].equals("-d")) {
237                 setLogOption("debug", "debug");
238             } else if (args[i].equals("-r")) {
239                 factory.setStrictValidation(false);
240             } else if (args[i].equals("-dpi")) {
241                 i = i + parseResolution(args, i);
242             } else if (args[i].equals("-q") || args[i].equals("--quiet")) {
243                 setLogOption("quiet", "error");
244             } else if (args[i].equals("-fo")) {
245                 i = i + parseFOInputOption(args, i);
246             } else if (args[i].equals("-xsl")) {
247                 i = i + parseXSLInputOption(args, i);
248             } else if (args[i].equals("-xml")) {
249                 i = i + parseXMLInputOption(args, i);
250             } else if (args[i].equals("-atin")) {
251                 i = i + parseAreaTreeInputOption(args, i);
252             } else if (args[i].equals("-awt")) {
253                 i = i + parseAWTOutputOption(args, i);
254             } else if (args[i].equals("-pdf")) {
255                 i = i + parsePDFOutputOption(args, i, null);
256             } else if (args[i].equals("-pdfa1b")) {
257                 i = i + parsePDFOutputOption(args, i, "PDF/A-1b");
258             } else if (args[i].equals("-mif")) {
259                 i = i + parseMIFOutputOption(args, i);
260             } else if (args[i].equals("-rtf")) {
261                 i = i + parseRTFOutputOption(args, i);
262             } else if (args[i].equals("-tiff")) {
263                 i = i + parseTIFFOutputOption(args, i);
264             } else if (args[i].equals("-png")) {
265                 i = i + parsePNGOutputOption(args, i);
266             } else if (args[i].equals("-print")) {
267                 i = i + parsePrintOutputOption(args, i);
268                 // show print help
269
if (i + 1 < args.length) {
270                     if (args[i + 1].equals("help")) {
271                         printUsagePrintOutput();
272                         return false;
273                     }
274                 }
275             } else if (args[i].equals("-pcl")) {
276                 i = i + parsePCLOutputOption(args, i);
277             } else if (args[i].equals("-ps")) {
278                 i = i + parsePostscriptOutputOption(args, i);
279             } else if (args[i].equals("-txt")) {
280                 i = i + parseTextOutputOption(args, i);
281             } else if (args[i].equals("-svg")) {
282                 i = i + parseSVGOutputOption(args, i);
283             } else if (args[i].equals("-afp")) {
284                 i = i + parseAFPOutputOption(args, i);
285             } else if (args[i].equals("-foout")) {
286                 i = i + parseFOOutputOption(args, i);
287             } else if (args[i].equals("-out")) {
288                 i = i + parseCustomOutputOption(args, i);
289             } else if (args[i].charAt(0) != '-') {
290                 i = i + parseUnknownOption(args, i);
291             } else if (args[i].equals("-at")) {
292                 i = i + parseAreaTreeOption(args, i);
293             } else if (args[i].equals("-v")) {
294                 System.out.println("FOP Version " + Version.getVersion());
295             } else if (args[i].equals("-param")) {
296                   if (i + 2 < args.length) {
297                       String JavaDoc name = args[++i];
298                       String JavaDoc expression = args[++i];
299                       addXSLTParameter(name, expression);
300                   } else {
301                     throw new FOPException("invalid param usage: use -param <name> <value>");
302                   }
303             } else if (args[i].equals("-o")) {
304                 i = i + parsePDFOwnerPassword(args, i);
305             } else if (args[i].equals("-u")) {
306                 i = i + parsePDFUserPassword(args, i);
307             } else if (args[i].equals("-pdfprofile")) {
308                 i = i + parsePDFProfile(args, i);
309             } else if (args[i].equals("-noprint")) {
310                 getPDFEncryptionParams().setAllowPrint(false);
311             } else if (args[i].equals("-nocopy")) {
312                 getPDFEncryptionParams().setAllowCopyContent(false);
313             } else if (args[i].equals("-noedit")) {
314                 getPDFEncryptionParams().setAllowEditContent(false);
315             } else if (args[i].equals("-noannotations")) {
316                 getPDFEncryptionParams().setAllowEditAnnotations(false);
317             } else {
318                 printUsage();
319                 return false;
320             }
321         }
322         return true;
323     } // end parseOptions
324

325     private int parseConfigurationOption(String JavaDoc[] args, int i) throws FOPException {
326         if ((i + 1 == args.length)
327                 || (args[i + 1].charAt(0) == '-')) {
328             throw new FOPException("if you use '-c', you must specify "
329               + "the name of the configuration file");
330         } else {
331             userConfigFile = new File JavaDoc(args[i + 1]);
332             return 1;
333         }
334     }
335
336     private int parseLanguageOption(String JavaDoc[] args, int i) throws FOPException {
337         if ((i + 1 == args.length)
338                 || (args[i + 1].charAt(0) == '-')) {
339             throw new FOPException("if you use '-l', you must specify a language");
340         } else {
341             Locale.setDefault(new Locale JavaDoc(args[i + 1], ""));
342             return 1;
343         }
344     }
345
346     private int parseResolution(String JavaDoc[] args, int i) throws FOPException {
347         if ((i + 1 == args.length)
348                 || (args[i + 1].charAt(0) == '-')) {
349             throw new FOPException(
350                     "if you use '-dpi', you must specify a resolution (dots per inch)");
351         } else {
352             this.targetResolution = Integer.parseInt(args[i + 1]);
353             return 1;
354         }
355     }
356
357     private int parseFOInputOption(String JavaDoc[] args, int i) throws FOPException {
358         inputmode = FO_INPUT;
359         if ((i + 1 == args.length)
360                 || (args[i + 1].charAt(0) == '-')) {
361             throw new FOPException("you must specify the fo file for the '-fo' option");
362         } else {
363             fofile = new File JavaDoc(args[i + 1]);
364             return 1;
365         }
366     }
367
368     private int parseXSLInputOption(String JavaDoc[] args, int i) throws FOPException {
369         inputmode = XSLT_INPUT;
370         if ((i + 1 == args.length)
371                 || (args[i + 1].charAt(0) == '-')) {
372             throw new FOPException("you must specify the stylesheet "
373                             + "file for the '-xsl' option");
374         } else {
375             xsltfile = new File JavaDoc(args[i + 1]);
376             return 1;
377         }
378     }
379
380     private int parseXMLInputOption(String JavaDoc[] args, int i) throws FOPException {
381         inputmode = XSLT_INPUT;
382         if ((i + 1 == args.length)
383                 || (args[i + 1].charAt(0) == '-')) {
384             throw new FOPException("you must specify the input file "
385                             + "for the '-xml' option");
386         } else {
387             xmlfile = new File JavaDoc(args[i + 1]);
388             return 1;
389         }
390     }
391
392     private int parseAWTOutputOption(String JavaDoc[] args, int i) throws FOPException {
393         setOutputMode(MimeConstants.MIME_FOP_AWT_PREVIEW);
394         return 0;
395     }
396
397     private int parsePDFOutputOption(String JavaDoc[] args, int i, String JavaDoc pdfAMode) throws FOPException {
398         setOutputMode(MimeConstants.MIME_PDF);
399         if ((i + 1 == args.length)
400                 || (args[i + 1].charAt(0) == '-')) {
401             throw new FOPException("you must specify the PDF output file");
402         } else {
403             outfile = new File JavaDoc(args[i + 1]);
404             if (pdfAMode != null) {
405                 if (renderingOptions.get("pdf-a-mode") != null) {
406                     throw new FOPException("PDF/A mode already set");
407                 }
408                 renderingOptions.put("pdf-a-mode", pdfAMode);
409             }
410             return 1;
411         }
412     }
413
414     private int parseMIFOutputOption(String JavaDoc[] args, int i) throws FOPException {
415         setOutputMode(MimeConstants.MIME_MIF);
416         if ((i + 1 == args.length)
417                 || (args[i + 1].charAt(0) == '-')) {
418             throw new FOPException("you must specify the MIF output file");
419         } else {
420             outfile = new File JavaDoc(args[i + 1]);
421             return 1;
422         }
423     }
424
425     private int parseRTFOutputOption(String JavaDoc[] args, int i) throws FOPException {
426         setOutputMode(MimeConstants.MIME_RTF);
427         if ((i + 1 == args.length)
428                 || (args[i + 1].charAt(0) == '-')) {
429             throw new FOPException("you must specify the RTF output file");
430         } else {
431             outfile = new File JavaDoc(args[i + 1]);
432             return 1;
433         }
434     }
435
436     private int parseTIFFOutputOption(String JavaDoc[] args, int i) throws FOPException {
437         setOutputMode(MimeConstants.MIME_TIFF);
438         if ((i + 1 == args.length)
439                 || (args[i + 1].charAt(0) == '-')) {
440             throw new FOPException("you must specify the TIFF output file");
441         } else {
442             outfile = new File JavaDoc(args[i + 1]);
443             return 1;
444         }
445     }
446
447     private int parsePNGOutputOption(String JavaDoc[] args, int i) throws FOPException {
448         setOutputMode(MimeConstants.MIME_PNG);
449         if ((i + 1 == args.length)
450                 || (args[i + 1].charAt(0) == '-')) {
451             throw new FOPException("you must specify the PNG output file");
452         } else {
453             outfile = new File JavaDoc(args[i + 1]);
454             return 1;
455         }
456     }
457
458     private int parsePrintOutputOption(String JavaDoc[] args, int i) throws FOPException {
459         setOutputMode(MimeConstants.MIME_FOP_PRINT);
460         return 0;
461     }
462
463     private int parsePCLOutputOption(String JavaDoc[] args, int i) throws FOPException {
464         setOutputMode(MimeConstants.MIME_PCL);
465         if ((i + 1 == args.length)
466                 || (args[i + 1].charAt(0) == '-')) {
467             throw new FOPException("you must specify the PDF output file");
468         } else {
469             outfile = new File JavaDoc(args[i + 1]);
470             return 1;
471         }
472     }
473
474     private int parsePostscriptOutputOption(String JavaDoc[] args, int i) throws FOPException {
475         setOutputMode(MimeConstants.MIME_POSTSCRIPT);
476         if ((i + 1 == args.length)
477                 || (args[i + 1].charAt(0) == '-')) {
478             throw new FOPException("you must specify the PostScript output file");
479         } else {
480             outfile = new File JavaDoc(args[i + 1]);
481             return 1;
482         }
483     }
484
485     private int parseTextOutputOption(String JavaDoc[] args, int i) throws FOPException {
486         setOutputMode(MimeConstants.MIME_PLAIN_TEXT);
487         if ((i + 1 == args.length)
488                 || (args[i + 1].charAt(0) == '-')) {
489             throw new FOPException("you must specify the text output file");
490         } else {
491             outfile = new File JavaDoc(args[i + 1]);
492             return 1;
493         }
494     }
495
496     private int parseSVGOutputOption(String JavaDoc[] args, int i) throws FOPException {
497         setOutputMode(MimeConstants.MIME_SVG);
498         if ((i + 1 == args.length)
499                 || (args[i + 1].charAt(0) == '-')) {
500             throw new FOPException("you must specify the SVG output file");
501         } else {
502             outfile = new File JavaDoc(args[i + 1]);
503             return 1;
504         }
505     }
506
507     private int parseAFPOutputOption(String JavaDoc[] args, int i) throws FOPException {
508         setOutputMode(MimeConstants.MIME_AFP);
509         if ((i + 1 == args.length)
510                 || (args[i + 1].charAt(0) == '-')) {
511             throw new FOPException("you must specify the AFP output file");
512         } else {
513             outfile = new File JavaDoc(args[i + 1]);
514             return 1;
515         }
516     }
517
518     private int parseFOOutputOption(String JavaDoc[] args, int i) throws FOPException {
519         setOutputMode(MimeConstants.MIME_XSL_FO);
520         if ((i + 1 == args.length)
521                 || (args[i + 1].charAt(0) == '-')) {
522             throw new FOPException("you must specify the FO output file");
523         } else {
524             outfile = new File JavaDoc(args[i + 1]);
525             return 1;
526         }
527     }
528
529     private int parseCustomOutputOption(String JavaDoc[] args, int i) throws FOPException {
530         String JavaDoc mime = null;
531         if ((i + 1 < args.length)
532                 || (args[i + 1].charAt(0) != '-')) {
533             mime = args[i + 1];
534             if ("list".equals(mime)) {
535                 String JavaDoc[] mimes = factory.getRendererFactory().listSupportedMimeTypes();
536                 System.out.println("Supported MIME types:");
537                 for (int j = 0; j < mimes.length; j++) {
538                     System.out.println(" " + mimes[j]);
539                 }
540                 System.exit(0);
541             }
542         }
543         if ((i + 2 >= args.length)
544                 || (args[i + 1].charAt(0) == '-')
545                 || (args[i + 2].charAt(0) == '-')) {
546             throw new FOPException("you must specify the output format and the output file");
547         } else {
548             setOutputMode(mime);
549             outfile = new File JavaDoc(args[i + 2]);
550             return 2;
551         }
552     }
553
554     private int parseUnknownOption(String JavaDoc[] args, int i) throws FOPException {
555         if (inputmode == NOT_SET) {
556             inputmode = FO_INPUT;
557             fofile = new File JavaDoc(args[i]);
558         } else if (outputmode == null) {
559             outputmode = MimeConstants.MIME_PDF;
560             outfile = new File JavaDoc(args[i]);
561         } else {
562             throw new FOPException("Don't know what to do with "
563                            + args[i]);
564         }
565         return 0;
566     }
567
568     private int parseAreaTreeOption(String JavaDoc[] args, int i) throws FOPException {
569         setOutputMode(MimeConstants.MIME_FOP_AREA_TREE);
570         if ((i + 1 == args.length)
571                 || (args[i + 1].charAt(0) == '-')) {
572             throw new FOPException("you must specify the area-tree output file");
573           } else if ((i + 2 == args.length)
574                 || (args[i + 2].charAt(0) == '-')) {
575             // only output file is specified
576
outfile = new File JavaDoc(args[i + 1]);
577             return 1;
578         } else {
579             // mimic format and output file have been specified
580
mimicRenderer = args[i + 1];
581             outfile = new File JavaDoc(args[i + 2]);
582             return 2;
583         }
584     }
585
586     private int parseAreaTreeInputOption(String JavaDoc[] args, int i) throws FOPException {
587         inputmode = AREATREE_INPUT;
588         if ((i + 1 == args.length)
589                 || (args[i + 1].charAt(0) == '-')) {
590             throw new FOPException("you must specify the Area Tree file for the '-atin' option");
591         } else {
592             areatreefile = new File JavaDoc(args[i + 1]);
593             return 1;
594         }
595     }
596
597     private PDFEncryptionParams getPDFEncryptionParams() throws FOPException {
598         PDFEncryptionParams params = (PDFEncryptionParams)renderingOptions.get(
599                         PDFRenderer.ENCRYPTION_PARAMS);
600         if (params == null) {
601             if (!PDFEncryptionManager.checkAvailableAlgorithms()) {
602                 throw new FOPException("PDF encryption requested but it is not available."
603                         + " Please make sure MD5 and RC4 algorithms are available.");
604             }
605             params = new PDFEncryptionParams();
606             renderingOptions.put(PDFRenderer.ENCRYPTION_PARAMS, params);
607         }
608         return params;
609     }
610
611     private int parsePDFOwnerPassword(String JavaDoc[] args, int i) throws FOPException {
612         if ((i + 1 == args.length)
613                 || (args[i + 1].charAt(0) == '-')) {
614             getPDFEncryptionParams().setOwnerPassword("");
615             return 0;
616         } else {
617             getPDFEncryptionParams().setOwnerPassword(args[i + 1]);
618             return 1;
619         }
620     }
621
622     private int parsePDFUserPassword(String JavaDoc[] args, int i) throws FOPException {
623         if ((i + 1 == args.length)
624                 || (args[i + 1].charAt(0) == '-')) {
625             getPDFEncryptionParams().setUserPassword("");
626             return 0;
627         } else {
628             getPDFEncryptionParams().setUserPassword(args[i + 1]);
629             return 1;
630         }
631     }
632
633     private int parsePDFProfile(String JavaDoc[] args, int i) throws FOPException {
634         if ((i + 1 == args.length)
635                 || (args[i + 1].charAt(0) == '-')) {
636             throw new FOPException("You must specify a PDF profile");
637         } else {
638             String JavaDoc profile = args[i + 1];
639             PDFAMode pdfAMode = PDFAMode.valueOf(profile);
640             if (pdfAMode != null && pdfAMode != PDFAMode.DISABLED) {
641                 if (renderingOptions.get("pdf-a-mode") != null) {
642                     throw new FOPException("PDF/A mode already set");
643                 }
644                 renderingOptions.put("pdf-a-mode", pdfAMode.getName());
645                 return 1;
646             } else {
647                 PDFXMode pdfXMode = PDFXMode.valueOf(profile);
648                 if (pdfXMode != null && pdfXMode != PDFXMode.DISABLED) {
649                     if (renderingOptions.get("pdf-x-mode") != null) {
650                         throw new FOPException("PDF/X mode already set");
651                     }
652                     renderingOptions.put("pdf-x-mode", pdfXMode.getName());
653                     return 1;
654                 }
655             }
656             throw new FOPException("Unsupported PDF profile: " + profile);
657         }
658     }
659
660     private void setOutputMode(String JavaDoc mime) throws FOPException {
661         if (outputmode == null) {
662             outputmode = mime;
663         } else {
664             throw new FOPException("you can only set one output method");
665         }
666     }
667
668     private void setLogOption (String JavaDoc option, String JavaDoc level) {
669         if (log instanceof CommandLineLogger
670             || System.getProperty("org.apache.commons.logging.Log") == null) {
671             setLogLevel(level);
672         } else if (log != null) {
673             log.warn("The option " + option + " can only be used");
674             log.warn("with FOP's command line logger,");
675             log.warn("which is the default on the command line.");
676             log.warn("Configure other loggers using Java system properties.");
677         }
678     }
679
680     private void setLogLevel(String JavaDoc level) {
681         // Set the level for future loggers.
682
LogFactory.getFactory().setAttribute("level", level);
683         if (log instanceof CommandLineLogger) {
684             // Set the level for the logger created already.
685
((CommandLineLogger) log).setLogLevel(level);
686         }
687     }
688
689     /**
690      * checks whether all necessary information has been given in a consistent way
691      */

692     private void checkSettings() throws FOPException, FileNotFoundException JavaDoc {
693         if (inputmode == NOT_SET) {
694             throw new FOPException("No input file specified");
695         }
696
697         if (outputmode == null) {
698             throw new FOPException("No output file specified");
699         }
700
701         if ((outputmode.equals(MimeConstants.MIME_FOP_AWT_PREVIEW)
702                 || outputmode.equals(MimeConstants.MIME_FOP_PRINT))
703                     && outfile != null) {
704             throw new FOPException("Output file may not be specified "
705                     + "for AWT or PRINT output");
706         }
707
708         if (inputmode == XSLT_INPUT) {
709             // check whether xml *and* xslt file have been set
710
if (xmlfile == null) {
711                 throw new FOPException("XML file must be specified for the transform mode");
712             }
713             if (xsltfile == null) {
714                 throw new FOPException("XSLT file must be specified for the transform mode");
715             }
716
717             // warning if fofile has been set in xslt mode
718
if (fofile != null) {
719                 log.warn("Can't use fo file with transform mode! Ignoring.\n"
720                                        + "Your input is " + "\n xmlfile: "
721                                        + xmlfile.getAbsolutePath()
722                                        + "\nxsltfile: "
723                                        + xsltfile.getAbsolutePath()
724                                        + "\n fofile: "
725                                        + fofile.getAbsolutePath());
726             }
727             if (!xmlfile.exists()) {
728                 throw new FileNotFoundException JavaDoc("Error: xml file "
729                                                 + xmlfile.getAbsolutePath()
730                                                 + " not found ");
731             }
732             if (!xsltfile.exists()) {
733                 throw new FileNotFoundException JavaDoc("Error: xsl file "
734                                                 + xsltfile.getAbsolutePath()
735                                                 + " not found ");
736             }
737
738         } else if (inputmode == FO_INPUT) {
739             if (outputmode.equals(MimeConstants.MIME_XSL_FO)) {
740                 throw new FOPException(
741                         "FO output mode is only available if you use -xml and -xsl");
742             }
743             if (xmlfile != null || xsltfile != null) {
744                 log.warn("fo input mode, but xmlfile or xslt file are set:");
745                 log.error("xml file: " + xmlfile.toString());
746                 log.error("xslt file: " + xsltfile.toString());
747             }
748             if (!fofile.exists()) {
749                 throw new FileNotFoundException JavaDoc("Error: fo file "
750                                                 + fofile.getAbsolutePath()
751                                                 + " not found ");
752             }
753         } else if (inputmode == AREATREE_INPUT) {
754             if (outputmode.equals(MimeConstants.MIME_XSL_FO)) {
755                 throw new FOPException(
756                         "FO output mode is only available if you use -xml and -xsl");
757             } else if (outputmode.equals(MimeConstants.MIME_FOP_AREA_TREE)) {
758                 throw new FOPException(
759                         "Area Tree Output is not available if Area Tree is used as input!");
760             }
761             if (xmlfile != null || xsltfile != null) {
762                 log.warn("area tree input mode, but xmlfile or xslt file are set:");
763                 log.error("xml file: " + xmlfile.toString());
764                 log.error("xslt file: " + xsltfile.toString());
765             }
766             if (!areatreefile.exists()) {
767                 throw new FileNotFoundException JavaDoc("Error: area tree file "
768                                               + areatreefile.getAbsolutePath()
769                                               + " not found ");
770             }
771         }
772     } // end checkSettings
773

774     /**
775      * Create the user configuration.
776      * @throws FOPException if creating the user configuration fails
777      * @throws IOException
778      */

779     private void createUserConfig() throws FOPException, IOException JavaDoc {
780         if (userConfigFile == null) {
781             return;
782         }
783         try {
784             factory.setUserConfig(userConfigFile);
785         } catch (SAXException JavaDoc e) {
786             throw new FOPException(e);
787         }
788      }
789
790     /**
791      * @return the chosen output format (MIME type)
792      * @throws FOPException for invalid output formats
793      */

794     protected String JavaDoc getOutputFormat() throws FOPException {
795         if (outputmode == null) {
796             throw new FOPException("Renderer has not been set!");
797         }
798         if (outputmode.equals(MimeConstants.MIME_FOP_AREA_TREE)) {
799             renderingOptions.put("fineDetail", isCoarseAreaXml());
800         }
801         return outputmode;
802     }
803
804     /**
805      * Create an InputHandler object based on command-line parameters
806      * @return a new InputHandler instance
807      * @throws IllegalArgumentException if invalid/missing parameters
808      */

809     private InputHandler createInputHandler() throws IllegalArgumentException JavaDoc {
810         switch (inputmode) {
811             case FO_INPUT:
812                 return new InputHandler(fofile);
813             case AREATREE_INPUT:
814                 return new AreaTreeInputHandler(areatreefile);
815             case XSLT_INPUT:
816                 return new InputHandler(xmlfile, xsltfile, xsltParams);
817             default:
818                 throw new IllegalArgumentException JavaDoc("Error creating InputHandler object.");
819         }
820     }
821
822     /**
823      * Get the FOUserAgent for this Command-Line run
824      * @return FOUserAgent instance
825      */

826     protected FOUserAgent getFOUserAgent() {
827         return foUserAgent;
828     }
829
830     /**
831      * Returns the XSL-FO file if set.
832      * @return the XSL-FO file, null if not set
833      */

834     public File JavaDoc getFOFile() {
835         return fofile;
836     }
837
838     /**
839      * Returns the input XML file if set.
840      * @return the input XML file, null if not set
841      */

842     public File JavaDoc getXMLFile() {
843         return xmlfile;
844     }
845
846     /**
847      * Returns the stylesheet to be used for transformation to XSL-FO.
848      * @return stylesheet
849      */

850     public File JavaDoc getXSLFile() {
851         return xsltfile;
852     }
853
854     /**
855      * Returns the output file
856      * @return the output file
857      */

858     public File JavaDoc getOutputFile() {
859         return outfile;
860     }
861
862     /**
863      * Returns the user configuration file to be used.
864      * @return the userconfig.xml file
865      */

866     public File JavaDoc getUserConfigFile() {
867         return userConfigFile;
868     }
869
870     /**
871      * Indicates whether the XML renderer should generate coarse area XML
872      * @return true if coarse area XML is desired
873      */

874     public Boolean JavaDoc isCoarseAreaXml() {
875         return suppressLowLevelAreas;
876     }
877
878     /**
879      * Returns the input file.
880      * @return either the fofile or the xmlfile
881      */

882     public File JavaDoc getInputFile() {
883         switch (inputmode) {
884         case FO_INPUT:
885             return fofile;
886         case XSLT_INPUT:
887             return xmlfile;
888         default:
889             return fofile;
890         }
891     }
892
893     /**
894      * shows the commandline syntax including a summary of all available options and some examples
895      */

896     public static void printUsage() {
897         System.err.println(
898               "\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] "
899                     + "[-awt|-pdf|-mif|-rtf|-tiff|-png|-pcl|-ps|-txt|-at [mime]|-print] <outfile>\n"
900             + " [OPTIONS] \n"
901             + " -d debug mode \n"
902             + " -x dump configuration settings \n"
903             + " -q quiet mode \n"
904             + " -c cfg.xml use additional configuration file cfg.xml\n"
905             + " -l lang the language to use for user information \n"
906             + " -r relaxed/less strict validation (where available)\n"
907             + " -dpi xxx target resolution in dots per inch (dpi) where xxx is a number\n"
908             + " -s for area tree XML, down to block areas only\n"
909             + " -v to show FOP version being used\n\n"
910             + " -o [password] PDF file will be encrypted with option owner password\n"
911             + " -u [password] PDF file will be encrypted with option user password\n"
912             + " -noprint PDF file will be encrypted without printing permission\n"
913             + " -nocopy PDF file will be encrypted without copy content permission\n"
914             + " -noedit PDF file will be encrypted without edit content permission\n"
915             + " -noannotations PDF file will be encrypted without edit annotation permission\n"
916             + " -pdfprofile prof PDF file will be generated with the specified profile\n"
917             + " (Examples for prof: PDF/A-1b or PDF/X-3:2003)\n\n"
918             + " [INPUT] \n"
919             + " infile xsl:fo input file (the same as the next) \n"
920             + " -fo infile xsl:fo input file \n"
921             + " -xml infile xml input file, must be used together with -xsl \n"
922             + " -atin infile area tree input file \n"
923             + " -xsl stylesheet xslt stylesheet \n \n"
924             + " -param name value <value> to use for parameter <name> in xslt stylesheet\n"
925             + " (repeat '-param name value' for each parameter)\n \n"
926             + " [OUTPUT] \n"
927             + " outfile input will be rendered as PDF into outfile\n"
928             + " -pdf outfile input will be rendered as PDF (outfile req'd)\n"
929             + " -pdfa1b outfile input will be rendered as PDF/A-1b compliant PDF\n"
930             + " (outfile req'd, same as \"-pdf outfile -pdfprofile PDF/A-1b\")\n"
931             + " -awt input will be displayed on screen \n"
932             + " -mif outfile input will be rendered as MIF (FrameMaker) (outfile req'd)\n"
933             + " -rtf outfile input will be rendered as RTF (outfile req'd)\n"
934             + " -tiff outfile input will be rendered as TIFF (outfile req'd)\n"
935             + " -png outfile input will be rendered as PNG (outfile req'd)\n"
936             + " -pcl outfile input will be rendered as PCL (outfile req'd) \n"
937             + " -ps outfile input will be rendered as PostScript (outfile req'd) \n"
938             + " -txt outfile input will be rendered as plain text (outfile req'd) \n"
939             + " -svg outfile input will be rendered as an SVG slides file (outfile req'd) \n"
940             + " -at [mime] out representation of area tree as XML (outfile req'd) \n"
941             + " specify optional mime output to allow AT to be converted\n"
942             + " to final format later\n"
943             + " -print input file will be rendered and sent to the printer \n"
944             + " see options with \"-print help\" \n"
945             + " -out mime outfile input will be rendered using the given MIME type\n"
946             + " (outfile req'd) Example: \"-out application/pdf D:\\out.pdf\"\n"
947             + " (Tip: \"-out list\" prints the list of supported MIME types)\n"
948             + "\n"
949             + " -foout outfile input will only be XSL transformed. The intermediate \n"
950             + " XSL-FO file is saved and no rendering is performed. \n"
951             + " (Only available if you use -xml and -xsl parameters)\n\n"
952             + "\n"
953             + " [Examples]\n" + " Fop foo.fo foo.pdf \n"
954             + " Fop -fo foo.fo -pdf foo.pdf (does the same as the previous line)\n"
955             + " Fop -xml foo.xml -xsl foo.xsl -pdf foo.pdf\n"
956             + " Fop -xml foo.xml -xsl foo.xsl -foout foo.fo\n"
957             + " Fop foo.fo -mif foo.mif\n"
958             + " Fop foo.fo -rtf foo.rtf\n"
959             + " Fop foo.fo -print or Fop -print foo.fo \n"
960             + " Fop foo.fo -awt \n");
961     }
962
963     /**
964      * shows the options for print output
965      */

966     private void printUsagePrintOutput() {
967         System.err.println("USAGE: -print [-Dstart=i] [-Dend=i] [-Dcopies=i] [-Deven=true|false] "
968                            + " org.apache.fop.apps.Fop (..) -print \n"
969                            + "Example:\n"
970                            + "java -Dstart=1 -Dend=2 org.apache.Fop.apps.Fop infile.fo -print ");
971     }
972
973     /**
974      * Outputs all commandline settings
975      */

976     private void dumpConfiguration() {
977         log.info("Input mode: ");
978         switch (inputmode) {
979         case NOT_SET:
980             log.info("not set");
981             break;
982         case FO_INPUT:
983             log.info("FO ");
984             log.info("fo input file: " + fofile.toString());
985             break;
986         case XSLT_INPUT:
987             log.info("xslt transformation");
988             log.info("xml input file: " + xmlfile.toString());
989             log.info("xslt stylesheet: " + xsltfile.toString());
990             break;
991         default:
992             log.info("unknown input type");
993         }
994         log.info("Output mode: ");
995         if (outputmode == null) {
996             log.info("not set");
997         } else if (MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputmode)) {
998             log.info("awt on screen");
999             if (outfile != null) {
1000                log.error("awt mode, but outfile is set:");
1001                log.info("out file: " + outfile.toString());
1002            }
1003        } else if (MimeConstants.MIME_FOP_PRINT.equals(outputmode)) {
1004            log.info("print directly");
1005            if (outfile != null) {
1006                log.error("print mode, but outfile is set:");
1007                log.error("out file: " + outfile.toString());
1008            }
1009        } else if (MimeConstants.MIME_FOP_AREA_TREE.equals(outputmode)) {
1010            log.info("area tree");
1011            if (mimicRenderer != null) {
1012              log.info("mimic renderer: " + mimicRenderer);
1013            }
1014            log.info("output file: " + outfile.toString());
1015        } else {
1016            log.info(outputmode);
1017            log.info("output file: " + outfile.toString());
1018        }
1019
1020        log.info("OPTIONS");
1021
1022        if (userConfigFile != null) {
1023            log.info("user configuration file: "
1024                                 + userConfigFile.toString());
1025        } else {
1026            log.info("no user configuration file is used [default]");
1027        }
1028    }
1029
1030}
1031
1032
Popular Tags