Refactored code to throw FOPExceptions so embedding FOP wont' cause app server JVMs to exit. PR: Obtained from: Submitted by: Reviewed by: git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194208 13f79535-47bb-0310-9956-ffa450edef68pull/33/head
private Translator resource; | private Translator resource; | ||||
public AWTStarter (CommandLineOptions commandLineOptions) { | |||||
public AWTStarter (CommandLineOptions commandLineOptions) | |||||
throws FOPException | |||||
{ | |||||
super(commandLineOptions); | super(commandLineOptions); | ||||
init(); | init(); | ||||
} | } | ||||
} | } | ||||
public void run () { | |||||
public void run () | |||||
throws FOPException | |||||
{ | |||||
Driver driver = new Driver(); | Driver driver = new Driver(); | ||||
if (errorDump) { | if (errorDump) { | ||||
driver.setErrorDump(true); | driver.setErrorDump(true); | ||||
XMLReader parser = inputHandler.getParser(); | XMLReader parser = inputHandler.getParser(); | ||||
if (parser == null) { | if (parser == null) { | ||||
MessageHandler.errorln("ERROR: Unable to create SAX parser"); | |||||
System.exit(1); | |||||
throw new FOPException("Unable to create SAX parser"); | |||||
} | } | ||||
setParserFeatures(parser); | setParserFeatures(parser); | ||||
frame.showPage(); | frame.showPage(); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
MessageHandler.errorln("FATAL ERROR: " + e.getMessage()); | |||||
e.printStackTrace(); | |||||
System.exit(1); | |||||
if (e instanceof FOPException) { | |||||
throw (FOPException)e; | |||||
} | |||||
throw new FOPException(e); | |||||
} | } | ||||
} | } |
//java | //java | ||||
import java.util.Vector; | import java.util.Vector; | ||||
import java.io.File; | import java.io.File; | ||||
import java.io.FileNotFoundException; | |||||
// FOP | // FOP | ||||
import org.apache.fop.messaging.MessageHandler; | import org.apache.fop.messaging.MessageHandler; | ||||
private static final int TXT_OUTPUT = 6; | private static final int TXT_OUTPUT = 6; | ||||
/* use debug mode*/ | /* use debug mode*/ | ||||
Boolean errorDump = null; | |||||
Boolean errorDump = new Boolean(false); | |||||
/* show configuration information */ | /* show configuration information */ | ||||
Boolean dumpConfiguration = null; | |||||
Boolean dumpConfiguration = new Boolean(false); | |||||
/*suppress any progress information */ | /*suppress any progress information */ | ||||
Boolean quiet = null; | |||||
Boolean quiet = new Boolean(false); | |||||
/* name of user configuration file*/ | /* name of user configuration file*/ | ||||
File userConfigFile = null; | File userConfigFile = null; | ||||
/* name of input fo file */ | /* name of input fo file */ | ||||
/* language for user information */ | /* language for user information */ | ||||
String language = null; | String language = null; | ||||
public CommandLineOptions (String [] args) { | |||||
parseOptions(args); | |||||
checkSettings (); | |||||
if (errorDump != null && errorDump.booleanValue()) { | |||||
debug(); | |||||
} | |||||
public CommandLineOptions (String [] args) | |||||
throws FOPException, FileNotFoundException | |||||
{ | |||||
boolean optionsParsed = true; | |||||
try { | |||||
optionsParsed = parseOptions(args); | |||||
if (optionsParsed) { | |||||
checkSettings (); | |||||
if (errorDump != null && errorDump.booleanValue()) { | |||||
debug(); | |||||
} | |||||
} | |||||
} | |||||
catch (FOPException e) { | |||||
printUsage(); | |||||
throw e; | |||||
} | |||||
catch (java.io.FileNotFoundException e) { | |||||
printUsage(); | |||||
throw e; | |||||
} | |||||
} | } | ||||
/** | /** | ||||
* parses the commandline arguments | |||||
*/ | |||||
private void parseOptions (String args[]) { | |||||
* parses the commandline arguments | |||||
* @return true if parse was successful and procesing can continue, false if processing should stop | |||||
* @exception FOPException if there was an error in the format of the options | |||||
*/ | |||||
private boolean parseOptions (String args[]) | |||||
throws FOPException | |||||
{ | |||||
for (int i = 0; i < args.length; i++) { | for (int i = 0; i < args.length; i++) { | ||||
if (args[i].equals("-d") || args[i].equals("--full-error-dump")) { | if (args[i].equals("-d") || args[i].equals("--full-error-dump")) { | ||||
errorDump = new Boolean(true); | errorDump = new Boolean(true); | ||||
} else if (args[i].equals("-c")) { | } else if (args[i].equals("-c")) { | ||||
if ((i + 1 == args.length) || | if ((i + 1 == args.length) || | ||||
(args[i + 1].charAt(0) == '-')) { | (args[i + 1].charAt(0) == '-')) { | ||||
MessageHandler.errorln("ERROR: if you use '-c', you must specify the name of the configuration file"); | |||||
printUsage(); | |||||
throw new FOPException("if you use '-c', you must specify the name of the configuration file"); | |||||
} else { | } else { | ||||
userConfigFile = new File (args[i + 1]); | userConfigFile = new File (args[i + 1]); | ||||
i++; | i++; | ||||
} else if (args[i].equals("-l")) { | } else if (args[i].equals("-l")) { | ||||
if ((i + 1 == args.length) || | if ((i + 1 == args.length) || | ||||
(args[i + 1].charAt(0) == '-')) { | (args[i + 1].charAt(0) == '-')) { | ||||
MessageHandler.errorln("ERROR: if you use '-l', you must specify a language"); | |||||
printUsage(); | |||||
throw new FOPException("if you use '-l', you must specify a language"); | |||||
} else { | } else { | ||||
language = args[i + 1]; | language = args[i + 1]; | ||||
i++; | i++; | ||||
inputmode = FO_INPUT; | inputmode = FO_INPUT; | ||||
if ((i + 1 == args.length) || | if ((i + 1 == args.length) || | ||||
(args[i + 1].charAt(0) == '-')) { | (args[i + 1].charAt(0) == '-')) { | ||||
MessageHandler.errorln("ERROR: you must specify the fo file"); | |||||
printUsage(); | |||||
throw new FOPException("you must specify the fo file for the '-fo' option"); | |||||
} else { | } else { | ||||
fofile = new File (args[i + 1]); | fofile = new File (args[i + 1]); | ||||
i++; | i++; | ||||
inputmode = XSLT_INPUT; | inputmode = XSLT_INPUT; | ||||
if ((i + 1 == args.length) || | if ((i + 1 == args.length) || | ||||
(args[i + 1].charAt(0) == '-')) { | (args[i + 1].charAt(0) == '-')) { | ||||
MessageHandler.errorln("ERROR: you must specify the stylesheet file"); | |||||
printUsage(); | |||||
throw new FOPException("you must specify the stylesheet file for the '-xsl' option"); | |||||
} else { | } else { | ||||
xsltfile = new File(args[i + 1]); | xsltfile = new File(args[i + 1]); | ||||
i++; | i++; | ||||
inputmode = XSLT_INPUT; | inputmode = XSLT_INPUT; | ||||
if ((i + 1 == args.length) || | if ((i + 1 == args.length) || | ||||
(args[i + 1].charAt(0) == '-')) { | (args[i + 1].charAt(0) == '-')) { | ||||
MessageHandler.errorln("ERROR: you must specify the input file"); | |||||
printUsage(); | |||||
throw new FOPException("you must specify the input file for the '-xml' option"); | |||||
} else { | } else { | ||||
xmlfile = new File(args[i + 1]); | xmlfile = new File(args[i + 1]); | ||||
i++; | i++; | ||||
} | } | ||||
} else if (args[i].equals("-awt")) { | } else if (args[i].equals("-awt")) { | ||||
if (outputmode == NOT_SET) { | |||||
outputmode = AWT_OUTPUT; | |||||
} else { | |||||
MessageHandler.errorln("ERROR: you can only set one output method"); | |||||
printUsage(); | |||||
} | |||||
} else if (args[i].equals("-pdf")) { | |||||
if (outputmode == NOT_SET) { | |||||
outputmode = PDF_OUTPUT; | |||||
} else { | |||||
MessageHandler.errorln("ERROR: you can only set one output method"); | |||||
printUsage(); | |||||
} | |||||
if ((i + 1 == args.length) || | |||||
(args[i + 1].charAt(0) == '-')) { | |||||
MessageHandler.errorln("ERROR: you must specify the pdf output file"); | |||||
printUsage(); | |||||
setOutputMode(AWT_OUTPUT); | |||||
} else if (args[i].equals("-pdf")) { | |||||
setOutputMode(PDF_OUTPUT); | |||||
if ((i + 1 == args.length) || | |||||
(args[i + 1].charAt(0) == '-')) { | |||||
throw new FOPException("you must specify the pdf output file"); | |||||
} else { | } else { | ||||
outfile = new File (args[i + 1]); | outfile = new File (args[i + 1]); | ||||
i++; | i++; | ||||
} | } | ||||
} else if (args[i].equals("-mif")) { | } else if (args[i].equals("-mif")) { | ||||
if (outputmode == NOT_SET) { | |||||
outputmode = MIF_OUTPUT; | |||||
} else { | |||||
MessageHandler.errorln("ERROR: you can only set one output method"); | |||||
printUsage(); | |||||
} | |||||
setOutputMode(MIF_OUTPUT); | |||||
if ((i + 1 == args.length) || | if ((i + 1 == args.length) || | ||||
(args[i + 1].charAt(0) == '-')) { | (args[i + 1].charAt(0) == '-')) { | ||||
MessageHandler.errorln("ERROR: you must specify the mif output file"); | |||||
printUsage(); | |||||
throw new FOPException("you must specify the mif output file"); | |||||
} else { | } else { | ||||
outfile = new File(args[i + 1]); | outfile = new File(args[i + 1]); | ||||
i++; | i++; | ||||
} | } | ||||
} else if (args[i].equals("-print")) { | } else if (args[i].equals("-print")) { | ||||
if (outputmode == NOT_SET) { | |||||
outputmode = PRINT_OUTPUT; | |||||
} else { | |||||
MessageHandler.errorln("ERROR: you can only set one output method"); | |||||
printUsage(); | |||||
} | |||||
setOutputMode(PRINT_OUTPUT); | |||||
//show print help | //show print help | ||||
if (i + 1 < args.length) { | if (i + 1 < args.length) { | ||||
if (args[i + 1].equals("help")) { | if (args[i + 1].equals("help")) { | ||||
printUsagePrintOutput(); | printUsagePrintOutput(); | ||||
} | |||||
return false; | |||||
} | |||||
} | } | ||||
} else if (args[i].equals("-pcl")) { | } else if (args[i].equals("-pcl")) { | ||||
if (outputmode == NOT_SET) { | |||||
outputmode = PCL_OUTPUT; | |||||
} else { | |||||
MessageHandler.errorln("ERROR: you can only set one output method"); | |||||
printUsage(); | |||||
} | |||||
setOutputMode(PCL_OUTPUT); | |||||
if ((i + 1 == args.length) || | if ((i + 1 == args.length) || | ||||
(args[i + 1].charAt(0) == '-')) { | (args[i + 1].charAt(0) == '-')) { | ||||
MessageHandler.errorln("ERROR: you must specify the pdf output file"); | |||||
printUsage(); | |||||
throw new FOPException("you must specify the pdf output file"); | |||||
} else { | } else { | ||||
outfile = new File (args[i + 1]); | outfile = new File (args[i + 1]); | ||||
i++; | i++; | ||||
} | } | ||||
} else if (args[i].equals("-txt")) { | } else if (args[i].equals("-txt")) { | ||||
if (outputmode == NOT_SET) { | |||||
outputmode = TXT_OUTPUT; | |||||
} else { | |||||
MessageHandler.errorln("ERROR: you can only set one output method"); | |||||
printUsage(); | |||||
} | |||||
setOutputMode(TXT_OUTPUT); | |||||
if ((i + 1 == args.length) || | if ((i + 1 == args.length) || | ||||
(args[i + 1].charAt(0) == '-')) { | (args[i + 1].charAt(0) == '-')) { | ||||
MessageHandler.errorln("ERROR: you must specify the pdf output file"); | |||||
printUsage(); | |||||
throw new FOPException("you must specify the text output file"); | |||||
} else { | } else { | ||||
outfile = new File (args[i + 1]); | outfile = new File (args[i + 1]); | ||||
i++; | i++; | ||||
outputmode = PDF_OUTPUT; | outputmode = PDF_OUTPUT; | ||||
outfile = new File(args[i]); | outfile = new File(args[i]); | ||||
} else { | } else { | ||||
MessageHandler.errorln( | |||||
"ERROR: Don't know what to do with " + args[i]); | |||||
printUsage(); | |||||
throw new FOPException("Don't know what to do with " + args[i]); | |||||
} | } | ||||
} else { | } else { | ||||
printUsage(); | printUsage(); | ||||
} | |||||
return false; | |||||
} | |||||
} | } | ||||
return true; | |||||
} //end parseOptions | } //end parseOptions | ||||
private void setOutputMode(int mode) | |||||
throws FOPException | |||||
{ | |||||
if (outputmode == NOT_SET) { | |||||
outputmode = mode; | |||||
} else { | |||||
throw new FOPException("you can only set one output method"); | |||||
} | |||||
} | |||||
/** | /** | ||||
* checks whether all necessary information has been given in a consistent way | * checks whether all necessary information has been given in a consistent way | ||||
*/ | */ | ||||
private void checkSettings () { | |||||
private void checkSettings () | |||||
throws FOPException, FileNotFoundException | |||||
{ | |||||
if (inputmode == NOT_SET) { | if (inputmode == NOT_SET) { | ||||
MessageHandler.errorln("ERROR: you have to specify an input file"); | |||||
printUsage(); | |||||
} | |||||
throw new FOPException("No input file specified"); | |||||
} | |||||
if (outputmode == NOT_SET) { | if (outputmode == NOT_SET) { | ||||
MessageHandler.errorln("ERROR: you have to specify an output mode"); | |||||
printUsage(); | |||||
} | |||||
throw new FOPException("No output file specified"); | |||||
} | |||||
if (inputmode == XSLT_INPUT) { | if (inputmode == XSLT_INPUT) { | ||||
//check whether xml *and* xslt file have been set | //check whether xml *and* xslt file have been set | ||||
if (xmlfile == null || xsltfile == null) { | |||||
MessageHandler.errorln( | |||||
"ERROR: if you want to use an xml file transformed with an xslt file as input\n" + | |||||
" you must specify both files. \n" + | |||||
" Your input is \nxmlfile: " + | |||||
xmlfile.getAbsolutePath() + "\nxsltfile: " + | |||||
xsltfile.getAbsolutePath()); | |||||
printUsage(); | |||||
} | |||||
if (xmlfile == null) { | |||||
throw new FOPException("XML file must be specified for the tranform mode"); | |||||
} | |||||
if (xsltfile == null) { | |||||
throw new FOPException("XSLT file must be specified for the tranform mode"); | |||||
} | |||||
//warning if fofile has been set in xslt mode | //warning if fofile has been set in xslt mode | ||||
if (fofile != null) { | if (fofile != null) { | ||||
MessageHandler.errorln( | MessageHandler.errorln( | ||||
"ERROR: Can't use fo file in transformation!" + | |||||
" Your input is \nxmlfile: " + | |||||
xmlfile.getAbsolutePath() + "\nxsltfile: " + | |||||
xsltfile.getAbsolutePath() + "\nfofile: " + | |||||
fofile.getAbsolutePath()); | |||||
"WARNING: Can't use fo file with transform mode! Ignoring.\n" + | |||||
"Your input is "+ | |||||
"\n xmlfile: " + xmlfile.getAbsolutePath() + | |||||
"\nxsltfile: " + xsltfile.getAbsolutePath() + | |||||
"\n fofile: " + fofile.getAbsolutePath()); | |||||
} | } | ||||
if (!xmlfile.exists()) { | if (!xmlfile.exists()) { | ||||
MessageHandler.errorln("ERROR: xml file " + | |||||
xmlfile.getAbsolutePath() + " not found "); | |||||
System.exit(1); | |||||
throw new FileNotFoundException("xml file " + | |||||
xmlfile.getAbsolutePath() + " not found "); | |||||
} | } | ||||
if (!xsltfile.exists()) { | if (!xsltfile.exists()) { | ||||
MessageHandler.errorln("ERROR: xsl file " + | |||||
throw new FileNotFoundException("xsl file " + | |||||
xsltfile.getAbsolutePath() + " not found "); | xsltfile.getAbsolutePath() + " not found "); | ||||
System.exit(1); | |||||
} | } | ||||
} else if (inputmode == FO_INPUT) { | } else if (inputmode == FO_INPUT) { | ||||
if (xmlfile != null || xsltfile != null) { | if (xmlfile != null || xsltfile != null) { | ||||
MessageHandler.logln("ERROR: fo input mode, but xmlfile or xslt file are set:"); | |||||
MessageHandler.logln("xml file: " + xmlfile.toString()); | |||||
MessageHandler.logln("xslt file: " + xsltfile.toString()); | |||||
MessageHandler.errorln("WARNING: fo input mode, but xmlfile or xslt file are set:"); | |||||
MessageHandler.errorln("xml file: " + xmlfile.toString()); | |||||
MessageHandler.errorln("xslt file: " + xsltfile.toString()); | |||||
} | } | ||||
if (!fofile.exists()) { | if (!fofile.exists()) { | ||||
MessageHandler.errorln("ERROR: fo file " + | |||||
throw new FileNotFoundException("fo file " + | |||||
fofile.getAbsolutePath() + " not found "); | fofile.getAbsolutePath() + " not found "); | ||||
System.exit(1); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
public Starter getStarter() { | |||||
public Starter getStarter() | |||||
throws FOPException | |||||
{ | |||||
switch (outputmode) { | switch (outputmode) { | ||||
case PDF_OUTPUT: | case PDF_OUTPUT: | ||||
return new CommandLineStarter(this); | return new CommandLineStarter(this); | ||||
new Class[]{CommandLineOptions.class}). | new Class[]{CommandLineOptions.class}). | ||||
newInstance(new Object[]{this})); | newInstance(new Object[]{this})); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
MessageHandler.errorln( | |||||
"ERROR: AWTStarter could not be loaded. " + | |||||
e.toString()); | |||||
return(null); | |||||
if (e instanceof FOPException) { | |||||
throw (FOPException)e; | |||||
} | |||||
throw new FOPException("AWTStarter could not be loaded.",e); | |||||
} | } | ||||
case MIF_OUTPUT: | case MIF_OUTPUT: | ||||
return new CommandLineStarter(this); | return new CommandLineStarter(this); | ||||
new Class[]{CommandLineOptions.class}). | new Class[]{CommandLineOptions.class}). | ||||
newInstance(new Object[]{this})); | newInstance(new Object[]{this})); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
MessageHandler.errorln( | |||||
"ERROR: PrintStarter could not be loaded. " + | |||||
e.toString()); | |||||
return(null); | |||||
if (e instanceof FOPException) { | |||||
throw (FOPException)e; | |||||
} | |||||
throw new FOPException("PrintStarter could not be loaded.",e); | |||||
} | } | ||||
default: | default: | ||||
* shows the commandline syntax including a summary of all available options and some examples | * shows the commandline syntax including a summary of all available options and some examples | ||||
*/ | */ | ||||
public static void printUsage() { | public static void printUsage() { | ||||
MessageHandler.logln( | |||||
MessageHandler.errorln( | |||||
"\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] [-awt|-pdf|-mif|-pcl|-txt|-print] <outfile>\n" + | "\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] [-awt|-pdf|-mif|-pcl|-txt|-print] <outfile>\n" + | ||||
" [OPTIONS] \n" + " -d debug mode \n" + | " [OPTIONS] \n" + " -d debug mode \n" + | ||||
" -x dump configuration settings \n" + | " -x dump configuration settings \n" + | ||||
" Fop -fo foo.fo -pdf foo.pdf (does the same as the previous line)\n" + | " Fop -fo foo.fo -pdf foo.pdf (does the same as the previous line)\n" + | ||||
" Fop -xsl foo.xsl -xml foo.xml -pdf foo.pdf\n" + | " Fop -xsl foo.xsl -xml foo.xml -pdf foo.pdf\n" + | ||||
" Fop foo.fo -mif foo.mif\n" + | " Fop foo.fo -mif foo.mif\n" + | ||||
" Fop foo.fo -print or Fop -print foo.fo \n" + " Fop foo.fo -awt "); | |||||
System.exit(1); | |||||
" Fop foo.fo -print or Fop -print foo.fo \n" + " Fop foo.fo -awt \n"); | |||||
} | } | ||||
/** | /** | ||||
"USAGE: -print [-Dstart=i] [-Dend=i] [-Dcopies=i] [-Deven=true|false] " + | "USAGE: -print [-Dstart=i] [-Dend=i] [-Dcopies=i] [-Deven=true|false] " + | ||||
" org.apache.fop.apps.Fop (..) -print \n" + | " org.apache.fop.apps.Fop (..) -print \n" + | ||||
"Example:\n" + "java -Dstart=1 -Dend=2 org.apache.Fop.apps.Fop infile.fo -print "); | "Example:\n" + "java -Dstart=1 -Dend=2 org.apache.Fop.apps.Fop infile.fo -print "); | ||||
System.exit(1); | |||||
} | } | ||||
for (int i = 0; i < args.length; i++) { | for (int i = 0; i < args.length; i++) { | ||||
MessageHandler.logln(">"+args[i]+"<"); | MessageHandler.logln(">"+args[i]+"<"); | ||||
}*/ | }*/ | ||||
CommandLineOptions options = new CommandLineOptions (args); | |||||
try { | |||||
CommandLineOptions options = new CommandLineOptions (args); | |||||
} | |||||
catch (Exception e) { | |||||
e.printStackTrace(); | |||||
} | |||||
//options.debug(); | //options.debug(); | ||||
} | } | ||||
} | } |
CommandLineOptions commandLineOptions; | CommandLineOptions commandLineOptions; | ||||
boolean errorDump; | boolean errorDump; | ||||
public CommandLineStarter (CommandLineOptions commandLineOptions) { | |||||
public CommandLineStarter (CommandLineOptions commandLineOptions) | |||||
throws FOPException | |||||
{ | |||||
this.commandLineOptions = commandLineOptions; | this.commandLineOptions = commandLineOptions; | ||||
options.setCommandLineOptions(commandLineOptions); | |||||
errorDump = Configuration.getBooleanValue("debugMode").booleanValue(); | |||||
super.setInputHandler(commandLineOptions.getInputHandler()); | |||||
options.setCommandLineOptions(commandLineOptions); | |||||
errorDump = Configuration.getBooleanValue("debugMode").booleanValue(); | |||||
super.setInputHandler(commandLineOptions.getInputHandler()); | |||||
} | } | ||||
public void run() { | |||||
/** | |||||
* Run the format. | |||||
* @exception FOPException if there is an error during processing | |||||
*/ | |||||
public void run() | |||||
throws FOPException | |||||
{ | |||||
String version = Version.getVersion(); | String version = Version.getVersion(); | ||||
MessageHandler.logln(version); | MessageHandler.logln(version); | ||||
XMLReader parser = inputHandler.getParser(); | XMLReader parser = inputHandler.getParser(); | ||||
setParserFeatures(parser,errorDump); | |||||
setParserFeatures(parser); | |||||
Driver driver = new Driver(); | Driver driver = new Driver(); | ||||
if (errorDump) { | if (errorDump) { | ||||
driver.setOutputStream(new FileOutputStream(commandLineOptions.getOutputFile())); | driver.setOutputStream(new FileOutputStream(commandLineOptions.getOutputFile())); | ||||
driver.render(); | driver.render(); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
MessageHandler.errorln("FATAL ERROR: " + e.getMessage()); | |||||
if (errorDump) { | |||||
e.printStackTrace(); | |||||
} | |||||
System.exit(1); | |||||
} | |||||
if (e instanceof FOPException) { | |||||
throw (FOPException) e; | |||||
} | |||||
throw new FOPException(e); | |||||
} | |||||
} | } | ||||
} | } |
return super.fileInputSource(fofile); | return super.fileInputSource(fofile); | ||||
} | } | ||||
public XMLReader getParser() { | |||||
XMLReader parser = super.createParser(); | |||||
if (parser == null) { | |||||
MessageHandler.errorln("ERROR: Unable to create SAX parser"); | |||||
System.exit(1); | |||||
} | |||||
return parser; | |||||
public XMLReader getParser() | |||||
throws FOPException | |||||
{ | |||||
return super.createParser(); | |||||
} | } | ||||
} | } | ||||
package org.apache.fop.apps; | package org.apache.fop.apps; | ||||
import org.xml.sax.SAXException; | |||||
/** | /** | ||||
* Exception thrown when FOP has a problem | * Exception thrown when FOP has a problem | ||||
*/ | */ | ||||
public class FOPException extends Exception { | public class FOPException extends Exception { | ||||
private static final String EXCEPTION_SEPARATOR = "\n---------\n"; | |||||
private Throwable _exception; | private Throwable _exception; | ||||
/** | /** | ||||
} | } | ||||
public FOPException(Throwable e) { | public FOPException(Throwable e) { | ||||
super(e.getMessage()); | super(e.getMessage()); | ||||
_exception = e; | |||||
setException(e); | |||||
} | |||||
public FOPException(String message, Throwable e) { | |||||
super(message); | |||||
setException(e); | |||||
} | |||||
protected void setException(Throwable t) | |||||
{ | |||||
_exception = t; | |||||
} | } | ||||
public Throwable getException() | public Throwable getException() | ||||
{ | { | ||||
return _exception; | return _exception; | ||||
} | } | ||||
protected Throwable getRootException() | |||||
{ | |||||
Throwable result = _exception; | |||||
if (result instanceof SAXException) { | |||||
result = ((SAXException)result).getException(); | |||||
} | |||||
if (result instanceof java.lang.reflect.InvocationTargetException) { | |||||
result = ((java.lang.reflect.InvocationTargetException)result).getTargetException(); | |||||
} | |||||
if (result != _exception) { | |||||
return result; | |||||
} | |||||
return null; | |||||
} | |||||
public void printStackTrace() | |||||
{ | |||||
synchronized (System.err) { | |||||
super.printStackTrace(); | |||||
if (_exception != null) { | |||||
System.err.println(EXCEPTION_SEPARATOR); | |||||
_exception.printStackTrace(); | |||||
} | |||||
if (getRootException() != null) { | |||||
System.err.println(EXCEPTION_SEPARATOR); | |||||
getRootException().printStackTrace(); | |||||
} | |||||
} | |||||
} | |||||
public void printStackTrace(java.io.PrintStream stream) | |||||
{ | |||||
synchronized (stream) { | |||||
super.printStackTrace(stream); | |||||
if (_exception != null) { | |||||
stream.println(EXCEPTION_SEPARATOR); | |||||
_exception.printStackTrace(stream); | |||||
} | |||||
if (getRootException() != null) { | |||||
System.err.println(EXCEPTION_SEPARATOR); | |||||
getRootException().printStackTrace(stream); | |||||
} | |||||
} | |||||
} | |||||
public void printStackTrace(java.io.PrintWriter writer) | |||||
{ | |||||
synchronized (writer) { | |||||
super.printStackTrace(writer); | |||||
if (_exception != null) { | |||||
writer.println(EXCEPTION_SEPARATOR); | |||||
_exception.printStackTrace(writer); | |||||
} | |||||
if (getRootException() != null) { | |||||
System.err.println(EXCEPTION_SEPARATOR); | |||||
getRootException().printStackTrace(writer); | |||||
} | |||||
} | |||||
} | |||||
} | } |
package org.apache.fop.apps; | package org.apache.fop.apps; | ||||
import org.apache.fop.messaging.MessageHandler; | |||||
public class Fop { | public class Fop { | ||||
public static void main (String [] args) { | public static void main (String [] args) { | ||||
CommandLineOptions options = new CommandLineOptions (args); | |||||
Starter starter = options.getStarter(); | |||||
starter.run(); | |||||
CommandLineOptions options = null; | |||||
try { | |||||
options = new CommandLineOptions (args); | |||||
Starter starter = options.getStarter(); | |||||
starter.run(); | |||||
} | |||||
catch (FOPException e) { | |||||
MessageHandler.errorln("ERROR: "+e.getMessage()); | |||||
if (options != null && options.isDebugMode().booleanValue()) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
catch (java.io.FileNotFoundException e) { | |||||
MessageHandler.errorln("ERROR: "+e.getMessage()); | |||||
if (options != null && options.isDebugMode().booleanValue()) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
} | } | ||||
} | } | ||||
abstract public InputSource getInputSource(); | abstract public InputSource getInputSource(); | ||||
abstract public XMLReader getParser(); | |||||
abstract public XMLReader getParser() throws FOPException; | |||||
/** | /** | ||||
* | * | ||||
* @return the created SAX parser | * @return the created SAX parser | ||||
*/ | */ | ||||
static XMLReader createParser() { | |||||
boolean debugMode = Configuration.getBooleanValue("debugMode").booleanValue(); | |||||
protected static XMLReader createParser() | |||||
throws FOPException | |||||
{ | |||||
String parserClassName = System.getProperty("org.xml.sax.parser"); | String parserClassName = System.getProperty("org.xml.sax.parser"); | ||||
if (parserClassName == null) { | if (parserClassName == null) { | ||||
parserClassName = "org.apache.xerces.parsers.SAXParser"; | parserClassName = "org.apache.xerces.parsers.SAXParser"; | ||||
return (XMLReader) Class.forName( | return (XMLReader) Class.forName( | ||||
parserClassName).newInstance(); | parserClassName).newInstance(); | ||||
} catch (ClassNotFoundException e) { | } catch (ClassNotFoundException e) { | ||||
MessageHandler.errorln("Could not find " + parserClassName); | |||||
if (debugMode) { | |||||
e.printStackTrace(); | |||||
} | |||||
throw new FOPException(e); | |||||
} | } | ||||
catch (InstantiationException e) { | catch (InstantiationException e) { | ||||
MessageHandler.errorln("Could not instantiate " + | |||||
parserClassName); | |||||
if (debugMode) { | |||||
e.printStackTrace(); | |||||
} | |||||
throw new FOPException("Could not instantiate " + | |||||
parserClassName,e); | |||||
} | } | ||||
catch (IllegalAccessException e) { | catch (IllegalAccessException e) { | ||||
MessageHandler.errorln("Could not access " + parserClassName); | |||||
if (debugMode) { | |||||
e.printStackTrace(); | |||||
} | |||||
throw new FOPException("Could not access " + parserClassName,e); | |||||
} | } | ||||
catch (ClassCastException e) { | catch (ClassCastException e) { | ||||
MessageHandler.errorln(parserClassName + " is not a SAX driver"); | |||||
if (debugMode) { | |||||
e.printStackTrace(); | |||||
} | |||||
throw new FOPException(parserClassName + " is not a SAX driver",e); | |||||
} | } | ||||
return null; | |||||
} | } | ||||
} | } | ||||
import org.apache.fop.configuration.ConfigurationReader; | import org.apache.fop.configuration.ConfigurationReader; | ||||
/** | /** | ||||
* Options handles loading of configuration files and | |||||
* Options handles loading of configuration files and | |||||
* additional setting of commandline options | * additional setting of commandline options | ||||
*/ | */ | ||||
public class Options { | public class Options { | ||||
boolean errorDump = false; | boolean errorDump = false; | ||||
public Options () { | |||||
this.loadStandardConfiguration(); | |||||
initOptions (); | |||||
} | |||||
public Options (File userConfigFile) { | |||||
this(); | |||||
this.loadUserconfiguration(userConfigFile); | |||||
} | |||||
public Options () | |||||
throws FOPException | |||||
{ | |||||
this.loadStandardConfiguration(); | |||||
initOptions (); | |||||
} | |||||
public Options (File userConfigFile) | |||||
throws FOPException | |||||
{ | |||||
this(); | |||||
this.loadUserconfiguration(userConfigFile); | |||||
} | |||||
public Options (CommandLineOptions clOptions) | |||||
throws FOPException | |||||
public Options (CommandLineOptions clOptions) { | |||||
this(); | |||||
this.setCommandLineOptions(clOptions); | |||||
{ | |||||
this(); | |||||
this.setCommandLineOptions(clOptions); | |||||
} | |||||
//initializing option settings | |||||
void initOptions () { | |||||
if (Configuration.getBooleanValue("quiet").booleanValue()) { | |||||
MessageHandler.setQuiet(true); | |||||
} | } | ||||
//initializing option settings | |||||
void initOptions () { | |||||
if (Configuration.getBooleanValue("quiet").booleanValue()) { | |||||
MessageHandler.setQuiet(true); | |||||
} | |||||
if (Configuration.getBooleanValue("debugMode").booleanValue()) { | |||||
errorDump = true; | |||||
} | |||||
if (Configuration.getBooleanValue("dumpConfiguration").booleanValue()) { | |||||
Configuration.put("dumpConfiguration","true"); | |||||
Configuration.dumpConfiguration(); | |||||
} | |||||
if (Configuration.getBooleanValue("debugMode").booleanValue()) { | |||||
errorDump = true; | |||||
} | } | ||||
if (Configuration.getBooleanValue("dumpConfiguration").booleanValue()) { | |||||
Configuration.put("dumpConfiguration","true"); | |||||
Configuration.dumpConfiguration(); | |||||
} | |||||
} | |||||
//setting clOptions | |||||
//setting clOptions | |||||
void setCommandLineOptions(CommandLineOptions clOptions) { | void setCommandLineOptions(CommandLineOptions clOptions) { | ||||
//load user configuration file,if there is one | |||||
File userConfigFile = clOptions.getUserConfigFile(); | |||||
//load user configuration file,if there is one | |||||
File userConfigFile = clOptions.getUserConfigFile(); | |||||
if (userConfigFile != null) { | if (userConfigFile != null) { | ||||
this.loadUserconfiguration(userConfigFile); | this.loadUserconfiguration(userConfigFile); | ||||
} | } | ||||
//debug mode | //debug mode | ||||
if (clOptions.isDebugMode() != null) { | |||||
errorDump = clOptions.isDebugMode().booleanValue(); | |||||
Configuration.put("debugMode",new Boolean(errorDump)); | |||||
} | |||||
if (clOptions.isDebugMode() != null) { | |||||
errorDump = clOptions.isDebugMode().booleanValue(); | |||||
Configuration.put("debugMode",new Boolean(errorDump)); | |||||
} | |||||
//show configuration settings | |||||
boolean dumpConfiguration; | |||||
if (clOptions.dumpConfiguration() != null) { | |||||
dumpConfiguration = clOptions.dumpConfiguration().booleanValue(); | |||||
} else { | |||||
dumpConfiguration = Configuration.getBooleanValue("dumpConfiguration").booleanValue(); | |||||
} | |||||
//show configuration settings | |||||
boolean dumpConfiguration; | |||||
if (clOptions.dumpConfiguration() != null) { | |||||
dumpConfiguration = clOptions.dumpConfiguration().booleanValue(); | |||||
} else { | |||||
dumpConfiguration = Configuration.getBooleanValue("dumpConfiguration").booleanValue(); | |||||
} | |||||
if (dumpConfiguration) { | if (dumpConfiguration) { | ||||
Configuration.put("dumpConfiguration","true"); | |||||
Configuration.dumpConfiguration(); | |||||
Configuration.put("dumpConfiguration","true"); | |||||
Configuration.dumpConfiguration(); | |||||
System.exit(0); | System.exit(0); | ||||
} | |||||
} | |||||
//quiet mode | |||||
//quiet mode | |||||
if (clOptions.isQuiet() != null) { | if (clOptions.isQuiet() != null) { | ||||
MessageHandler.setQuiet(clOptions.isQuiet().booleanValue()); | MessageHandler.setQuiet(clOptions.isQuiet().booleanValue()); | ||||
} | |||||
} | |||||
//set base directory | |||||
//set base directory | |||||
String baseDir = Configuration.getStringValue("baseDir"); | String baseDir = Configuration.getStringValue("baseDir"); | ||||
if (baseDir == null) { | if (baseDir == null) { | ||||
baseDir = new File(clOptions.getInputFile().getAbsolutePath()).getParent(); | baseDir = new File(clOptions.getInputFile().getAbsolutePath()).getParent(); | ||||
/** | /** | ||||
* loads standard configuration file and a user file, if it has been specified | * loads standard configuration file and a user file, if it has been specified | ||||
*/ | */ | ||||
public void loadStandardConfiguration() { | |||||
public void loadStandardConfiguration() | |||||
throws FOPException | |||||
{ | |||||
String file = "config.xml"; | String file = "config.xml"; | ||||
// the entry /conf/config.xml refers to a directory conf which is a sibling of org | // the entry /conf/config.xml refers to a directory conf which is a sibling of org | ||||
InputStream configfile = | InputStream configfile = | ||||
ConfigurationReader.class.getResourceAsStream("/conf/"+ | |||||
file); | |||||
ConfigurationReader.class.getResourceAsStream("/conf/"+ | |||||
file); | |||||
if (configfile == null) { | if (configfile == null) { | ||||
MessageHandler.errorln("Fatal error: can't find default configuration file"); | |||||
System.exit(1); | |||||
throw new FOPException("can't find default configuration file"); | |||||
} | } | ||||
if (errorDump) { | if (errorDump) { | ||||
MessageHandler.logln("reading default configuration file"); | MessageHandler.logln("reading default configuration file"); | ||||
} | } | ||||
ConfigurationReader reader = | ConfigurationReader reader = | ||||
new ConfigurationReader (new InputSource(configfile)); | |||||
new ConfigurationReader (new InputSource(configfile)); | |||||
if (errorDump) { | if (errorDump) { | ||||
reader.setDumpError(true); | reader.setDumpError(true); | ||||
} | } | ||||
try { | |||||
reader.start(); | |||||
} catch (org.apache.fop.apps.FOPException error) { | |||||
MessageHandler.errorln("Fatal Error: Can't process default configuration file. \nProbably it is not well-formed."); | |||||
if (errorDump) { | |||||
reader.dumpError(error); | |||||
} | |||||
System.exit(1); | |||||
} | |||||
reader.start(); | |||||
} | } | ||||
public void loadUserconfiguration(String userConfigFile) { | |||||
public void loadUserconfiguration(String userConfigFile) | |||||
{ | |||||
loadUserconfiguration(new File(userConfigFile)); | loadUserconfiguration(new File(userConfigFile)); | ||||
} | } | ||||
public void loadUserconfiguration(File userConfigFile) { | |||||
public void loadUserconfiguration(File userConfigFile) | |||||
{ | |||||
//read user configuration file | //read user configuration file | ||||
if (userConfigFile != null) { | if (userConfigFile != null) { | ||||
MessageHandler.logln("reading user configuration file"); | MessageHandler.logln("reading user configuration file"); | ||||
ConfigurationReader reader = new ConfigurationReader ( | ConfigurationReader reader = new ConfigurationReader ( | ||||
InputHandler.fileInputSource(userConfigFile)); | |||||
InputHandler.fileInputSource(userConfigFile)); | |||||
if (errorDump) { | if (errorDump) { | ||||
reader.setDumpError(true); | reader.setDumpError(true); | ||||
} | } | ||||
try { | |||||
reader.start(); | |||||
try { | |||||
reader.start(); | |||||
} catch (org.apache.fop.apps.FOPException error) { | } catch (org.apache.fop.apps.FOPException error) { | ||||
MessageHandler.errorln( | |||||
"Can't find user configuration file " + | |||||
userConfigFile); | |||||
MessageHandler.errorln("Can't find user configuration file " + | |||||
userConfigFile); | |||||
MessageHandler.errorln("using default values"); | MessageHandler.errorln("using default values"); | ||||
if (errorDump) { | if (errorDump) { | ||||
reader.dumpError(error); | reader.dumpError(error); |
*/ | */ | ||||
public class PrintStarter extends CommandLineStarter { | public class PrintStarter extends CommandLineStarter { | ||||
public PrintStarter (CommandLineOptions options) { | |||||
public PrintStarter (CommandLineOptions options) | |||||
throws FOPException | |||||
{ | |||||
super(options); | super(options); | ||||
} | } | ||||
public void run () { | |||||
public void run () | |||||
throws FOPException | |||||
{ | |||||
Driver driver = new Driver(); | Driver driver = new Driver(); | ||||
if (errorDump) { | if (errorDump) { | ||||
driver.setErrorDump(true); | driver.setErrorDump(true); | ||||
XMLReader parser = inputHandler.getParser(); | XMLReader parser = inputHandler.getParser(); | ||||
setParserFeatures(parser,errorDump); | |||||
setParserFeatures(parser); | |||||
PrintRenderer renderer = new PrintRenderer(); | PrintRenderer renderer = new PrintRenderer(); | ||||
driver.format(); | driver.format(); | ||||
driver.render(); | driver.render(); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
MessageHandler.errorln("FATAL ERROR: " + e.getMessage()); | |||||
if (errorDump) { | |||||
e.printStackTrace(); | |||||
} | |||||
System.exit(1); | |||||
} | |||||
if (e instanceof FOPException) { | |||||
throw (FOPException)e; | |||||
} | |||||
throw new FOPException(e); | |||||
} | |||||
int copies = PrintRenderer.getIntProperty("copies", 1); | int copies = PrintRenderer.getIntProperty("copies", 1); | ||||
renderer.setCopies(copies); | renderer.setCopies(copies); |
*/ | */ | ||||
public abstract class Starter { | public abstract class Starter { | ||||
Options options; | |||||
InputHandler inputHandler; | |||||
Options options; | |||||
InputHandler inputHandler; | |||||
public Starter() { | |||||
options = new Options (); | |||||
} | |||||
public void setInputHandler(InputHandler inputHandler) { | |||||
this.inputHandler = inputHandler; | |||||
} | |||||
abstract public void run(); | |||||
// setting the parser features | |||||
public void setParserFeatures (XMLReader parser) { | |||||
setParserFeatures (parser,true); | |||||
} | |||||
public void setParserFeatures (XMLReader parser,boolean errorDump) { | |||||
public Starter() | |||||
throws FOPException | |||||
{ | |||||
options = new Options (); | |||||
} | |||||
public void setInputHandler(InputHandler inputHandler) { | |||||
this.inputHandler = inputHandler; | |||||
} | |||||
abstract public void run() | |||||
throws FOPException; | |||||
// setting the parser features | |||||
public void setParserFeatures (XMLReader parser) | |||||
throws FOPException | |||||
{ | |||||
try { | try { | ||||
parser.setFeature("http://xml.org/sax/features/namespace-prefixes",true); | parser.setFeature("http://xml.org/sax/features/namespace-prefixes",true); | ||||
} catch (SAXException e) { | } catch (SAXException e) { | ||||
MessageHandler.errorln("Error in setting up parser feature namespace-prefixes"); | |||||
MessageHandler.errorln("You need a parser which supports SAX version 2"); | |||||
if (errorDump) { | |||||
e.printStackTrace(); | |||||
} | |||||
System.exit(1); | |||||
throw new FOPException("Error in setting up parser feature namespace-prefixes\n" + | |||||
"You need a parser which supports SAX version 2",e); | |||||
} | } | ||||
} | } | ||||
} | } |
* simple XMLReader which allows chaining of transformations | * simple XMLReader which allows chaining of transformations | ||||
* | * | ||||
*/ | */ | ||||
public XMLReader getParser() { | |||||
public XMLReader getParser() throws FOPException { | |||||
return this.getXMLFilter(xmlfile,xsltfile); | return this.getXMLFilter(xmlfile,xsltfile); | ||||
} | } | ||||
* @param xsltfile An xslt stylesheet | * @param xsltfile An xslt stylesheet | ||||
* @return XMLFilter an XMLFilter which can be chained together with other XMLReaders or XMLFilters | * @return XMLFilter an XMLFilter which can be chained together with other XMLReaders or XMLFilters | ||||
*/ | */ | ||||
public static XMLFilter getXMLFilter (File xmlfile, File xsltfile) { | |||||
public static XMLFilter getXMLFilter (File xmlfile, File xsltfile) | |||||
throws FOPException | |||||
{ | |||||
try { | try { | ||||
// Instantiate a TransformerFactory. | // Instantiate a TransformerFactory. | ||||
TransformerFactory tFactory = TransformerFactory.newInstance(); | TransformerFactory tFactory = TransformerFactory.newInstance(); | ||||
// Create an XMLReader. | // Create an XMLReader. | ||||
XMLReader parser = createParser(); | XMLReader parser = createParser(); | ||||
if (parser == null) { | if (parser == null) { | ||||
MessageHandler.errorln("ERROR: Unable to create SAX parser"); | |||||
System.exit(1); | |||||
throw new FOPException("Unable to create SAX parser"); | |||||
} | } | ||||
// xmlFilter1 uses the XMLReader as its reader. | // xmlFilter1 uses the XMLReader as its reader. | ||||
xmlfilter.setParent(parser); | xmlfilter.setParent(parser); | ||||
return xmlfilter; | return xmlfilter; | ||||
} else { | } else { | ||||
MessageHandler.errorln( | |||||
throw new FOPException( | |||||
"Your parser doesn't support the features SAXSource and SAXResult." + | "Your parser doesn't support the features SAXSource and SAXResult." + | ||||
"\nMake sure you are using a xsl parser which supports TrAX"); | "\nMake sure you are using a xsl parser which supports TrAX"); | ||||
System.exit(1); | |||||
return null; | |||||
} | } | ||||
} | } | ||||
catch (Exception ex) { | catch (Exception ex) { | ||||
MessageHandler.errorln(ex.toString()); | |||||
return null; | |||||
} | |||||
if (ex instanceof FOPException) { | |||||
throw (FOPException)ex; | |||||
} | |||||
throw new FOPException(ex); | |||||
} | |||||
} | } | ||||
} | } | ||||
* get an XMLFilter. Otherwise, it falls back to using DOM documents | * get an XMLFilter. Otherwise, it falls back to using DOM documents | ||||
* | * | ||||
*/ | */ | ||||
public XMLReader getParser() { | |||||
public XMLReader getParser() | |||||
throws FOPException | |||||
{ | |||||
XMLReader result = null; | XMLReader result = null; | ||||
try { | try { | ||||
// try trax first | // try trax first | ||||
} | } | ||||
} | } | ||||
catch (ClassNotFoundException ex){ | catch (ClassNotFoundException ex){ | ||||
throw new FOPException(ex); | |||||
} | } | ||||
catch (InvocationTargetException ex) { | catch (InvocationTargetException ex) { | ||||
ex.printStackTrace(); | |||||
throw new FOPException(ex); | |||||
} | } | ||||
catch (IllegalAccessException ex) { | catch (IllegalAccessException ex) { | ||||
ex.printStackTrace(); | |||||
throw new FOPException(ex); | |||||
} | } | ||||
catch (NoSuchMethodException ex) { | catch (NoSuchMethodException ex) { | ||||
ex.printStackTrace(); | |||||
throw new FOPException(ex); | |||||
} | } | ||||
// otherwise, use DOM documents via our XSLTransform tool class old style | // otherwise, use DOM documents via our XSLTransform tool class old style | ||||
if (result == null) { | if (result == null) { |
public void start () throws FOPException { | public void start () throws FOPException { | ||||
XMLReader parser = createParser(); | XMLReader parser = createParser(); | ||||
if (parser == null) { | |||||
MessageHandler.errorln("ERROR: Unable to create SAX parser"); | |||||
System.exit(1); | |||||
} | |||||
// setting the parser features | // setting the parser features | ||||
try { | try { | ||||
parser.setFeature("http://xml.org/sax/features/namespace-prefixes", | parser.setFeature("http://xml.org/sax/features/namespace-prefixes", | ||||
false); | false); | ||||
} catch (SAXException e) { | } catch (SAXException e) { | ||||
MessageHandler.errorln("You need a parser which supports SAX version 2"); | |||||
if (errorDump) { | |||||
e.printStackTrace(); | |||||
} | |||||
System.exit(1); | |||||
throw new FOPException("You need a parser which supports SAX version 2",e); | |||||
} | } | ||||
ConfigurationParser configurationParser = new ConfigurationParser(); | ConfigurationParser configurationParser = new ConfigurationParser(); | ||||
parser.setContentHandler(configurationParser); | parser.setContentHandler(configurationParser); | ||||
try { | try { | ||||
parser.parse(filename); | parser.parse(filename); | ||||
// Configuration.setup(Configuration.STANDARD, configurationParser.getConfiguration(Configuration.STANDARD)); | |||||
// Configuration.setup(Configuration.PDF, configurationParser.getConfiguration(Configuration.PDF)); | |||||
// Configuration.setup(Configuration.AWT, configurationParser.getConfiguration(Configuration.AWT)); | |||||
} catch (SAXException e) { | } catch (SAXException e) { | ||||
if (e.getException() instanceof FOPException) { | if (e.getException() instanceof FOPException) { | ||||
dumpError(e.getException()); | |||||
throw (FOPException) e.getException(); | throw (FOPException) e.getException(); | ||||
} else { | } else { | ||||
dumpError(e); | |||||
throw new FOPException(e.getMessage()); | |||||
throw new FOPException(e); | |||||
} | } | ||||
} | } | ||||
catch (IOException e) { | catch (IOException e) { | ||||
dumpError(e); | |||||
throw new FOPException(e.getMessage()); | |||||
throw new FOPException(e); | |||||
} | } | ||||
} | } | ||||
* | * | ||||
* @return the created SAX parser | * @return the created SAX parser | ||||
*/ | */ | ||||
public static XMLReader createParser() { | |||||
public static XMLReader createParser() | |||||
throws FOPException | |||||
{ | |||||
String parserClassName = System.getProperty("org.xml.sax.parser"); | String parserClassName = System.getProperty("org.xml.sax.parser"); | ||||
if (parserClassName == null) { | if (parserClassName == null) { | ||||
parserClassName = "org.apache.xerces.parsers.SAXParser"; | parserClassName = "org.apache.xerces.parsers.SAXParser"; | ||||
return (XMLReader) Class.forName( | return (XMLReader) Class.forName( | ||||
parserClassName).newInstance(); | parserClassName).newInstance(); | ||||
} catch (ClassNotFoundException e) { | } catch (ClassNotFoundException e) { | ||||
MessageHandler.errorln("Could not find " + parserClassName); | |||||
if (errorDump) { | |||||
e.printStackTrace(); | |||||
} | |||||
throw new FOPException("Could not find " + parserClassName,e); | |||||
} | } | ||||
catch (InstantiationException e) { | catch (InstantiationException e) { | ||||
MessageHandler.errorln("Could not instantiate " + | |||||
parserClassName); | |||||
if (errorDump) { | |||||
e.printStackTrace(); | |||||
} | |||||
throw new FOPException("Could not instantiate " + | |||||
parserClassName,e); | |||||
} | } | ||||
catch (IllegalAccessException e) { | catch (IllegalAccessException e) { | ||||
MessageHandler.errorln("Could not access " + parserClassName); | |||||
if (errorDump) { | |||||
e.printStackTrace(); | |||||
} | |||||
throw new FOPException("Could not access " + parserClassName,e); | |||||
} | } | ||||
catch (ClassCastException e) { | catch (ClassCastException e) { | ||||
MessageHandler.errorln(parserClassName + " is not a SAX driver"); | |||||
if (errorDump) { | |||||
e.printStackTrace(); | |||||
} | |||||
throw new FOPException(parserClassName + " is not a SAX driver",e); | |||||
} | } | ||||
return null; | |||||
} | } | ||||
/** | /** | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* long or short error messages | * long or short error messages |
} | } | ||||
else | else | ||||
{ | { | ||||
System.err.println("Trying to balance balanced area"); | |||||
System.exit(0); | |||||
throw new IllegalStateException("Trying to balance balanced area"); | |||||
} | } | ||||
} | } | ||||
import org.apache.fop.pdf.PDFWArray; | import org.apache.fop.pdf.PDFWArray; | ||||
import org.apache.fop.pdf.PDFCIDFont; | import org.apache.fop.pdf.PDFCIDFont; | ||||
import org.apache.fop.configuration.ConfigurationReader; | import org.apache.fop.configuration.ConfigurationReader; | ||||
import org.apache.fop.apps.FOPException; | |||||
/** | /** | ||||
* Class for reading a metric.xml file and creating a font object. | * Class for reading a metric.xml file and creating a font object. | ||||
private Vector bfranges = null; | private Vector bfranges = null; | ||||
private void createFont(String path) throws IOException { | |||||
private void createFont(String path) throws FOPException { | |||||
XMLReader parser = ConfigurationReader.createParser(); | XMLReader parser = ConfigurationReader.createParser(); | ||||
if (parser == null) | if (parser == null) | ||||
throw new IOException("Unable to create SAX parser"); | |||||
throw new FOPException("Unable to create SAX parser"); | |||||
try { | try { | ||||
parser.setFeature("http://xml.org/sax/features/namespace-prefixes", | parser.setFeature("http://xml.org/sax/features/namespace-prefixes", | ||||
false); | false); | ||||
} catch (SAXException e) { | } catch (SAXException e) { | ||||
throw new IOException ( | |||||
"You need a SAX parser which supports " + "SAX version 2"); | |||||
throw new FOPException ( | |||||
"You need a SAX parser which supports SAX version 2",e); | |||||
} | } | ||||
parser.setContentHandler(this); | parser.setContentHandler(this); | ||||
try { | try { | ||||
parser.parse(path); | parser.parse(path); | ||||
} catch (SAXException e) { | } catch (SAXException e) { | ||||
throw new IOException(e.getMessage()); | |||||
throw new FOPException(e); | |||||
} | } | ||||
catch (IOException e) { | |||||
throw new FOPException(e); | |||||
} | |||||
} | } | ||||
/** | /** | ||||
* Construct a FontReader object from a path to a metric.xml file | * Construct a FontReader object from a path to a metric.xml file | ||||
* and read metric data | * and read metric data | ||||
*/ | */ | ||||
public FontReader(String path) throws IOException { | |||||
public FontReader(String path) throws FOPException { | |||||
createFont(path); | createFont(path); | ||||
} | } | ||||
import org.apache.fop.apps.InputHandler; | import org.apache.fop.apps.InputHandler; | ||||
import org.apache.fop.apps.FOInputHandler; | import org.apache.fop.apps.FOInputHandler; | ||||
import org.apache.fop.apps.Driver; | import org.apache.fop.apps.Driver; | ||||
import org.apache.fop.apps.FOPException; | |||||
import org.apache.fop.configuration.Configuration; | import org.apache.fop.configuration.Configuration; | ||||
* Starts execution of this task | * Starts execution of this task | ||||
*/ | */ | ||||
public void execute () throws BuildException { | public void execute () throws BuildException { | ||||
Starter starter = new FOPTaskStarter(this); | |||||
starter.run(); | |||||
try { | |||||
Starter starter = new FOPTaskStarter(this); | |||||
starter.run(); | |||||
} | |||||
catch (FOPException ex) { | |||||
throw new BuildException(ex); | |||||
} | |||||
} | } | ||||
} | } | ||||
Fop task; | Fop task; | ||||
MessageLogger logger; | MessageLogger logger; | ||||
FOPTaskStarter(Fop task) { | |||||
FOPTaskStarter(Fop task) | |||||
throws FOPException | |||||
{ | |||||
this.task = task; | this.task = task; | ||||
MessageHandler.setOutputMethod(MessageHandler.EVENT); | MessageHandler.setOutputMethod(MessageHandler.EVENT); | ||||
logger = new MessageLogger(new MessageHandler(), task); | logger = new MessageLogger(new MessageHandler(), task); | ||||
logger.setMessageLevel(task.getMessageType()); | logger.setMessageLevel(task.getMessageType()); | ||||
} | } | ||||
public void run () { | |||||
public void run () | |||||
throws FOPException | |||||
{ | |||||
Configuration.put("basedir", task.getBasedir()); | Configuration.put("basedir", task.getBasedir()); | ||||
InputHandler inputHandler = new FOInputHandler(task.getFofile()); | InputHandler inputHandler = new FOInputHandler(task.getFofile()); |