* Parse the command line arguments.
* @param args the command line arguments.
* @throws FOPException for general errors
- * @throws FileNotFoundException if an input file wasn't found
* @throws IOException if the the configuration file could not be loaded
* @return true if the processing can continue, false to abort
*/
i = i + parseFOOutputOption(args, i);
} else if (args[i].equals("-out")) {
i = i + parseCustomOutputOption(args, i);
- } else if (args[i].charAt(0) != '-') {
- i = i + parseUnknownOption(args, i);
} else if (args[i].equals("-at")) {
i = i + parseAreaTreeOption(args, i);
} else if (args[i].equals("-v")) {
getPDFEncryptionParams().setAllowEditContent(false);
} else if (args[i].equals("-noannotations")) {
getPDFEncryptionParams().setAllowEditAnnotations(false);
+ } else if (!isOption(args[i])) {
+ i = i + parseUnknownOption(args, i);
} else {
printUsage();
return false;
private int parseConfigurationOption(String[] args, int i) throws FOPException {
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("if you use '-c', you must specify "
+ "the name of the configuration file");
} else {
private int parseLanguageOption(String[] args, int i) throws FOPException {
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("if you use '-l', you must specify a language");
} else {
Locale.setDefault(new Locale(args[i + 1], ""));
private int parseResolution(String[] args, int i) throws FOPException {
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException(
"if you use '-dpi', you must specify a resolution (dots per inch)");
} else {
private int parseFOInputOption(String[] args, int i) throws FOPException {
inputmode = FO_INPUT;
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the fo file for the '-fo' option");
} else {
String filename = args[i + 1];
private int parseXSLInputOption(String[] args, int i) throws FOPException {
inputmode = XSLT_INPUT;
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the stylesheet "
+ "file for the '-xsl' option");
} else {
private int parseXMLInputOption(String[] args, int i) throws FOPException {
inputmode = XSLT_INPUT;
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the input file "
+ "for the '-xml' option");
} else {
private int parsePDFOutputOption(String[] args, int i, String pdfAMode) throws FOPException {
setOutputMode(MimeConstants.MIME_PDF);
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the PDF output file");
} else {
setOutputFile(args[i + 1]);
}
}
+ /**
+ * Checks whether the given argument is the next option or the specification of
+ * stdin/stdout.
+ *
+ * TODO this is very ad-hoc and should be better handled. Consider the adoption of
+ * Apache Commons CLI.
+ *
+ * @param arg an argument
+ * @return true if the argument is an option ("-something"), false otherwise
+ */
+ private boolean isOption(String arg) {
+ return arg.length() > 1 && arg.startsWith("-");
+ }
+
private boolean isSystemInOutFile(String filename) {
- return "#".equals(filename);
+ return "-".equals(filename);
}
private int parseMIFOutputOption(String[] args, int i) throws FOPException {
setOutputMode(MimeConstants.MIME_MIF);
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the MIF output file");
} else {
setOutputFile(args[i + 1]);
private int parseRTFOutputOption(String[] args, int i) throws FOPException {
setOutputMode(MimeConstants.MIME_RTF);
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the RTF output file");
} else {
setOutputFile(args[i + 1]);
private int parseTIFFOutputOption(String[] args, int i) throws FOPException {
setOutputMode(MimeConstants.MIME_TIFF);
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the TIFF output file");
} else {
setOutputFile(args[i + 1]);
private int parsePNGOutputOption(String[] args, int i) throws FOPException {
setOutputMode(MimeConstants.MIME_PNG);
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the PNG output file");
} else {
setOutputFile(args[i + 1]);
private int parseCopiesOption(String[] args, int i) throws FOPException {
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the number of copies");
} else {
renderingOptions.put(PrintRenderer.COPIES, new Integer(args[i + 1]));
private int parsePCLOutputOption(String[] args, int i) throws FOPException {
setOutputMode(MimeConstants.MIME_PCL);
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the PDF output file");
} else {
setOutputFile(args[i + 1]);
private int parsePostscriptOutputOption(String[] args, int i) throws FOPException {
setOutputMode(MimeConstants.MIME_POSTSCRIPT);
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the PostScript output file");
} else {
setOutputFile(args[i + 1]);
private int parseTextOutputOption(String[] args, int i) throws FOPException {
setOutputMode(MimeConstants.MIME_PLAIN_TEXT);
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the text output file");
} else {
setOutputFile(args[i + 1]);
private int parseSVGOutputOption(String[] args, int i) throws FOPException {
setOutputMode(MimeConstants.MIME_SVG);
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the SVG output file");
} else {
setOutputFile(args[i + 1]);
private int parseAFPOutputOption(String[] args, int i) throws FOPException {
setOutputMode(MimeConstants.MIME_AFP);
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the AFP output file");
} else {
setOutputFile(args[i + 1]);
private int parseFOOutputOption(String[] args, int i) throws FOPException {
setOutputMode(MimeConstants.MIME_XSL_FO);
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the FO output file");
} else {
setOutputFile(args[i + 1]);
}
}
if ((i + 2 >= args.length)
- || (args[i + 1].charAt(0) == '-')
- || (args[i + 2].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))
+ || (isOption(args[i + 2]))) {
throw new FOPException("you must specify the output format and the output file");
} else {
setOutputMode(mime);
private int parseUnknownOption(String[] args, int i) throws FOPException {
if (inputmode == NOT_SET) {
inputmode = FO_INPUT;
- fofile = new File(args[i]);
+ String filename = args[i];
+ if (isSystemInOutFile(filename)) {
+ this.useStdIn = true;
+ } else {
+ fofile = new File(filename);
+ }
} else if (outputmode == null) {
outputmode = MimeConstants.MIME_PDF;
setOutputFile(args[i]);
private int parseAreaTreeOption(String[] args, int i) throws FOPException {
setOutputMode(MimeConstants.MIME_FOP_AREA_TREE);
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the area-tree output file");
} else if ((i + 2 == args.length)
- || (args[i + 2].charAt(0) == '-')) {
+ || (isOption(args[i + 2]))) {
// only output file is specified
setOutputFile(args[i + 1]);
return 1;
private int parseAreaTreeInputOption(String[] args, int i) throws FOPException {
inputmode = AREATREE_INPUT;
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the Area Tree file for the '-atin' option");
} else {
String filename = args[i + 1];
private int parseImageInputOption(String[] args, int i) throws FOPException {
inputmode = IMAGE_INPUT;
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("you must specify the image file for the '-imagein' option");
} else {
String filename = args[i + 1];
private int parsePDFOwnerPassword(String[] args, int i) throws FOPException {
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
getPDFEncryptionParams().setOwnerPassword("");
return 0;
} else {
private int parsePDFUserPassword(String[] args, int i) throws FOPException {
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
getPDFEncryptionParams().setUserPassword("");
return 0;
} else {
private int parsePDFProfile(String[] args, int i) throws FOPException {
if ((i + 1 == args.length)
- || (args[i + 1].charAt(0) == '-')) {
+ || (isOption(args[i + 1]))) {
throw new FOPException("You must specify a PDF profile");
} else {
String profile = args[i + 1];
* @return a new InputHandler instance
* @throws IllegalArgumentException if invalid/missing parameters
*/
- private InputHandler createInputHandler() throws IllegalArgumentException {
+ private InputHandler createInputHandler() {
switch (inputmode) {
case FO_INPUT:
return new InputHandler(fofile);
+ " (Examples for prof: PDF/A-1b or PDF/X-3:2003)\n\n"
+ " [INPUT] \n"
+ " infile xsl:fo input file (the same as the next) \n"
- + " (use # for infile to pipe input from stdin)\n"
+ + " (use '-' for infile to pipe input from stdin)\n"
+ " -fo infile xsl:fo input file \n"
+ " -xml infile xml input file, must be used together with -xsl \n"
+ " -atin infile area tree input file \n"
+ " (repeat '-param name value' for each parameter)\n \n"
+ " [OUTPUT] \n"
+ " outfile input will be rendered as PDF into outfile\n"
- + " (use # for outfile to pipe output to stdout)\n"
+ + " (use '-' for outfile to pipe output to stdout)\n"
+ " -pdf outfile input will be rendered as PDF (outfile req'd)\n"
+ " -pdfa1b outfile input will be rendered as PDF/A-1b compliant PDF\n"
+ " (outfile req'd, same as \"-pdf outfile -pdfprofile PDF/A-1b\")\n"
+ " Fop -fo foo.fo -pdf foo.pdf (does the same as the previous line)\n"
+ " Fop -xml foo.xml -xsl foo.xsl -pdf foo.pdf\n"
+ " Fop -xml foo.xml -xsl foo.xsl -foout foo.fo\n"
- + " Fop -xml # -xsl foo.xsl -pdf #\n"
+ + " Fop -xml - -xsl foo.xsl -pdf -\n"
+ " Fop foo.fo -mif foo.mif\n"
+ " Fop foo.fo -rtf foo.rtf\n"
+ " Fop foo.fo -print\n"