import java.util.Locale;
import java.util.Vector;
+import org.apache.fop.fo.Constants;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.SimpleLog;
/**
* Options parses the commandline arguments
*/
-public class CommandLineOptions {
-
- /** input / output not set */
- public static final int NOT_SET = 0;
- /** input: fo file */
- public static final int FO_INPUT = 1;
- /** input: xml+xsl file */
- public static final int XSLT_INPUT = 2;
- /** output: pdf file */
- public static final int PDF_OUTPUT = 1;
- /** output: screen using swing */
- public static final int AWT_OUTPUT = 2;
- /** output: mif file */
- public static final int MIF_OUTPUT = 3;
- /** output: sent swing rendered file to printer */
- public static final int PRINT_OUTPUT = 4;
- /** output: pcl file */
- public static final int PCL_OUTPUT = 5;
- /** output: postscript file */
- public static final int PS_OUTPUT = 6;
- /** output: text file */
- public static final int TXT_OUTPUT = 7;
- /** output: svg file */
- public static final int SVG_OUTPUT = 8;
- /** output: XML area tree */
- public static final int AREA_OUTPUT = 9;
- /** output: RTF file */
- public static final int RTF_OUTPUT = 10;
+public class CommandLineOptions implements Constants {
/* show configuration information */
private Boolean showConfiguration = Boolean.FALSE;
}
private int parseAWTOutputOption(String[] args, int i) throws FOPException {
- setOutputMode(AWT_OUTPUT);
+ setOutputMode(RENDER_AWT);
return 0;
}
private int parsePDFOutputOption(String[] args, int i) throws FOPException {
- setOutputMode(PDF_OUTPUT);
+ setOutputMode(RENDER_PDF);
if ((i + 1 == args.length)
|| (args[i + 1].charAt(0) == '-')) {
throw new FOPException("you must specify the pdf output file");
}
private int parseMIFOutputOption(String[] args, int i) throws FOPException {
- setOutputMode(MIF_OUTPUT);
+ setOutputMode(RENDER_MIF);
if ((i + 1 == args.length)
|| (args[i + 1].charAt(0) == '-')) {
throw new FOPException("you must specify the mif output file");
}
private int parseRTFOutputOption(String[] args, int i) throws FOPException {
- setOutputMode(RTF_OUTPUT);
+ setOutputMode(RENDER_RTF);
if ((i + 1 == args.length)
|| (args[i + 1].charAt(0) == '-')) {
throw new FOPException("you must specify the rtf output file");
}
private int parsePrintOutputOption(String[] args, int i) throws FOPException {
- setOutputMode(PRINT_OUTPUT);
+ setOutputMode(RENDER_PRINT);
return 0;
}
private int parsePCLOutputOption(String[] args, int i) throws FOPException {
- setOutputMode(PCL_OUTPUT);
+ setOutputMode(RENDER_PCL);
if ((i + 1 == args.length)
|| (args[i + 1].charAt(0) == '-')) {
throw new FOPException("you must specify the pdf output file");
}
private int parsePostscriptOutputOption(String[] args, int i) throws FOPException {
- setOutputMode(PS_OUTPUT);
+ setOutputMode(RENDER_PS);
if ((i + 1 == args.length)
|| (args[i + 1].charAt(0) == '-')) {
throw new FOPException("you must specify the PostScript output file");
}
private int parseTextOutputOption(String[] args, int i) throws FOPException {
- setOutputMode(TXT_OUTPUT);
+ setOutputMode(RENDER_TXT);
if ((i + 1 == args.length)
|| (args[i + 1].charAt(0) == '-')) {
throw new FOPException("you must specify the text output file");
}
private int parseSVGOutputOption(String[] args, int i) throws FOPException {
- setOutputMode(SVG_OUTPUT);
+ setOutputMode(RENDER_SVG);
if ((i + 1 == args.length)
|| (args[i + 1].charAt(0) == '-')) {
throw new FOPException("you must specify the svg output file");
inputmode = FO_INPUT;
fofile = new File(args[i]);
} else if (outputmode == NOT_SET) {
- outputmode = PDF_OUTPUT;
+ outputmode = RENDER_PDF;
outfile = new File(args[i]);
} else {
throw new FOPException("Don't know what to do with "
}
private int parseAreaTreeOption(String[] args, int i) throws FOPException {
- setOutputMode(AREA_OUTPUT);
+ setOutputMode(RENDER_XML);
if ((i + 1 == args.length)
|| (args[i + 1].charAt(0) == '-')) {
throw new FOPException("you must specify the area-tree output file");
throw new FOPException("No output file specified");
}
- if ((outputmode == AWT_OUTPUT || outputmode == PRINT_OUTPUT) && outfile != null) {
+ if ((outputmode == RENDER_AWT || outputmode == RENDER_PRINT) && outfile != null) {
throw new FOPException("Output file may not be specified " +
"for AWT or PRINT output");
}
switch (outputmode) {
case NOT_SET:
throw new FOPException("Renderer has not been set!");
- case PDF_OUTPUT:
+ case RENDER_PDF:
return Driver.RENDER_PDF;
- case AWT_OUTPUT:
+ case RENDER_AWT:
return Driver.RENDER_AWT;
- case MIF_OUTPUT:
+ case RENDER_MIF:
return Driver.RENDER_MIF;
- case PRINT_OUTPUT:
+ case RENDER_PRINT:
return Driver.RENDER_PRINT;
- case PCL_OUTPUT:
+ case RENDER_PCL:
return Driver.RENDER_PCL;
- case PS_OUTPUT:
+ case RENDER_PS:
return Driver.RENDER_PS;
- case TXT_OUTPUT:
+ case RENDER_TXT:
return Driver.RENDER_TXT;
- case SVG_OUTPUT:
+ case RENDER_SVG:
return Driver.RENDER_SVG;
- case AREA_OUTPUT:
+ case RENDER_XML:
foUserAgent.getRendererOptions().put("fineDetail", isCoarseAreaXml());
return Driver.RENDER_XML;
- case RTF_OUTPUT:
+ case RENDER_RTF:
return Driver.RENDER_RTF;
default:
throw new FOPException("Invalid Renderer setting!");
}
/**
- * Returns the output mode (output format, ex. NOT_SET or PDF_OUTPUT)
+ * Returns the output mode (output format, ex. NOT_SET or RENDER_PDF)
* @return the output mode
*/
public int getOutputMode() {
case NOT_SET:
log.info("not set");
break;
- case PDF_OUTPUT:
+ case RENDER_PDF:
log.info("pdf");
log.info("output file: " + outfile.toString());
break;
- case AWT_OUTPUT:
+ case RENDER_AWT:
log.info("awt on screen");
if (outfile != null) {
log.error("awt mode, but outfile is set:");
log.info("out file: " + outfile.toString());
}
break;
- case MIF_OUTPUT:
+ case RENDER_MIF:
log.info("mif");
log.info("output file: " + outfile.toString());
break;
- case RTF_OUTPUT:
+ case RENDER_RTF:
log.info("rtf");
log.info("output file: " + outfile.toString());
break;
- case PRINT_OUTPUT:
+ case RENDER_PRINT:
log.info("print directly");
if (outfile != null) {
log.error("print mode, but outfile is set:");
log.error("out file: " + outfile.toString());
}
break;
- case PCL_OUTPUT:
+ case RENDER_PCL:
log.info("pcl");
log.info("output file: " + outfile.toString());
break;
- case PS_OUTPUT:
+ case RENDER_PS:
log.info("PostScript");
log.info("output file: " + outfile.toString());
break;
- case TXT_OUTPUT:
+ case RENDER_TXT:
log.info("txt");
log.info("output file: " + outfile.toString());
break;
- case SVG_OUTPUT:
+ case RENDER_SVG:
log.info("svg");
log.info("output file: " + outfile.toString());
break;
import org.w3c.dom.Document;
// FOP
+import org.apache.fop.fo.Constants;
import org.apache.fop.fo.ElementMapping;
import org.apache.fop.fo.FOTreeBuilder;
import org.apache.fop.fo.FOInputHandler;
* driver.render(parser, fileInputSource(args[0]));
* </PRE>
*/
-public class Driver {
-
- /**
- * private constant to indicate renderer was not defined.
- */
- private static final int NOT_SET = 0;
-
- /**
- * Render to PDF. OutputStream must be set
- */
- public static final int RENDER_PDF = 1;
-
- /**
- * Render to a GUI window. No OutputStream neccessary
- */
- public static final int RENDER_AWT = 2;
-
- /**
- * Render to MIF. OutputStream must be set
- */
- public static final int RENDER_MIF = 3;
-
- /**
- * Render to XML. OutputStream must be set
- */
- public static final int RENDER_XML = 4;
-
- /**
- * Render to PRINT. No OutputStream neccessary
- */
- public static final int RENDER_PRINT = 5;
-
- /**
- * Render to PCL. OutputStream must be set
- */
- 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
- */
- public static final int RENDER_TXT = 8;
-
- /**
- * Render to SVG. OutputStream must be set
- */
- public static final int RENDER_SVG = 9;
-
- /**
- * Render to RTF. OutputStream must be set
- */
- public static final int RENDER_RTF = 10;
+public class Driver implements Constants {
/**
* the FO tree builder
stream = null;
}
+ /**
+ * Constructor for AWTRenderer, which reuses the
+ * same renderer instance for document reloading
+ */
+ public Driver(AWTRenderer renderer) {
+ this();
+ setRenderer(renderer);
+ }
+
/**
* Convenience constructor for directly setting input and output.
* @param source InputSource to take the XSL-FO input from
*/
public void setUserAgent(FOUserAgent agent) {
userAgent = agent;
+ if (renderer != null) {
+ renderer.setUserAgent(userAgent);
+ }
}
+ /**
+ * Get the FOUserAgent instance for this process
+ * @return the user agent
+ */
public FOUserAgent getUserAgent() {
if (userAgent == null) {
userAgent = new FOUserAgent();
}
}
- /**
- * Indicates whether FOP has already received input data.
- * @return true, if input data was received
- */
- public boolean hasData() {
- return (treeBuilder.hasData());
- }
-
/**
* Set the OutputStream to use to output the result of the Renderer
* (if applicable)
}
/**
- * Shortcut to set the rendering type to use. Must be one of
+ * Method to set the rendering type to use. Must be one of
* <ul>
* <li>RENDER_PDF</li>
* <li>RENDER_AWT</li>
rendererType = renderer;
switch (renderer) {
case RENDER_PDF:
- setRenderer("org.apache.fop.render.pdf.PDFRenderer");
+ setRenderer(new org.apache.fop.render.pdf.PDFRenderer());
break;
case RENDER_AWT:
- setRenderer("org.apache.fop.render.awt.AWTRenderer");
+ setRenderer(new org.apache.fop.render.awt.AWTRenderer());
break;
case RENDER_PRINT:
- setRenderer("org.apache.fop.render.awt.AWTPrintRenderer");
+ setRenderer(new org.apache.fop.render.awt.AWTPrintRenderer());
break;
case RENDER_PCL:
- setRenderer("org.apache.fop.render.pcl.PCLRenderer");
+ setRenderer(new org.apache.fop.render.pcl.PCLRenderer());
break;
case RENDER_PS:
- setRenderer("org.apache.fop.render.ps.PSRenderer");
+ setRenderer(new org.apache.fop.render.ps.PSRenderer());
break;
case RENDER_TXT:
- setRenderer("org.apache.fop.render.txt.TXTRenderer()");
+ setRenderer(new org.apache.fop.render.txt.TXTRenderer());
break;
case RENDER_MIF:
//foInputHandler will be set later
break;
case RENDER_XML:
- setRenderer("org.apache.fop.render.xml.XMLRenderer");
+ setRenderer(new org.apache.fop.render.xml.XMLRenderer());
break;
case RENDER_SVG:
- setRenderer("org.apache.fop.render.svg.SVGRenderer");
+ setRenderer(new org.apache.fop.render.svg.SVGRenderer());
break;
case RENDER_RTF:
//foInputHandler will be set later
* Set the Renderer to use.
* @param renderer the renderer instance to use
*/
- public void setRenderer(Renderer renderer) {
- // AWTStarter calls this function directly
- if (renderer instanceof AWTRenderer) {
- rendererType = RENDER_AWT;
- }
+ private void setRenderer(Renderer renderer) {
renderer.setUserAgent(getUserAgent());
- userAgent.setProducer("FOP Version" + Fop.getVersion());
this.renderer = renderer;
}
- /**
- * Set the class name of the Renderer to use as well as the
- * producer string for those renderers that can make use of it.
- * @param rendererClassName classname of the renderer to use such as
- * "org.apache.fop.render.pdf.PDFRenderer"
- * @exception IllegalArgumentException if the classname was invalid.
- * @see #setRenderer(int)
- */
- public void setRenderer(String rendererClassName)
- throws IllegalArgumentException {
- try {
- renderer = (Renderer)Class.forName(rendererClassName).newInstance();
- renderer.setUserAgent(getUserAgent());
- userAgent.setProducer("FOP Version" + Fop.getVersion());
- } catch (ClassNotFoundException e) {
- throw new IllegalArgumentException("Could not find "
- + rendererClassName);
- } catch (InstantiationException e) {
- throw new IllegalArgumentException("Could not instantiate "
- + rendererClassName);
- } catch (IllegalAccessException e) {
- throw new IllegalArgumentException("Could not access "
- + rendererClassName);
- } catch (ClassCastException e) {
- throw new IllegalArgumentException(rendererClassName
- + " is not a renderer");
- }
- }
-
/**
* Add the given element mapping.
* An element mapping maps element names to Java classes.
/** Producer: Metadata element for the system/software that produces
* the document. (Some renderers can store this in the document.)
*/
- protected String producer = "FOP " + Fop.getVersion();
+ protected String producer = "FOP Version " + Fop.getVersion();
/** Creator: Metadata element for the user that created the
* document. (Some renderers can store this in the document.)
// System.exit(0) called to close AWT/SVG-created threads, if any.
// AWTRenderer closes with window shutdown, so exit() should not
// be called here
- if (options.getOutputMode() != CommandLineOptions.AWT_OUTPUT) {
+ if (options.getOutputMode() != CommandLineOptions.RENDER_AWT) {
System.exit(0);
}
} catch (FOPException e) {
public interface Constants {
+ /* These constants are used by apps.CommandLineOptions and
+ apps.Driver to describe the input (either .FO or .XML/.XSL)
+ and desired output (PDF, PS, AWT, etc.) of the document */
+
+ /** input / output not set */
+ int NOT_SET = 0;
+ /** input: fo file */
+ int FO_INPUT = 1;
+ /** input: xml+xsl file */
+ int XSLT_INPUT = 2;
+ /** output: pdf file */
+ int RENDER_PDF = 1;
+ /** output: screen using swing */
+ int RENDER_AWT = 2;
+ /** output: mif file */
+ int RENDER_MIF = 3;
+ /** output: sent swing rendered file to printer */
+ int RENDER_PRINT = 4;
+ /** output: pcl file */
+ int RENDER_PCL = 5;
+ /** output: postscript file */
+ int RENDER_PS = 6;
+ /** output: text file */
+ int RENDER_TXT = 7;
+ /** output: svg file */
+ int RENDER_SVG = 8;
+ /** output: XML area tree */
+ int RENDER_XML = 9;
+ /** output: RTF file */
+ int RENDER_RTF = 10;
+
// element constants
-
int FO_BASIC_LINK = 1;
int FO_BIDI_OVERRIDE = 2;
int FO_BLOCK = 3;
foInputHandler = null;
}
- /**
- * Indicates if data has been processed.
- * @return True if data has been processed
- */
- public boolean hasData() {
- return (rootFObj != null);
- }
-
}
// code stolen from org.apache.batik.util and modified slightly
return null;
}
+ /**
+ * Convenience method for validity checking. Checks if the
+ * incoming node is a member of the "%block;" parameter entity
+ * as defined in Sect. 6.2 of the XSL 1.0 & 1.1 Recommendations
+ * @param nsURI namespace URI of incoming invalid node
+ * @param lName local name (i.e., no prefix) of incoming node
+ * @return true if a member, false if not
+ */
+ protected static boolean isBlockItem(String nsURI, String lName) {
+ return (nsURI == FOElementMapping.URI &&
+ (lName.equals("block")
+ || lName.equals("table")
+ || lName.equals("table-and-caption")
+ || lName.equals("block-container")
+ || lName.equals("list-block")
+ || lName.equals("float")))
+ || isNeutralItem(nsURI, lName);
+ }
+
+ /**
+ * Convenience method for validity checking. Checks if the
+ * incoming node is a member of the neutral item list
+ * as defined in Sect. 6.2 of the XSL 1.0 & 1.1 Recommendations
+ * @param nsURI namespace URI of incoming invalid node
+ * @param lName local name (i.e., no prefix) of incoming node
+ * @return true if a member, false if not
+ */
+ protected static boolean isNeutralItem(String nsURI, String lName) {
+ return (nsURI == FOElementMapping.URI &&
+ (lName.equals("multi-switch")
+ || lName.equals("multi-properties")
+ || lName.equals("wrapper")
+ || lName.equals("retrieve-marker")));
+ }
}
// XML
import org.xml.sax.Attributes;
+import org.xml.sax.Locator;
// FOP
import org.apache.fop.fo.FONode;
super(parent);
}
+ /**
+ * @see org.apache.fop.fo.FONode#validateChildNode(Locator, String, String)
+ * XSL/FOP Content Model: (%block;)+
+ */
+ protected void validateChildNode(Locator loc, String nsURI, String localName) {
+ if (!isBlockItem(nsURI, localName)) {
+ invalidChildError(loc, nsURI, localName);
+ }
+ }
+
+ /**
+ * Make sure content model satisfied, if so then tell the
+ * StructureRenderer that we are at the end of the flow.
+ * @see org.apache.fop.fo.FONode#end
+ */
+ protected void end() {
+ if (children == null) {
+ missingChildElementError("(%block;)+");
+ }
+ getFOInputHandler().endFlow(this);
+ }
+
/**
* @see org.apache.fop.fo.FObj#addProperties
*/
getFOInputHandler().startFlow(this);
}
- /**
- * Tell the StructureRenderer that we are at the end of the flow.
- */
- protected void end() {
- getFOInputHandler().endFlow(this);
- }
-
/**
* @param name the name of the flow to set
* @throws FOPException for an empty name
*/
protected void end() {
if (children == null) {
- missingChildElementError("(simple-page-master|page-sequence-master)+");
+ missingChildElementError("(simple-page-master|page-sequence-master)+");
}
}
private class Reloader extends Thread {
public void run() {
if (driver == null) {
- driver = new Driver();
- driver.setRenderer(renderer);
+ driver = new Driver(renderer);
} else {
driver.reset();
}
private void renderPDF(String fo, String digestIn, String digestOut)
throws Exception {
- PDFRenderer renderer = new PDFRenderer();
FOUserAgent foUserAgent = new FOUserAgent();
foUserAgent.setCreationDate(new Date(10000));
- renderer.setUserAgent(foUserAgent);
MessageDigest outDigest = MessageDigest.getInstance("MD5");
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
DigestOutputStream out =
new DigestOutputStream(new ByteArrayOutputStream(), outDigest);
Driver driver = new Driver();
- driver.setRenderer(renderer);
+ driver.setUserAgent(foUserAgent);
+ driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(out);
InputSource source = new InputSource(new StringReader(fo));
DigestFilter filter = new DigestFilter("MD5");