Browse Source

command line support for new ps renderer

also cleaned up code
Submitted by:  Jeremias Maerki <jeremias.maerki@outline.ch>


git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194311 13f79535-47bb-0310-9956-ffa450edef68
pull/33/head
Keiron Liddle 23 years ago
parent
commit
bfc682a0e2
2 changed files with 321 additions and 328 deletions
  1. 147
    150
      src/org/apache/fop/apps/CommandLineOptions.java
  2. 174
    178
      src/org/apache/fop/apps/Driver.java

+ 147
- 150
src/org/apache/fop/apps/CommandLineOptions.java View File

/*
/* $Id$
* Copyright (C) 2001 The Apache Software Foundation. All rights reserved. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the * For details on use and redistribution please refer to the
* LICENSE file included with these sources."
* LICENSE file included with these sources.
*/ */



package org.apache.fop.apps; package org.apache.fop.apps;


//java //java
/** /**
* Options parses the commandline arguments * Options parses the commandline arguments
*/ */

public class CommandLineOptions { public class CommandLineOptions {


/* input / output not set */ /* input / output not set */
private static final int PRINT_OUTPUT = 4; private static final int PRINT_OUTPUT = 4;
/* output: pcl file */ /* output: pcl file */
private static final int PCL_OUTPUT = 5; private static final int PCL_OUTPUT = 5;
/* output: postscript file */
private static final int PS_OUTPUT = 6;
/* output: text file */ /* output: text file */
private static final int TXT_OUTPUT = 6;
/* System buffers */
private static final int BUFFER_FILE = 7;
/* System buffers */
private static final int AREA_OUTPUT = 8;
private static final int TXT_OUTPUT = 7;
/* System buffers */
private static final int BUFFER_FILE = 8;
/* System buffers */
private static final int AREA_OUTPUT = 9;
/* use debug mode*/ /* use debug mode*/
Boolean errorDump = new Boolean(false); Boolean errorDump = new Boolean(false);
/* show configuration information */ /* show configuration information */
Boolean dumpConfiguration = new Boolean(false); Boolean dumpConfiguration = new Boolean(false);
/*suppress any progress information */ /*suppress any progress information */
Boolean quiet = new Boolean(false); Boolean quiet = new Boolean(false);
/* for area tree XML output, only down to block area level */
Boolean suppressLowLevelAreas = new Boolean(false);
/* for area tree XML output, only down to block area level */
Boolean suppressLowLevelAreas = 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;


private java.util.Hashtable rendererOptions;
public CommandLineOptions (String [] args)
throws FOPException, FileNotFoundException
{
boolean optionsParsed = true;
rendererOptions = new java.util.Hashtable();
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;
}
private java.util.Hashtable rendererOptions;

public CommandLineOptions (String [] args) throws FOPException,
FileNotFoundException {
boolean optionsParsed = true;
rendererOptions = new java.util.Hashtable();
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 * parses the commandline arguments
* @return true if parse was successful and procesing can continue, false if processing should stop * @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 * @exception FOPException if there was an error in the format of the options
*/ */
private boolean parseOptions (String args[])
throws FOPException
{
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) == '-')) {
throw new FOPException("if you use '-c', you must specify the name of the configuration file");
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("-s")) { } else if (args[i].equals("-s")) {
suppressLowLevelAreas = new Boolean(true); suppressLowLevelAreas = new Boolean(true);
} else if (args[i].equals("-fo")) {
} else if (args[i].equals("-fo")) {
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) == '-')) {
i++; i++;
} }
} else if (args[i].equals("-awt")) { } else if (args[i].equals("-awt")) {
setOutputMode(AWT_OUTPUT);
} else if (args[i].equals("-pdf")) {
setOutputMode(PDF_OUTPUT);
if ((i + 1 == args.length) ||
(args[i + 1].charAt(0) == '-')) {
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"); 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")) {
setOutputMode(MIF_OUTPUT);
setOutputMode(MIF_OUTPUT);
if ((i + 1 == args.length) || if ((i + 1 == args.length) ||
(args[i + 1].charAt(0) == '-')) { (args[i + 1].charAt(0) == '-')) {
throw new FOPException("you must specify the mif output file");
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")) {
setOutputMode(PRINT_OUTPUT);
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;
}
return false;
}
} }
} else if (args[i].equals("-pcl")) { } else if (args[i].equals("-pcl")) {
setOutputMode(PCL_OUTPUT);
setOutputMode(PCL_OUTPUT);
if ((i + 1 == args.length) || if ((i + 1 == args.length) ||
(args[i + 1].charAt(0) == '-')) { (args[i + 1].charAt(0) == '-')) {
throw new FOPException("you must specify the pdf output file"); throw new FOPException("you must specify the pdf output file");
outfile = new File (args[i + 1]); outfile = new File (args[i + 1]);
i++; i++;
} }
} else if (args[i].equals("-ps")) {
setOutputMode(PS_OUTPUT);
if ((i + 1 == args.length) ||
(args[i + 1].charAt(0) == '-')) {
throw new FOPException("you must specify the PostScript output file");
} else {
outfile = new File (args[i + 1]);
i++;
}
} else if (args[i].equals("-txt")) { } else if (args[i].equals("-txt")) {
setOutputMode(TXT_OUTPUT);
setOutputMode(TXT_OUTPUT);
if ((i + 1 == args.length) || if ((i + 1 == args.length) ||
(args[i + 1].charAt(0) == '-')) { (args[i + 1].charAt(0) == '-')) {
throw new FOPException("you must specify the text output file"); throw new FOPException("you must specify the text output file");
outputmode = PDF_OUTPUT; outputmode = PDF_OUTPUT;
outfile = new File(args[i]); outfile = new File(args[i]);
} else { } else {
throw new FOPException("Don't know what to do with " + args[i]);
throw new FOPException(
"Don't know what to do with " + args[i]);
} }
} else if (args[i].equals("-buf")) {
} else if (args[i].equals("-buf")) {
if (buffermode == NOT_SET) { if (buffermode == NOT_SET) {
buffermode = BUFFER_FILE; buffermode = BUFFER_FILE;
} else { } else {
i++; i++;
} }
} else if (args[i].equals("-at")) { } else if (args[i].equals("-at")) {
setOutputMode(AREA_OUTPUT);
setOutputMode(AREA_OUTPUT);
if ((i + 1 == args.length) || if ((i + 1 == args.length) ||
(args[i + 1].charAt(0) == '-')) { (args[i + 1].charAt(0) == '-')) {
throw new FOPException("you must specify the area-tree output file"); throw new FOPException("you must specify the area-tree output file");
} }
} else { } else {
printUsage(); printUsage();
return false;
}
return false;
}
} }
return true;
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");
}
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 ()
throws FOPException, FileNotFoundException
{
private void checkSettings () throws FOPException,
FileNotFoundException {
if (inputmode == NOT_SET) { if (inputmode == NOT_SET) {
throw new FOPException("No input file specified");
}
throw new FOPException("No input file specified");
}


if (outputmode == NOT_SET) { if (outputmode == NOT_SET) {
throw new FOPException("No output file specified");
}
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) {
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");
}
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(
"WARNING: Can't use fo file with transform mode! Ignoring.\n" + "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());
"Your input is "+ "\n xmlfile: " +
xmlfile.getAbsolutePath() + "\nxsltfile: " +
xsltfile.getAbsolutePath() + "\n fofile: " +
fofile.getAbsolutePath());
} }
if (!xmlfile.exists()) { if (!xmlfile.exists()) {
throw new FileNotFoundException("xml file " +
xmlfile.getAbsolutePath() + " not found ");
throw new FileNotFoundException("xml file " +
xmlfile.getAbsolutePath() + " not found ");
} }
if (!xsltfile.exists()) { if (!xsltfile.exists()) {
throw new FileNotFoundException("xsl file " + throw new FileNotFoundException("xsl file " +
xsltfile.getAbsolutePath() + " not found ");
xsltfile.getAbsolutePath() + " not found ");
} }


} else if (inputmode == FO_INPUT) { } else if (inputmode == FO_INPUT) {
MessageHandler.errorln("xslt file: " + xsltfile.toString()); MessageHandler.errorln("xslt file: " + xsltfile.toString());
} }
if (!fofile.exists()) { if (!fofile.exists()) {
throw new FileNotFoundException("fo file " +
fofile.getAbsolutePath() + " not found ");
throw new FileNotFoundException("fo file " +
fofile.getAbsolutePath() + " not found ");
} }


} }
}// end checkSettings }// end checkSettings



/** /**
* returns the chosen renderer, throws FOPException * returns the chosen renderer, throws FOPException
*/ */
return Driver.RENDER_PRINT; return Driver.RENDER_PRINT;
case PCL_OUTPUT: case PCL_OUTPUT:
return Driver.RENDER_PCL; return Driver.RENDER_PCL;
case PS_OUTPUT:
return Driver.RENDER_PS;
case TXT_OUTPUT: case TXT_OUTPUT:
return Driver.RENDER_TXT; return Driver.RENDER_TXT;
case AREA_OUTPUT:
rendererOptions.put("fineDetail", isCoarseAreaXml());
return Driver.RENDER_XML;
case AREA_OUTPUT:
rendererOptions.put("fineDetail", isCoarseAreaXml());
return Driver.RENDER_XML;
default: default:
throw new FOPException("Invalid Renderer setting!"); throw new FOPException("Invalid Renderer setting!");
} }
} }


/** /**
*
**/
*
**/
public InputHandler getInputHandler () { public InputHandler getInputHandler () {
switch (inputmode) { switch (inputmode) {
case FO_INPUT: case FO_INPUT:
} }
} }


public java.util.Hashtable getRendererOptions() {
return rendererOptions;
}
public Starter getStarter()
throws FOPException
{
public java.util.Hashtable getRendererOptions() {
return rendererOptions;
}

public Starter getStarter() throws FOPException {
switch (outputmode) { switch (outputmode) {
case PDF_OUTPUT:
return new CommandLineStarter(this);
case AWT_OUTPUT: case AWT_OUTPUT:
try { try {
return((Starter) Class.forName( return((Starter) Class.forName(
new Class[]{CommandLineOptions.class}). new Class[]{CommandLineOptions.class}).
newInstance(new Object[]{this})); newInstance(new Object[]{this}));
} catch (Exception e) { } catch (Exception e) {
if (e instanceof FOPException) {
throw (FOPException)e;
}
throw new FOPException("AWTStarter could not be loaded.",e);
if (e instanceof FOPException) {
throw (FOPException) e;
}
throw new FOPException("AWTStarter could not be loaded.",e);
} }
case MIF_OUTPUT:
return new CommandLineStarter(this);
case PRINT_OUTPUT: case PRINT_OUTPUT:
try { try {
return((Starter) Class.forName( return((Starter) Class.forName(
new Class[]{CommandLineOptions.class}). new Class[]{CommandLineOptions.class}).
newInstance(new Object[]{this})); newInstance(new Object[]{this}));
} catch (Exception e) { } catch (Exception e) {
if (e instanceof FOPException) {
throw (FOPException)e;
}
throw new FOPException("PrintStarter could not be loaded.",e);
if (e instanceof FOPException) {
throw (FOPException) e;
}
throw new FOPException("PrintStarter could not be loaded.",e);
} }


case AREA_OUTPUT:
return new CommandLineStarter(this);
default: default:
return new CommandLineStarter(this); return new CommandLineStarter(this);
} }
return language; return language;
} }


public File getBufferFile() {
return bufferFile;
}
public File getBufferFile() {
return bufferFile;
}
public Boolean isQuiet() { public Boolean isQuiet() {
return quiet; return quiet;
} }
return errorDump; return errorDump;
} }


public Boolean isCoarseAreaXml() {
return suppressLowLevelAreas;
}
public Boolean isCoarseAreaXml() {
return suppressLowLevelAreas;
}
/** /**
* return either the fofile or the xmlfile
*/
* return either the fofile or the xmlfile
*/
public File getInputFile() { public File getInputFile() {
switch (inputmode) { switch (inputmode) {
case FO_INPUT: case FO_INPUT:
} }
} }



/** /**
* 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.errorln( MessageHandler.errorln(
"\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] [-awt|-pdf|-mif|-pcl|-txt|-at|-print] <outfile>\n" +
"\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] [-awt|-pdf|-mif|-pcl|-ps|-txt|-at|-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" +
" -q quiet mode \n" + " -q quiet mode \n" +
" -c cfg.xml use additional configuration file cfg.xml\n" + " -c cfg.xml use additional configuration file cfg.xml\n" +
" -l lang the language to use for user information \n" + " -l lang the language to use for user information \n" +
" -s for area tree XML, down to block areas only\n\n" +
" -s for area tree XML, down to block areas only\n\n" +
" [INPUT] \n" + " [INPUT] \n" +
" infile xsl:fo input file (the same as the next) \n" + " infile xsl:fo input file (the same as the next) \n" +
" -fo infile xsl:fo input file \n" + " -fo infile xsl:fo input file \n" +
" -awt input will be displayed on screen \n" + " -awt input will be displayed on screen \n" +
" -mif outfile input will be rendered as mif file (outfile req'd)\n" + " -mif outfile input will be rendered as mif file (outfile req'd)\n" +
" -pcl outfile input will be rendered as pcl file (outfile req'd) \n" + " -pcl outfile input will be rendered as pcl file (outfile req'd) \n" +
" -ps outfile input will be rendered as PostScript file (outfile req'd) \n" +
" -txt outfile input will be rendered as text file (outfile req'd) \n" + " -txt outfile input will be rendered as text file (outfile req'd) \n" +
" -at outfile representation of area tree as XML (outfile req'd) \n" + " -at outfile representation of area tree as XML (outfile req'd) \n" +
" -print input file will be rendered and sent to the printer \n" + " -print input file will be rendered and sent to the printer \n" +
} }


/** /**
* shows the options for print output
*/
* shows the options for print output
*/
public void printUsagePrintOutput() { public void printUsagePrintOutput() {
MessageHandler.errorln( MessageHandler.errorln(
"USAGE: -print [-Dstart=i] [-Dend=i] [-Dcopies=i] [-Deven=true|false] " + "USAGE: -print [-Dstart=i] [-Dend=i] [-Dcopies=i] [-Deven=true|false] " +




/** /**
* debug mode. outputs all commandline settings
*/
* debug mode. outputs all commandline settings
*/
private void debug () { private void debug () {
System.out.print("Input mode: "); System.out.print("Input mode: ");
switch (inputmode) { switch (inputmode) {
MessageHandler.logln("pcl"); MessageHandler.logln("pcl");
MessageHandler.logln("output file: " + outfile.toString()); MessageHandler.logln("output file: " + outfile.toString());
break; break;
case PS_OUTPUT:
MessageHandler.logln("PostScript");
MessageHandler.logln("output file: " + outfile.toString());
break;
case TXT_OUTPUT: case TXT_OUTPUT:
MessageHandler.logln("txt"); MessageHandler.logln("txt");
MessageHandler.logln("output file: " + outfile.toString()); MessageHandler.logln("output file: " + outfile.toString());
//debug: create class and output all settings //debug: create class and output all settings
public static void main (String args[]) { public static void main (String args[]) {
/* /*
for (int i = 0; i < args.length; i++) {
MessageHandler.logln(">"+args[i]+"<");
}*/
try {
CommandLineOptions options = new CommandLineOptions (args);
}
catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < args.length; i++) {
MessageHandler.logln(">"+args[i]+"<");
}*/
try {
CommandLineOptions options = new CommandLineOptions (args);
} catch (Exception e) {
e.printStackTrace();
}

//options.debug(); //options.debug();
} }
} }

+ 174
- 178
src/org/apache/fop/apps/Driver.java View File

/* $Id$ /* $Id$
* Copyright (C) 2001 The Apache Software Foundation. All rights reserved. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the
* LICENSE file included with these sources."
* For details on use and redistribution please refer to the
* LICENSE file included with these sources.
*/ */



package org.apache.fop.apps; package org.apache.fop.apps;


// FOP // FOP
* Here is an example use of Driver which outputs PDF: * Here is an example use of Driver which outputs PDF:
* *
* <PRE> * <PRE>
* Driver driver = new Driver(new InputSource (args[0]),
* Driver driver = new Driver(new InputSource (args[0]),
* new FileOutputStream(args[1])); * new FileOutputStream(args[1]));
* driver.setRenderer(RENDER_PDF); * driver.setRenderer(RENDER_PDF);
* driver.run(); * driver.run();
* </PRE> * </PRE>
*/ */
public class Driver { public class Driver {
/** Render to PDF. OutputStream must be set */ /** Render to PDF. OutputStream must be set */
public static final int RENDER_PDF = 1; public static final int RENDER_PDF = 1;
/** Render to a GUI window. No OutputStream neccessary */ /** Render to a GUI window. No OutputStream neccessary */
public static final int RENDER_AWT = 2; public static final int RENDER_AWT = 2;




/** Render to XML. OutputStream must be set */ /** Render to XML. OutputStream must be set */
public static final int RENDER_XML = 4; public static final int RENDER_XML = 4;
/** Render to PRINT. No OutputStream neccessary */ /** Render to PRINT. No OutputStream neccessary */
public static final int RENDER_PRINT = 5; public static final int RENDER_PRINT = 5;


/** Render to PCL. OutputStream must be set */ /** Render to PCL. OutputStream must be set */
public static final int RENDER_PCL = 6; public static final int RENDER_PCL = 6;


/** Render to Postscript. OutputStream must be set */
public static final int RENDER_PS = 7;

/** Render to Text. OutputStream must be set */ /** Render to Text. OutputStream must be set */
public static final int RENDER_TXT = 7;
public static final int RENDER_TXT = 8;


/** the FO tree builder */ /** the FO tree builder */
private FOTreeBuilder _treeBuilder; private FOTreeBuilder _treeBuilder;


/** the source of the FO file */ /** the source of the FO file */
private InputSource _source; private InputSource _source;
/** the stream to use to output the results of the renderer */ /** the stream to use to output the results of the renderer */
private OutputStream _stream; private OutputStream _stream;


/** The XML parser to use when building the FO tree */ /** The XML parser to use when building the FO tree */
private XMLReader _reader; private XMLReader _reader;
/** If true, full error stacks are reported */ /** If true, full error stacks are reported */
private boolean _errorDump = false; private boolean _errorDump = false;




/** create a new Driver */ /** create a new Driver */
public Driver() { public Driver() {
_stream = null;
_bufferManager = new BufferManager();
_treeBuilder = new FOTreeBuilder();
_treeBuilder.setBufferManager(_bufferManager);
setupDefaultMappings();
_stream = null;
_bufferManager = new BufferManager();
_treeBuilder = new FOTreeBuilder();
_treeBuilder.setBufferManager(_bufferManager);
setupDefaultMappings();
} }


public Driver(InputSource source, OutputStream stream) { public Driver(InputSource source, OutputStream stream) {
this();
_source = source;
_stream = stream;
this();
_source = source;
_stream = stream;
} }


/** /**
* Resets the Driver so it can be reused. Property and element
* Resets the Driver so it can be reused. Property and element
* mappings are reset to defaults. * mappings are reset to defaults.
* The output stream is cleared. The renderer is cleared. * The output stream is cleared. The renderer is cleared.
*/ */
public synchronized void reset()
{
_areaTree = null;
_treeBuilder.reset();
public synchronized void reset() {
_areaTree = null;
_treeBuilder.reset();
} }
public boolean hasData()
{
return(_treeBuilder.hasData());

public boolean hasData() {
return(_treeBuilder.hasData());
} }
/**
/**
* Set the error dump option * Set the error dump option
* @param dump if true, full stacks will be reported to the error log * @param dump if true, full stacks will be reported to the error log
*/ */
} }


/** /**
* Set the source for the FO document. This can be a normal SAX
* Set the source for the FO document. This can be a normal SAX
* InputSource, or an DocumentInputSource containing a DOM document. * InputSource, or an DocumentInputSource containing a DOM document.
* @see DocumentInputSource * @see DocumentInputSource
*/ */
public void setInputSource(InputSource source)
{
_source = source;
public void setInputSource(InputSource source) {
_source = source;
} }
/** /**
* Sets the reader used when reading in the source. If not set, * Sets the reader used when reading in the source. If not set,
* this defaults to a basic SAX parser. * this defaults to a basic SAX parser.
*/ */
public void setXMLReader(XMLReader reader)
{
_reader = reader;
public void setXMLReader(XMLReader reader) {
_reader = reader;
} }


/** /**
* Sets all the element and property list mappings to their default values. * Sets all the element and property list mappings to their default values.
* *
*/ */
public void setupDefaultMappings()
{
addElementMapping("org.apache.fop.fo.StandardElementMapping");
addPropertyList ("org.apache.fop.fo.StandardPropertyListMapping");
public void setupDefaultMappings() {
addElementMapping("org.apache.fop.fo.StandardElementMapping");
addPropertyList ("org.apache.fop.fo.StandardPropertyListMapping");


addElementMapping("org.apache.fop.svg.SVGElementMapping");
addPropertyList ("org.apache.fop.svg.SVGPropertyListMapping");
addElementMapping("org.apache.fop.svg.SVGElementMapping");
addPropertyList ("org.apache.fop.svg.SVGPropertyListMapping");


addElementMapping("org.apache.fop.extensions.ExtensionElementMapping"); addElementMapping("org.apache.fop.extensions.ExtensionElementMapping");
addPropertyList ("org.apache.fop.extensions.ExtensionPropertyListMapping");
addPropertyList ("org.apache.fop.extensions.ExtensionPropertyListMapping");
} }


/** /**
* Set the rendering type to use. Must be one of
* Set the rendering type to use. Must be one of
* <ul> * <ul>
* <li>RENDER_PDF * <li>RENDER_PDF
* <li>RENDER_AWT * <li>RENDER_AWT
* <li>RENDER_MIF * <li>RENDER_MIF
* <li>RENDER_XML * <li>RENDER_XML
* <li>RENDER_PCL * <li>RENDER_PCL
* <li>RENDER_PS
* <li>RENDER_TXT * <li>RENDER_TXT
* </ul> * </ul>
* @param renderer the type of renderer to use * @param renderer the type of renderer to use
*/ */
public void setRenderer(int renderer)
throws IllegalArgumentException
{
switch (renderer) {
case RENDER_PDF:
setRenderer(new org.apache.fop.render.pdf.PDFRenderer());
break;
case RENDER_AWT:
throw new IllegalArgumentException("Use renderer form of setRenderer() for AWT");
case RENDER_PRINT:
throw new IllegalArgumentException("Use renderer form of setRenderer() for PRINT");
case RENDER_PCL:
setRenderer(new org.apache.fop.render.pcl.PCLRenderer());
break;
case RENDER_TXT:
setRenderer(new org.apache.fop.render.txt.TXTRenderer());
break;
case RENDER_MIF:
setRenderer(new org.apache.fop.render.mif.MIFRenderer());
break;
case RENDER_XML:
setRenderer(new org.apache.fop.render.xml.XMLRenderer());
break;
default:
throw new IllegalArgumentException("Unknown renderer type");
}
public void setRenderer(int renderer) throws IllegalArgumentException {
switch (renderer) {
case RENDER_PDF:
setRenderer(new org.apache.fop.render.pdf.PDFRenderer());
break;
case RENDER_AWT:
throw new IllegalArgumentException("Use renderer form of setRenderer() for AWT");
case RENDER_PRINT:
throw new IllegalArgumentException("Use renderer form of setRenderer() for PRINT");
case RENDER_PCL:
setRenderer(new org.apache.fop.render.pcl.PCLRenderer());
break;
case RENDER_PS:
setRenderer(new org.apache.fop.render.ps.PSRenderer());
break;
case RENDER_TXT:
setRenderer(new org.apache.fop.render.txt.TXTRenderer());
break;
case RENDER_MIF:
setRenderer(new org.apache.fop.render.mif.MIFRenderer());
break;
case RENDER_XML:
setRenderer(new org.apache.fop.render.xml.XMLRenderer());
break;
default:
throw new IllegalArgumentException("Unknown renderer type");
}

} }


/**
* Set the Renderer to use

/**
* Set the Renderer to use
* @param renderer the renderer instance to use * @param renderer the renderer instance to use
*/ */
public void setRenderer(Renderer renderer) { public void setRenderer(Renderer renderer) {
* @see #setRenderer(int) * @see #setRenderer(int)
* @see #setRenderer(Renderer) * @see #setRenderer(Renderer)
*/ */
public void setRenderer(String rendererClassName, String version)
{
setRenderer(rendererClassName);
public void setRenderer(String rendererClassName, String version) {
setRenderer(rendererClassName);
} }
/** /**
* Set the class name of the Renderer to use as well as the * Set the class name of the Renderer to use as well as the
* producer string for those renderers that can make use of it. * producer string for those renderers that can make use of it.
* @param rendererClassName classname of the renderer to use such as
* @param rendererClassName classname of the renderer to use such as
* "org.apache.fop.render.pdf.PDFRenderer" * "org.apache.fop.render.pdf.PDFRenderer"
* @exception IllegalArgumentException if the classname was invalid. * @exception IllegalArgumentException if the classname was invalid.
* @see #setRenderer(int) * @see #setRenderer(int)
*/ */
public void setRenderer(String rendererClassName)
throws IllegalArgumentException
{
try {
_renderer = (Renderer) Class.forName(rendererClassName).newInstance();
_renderer.setProducer(Version.getVersion());
}
catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not find " +
rendererClassName);
public void setRenderer(String rendererClassName)
throws IllegalArgumentException {
try {
_renderer = (Renderer) Class.forName(
rendererClassName).newInstance();
_renderer.setProducer(Version.getVersion());
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not find " +
rendererClassName);
} }
catch (InstantiationException e) { catch (InstantiationException e) {
throw new IllegalArgumentException("Could not instantiate " +
rendererClassName);
throw new IllegalArgumentException(
"Could not instantiate " + rendererClassName);
} }
catch (IllegalAccessException e) { catch (IllegalAccessException e) {
throw new IllegalArgumentException("Could not access " + rendererClassName);
throw new IllegalArgumentException("Could not access " +
rendererClassName);
} }
catch (ClassCastException e) { catch (ClassCastException e) {
throw new IllegalArgumentException(rendererClassName + " is not a renderer"); throw new IllegalArgumentException(rendererClassName + " is not a renderer");
* @param mapping the element mappingto add * @param mapping the element mappingto add
*/ */
public void addElementMapping(ElementMapping mapping) { public void addElementMapping(ElementMapping mapping) {
mapping.addToBuilder(_treeBuilder);
mapping.addToBuilder(_treeBuilder);
} }


/** /**
* add the element mapping with the given class name * add the element mapping with the given class name
*/ */
public void addElementMapping(String mappingClassName)
throws IllegalArgumentException
{
try {
public void addElementMapping(String mappingClassName)
throws IllegalArgumentException {
try {
ElementMapping mapping = (ElementMapping) Class.forName( ElementMapping mapping = (ElementMapping) Class.forName(
mappingClassName).newInstance();
addElementMapping(mapping);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not find " + mappingClassName);
}
mappingClassName).newInstance();
addElementMapping(mapping);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not find " +
mappingClassName);
}
catch (InstantiationException e) { catch (InstantiationException e) {
throw new IllegalArgumentException("Could not instantiate " +
mappingClassName);
}
throw new IllegalArgumentException(
"Could not instantiate " + mappingClassName);
}
catch (IllegalAccessException e) { catch (IllegalAccessException e) {
throw new IllegalArgumentException("Could not access " + mappingClassName);
}
throw new IllegalArgumentException("Could not access " +
mappingClassName);
}
catch (ClassCastException e) { catch (ClassCastException e) {
throw new IllegalArgumentException(mappingClassName + " is not an ElementMapping"); throw new IllegalArgumentException(mappingClassName + " is not an ElementMapping");
}
}
} }


/** /**
* Add the PropertyListMapping. * Add the PropertyListMapping.
*/ */
public void addPropertyList(PropertyListMapping mapping)
{
mapping.addToBuilder(_treeBuilder);
public void addPropertyList(PropertyListMapping mapping) {
mapping.addToBuilder(_treeBuilder);
} }


/** /**
* Add the PropertyListMapping with the given class name. * Add the PropertyListMapping with the given class name.
*/ */
public void addPropertyList(String listClassName)
throws IllegalArgumentException
{
try {
PropertyListMapping mapping = (PropertyListMapping) Class.forName(
listClassName).newInstance();
addPropertyList(mapping);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not find " + listClassName);
}
public void addPropertyList(String listClassName)
throws IllegalArgumentException {
try {
PropertyListMapping mapping =
(PropertyListMapping) Class.forName(
listClassName).newInstance();
addPropertyList(mapping);

} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not find " +
listClassName);
}
catch (InstantiationException e) { catch (InstantiationException e) {
throw new IllegalArgumentException("Could not instantiate " +
listClassName);
}
throw new IllegalArgumentException(
"Could not instantiate " + listClassName);
}
catch (IllegalAccessException e) { catch (IllegalAccessException e) {
throw new IllegalArgumentException("Could not access " + listClassName);
}
throw new IllegalArgumentException("Could not access " +
listClassName);
}
catch (ClassCastException e) { catch (ClassCastException e) {
throw new IllegalArgumentException(listClassName + " is not an ElementMapping"); throw new IllegalArgumentException(listClassName + " is not an ElementMapping");
}
}
} }


/** /**
* SAX InputSource * SAX InputSource
*/ */
public synchronized void buildFOTree(XMLReader parser, public synchronized void buildFOTree(XMLReader parser,
InputSource source)
throws FOPException
{
InputSource source)
throws FOPException {


parser.setContentHandler(_treeBuilder); parser.setContentHandler(_treeBuilder);
try { try {
/** /**
* Build the formatting object tree using the given DOM Document * Build the formatting object tree using the given DOM Document
*/ */
public synchronized void buildFOTree(Document document)
throws FOPException
{
try {
DocumentInputSource source = new DocumentInputSource(document);
DocumentReader reader = new DocumentReader();
reader.setContentHandler(_treeBuilder);
reader.parse(source);
} catch (SAXException e) {
public synchronized void buildFOTree(Document document)
throws FOPException {
try {
DocumentInputSource source = new DocumentInputSource(document);
DocumentReader reader = new DocumentReader();
reader.setContentHandler(_treeBuilder);
reader.parse(source);
} catch (SAXException e) {
throw new FOPException(e); throw new FOPException(e);
} catch (IOException e) {
}
catch (IOException e) {
throw new FOPException(e); throw new FOPException(e);
}
}
}
}


/** /**
* Dumps an error * Dumps an error
if (((SAXException) e).getException() != null) { if (((SAXException) e).getException() != null) {
((SAXException) e).getException().printStackTrace(); ((SAXException) e).getException().printStackTrace();
} }
}
else if (e instanceof FOPException) {
} else if (e instanceof FOPException) {
e.printStackTrace(); e.printStackTrace();
if (((FOPException) e).getException() != null) { if (((FOPException) e).getException() != null) {
((FOPException) e).getException().printStackTrace(); ((FOPException) e).getException().printStackTrace();
} }
}
else {
} else {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }


/* Set up the system buffers */ /* Set up the system buffers */
public void setBufferFile(File bufferFile) { public void setBufferFile(File bufferFile) {
this._bufferManager.addBufferFile(bufferFile);
this._bufferManager.addBufferFile(bufferFile);
} }
/** /**
* format the formatting object tree into an area tree * format the formatting object tree into an area tree
*/ */
public synchronized void format() throws FOPException {
public synchronized void format()
throws FOPException {
FontInfo fontInfo = new FontInfo(); FontInfo fontInfo = new FontInfo();
_renderer.setupFontInfo(fontInfo); _renderer.setupFontInfo(fontInfo);


/** /**
* render the area tree to the output form * render the area tree to the output form
*/ */
public synchronized void render() throws IOException, FOPException {
public synchronized void render()
throws IOException, FOPException {
_renderer.render(_areaTree, _stream); _renderer.render(_areaTree, _stream);
} }


* inputsource and outputstream * inputsource and outputstream
*/ */
public synchronized void run() public synchronized void run()
throws IOException, FOPException
{
if (_renderer == null) {
setRenderer(RENDER_PDF);
}
if (_source == null) {
throw new FOPException("InputSource is not set.");
}
if (_reader == null) {
if (!(_source instanceof DocumentInputSource)) {
_reader = ConfigurationReader.createParser();
}
}
if (_source instanceof DocumentInputSource) {
buildFOTree(((DocumentInputSource)_source).getDocument());
}
else {
buildFOTree(_reader, _source);
}
format();
render();
throws IOException, FOPException {
if (_renderer == null) {
setRenderer(RENDER_PDF);
}
if (_source == null) {
throw new FOPException("InputSource is not set.");
}
if (_reader == null) {
if (!(_source instanceof DocumentInputSource)) {
_reader = ConfigurationReader.createParser();
}
}
if (_source instanceof DocumentInputSource) {
buildFOTree(((DocumentInputSource)_source).getDocument());
} else {
buildFOTree(_reader, _source);
}
format();
render();
} }

} }

Loading…
Cancel
Save