Finalized API according to the plan in the Wiki: http://wiki.apache.org/xmlgraphics-fop/ApiDesign In addition to that: Deprecated the rest of the Fop constructors. Refactored the FopServlet a bit to make it more versatile and still easy to understand. The FopPrintServlet is now a subclass of FopServlet. Some further cleanup on the way. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@389086 13f79535-47bb-0310-9956-ffa450edef68pull/35/head
<property name="Name" value="Apache FOP"/> | <property name="Name" value="Apache FOP"/> | ||||
<property name="name" value="fop"/> | <property name="name" value="fop"/> | ||||
<property name="NAME" value="FOP"/> | <property name="NAME" value="FOP"/> | ||||
<property name="version" value="0.91svn"/> | |||||
<property name="version" value="svn-trunk"/> | |||||
<property name="year" value="1999-2006"/> | <property name="year" value="1999-2006"/> | ||||
<property name="javac.debug" value="on"/> | <property name="javac.debug" value="on"/> |
/* | /* | ||||
* Copyright 1999-2003,2005 The Apache Software Foundation. | |||||
* Copyright 1999-2003,2005-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
//FOP | //FOP | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.fo.Constants; | |||||
/** | /** | ||||
* This class demonstrates the use of the AWT Viewer. | * This class demonstrates the use of the AWT Viewer. | ||||
*/ | */ | ||||
public class ExampleAWTViewer { | public class ExampleAWTViewer { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
/** | |||||
* Display an FO file in the AWT Preview. | |||||
* @param fo the FO file | |||||
* @throws IOException In case of an I/O problem | |||||
* @throws FOPException In case of a problem during layout | |||||
* @throws TransformerException In case of a problem during XML processing | |||||
*/ | |||||
public void viewFO(File fo) | public void viewFO(File fo) | ||||
throws IOException, FOPException, TransformerException { | throws IOException, FOPException, TransformerException { | ||||
//Setup FOP | //Setup FOP | ||||
Fop fop = new Fop(MimeConstants.MIME_FOP_AWT_PREVIEW); | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AWT_PREVIEW); | |||||
try { | try { | ||||
} | } | ||||
} | } | ||||
/** | |||||
* Main method. | |||||
* @param args the command-line arguments | |||||
*/ | |||||
public static void main(String[] args) { | public static void main(String[] args) { | ||||
try { | try { | ||||
System.out.println("FOP ExampleAWTViewer\n"); | System.out.println("FOP ExampleAWTViewer\n"); |
/* | /* | ||||
* Copyright 1999-2004 The Apache Software Foundation. | |||||
* Copyright 1999-2004, 2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
// Java | // Java | ||||
import java.io.File; | import java.io.File; | ||||
import java.io.IOException; | |||||
import java.io.OutputStream; | import java.io.OutputStream; | ||||
import javax.xml.parsers.DocumentBuilderFactory; | import javax.xml.parsers.DocumentBuilderFactory; | ||||
import javax.xml.parsers.DocumentBuilder; | import javax.xml.parsers.DocumentBuilder; | ||||
import javax.xml.parsers.ParserConfigurationException; | |||||
//JAXP | //JAXP | ||||
import javax.xml.transform.Transformer; | import javax.xml.transform.Transformer; | ||||
import org.w3c.dom.Text; | import org.w3c.dom.Text; | ||||
// FOP | // FOP | ||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
*/ | */ | ||||
public class ExampleDOM2PDF { | public class ExampleDOM2PDF { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
/** xsl-fo namespace URI */ | /** xsl-fo namespace URI */ | ||||
protected static String foNS = "http://www.w3.org/1999/XSL/Format"; | protected static String foNS = "http://www.w3.org/1999/XSL/Format"; | ||||
* Converts a DOM Document to a PDF file using FOP. | * Converts a DOM Document to a PDF file using FOP. | ||||
* @param xslfoDoc the DOM Document | * @param xslfoDoc the DOM Document | ||||
* @param pdf the target PDF file | * @param pdf the target PDF file | ||||
* @throws IOException In case of an I/O problem | |||||
* @throws FOPException In case of a FOP problem | |||||
*/ | */ | ||||
public void convertDOM2PDF(Document xslfoDoc, File pdf) { | public void convertDOM2PDF(Document xslfoDoc, File pdf) { | ||||
try { | try { | ||||
// Construct fop with desired output format | |||||
Fop fop = new Fop(MimeConstants.MIME_PDF); | |||||
FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | |||||
// configure foUserAgent as desired | |||||
// Setup output | // Setup output | ||||
OutputStream out = new java.io.FileOutputStream(pdf); | OutputStream out = new java.io.FileOutputStream(pdf); | ||||
out = new java.io.BufferedOutputStream(out); | out = new java.io.BufferedOutputStream(out); | ||||
try { | try { | ||||
fop.setOutputStream(out); | |||||
// Construct fop with desired output format and output stream | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); | |||||
// Setup Identity Transformer | // Setup Identity Transformer | ||||
TransformerFactory factory = TransformerFactory.newInstance(); | TransformerFactory factory = TransformerFactory.newInstance(); | ||||
System.out.println("PDF Output File: " + pdffile); | System.out.println("PDF Output File: " + pdffile); | ||||
System.out.println(); | System.out.println(); | ||||
// Create a sample XSL-FO DOM document | |||||
Document foDoc = null; | |||||
Element root = null, ele1 = null, ele2 = null, ele3 = null; | |||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |||||
dbf.setNamespaceAware(true); | |||||
DocumentBuilder db = dbf.newDocumentBuilder(); | |||||
foDoc = db.newDocument(); | |||||
root = foDoc.createElementNS(foNS, "fo:root"); | |||||
foDoc.appendChild(root); | |||||
ele1 = foDoc.createElementNS(foNS, "fo:layout-master-set"); | |||||
root.appendChild(ele1); | |||||
ele2 = foDoc.createElementNS(foNS, "fo:simple-page-master"); | |||||
ele1.appendChild(ele2); | |||||
ele2.setAttributeNS(null, "master-name", "letter"); | |||||
ele2.setAttributeNS(null, "page-height", "11in"); | |||||
ele2.setAttributeNS(null, "page-width", "8.5in"); | |||||
ele2.setAttributeNS(null, "margin-top", "1in"); | |||||
ele2.setAttributeNS(null, "margin-bottom", "1in"); | |||||
ele2.setAttributeNS(null, "margin-left", "1in"); | |||||
ele2.setAttributeNS(null, "margin-right", "1in"); | |||||
ele3 = foDoc.createElementNS(foNS, "fo:region-body"); | |||||
ele2.appendChild(ele3); | |||||
ele1 = foDoc.createElementNS(foNS, "fo:page-sequence"); | |||||
root.appendChild(ele1); | |||||
ele1.setAttributeNS(null, "master-reference", "letter"); | |||||
ele2 = foDoc.createElementNS(foNS, "fo:flow"); | |||||
ele1.appendChild(ele2); | |||||
ele2.setAttributeNS(null, "flow-name", "xsl-region-body"); | |||||
addElement(ele2, "fo:block", "Hello World!"); | |||||
Document foDoc = buildDOMDocument(); | |||||
ExampleDOM2PDF app = new ExampleDOM2PDF(); | ExampleDOM2PDF app = new ExampleDOM2PDF(); | ||||
app.convertDOM2PDF(foDoc, pdffile); | app.convertDOM2PDF(foDoc, pdffile); | ||||
} | } | ||||
} | } | ||||
/** | |||||
* Builds the example FO document as a DOM in memory. | |||||
* @return the FO document | |||||
* @throws ParserConfigurationException In case there is a problem creating a DOM document | |||||
*/ | |||||
private static Document buildDOMDocument() throws ParserConfigurationException { | |||||
// Create a sample XSL-FO DOM document | |||||
Document foDoc = null; | |||||
Element root = null, ele1 = null, ele2 = null, ele3 = null; | |||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |||||
dbf.setNamespaceAware(true); | |||||
DocumentBuilder db = dbf.newDocumentBuilder(); | |||||
foDoc = db.newDocument(); | |||||
root = foDoc.createElementNS(foNS, "fo:root"); | |||||
foDoc.appendChild(root); | |||||
ele1 = foDoc.createElementNS(foNS, "fo:layout-master-set"); | |||||
root.appendChild(ele1); | |||||
ele2 = foDoc.createElementNS(foNS, "fo:simple-page-master"); | |||||
ele1.appendChild(ele2); | |||||
ele2.setAttributeNS(null, "master-name", "letter"); | |||||
ele2.setAttributeNS(null, "page-height", "11in"); | |||||
ele2.setAttributeNS(null, "page-width", "8.5in"); | |||||
ele2.setAttributeNS(null, "margin-top", "1in"); | |||||
ele2.setAttributeNS(null, "margin-bottom", "1in"); | |||||
ele2.setAttributeNS(null, "margin-left", "1in"); | |||||
ele2.setAttributeNS(null, "margin-right", "1in"); | |||||
ele3 = foDoc.createElementNS(foNS, "fo:region-body"); | |||||
ele2.appendChild(ele3); | |||||
ele1 = foDoc.createElementNS(foNS, "fo:page-sequence"); | |||||
root.appendChild(ele1); | |||||
ele1.setAttributeNS(null, "master-reference", "letter"); | |||||
ele2 = foDoc.createElementNS(foNS, "fo:flow"); | |||||
ele1.appendChild(ele2); | |||||
ele2.setAttributeNS(null, "flow-name", "xsl-region-body"); | |||||
addElement(ele2, "fo:block", "Hello World!"); | |||||
return foDoc; | |||||
} | |||||
/** | /** | ||||
* Adds an element to the DOM. | * Adds an element to the DOM. | ||||
* @param parent parent node to attach the new element to | * @param parent parent node to attach the new element to |
/* | /* | ||||
* Copyright 1999-2005 The Apache Software Foundation. | |||||
* Copyright 1999-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.MimeConstants; | |||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.render.print.PrintRenderer; | import org.apache.fop.render.print.PrintRenderer; | ||||
/** | /** | ||||
*/ | */ | ||||
public class ExampleFO2OldStylePrint { | public class ExampleFO2OldStylePrint { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
/** | /** | ||||
* Prints an FO file using an old-style PrinterJob. | * Prints an FO file using an old-style PrinterJob. | ||||
* @param fo the FO file | * @param fo the FO file | ||||
try { | try { | ||||
//Set up a custom user agent so we can supply our own renderer instance | //Set up a custom user agent so we can supply our own renderer instance | ||||
FOUserAgent userAgent = new FOUserAgent(); | |||||
FOUserAgent userAgent = fopFactory.newFOUserAgent(); | |||||
userAgent.setRendererOverride(renderer); | userAgent.setRendererOverride(renderer); | ||||
// Construct fop with desired output format | |||||
Fop fop = new Fop(MimeConstants.MIME_FOP_PRINT, userAgent); | |||||
//Note: the first parameter here has no effect if we use | |||||
//FOUserAgent.setRendererOverride() | |||||
// Construct fop with desired output format (here, it is set through the user agent) | |||||
Fop fop = fopFactory.newFop(userAgent); | |||||
// Setup JAXP using identity transformer | // Setup JAXP using identity transformer | ||||
TransformerFactory factory = TransformerFactory.newInstance(); | TransformerFactory factory = TransformerFactory.newInstance(); |
/* | /* | ||||
* Copyright 1999-2005 The Apache Software Foundation. | |||||
* Copyright 1999-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
// FOP | // FOP | ||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.FormattingResults; | import org.apache.fop.apps.FormattingResults; | ||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.apps.PageSequenceResults; | import org.apache.fop.apps.PageSequenceResults; | ||||
*/ | */ | ||||
public class ExampleFO2PDF { | public class ExampleFO2PDF { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
/** | /** | ||||
* Converts an FO file to a PDF file using FOP | * Converts an FO file to a PDF file using FOP | ||||
* @param fo the FO file | * @param fo the FO file | ||||
OutputStream out = null; | OutputStream out = null; | ||||
try { | try { | ||||
// Construct fop with desired output format | |||||
Fop fop = new Fop(MimeConstants.MIME_PDF); | |||||
FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | |||||
// configure foUserAgent as desired | |||||
// Setup output stream. Note: Using BufferedOutputStream | // Setup output stream. Note: Using BufferedOutputStream | ||||
// for performance reasons (helpful with FileOutputStreams). | // for performance reasons (helpful with FileOutputStreams). | ||||
out = new FileOutputStream(pdf); | out = new FileOutputStream(pdf); | ||||
out = new BufferedOutputStream(out); | out = new BufferedOutputStream(out); | ||||
fop.setOutputStream(out); | |||||
// Construct fop with desired output format | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); | |||||
// Setup JAXP using identity transformer | // Setup JAXP using identity transformer | ||||
TransformerFactory factory = TransformerFactory.newInstance(); | TransformerFactory factory = TransformerFactory.newInstance(); |
/* | /* | ||||
* Copyright 1999-2004 The Apache Software Foundation. | |||||
* Copyright 1999-2004,2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import org.xml.sax.SAXException; | import org.xml.sax.SAXException; | ||||
// FOP | // FOP | ||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FOPException; | |||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
/** | /** | ||||
*/ | */ | ||||
public class ExampleFO2PDFUsingSAXParser { | public class ExampleFO2PDFUsingSAXParser { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
/** | /** | ||||
* Converts an FO file to a PDF file using FOP | * Converts an FO file to a PDF file using FOP | ||||
* @param fo the FO file | * @param fo the FO file | ||||
* @param pdf the target PDF file | * @param pdf the target PDF file | ||||
* @throws FactoryConfigurationError | |||||
* @throws ParserConfigurationException | |||||
* @throws SAXException | |||||
* @throws FactoryConfigurationError In case of a problem with the JAXP factory configuration | |||||
* @throws ParserConfigurationException In case of a problem with the parser configuration | |||||
* @throws SAXException In case of a problem during XML processing | |||||
* @throws IOException In case of an I/O problem | * @throws IOException In case of an I/O problem | ||||
* @throws FOPException In case of a FOP problem | |||||
*/ | */ | ||||
public void convertFO2PDF(File fo, File pdf) | public void convertFO2PDF(File fo, File pdf) | ||||
throws FactoryConfigurationError, | throws FactoryConfigurationError, | ||||
ParserConfigurationException, | ParserConfigurationException, | ||||
FOPException, SAXException, IOException { | |||||
SAXException, IOException { | |||||
FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | |||||
// configure foUserAgent as desired | |||||
OutputStream out = null; | OutputStream out = null; | ||||
try { | try { | ||||
// Construct fop and setup output format | |||||
Fop fop = new Fop(MimeConstants.MIME_PDF); | |||||
// Setup output stream. Note: Using BufferedOutputStream | // Setup output stream. Note: Using BufferedOutputStream | ||||
// for performance reasons (helpful with FileOutputStreams). | // for performance reasons (helpful with FileOutputStreams). | ||||
out = new FileOutputStream(pdf); | out = new FileOutputStream(pdf); | ||||
out = new BufferedOutputStream(out); | out = new BufferedOutputStream(out); | ||||
fop.setOutputStream(out); | |||||
// Construct fop and setup output format | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); | |||||
// Setup SAX parser | // Setup SAX parser | ||||
// throws FactoryConfigurationError | // throws FactoryConfigurationError |
/* | /* | ||||
* Copyright 2005 The Apache Software Foundation. | |||||
* Copyright 2005-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import javax.xml.transform.sax.SAXResult; | import javax.xml.transform.sax.SAXResult; | ||||
// FOP | // FOP | ||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
/** | /** | ||||
* This class demonstrates the conversion of an FO file to RTF using FOP. | * This class demonstrates the conversion of an FO file to RTF using FOP. | ||||
* <p> | * <p> | ||||
* Please note that this is practically the same as the ExampleFO2PDF example. Only | * Please note that this is practically the same as the ExampleFO2PDF example. Only | ||||
* one parameter to the Fop contructor is different! | |||||
* the MIME parameter to the newFop() method is different! | |||||
*/ | */ | ||||
public class ExampleFO2RTF { | public class ExampleFO2RTF { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
/** | /** | ||||
* Converts an FO file to a RTF file using FOP | * Converts an FO file to a RTF file using FOP | ||||
* @param fo the FO file | * @param fo the FO file | ||||
*/ | */ | ||||
public void convertFO2RTF(File fo, File rtf) throws IOException, FOPException { | public void convertFO2RTF(File fo, File rtf) throws IOException, FOPException { | ||||
FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | |||||
// configure foUserAgent as desired | |||||
OutputStream out = null; | OutputStream out = null; | ||||
try { | try { | ||||
// Construct fop with desired output format | |||||
Fop fop = new Fop(MimeConstants.MIME_RTF); | |||||
// Setup output stream. Note: Using BufferedOutputStream | // Setup output stream. Note: Using BufferedOutputStream | ||||
// for performance reasons (helpful with FileOutputStreams). | // for performance reasons (helpful with FileOutputStreams). | ||||
out = new FileOutputStream(rtf); | out = new FileOutputStream(rtf); | ||||
out = new BufferedOutputStream(out); | out = new BufferedOutputStream(out); | ||||
fop.setOutputStream(out); | |||||
// Construct fop with desired output format | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_RTF, foUserAgent, out); | |||||
// Setup JAXP using identity transformer | // Setup JAXP using identity transformer | ||||
TransformerFactory factory = TransformerFactory.newInstance(); | TransformerFactory factory = TransformerFactory.newInstance(); | ||||
Transformer transformer = factory.newTransformer(); // identity transformer | Transformer transformer = factory.newTransformer(); // identity transformer |
/* | /* | ||||
* Copyright 1999-2004 The Apache Software Foundation. | |||||
* Copyright 1999-2004,2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import javax.xml.transform.sax.SAXResult; | import javax.xml.transform.sax.SAXResult; | ||||
// FOP | // FOP | ||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import embedding.model.ProjectTeam; | import embedding.model.ProjectTeam; | ||||
*/ | */ | ||||
public class ExampleObj2PDF { | public class ExampleObj2PDF { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
/** | /** | ||||
* Converts a ProjectTeam object to a PDF file. | * Converts a ProjectTeam object to a PDF file. | ||||
* @param team the ProjectTeam object | * @param team the ProjectTeam object | ||||
public void convertProjectTeam2PDF(ProjectTeam team, File xslt, File pdf) | public void convertProjectTeam2PDF(ProjectTeam team, File xslt, File pdf) | ||||
throws IOException, FOPException, TransformerException { | throws IOException, FOPException, TransformerException { | ||||
// Construct fop with desired output format | |||||
Fop fop = new Fop(MimeConstants.MIME_PDF); | |||||
FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | |||||
// configure foUserAgent as desired | |||||
// Setup output | // Setup output | ||||
OutputStream out = new java.io.FileOutputStream(pdf); | OutputStream out = new java.io.FileOutputStream(pdf); | ||||
out = new java.io.BufferedOutputStream(out); | out = new java.io.BufferedOutputStream(out); | ||||
try { | try { | ||||
fop.setOutputStream(out); | |||||
// Construct fop with desired output format | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); | |||||
// Setup XSLT | // Setup XSLT | ||||
TransformerFactory factory = TransformerFactory.newInstance(); | TransformerFactory factory = TransformerFactory.newInstance(); |
/* | /* | ||||
* Copyright 1999-2005 The Apache Software Foundation. | |||||
* Copyright 1999-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import javax.xml.transform.sax.SAXResult; | import javax.xml.transform.sax.SAXResult; | ||||
//FOP | //FOP | ||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
/** | /** | ||||
System.out.println(); | System.out.println(); | ||||
System.out.println("Transforming..."); | System.out.println("Transforming..."); | ||||
// Construct fop with desired output format | |||||
Fop fop = new Fop(MimeConstants.MIME_PDF); | |||||
// configure fopFactory as desired | |||||
FopFactory fopFactory = FopFactory.newInstance(); | |||||
FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | |||||
// configure foUserAgent as desired | |||||
// Setup output | // Setup output | ||||
OutputStream out = new java.io.FileOutputStream(pdffile); | OutputStream out = new java.io.FileOutputStream(pdffile); | ||||
out = new java.io.BufferedOutputStream(out); | out = new java.io.BufferedOutputStream(out); | ||||
try { | try { | ||||
fop.setOutputStream(out); | |||||
// Construct fop with desired output format | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); | |||||
// Setup XSLT | // Setup XSLT | ||||
TransformerFactory factory = TransformerFactory.newInstance(); | TransformerFactory factory = TransformerFactory.newInstance(); |
/* | |||||
* Copyright 2006 The Apache Software Foundation. | |||||
* | |||||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||||
* you may not use this file except in compliance with the License. | |||||
* You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
*/ | |||||
/* $Id$ */ | |||||
package embedding; | |||||
// Java | |||||
import java.io.BufferedOutputStream; | |||||
import java.io.BufferedReader; | |||||
import java.io.File; | |||||
import java.io.FileOutputStream; | |||||
import java.io.IOException; | |||||
import java.io.OutputStream; | |||||
//JAXP | |||||
import javax.xml.transform.Transformer; | |||||
import javax.xml.transform.TransformerException; | |||||
import javax.xml.transform.TransformerFactory; | |||||
import javax.xml.transform.Source; | |||||
import javax.xml.transform.Result; | |||||
import javax.xml.transform.stream.StreamSource; | |||||
import javax.xml.transform.sax.SAXResult; | |||||
// FOP | |||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.apps.Fop; | |||||
import org.apache.fop.apps.FOPException; | |||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.FormattingResults; | |||||
import org.apache.fop.apps.MimeConstants; | |||||
import org.apache.fop.apps.PageSequenceResults; | |||||
/** | |||||
* This class demonstrates the conversion of multiple FO files to PDF using FOP. | |||||
* The FopFactory is reused. Its configuration is applied to each rendering run. | |||||
* The FOUserAgent and Fop are newly created by the FopFactory for each run. | |||||
* The FOUserAgent can be configured differently for each run. | |||||
*/ | |||||
public class MultipleFO2PDF { | |||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
// JAXP TransformerFactory can be reused, too | |||||
private TransformerFactory factory = TransformerFactory.newInstance(); | |||||
/** | |||||
* Converts an FO file to a PDF file using FOP | |||||
* @param fo the FO file | |||||
* @param pdf the target PDF file | |||||
* @throws TransformerException in case of a transformation problem | |||||
* @throws IOException in case of an I/O problem | |||||
* @throws FOPException in case of a FOP problem | |||||
* @return the formatting results of the run | |||||
*/ | |||||
public FormattingResults convertFO2PDF(File fo, File pdf) | |||||
throws TransformerException, IOException, FOPException { | |||||
OutputStream out = null; | |||||
Fop fop; | |||||
try { | |||||
FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | |||||
// configure foUserAgent as desired | |||||
// Setup output stream. Note: Using BufferedOutputStream | |||||
// for performance reasons (helpful with FileOutputStreams). | |||||
out = new FileOutputStream(pdf); | |||||
out = new BufferedOutputStream(out); | |||||
// Construct fop with desired output format and output stream | |||||
fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); | |||||
// Setup JAXP using identity transformer | |||||
Transformer transformer = factory.newTransformer(); // identity transformer | |||||
// Setup input stream | |||||
Source src = new StreamSource(fo); | |||||
// Resulting SAX events (the generated FO) must be piped through to FOP | |||||
Result res = new SAXResult(fop.getDefaultHandler()); | |||||
// Start XSLT transformation and FOP processing | |||||
transformer.transform(src, res); | |||||
} finally { | |||||
out.close(); | |||||
} | |||||
return fop.getResults(); | |||||
} | |||||
/** | |||||
* Listens on standard in for names of fo files to be transformed to pdf. | |||||
* 'quit' or the null string (for piped input) cause the listener to stop listening. | |||||
*/ | |||||
public void listen() { | |||||
//Setup directories | |||||
File baseDir = new File("."); | |||||
File outDir = new File(baseDir, "out"); | |||||
outDir.mkdirs(); | |||||
BufferedReader in = new BufferedReader(new java.io.InputStreamReader(System.in)); | |||||
while (true) { | |||||
try { | |||||
// Listen for the input file name | |||||
System.out.print("Input XSL-FO file ('quit' to stop): "); | |||||
String foname = in.readLine(); | |||||
if (foname == null) { | |||||
System.out.println("Null input, quitting"); | |||||
return; | |||||
} | |||||
foname.trim(); | |||||
if (foname.equals("quit")) { | |||||
System.out.println("Quitting"); | |||||
return; | |||||
} | |||||
File fofile = new File(baseDir, foname); | |||||
String pdfname = foname; | |||||
pdfname.replaceFirst("\\.fo", ".pdf"); | |||||
File pdffile = new File(outDir, pdfname); | |||||
// transform and render | |||||
System.out.print("Transforming " + fofile + " to PDF file " + pdffile + "..."); | |||||
FormattingResults foResults = convertFO2PDF(fofile, pdffile); | |||||
System.out.println("done!"); | |||||
// Result processing | |||||
java.util.List pageSequences = foResults.getPageSequences(); | |||||
for (java.util.Iterator it = pageSequences.iterator(); it.hasNext();) { | |||||
PageSequenceResults pageSequenceResults = (PageSequenceResults)it.next(); | |||||
System.out.println("PageSequence " | |||||
+ (String.valueOf(pageSequenceResults.getID()).length() > 0 | |||||
? pageSequenceResults.getID() : "<no id>") | |||||
+ " generated " + pageSequenceResults.getPageCount() + " pages."); | |||||
} | |||||
System.out.println("Generated " + foResults.getPageCount() + " pages in total."); | |||||
} catch (Exception e) { | |||||
System.out.println("failure!"); | |||||
e.printStackTrace(System.out); | |||||
} finally { | |||||
System.out.println(""); | |||||
} | |||||
} | |||||
} | |||||
/** | |||||
* Main method. Set up the listener. | |||||
* @param args command-line arguments | |||||
*/ | |||||
public static void main(String[] args) { | |||||
System.out.println("FOP MultipleFO2PDF\n"); | |||||
System.out.println("Preparing..."); | |||||
MultipleFO2PDF m = new MultipleFO2PDF(); | |||||
m.listen(); | |||||
} | |||||
} |
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.area.AreaTreeModel; | import org.apache.fop.area.AreaTreeModel; | ||||
import org.apache.fop.area.AreaTreeParser; | import org.apache.fop.area.AreaTreeParser; | ||||
*/ | */ | ||||
public class ExampleConcat { | public class ExampleConcat { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
/** | /** | ||||
* Creates a sample ProjectTeam instance for this demo. | * Creates a sample ProjectTeam instance for this demo. | ||||
* @return ProjectTeam the newly created ProjectTeam instance | * @return ProjectTeam the newly created ProjectTeam instance | ||||
throws IOException, FOPException, TransformerException { | throws IOException, FOPException, TransformerException { | ||||
//Create a user agent | //Create a user agent | ||||
FOUserAgent userAgent = new FOUserAgent(); | |||||
FOUserAgent userAgent = fopFactory.newFOUserAgent(); | |||||
//Create an instance of the target renderer so the XMLRenderer can use its font setup | //Create an instance of the target renderer so the XMLRenderer can use its font setup | ||||
Renderer targetRenderer = userAgent.getRendererFactory().createRenderer( | Renderer targetRenderer = userAgent.getRendererFactory().createRenderer( | ||||
//Make sure the prepared XMLRenderer is used | //Make sure the prepared XMLRenderer is used | ||||
userAgent.setRendererOverride(xmlRenderer); | userAgent.setRendererOverride(xmlRenderer); | ||||
// Construct fop (the MIME type here is unimportant due to the override on the user agent) | |||||
Fop fop = new Fop(MimeConstants.MIME_FOP_AREA_TREE, userAgent); | |||||
// Setup output | // Setup output | ||||
OutputStream out = new java.io.FileOutputStream(intermediate); | OutputStream out = new java.io.FileOutputStream(intermediate); | ||||
out = new java.io.BufferedOutputStream(out); | out = new java.io.BufferedOutputStream(out); | ||||
try { | try { | ||||
fop.setOutputStream(out); | |||||
// Construct fop (the MIME type here is unimportant due to the override on the user agent) | |||||
Fop fop = new Fop(MimeConstants.MIME_FOP_AREA_TREE, userAgent, out); | |||||
// Setup XSLT | // Setup XSLT | ||||
TransformerFactory factory = TransformerFactory.newInstance(); | TransformerFactory factory = TransformerFactory.newInstance(); | ||||
try { | try { | ||||
//Setup fonts and user agent | //Setup fonts and user agent | ||||
FontInfo fontInfo = new FontInfo(); | FontInfo fontInfo = new FontInfo(); | ||||
FOUserAgent userAgent = new FOUserAgent(); | |||||
FOUserAgent userAgent = fopFactory.newFOUserAgent(); | |||||
//Construct the AreaTreeModel that will received the individual pages | //Construct the AreaTreeModel that will received the individual pages | ||||
AreaTreeModel treeModel = new RenderPagesModel(userAgent, | AreaTreeModel treeModel = new RenderPagesModel(userAgent, |
import javax.xml.transform.stream.StreamSource; | import javax.xml.transform.stream.StreamSource; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.area.AreaTreeModel; | import org.apache.fop.area.AreaTreeModel; | ||||
import org.apache.fop.area.AreaTreeParser; | import org.apache.fop.area.AreaTreeParser; | ||||
*/ | */ | ||||
public class ExampleStamp { | public class ExampleStamp { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
/** | /** | ||||
* Stamps an intermediate file and renders it to a PDF file. | * Stamps an intermediate file and renders it to a PDF file. | ||||
* @param atfile the intermediate file (area tree XML) | * @param atfile the intermediate file (area tree XML) | ||||
try { | try { | ||||
//Setup fonts and user agent | //Setup fonts and user agent | ||||
FontInfo fontInfo = new FontInfo(); | FontInfo fontInfo = new FontInfo(); | ||||
FOUserAgent userAgent = new FOUserAgent(); | |||||
FOUserAgent userAgent = fopFactory.newFOUserAgent(); | |||||
//Construct the AreaTreeModel that will received the individual pages | //Construct the AreaTreeModel that will received the individual pages | ||||
AreaTreeModel treeModel = new RenderPagesModel(userAgent, | AreaTreeModel treeModel = new RenderPagesModel(userAgent, |
// Java | // Java | ||||
import java.io.File; | import java.io.File; | ||||
import java.net.URL; | |||||
import java.net.MalformedURLException; | |||||
import java.util.Date; | import java.util.Date; | ||||
import java.util.List; | |||||
import java.util.Map; | import java.util.Map; | ||||
import javax.xml.transform.Source; | import javax.xml.transform.Source; | ||||
import javax.xml.transform.TransformerException; | import javax.xml.transform.TransformerException; | ||||
// FOP | // FOP | ||||
import org.apache.fop.Version; | import org.apache.fop.Version; | ||||
import org.apache.fop.fo.ElementMapping; | |||||
import org.apache.fop.fo.FOEventHandler; | import org.apache.fop.fo.FOEventHandler; | ||||
import org.apache.fop.hyphenation.HyphenationTreeResolver; | |||||
import org.apache.fop.layoutmgr.LayoutManagerMaker; | |||||
import org.apache.fop.pdf.PDFEncryptionParams; | import org.apache.fop.pdf.PDFEncryptionParams; | ||||
import org.apache.fop.render.Renderer; | import org.apache.fop.render.Renderer; | ||||
import org.apache.fop.render.RendererFactory; | import org.apache.fop.render.RendererFactory; | ||||
import org.apache.fop.render.XMLHandlerRegistry; | import org.apache.fop.render.XMLHandlerRegistry; | ||||
import org.apache.fop.render.pdf.PDFRenderer; | |||||
/** | /** | ||||
* The User Agent for fo. | |||||
* This user agent is used by the processing to obtain user configurable | |||||
* options. | |||||
* This is the user agent for FOP. | |||||
* It is the entity through which you can interact with the XSL-FO processing and is | |||||
* used by the processing to obtain user configurable options. | |||||
* <p> | * <p> | ||||
* Renderer specific extensions (that do not produce normal areas on | * Renderer specific extensions (that do not produce normal areas on | ||||
* the output) will be done like so: | * the output) will be done like so: | ||||
*/ | */ | ||||
public class FOUserAgent { | public class FOUserAgent { | ||||
/** Defines the default source resolution (72dpi) for FOP */ | |||||
public static final float DEFAULT_SOURCE_RESOLUTION = 72.0f; //dpi | |||||
/** Defines the default target resolution (72dpi) for FOP */ | /** Defines the default target resolution (72dpi) for FOP */ | ||||
public static final float DEFAULT_TARGET_RESOLUTION = 72.0f; //dpi | public static final float DEFAULT_TARGET_RESOLUTION = 72.0f; //dpi | ||||
/** Defines the default page-height */ | |||||
public static final String DEFAULT_PAGE_HEIGHT = "11in"; | |||||
/** Defines the default page-width */ | |||||
public static final String DEFAULT_PAGE_WIDTH = "8.26in"; | |||||
/** Factory for Renderers and FOEventHandlers */ | |||||
private RendererFactory rendererFactory = new RendererFactory(); | |||||
/** Registry for XML handlers */ | |||||
private XMLHandlerRegistry xmlHandlers = new XMLHandlerRegistry(); | |||||
/** The resolver for user-supplied hyphenation patterns */ | |||||
private HyphenationTreeResolver hyphResolver; | |||||
private static Log log = LogFactory.getLog("FOP"); | |||||
private FopFactory factory; | |||||
/** The base URL for all URL resolutions, especially for external-graphics */ | /** The base URL for all URL resolutions, especially for external-graphics */ | ||||
private String baseURL; | private String baseURL; | ||||
/** The base URL for all font URL resolutions */ | |||||
private String fontBaseURL; | |||||
/** A user settable URI Resolver */ | /** A user settable URI Resolver */ | ||||
private URIResolver uriResolver = null; | private URIResolver uriResolver = null; | ||||
/** Our default resolver if none is set */ | |||||
private URIResolver foURIResolver = new FOURIResolver(); | |||||
private PDFEncryptionParams pdfEncryptionParams; | |||||
private float sourceResolution = DEFAULT_SOURCE_RESOLUTION; | |||||
private float targetResolution = DEFAULT_TARGET_RESOLUTION; | private float targetResolution = DEFAULT_TARGET_RESOLUTION; | ||||
private String pageHeight = DEFAULT_PAGE_HEIGHT; | |||||
private String pageWidth = DEFAULT_PAGE_WIDTH; | |||||
private Map rendererOptions = new java.util.HashMap(); | private Map rendererOptions = new java.util.HashMap(); | ||||
private File outputFile = null; | private File outputFile = null; | ||||
private Renderer rendererOverride = null; | private Renderer rendererOverride = null; | ||||
private FOEventHandler foEventHandlerOverride = null; | private FOEventHandler foEventHandlerOverride = null; | ||||
private LayoutManagerMaker lmMakerOverride = null; | |||||
/* user configuration */ | |||||
private Configuration userConfig = null; | |||||
private Log log = LogFactory.getLog("FOP"); | |||||
/* FOP has the ability, for some FO's, to continue processing even if the | |||||
* input XSL violates that FO's content model. This is the default | |||||
* behavior for FOP. However, this flag, if set, provides the user the | |||||
* ability for FOP to halt on all content model violations if desired. | |||||
*/ | |||||
private boolean strictValidation = true; | |||||
/** @see #setBreakIndentInheritanceOnReferenceAreaBoundary(boolean) */ | |||||
private boolean breakIndentInheritanceOnReferenceAreaBoundary = false; | |||||
/** Allows enabling kerning on the base 14 fonts, default is false */ | |||||
private boolean enableBase14Kerning = false; | |||||
/* Additional fo.ElementMapping subclasses set by user */ | |||||
private List additionalElementMappings = null; | |||||
/** Producer: Metadata element for the system/software that produces | /** Producer: Metadata element for the system/software that produces | ||||
* the document. (Some renderers can store this in the document.) | * the document. (Some renderers can store this in the document.) | ||||
*/ | */ | ||||
protected String keywords = null; | protected String keywords = null; | ||||
/** | /** | ||||
* Add the element mapping with the given class name. | |||||
* @param elementMapping the class name representing the element mapping. | |||||
* Default constructor | |||||
* @see org.apache.fop.apps.FopFactory | |||||
* @deprecated Provided for compatibility only. Please use the methods from | |||||
* FopFactory to construct FOUserAgent instances! | |||||
*/ | */ | ||||
public void addElementMapping(ElementMapping elementMapping) { | |||||
if (additionalElementMappings == null) { | |||||
additionalElementMappings = new java.util.ArrayList(); | |||||
} | |||||
additionalElementMappings.add(elementMapping); | |||||
public FOUserAgent() { | |||||
this(FopFactory.newInstance()); | |||||
} | } | ||||
/** | /** | ||||
* Returns the List of user-added ElementMapping class names | |||||
* @return List of Strings holding ElementMapping names. | |||||
* Main constructor. <b>This constructor should not be called directly. Please use the | |||||
* methods from FopFactory to construct FOUserAgent instances!</b> | |||||
* @param factory the factory that provides environment-level information | |||||
* @see org.apache.fop.apps.FopFactory | |||||
*/ | */ | ||||
public List getAdditionalElementMappings() { | |||||
return additionalElementMappings; | |||||
public FOUserAgent(FopFactory factory) { | |||||
if (factory == null) { | |||||
throw new NullPointerException("The factory parameter must not be null"); | |||||
} | |||||
this.factory = factory; | |||||
try { | |||||
if (factory.getUserConfig() != null) { | |||||
configure(factory.getUserConfig()); | |||||
} | |||||
} catch (ConfigurationException cfge) { | |||||
log.error("Error while initializing the user asgent: " | |||||
+ cfge.getMessage()); | |||||
} | |||||
} | } | ||||
/** @return the associated FopFactory instance */ | |||||
public FopFactory getFactory() { | |||||
return this.factory; | |||||
} | |||||
// ---------------------------------------------- rendering-run dependent stuff | |||||
/** | /** | ||||
* Sets an explicit renderer to use which overrides the one defined by the | * Sets an explicit renderer to use which overrides the one defined by the | ||||
* render type setting. | * render type setting. | ||||
return this.foEventHandlerOverride; | return this.foEventHandlerOverride; | ||||
} | } | ||||
/** | |||||
* Activates strict XSL content model validation for FOP | |||||
* Default is false (FOP will continue processing where it can) | |||||
* @param validateStrictly true to turn on strict validation | |||||
*/ | |||||
public void setStrictValidation(boolean validateStrictly) { | |||||
this.strictValidation = validateStrictly; | |||||
} | |||||
/** | |||||
* Returns whether FOP is strictly validating input XSL | |||||
* @return true of strict validation turned on, false otherwise | |||||
*/ | |||||
public boolean validateStrictly() { | |||||
return strictValidation; | |||||
} | |||||
/** | |||||
* @return true if the indent inheritance should be broken when crossing reference area | |||||
* boundaries (for more info, see the javadoc for the relative member variable) | |||||
*/ | |||||
public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() { | |||||
return breakIndentInheritanceOnReferenceAreaBoundary; | |||||
} | |||||
/** | |||||
* Controls whether to enable a feature that breaks indent inheritance when crossing | |||||
* reference area boundaries. | |||||
* <p> | |||||
* This flag controls whether FOP will enable special code that breaks property | |||||
* inheritance for start-indent and end-indent when the evaluation of the inherited | |||||
* value would cross a reference area. This is described under | |||||
* http://wiki.apache.org/xmlgraphics-fop/IndentInheritance as is intended to | |||||
* improve interoperability with commercial FO implementations and to produce | |||||
* results that are more in line with the expectation of unexperienced FO users. | |||||
* Note: Enabling this features violates the XSL specification! | |||||
* @param value true to enable the feature | |||||
*/ | |||||
public void setBreakIndentInheritanceOnReferenceAreaBoundary(boolean value) { | |||||
this.breakIndentInheritanceOnReferenceAreaBoundary = value; | |||||
} | |||||
/** @return true if kerning on base 14 fonts is enabled */ | |||||
public boolean isBase14KerningEnabled() { | |||||
return this.enableBase14Kerning; | |||||
} | |||||
/** | |||||
* Controls whether kerning is activated on base 14 fonts. | |||||
* @param value true if kerning should be activated | |||||
*/ | |||||
public void setBase14KerningEnabled(boolean value) { | |||||
this.enableBase14Kerning = value; | |||||
} | |||||
/** | |||||
* Sets an explicit LayoutManagerMaker instance which overrides the one | |||||
* defined by the AreaTreeHandler. | |||||
* @param lmMaker the LayoutManagerMaker instance | |||||
*/ | |||||
public void setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker) { | |||||
this.lmMakerOverride = lmMaker; | |||||
} | |||||
/** | |||||
* Returns the overriding LayoutManagerMaker instance, if any. | |||||
* @return the overriding LayoutManagerMaker or null | |||||
*/ | |||||
public LayoutManagerMaker getLayoutManagerMakerOverride() { | |||||
return this.lmMakerOverride; | |||||
} | |||||
/** | /** | ||||
* Sets the producer of the document. | * Sets the producer of the document. | ||||
* @param producer source of document | * @param producer source of document | ||||
} | } | ||||
/** | /** | ||||
* Set the user configuration. | |||||
* @param userConfig configuration | |||||
*/ | |||||
public void setUserConfig(Configuration userConfig) { | |||||
this.userConfig = userConfig; | |||||
try { | |||||
initUserConfig(); | |||||
} catch (ConfigurationException cfge) { | |||||
log.error("Error initializing User Agent configuration: " | |||||
+ cfge.getMessage()); | |||||
} | |||||
} | |||||
/** | |||||
* Get the user configuration. | |||||
* @return the user configuration | |||||
* Configures the FOUserAgent through the factory's configuration. | |||||
* @see org.apache.avalon.framework.configuration.Configurable | |||||
*/ | */ | ||||
public Configuration getUserConfig() { | |||||
return userConfig; | |||||
} | |||||
/** | |||||
* Initializes user agent settings from the user configuration | |||||
* file, if present: baseURL, resolution, default page size,... | |||||
* | |||||
* @throws ConfigurationException when there is an entry that | |||||
* misses the required attribute | |||||
*/ | |||||
public void initUserConfig() throws ConfigurationException { | |||||
log.debug("Initializing User Agent Configuration"); | |||||
setBaseURL(getBaseURLfromConfig("base")); | |||||
setFontBaseURL(getBaseURLfromConfig("font-base")); | |||||
final String hyphBase = getBaseURLfromConfig("hyphenation-base"); | |||||
if (hyphBase != null) { | |||||
this.hyphResolver = new HyphenationTreeResolver() { | |||||
public Source resolve(String href) { | |||||
return resolveURI(href, hyphBase); | |||||
} | |||||
}; | |||||
} | |||||
if (userConfig.getChild("source-resolution", false) != null) { | |||||
this.sourceResolution | |||||
= userConfig.getChild("source-resolution").getValueAsFloat( | |||||
DEFAULT_SOURCE_RESOLUTION); | |||||
log.info("Source resolution set to: " + sourceResolution | |||||
+ "dpi (px2mm=" + getSourcePixelUnitToMillimeter() + ")"); | |||||
} | |||||
if (userConfig.getChild("target-resolution", false) != null) { | |||||
protected void configure(Configuration cfg) throws ConfigurationException { | |||||
setBaseURL(FopFactory.getBaseURLfromConfig(cfg, "base")); | |||||
if (cfg.getChild("target-resolution", false) != null) { | |||||
this.targetResolution | this.targetResolution | ||||
= userConfig.getChild("target-resolution").getValueAsFloat( | |||||
= cfg.getChild("target-resolution").getValueAsFloat( | |||||
DEFAULT_TARGET_RESOLUTION); | DEFAULT_TARGET_RESOLUTION); | ||||
log.info("Target resolution set to: " + targetResolution | log.info("Target resolution set to: " + targetResolution | ||||
+ "dpi (px2mm=" + getTargetPixelUnitToMillimeter() + ")"); | + "dpi (px2mm=" + getTargetPixelUnitToMillimeter() + ")"); | ||||
} | } | ||||
if (userConfig.getChild("strict-validation", false) != null) { | |||||
this.strictValidation = userConfig.getChild("strict-validation").getValueAsBoolean(); | |||||
} | |||||
if (userConfig.getChild("break-indent-inheritance", false) != null) { | |||||
this.breakIndentInheritanceOnReferenceAreaBoundary | |||||
= userConfig.getChild("break-indent-inheritance").getValueAsBoolean(); | |||||
} | |||||
Configuration pageConfig = userConfig.getChild("default-page-settings"); | |||||
if (pageConfig.getAttribute("height", null) != null) { | |||||
setPageHeight(pageConfig.getAttribute("height")); | |||||
log.info("Default page-height set to: " + pageHeight); | |||||
} | |||||
if (pageConfig.getAttribute("width", null) != null) { | |||||
setPageWidth(pageConfig.getAttribute("width")); | |||||
log.info("Default page-width set to: " + pageWidth); | |||||
} | |||||
} | |||||
private String getBaseURLfromConfig(String name) { | |||||
if (userConfig.getChild(name, false) != null) { | |||||
try { | |||||
String cfgBaseDir = userConfig.getChild(name).getValue(null); | |||||
if (cfgBaseDir != null) { | |||||
File dir = new File(cfgBaseDir); | |||||
if (dir.isDirectory()) { | |||||
cfgBaseDir = dir.toURL().toExternalForm(); | |||||
} | |||||
} | |||||
log.info(name + " set to: " + cfgBaseDir); | |||||
return cfgBaseDir; | |||||
} catch (MalformedURLException mue) { | |||||
log.error("Base URL in user config is malformed!"); | |||||
} | |||||
} | |||||
return null; | |||||
} | } | ||||
/** | /** | ||||
* @param mimeType MIME type of the renderer | * @param mimeType MIME type of the renderer | ||||
* @return the requested configuration subtree, null if there's no configuration | * @return the requested configuration subtree, null if there's no configuration | ||||
*/ | */ | ||||
public Configuration getUserRendererConfig (String mimeType) { | |||||
public Configuration getUserRendererConfig(String mimeType) { | |||||
if (userConfig == null || mimeType == null) { | |||||
Configuration cfg = getFactory().getUserConfig(); | |||||
if (cfg == null || mimeType == null) { | |||||
return null; | return null; | ||||
} | } | ||||
Configuration userRendererConfig = null; | Configuration userRendererConfig = null; | ||||
Configuration[] cfgs | Configuration[] cfgs | ||||
= userConfig.getChild("renderers").getChildren("renderer"); | |||||
= cfg.getChild("renderers").getChildren("renderer"); | |||||
for (int i = 0; i < cfgs.length; ++i) { | for (int i = 0; i < cfgs.length; ++i) { | ||||
Configuration cfg = cfgs[i]; | |||||
Configuration child = cfgs[i]; | |||||
try { | try { | ||||
if (cfg.getAttribute("mime").equals(mimeType)) { | |||||
userRendererConfig = cfg; | |||||
if (child.getAttribute("mime").equals(mimeType)) { | |||||
userRendererConfig = child; | |||||
break; | break; | ||||
} | } | ||||
} catch (ConfigurationException e) { | } catch (ConfigurationException e) { | ||||
return this.baseURL; | return this.baseURL; | ||||
} | } | ||||
/** | |||||
* Sets the font base URL. | |||||
* @param fontBaseURL font base URL | |||||
*/ | |||||
public void setFontBaseURL(String fontBaseURL) { | |||||
this.fontBaseURL = fontBaseURL; | |||||
} | |||||
/** | |||||
* Returns the font base URL. | |||||
* @return the font base URL | |||||
*/ | |||||
public String getFontBaseURL() { | |||||
return this.fontBaseURL != null ? this.fontBaseURL : this.baseURL; | |||||
} | |||||
/** | /** | ||||
* Sets the URI Resolver. | * Sets the URI Resolver. | ||||
* @param uriResolver the new URI resolver | |||||
* @param resolver the new URI resolver | |||||
*/ | */ | ||||
public void setURIResolver(URIResolver uriResolver) { | |||||
this.uriResolver = uriResolver; | |||||
public void setURIResolver(URIResolver resolver) { | |||||
this.uriResolver = resolver; | |||||
} | } | ||||
/** | /** | ||||
/** | /** | ||||
* Returns the parameters for PDF encryption. | * Returns the parameters for PDF encryption. | ||||
* @return the PDF encryption parameters, null if not applicable | * @return the PDF encryption parameters, null if not applicable | ||||
* @deprecated Use (PDFEncryptionParams)getRendererOptions().get("encryption-params") | |||||
* instead. | |||||
*/ | */ | ||||
public PDFEncryptionParams getPDFEncryptionParams() { | public PDFEncryptionParams getPDFEncryptionParams() { | ||||
return pdfEncryptionParams; | |||||
return (PDFEncryptionParams)getRendererOptions().get(PDFRenderer.ENCRYPTION_PARAMS); | |||||
} | } | ||||
/** | /** | ||||
* Sets the parameters for PDF encryption. | * Sets the parameters for PDF encryption. | ||||
* @param pdfEncryptionParams the PDF encryption parameters, null to | * @param pdfEncryptionParams the PDF encryption parameters, null to | ||||
* disable PDF encryption | * disable PDF encryption | ||||
* @deprecated Use getRendererOptions().put("encryption-params", | |||||
* new PDFEncryptionParams(..)) instead or set every parameter separately: | |||||
* getRendererOptions().put("noprint", Boolean.TRUE). | |||||
*/ | */ | ||||
public void setPDFEncryptionParams(PDFEncryptionParams pdfEncryptionParams) { | public void setPDFEncryptionParams(PDFEncryptionParams pdfEncryptionParams) { | ||||
this.pdfEncryptionParams = pdfEncryptionParams; | |||||
getRendererOptions().put(PDFRenderer.ENCRYPTION_PARAMS, pdfEncryptionParams); | |||||
} | } | ||||
* Will use the configured resolver and if not successful fall back | * Will use the configured resolver and if not successful fall back | ||||
* to the default resolver. | * to the default resolver. | ||||
* @param uri URI to access | * @param uri URI to access | ||||
* @param baseURL the base url to resolve against | |||||
* @param base the base URI to resolve against | |||||
* @return A {@link javax.xml.transform.Source} object, or null if the URI | * @return A {@link javax.xml.transform.Source} object, or null if the URI | ||||
* cannot be resolved. | * cannot be resolved. | ||||
* @see org.apache.fop.apps.FOURIResolver | * @see org.apache.fop.apps.FOURIResolver | ||||
*/ | */ | ||||
public Source resolveURI(String uri, String baseURL) { | |||||
public Source resolveURI(String uri, String base) { | |||||
Source source = null; | Source source = null; | ||||
if (uriResolver != null) { | if (uriResolver != null) { | ||||
try { | try { | ||||
source = uriResolver.resolve(uri, baseURL); | |||||
source = uriResolver.resolve(uri, base); | |||||
} catch (TransformerException te) { | } catch (TransformerException te) { | ||||
log.error("Attempt to resolve URI '" + uri + "' failed: ", te); | log.error("Attempt to resolve URI '" + uri + "' failed: ", te); | ||||
} | } | ||||
} | } | ||||
if (source == null) { | if (source == null) { | ||||
// URI Resolver not configured or returned null, use default resolver | |||||
try { | |||||
source = foURIResolver.resolve(uri, baseURL); | |||||
} catch (TransformerException te) { | |||||
log.error("Attempt to resolve URI '" + uri + "' failed: ", te); | |||||
} | |||||
// URI Resolver not configured or returned null, use default resolver from the factory | |||||
source = getFactory().resolveURI(uri, base); | |||||
} | } | ||||
return source; | return source; | ||||
} | } | ||||
return outputFile; | return outputFile; | ||||
} | } | ||||
/** | |||||
* Returns the conversion factor from pixel units to millimeters. This | |||||
* depends on the desired source resolution. | |||||
* @return float conversion factor | |||||
* @see getSourceResolution() | |||||
*/ | |||||
public float getSourcePixelUnitToMillimeter() { | |||||
return 25.4f / this.sourceResolution; | |||||
} | |||||
/** | /** | ||||
* Returns the conversion factor from pixel units to millimeters. This | * Returns the conversion factor from pixel units to millimeters. This | ||||
* depends on the desired target resolution. | * depends on the desired target resolution. | ||||
* @return float conversion factor | * @return float conversion factor | ||||
* @see getTargetResolution() | |||||
* @see #getTargetResolution() | |||||
*/ | */ | ||||
public float getTargetPixelUnitToMillimeter() { | public float getTargetPixelUnitToMillimeter() { | ||||
return 25.4f / this.targetResolution; | return 25.4f / this.targetResolution; | ||||
} | } | ||||
/** @return the resolution for resolution-dependant input */ | |||||
public float getSourceResolution() { | |||||
return this.sourceResolution; | |||||
} | |||||
/** @return the resolution for resolution-dependant output */ | /** @return the resolution for resolution-dependant output */ | ||||
public float getTargetResolution() { | public float getTargetResolution() { | ||||
return this.targetResolution; | return this.targetResolution; | ||||
} | } | ||||
/** | |||||
* Sets the source resolution in dpi. This value is used to interpret the pixel size | |||||
* of source documents like SVG images and bitmap images without resolution information. | |||||
* @param dpi resolution in dpi | |||||
*/ | |||||
public void setSourceResolution(int dpi) { | |||||
this.sourceResolution = dpi; | |||||
} | |||||
/** | /** | ||||
* Sets the target resolution in dpi. This value defines the target resolution of | * Sets the target resolution in dpi. This value defines the target resolution of | ||||
* bitmap images generated by the bitmap renderers (such as the TIFF renderer) and of | * bitmap images generated by the bitmap renderers (such as the TIFF renderer) and of | ||||
this.targetResolution = dpi; | this.targetResolution = dpi; | ||||
} | } | ||||
// ---------------------------------------------- environment-level stuff | |||||
// (convenience access to FopFactory methods) | |||||
/** @return the font base URL */ | |||||
public String getFontBaseURL() { | |||||
String fontBaseURL = getFactory().getFontBaseURL(); | |||||
return fontBaseURL != null ? fontBaseURL : this.baseURL; | |||||
} | |||||
/** | /** | ||||
* Gets the default page-height to use as fallback, | |||||
* in case page-height="auto" | |||||
* | |||||
* @return the page-height, as a String | |||||
* Returns the conversion factor from pixel units to millimeters. This | |||||
* depends on the desired source resolution. | |||||
* @return float conversion factor | |||||
* @see #getSourceResolution() | |||||
*/ | */ | ||||
public String getPageHeight() { | |||||
return this.pageHeight; | |||||
public float getSourcePixelUnitToMillimeter() { | |||||
return getFactory().getSourcePixelUnitToMillimeter(); | |||||
} | } | ||||
/** @return the resolution for resolution-dependant input */ | |||||
public float getSourceResolution() { | |||||
return getFactory().getSourceResolution(); | |||||
} | |||||
/** | /** | ||||
* Sets the page-height to use as fallback, in case | |||||
* page-height="auto" | |||||
* Gets the default page-height to use as fallback, | |||||
* in case page-height="auto" | |||||
* | * | ||||
* @param pageHeight page-height as a String | |||||
* @return the page-height, as a String | |||||
* @see FopFactory#getPageHeight() | |||||
*/ | */ | ||||
public void setPageHeight(String pageHeight) { | |||||
this.pageHeight = pageHeight; | |||||
public String getPageHeight() { | |||||
return getFactory().getPageHeight(); | |||||
} | } | ||||
/** | /** | ||||
* in case page-width="auto" | * in case page-width="auto" | ||||
* | * | ||||
* @return the page-width, as a String | * @return the page-width, as a String | ||||
* @see FopFactory#getPageWidth() | |||||
*/ | */ | ||||
public String getPageWidth() { | public String getPageWidth() { | ||||
return this.pageWidth; | |||||
return getFactory().getPageWidth(); | |||||
} | } | ||||
/** | /** | ||||
* Sets the page-width to use as fallback, in case | |||||
* page-width="auto" | |||||
* | |||||
* @param pageWidth page-width as a String | |||||
* Returns whether FOP is strictly validating input XSL | |||||
* @return true of strict validation turned on, false otherwise | |||||
* @see FopFactory#validateStrictly() | |||||
*/ | */ | ||||
public void setPageWidth(String pageWidth) { | |||||
this.pageWidth = pageWidth; | |||||
public boolean validateStrictly() { | |||||
return getFactory().validateStrictly(); | |||||
} | } | ||||
/** | /** | ||||
* If to create hot links to footnotes and before floats. | |||||
* @return True if hot links should be created | |||||
* @return true if the indent inheritance should be broken when crossing reference area | |||||
* boundaries (for more info, see the javadoc for the relative member variable) | |||||
* @see FopFactory#isBreakIndentInheritanceOnReferenceAreaBoundary() | |||||
*/ | */ | ||||
/* TODO This method is never referenced! | |||||
public boolean linkToFootnotes() { | |||||
return true; | |||||
}*/ | |||||
public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() { | |||||
return getFactory().isBreakIndentInheritanceOnReferenceAreaBoundary(); | |||||
} | |||||
/** | /** | ||||
* @return the RendererFactory | * @return the RendererFactory | ||||
*/ | */ | ||||
public RendererFactory getRendererFactory() { | public RendererFactory getRendererFactory() { | ||||
return this.rendererFactory; | |||||
return getFactory().getRendererFactory(); | |||||
} | } | ||||
/** | /** | ||||
* @return the XML handler registry | * @return the XML handler registry | ||||
*/ | */ | ||||
public XMLHandlerRegistry getXMLHandlerRegistry() { | public XMLHandlerRegistry getXMLHandlerRegistry() { | ||||
return this.xmlHandlers; | |||||
return getFactory().getXMLHandlerRegistry(); | |||||
} | } | ||||
/** @return the HyphenationTreeResolver for resolving user-supplied hyphenation patterns. */ | |||||
public HyphenationTreeResolver getHyphenationTreeResolver() { | |||||
return this.hyphResolver; | |||||
} | |||||
} | } | ||||
/* | /* | ||||
* Copyright 1999-2005 The Apache Software Foundation. | |||||
* Copyright 1999-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import org.xml.sax.helpers.DefaultHandler; | import org.xml.sax.helpers.DefaultHandler; | ||||
// FOP | // FOP | ||||
import org.apache.fop.fo.Constants; | |||||
import org.apache.fop.fo.FOTreeBuilder; | import org.apache.fop.fo.FOTreeBuilder; | ||||
/** | /** | ||||
* At the moment, it is recommended not to reuse an instance of this | * At the moment, it is recommended not to reuse an instance of this | ||||
* class for more than one rendering run. | * class for more than one rendering run. | ||||
*/ | */ | ||||
public class Fop implements Constants { | |||||
public class Fop { | |||||
// desired output format: MIME type such as "application/pdf", "application/postscript" etc. | // desired output format: MIME type such as "application/pdf", "application/postscript" etc. | ||||
private String outputFormat = null; | private String outputFormat = null; | ||||
* output format (ex. "application/pdf" for PDF). | * output format (ex. "application/pdf" for PDF). | ||||
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf"). | * @param outputFormat the MIME type of the output format to use (ex. "application/pdf"). | ||||
* @param ua FOUserAgent object | * @param ua FOUserAgent object | ||||
* @param stream the output stream | |||||
* @throws FOPException if setting up the DefaultHandler fails | |||||
* @deprecated End-users should use {@link FopFactory#newFop(String, FOUserAgent, OutputStream)} | |||||
* instead! This constructor will become invisible with FOP 1.0. | |||||
*/ | |||||
public Fop(String outputFormat, FOUserAgent ua, OutputStream stream) throws FOPException { | |||||
this.outputFormat = outputFormat; | |||||
foUserAgent = ua; | |||||
if (foUserAgent == null) { | |||||
foUserAgent = FopFactory.newInstance().newFOUserAgent(); | |||||
} | |||||
this.stream = stream; | |||||
createDefaultHandler(); | |||||
} | |||||
/** | |||||
* Constructor for use with already-created FOUserAgents. It uses MIME types to select the | |||||
* output format (ex. "application/pdf" for PDF). | |||||
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf"). | |||||
* @param ua FOUserAgent object | |||||
* @throws FOPException if setting up the DefaultHandler fails | |||||
* @deprecated End-users should use {@link FopFactory#newFop(String, FOUserAgent)} instead! | |||||
* This constructor will become invisible with FOP 1.0. | |||||
*/ | */ | ||||
public Fop(String outputFormat, FOUserAgent ua) { | |||||
public Fop(String outputFormat, FOUserAgent ua) throws FOPException { | |||||
this.outputFormat = outputFormat; | this.outputFormat = outputFormat; | ||||
foUserAgent = ua; | foUserAgent = ua; | ||||
if (foUserAgent == null) { | if (foUserAgent == null) { | ||||
foUserAgent = new FOUserAgent(); | |||||
foUserAgent = FopFactory.newInstance().newFOUserAgent(); | |||||
} | } | ||||
createDefaultHandler(); | |||||
} | } | ||||
/** | /** | ||||
* Constructor for FOP with a default FOUserAgent. It uses MIME types to select the | * Constructor for FOP with a default FOUserAgent. It uses MIME types to select the | ||||
* output format (ex. "application/pdf" for PDF). | * output format (ex. "application/pdf" for PDF). | ||||
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf"). | * @param outputFormat the MIME type of the output format to use (ex. "application/pdf"). | ||||
* @deprecated End-users should use {@link FopFactory#newFop(String)} instead! | |||||
* This constructor will become invisible with FOP 1.0. | |||||
*/ | */ | ||||
public Fop(String outputFormat) { | public Fop(String outputFormat) { | ||||
this(outputFormat, null); | |||||
this.outputFormat = outputFormat; | |||||
foUserAgent = FopFactory.newInstance().newFOUserAgent(); | |||||
} | } | ||||
/** | /** | ||||
* Set the OutputStream to use to output the result of the Render | * Set the OutputStream to use to output the result of the Render | ||||
* (if applicable) | * (if applicable) | ||||
* @param stream the stream to output the result of rendering to | * @param stream the stream to output the result of rendering to | ||||
* @deprecated Use one of the factory methods on {@link FopFactory} with an OutputStream | |||||
* parameter instead. This method will be removed with FOP 1.0. | |||||
*/ | */ | ||||
public void setOutputStream(OutputStream stream) { | public void setOutputStream(OutputStream stream) { | ||||
this.stream = stream; | this.stream = stream; | ||||
} | } | ||||
/** | /** | ||||
* Returns a DefaultHandler object used to generate the document. | |||||
* Creates a DefaultHandler object used to generate the document. | |||||
* Note this object implements the ContentHandler interface. | * Note this object implements the ContentHandler interface. | ||||
* For processing with a Transformer object, this DefaultHandler object | * For processing with a Transformer object, this DefaultHandler object | ||||
* can be used in the SAXResult constructor. | * can be used in the SAXResult constructor. | ||||
* Alternatively, for processing with a SAXParser, this object can be | * Alternatively, for processing with a SAXParser, this object can be | ||||
* used as the DefaultHandler argument to its parse() methods. | * used as the DefaultHandler argument to its parse() methods. | ||||
* | * | ||||
* @return a SAX DefaultHandler for handling the SAX events. | |||||
* @throws FOPException if setting up the DefaultHandler fails | |||||
*/ | |||||
private void createDefaultHandler() throws FOPException { | |||||
this.foTreeBuilder = new FOTreeBuilder(outputFormat, foUserAgent, stream); | |||||
} | |||||
/** | |||||
* Returns the DefaultHandler object used to generate the document. | |||||
* Checking for null and the exception is only for the deprecated constructor. | |||||
* @return the SAX DefaultHandler for handling the SAX events. | |||||
* @throws FOPException if setting up the DefaultHandler fails | * @throws FOPException if setting up the DefaultHandler fails | ||||
*/ | */ | ||||
public DefaultHandler getDefaultHandler() throws FOPException { | public DefaultHandler getDefaultHandler() throws FOPException { | ||||
if (foTreeBuilder == null) { | if (foTreeBuilder == null) { | ||||
this.foTreeBuilder = new FOTreeBuilder(outputFormat, foUserAgent, stream); | |||||
createDefaultHandler(); | |||||
} | } | ||||
return this.foTreeBuilder; | return this.foTreeBuilder; | ||||
} | } | ||||
* Get the version of FOP | * Get the version of FOP | ||||
* @return the version string | * @return the version string | ||||
* @deprecated Use {@link org.apache.fop.Version#getVersion()} instead! | * @deprecated Use {@link org.apache.fop.Version#getVersion()} instead! | ||||
* This method will be removed with FOP 1.0. | |||||
*/ | */ | ||||
public static String getVersion() { | public static String getVersion() { | ||||
return org.apache.fop.Version.getVersion(); | return org.apache.fop.Version.getVersion(); |
/* | |||||
* Copyright 2006 The Apache Software Foundation. | |||||
* | |||||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||||
* you may not use this file except in compliance with the License. | |||||
* You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
*/ | |||||
/* $Id$ */ | |||||
package org.apache.fop.apps; | |||||
import java.io.File; | |||||
import java.io.IOException; | |||||
import java.io.OutputStream; | |||||
import java.net.MalformedURLException; | |||||
import java.util.List; | |||||
import javax.xml.transform.Source; | |||||
import javax.xml.transform.TransformerException; | |||||
import javax.xml.transform.URIResolver; | |||||
import org.apache.avalon.framework.configuration.Configuration; | |||||
import org.apache.avalon.framework.configuration.ConfigurationException; | |||||
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; | |||||
import org.apache.commons.logging.Log; | |||||
import org.apache.commons.logging.LogFactory; | |||||
import org.apache.fop.fo.ElementMapping; | |||||
import org.apache.fop.fo.ElementMappingRegistry; | |||||
import org.apache.fop.hyphenation.HyphenationTreeResolver; | |||||
import org.apache.fop.image.ImageFactory; | |||||
import org.apache.fop.layoutmgr.LayoutManagerMaker; | |||||
import org.apache.fop.render.RendererFactory; | |||||
import org.apache.fop.render.XMLHandlerRegistry; | |||||
import org.apache.fop.util.ContentHandlerFactoryRegistry; | |||||
import org.xml.sax.SAXException; | |||||
/** | |||||
* Factory class which instantiates new Fop and FOUserAgent instances. This class also holds | |||||
* environmental information and configuration used by FOP. Information that may potentially be | |||||
* different for each rendering run can be found and managed in the FOUserAgent. | |||||
*/ | |||||
public class FopFactory { | |||||
/** Defines the default source resolution (72dpi) for FOP */ | |||||
private static final float DEFAULT_SOURCE_RESOLUTION = 72.0f; //dpi | |||||
/** Defines the default page-height */ | |||||
private static final String DEFAULT_PAGE_HEIGHT = "11in"; | |||||
/** Defines the default page-width */ | |||||
private static final String DEFAULT_PAGE_WIDTH = "8.26in"; | |||||
/** logger instance */ | |||||
private static Log log = LogFactory.getLog(FopFactory.class); | |||||
/** Factory for Renderers and FOEventHandlers */ | |||||
private RendererFactory rendererFactory = new RendererFactory(); | |||||
/** Registry for XML handlers */ | |||||
private XMLHandlerRegistry xmlHandlers = new XMLHandlerRegistry(); | |||||
/** The registry for ElementMapping instances */ | |||||
private ElementMappingRegistry elementMappingRegistry; | |||||
/** The registry for ContentHandlerFactory instance */ | |||||
private ContentHandlerFactoryRegistry contentHandlerFactoryRegistry | |||||
= new ContentHandlerFactoryRegistry(); | |||||
/** Our default resolver if none is set */ | |||||
private URIResolver foURIResolver = new FOURIResolver(); | |||||
/** A user settable URI Resolver */ | |||||
private URIResolver uriResolver = null; | |||||
/** The resolver for user-supplied hyphenation patterns */ | |||||
private HyphenationTreeResolver hyphResolver; | |||||
private ImageFactory imageFactory = new ImageFactory(); | |||||
/** user configuration */ | |||||
private Configuration userConfig = null; | |||||
/** The base URL for all font URL resolutions */ | |||||
private String fontBaseURL; | |||||
/** | |||||
* FOP has the ability, for some FO's, to continue processing even if the | |||||
* input XSL violates that FO's content model. This is the default | |||||
* behavior for FOP. However, this flag, if set, provides the user the | |||||
* ability for FOP to halt on all content model violations if desired. | |||||
*/ | |||||
private boolean strictValidation = true; | |||||
/** Allows enabling kerning on the base 14 fonts, default is false */ | |||||
private boolean enableBase14Kerning = false; | |||||
/** Source resolution in dpi */ | |||||
private float sourceResolution = DEFAULT_SOURCE_RESOLUTION; | |||||
private String pageHeight = DEFAULT_PAGE_HEIGHT; | |||||
private String pageWidth = DEFAULT_PAGE_WIDTH; | |||||
/** @see #setBreakIndentInheritanceOnReferenceAreaBoundary(boolean) */ | |||||
private boolean breakIndentInheritanceOnReferenceAreaBoundary = false; | |||||
/** Additional fo.ElementMapping subclasses set by user */ | |||||
private List additionalElementMappings = null; | |||||
/** Optional overriding LayoutManagerMaker */ | |||||
private LayoutManagerMaker lmMakerOverride = null; | |||||
/** | |||||
* Main constructor. | |||||
*/ | |||||
protected FopFactory() { | |||||
this.elementMappingRegistry = new ElementMappingRegistry(this); | |||||
} | |||||
/** | |||||
* Returns a new FopFactory instance. | |||||
* @return the requested FopFactory instance. | |||||
*/ | |||||
public static FopFactory newInstance() { | |||||
return new FopFactory(); | |||||
} | |||||
/** | |||||
* Returns a new FOUserAgent instance. Use the FOUserAgent to configure special values that | |||||
* are particular to a rendering run. Don't reuse instances over multiple rendering runs but | |||||
* instead create a new one each time and reuse the FopFactory. | |||||
* @return the newly created FOUserAgent instance initialized with default values | |||||
*/ | |||||
public FOUserAgent newFOUserAgent() { | |||||
FOUserAgent userAgent = new FOUserAgent(this); | |||||
return userAgent; | |||||
} | |||||
/** | |||||
* Returns a new {@link Fop} instance. FOP will be configured with a default user agent | |||||
* instance. | |||||
* <p> | |||||
* MIME types are used to select the output format (ex. "application/pdf" for PDF). You can | |||||
* use the constants defined in {@link MimeConstants}. | |||||
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf"). | |||||
* @return the new Fop instance | |||||
* @throws FOPException when the constructor fails | |||||
*/ | |||||
public Fop newFop(String outputFormat) throws FOPException { | |||||
return new Fop(outputFormat, newFOUserAgent()); | |||||
} | |||||
/** | |||||
* Returns a new {@link Fop} instance. Use this factory method if you want to configure this | |||||
* very rendering run, i.e. if you want to set some metadata like the title and author of the | |||||
* document you want to render. In that case, create a new {@link FOUserAgent} | |||||
* instance using {@link #newFOUserAgent()}. | |||||
* <p> | |||||
* MIME types are used to select the output format (ex. "application/pdf" for PDF). You can | |||||
* use the constants defined in {@link MimeConstants}. | |||||
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf"). | |||||
* @param userAgent the user agent that will be used to control the rendering run | |||||
* @return the new Fop instance | |||||
* @throws FOPException when the constructor fails | |||||
*/ | |||||
public Fop newFop(String outputFormat, FOUserAgent userAgent) throws FOPException { | |||||
if (userAgent == null) { | |||||
throw new NullPointerException("The userAgent parameter must not be null!"); | |||||
} | |||||
return new Fop(outputFormat, userAgent); | |||||
} | |||||
/** | |||||
* Returns a new {@link Fop} instance. FOP will be configured with a default user agent | |||||
* instance. Use this factory method if your output type requires an output stream. | |||||
* <p> | |||||
* MIME types are used to select the output format (ex. "application/pdf" for PDF). You can | |||||
* use the constants defined in {@link MimeConstants}. | |||||
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf"). | |||||
* @param stream the output stream | |||||
* @return the new Fop instance | |||||
* @throws FOPException when the constructor fails | |||||
*/ | |||||
public Fop newFop(String outputFormat, OutputStream stream) throws FOPException { | |||||
return new Fop(outputFormat, newFOUserAgent(), stream); | |||||
} | |||||
/** | |||||
* Returns a new {@link Fop} instance. Use this factory method if your output type | |||||
* requires an output stream and you want to configure this very rendering run, | |||||
* i.e. if you want to set some metadata like the title and author of the document | |||||
* you want to render. In that case, create a new {@link FOUserAgent} instance | |||||
* using {@link #newFOUserAgent()}. | |||||
* <p> | |||||
* MIME types are used to select the output format (ex. "application/pdf" for PDF). You can | |||||
* use the constants defined in {@link MimeConstants}. | |||||
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf"). | |||||
* @param userAgent the user agent that will be used to control the rendering run | |||||
* @param stream the output stream | |||||
* @return the new Fop instance | |||||
* @throws FOPException when the constructor fails | |||||
*/ | |||||
public Fop newFop(String outputFormat, FOUserAgent userAgent, OutputStream stream) | |||||
throws FOPException { | |||||
if (userAgent == null) { | |||||
throw new NullPointerException("The userAgent parameter must not be null!"); | |||||
} | |||||
return new Fop(outputFormat, userAgent, stream); | |||||
} | |||||
/** | |||||
* Returns a new {@link Fop} instance. Use this factory method if you want to supply your | |||||
* own {@link org.apache.fop.render.Renderer Renderer} or | |||||
* {@link org.apache.fop.fo.FOEventHandler FOEventHandler} | |||||
* instance instead of the default ones created internally by FOP. | |||||
* @param userAgent the user agent that will be used to control the rendering run | |||||
* @return the new Fop instance | |||||
* @throws FOPException when the constructor fails | |||||
*/ | |||||
public Fop newFop(FOUserAgent userAgent) throws FOPException { | |||||
if (userAgent.getRendererOverride() == null | |||||
&& userAgent.getFOEventHandlerOverride() == null) { | |||||
throw new IllegalStateException("Either the overriding renderer or the overriding" | |||||
+ " FOEventHandler must be set when this factory method is used!"); | |||||
} | |||||
return newFop(null, userAgent); | |||||
} | |||||
/** @return the RendererFactory */ | |||||
public RendererFactory getRendererFactory() { | |||||
return this.rendererFactory; | |||||
} | |||||
/** @return the XML handler registry */ | |||||
public XMLHandlerRegistry getXMLHandlerRegistry() { | |||||
return this.xmlHandlers; | |||||
} | |||||
/** @return the element mapping registry */ | |||||
public ElementMappingRegistry getElementMappingRegistry() { | |||||
return this.elementMappingRegistry; | |||||
} | |||||
/** @return the content handler factory registry */ | |||||
public ContentHandlerFactoryRegistry getContentHandlerFactoryRegistry() { | |||||
return this.contentHandlerFactoryRegistry; | |||||
} | |||||
/** @return the image factory */ | |||||
public ImageFactory getImageFactory() { | |||||
return this.imageFactory; | |||||
} | |||||
/** | |||||
* Add the element mapping with the given class name. | |||||
* @param elementMapping the class name representing the element mapping. | |||||
*/ | |||||
public void addElementMapping(ElementMapping elementMapping) { | |||||
if (additionalElementMappings == null) { | |||||
additionalElementMappings = new java.util.ArrayList(); | |||||
} | |||||
additionalElementMappings.add(elementMapping); | |||||
} | |||||
/** | |||||
* Returns the List of user-added ElementMapping class names | |||||
* @return List of Strings holding ElementMapping names. | |||||
*/ | |||||
public List getAdditionalElementMappings() { | |||||
return additionalElementMappings; | |||||
} | |||||
/** | |||||
* Sets an explicit LayoutManagerMaker instance which overrides the one | |||||
* defined by the AreaTreeHandler. | |||||
* @param lmMaker the LayoutManagerMaker instance | |||||
*/ | |||||
public void setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker) { | |||||
this.lmMakerOverride = lmMaker; | |||||
} | |||||
/** | |||||
* Returns the overriding LayoutManagerMaker instance, if any. | |||||
* @return the overriding LayoutManagerMaker or null | |||||
*/ | |||||
public LayoutManagerMaker getLayoutManagerMakerOverride() { | |||||
return this.lmMakerOverride; | |||||
} | |||||
/** | |||||
* Sets the font base URL. | |||||
* @param fontBaseURL font base URL | |||||
*/ | |||||
public void setFontBaseURL(String fontBaseURL) { | |||||
this.fontBaseURL = fontBaseURL; | |||||
} | |||||
/** @return the font base URL */ | |||||
public String getFontBaseURL() { | |||||
return this.fontBaseURL; | |||||
} | |||||
/** | |||||
* Sets the URI Resolver. It is used for resolving factory-level URIs like hyphenation | |||||
* patterns and as backup for URI resolution performed during a rendering run. | |||||
* @param resolver the new URI resolver | |||||
*/ | |||||
public void setURIResolver(URIResolver resolver) { | |||||
this.uriResolver = resolver; | |||||
} | |||||
/** | |||||
* Returns the URI Resolver. | |||||
* @return the URI Resolver | |||||
*/ | |||||
public URIResolver getURIResolver() { | |||||
return this.uriResolver; | |||||
} | |||||
/** @return the HyphenationTreeResolver for resolving user-supplied hyphenation patterns. */ | |||||
public HyphenationTreeResolver getHyphenationTreeResolver() { | |||||
return this.hyphResolver; | |||||
} | |||||
/** | |||||
* Activates strict XSL content model validation for FOP | |||||
* Default is false (FOP will continue processing where it can) | |||||
* @param validateStrictly true to turn on strict validation | |||||
*/ | |||||
public void setStrictValidation(boolean validateStrictly) { | |||||
this.strictValidation = validateStrictly; | |||||
} | |||||
/** | |||||
* Returns whether FOP is strictly validating input XSL | |||||
* @return true of strict validation turned on, false otherwise | |||||
*/ | |||||
public boolean validateStrictly() { | |||||
return strictValidation; | |||||
} | |||||
/** | |||||
* @return true if the indent inheritance should be broken when crossing reference area | |||||
* boundaries (for more info, see the javadoc for the relative member variable) | |||||
*/ | |||||
public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() { | |||||
return breakIndentInheritanceOnReferenceAreaBoundary; | |||||
} | |||||
/** | |||||
* Controls whether to enable a feature that breaks indent inheritance when crossing | |||||
* reference area boundaries. | |||||
* <p> | |||||
* This flag controls whether FOP will enable special code that breaks property | |||||
* inheritance for start-indent and end-indent when the evaluation of the inherited | |||||
* value would cross a reference area. This is described under | |||||
* http://wiki.apache.org/xmlgraphics-fop/IndentInheritance as is intended to | |||||
* improve interoperability with commercial FO implementations and to produce | |||||
* results that are more in line with the expectation of unexperienced FO users. | |||||
* Note: Enabling this features violates the XSL specification! | |||||
* @param value true to enable the feature | |||||
*/ | |||||
public void setBreakIndentInheritanceOnReferenceAreaBoundary(boolean value) { | |||||
this.breakIndentInheritanceOnReferenceAreaBoundary = value; | |||||
} | |||||
/** @return true if kerning on base 14 fonts is enabled */ | |||||
public boolean isBase14KerningEnabled() { | |||||
return this.enableBase14Kerning; | |||||
} | |||||
/** | |||||
* Controls whether kerning is activated on base 14 fonts. | |||||
* @param value true if kerning should be activated | |||||
*/ | |||||
public void setBase14KerningEnabled(boolean value) { | |||||
this.enableBase14Kerning = value; | |||||
} | |||||
/** @return the resolution for resolution-dependant input */ | |||||
public float getSourceResolution() { | |||||
return this.sourceResolution; | |||||
} | |||||
/** | |||||
* Returns the conversion factor from pixel units to millimeters. This | |||||
* depends on the desired source resolution. | |||||
* @return float conversion factor | |||||
* @see #getSourceResolution() | |||||
*/ | |||||
public float getSourcePixelUnitToMillimeter() { | |||||
return 25.4f / getSourceResolution(); | |||||
} | |||||
/** | |||||
* Sets the source resolution in dpi. This value is used to interpret the pixel size | |||||
* of source documents like SVG images and bitmap images without resolution information. | |||||
* @param dpi resolution in dpi | |||||
*/ | |||||
public void setSourceResolution(int dpi) { | |||||
this.sourceResolution = dpi; | |||||
} | |||||
/** | |||||
* Gets the default page-height to use as fallback, | |||||
* in case page-height="auto" | |||||
* | |||||
* @return the page-height, as a String | |||||
*/ | |||||
public String getPageHeight() { | |||||
return this.pageHeight; | |||||
} | |||||
/** | |||||
* Sets the page-height to use as fallback, in case | |||||
* page-height="auto" | |||||
* | |||||
* @param pageHeight page-height as a String | |||||
*/ | |||||
public void setPageHeight(String pageHeight) { | |||||
this.pageHeight = pageHeight; | |||||
} | |||||
/** | |||||
* Gets the default page-width to use as fallback, | |||||
* in case page-width="auto" | |||||
* | |||||
* @return the page-width, as a String | |||||
*/ | |||||
public String getPageWidth() { | |||||
return this.pageWidth; | |||||
} | |||||
/** | |||||
* Sets the page-width to use as fallback, in case | |||||
* page-width="auto" | |||||
* | |||||
* @param pageWidth page-width as a String | |||||
*/ | |||||
public void setPageWidth(String pageWidth) { | |||||
this.pageWidth = pageWidth; | |||||
} | |||||
//------------------------------------------- Configuration stuff | |||||
/** | |||||
* Set the user configuration. | |||||
* @param userConfigFile the configuration file | |||||
* @throws IOException if an I/O error occurs | |||||
* @throws SAXException if a parsing error occurs | |||||
*/ | |||||
public void setUserConfig(File userConfigFile) | |||||
throws SAXException, IOException { | |||||
try { | |||||
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); | |||||
setUserConfig(cfgBuilder.buildFromFile(userConfigFile)); | |||||
} catch (ConfigurationException cfge) { | |||||
log.error("Error loading configuration: " | |||||
+ cfge.getMessage()); | |||||
} | |||||
} | |||||
/** | |||||
* Set the user configuration from an URI. | |||||
* @param uri the URI to the configuration file | |||||
* @throws IOException if an I/O error occurs | |||||
* @throws SAXException if a parsing error occurs | |||||
*/ | |||||
public void setUserConfig(String uri) | |||||
throws SAXException, IOException { | |||||
try { | |||||
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); | |||||
setUserConfig(cfgBuilder.build(uri)); | |||||
} catch (ConfigurationException cfge) { | |||||
log.error("Error loading configuration: " | |||||
+ cfge.getMessage()); | |||||
} | |||||
} | |||||
/** | |||||
* Set the user configuration. | |||||
* @param userConfig configuration | |||||
*/ | |||||
public void setUserConfig(Configuration userConfig) { | |||||
this.userConfig = userConfig; | |||||
try { | |||||
initUserConfig(); | |||||
} catch (ConfigurationException cfge) { | |||||
log.error("Error initializing factory configuration: " | |||||
+ cfge.getMessage()); | |||||
} | |||||
} | |||||
/** | |||||
* Get the user configuration. | |||||
* @return the user configuration | |||||
*/ | |||||
public Configuration getUserConfig() { | |||||
return userConfig; | |||||
} | |||||
/** | |||||
* Initializes user agent settings from the user configuration | |||||
* file, if present: baseURL, resolution, default page size,... | |||||
* | |||||
* @throws ConfigurationException when there is an entry that | |||||
* misses the required attribute | |||||
*/ | |||||
public void initUserConfig() throws ConfigurationException { | |||||
log.debug("Initializing User Agent Configuration"); | |||||
setFontBaseURL(getBaseURLfromConfig(userConfig, "font-base")); | |||||
final String hyphBase = getBaseURLfromConfig(userConfig, "hyphenation-base"); | |||||
if (hyphBase != null) { | |||||
this.hyphResolver = new HyphenationTreeResolver() { | |||||
public Source resolve(String href) { | |||||
return resolveURI(href, hyphBase); | |||||
} | |||||
}; | |||||
} | |||||
if (userConfig.getChild("source-resolution", false) != null) { | |||||
this.sourceResolution | |||||
= userConfig.getChild("source-resolution").getValueAsFloat( | |||||
DEFAULT_SOURCE_RESOLUTION); | |||||
log.info("Source resolution set to: " + sourceResolution | |||||
+ "dpi (px2mm=" + getSourcePixelUnitToMillimeter() + ")"); | |||||
} | |||||
if (userConfig.getChild("strict-validation", false) != null) { | |||||
this.strictValidation = userConfig.getChild("strict-validation").getValueAsBoolean(); | |||||
} | |||||
if (userConfig.getChild("break-indent-inheritance", false) != null) { | |||||
this.breakIndentInheritanceOnReferenceAreaBoundary | |||||
= userConfig.getChild("break-indent-inheritance").getValueAsBoolean(); | |||||
} | |||||
Configuration pageConfig = userConfig.getChild("default-page-settings"); | |||||
if (pageConfig.getAttribute("height", null) != null) { | |||||
setPageHeight(pageConfig.getAttribute("height")); | |||||
log.info("Default page-height set to: " + pageHeight); | |||||
} | |||||
if (pageConfig.getAttribute("width", null) != null) { | |||||
setPageWidth(pageConfig.getAttribute("width")); | |||||
log.info("Default page-width set to: " + pageWidth); | |||||
} | |||||
} | |||||
/** | |||||
* Retrieves and verifies a base URL. | |||||
* @param cfg The Configuration object to retrieve the base URL from | |||||
* @param name the element name for the base URL | |||||
* @return the requested base URL or null if not available | |||||
*/ | |||||
public static String getBaseURLfromConfig(Configuration cfg, String name) { | |||||
if (cfg.getChild(name, false) != null) { | |||||
try { | |||||
String cfgBaseDir = cfg.getChild(name).getValue(null); | |||||
if (cfgBaseDir != null) { | |||||
File dir = new File(cfgBaseDir); | |||||
if (dir.isDirectory()) { | |||||
cfgBaseDir = dir.toURL().toExternalForm(); | |||||
} | |||||
} | |||||
log.info(name + " set to: " + cfgBaseDir); | |||||
return cfgBaseDir; | |||||
} catch (MalformedURLException mue) { | |||||
log.error("Base URL in user config is malformed!"); | |||||
} | |||||
} | |||||
return null; | |||||
} | |||||
//------------------------------------------- URI resolution | |||||
/** | |||||
* Attempts to resolve the given URI. | |||||
* Will use the configured resolver and if not successful fall back | |||||
* to the default resolver. | |||||
* @param uri URI to access | |||||
* @param base the base URI to resolve against | |||||
* @return A {@link javax.xml.transform.Source} object, or null if the URI | |||||
* cannot be resolved. | |||||
* @see org.apache.fop.apps.FOURIResolver | |||||
*/ | |||||
public Source resolveURI(String uri, String base) { | |||||
Source source = null; | |||||
if (uriResolver != null) { | |||||
try { | |||||
source = uriResolver.resolve(uri, base); | |||||
} catch (TransformerException te) { | |||||
log.error("Attempt to resolve URI '" + uri + "' failed: ", te); | |||||
} | |||||
} | |||||
if (source == null) { | |||||
// URI Resolver not configured or returned null, use default resolver | |||||
try { | |||||
source = foURIResolver.resolve(uri, base); | |||||
} catch (TransformerException te) { | |||||
log.error("Attempt to resolve URI '" + uri + "' failed: ", te); | |||||
} | |||||
} | |||||
return source; | |||||
} | |||||
} |
setupModel(userAgent, outputFormat, stream); | setupModel(userAgent, outputFormat, stream); | ||||
lmMaker = userAgent.getLayoutManagerMakerOverride(); | |||||
lmMaker = userAgent.getFactory().getLayoutManagerMakerOverride(); | |||||
if (lmMaker == null) { | if (lmMaker == null) { | ||||
lmMaker = new LayoutManagerMapping(); | lmMaker = new LayoutManagerMapping(); | ||||
} | } |
* @return the ContentHandler instance to receive the SAX stream from the area tree XML | * @return the ContentHandler instance to receive the SAX stream from the area tree XML | ||||
*/ | */ | ||||
public ContentHandler getContentHandler(AreaTreeModel treeModel, FOUserAgent userAgent) { | public ContentHandler getContentHandler(AreaTreeModel treeModel, FOUserAgent userAgent) { | ||||
//TODO Retrieve this instance from the environment class once it has been created. | |||||
ElementMappingRegistry elementMappingRegistry = new ElementMappingRegistry(userAgent); | |||||
ElementMappingRegistry elementMappingRegistry | |||||
= userAgent.getFactory().getElementMappingRegistry(); | |||||
return new Handler(treeModel, userAgent, elementMappingRegistry); | return new Handler(treeModel, userAgent, elementMappingRegistry); | ||||
} | } | ||||
handled = false; | handled = false; | ||||
} | } | ||||
} else { | } else { | ||||
ContentHandlerFactory factory | |||||
= ContentHandlerFactoryRegistry.getInstance().getFactory(uri); | |||||
ContentHandlerFactoryRegistry registry | |||||
= userAgent.getFactory().getContentHandlerFactoryRegistry(); | |||||
ContentHandlerFactory factory = registry.getFactory(uri); | |||||
if (factory != null) { | if (factory != null) { | ||||
delegate = factory.createContentHandler(); | delegate = factory.createContentHandler(); | ||||
delegateStack.push(qName); | delegateStack.push(qName); | ||||
if (url != null) { | if (url != null) { | ||||
bkg.setURL(url); | bkg.setURL(url); | ||||
ImageFactory fact = ImageFactory.getInstance(); | |||||
ImageFactory fact = userAgent.getFactory().getImageFactory(); | |||||
FopImage img = fact.getImage(url, userAgent); | FopImage img = fact.getImage(url, userAgent); | ||||
if (img == null) { | if (img == null) { | ||||
log.error("Background image not available: " + url); | log.error("Background image not available: " + url); |
public void renderTo(FOUserAgent userAgent, String outputFormat, OutputStream out) | public void renderTo(FOUserAgent userAgent, String outputFormat, OutputStream out) | ||||
throws FOPException { | throws FOPException { | ||||
FontInfo fontInfo = new FontInfo(); | FontInfo fontInfo = new FontInfo(); | ||||
FOUserAgent effUserAgent = userAgent; | |||||
if (effUserAgent == null) { | |||||
effUserAgent = new FOUserAgent(); | |||||
} | |||||
AreaTreeModel treeModel = new RenderPagesModel(userAgent, | AreaTreeModel treeModel = new RenderPagesModel(userAgent, | ||||
outputFormat, fontInfo, out); | outputFormat, fontInfo, out); | ||||
import org.apache.fop.Version; | import org.apache.fop.Version; | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.pdf.PDFEncryptionManager; | import org.apache.fop.pdf.PDFEncryptionManager; | ||||
import org.apache.fop.pdf.PDFEncryptionParams; | import org.apache.fop.pdf.PDFEncryptionParams; | ||||
import org.apache.fop.render.awt.AWTRenderer; | import org.apache.fop.render.awt.AWTRenderer; | ||||
import org.apache.fop.render.Renderer; | import org.apache.fop.render.Renderer; | ||||
import org.apache.fop.render.pdf.PDFRenderer; | |||||
import org.apache.fop.render.xml.XMLRenderer; | import org.apache.fop.render.xml.XMLRenderer; | ||||
import org.apache.fop.util.CommandLineLogger; | import org.apache.fop.util.CommandLineLogger; | ||||
import org.apache.commons.logging.LogFactory; | import org.apache.commons.logging.LogFactory; | ||||
// SAX | // SAX | ||||
import org.xml.sax.XMLReader; | |||||
import org.xml.sax.SAXException; | import org.xml.sax.SAXException; | ||||
import javax.xml.parsers.SAXParserFactory; | |||||
// avalon configuration | |||||
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; | |||||
import org.apache.avalon.framework.configuration.Configuration; | |||||
import org.apache.avalon.framework.configuration.ConfigurationException; | |||||
/** | /** | ||||
* Options parses the commandline arguments | * Options parses the commandline arguments | ||||
/* output mode */ | /* output mode */ | ||||
private String outputmode = null; | private String outputmode = null; | ||||
private FopFactory factory = FopFactory.newInstance(); | |||||
private FOUserAgent foUserAgent; | private FOUserAgent foUserAgent; | ||||
private InputHandler inputHandler; | private InputHandler inputHandler; | ||||
throws FOPException, IOException { | throws FOPException, IOException { | ||||
boolean optionsParsed = true; | boolean optionsParsed = true; | ||||
foUserAgent = new FOUserAgent(); | |||||
foUserAgent = factory.newFOUserAgent(); | |||||
try { | try { | ||||
optionsParsed = parseOptions(args); | optionsParsed = parseOptions(args); | ||||
} else if (args[i].equals("-d")) { | } else if (args[i].equals("-d")) { | ||||
setLogOption("debug", "debug"); | setLogOption("debug", "debug"); | ||||
} else if (args[i].equals("-r")) { | } else if (args[i].equals("-r")) { | ||||
foUserAgent.setStrictValidation(false); | |||||
factory.setStrictValidation(false); | |||||
} else if (args[i].equals("-dpi")) { | } else if (args[i].equals("-dpi")) { | ||||
i = i + parseResolution(args, i); | i = i + parseResolution(args, i); | ||||
} else if (args[i].equals("-q") || args[i].equals("--quiet")) { | } else if (args[i].equals("-q") || args[i].equals("--quiet")) { | ||||
} | } | ||||
private PDFEncryptionParams getPDFEncryptionParams() throws FOPException { | private PDFEncryptionParams getPDFEncryptionParams() throws FOPException { | ||||
if (foUserAgent.getPDFEncryptionParams() == null) { | |||||
PDFEncryptionParams params = (PDFEncryptionParams)foUserAgent.getRendererOptions().get( | |||||
PDFRenderer.ENCRYPTION_PARAMS); | |||||
if (params == null) { | |||||
if (!PDFEncryptionManager.checkAvailableAlgorithms()) { | if (!PDFEncryptionManager.checkAvailableAlgorithms()) { | ||||
throw new FOPException("PDF encryption requested but it is not available." | throw new FOPException("PDF encryption requested but it is not available." | ||||
+ " Please make sure MD5 and RC4 algorithms are available."); | + " Please make sure MD5 and RC4 algorithms are available."); | ||||
} | } | ||||
foUserAgent.setPDFEncryptionParams(new PDFEncryptionParams()); | |||||
params = new PDFEncryptionParams(); | |||||
foUserAgent.getRendererOptions().put(PDFRenderer.ENCRYPTION_PARAMS, params); | |||||
} | } | ||||
return foUserAgent.getPDFEncryptionParams(); | |||||
return params; | |||||
} | } | ||||
private int parsePDFOwnerPassword(String[] args, int i) throws FOPException { | private int parsePDFOwnerPassword(String[] args, int i) throws FOPException { | ||||
if (userConfigFile == null) { | if (userConfigFile == null) { | ||||
return; | return; | ||||
} | } | ||||
XMLReader parser = createParser(); | |||||
DefaultConfigurationBuilder configBuilder | |||||
= new DefaultConfigurationBuilder(parser); | |||||
Configuration userConfig = null; | |||||
try { | try { | ||||
userConfig = configBuilder.buildFromFile(userConfigFile); | |||||
factory.setUserConfig(userConfigFile); | |||||
} catch (SAXException e) { | } catch (SAXException e) { | ||||
throw new FOPException(e); | throw new FOPException(e); | ||||
} catch (ConfigurationException e) { | |||||
throw new FOPException(e); | |||||
} | } | ||||
foUserAgent.setUserConfig(userConfig); | |||||
} | } | ||||
/** | /** | ||||
} | } | ||||
} | } | ||||
/** | |||||
* Creates <code>XMLReader</code> object using default | |||||
* <code>SAXParserFactory</code> | |||||
* @return the created <code>XMLReader</code> | |||||
* @throws FOPException if the parser couldn't be created or configured for proper operation. | |||||
*/ | |||||
private XMLReader createParser() throws FOPException { | |||||
try { | |||||
SAXParserFactory factory = SAXParserFactory.newInstance(); | |||||
factory.setNamespaceAware(true); | |||||
return factory.newSAXParser().getXMLReader(); | |||||
} catch (Exception e) { | |||||
throw new FOPException("Couldn't create XMLReader", e); | |||||
} | |||||
} | |||||
} | } | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.render.awt.viewer.Renderable; | import org.apache.fop.render.awt.viewer.Renderable; | ||||
/** | /** | ||||
*/ | */ | ||||
public void renderTo(FOUserAgent userAgent, String outputFormat, OutputStream out) | public void renderTo(FOUserAgent userAgent, String outputFormat, OutputStream out) | ||||
throws FOPException { | throws FOPException { | ||||
Fop fop = new Fop(outputFormat, userAgent); | |||||
FopFactory factory = userAgent.getFactory(); | |||||
Fop fop; | |||||
if (out != null) { | if (out != null) { | ||||
fop.setOutputStream(out); | |||||
fop = factory.newFop(outputFormat, userAgent, out); | |||||
} else { | |||||
fop = factory.newFop(outputFormat, userAgent); | |||||
} | } | ||||
// if base URL was not explicitly set in FOUserAgent, obtain here | // if base URL was not explicitly set in FOUserAgent, obtain here |
import org.apache.commons.logging.Log; | import org.apache.commons.logging.Log; | ||||
import org.apache.commons.logging.LogFactory; | import org.apache.commons.logging.LogFactory; | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.fo.ElementMapping.Maker; | import org.apache.fop.fo.ElementMapping.Maker; | ||||
import org.apache.fop.util.Service; | import org.apache.fop.util.Service; | ||||
import org.w3c.dom.DOMImplementation; | import org.w3c.dom.DOMImplementation; | ||||
/** | /** | ||||
* Main constructor. Adds all default element mapping as well as detects ElementMapping | * Main constructor. Adds all default element mapping as well as detects ElementMapping | ||||
* through the Service discovery. | * through the Service discovery. | ||||
* @param userAgent the user agent | |||||
* @param factory the Fop Factory | |||||
*/ | */ | ||||
public ElementMappingRegistry(FOUserAgent userAgent) { | |||||
public ElementMappingRegistry(FopFactory factory) { | |||||
// Add standard element mappings | // Add standard element mappings | ||||
setupDefaultMappings(); | setupDefaultMappings(); | ||||
// add additional ElementMappings defined within FOUserAgent | // add additional ElementMappings defined within FOUserAgent | ||||
List addlEMs = userAgent.getAdditionalElementMappings(); | |||||
List addlEMs = factory.getAdditionalElementMappings(); | |||||
if (addlEMs != null) { | if (addlEMs != null) { | ||||
for (int i = 0; i < addlEMs.size(); i++) { | for (int i = 0; i < addlEMs.size(); i++) { |
/** The registry for ElementMapping instances */ | /** The registry for ElementMapping instances */ | ||||
protected ElementMappingRegistry elementMappingRegistry; | protected ElementMappingRegistry elementMappingRegistry; | ||||
//TODO Remove the ElementMappingRegistry from here and move it to a new environmental class | |||||
//FOTreeBuilder should be a "one-use" component. | |||||
/** | /** | ||||
* The root of the formatting object tree | * The root of the formatting object tree | ||||
*/ | */ | ||||
OutputStream stream) throws FOPException { | OutputStream stream) throws FOPException { | ||||
this.userAgent = foUserAgent; | this.userAgent = foUserAgent; | ||||
this.elementMappingRegistry = userAgent.getFactory().getElementMappingRegistry(); | |||||
//This creates either an AreaTreeHandler and ultimately a Renderer, or | //This creates either an AreaTreeHandler and ultimately a Renderer, or | ||||
//one of the RTF-, MIF- etc. Handlers. | //one of the RTF-, MIF- etc. Handlers. | ||||
foEventHandler = foUserAgent.getRendererFactory().createFOEventHandler( | foEventHandler = foUserAgent.getRendererFactory().createFOEventHandler( | ||||
return new StaticPropertyList(fobj, parentPropertyList); | return new StaticPropertyList(fobj, parentPropertyList); | ||||
} | } | ||||
}); | }); | ||||
this.elementMappingRegistry = new ElementMappingRegistry(foUserAgent); | |||||
} | } | ||||
/** | /** | ||||
foEventHandler.endDocument(); | foEventHandler.endDocument(); | ||||
//Notify the image factory that this user agent has expired. | //Notify the image factory that this user agent has expired. | ||||
ImageFactory.getInstance().removeContext(this.userAgent); | |||||
ImageFactory imageFactory = userAgent.getFactory().getImageFactory(); | |||||
imageFactory.removeContext(this.userAgent); | |||||
} | } | ||||
/** | /** |
package org.apache.fop.fo.flow; | package org.apache.fop.fo.flow; | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.fo.FONode; | import org.apache.fop.fo.FONode; | ||||
import org.apache.fop.fo.PropertyList; | import org.apache.fop.fo.PropertyList; | ||||
import org.apache.fop.fo.ValidationException; | import org.apache.fop.fo.ValidationException; | ||||
//Additional processing: preload image | //Additional processing: preload image | ||||
url = ImageFactory.getURL(getSrc()); | url = ImageFactory.getURL(getSrc()); | ||||
ImageFactory fact = ImageFactory.getInstance(); | |||||
fopimage = fact.getImage(url, getUserAgent()); | |||||
FOUserAgent userAgent = getUserAgent(); | |||||
ImageFactory fact = userAgent.getFactory().getImageFactory(); | |||||
fopimage = fact.getImage(url, userAgent); | |||||
if (fopimage == null) { | if (fopimage == null) { | ||||
getLogger().error("Image not available: " + getSrc()); | getLogger().error("Image not available: " + getSrc()); | ||||
} else { | } else { |
package org.apache.fop.fo.properties; | package org.apache.fop.fo.properties; | ||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.datatypes.ColorType; | import org.apache.fop.datatypes.ColorType; | ||||
import org.apache.fop.datatypes.Length; | import org.apache.fop.datatypes.Length; | ||||
import org.apache.fop.datatypes.PercentBaseContext; | import org.apache.fop.datatypes.PercentBaseContext; | ||||
//Additional processing: preload image | //Additional processing: preload image | ||||
String url = ImageFactory.getURL(backgroundImage); | String url = ImageFactory.getURL(backgroundImage); | ||||
ImageFactory fact = ImageFactory.getInstance(); | |||||
fopimage = fact.getImage(url, fobj.getUserAgent()); | |||||
FOUserAgent userAgent = fobj.getUserAgent(); | |||||
ImageFactory fact = userAgent.getFactory().getImageFactory(); | |||||
fopimage = fact.getImage(url, userAgent); | |||||
if (fopimage == null) { | if (fopimage == null) { | ||||
fobj.getLogger().error("Background image not available: " + backgroundImage); | fobj.getLogger().error("Background image not available: " + backgroundImage); | ||||
} else { | } else { |
*/ | */ | ||||
protected static Log log = LogFactory.getLog(FopImage.class); | protected static Log log = LogFactory.getLog(FopImage.class); | ||||
private static ImageFactory factory = new ImageFactory(); | |||||
private HashMap imageMimeTypes = new HashMap(); | private HashMap imageMimeTypes = new HashMap(); | ||||
private ImageCache cache = new ContextImageCache(true); | private ImageCache cache = new ContextImageCache(true); | ||||
private ImageFactory() { | |||||
/** | |||||
* Main constructor for the ImageFactory. | |||||
*/ | |||||
public ImageFactory() { | |||||
/* @todo The mappings set up below of image mime types to implementing | /* @todo The mappings set up below of image mime types to implementing | ||||
* classes should be made externally configurable | * classes should be made externally configurable | ||||
*/ | */ | ||||
imt.addProvider(emfImage); | imt.addProvider(emfImage); | ||||
} | } | ||||
/** | |||||
* Get static image factory instance. | |||||
* | |||||
* @return the image factory instance | |||||
*/ | |||||
public static ImageFactory getInstance() { | |||||
return factory; | |||||
} | |||||
/** | /** | ||||
* Get the url string from a wrapped url. | * Get the url string from a wrapped url. | ||||
* | * |
if (!valid || image != null) { | if (!valid || image != null) { | ||||
return image; | return image; | ||||
} | } | ||||
image = ImageFactory.getInstance().loadImage(url, userAgent); | |||||
ImageFactory imageFactory = userAgent.getFactory().getImageFactory(); | |||||
image = imageFactory.loadImage(url, userAgent); | |||||
if (image == null) { | if (image == null) { | ||||
cache.invalidateImage(url, userAgent); | cache.invalidateImage(url, userAgent); | ||||
valid = false; | valid = false; |
/* | /* | ||||
* Copyright 1999-2005 The Apache Software Foundation. | |||||
* Copyright 1999-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import java.util.LinkedList; | import java.util.LinkedList; | ||||
import org.apache.fop.fo.properties.CommonBorderPaddingBackground; | import org.apache.fop.fo.properties.CommonBorderPaddingBackground; | ||||
import org.apache.fop.layoutmgr.inline.AlignmentContext; | import org.apache.fop.layoutmgr.inline.AlignmentContext; | ||||
import org.apache.fop.layoutmgr.inline.LeafNodeLayoutManager.AreaInfo; | |||||
/** | /** | ||||
* LayoutManager for the fo:character formatting object | * LayoutManager for the fo:character formatting object | ||||
/** @see InlineLevelLayoutManager#getWordChars(StringBuffer, Position) */ | /** @see InlineLevelLayoutManager#getWordChars(StringBuffer, Position) */ | ||||
public void getWordChars(StringBuffer sbChars, Position bp) { | public void getWordChars(StringBuffer sbChars, Position bp) { | ||||
sbChars.append | sbChars.append | ||||
(((org.apache.fop.area.inline.Character) curArea).getChar()); | |||||
(((org.apache.fop.area.inline.TextArea) curArea).getText()); | |||||
} | } | ||||
/** @see InlineLevelLayoutManager#hyphenate(Position, HyphContext) */ | /** @see InlineLevelLayoutManager#hyphenate(Position, HyphContext) */ |
// on an inline or wrapper below the block level. | // on an inline or wrapper below the block level. | ||||
Hyphenation hyph | Hyphenation hyph | ||||
= Hyphenator.hyphenate(hyphenationProperties.language, | = Hyphenator.hyphenate(hyphenationProperties.language, | ||||
hyphenationProperties.country, | |||||
getFObj().getUserAgent().getHyphenationTreeResolver(), | |||||
sbChars.toString(), | |||||
hyphenationProperties.hyphenationRemainCharacterCount, | |||||
hyphenationProperties.hyphenationPushCharacterCount); | |||||
hyphenationProperties.country, | |||||
getFObj().getUserAgent().getFactory().getHyphenationTreeResolver(), | |||||
sbChars.toString(), | |||||
hyphenationProperties.hyphenationRemainCharacterCount, | |||||
hyphenationProperties.hyphenationPushCharacterCount); | |||||
// They hyph structure contains the information we need | // They hyph structure contains the information we need | ||||
// Now start from prev: reset to that position, ask that LM to get | // Now start from prev: reset to that position, ask that LM to get | ||||
// a Position for the first hyphenation offset. If the offset isn't in | // a Position for the first hyphenation offset. If the offset isn't in |
public void setupFontInfo(FontInfo inFontInfo) { | public void setupFontInfo(FontInfo inFontInfo) { | ||||
this.fontInfo = inFontInfo; | this.fontInfo = inFontInfo; | ||||
FontResolver resolver = new DefaultFontResolver(userAgent); | FontResolver resolver = new DefaultFontResolver(userAgent); | ||||
FontSetup.setup(fontInfo, fontList, resolver, userAgent.isBase14KerningEnabled()); | |||||
FontSetup.setup(fontInfo, fontList, resolver, | |||||
userAgent.getFactory().isBase14KerningEnabled()); | |||||
} | } | ||||
/** | /** |
if (out == null | if (out == null | ||||
&& userAgent.getRendererOverride() == null | && userAgent.getRendererOverride() == null | ||||
&& rendMaker.needsOutputStream()) { | && rendMaker.needsOutputStream()) { | ||||
throw new IllegalStateException( | |||||
throw new FOPException( | |||||
"OutputStream has not been set"); | "OutputStream has not been set"); | ||||
} | } | ||||
//Found a Renderer so we need to construct an AreaTreeHandler. | //Found a Renderer so we need to construct an AreaTreeHandler. |
int y = currentBPPosition; | int y = currentBPPosition; | ||||
String url = ImageFactory.getURL(pUrl); | String url = ImageFactory.getURL(pUrl); | ||||
ImageFactory fact = ImageFactory.getInstance(); | |||||
ImageFactory fact = userAgent.getFactory().getImageFactory(); | |||||
FopImage fopimage = fact.getImage(url, userAgent); | FopImage fopimage = fact.getImage(url, userAgent); | ||||
if (fopimage == null) { | if (fopimage == null) { |
import org.apache.fop.pdf.PDFColor; | import org.apache.fop.pdf.PDFColor; | ||||
import org.apache.fop.pdf.PDFDocument; | import org.apache.fop.pdf.PDFDocument; | ||||
import org.apache.fop.pdf.PDFEncryptionManager; | import org.apache.fop.pdf.PDFEncryptionManager; | ||||
import org.apache.fop.pdf.PDFEncryptionParams; | |||||
import org.apache.fop.pdf.PDFFilterList; | import org.apache.fop.pdf.PDFFilterList; | ||||
import org.apache.fop.pdf.PDFInfo; | import org.apache.fop.pdf.PDFInfo; | ||||
import org.apache.fop.pdf.PDFLink; | import org.apache.fop.pdf.PDFLink; | ||||
*/ | */ | ||||
public static final String MIME_TYPE = MimeConstants.MIME_PDF; | public static final String MIME_TYPE = MimeConstants.MIME_PDF; | ||||
/** PDF encryption parameter: all parameters as object, datatype: PDFEncryptionParams */ | |||||
public static final String ENCRYPTION_PARAMS = "encryption-params"; | |||||
/** PDF encryption parameter: user password, datatype: String */ | |||||
public static final String USER_PASSWORD = "user-password"; | |||||
/** PDF encryption parameter: owner password, datatype: String */ | |||||
public static final String OWNER_PASSWORD = "owner-password"; | |||||
/** PDF encryption parameter: Forbids printing, datatype: Boolean or "true"/"false" */ | |||||
public static final String NO_PRINT = "noprint"; | |||||
/** PDF encryption parameter: Forbids copying content, datatype: Boolean or "true"/"false" */ | |||||
public static final String NO_COPY_CONTENT = "nocopy"; | |||||
/** PDF encryption parameter: Forbids editing content, datatype: Boolean or "true"/"false" */ | |||||
public static final String NO_EDIT_CONTENT = "noedit"; | |||||
/** PDF encryption parameter: Forbids annotations, datatype: Boolean or "true"/"false" */ | |||||
public static final String NO_ANNOTATIONS = "noannotations"; | |||||
/** Rendering Options key for the PDF/A mode. */ | /** Rendering Options key for the PDF/A mode. */ | ||||
public static final String PDF_A_MODE = "pdf-a-mode"; | public static final String PDF_A_MODE = "pdf-a-mode"; | ||||
*/ | */ | ||||
protected PDFPage currentPage; | protected PDFPage currentPage; | ||||
/** the (optional) encryption parameters */ | |||||
protected PDFEncryptionParams encryptionParams; | |||||
/** The current Transform */ | /** The current Transform */ | ||||
protected AffineTransform currentBasicTransform; | protected AffineTransform currentBasicTransform; | ||||
} | } | ||||
} | } | ||||
private boolean booleanValueOf(Object obj) { | |||||
if (obj instanceof Boolean) { | |||||
return ((Boolean)obj).booleanValue(); | |||||
} else if (obj instanceof String) { | |||||
return Boolean.valueOf((String)obj).booleanValue(); | |||||
} else { | |||||
throw new IllegalArgumentException("Boolean or \"true\" or \"false\" expected."); | |||||
} | |||||
} | |||||
/** | /** | ||||
* @see org.apache.fop.render.Renderer#setUserAgent(FOUserAgent) | * @see org.apache.fop.render.Renderer#setUserAgent(FOUserAgent) | ||||
*/ | */ | ||||
public void setUserAgent(FOUserAgent agent) { | public void setUserAgent(FOUserAgent agent) { | ||||
super.setUserAgent(agent); | super.setUserAgent(agent); | ||||
PDFEncryptionParams params | |||||
= (PDFEncryptionParams)agent.getRendererOptions().get(ENCRYPTION_PARAMS); | |||||
if (params != null) { | |||||
this.encryptionParams = params; //overwrite if available | |||||
} | |||||
String pwd; | |||||
pwd = (String)agent.getRendererOptions().get(USER_PASSWORD); | |||||
if (pwd != null) { | |||||
if (encryptionParams == null) { | |||||
this.encryptionParams = new PDFEncryptionParams(); | |||||
} | |||||
this.encryptionParams.setUserPassword(pwd); | |||||
} | |||||
pwd = (String)agent.getRendererOptions().get(OWNER_PASSWORD); | |||||
if (pwd != null) { | |||||
if (encryptionParams == null) { | |||||
this.encryptionParams = new PDFEncryptionParams(); | |||||
} | |||||
this.encryptionParams.setOwnerPassword(pwd); | |||||
} | |||||
Object setting; | |||||
setting = agent.getRendererOptions().get(NO_PRINT); | |||||
if (setting != null) { | |||||
if (encryptionParams == null) { | |||||
this.encryptionParams = new PDFEncryptionParams(); | |||||
} | |||||
this.encryptionParams.setAllowPrint(!booleanValueOf(setting)); | |||||
} | |||||
setting = agent.getRendererOptions().get(NO_COPY_CONTENT); | |||||
if (setting != null) { | |||||
if (encryptionParams == null) { | |||||
this.encryptionParams = new PDFEncryptionParams(); | |||||
} | |||||
this.encryptionParams.setAllowCopyContent(!booleanValueOf(setting)); | |||||
} | |||||
setting = agent.getRendererOptions().get(NO_EDIT_CONTENT); | |||||
if (setting != null) { | |||||
if (encryptionParams == null) { | |||||
this.encryptionParams = new PDFEncryptionParams(); | |||||
} | |||||
this.encryptionParams.setAllowEditContent(!booleanValueOf(setting)); | |||||
} | |||||
setting = agent.getRendererOptions().get(NO_ANNOTATIONS); | |||||
if (setting != null) { | |||||
if (encryptionParams == null) { | |||||
this.encryptionParams = new PDFEncryptionParams(); | |||||
} | |||||
this.encryptionParams.setAllowEditAnnotations(!booleanValueOf(setting)); | |||||
} | |||||
String s = (String)agent.getRendererOptions().get(PDF_A_MODE); | String s = (String)agent.getRendererOptions().get(PDF_A_MODE); | ||||
if (s != null) { | if (s != null) { | ||||
this.pdfAMode = PDFAMode.valueOf(s); | this.pdfAMode = PDFAMode.valueOf(s); | ||||
this.pdfDoc.outputHeader(stream); | this.pdfDoc.outputHeader(stream); | ||||
//Setup encryption if necessary | //Setup encryption if necessary | ||||
PDFEncryptionManager.setupPDFEncryption( | |||||
userAgent.getPDFEncryptionParams(), this.pdfDoc); | |||||
PDFEncryptionManager.setupPDFEncryption(encryptionParams, this.pdfDoc); | |||||
} | } | ||||
/** | /** | ||||
} | } | ||||
url = ImageFactory.getURL(url); | url = ImageFactory.getURL(url); | ||||
ImageFactory fact = ImageFactory.getInstance(); | |||||
ImageFactory fact = userAgent.getFactory().getImageFactory(); | |||||
FopImage fopimage = fact.getImage(url, userAgent); | FopImage fopimage = fact.getImage(url, userAgent); | ||||
if (fopimage == null) { | if (fopimage == null) { | ||||
return; | return; |
protected void drawImage(String url, Rectangle2D pos) { | protected void drawImage(String url, Rectangle2D pos) { | ||||
endTextObject(); | endTextObject(); | ||||
url = ImageFactory.getURL(url); | url = ImageFactory.getURL(url); | ||||
ImageFactory fact = ImageFactory.getInstance(); | |||||
ImageFactory fact = userAgent.getFactory().getImageFactory(); | |||||
FopImage fopimage = fact.getImage(url, userAgent); | FopImage fopimage = fact.getImage(url, userAgent); | ||||
if (fopimage == null) { | if (fopimage == null) { | ||||
return; | return; |
String url = eg.getURL(); | String url = eg.getURL(); | ||||
//set image data | //set image data | ||||
ImageFactory fact = ImageFactory.getInstance(); | |||||
FopImage fopimage = fact.getImage(url, eg.getUserAgent()); | |||||
FOUserAgent userAgent = eg.getUserAgent(); | |||||
ImageFactory fact = userAgent.getFactory().getImageFactory(); | |||||
FopImage fopimage = fact.getImage(url, userAgent); | |||||
if (fopimage == null) { | if (fopimage == null) { | ||||
log.error("Image could not be found: " + url); | log.error("Image could not be found: " + url); | ||||
return; | return; |
/* | /* | ||||
* Copyright 1999-2005 The Apache Software Foundation. | |||||
* Copyright 1999-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
package org.apache.fop.servlet; | package org.apache.fop.servlet; | ||||
import java.io.File; | |||||
import java.io.InputStream; | |||||
import java.io.IOException; | |||||
import java.io.PrintWriter; | import java.io.PrintWriter; | ||||
// JAXP | |||||
import javax.servlet.ServletException; | |||||
import javax.servlet.http.HttpServlet; | |||||
import javax.servlet.http.HttpServletRequest; | |||||
import javax.servlet.http.HttpServletResponse; | import javax.servlet.http.HttpServletResponse; | ||||
import javax.xml.transform.Transformer; | import javax.xml.transform.Transformer; | ||||
import javax.xml.transform.TransformerFactory; | |||||
import javax.xml.transform.TransformerException; | |||||
import javax.xml.transform.Result; | import javax.xml.transform.Result; | ||||
import javax.xml.transform.Source; | import javax.xml.transform.Source; | ||||
import javax.xml.transform.URIResolver; | |||||
import javax.xml.transform.sax.SAXResult; | import javax.xml.transform.sax.SAXResult; | ||||
import javax.xml.transform.stream.StreamSource; | |||||
// XML | |||||
import org.apache.commons.logging.impl.SimpleLog; | |||||
import org.apache.fop.apps.FOPException; | |||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
* Example URL: http://servername/fop/servlet/FopPrintServlet?fo=readme.fo | * Example URL: http://servername/fop/servlet/FopPrintServlet?fo=readme.fo | ||||
* <br/> | * <br/> | ||||
* Example URL: http://servername/fop/servlet/FopPrintServlet?xml=data.xml&xsl=format.xsl | * Example URL: http://servername/fop/servlet/FopPrintServlet?xml=data.xml&xsl=format.xsl | ||||
* | |||||
* @author <a href="mailto:fop-dev@xml.apache.org">Apache XML FOP Development Team</a> | |||||
* <br/> | |||||
* <b>Note:</b> This servlet is derived from FopServlet. Most methods are inherited from the | |||||
* superclass. Only the differences to the base class are necessary. | |||||
* | |||||
* @author <a href="mailto:fop-dev@xmlgraphics.apache.org">Apache FOP Development Team</a> | |||||
* @version $Id$ | * @version $Id$ | ||||
* (todo) Doesn't work since there's no AWTRenderer at the moment. Revisit when | |||||
* available. | |||||
* (todo) Ev. add caching mechanism for Templates objects | |||||
*/ | */ | ||||
public class FopPrintServlet extends HttpServlet { | |||||
/** Name of the parameter used for the XSL-FO file */ | |||||
protected static final String FO_REQUEST_PARAM = "fo"; | |||||
/** Name of the parameter used for the XML file */ | |||||
protected static final String XML_REQUEST_PARAM = "xml"; | |||||
/** Name of the parameter used for the XSLT file */ | |||||
protected static final String XSLT_REQUEST_PARAM = "xslt"; | |||||
/** Logger to give to FOP */ | |||||
protected SimpleLog log = null; | |||||
/** The TransformerFactory to use to create Transformer instances */ | |||||
protected TransformerFactory transFactory = null; | |||||
/** URIResolver for use by this servlet */ | |||||
protected URIResolver uriResolver; | |||||
/** | |||||
* @see javax.servlet.GenericServlet#init() | |||||
*/ | |||||
public void init() throws ServletException { | |||||
this.log = new SimpleLog("FOP/Print Servlet"); | |||||
log.setLevel(SimpleLog.LOG_LEVEL_WARN); | |||||
this.uriResolver = new ServletContextURIResolver(getServletContext()); | |||||
this.transFactory = TransformerFactory.newInstance(); | |||||
this.transFactory.setURIResolver(this.uriResolver); | |||||
} | |||||
/** | |||||
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest, HttpServletResponse) | |||||
*/ | |||||
public void doGet(HttpServletRequest request, | |||||
HttpServletResponse response) throws ServletException { | |||||
if (log == null) { | |||||
log = new SimpleLog("FOP/Print Servlet"); | |||||
log.setLevel(SimpleLog.LOG_LEVEL_WARN); | |||||
} | |||||
try { | |||||
String foParam = request.getParameter(FO_REQUEST_PARAM); | |||||
String xmlParam = request.getParameter(XML_REQUEST_PARAM); | |||||
String xsltParam = request.getParameter(XSLT_REQUEST_PARAM); | |||||
if (foParam != null) { | |||||
InputStream file = new java.io.FileInputStream(foParam); | |||||
renderFO(file, response); | |||||
} else if ((xmlParam != null) && (xsltParam != null)) { | |||||
renderXML(new File(xmlParam), new File(xsltParam), response); | |||||
} else { | |||||
response.setContentType("text/html"); | |||||
PrintWriter out = response.getWriter(); | |||||
out.println("<html><title>Error</title>\n" | |||||
+ "<body><h1>FopServlet Error</h1>\n" | |||||
+ "<h3>No 'fo' or 'xml/xsl' " | |||||
+ "request param given.</h3></body>\n</html>"); | |||||
} | |||||
} catch (ServletException ex) { | |||||
throw ex; | |||||
} catch (Exception ex) { | |||||
throw new ServletException(ex); | |||||
} | |||||
} | |||||
/** | |||||
* Renders an FO inputsource to the default printer. | |||||
* @param foFile The XSL-FO file | |||||
* @param response Response to write to | |||||
* @throws ServletException In case of a problem | |||||
*/ | |||||
public void renderFO(InputStream foFile, | |||||
HttpServletResponse response) throws ServletException { | |||||
try { | |||||
Fop fop = new Fop(MimeConstants.MIME_FOP_PRINT, getFOUserAgent()); | |||||
// Setup JAXP | |||||
TransformerFactory factory = TransformerFactory.newInstance(); | |||||
Transformer transformer = factory.newTransformer(); //identity transformer | |||||
transformer.setURIResolver(this.uriResolver); | |||||
// Setup input for XSLT transformation | |||||
Source src = new StreamSource(foFile); | |||||
// Resulting SAX events (the generated FO) must be piped through to FOP | |||||
Result res = new SAXResult(fop.getDefaultHandler()); | |||||
// Start XSLT transformation and FOP processing | |||||
transformer.transform(src, res); | |||||
reportOK (response); | |||||
} catch (Exception ex) { | |||||
throw new ServletException(ex); | |||||
} | |||||
} | |||||
public class FopPrintServlet extends FopServlet { | |||||
/** | /** | ||||
* Renders an FO generated using an XML and a stylesheet to the default printer. | |||||
* @param xmlfile XML file object | |||||
* @param xsltfile XSLT stylesheet | |||||
* @param response HTTP response object | |||||
* @throws ServletException In case of a problem | |||||
* @see org.apache.fop.servlet.FopServlet#render(javax.xml.transform.Source, | |||||
* javax.xml.transform.Transformer, javax.servlet.http.HttpServletResponse) | |||||
*/ | */ | ||||
public void renderXML(File xmlfile, File xsltfile, | |||||
HttpServletResponse response) throws ServletException { | |||||
try { | |||||
Fop fop = new Fop(MimeConstants.MIME_FOP_PRINT, getFOUserAgent()); | |||||
protected void render(Source src, Transformer transformer, HttpServletResponse response) | |||||
throws FOPException, TransformerException, IOException { | |||||
// Setup XSLT | |||||
TransformerFactory factory = TransformerFactory.newInstance(); | |||||
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile)); | |||||
transformer.setURIResolver(this.uriResolver); | |||||
// Setup input for XSLT transformation | |||||
Source src = new StreamSource(xmlfile); | |||||
FOUserAgent foUserAgent = getFOUserAgent(); | |||||
// Resulting SAX events (the generated FO) must be piped through to FOP | |||||
Result res = new SAXResult(fop.getDefaultHandler()); | |||||
// Start XSLT transformation and FOP processing | |||||
transformer.transform(src, res); | |||||
reportOK (response); | |||||
} catch (Exception ex) { | |||||
throw new ServletException(ex); | |||||
} | |||||
//Setup FOP | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_PRINT, foUserAgent); | |||||
//Make sure the XSL transformation's result is piped through to FOP | |||||
Result res = new SAXResult(fop.getDefaultHandler()); | |||||
//Start the transformation and rendering process | |||||
transformer.transform(src, res); | |||||
//Return the result | |||||
reportOK(response); | |||||
} | } | ||||
// private helper, tell (browser) user that file printed | // private helper, tell (browser) user that file printed | ||||
private void reportOK(HttpServletResponse response) | |||||
throws ServletException { | |||||
private void reportOK(HttpServletResponse response) throws IOException { | |||||
String sMsg = "<html><title>Success</title>\n" | String sMsg = "<html><title>Success</title>\n" | ||||
+ "<body><h1>FopPrintServlet: </h1>" | + "<body><h1>FopPrintServlet: </h1>" | ||||
+ "<h3>The requested data was printed</h3></body></html>"; | |||||
+ "<h3>The requested data was printed to the default printer.</h3></body></html>"; | |||||
response.setContentType("text/html"); | response.setContentType("text/html"); | ||||
response.setContentLength(sMsg.length()); | response.setContentLength(sMsg.length()); | ||||
try { | |||||
PrintWriter out = response.getWriter(); | |||||
out.println(sMsg); | |||||
out.flush(); | |||||
} catch (Exception ex) { | |||||
throw new ServletException(ex); | |||||
} | |||||
} | |||||
/** @return a new FOUserAgent for FOP */ | |||||
protected FOUserAgent getFOUserAgent() { | |||||
FOUserAgent userAgent = new FOUserAgent(); | |||||
userAgent.setURIResolver(this.uriResolver); | |||||
return userAgent; | |||||
PrintWriter out = response.getWriter(); | |||||
out.println(sMsg); | |||||
out.flush(); | |||||
} | } | ||||
} | } |
package org.apache.fop.servlet; | package org.apache.fop.servlet; | ||||
import java.io.File; | import java.io.File; | ||||
import java.io.IOException; | |||||
import java.io.PrintWriter; | import java.io.PrintWriter; | ||||
import javax.servlet.ServletException; | import javax.servlet.ServletException; | ||||
import org.apache.commons.io.output.ByteArrayOutputStream; | import org.apache.commons.io.output.ByteArrayOutputStream; | ||||
import org.apache.commons.logging.impl.SimpleLog; | import org.apache.commons.logging.impl.SimpleLog; | ||||
//FOP | |||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
/** | /** | ||||
* For this to work with Internet Explorer, you might need to append "&ext=.pdf" | * For this to work with Internet Explorer, you might need to append "&ext=.pdf" | ||||
* to the URL. | * to the URL. | ||||
* | * | ||||
* @author <a href="mailto:fop-dev@xml.apache.org">Apache XML FOP Development Team</a> | |||||
* @author <a href="mailto:fop-dev@xmlgraphics.apache.org">Apache FOP Development Team</a> | |||||
* @version $Id$ | * @version $Id$ | ||||
* (todo) Ev. add caching mechanism for Templates objects | * (todo) Ev. add caching mechanism for Templates objects | ||||
*/ | */ | ||||
/** Logger to give to FOP */ | /** Logger to give to FOP */ | ||||
protected SimpleLog log = null; | protected SimpleLog log = null; | ||||
/** The TransformerFactory to use to create Transformer instances */ | |||||
/** The TransformerFactory used to create Transformer instances */ | |||||
protected TransformerFactory transFactory = null; | protected TransformerFactory transFactory = null; | ||||
/** The FopFactory used to create Fop instances */ | |||||
protected FopFactory fopFactory = null; | |||||
/** URIResolver for use by this servlet */ | /** URIResolver for use by this servlet */ | ||||
protected URIResolver uriResolver; | protected URIResolver uriResolver; | ||||
this.uriResolver = new ServletContextURIResolver(getServletContext()); | this.uriResolver = new ServletContextURIResolver(getServletContext()); | ||||
this.transFactory = TransformerFactory.newInstance(); | this.transFactory = TransformerFactory.newInstance(); | ||||
this.transFactory.setURIResolver(this.uriResolver); | this.transFactory.setURIResolver(this.uriResolver); | ||||
//Configure FopFactory as desired | |||||
this.fopFactory = FopFactory.newInstance(); | |||||
this.fopFactory.setURIResolver(this.uriResolver); | |||||
configureFopFactory(); | |||||
} | |||||
/** | |||||
* This method is called right after the FopFactory is instantiated and can be overridden | |||||
* by subclasses to perform additional configuration. | |||||
*/ | |||||
protected void configureFopFactory() { | |||||
//Subclass and override this method to perform additional configuration | |||||
} | } | ||||
/** | /** | ||||
String xsltParam = request.getParameter(XSLT_REQUEST_PARAM); | String xsltParam = request.getParameter(XSLT_REQUEST_PARAM); | ||||
//Analyze parameters and decide with method to use | //Analyze parameters and decide with method to use | ||||
byte[] content = null; | |||||
if (foParam != null) { | if (foParam != null) { | ||||
content = renderFO(foParam); | |||||
renderFO(foParam, response); | |||||
} else if ((xmlParam != null) && (xsltParam != null)) { | } else if ((xmlParam != null) && (xsltParam != null)) { | ||||
content = renderXML(xmlParam, xsltParam); | |||||
renderXML(xmlParam, xsltParam, response); | |||||
} else { | } else { | ||||
response.setContentType("text/html"); | |||||
PrintWriter out = response.getWriter(); | PrintWriter out = response.getWriter(); | ||||
out.println("<html><head><title>Error</title></head>\n" | out.println("<html><head><title>Error</title></head>\n" | ||||
+ "<body><h1>FopServlet Error</h1><h3>No 'fo' " | + "<body><h1>FopServlet Error</h1><h3>No 'fo' " | ||||
+ "request param given.</body></html>"); | + "request param given.</body></html>"); | ||||
} | } | ||||
if (content != null) { | |||||
//Send the result back to the client | |||||
response.setContentType("application/pdf"); | |||||
response.setContentLength(content.length); | |||||
response.getOutputStream().write(content); | |||||
response.getOutputStream().flush(); | |||||
} | |||||
} catch (Exception ex) { | } catch (Exception ex) { | ||||
throw new ServletException(ex); | throw new ServletException(ex); | ||||
} | } | ||||
return new StreamSource(new File(param)); | return new StreamSource(new File(param)); | ||||
} | } | ||||
private void sendPDF(byte[] content, HttpServletResponse response) throws IOException { | |||||
//Send the result back to the client | |||||
response.setContentType("application/pdf"); | |||||
response.setContentLength(content.length); | |||||
response.getOutputStream().write(content); | |||||
response.getOutputStream().flush(); | |||||
} | |||||
/** | /** | ||||
* Renders an XSL-FO file into a PDF file. The PDF is written to a byte | * Renders an XSL-FO file into a PDF file. The PDF is written to a byte | ||||
* array that is returned as the method's result. | * array that is returned as the method's result. | ||||
* @param fo the XSL-FO file | * @param fo the XSL-FO file | ||||
* @return byte[] the rendered PDF file | |||||
* @param response HTTP response object | |||||
* @throws FOPException If an error occurs during the rendering of the | * @throws FOPException If an error occurs during the rendering of the | ||||
* XSL-FO | * XSL-FO | ||||
* @throws TransformerException If an error occurs while parsing the input | * @throws TransformerException If an error occurs while parsing the input | ||||
* file | * file | ||||
* @throws IOException In case of an I/O problem | |||||
*/ | */ | ||||
protected byte[] renderFO(String fo) | |||||
throws FOPException, TransformerException { | |||||
protected void renderFO(String fo, HttpServletResponse response) | |||||
throws FOPException, TransformerException, IOException { | |||||
//Setup source | //Setup source | ||||
Source foSrc = convertString2Source(fo); | Source foSrc = convertString2Source(fo); | ||||
transformer.setURIResolver(this.uriResolver); | transformer.setURIResolver(this.uriResolver); | ||||
//Start transformation and rendering process | //Start transformation and rendering process | ||||
return render(foSrc, transformer); | |||||
render(foSrc, transformer, response); | |||||
} | } | ||||
/** | /** | ||||
* that is returned as the method's result. | * that is returned as the method's result. | ||||
* @param xml the XML file | * @param xml the XML file | ||||
* @param xslt the XSLT file | * @param xslt the XSLT file | ||||
* @return byte[] the rendered PDF file | |||||
* @param response HTTP response object | |||||
* @throws FOPException If an error occurs during the rendering of the | * @throws FOPException If an error occurs during the rendering of the | ||||
* XSL-FO | * XSL-FO | ||||
* @throws TransformerException If an error occurs during XSL | * @throws TransformerException If an error occurs during XSL | ||||
* transformation | * transformation | ||||
* @throws IOException In case of an I/O problem | |||||
*/ | */ | ||||
protected byte[] renderXML(String xml, String xslt) | |||||
throws FOPException, TransformerException { | |||||
protected void renderXML(String xml, String xslt, HttpServletResponse response) | |||||
throws FOPException, TransformerException, IOException { | |||||
//Setup sources | //Setup sources | ||||
Source xmlSrc = convertString2Source(xml); | Source xmlSrc = convertString2Source(xml); | ||||
transformer.setURIResolver(this.uriResolver); | transformer.setURIResolver(this.uriResolver); | ||||
//Start transformation and rendering process | //Start transformation and rendering process | ||||
return render(xmlSrc, transformer); | |||||
render(xmlSrc, transformer, response); | |||||
} | } | ||||
/** | /** | ||||
* returned as the method's result. | * returned as the method's result. | ||||
* @param src Input XML or XSL-FO | * @param src Input XML or XSL-FO | ||||
* @param transformer Transformer to use for optional transformation | * @param transformer Transformer to use for optional transformation | ||||
* @return byte[] the rendered PDF file | |||||
* @param response HTTP response object | |||||
* @throws FOPException If an error occurs during the rendering of the | * @throws FOPException If an error occurs during the rendering of the | ||||
* XSL-FO | * XSL-FO | ||||
* @throws TransformerException If an error occurs during XSL | * @throws TransformerException If an error occurs during XSL | ||||
* transformation | * transformation | ||||
* @throws IOException In case of an I/O problem | |||||
*/ | */ | ||||
protected byte[] render(Source src, Transformer transformer) | |||||
throws FOPException, TransformerException { | |||||
protected void render(Source src, Transformer transformer, HttpServletResponse response) | |||||
throws FOPException, TransformerException, IOException { | |||||
//Setup FOP | |||||
Fop fop = new Fop(MimeConstants.MIME_PDF, getFOUserAgent()); | |||||
FOUserAgent foUserAgent = getFOUserAgent(); | |||||
//Setup output | //Setup output | ||||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
fop.setOutputStream(out); | |||||
//Setup FOP | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); | |||||
//Make sure the XSL transformation's result is piped through to FOP | //Make sure the XSL transformation's result is piped through to FOP | ||||
Result res = new SAXResult(fop.getDefaultHandler()); | Result res = new SAXResult(fop.getDefaultHandler()); | ||||
transformer.transform(src, res); | transformer.transform(src, res); | ||||
//Return the result | //Return the result | ||||
return out.toByteArray(); | |||||
sendPDF(out.toByteArray(), response); | |||||
} | } | ||||
/** @return a new FOUserAgent for FOP */ | /** @return a new FOUserAgent for FOP */ | ||||
protected FOUserAgent getFOUserAgent() { | protected FOUserAgent getFOUserAgent() { | ||||
FOUserAgent userAgent = new FOUserAgent(); | |||||
userAgent.setURIResolver(this.uriResolver); | |||||
FOUserAgent userAgent = fopFactory.newFOUserAgent(); | |||||
//Configure foUserAgent as desired | |||||
return userAgent; | return userAgent; | ||||
} | } | ||||
/* | /* | ||||
* Copyright 1999-2005 The Apache Software Foundation. | |||||
* Copyright 1999-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import javax.xml.parsers.DocumentBuilder; | import javax.xml.parsers.DocumentBuilder; | ||||
import javax.xml.parsers.DocumentBuilderFactory; | import javax.xml.parsers.DocumentBuilderFactory; | ||||
import org.apache.fop.apps.Fop; | |||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.cli.InputHandler; | import org.apache.fop.cli.InputHandler; | ||||
*/ | */ | ||||
public class TestConverter { | public class TestConverter { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
private boolean failOnly = false; | private boolean failOnly = false; | ||||
private String outputFormat = MimeConstants.MIME_FOP_AREA_TREE; | private String outputFormat = MimeConstants.MIME_FOP_AREA_TREE; | ||||
private File destdir; | private File destdir; | ||||
+ xsl), null); | + xsl), null); | ||||
} | } | ||||
FOUserAgent userAgent = new FOUserAgent(); | |||||
FOUserAgent userAgent = fopFactory.newFOUserAgent(); | |||||
userAgent.setBaseURL(baseURL); | userAgent.setBaseURL(baseURL); | ||||
userAgent.getRendererOptions().put("fineDetail", new Boolean(false)); | userAgent.getRendererOptions().put("fineDetail", new Boolean(false)); |
// FOP | // FOP | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.cli.InputHandler; | import org.apache.fop.cli.InputHandler; | ||||
class FOPTaskStarter { | class FOPTaskStarter { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
private Fop task; | private Fop task; | ||||
private String baseURL = null; | private String baseURL = null; | ||||
boolean success = false; | boolean success = false; | ||||
try { | try { | ||||
FOUserAgent userAgent = new FOUserAgent(); | |||||
FOUserAgent userAgent = fopFactory.newFOUserAgent(); | |||||
userAgent.setBaseURL(this.baseURL); | userAgent.setBaseURL(this.baseURL); | ||||
if (userConfig != null) { | |||||
userAgent.setUserConfig(userConfig); | |||||
} | |||||
inputHandler.renderTo(userAgent, outputFormat, out); | inputHandler.renderTo(userAgent, outputFormat, out); | ||||
success = true; | success = true; | ||||
} catch (Exception ex) { | } catch (Exception ex) { |
/** the logger */ | /** the logger */ | ||||
private static Log log = LogFactory.getLog(ContentHandlerFactoryRegistry.class); | private static Log log = LogFactory.getLog(ContentHandlerFactoryRegistry.class); | ||||
private static ContentHandlerFactoryRegistry instance; | |||||
/** Map from namespace URIs to ContentHandlerFactories */ | /** Map from namespace URIs to ContentHandlerFactories */ | ||||
private Map factories = new java.util.HashMap(); | private Map factories = new java.util.HashMap(); | ||||
/** | |||||
* @return a singleton instance of the ContentHandlerFactoryRegistry. | |||||
*/ | |||||
public static ContentHandlerFactoryRegistry getInstance() { | |||||
if (instance == null) { | |||||
instance = new ContentHandlerFactoryRegistry(); | |||||
} | |||||
return instance; | |||||
} | |||||
/** | /** | ||||
* Default constructor. | * Default constructor. | ||||
*/ | */ |
/* | /* | ||||
* Copyright 1999-2005 The Apache Software Foundation. | |||||
* Copyright 1999-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import javax.xml.transform.stream.StreamSource; | import javax.xml.transform.stream.StreamSource; | ||||
import org.apache.commons.io.output.ByteArrayOutputStream; | import org.apache.commons.io.output.ByteArrayOutputStream; | ||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.cli.InputHandler; | import org.apache.fop.cli.InputHandler; | ||||
*/ | */ | ||||
public class BasicDriverTestCase extends AbstractFOPTestCase { | public class BasicDriverTestCase extends AbstractFOPTestCase { | ||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
/** | /** | ||||
* @see junit.framework.TestCase#TestCase(String) | * @see junit.framework.TestCase#TestCase(String) | ||||
*/ | */ | ||||
* @throws Exception if anything fails | * @throws Exception if anything fails | ||||
*/ | */ | ||||
public void testFO2PDFWithJAXP() throws Exception { | public void testFO2PDFWithJAXP() throws Exception { | ||||
FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | |||||
File foFile = new File(getBaseDir(), "test/xml/bugtests/block.fo"); | File foFile = new File(getBaseDir(), "test/xml/bugtests/block.fo"); | ||||
ByteArrayOutputStream baout = new ByteArrayOutputStream(); | ByteArrayOutputStream baout = new ByteArrayOutputStream(); | ||||
Fop fop = new Fop(MimeConstants.MIME_PDF); | |||||
fop.setOutputStream(baout); | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, baout); | |||||
TransformerFactory factory = TransformerFactory.newInstance(); | TransformerFactory factory = TransformerFactory.newInstance(); | ||||
Transformer transformer = factory.newTransformer(); //Identity transf. | Transformer transformer = factory.newTransformer(); //Identity transf. | ||||
* @throws Exception if anything fails | * @throws Exception if anything fails | ||||
*/ | */ | ||||
public void testFO2PSWithJAXP() throws Exception { | public void testFO2PSWithJAXP() throws Exception { | ||||
FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | |||||
File foFile = new File(getBaseDir(), "test/xml/bugtests/block.fo"); | File foFile = new File(getBaseDir(), "test/xml/bugtests/block.fo"); | ||||
ByteArrayOutputStream baout = new ByteArrayOutputStream(); | ByteArrayOutputStream baout = new ByteArrayOutputStream(); | ||||
Fop fop = new Fop(MimeConstants.MIME_POSTSCRIPT); | |||||
fop.setOutputStream(baout); | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_POSTSCRIPT, foUserAgent, baout); | |||||
TransformerFactory factory = TransformerFactory.newInstance(); | TransformerFactory factory = TransformerFactory.newInstance(); | ||||
Transformer transformer = factory.newTransformer(); //Identity transf. | Transformer transformer = factory.newTransformer(); //Identity transf. | ||||
* @throws Exception if anything fails | * @throws Exception if anything fails | ||||
*/ | */ | ||||
public void testFO2RTFWithJAXP() throws Exception { | public void testFO2RTFWithJAXP() throws Exception { | ||||
FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | |||||
File foFile = new File(getBaseDir(), "test/xml/bugtests/block.fo"); | File foFile = new File(getBaseDir(), "test/xml/bugtests/block.fo"); | ||||
ByteArrayOutputStream baout = new ByteArrayOutputStream(); | ByteArrayOutputStream baout = new ByteArrayOutputStream(); | ||||
Fop fop = new Fop(MimeConstants.MIME_RTF); | |||||
fop.setOutputStream(baout); | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_RTF, foUserAgent, baout); | |||||
TransformerFactory factory = TransformerFactory.newInstance(); | TransformerFactory factory = TransformerFactory.newInstance(); | ||||
Transformer transformer = factory.newTransformer(); //Identity transf. | Transformer transformer = factory.newTransformer(); //Identity transf. | ||||
* @throws Exception if anything fails | * @throws Exception if anything fails | ||||
*/ | */ | ||||
public void testFO2PDFWithXSLTInputHandler() throws Exception { | public void testFO2PDFWithXSLTInputHandler() throws Exception { | ||||
FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | |||||
File xmlFile = new File(getBaseDir(), "test/xml/1.xml"); | File xmlFile = new File(getBaseDir(), "test/xml/1.xml"); | ||||
File xsltFile = new File(getBaseDir(), "test/xsl/doc.xsl"); | File xsltFile = new File(getBaseDir(), "test/xsl/doc.xsl"); | ||||
ByteArrayOutputStream baout = new ByteArrayOutputStream(); | ByteArrayOutputStream baout = new ByteArrayOutputStream(); | ||||
InputHandler handler = new InputHandler(xmlFile, xsltFile, null); | InputHandler handler = new InputHandler(xmlFile, xsltFile, null); | ||||
handler.renderTo(null, MimeConstants.MIME_PDF, baout); | |||||
handler.renderTo(foUserAgent, MimeConstants.MIME_PDF, baout); | |||||
assertTrue("Generated PDF has zero length", baout.size() > 0); | assertTrue("Generated PDF has zero length", baout.size() > 0); | ||||
} | } |
/* | /* | ||||
* Copyright 1999-2005 The Apache Software Foundation. | |||||
* Copyright 1999-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.util.DigestFilter; | import org.apache.fop.util.DigestFilter; | ||||
import org.xml.sax.InputSource; | import org.xml.sax.InputSource; | ||||
*/ | */ | ||||
public final class GenericFOPTestCase extends TestCase { | public final class GenericFOPTestCase extends TestCase { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
protected SAXParserFactory parserFactory; | protected SAXParserFactory parserFactory; | ||||
public static Test suite() { | public static Test suite() { | ||||
private void renderPDF(String fo, String digestIn, String digestOut) | private void renderPDF(String fo, String digestIn, String digestOut) | ||||
throws Exception { | throws Exception { | ||||
FOUserAgent foUserAgent = new FOUserAgent(); | |||||
FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | |||||
foUserAgent.setCreationDate(new Date(10000)); | foUserAgent.setCreationDate(new Date(10000)); | ||||
MessageDigest outDigest = MessageDigest.getInstance("MD5"); | MessageDigest outDigest = MessageDigest.getInstance("MD5"); | ||||
DigestOutputStream out = | DigestOutputStream out = | ||||
new DigestOutputStream(new ByteArrayOutputStream(), outDigest); | new DigestOutputStream(new ByteArrayOutputStream(), outDigest); | ||||
Fop fop = new Fop(MimeConstants.MIME_PDF, foUserAgent); | |||||
fop.setOutputStream(out); | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); | |||||
InputSource source = new InputSource(new StringReader(fo)); | InputSource source = new InputSource(new StringReader(fo)); | ||||
DigestFilter filter = new DigestFilter("MD5"); | DigestFilter filter = new DigestFilter("MD5"); | ||||
filter.setParent(parserFactory.newSAXParser().getXMLReader()); | filter.setParent(parserFactory.newSAXParser().getXMLReader()); |
/* | /* | ||||
* Copyright 2005 The Apache Software Foundation. | |||||
* Copyright 2005-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.image.ImageFactory; | |||||
import org.apache.fop.render.xml.XMLRenderer; | import org.apache.fop.render.xml.XMLRenderer; | ||||
import org.apache.xpath.XPathAPI; | import org.apache.xpath.XPathAPI; | ||||
import org.apache.xpath.objects.XObject; | import org.apache.xpath.objects.XObject; | ||||
*/ | */ | ||||
public class URIResolutionTestCase extends AbstractFOPTestCase { | public class URIResolutionTestCase extends AbstractFOPTestCase { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
private SAXTransformerFactory tfactory | private SAXTransformerFactory tfactory | ||||
= (SAXTransformerFactory)SAXTransformerFactory.newInstance(); | = (SAXTransformerFactory)SAXTransformerFactory.newInstance(); | ||||
} | } | ||||
private void innerTestFO1(boolean withStream) throws Exception { | private void innerTestFO1(boolean withStream) throws Exception { | ||||
FOUserAgent ua = fopFactory.newFOUserAgent(); | |||||
//Reset the image caches to force URI resolution! | //Reset the image caches to force URI resolution! | ||||
ImageFactory.getInstance().clearCaches(); | |||||
ua.getFactory().getImageFactory().clearCaches(); | |||||
File foFile = new File(getBaseDir(), "test/xml/uri-resolution1.fo"); | File foFile = new File(getBaseDir(), "test/xml/uri-resolution1.fo"); | ||||
FOUserAgent ua = new FOUserAgent(); | |||||
MyURIResolver resolver = new MyURIResolver(withStream); | MyURIResolver resolver = new MyURIResolver(withStream); | ||||
ua.setURIResolver(resolver); | ua.setURIResolver(resolver); | ||||
ua.setBaseURL(foFile.getParentFile().toURL().toString()); | ua.setBaseURL(foFile.getParentFile().toURL().toString()); | ||||
//TODO This will only work when we can do URI resolution inside Batik! | //TODO This will only work when we can do URI resolution inside Batik! | ||||
File foFile = new File(getBaseDir(), "test/xml/uri-resolution2.fo"); | File foFile = new File(getBaseDir(), "test/xml/uri-resolution2.fo"); | ||||
FOUserAgent ua = new FOUserAgent(); | |||||
FOUserAgent ua = fopFactory.newFOUserAgent(); | |||||
MyURIResolver resolver = new MyURIResolver(false); | MyURIResolver resolver = new MyURIResolver(false); | ||||
ua.setURIResolver(resolver); | ua.setURIResolver(resolver); | ||||
ua.setBaseURL(foFile.getParentFile().toURL().toString()); | ua.setBaseURL(foFile.getParentFile().toURL().toString()); | ||||
Fop fop = new Fop(MimeConstants.MIME_PDF, ua); | |||||
ByteArrayOutputStream baout = new ByteArrayOutputStream(); | ByteArrayOutputStream baout = new ByteArrayOutputStream(); | ||||
fop.setOutputStream(baout); | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, ua, baout); | |||||
Transformer transformer = tfactory.newTransformer(); //Identity transf. | Transformer transformer = tfactory.newTransformer(); //Identity transf. | ||||
Source src = new StreamSource(foFile); | Source src = new StreamSource(foFile); | ||||
atrenderer.setContentHandler(athandler); | atrenderer.setContentHandler(athandler); | ||||
ua.setRendererOverride(atrenderer); | ua.setRendererOverride(atrenderer); | ||||
Fop fop = new Fop(MimeConstants.MIME_FOP_AREA_TREE, ua); | |||||
Fop fop = fopFactory.newFop(ua); | |||||
Transformer transformer = tfactory.newTransformer(); //Identity transf. | Transformer transformer = tfactory.newTransformer(); //Identity transf. | ||||
Source src = new StreamSource(fo); | Source src = new StreamSource(fo); |
/* | /* | ||||
* Copyright 2005 The Apache Software Foundation. | |||||
* Copyright 2005-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import javax.xml.parsers.SAXParser; | import javax.xml.parsers.SAXParser; | ||||
import javax.xml.parsers.SAXParserFactory; | import javax.xml.parsers.SAXParserFactory; | ||||
import javax.xml.transform.Source; | |||||
import javax.xml.transform.Transformer; | |||||
import javax.xml.transform.sax.SAXResult; | |||||
import javax.xml.transform.sax.SAXTransformerFactory; | |||||
import javax.xml.transform.stream.StreamSource; | |||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.MimeConstants; | |||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.fotreetest.ext.TestElementMapping; | import org.apache.fop.fotreetest.ext.TestElementMapping; | ||||
import org.xml.sax.InputSource; | |||||
import org.xml.sax.SAXException; | import org.xml.sax.SAXException; | ||||
import org.xml.sax.XMLReader; | import org.xml.sax.XMLReader; | ||||
import org.xml.sax.helpers.XMLFilterImpl; | import org.xml.sax.helpers.XMLFilterImpl; | ||||
*/ | */ | ||||
public class FOTreeTester { | public class FOTreeTester { | ||||
private SAXTransformerFactory tfactory | |||||
= (SAXTransformerFactory)SAXTransformerFactory.newInstance(); | |||||
/** | /** | ||||
* Runs a test. | * Runs a test. | ||||
* @param testFile the test file. | * @param testFile the test file. | ||||
XMLReader reader = parser.getXMLReader(); | XMLReader reader = parser.getXMLReader(); | ||||
//Setup FOP for area tree rendering | //Setup FOP for area tree rendering | ||||
FOUserAgent ua = new FOUserAgent(); | |||||
FopFactory fopFactory = FopFactory.newInstance(); | |||||
fopFactory.addElementMapping(new TestElementMapping()); | |||||
FOUserAgent ua = fopFactory.newFOUserAgent(); | |||||
ua.setBaseURL(testFile.getParentFile().toURL().toString()); | ua.setBaseURL(testFile.getParentFile().toURL().toString()); | ||||
ua.setFOEventHandlerOverride(new DummyFOEventHandler(ua)); | ua.setFOEventHandlerOverride(new DummyFOEventHandler(ua)); | ||||
ua.addElementMapping(new TestElementMapping()); | |||||
//Used to set values in the user agent through processing instructions | //Used to set values in the user agent through processing instructions | ||||
reader = new PIListener(reader, ua); | reader = new PIListener(reader, ua); | ||||
Fop fop = new Fop(MimeConstants.MIME_FOP_AREA_TREE, ua); | |||||
Fop fop = fopFactory.newFop(ua); | |||||
reader.setContentHandler(fop.getDefaultHandler()); | reader.setContentHandler(fop.getDefaultHandler()); | ||||
reader.setDTDHandler(fop.getDefaultHandler()); | reader.setDTDHandler(fop.getDefaultHandler()); | ||||
/** @see org.xml.sax.helpers.XMLFilterImpl */ | /** @see org.xml.sax.helpers.XMLFilterImpl */ | ||||
public void processingInstruction(String target, String data) throws SAXException { | public void processingInstruction(String target, String data) throws SAXException { | ||||
if ("fop-useragent-break-indent-inheritance".equals(target)) { | if ("fop-useragent-break-indent-inheritance".equals(target)) { | ||||
userAgent.setBreakIndentInheritanceOnReferenceAreaBoundary( | |||||
userAgent.getFactory().setBreakIndentInheritanceOnReferenceAreaBoundary( | |||||
Boolean.valueOf(data).booleanValue()); | Boolean.valueOf(data).booleanValue()); | ||||
} | } | ||||
super.processingInstruction(target, data); | super.processingInstruction(target, data); |
import org.apache.commons.io.output.ByteArrayOutputStream; | import org.apache.commons.io.output.ByteArrayOutputStream; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.fop.area.AreaTreeModel; | import org.apache.fop.area.AreaTreeModel; | ||||
import org.apache.fop.area.AreaTreeParser; | import org.apache.fop.area.AreaTreeParser; | ||||
*/ | */ | ||||
public class AreaTreeParserTestCase extends XMLTestCase { | public class AreaTreeParserTestCase extends XMLTestCase { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
private static SAXTransformerFactory tFactory | private static SAXTransformerFactory tFactory | ||||
= (SAXTransformerFactory)SAXTransformerFactory.newInstance(); | = (SAXTransformerFactory)SAXTransformerFactory.newInstance(); | ||||
private static Templates stylesheet = null; | private static Templates stylesheet = null; | ||||
} | } | ||||
private FOUserAgent createUserAgent() { | private FOUserAgent createUserAgent() { | ||||
FOUserAgent userAgent = new FOUserAgent(); | |||||
FOUserAgent userAgent = fopFactory.newFOUserAgent(); | |||||
try { | try { | ||||
userAgent.setBaseURL(testDir.toURL().toExternalForm()); | userAgent.setBaseURL(testDir.toURL().toExternalForm()); | ||||
} catch (MalformedURLException e) { | } catch (MalformedURLException e) { | ||||
userAgent.setRendererOverride(renderer); | userAgent.setRendererOverride(renderer); | ||||
Fop fop = new Fop(MimeConstants.MIME_FOP_AREA_TREE, userAgent); | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AREA_TREE, userAgent); | |||||
Result res = new SAXResult(fop.getDefaultHandler()); | Result res = new SAXResult(fop.getDefaultHandler()); | ||||
transformer.transform(src, res); | transformer.transform(src, res); | ||||
import java.io.File; | import java.io.File; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.lang.reflect.Constructor; | import java.lang.reflect.Constructor; | ||||
import java.net.MalformedURLException; | |||||
import java.util.Iterator; | import java.util.Iterator; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | import java.util.Map; | ||||
import javax.xml.transform.stream.StreamResult; | import javax.xml.transform.stream.StreamResult; | ||||
import javax.xml.transform.stream.StreamSource; | import javax.xml.transform.stream.StreamSource; | ||||
import org.apache.fop.apps.FOPException; | |||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.FormattingResults; | import org.apache.fop.apps.FormattingResults; | ||||
import org.apache.fop.apps.MimeConstants; | |||||
import org.apache.fop.layoutmgr.ElementListObserver; | import org.apache.fop.layoutmgr.ElementListObserver; | ||||
import org.apache.fop.render.xml.XMLRenderer; | import org.apache.fop.render.xml.XMLRenderer; | ||||
import org.apache.xpath.XPathAPI; | import org.apache.xpath.XPathAPI; | ||||
import org.w3c.dom.Element; | import org.w3c.dom.Element; | ||||
import org.w3c.dom.Node; | import org.w3c.dom.Node; | ||||
import org.w3c.dom.NodeList; | import org.w3c.dom.NodeList; | ||||
import org.xml.sax.InputSource; | |||||
import org.xml.sax.SAXException; | import org.xml.sax.SAXException; | ||||
/** | /** | ||||
private static final Map CHECK_CLASSES = new java.util.HashMap(); | private static final Map CHECK_CLASSES = new java.util.HashMap(); | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
private FopFactory fopFactoryWithBase14Kerning = FopFactory.newInstance(); | |||||
private SAXTransformerFactory tfactory | private SAXTransformerFactory tfactory | ||||
= (SAXTransformerFactory)SAXTransformerFactory.newInstance(); | = (SAXTransformerFactory)SAXTransformerFactory.newInstance(); | ||||
*/ | */ | ||||
public LayoutEngineTester(File areaTreeBackupDir) { | public LayoutEngineTester(File areaTreeBackupDir) { | ||||
this.areaTreeBackupDir = areaTreeBackupDir; | this.areaTreeBackupDir = areaTreeBackupDir; | ||||
fopFactory.setBase14KerningEnabled(false); | |||||
fopFactoryWithBase14Kerning.setBase14KerningEnabled(true); | |||||
} | } | ||||
private Templates getTestcase2FOStylesheet() throws TransformerConfigurationException { | private Templates getTestcase2FOStylesheet() throws TransformerConfigurationException { | ||||
XObject xo = XPathAPI.eval(testDoc, "/testcase/cfg/base14kerning"); | XObject xo = XPathAPI.eval(testDoc, "/testcase/cfg/base14kerning"); | ||||
String s = xo.str(); | String s = xo.str(); | ||||
boolean base14kerning = ("true".equalsIgnoreCase(s)); | boolean base14kerning = ("true".equalsIgnoreCase(s)); | ||||
FopFactory effFactory = (base14kerning ? fopFactoryWithBase14Kerning : fopFactory); | |||||
//Setup Transformer to convert the testcase XML to XSL-FO | //Setup Transformer to convert the testcase XML to XSL-FO | ||||
Transformer transformer = getTestcase2FOStylesheet().newTransformer(); | Transformer transformer = getTestcase2FOStylesheet().newTransformer(); | ||||
athandler.setResult(domres); | athandler.setResult(domres); | ||||
//Setup FOP for area tree rendering | //Setup FOP for area tree rendering | ||||
FOUserAgent ua = new FOUserAgent(); | |||||
FOUserAgent ua = effFactory.newFOUserAgent(); | |||||
ua.setBaseURL(testFile.getParentFile().toURL().toString()); | ua.setBaseURL(testFile.getParentFile().toURL().toString()); | ||||
ua.setBase14KerningEnabled(base14kerning); | |||||
XMLRenderer atrenderer = new XMLRenderer(); | XMLRenderer atrenderer = new XMLRenderer(); | ||||
atrenderer.setUserAgent(ua); | atrenderer.setUserAgent(ua); | ||||
atrenderer.setContentHandler(athandler); | atrenderer.setContentHandler(athandler); | ||||
ua.setRendererOverride(atrenderer); | ua.setRendererOverride(atrenderer); | ||||
fop = new Fop(MimeConstants.MIME_FOP_AREA_TREE, ua); | |||||
fop = effFactory.newFop(ua); | |||||
SAXResult fores = new SAXResult(fop.getDefaultHandler()); | SAXResult fores = new SAXResult(fop.getDefaultHandler()); | ||||
transformer.transform(src, fores); | transformer.transform(src, fores); |
/* | /* | ||||
* Copyright 2004-2005 The Apache Software Foundation. | |||||
* Copyright 2004-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import org.apache.avalon.framework.configuration.Configuration; | import org.apache.avalon.framework.configuration.Configuration; | ||||
import org.apache.avalon.framework.configuration.ConfigurationException; | import org.apache.avalon.framework.configuration.ConfigurationException; | ||||
import org.apache.fop.apps.FOUserAgent; | |||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FOPException; | import org.apache.fop.apps.FOPException; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
import org.apache.avalon.framework.activity.Initializable; | import org.apache.avalon.framework.activity.Initializable; | ||||
public class FOProcessorImpl extends AbstractLogEnabled | public class FOProcessorImpl extends AbstractLogEnabled | ||||
implements FOProcessor, Configurable, Initializable { | implements FOProcessor, Configurable, Initializable { | ||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
private TransformerFactory factory = TransformerFactory.newInstance(); | |||||
private String baseDir; | private String baseDir; | ||||
private String fontBaseDir; | private String fontBaseDir; | ||||
private String userconfig; | private String userconfig; | ||||
public void process(InputStream in, Templates templates, OutputStream out) | public void process(InputStream in, Templates templates, OutputStream out) | ||||
throws org.apache.fop.apps.FOPException, java.io.IOException { | throws org.apache.fop.apps.FOPException, java.io.IOException { | ||||
Fop fop = new Fop(MimeConstants.MIME_PDF); | |||||
fop.setOutputStream(out); | |||||
FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); | |||||
try { | try { | ||||
Transformer transformer; | Transformer transformer; | ||||
if (templates == null) { | if (templates == null) { | ||||
TransformerFactory factory = TransformerFactory.newInstance(); | |||||
transformer = factory.newTransformer(); | transformer = factory.newTransformer(); | ||||
} else { | } else { | ||||
transformer = templates.newTransformer(); | transformer = templates.newTransformer(); |
/* | /* | ||||
* Copyright 2005 The Apache Software Foundation. | |||||
* Copyright 2005-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import org.apache.commons.io.IOUtils; | import org.apache.commons.io.IOUtils; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FopFactory; | |||||
/** | /** | ||||
* BitmapProducer implementation that uses the PS or PDF renderer and an external converter | * BitmapProducer implementation that uses the PS or PDF renderer and an external converter | ||||
public abstract class AbstractPSPDFBitmapProducer extends AbstractBitmapProducer | public abstract class AbstractPSPDFBitmapProducer extends AbstractBitmapProducer | ||||
implements Configurable { | implements Configurable { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
private String converter; | private String converter; | ||||
private boolean deleteTempFiles; | private boolean deleteTempFiles; | ||||
/** @see org.apache.fop.visual.BitmapProducer */ | /** @see org.apache.fop.visual.BitmapProducer */ | ||||
public BufferedImage produce(File src, ProducerContext context) { | public BufferedImage produce(File src, ProducerContext context) { | ||||
try { | try { | ||||
FOUserAgent userAgent = new FOUserAgent(); | |||||
FOUserAgent userAgent = fopFactory.newFOUserAgent(); | |||||
userAgent.setTargetResolution(context.getTargetResolution()); | userAgent.setTargetResolution(context.getTargetResolution()); | ||||
userAgent.setBaseURL(src.getParentFile().toURL().toString()); | userAgent.setBaseURL(src.getParentFile().toURL().toString()); | ||||
OutputStream out = new FileOutputStream(tempOut); | OutputStream out = new FileOutputStream(tempOut); | ||||
out = new BufferedOutputStream(out); | out = new BufferedOutputStream(out); | ||||
try { | try { | ||||
Fop fop = new Fop(getTargetFormat(), userAgent); | |||||
fop.setOutputStream(out); | |||||
Fop fop = fopFactory.newFop(getTargetFormat(), userAgent, out); | |||||
SAXResult res = new SAXResult(fop.getDefaultHandler()); | SAXResult res = new SAXResult(fop.getDefaultHandler()); | ||||
Transformer transformer = getTransformer(context); | Transformer transformer = getTransformer(context); |
/* | /* | ||||
* Copyright 2005 The Apache Software Foundation. | |||||
* Copyright 2005-2006 The Apache Software Foundation. | |||||
* | * | ||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
import org.apache.commons.io.IOUtils; | import org.apache.commons.io.IOUtils; | ||||
import org.apache.fop.apps.FOUserAgent; | import org.apache.fop.apps.FOUserAgent; | ||||
import org.apache.fop.apps.Fop; | import org.apache.fop.apps.Fop; | ||||
import org.apache.fop.apps.FopFactory; | |||||
import org.apache.fop.apps.MimeConstants; | import org.apache.fop.apps.MimeConstants; | ||||
/** | /** | ||||
*/ | */ | ||||
public class BitmapProducerJava2D extends AbstractBitmapProducer implements Configurable { | public class BitmapProducerJava2D extends AbstractBitmapProducer implements Configurable { | ||||
// configure fopFactory as desired | |||||
private FopFactory fopFactory = FopFactory.newInstance(); | |||||
private boolean deleteTempFiles; | private boolean deleteTempFiles; | ||||
/** @see org.apache.avalon.framework.configuration.Configurable */ | /** @see org.apache.avalon.framework.configuration.Configurable */ | ||||
/** @see org.apache.fop.visual.BitmapProducer */ | /** @see org.apache.fop.visual.BitmapProducer */ | ||||
public BufferedImage produce(File src, ProducerContext context) { | public BufferedImage produce(File src, ProducerContext context) { | ||||
try { | try { | ||||
FOUserAgent userAgent = new FOUserAgent(); | |||||
FOUserAgent userAgent = fopFactory.newFOUserAgent(); | |||||
userAgent.setTargetResolution(context.getTargetResolution()); | userAgent.setTargetResolution(context.getTargetResolution()); | ||||
userAgent.setBaseURL(src.getParentFile().toURL().toString()); | userAgent.setBaseURL(src.getParentFile().toURL().toString()); | ||||
OutputStream out = new FileOutputStream(outputFile); | OutputStream out = new FileOutputStream(outputFile); | ||||
out = new BufferedOutputStream(out); | out = new BufferedOutputStream(out); | ||||
try { | try { | ||||
Fop fop = new Fop(MimeConstants.MIME_PNG, userAgent); | |||||
fop.setOutputStream(out); | |||||
Fop fop = fopFactory.newFop(MimeConstants.MIME_PNG, userAgent, out); | |||||
SAXResult res = new SAXResult(fop.getDefaultHandler()); | SAXResult res = new SAXResult(fop.getDefaultHandler()); | ||||
Transformer transformer = getTransformer(context); | Transformer transformer = getTransformer(context); |