From 2153c1606caffaf86e343090c15293adcc6f93b3 Mon Sep 17 00:00:00 2001 From: Kelly Campbell Date: Thu, 12 Apr 2001 00:12:49 +0000 Subject: Removed uses of System.out where throwing a FOPException should suffice. 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-ffa450edef68 --- src/org/apache/fop/apps/AWTStarter.java | 18 +- src/org/apache/fop/apps/CommandLineOptions.java | 236 ++++++++++----------- src/org/apache/fop/apps/CommandLineStarter.java | 31 +-- src/org/apache/fop/apps/FOInputHandler.java | 11 +- src/org/apache/fop/apps/FOPException.java | 80 ++++++- src/org/apache/fop/apps/Fop.java | 25 ++- src/org/apache/fop/apps/InputHandler.java | 30 +-- src/org/apache/fop/apps/Options.java | 140 ++++++------ src/org/apache/fop/apps/PrintStarter.java | 22 +- src/org/apache/fop/apps/Starter.java | 46 ++-- src/org/apache/fop/apps/TraxInputHandler.java | 21 +- src/org/apache/fop/apps/XSLTInputHandler.java | 11 +- .../fop/configuration/ConfigurationReader.java | 49 ++--- src/org/apache/fop/layout/BodyAreaContainer.java | 3 +- src/org/apache/fop/render/pdf/FontReader.java | 17 +- src/org/apache/fop/tools/anttasks/Fop.java | 19 +- 16 files changed, 421 insertions(+), 338 deletions(-) diff --git a/src/org/apache/fop/apps/AWTStarter.java b/src/org/apache/fop/apps/AWTStarter.java index 1aa7de739..d78f1232f 100644 --- a/src/org/apache/fop/apps/AWTStarter.java +++ b/src/org/apache/fop/apps/AWTStarter.java @@ -54,7 +54,9 @@ public class AWTStarter extends CommandLineStarter { private Translator resource; - public AWTStarter (CommandLineOptions commandLineOptions) { + public AWTStarter (CommandLineOptions commandLineOptions) + throws FOPException + { super(commandLineOptions); init(); } @@ -89,7 +91,9 @@ public class AWTStarter extends CommandLineStarter { } - public void run () { + public void run () + throws FOPException + { Driver driver = new Driver(); if (errorDump) { driver.setErrorDump(true); @@ -100,8 +104,7 @@ public class AWTStarter extends CommandLineStarter { XMLReader parser = inputHandler.getParser(); if (parser == null) { - MessageHandler.errorln("ERROR: Unable to create SAX parser"); - System.exit(1); + throw new FOPException("Unable to create SAX parser"); } setParserFeatures(parser); @@ -125,9 +128,10 @@ public class AWTStarter extends CommandLineStarter { frame.showPage(); } 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); } } diff --git a/src/org/apache/fop/apps/CommandLineOptions.java b/src/org/apache/fop/apps/CommandLineOptions.java index 009d33d04..59e7048f4 100644 --- a/src/org/apache/fop/apps/CommandLineOptions.java +++ b/src/org/apache/fop/apps/CommandLineOptions.java @@ -10,6 +10,7 @@ package org.apache.fop.apps; //java import java.util.Vector; import java.io.File; +import java.io.FileNotFoundException; // FOP import org.apache.fop.messaging.MessageHandler; @@ -42,11 +43,11 @@ public class CommandLineOptions { private static final int TXT_OUTPUT = 6; /* use debug mode*/ - Boolean errorDump = null; + Boolean errorDump = new Boolean(false); /* show configuration information */ - Boolean dumpConfiguration = null; + Boolean dumpConfiguration = new Boolean(false); /*suppress any progress information */ - Boolean quiet = null; + Boolean quiet = new Boolean(false); /* name of user configuration file*/ File userConfigFile = null; /* name of input fo file */ @@ -64,18 +65,40 @@ public class CommandLineOptions { /* language for user information */ 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++) { if (args[i].equals("-d") || args[i].equals("--full-error-dump")) { errorDump = new Boolean(true); @@ -86,8 +109,7 @@ public class CommandLineOptions { } else if (args[i].equals("-c")) { if ((i + 1 == args.length) || (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 { userConfigFile = new File (args[i + 1]); i++; @@ -95,8 +117,7 @@ public class CommandLineOptions { } else if (args[i].equals("-l")) { if ((i + 1 == args.length) || (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 { language = args[i + 1]; i++; @@ -105,8 +126,7 @@ public class CommandLineOptions { inputmode = FO_INPUT; if ((i + 1 == args.length) || (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 { fofile = new File (args[i + 1]); i++; @@ -115,8 +135,7 @@ public class CommandLineOptions { inputmode = XSLT_INPUT; if ((i + 1 == args.length) || (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 { xsltfile = new File(args[i + 1]); i++; @@ -125,88 +144,54 @@ public class CommandLineOptions { inputmode = XSLT_INPUT; if ((i + 1 == args.length) || (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 { xmlfile = new File(args[i + 1]); i++; } } 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 { outfile = new File (args[i + 1]); i++; } } 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) || (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 { outfile = new File(args[i + 1]); i++; } } 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 if (i + 1 < args.length) { if (args[i + 1].equals("help")) { printUsagePrintOutput(); - } + return false; + } } } 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) || (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 { outfile = new File (args[i + 1]); i++; } } 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) || (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 { outfile = new File (args[i + 1]); i++; @@ -219,71 +204,77 @@ public class CommandLineOptions { outputmode = PDF_OUTPUT; outfile = new File(args[i]); } 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 { printUsage(); - } + return false; + } } + return true; } //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 */ - private void checkSettings () { + private void checkSettings () + throws FOPException, FileNotFoundException + { 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) { - MessageHandler.errorln("ERROR: you have to specify an output mode"); - printUsage(); - } + throw new FOPException("No output file specified"); + } if (inputmode == XSLT_INPUT) { //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 if (fofile != null) { 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()) { - MessageHandler.errorln("ERROR: xml file " + - xmlfile.getAbsolutePath() + " not found "); - System.exit(1); + throw new FileNotFoundException("xml file " + + xmlfile.getAbsolutePath() + " not found "); } if (!xsltfile.exists()) { - MessageHandler.errorln("ERROR: xsl file " + + throw new FileNotFoundException("xsl file " + xsltfile.getAbsolutePath() + " not found "); - System.exit(1); } } else if (inputmode == FO_INPUT) { 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()) { - MessageHandler.errorln("ERROR: fo file " + + throw new FileNotFoundException("fo file " + fofile.getAbsolutePath() + " not found "); - System.exit(1); } } @@ -329,7 +320,9 @@ public class CommandLineOptions { } - public Starter getStarter() { + public Starter getStarter() + throws FOPException + { switch (outputmode) { case PDF_OUTPUT: return new CommandLineStarter(this); @@ -341,10 +334,10 @@ public class CommandLineOptions { new Class[]{CommandLineOptions.class}). newInstance(new Object[]{this})); } 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: return new CommandLineStarter(this); @@ -356,10 +349,10 @@ public class CommandLineOptions { new Class[]{CommandLineOptions.class}). newInstance(new Object[]{this})); } 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: @@ -431,7 +424,7 @@ public class CommandLineOptions { * shows the commandline syntax including a summary of all available options and some examples */ public static void printUsage() { - MessageHandler.logln( + MessageHandler.errorln( "\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] [-awt|-pdf|-mif|-pcl|-txt|-print] \n" + " [OPTIONS] \n" + " -d debug mode \n" + " -x dump configuration settings \n" + @@ -456,8 +449,7 @@ public class CommandLineOptions { " 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 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"); } /** @@ -468,7 +460,6 @@ public class CommandLineOptions { "USAGE: -print [-Dstart=i] [-Dend=i] [-Dcopies=i] [-Deven=true|false] " + " org.apache.fop.apps.Fop (..) -print \n" + "Example:\n" + "java -Dstart=1 -Dend=2 org.apache.Fop.apps.Fop infile.fo -print "); - System.exit(1); } @@ -566,8 +557,13 @@ public class CommandLineOptions { for (int i = 0; i < args.length; i++) { MessageHandler.logln(">"+args[i]+"<"); }*/ - - CommandLineOptions options = new CommandLineOptions (args); + try { + CommandLineOptions options = new CommandLineOptions (args); + } + catch (Exception e) { + e.printStackTrace(); + } + //options.debug(); } } diff --git a/src/org/apache/fop/apps/CommandLineStarter.java b/src/org/apache/fop/apps/CommandLineStarter.java index 7d6aae9fc..7ef33c3ea 100644 --- a/src/org/apache/fop/apps/CommandLineStarter.java +++ b/src/org/apache/fop/apps/CommandLineStarter.java @@ -24,19 +24,27 @@ public class CommandLineStarter extends Starter { CommandLineOptions commandLineOptions; boolean errorDump; - public CommandLineStarter (CommandLineOptions commandLineOptions) { + public CommandLineStarter (CommandLineOptions commandLineOptions) + throws FOPException + { 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(); MessageHandler.logln(version); XMLReader parser = inputHandler.getParser(); - setParserFeatures(parser,errorDump); + setParserFeatures(parser); Driver driver = new Driver(); if (errorDump) { @@ -51,12 +59,11 @@ public class CommandLineStarter extends Starter { driver.setOutputStream(new FileOutputStream(commandLineOptions.getOutputFile())); driver.render(); } 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); + } } } diff --git a/src/org/apache/fop/apps/FOInputHandler.java b/src/org/apache/fop/apps/FOInputHandler.java index 9a40a6abc..6f99e223c 100644 --- a/src/org/apache/fop/apps/FOInputHandler.java +++ b/src/org/apache/fop/apps/FOInputHandler.java @@ -32,13 +32,10 @@ public class FOInputHandler extends InputHandler { 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(); } } diff --git a/src/org/apache/fop/apps/FOPException.java b/src/org/apache/fop/apps/FOPException.java index 94ca52bdf..765d9eede 100644 --- a/src/org/apache/fop/apps/FOPException.java +++ b/src/org/apache/fop/apps/FOPException.java @@ -6,11 +6,16 @@ package org.apache.fop.apps; +import org.xml.sax.SAXException; + + /** * Exception thrown when FOP has a problem */ public class FOPException extends Exception { + private static final String EXCEPTION_SEPARATOR = "\n---------\n"; + private Throwable _exception; /** @@ -23,11 +28,84 @@ public class FOPException extends Exception { } public FOPException(Throwable e) { 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() { 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); + } + } + } + } diff --git a/src/org/apache/fop/apps/Fop.java b/src/org/apache/fop/apps/Fop.java index 727fbbaf3..2f4032deb 100644 --- a/src/org/apache/fop/apps/Fop.java +++ b/src/org/apache/fop/apps/Fop.java @@ -7,11 +7,30 @@ package org.apache.fop.apps; +import org.apache.fop.messaging.MessageHandler; + public class Fop { 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(); + } + } } + } diff --git a/src/org/apache/fop/apps/InputHandler.java b/src/org/apache/fop/apps/InputHandler.java index 133d866ff..868a2de15 100644 --- a/src/org/apache/fop/apps/InputHandler.java +++ b/src/org/apache/fop/apps/InputHandler.java @@ -24,7 +24,7 @@ abstract public class InputHandler { abstract public InputSource getInputSource(); - abstract public XMLReader getParser(); + abstract public XMLReader getParser() throws FOPException; /** @@ -54,8 +54,9 @@ abstract public class InputHandler { * * @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"); if (parserClassName == null) { parserClassName = "org.apache.xerces.parsers.SAXParser"; @@ -66,31 +67,18 @@ abstract public class InputHandler { return (XMLReader) Class.forName( parserClassName).newInstance(); } catch (ClassNotFoundException e) { - MessageHandler.errorln("Could not find " + parserClassName); - if (debugMode) { - e.printStackTrace(); - } + throw new FOPException(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) { - MessageHandler.errorln("Could not access " + parserClassName); - if (debugMode) { - e.printStackTrace(); - } + throw new FOPException("Could not access " + parserClassName,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; } } diff --git a/src/org/apache/fop/apps/Options.java b/src/org/apache/fop/apps/Options.java index ca6a3f0a9..60a5a4148 100644 --- a/src/org/apache/fop/apps/Options.java +++ b/src/org/apache/fop/apps/Options.java @@ -20,75 +20,82 @@ import org.apache.fop.configuration.Configuration; 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 */ public class Options { 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) { - //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) { this.loadUserconfiguration(userConfigFile); } //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) { - Configuration.put("dumpConfiguration","true"); - Configuration.dumpConfiguration(); + Configuration.put("dumpConfiguration","true"); + Configuration.dumpConfiguration(); System.exit(0); - } + } - //quiet mode + //quiet mode if (clOptions.isQuiet() != null) { MessageHandler.setQuiet(clOptions.isQuiet().booleanValue()); - } + } - //set base directory + //set base directory String baseDir = Configuration.getStringValue("baseDir"); if (baseDir == null) { baseDir = new File(clOptions.getInputFile().getAbsolutePath()).getParent(); @@ -102,55 +109,50 @@ public class Options { /** * 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"; // the entry /conf/config.xml refers to a directory conf which is a sibling of org InputStream configfile = - ConfigurationReader.class.getResourceAsStream("/conf/"+ - file); + ConfigurationReader.class.getResourceAsStream("/conf/"+ + file); 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) { MessageHandler.logln("reading default configuration file"); } ConfigurationReader reader = - new ConfigurationReader (new InputSource(configfile)); + new ConfigurationReader (new InputSource(configfile)); if (errorDump) { 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)); } - public void loadUserconfiguration(File userConfigFile) { + public void loadUserconfiguration(File userConfigFile) + { //read user configuration file if (userConfigFile != null) { MessageHandler.logln("reading user configuration file"); ConfigurationReader reader = new ConfigurationReader ( - InputHandler.fileInputSource(userConfigFile)); + InputHandler.fileInputSource(userConfigFile)); if (errorDump) { reader.setDumpError(true); } - try { - reader.start(); + try { + reader.start(); } 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"); if (errorDump) { reader.dumpError(error); diff --git a/src/org/apache/fop/apps/PrintStarter.java b/src/org/apache/fop/apps/PrintStarter.java index 91c24f9ba..dbd1269ee 100644 --- a/src/org/apache/fop/apps/PrintStarter.java +++ b/src/org/apache/fop/apps/PrintStarter.java @@ -44,11 +44,15 @@ import org.apache.fop.messaging.MessageHandler; */ public class PrintStarter extends CommandLineStarter { - public PrintStarter (CommandLineOptions options) { + public PrintStarter (CommandLineOptions options) + throws FOPException + { super(options); } - public void run () { + public void run () + throws FOPException + { Driver driver = new Driver(); if (errorDump) { driver.setErrorDump(true); @@ -59,7 +63,7 @@ public class PrintStarter extends CommandLineStarter { XMLReader parser = inputHandler.getParser(); - setParserFeatures(parser,errorDump); + setParserFeatures(parser); PrintRenderer renderer = new PrintRenderer(); @@ -69,13 +73,11 @@ public class PrintStarter extends CommandLineStarter { driver.format(); driver.render(); } 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); renderer.setCopies(copies); diff --git a/src/org/apache/fop/apps/Starter.java b/src/org/apache/fop/apps/Starter.java index 3456aff81..c82ef9323 100644 --- a/src/org/apache/fop/apps/Starter.java +++ b/src/org/apache/fop/apps/Starter.java @@ -27,35 +27,31 @@ import org.apache.fop.messaging.MessageHandler; */ 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 { parser.setFeature("http://xml.org/sax/features/namespace-prefixes",true); } 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); } } } diff --git a/src/org/apache/fop/apps/TraxInputHandler.java b/src/org/apache/fop/apps/TraxInputHandler.java index 59f143103..473d7cdf6 100644 --- a/src/org/apache/fop/apps/TraxInputHandler.java +++ b/src/org/apache/fop/apps/TraxInputHandler.java @@ -58,7 +58,7 @@ public class TraxInputHandler extends InputHandler { * simple XMLReader which allows chaining of transformations * */ - public XMLReader getParser() { + public XMLReader getParser() throws FOPException { return this.getXMLFilter(xmlfile,xsltfile); } @@ -71,7 +71,9 @@ public class TraxInputHandler extends InputHandler { * @param xsltfile An xslt stylesheet * @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 { // Instantiate a TransformerFactory. TransformerFactory tFactory = TransformerFactory.newInstance(); @@ -89,25 +91,24 @@ public class TraxInputHandler extends InputHandler { // Create an XMLReader. XMLReader parser = createParser(); 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. xmlfilter.setParent(parser); return xmlfilter; } else { - MessageHandler.errorln( + throw new FOPException( "Your parser doesn't support the features SAXSource and SAXResult." + "\nMake sure you are using a xsl parser which supports TrAX"); - System.exit(1); - return null; } } catch (Exception ex) { - MessageHandler.errorln(ex.toString()); - return null; - } + if (ex instanceof FOPException) { + throw (FOPException)ex; + } + throw new FOPException(ex); + } } } diff --git a/src/org/apache/fop/apps/XSLTInputHandler.java b/src/org/apache/fop/apps/XSLTInputHandler.java index 8b29d4e45..ccce94447 100644 --- a/src/org/apache/fop/apps/XSLTInputHandler.java +++ b/src/org/apache/fop/apps/XSLTInputHandler.java @@ -86,7 +86,9 @@ public class XSLTInputHandler extends InputHandler { * get an XMLFilter. Otherwise, it falls back to using DOM documents * */ - public XMLReader getParser() { + public XMLReader getParser() + throws FOPException + { XMLReader result = null; try { // try trax first @@ -101,15 +103,16 @@ public class XSLTInputHandler extends InputHandler { } } catch (ClassNotFoundException ex){ + throw new FOPException(ex); } catch (InvocationTargetException ex) { - ex.printStackTrace(); + throw new FOPException(ex); } catch (IllegalAccessException ex) { - ex.printStackTrace(); + throw new FOPException(ex); } catch (NoSuchMethodException ex) { - ex.printStackTrace(); + throw new FOPException(ex); } // otherwise, use DOM documents via our XSLTransform tool class old style if (result == null) { diff --git a/src/org/apache/fop/configuration/ConfigurationReader.java b/src/org/apache/fop/configuration/ConfigurationReader.java index edde15624..7a22e40f6 100644 --- a/src/org/apache/fop/configuration/ConfigurationReader.java +++ b/src/org/apache/fop/configuration/ConfigurationReader.java @@ -101,42 +101,27 @@ public class ConfigurationReader { public void start () throws FOPException { XMLReader parser = createParser(); - if (parser == null) { - MessageHandler.errorln("ERROR: Unable to create SAX parser"); - System.exit(1); - } - // setting the parser features try { parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); } 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(); parser.setContentHandler(configurationParser); try { 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) { if (e.getException() instanceof FOPException) { - dumpError(e.getException()); throw (FOPException) e.getException(); } else { - dumpError(e); - throw new FOPException(e.getMessage()); + throw new FOPException(e); } } catch (IOException e) { - dumpError(e); - throw new FOPException(e.getMessage()); + throw new FOPException(e); } } @@ -147,7 +132,9 @@ public class ConfigurationReader { * * @return the created SAX parser */ - public static XMLReader createParser() { + public static XMLReader createParser() + throws FOPException + { String parserClassName = System.getProperty("org.xml.sax.parser"); if (parserClassName == null) { parserClassName = "org.apache.xerces.parsers.SAXParser"; @@ -161,31 +148,18 @@ public class ConfigurationReader { return (XMLReader) Class.forName( parserClassName).newInstance(); } catch (ClassNotFoundException e) { - MessageHandler.errorln("Could not find " + parserClassName); - if (errorDump) { - e.printStackTrace(); - } + throw new FOPException("Could not find " + parserClassName,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) { - MessageHandler.errorln("Could not access " + parserClassName); - if (errorDump) { - e.printStackTrace(); - } + throw new FOPException("Could not access " + parserClassName,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; } /** @@ -203,6 +177,7 @@ public class ConfigurationReader { } } } + /** * long or short error messages diff --git a/src/org/apache/fop/layout/BodyAreaContainer.java b/src/org/apache/fop/layout/BodyAreaContainer.java index d2aee8d3e..65630a4ab 100644 --- a/src/org/apache/fop/layout/BodyAreaContainer.java +++ b/src/org/apache/fop/layout/BodyAreaContainer.java @@ -326,8 +326,7 @@ public class BodyAreaContainer extends Area { } else { - System.err.println("Trying to balance balanced area"); - System.exit(0); + throw new IllegalStateException("Trying to balance balanced area"); } } diff --git a/src/org/apache/fop/render/pdf/FontReader.java b/src/org/apache/fop/render/pdf/FontReader.java index ce79dce42..e8430d399 100644 --- a/src/org/apache/fop/render/pdf/FontReader.java +++ b/src/org/apache/fop/render/pdf/FontReader.java @@ -20,6 +20,7 @@ import java.util.Hashtable; import org.apache.fop.pdf.PDFWArray; import org.apache.fop.pdf.PDFCIDFont; import org.apache.fop.configuration.ConfigurationReader; +import org.apache.fop.apps.FOPException; /** * Class for reading a metric.xml file and creating a font object. @@ -47,17 +48,17 @@ public class FontReader extends DefaultHandler { private Vector bfranges = null; - private void createFont(String path) throws IOException { + private void createFont(String path) throws FOPException { XMLReader parser = ConfigurationReader.createParser(); if (parser == null) - throw new IOException("Unable to create SAX parser"); + throw new FOPException("Unable to create SAX parser"); try { parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); } 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); @@ -65,8 +66,12 @@ public class FontReader extends DefaultHandler { try { parser.parse(path); } catch (SAXException e) { - throw new IOException(e.getMessage()); + throw new FOPException(e); } + catch (IOException e) { + throw new FOPException(e); + } + } /** @@ -101,7 +106,7 @@ public class FontReader extends DefaultHandler { * Construct a FontReader object from a path to a metric.xml file * and read metric data */ - public FontReader(String path) throws IOException { + public FontReader(String path) throws FOPException { createFont(path); } diff --git a/src/org/apache/fop/tools/anttasks/Fop.java b/src/org/apache/fop/tools/anttasks/Fop.java index 84b200b9e..509a04fa5 100644 --- a/src/org/apache/fop/tools/anttasks/Fop.java +++ b/src/org/apache/fop/tools/anttasks/Fop.java @@ -71,6 +71,7 @@ import org.apache.fop.apps.Starter; import org.apache.fop.apps.InputHandler; import org.apache.fop.apps.FOInputHandler; import org.apache.fop.apps.Driver; +import org.apache.fop.apps.FOPException; import org.apache.fop.configuration.Configuration; @@ -173,8 +174,14 @@ public class Fop extends Task { * Starts execution of this task */ 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); + } + } } @@ -182,14 +189,18 @@ class FOPTaskStarter extends Starter { Fop task; MessageLogger logger; - FOPTaskStarter(Fop task) { + FOPTaskStarter(Fop task) + throws FOPException + { this.task = task; MessageHandler.setOutputMethod(MessageHandler.EVENT); logger = new MessageLogger(new MessageHandler(), task); logger.setMessageLevel(task.getMessageType()); } - public void run () { + public void run () + throws FOPException + { Configuration.put("basedir", task.getBasedir()); InputHandler inputHandler = new FOInputHandler(task.getFofile()); -- cgit v1.2.3