From: Jeremias Maerki Date: Wed, 12 Mar 2003 11:14:55 +0000 (+0000) Subject: Embedding examples ported from maintenance branch. X-Git-Tag: Alt-Design-integration-base~33 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2ac9fa5376d798152bdb1d1e590a4d393b64c94e;p=xmlgraphics-fop.git Embedding examples ported from maintenance branch. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@196077 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/examples/embedding/.cvsignore b/examples/embedding/.cvsignore new file mode 100644 index 000000000..9bcdd5aba --- /dev/null +++ b/examples/embedding/.cvsignore @@ -0,0 +1,2 @@ +build +out diff --git a/examples/embedding/README b/examples/embedding/README new file mode 100644 index 000000000..eb17f212f --- /dev/null +++ b/examples/embedding/README @@ -0,0 +1,17 @@ +FOP Embedding Examples Readme + +This directory contains several examples on using FOP in Java. + +To compile the examples you can use the build.bat/build.sh scripts. +You can also directly run the individual examples with these scripts. +Just call + +Windows: build -projecthelp +Unix: ./build.sh -projecthelp + +for a listing of all available examples. + + + +Please go to the page indicated below to find more information: +http://xml.apache.org/fop/embedding.html diff --git a/examples/embedding/build.bat b/examples/embedding/build.bat new file mode 100755 index 000000000..2ae5bf0a4 --- /dev/null +++ b/examples/embedding/build.bat @@ -0,0 +1,34 @@ +@echo off + +echo Fop Build System +echo ---------------- + +if "%JAVA_HOME%" == "" goto error + +set LIBDIR=../../lib +set LOCALCLASSPATH=%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\lib\classes.zip +set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\ant.jar +set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xml-apis.jar +set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xercesImpl-2.2.1.jar +set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xalan-2.4.1.jar + +set ANT_HOME=%LIBDIR% + +echo Building with classpath %LOCALCLASSPATH% + +echo Starting Ant... + +%JAVA_HOME%\bin\java.exe -Dant.home=%ANT_HOME% -classpath "%LOCALCLASSPATH%" org.apache.tools.ant.Main %1 %2 %3 %4 %5 + +goto end + +:error + +echo ERROR: JAVA_HOME not found in your environment. +echo Please, set the JAVA_HOME variable in your environment to match the +echo location of the Java Virtual Machine you want to use. + +:end + +rem set LOCALCLASSPATH= + diff --git a/examples/embedding/build.sh b/examples/embedding/build.sh new file mode 100644 index 000000000..f835d05b5 --- /dev/null +++ b/examples/embedding/build.sh @@ -0,0 +1,29 @@ +#!/bin/sh +# This file should be executable. +echo +echo "Fop Build System" +echo "----------------" +echo + +if [ "$JAVA_HOME" = "" ] ; then + echo "ERROR: JAVA_HOME not found in your environment." + echo + echo "Please, set the JAVA_HOME variable in your environment to match the" + echo "location of the Java Virtual Machine you want to use." + exit 1 +fi +LIBDIR=../../lib +LOCALCLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/classes.zip +LOCALCLASSPATH=$LOCALCLASSPATH:$LIBDIR/ant.jar +LOCALCLASSPATH=$LOCALCLASSPATH:$LIBDIR/xml-apis.jar +LOCALCLASSPATH=$LOCALCLASSPATH:$LIBDIR/xercesImpl-2.2.1.jar +LOCALCLASSPATH=$LOCALCLASSPATH:$LIBDIR/xalan-2.4.1.jar + +ANT_HOME=$LIBDIR + +echo +echo Building with classpath $LOCALCLASSPATH +echo Starting Ant... +echo + +$JAVA_HOME/bin/java -Dant.home=$ANT_HOME -classpath "$LOCALCLASSPATH" org.apache.tools.ant.Main $* diff --git a/examples/embedding/build.xml b/examples/embedding/build.xml new file mode 100644 index 000000000..b201e50ec --- /dev/null +++ b/examples/embedding/build.xml @@ -0,0 +1,111 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/embedding/java/embedding/ExampleFO2PDF.java b/examples/embedding/java/embedding/ExampleFO2PDF.java new file mode 100644 index 000000000..6171e031d --- /dev/null +++ b/examples/embedding/java/embedding/ExampleFO2PDF.java @@ -0,0 +1,149 @@ +/* + * $Id: ExampleFO2PDF.java,v 1.1.2.2 2003/02/25 16:06:34 jeremias Exp $ + * ============================================================================ + * The Apache Software License, Version 1.1 + * ============================================================================ + * + * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowledgment: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, if + * and wherever such third-party acknowledgments normally appear. + * + * 4. The names "FOP" and "Apache Software Foundation" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", nor may + * "Apache" appear in their name, without prior written permission of the + * Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ============================================================================ + * + * This software consists of voluntary contributions made by many individuals + * on behalf of the Apache Software Foundation and was originally created by + * James Tauber . For more information on the Apache + * Software Foundation, please see . + */ +package embedding; + +//Java +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +//SAX +import org.xml.sax.InputSource; + +//Avalon +import org.apache.avalon.framework.ExceptionUtil; +import org.apache.avalon.framework.logger.Logger; +import org.apache.avalon.framework.logger.ConsoleLogger; + +//FOP +import org.apache.fop.apps.Driver; +import org.apache.fop.apps.FOPException; + +/** + * This class demonstrates the conversion of an FO file to PDF using FOP. + */ +public class ExampleFO2PDF { + + /** + * Converts an FO file to a PDF file using FOP + * @param fo the FO 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 convertFO2PDF(File fo, File pdf) throws IOException, FOPException { + + //Construct driver + Driver driver = new Driver(); + + //Setup logger + Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO); + driver.enableLogging(logger); + driver.initialize(); + + //Setup Renderer (output format) + driver.setRenderer(Driver.RENDER_PDF); + + //Setup output + OutputStream out = new java.io.FileOutputStream(pdf); + try { + driver.setOutputStream(out); + + //Setup input + InputStream in = new java.io.FileInputStream(fo); + try { + driver.setInputSource(new InputSource(in)); + + //Process FO + driver.run(); + } finally { + in.close(); + } + } finally { + out.close(); + } + } + + + /** + * Main method. + * @param args command-line arguments + */ + public static void main(String[] args) { + try { + System.out.println("FOP ExampleFO2PDF\n"); + System.out.println("Preparing..."); + + //Setup directories + File baseDir = new File("."); + File outDir = new File(baseDir, "out"); + outDir.mkdirs(); + + //Setup input and output files + File fofile = new File(baseDir, "xml/fo/helloworld.fo"); + File pdffile = new File(outDir, "ResultFO2PDF.pdf"); + + System.out.println("Input: XSL-FO (" + fofile + ")"); + System.out.println("Output: PDF (" + pdffile + ")"); + System.out.println(); + System.out.println("Transforming..."); + + ExampleFO2PDF app = new ExampleFO2PDF(); + app.convertFO2PDF(fofile, pdffile); + + System.out.println("Success!"); + } catch (Exception e) { + System.err.println(ExceptionUtil.printStackTrace(e)); + System.exit(-1); + } + } +} diff --git a/examples/embedding/java/embedding/ExampleObj2PDF.java b/examples/embedding/java/embedding/ExampleObj2PDF.java new file mode 100644 index 000000000..591142eff --- /dev/null +++ b/examples/embedding/java/embedding/ExampleObj2PDF.java @@ -0,0 +1,163 @@ +/* + * $Id: ExampleObj2PDF.java,v 1.1.2.2 2003/02/25 16:06:34 jeremias Exp $ + * ============================================================================ + * The Apache Software License, Version 1.1 + * ============================================================================ + * + * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowledgment: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, if + * and wherever such third-party acknowledgments normally appear. + * + * 4. The names "FOP" and "Apache Software Foundation" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", nor may + * "Apache" appear in their name, without prior written permission of the + * Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ============================================================================ + * + * This software consists of voluntary contributions made by many individuals + * on behalf of the Apache Software Foundation and was originally created by + * James Tauber . For more information on the Apache + * Software Foundation, please see . + */ +package embedding; + +//Java +import java.io.File; +import java.io.OutputStream; +import java.io.IOException; + +//JAXP +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerException; +import javax.xml.transform.Source; +import javax.xml.transform.Result; +import javax.xml.transform.stream.StreamSource; +import javax.xml.transform.sax.SAXResult; + +//Avalon +import org.apache.avalon.framework.ExceptionUtil; +import org.apache.avalon.framework.logger.ConsoleLogger; +import org.apache.avalon.framework.logger.Logger; + +//FOP +import org.apache.fop.apps.Driver; +import org.apache.fop.apps.FOPException; + +import embedding.model.ProjectTeam; + +/** + * This class demonstrates the conversion of an arbitrary object file to a + * PDF using JAXP (XSLT) and FOP (XSL:FO). + */ +public class ExampleObj2PDF { + + /** + * Converts a ProjectTeam object to a PDF file. + * @param team the ProjectTeam object + * @param xslt the stylesheet file + * @param pdf the target PDF file + * @throws IOException In case of an I/O problem + * @throws FOPException In case of a FOP problem + * @throws TransformerException In case of a XSL transformation problem + */ + public void convertProjectTeam2PDF(ProjectTeam team, File xslt, File pdf) + throws IOException, FOPException, TransformerException { + + //Construct driver + Driver driver = new Driver(); + + //Setup logger + Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO); + driver.enableLogging(logger); + driver.initialize(); + + //Setup Renderer (output format) + driver.setRenderer(Driver.RENDER_PDF); + + //Setup output + OutputStream out = new java.io.FileOutputStream(pdf); + try { + driver.setOutputStream(out); + + //Setup XSLT + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(new StreamSource(xslt)); + + //Setup input for XSLT transformation + Source src = team.getSourceForProjectTeam(); + + //Resulting SAX events (the generated FO) must be piped through to FOP + Result res = new SAXResult(driver.getContentHandler()); + + //Start XSLT transformation and FOP processing + transformer.transform(src, res); + } finally { + out.close(); + } + } + + + /** + * Main method. + * @param args command-line arguments + */ + public static void main(String[] args) { + try { + System.out.println("FOP ExampleObj2PDF\n"); + System.out.println("Preparing..."); + + //Setup directories + File baseDir = new File("."); + File outDir = new File(baseDir, "out"); + outDir.mkdirs(); + + //Setup input and output + File xsltfile = new File(baseDir, "xml/xslt/projectteam2FO.xsl"); + File pdffile = new File(outDir, "ResultObj2PDF.pdf"); + + System.out.println("Input: a ProjectTeam object"); + System.out.println("Stylesheet: " + xsltfile); + System.out.println("Output: PDF (" + pdffile + ")"); + System.out.println(); + System.out.println("Transforming..."); + + ExampleObj2PDF app = new ExampleObj2PDF(); + app.convertProjectTeam2PDF(ExampleObj2XML.createSampleProjectTeam(), xsltfile, pdffile); + + System.out.println("Success!"); + } catch (Exception e) { + System.err.println(ExceptionUtil.printStackTrace(e)); + System.exit(-1); + } + } +} diff --git a/examples/embedding/java/embedding/ExampleObj2XML.java b/examples/embedding/java/embedding/ExampleObj2XML.java new file mode 100644 index 000000000..ffecf6209 --- /dev/null +++ b/examples/embedding/java/embedding/ExampleObj2XML.java @@ -0,0 +1,157 @@ +/* + * $Id: ExampleObj2XML.java,v 1.1.2.2 2003/02/25 16:06:34 jeremias Exp $ + * ============================================================================ + * The Apache Software License, Version 1.1 + * ============================================================================ + * + * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowledgment: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, if + * and wherever such third-party acknowledgments normally appear. + * + * 4. The names "FOP" and "Apache Software Foundation" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", nor may + * "Apache" appear in their name, without prior written permission of the + * Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ============================================================================ + * + * This software consists of voluntary contributions made by many individuals + * on behalf of the Apache Software Foundation and was originally created by + * James Tauber . For more information on the Apache + * Software Foundation, please see . + */ +package embedding; + +//Hava +import java.io.File; +import java.io.IOException; + +//JAXP +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerException; +import javax.xml.transform.Source; +import javax.xml.transform.Result; +import javax.xml.transform.stream.StreamResult; + +//Avalon +import org.apache.avalon.framework.ExceptionUtil; + +import embedding.model.ProjectMember; +import embedding.model.ProjectTeam; + + +/** + * This class demonstrates the conversion of an arbitrary object file to an + * XML file. + */ +public class ExampleObj2XML { + + /** + * Converts a ProjectTeam object to XML. + * @param team the ProjectTeam object + * @param xml the target XML file + * @throws IOException In case of an I/O problem + * @throws TransformerException In case of a XSL transformation problem + */ + public void convertProjectTeam2XML(ProjectTeam team, File xml) + throws IOException, TransformerException { + + //Setup XSLT + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(); + /* Note: + We use the identity transformer, no XSL transformation is done. + The transformer is basically just used to serialize the + generated document to XML. */ + + //Setup input + Source src = team.getSourceForProjectTeam(); + + //Setup output + Result res = new StreamResult(xml); + + //Start XSLT transformation + transformer.transform(src, res); + } + + + /** + * Creates a sample ProjectTeam instance for this demo. + * @return ProjectTeam the newly created ProjectTeam instance + */ + public static ProjectTeam createSampleProjectTeam() { + ProjectTeam team = new ProjectTeam(); + team.setProjectName("Rule the Galaxy"); + team.addMember(new ProjectMember( + "Emperor Palpatine", "lead", "palpatine@empire.gxy")); + team.addMember(new ProjectMember( + "Lord Darth Vader", "Jedi-Killer", "vader@empire.gxy")); + team.addMember(new ProjectMember( + "Grand Moff Tarkin", "Planet-Killer", "tarkin@empire.gxy")); + team.addMember(new ProjectMember( + "Admiral Motti", "Death Star operations", "motti@empire.gxy")); + return team; + } + + + /** + * Main method. + * @param args command-line arguments + */ + public static void main(String[] args) { + try { + System.out.println("FOP ExampleObj2XML\n"); + System.out.println("Preparing..."); + + //Setup directories + File baseDir = new File("."); + File outDir = new File(baseDir, "out"); + outDir.mkdirs(); + + //Setup input and output + File xmlfile = new File(outDir, "ResultObj2XML.xml"); + + System.out.println("Input: a ProjectTeam object"); + System.out.println("Output: XML (" + xmlfile + ")"); + System.out.println(); + System.out.println("Serializing..."); + + ExampleObj2XML app = new ExampleObj2XML(); + app.convertProjectTeam2XML(createSampleProjectTeam(), xmlfile); + + System.out.println("Success!"); + } catch (Exception e) { + System.err.println(ExceptionUtil.printStackTrace(e)); + System.exit(-1); + } + } +} diff --git a/examples/embedding/java/embedding/ExampleXML2FO.java b/examples/embedding/java/embedding/ExampleXML2FO.java new file mode 100644 index 000000000..acd9d480f --- /dev/null +++ b/examples/embedding/java/embedding/ExampleXML2FO.java @@ -0,0 +1,142 @@ +/* + * $Id: ExampleXML2FO.java,v 1.1.2.2 2003/02/25 16:06:34 jeremias Exp $ + * ============================================================================ + * The Apache Software License, Version 1.1 + * ============================================================================ + * + * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowledgment: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, if + * and wherever such third-party acknowledgments normally appear. + * + * 4. The names "FOP" and "Apache Software Foundation" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", nor may + * "Apache" appear in their name, without prior written permission of the + * Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ============================================================================ + * + * This software consists of voluntary contributions made by many individuals + * on behalf of the Apache Software Foundation and was originally created by + * James Tauber . For more information on the Apache + * Software Foundation, please see . + */ +package embedding; + +//Java +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; + +//JAXP +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerException; +import javax.xml.transform.Source; +import javax.xml.transform.Result; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +//Avalon +import org.apache.avalon.framework.ExceptionUtil; + +/** + * This class demonstrates the conversion of an XML file to an XSL-FO file + * using JAXP (XSLT). + */ +public class ExampleXML2FO { + + /** + * Converts an XML file to an XSL-FO file using JAXP (XSLT). + * @param xml the XML file + * @param xslt the stylesheet file + * @param fo the target XSL-FO file + * @throws IOException In case of an I/O problem + * @throws TransformerException In case of a XSL transformation problem + */ + public void convertXML2FO(File xml, File xslt, File fo) + throws IOException, TransformerException { + + //Setup output + OutputStream out = new java.io.FileOutputStream(fo); + try { + //Setup XSLT + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(new StreamSource(xslt)); + + //Setup input for XSLT transformation + Source src = new StreamSource(xml); + + //Resulting SAX events (the generated FO) must be piped through to FOP + Result res = new StreamResult(out); + + //Start XSLT transformation and FOP processing + transformer.transform(src, res); + } finally { + out.close(); + } + } + + + /** + * Main method. + * @param args command-line arguments + */ + public static void main(String[] args) { + try { + System.out.println("FOP ExampleXML2FO\n"); + System.out.println("Preparing..."); + + //Setup directories + File baseDir = new File("."); + File outDir = new File(baseDir, "out"); + outDir.mkdirs(); + + //Setup input and output files + File xmlfile = new File(baseDir, "xml/xml/projectteam.xml"); + File xsltfile = new File(baseDir, "xml/xslt/projectteam2FO.xsl"); + File fofile = new File(outDir, "ResultXML2FO.fo"); + + System.out.println("Input: XML (" + xmlfile + ")"); + System.out.println("Stylesheet: " + xsltfile); + System.out.println("Output: XSL-FO (" + fofile + ")"); + System.out.println(); + System.out.println("Transforming..."); + + ExampleXML2FO app = new ExampleXML2FO(); + app.convertXML2FO(xmlfile, xsltfile, fofile); + + System.out.println("Success!"); + } catch (Exception e) { + System.err.println(ExceptionUtil.printStackTrace(e)); + System.exit(-1); + } + } +} diff --git a/examples/embedding/java/embedding/ExampleXML2PDF.java b/examples/embedding/java/embedding/ExampleXML2PDF.java new file mode 100644 index 000000000..186e6b087 --- /dev/null +++ b/examples/embedding/java/embedding/ExampleXML2PDF.java @@ -0,0 +1,161 @@ +/* + * $Id: ExampleXML2PDF.java,v 1.1.2.2 2003/02/25 16:06:34 jeremias Exp $ + * ============================================================================ + * The Apache Software License, Version 1.1 + * ============================================================================ + * + * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowledgment: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, if + * and wherever such third-party acknowledgments normally appear. + * + * 4. The names "FOP" and "Apache Software Foundation" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", nor may + * "Apache" appear in their name, without prior written permission of the + * Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ============================================================================ + * + * This software consists of voluntary contributions made by many individuals + * on behalf of the Apache Software Foundation and was originally created by + * James Tauber . For more information on the Apache + * Software Foundation, please see . + */ +package embedding; + +//Java +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; + +//JAXP +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerException; +import javax.xml.transform.Source; +import javax.xml.transform.Result; +import javax.xml.transform.stream.StreamSource; +import javax.xml.transform.sax.SAXResult; + +//Avalon +import org.apache.avalon.framework.ExceptionUtil; +import org.apache.avalon.framework.logger.ConsoleLogger; +import org.apache.avalon.framework.logger.Logger; + +//FOP +import org.apache.fop.apps.Driver; +import org.apache.fop.apps.FOPException; + +/** + * This class demonstrates the conversion of an XML file to PDF using + * JAXP (XSLT) and FOP (XSL-FO). + */ +public class ExampleXML2PDF { + + /** + * Converts an XML file to a PDF file using JAXP and FOP. + * @param xml the XML file + * @param xslt the stylesheet file + * @param pdf the target PDF file + * @throws IOException In case of an I/O problem + * @throws FOPException In case of a FOP problem + * @throws TransformerException In case of a XSL transformation problem + */ + public void convertXML2PDF(File xml, File xslt, File pdf) + throws IOException, FOPException, TransformerException { + //Construct driver + Driver driver = new Driver(); + + //Setup logger + Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO); + driver.enableLogging(logger); + driver.initialize(); + + //Setup Renderer (output format) + driver.setRenderer(Driver.RENDER_PDF); + + //Setup output + OutputStream out = new java.io.FileOutputStream(pdf); + try { + driver.setOutputStream(out); + + //Setup XSLT + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(new StreamSource(xslt)); + + //Setup input for XSLT transformation + Source src = new StreamSource(xml); + + //Resulting SAX events (the generated FO) must be piped through to FOP + Result res = new SAXResult(driver.getContentHandler()); + + //Start XSLT transformation and FOP processing + transformer.transform(src, res); + } finally { + out.close(); + } + } + + + /** + * Main method. + * @param args command-line arguments + */ + public static void main(String[] args) { + try { + System.out.println("FOP ExampleXML2PDF\n"); + System.out.println("Preparing..."); + + //Setup directories + File baseDir = new File("."); + File outDir = new File(baseDir, "out"); + outDir.mkdirs(); + + //Setup input and output files + File xmlfile = new File(baseDir, "xml/xml/projectteam.xml"); + File xsltfile = new File(baseDir, "xml/xslt/projectteam2FO.xsl"); + File pdffile = new File(outDir, "ResultXML2PDF.pdf"); + + System.out.println("Input: XML (" + xmlfile + ")"); + System.out.println("Stylesheet: " + xsltfile); + System.out.println("Output: PDF (" + pdffile + ")"); + System.out.println(); + System.out.println("Transforming..."); + + ExampleXML2PDF app = new ExampleXML2PDF(); + app.convertXML2PDF(xmlfile, xsltfile, pdffile); + + System.out.println("Success!"); + } catch (Exception e) { + System.err.println(ExceptionUtil.printStackTrace(e)); + System.exit(-1); + } + } +} diff --git a/examples/embedding/java/embedding/model/ProjectMember.java b/examples/embedding/java/embedding/model/ProjectMember.java new file mode 100644 index 000000000..f5d86ea73 --- /dev/null +++ b/examples/embedding/java/embedding/model/ProjectMember.java @@ -0,0 +1,135 @@ +/* + * $Id: ProjectMember.java,v 1.1.2.2 2003/02/25 16:06:34 jeremias Exp $ + * ============================================================================ + * The Apache Software License, Version 1.1 + * ============================================================================ + * + * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowledgment: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, if + * and wherever such third-party acknowledgments normally appear. + * + * 4. The names "FOP" and "Apache Software Foundation" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", nor may + * "Apache" appear in their name, without prior written permission of the + * Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ============================================================================ + * + * This software consists of voluntary contributions made by many individuals + * on behalf of the Apache Software Foundation and was originally created by + * James Tauber . For more information on the Apache + * Software Foundation, please see . + */ +package embedding.model; + +/** + * This bean represents a project member. + */ +public class ProjectMember { + + private String name; + private String function; + private String email; + + + /** + * Default no-parameter constructor. + */ + public ProjectMember() { + } + + + /** + * Convenience constructor. + * @param name name of the project member + * @param function function in the team + * @param email email address + */ + public ProjectMember(String name, String function, String email) { + setName(name); + setFunction(function); + setEmail(email); + } + + /** + * Returns the name. + * @return String the name + */ + public String getName() { + return name; + } + + + /** + * Returns the function. + * @return String the function + */ + public String getFunction() { + return function; + } + + + /** + * Returns the email address. + * @return String the email address + */ + public String getEmail() { + return email; + } + + + /** + * Sets the name. + * @param name The name to set + */ + public void setName(String name) { + this.name = name; + } + + + /** + * Sets the function. + * @param function The function to set + */ + public void setFunction(String function) { + this.function = function; + } + + + /** + * Sets the email address. + * @param email The email address to set + */ + public void setEmail(String email) { + this.email = email; + } + +} diff --git a/examples/embedding/java/embedding/model/ProjectTeam.java b/examples/embedding/java/embedding/model/ProjectTeam.java new file mode 100644 index 000000000..b6de729cb --- /dev/null +++ b/examples/embedding/java/embedding/model/ProjectTeam.java @@ -0,0 +1,114 @@ +/* + * $Id: ProjectTeam.java,v 1.1.2.2 2003/02/25 16:06:34 jeremias Exp $ + * ============================================================================ + * The Apache Software License, Version 1.1 + * ============================================================================ + * + * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowledgment: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, if + * and wherever such third-party acknowledgments normally appear. + * + * 4. The names "FOP" and "Apache Software Foundation" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", nor may + * "Apache" appear in their name, without prior written permission of the + * Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ============================================================================ + * + * This software consists of voluntary contributions made by many individuals + * on behalf of the Apache Software Foundation and was originally created by + * James Tauber . For more information on the Apache + * Software Foundation, please see . + */ +package embedding.model; + +import java.util.List; + +import javax.xml.transform.Source; +import javax.xml.transform.sax.SAXSource; + +/** + * This bean represents a ProjectTeam. + */ +public class ProjectTeam { + + private String projectName; + private List members = new java.util.ArrayList(); + + + /** + * Returns a list of project members. + * @return List a list of ProjectMember objects + */ + public List getMembers() { + return this.members; + } + + + /** + * Adds a ProjectMember to this project team. + * @param member the member to add + */ + public void addMember(ProjectMember member) { + this.members.add(member); + } + + + /** + * Returns the name of the project + * @return String the name of the project + */ + public String getProjectName() { + return projectName; + } + + + /** + * Sets the name of the project. + * @param projectName the project name to set + */ + public void setProjectName(String projectName) { + this.projectName = projectName; + } + + + /** + * Resturns a Source object for this object so it can be used as input for + * a JAXP transformation. + * @return Source The Source object + */ + public Source getSourceForProjectTeam() { + return new SAXSource(new ProjectTeamXMLReader(), + new ProjectTeamInputSource(this)); + } + + +} diff --git a/examples/embedding/java/embedding/model/ProjectTeamInputSource.java b/examples/embedding/java/embedding/model/ProjectTeamInputSource.java new file mode 100644 index 000000000..f2c3a35be --- /dev/null +++ b/examples/embedding/java/embedding/model/ProjectTeamInputSource.java @@ -0,0 +1,87 @@ +/* + * $Id: ProjectTeamInputSource.java,v 1.1.2.2 2003/02/25 16:06:34 jeremias Exp $ + * ============================================================================ + * The Apache Software License, Version 1.1 + * ============================================================================ + * + * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowledgment: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, if + * and wherever such third-party acknowledgments normally appear. + * + * 4. The names "FOP" and "Apache Software Foundation" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", nor may + * "Apache" appear in their name, without prior written permission of the + * Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ============================================================================ + * + * This software consists of voluntary contributions made by many individuals + * on behalf of the Apache Software Foundation and was originally created by + * James Tauber . For more information on the Apache + * Software Foundation, please see . + */ +package embedding.model; + +import org.xml.sax.InputSource; + +/** + * This class is a special InputSource decendant for using ProjectTeam + * instances as XML sources. + */ +public class ProjectTeamInputSource extends InputSource { + + private ProjectTeam projectTeam; + + /** + * Constructor for the ProjectTeamInputSource + * @param projectTeam The ProjectTeam object to use + */ + public ProjectTeamInputSource(ProjectTeam projectTeam) { + this.projectTeam = projectTeam; + } + + /** + * Returns the projectTeam. + * @return ProjectTeam + */ + public ProjectTeam getProjectTeam() { + return projectTeam; + } + + /** + * Sets the projectTeam. + * @param projectTeam The projectTeam to set + */ + public void setProjectTeam(ProjectTeam projectTeam) { + this.projectTeam = projectTeam; + } + +} diff --git a/examples/embedding/java/embedding/model/ProjectTeamXMLReader.java b/examples/embedding/java/embedding/model/ProjectTeamXMLReader.java new file mode 100644 index 000000000..3b06d3f2d --- /dev/null +++ b/examples/embedding/java/embedding/model/ProjectTeamXMLReader.java @@ -0,0 +1,149 @@ +/* + * $Id: ProjectTeamXMLReader.java,v 1.1.2.2 2003/02/25 16:06:34 jeremias Exp $ + * ============================================================================ + * The Apache Software License, Version 1.1 + * ============================================================================ + * + * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowledgment: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, if + * and wherever such third-party acknowledgments normally appear. + * + * 4. The names "FOP" and "Apache Software Foundation" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", nor may + * "Apache" appear in their name, without prior written permission of the + * Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ============================================================================ + * + * This software consists of voluntary contributions made by many individuals + * on behalf of the Apache Software Foundation and was originally created by + * James Tauber . For more information on the Apache + * Software Foundation, please see . + */ +package embedding.model; + +//Java +import java.util.Iterator; +import java.io.IOException; + +//SAX +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import embedding.tools.AbstractObjectReader; + +/** + * XMLReader implementation for the ProjectTeam class. This class is used to + * generate SAX events from the ProjectTeam class. + */ +public class ProjectTeamXMLReader extends AbstractObjectReader { + + /** + * @see org.xml.sax.XMLReader#parse(InputSource) + */ + public void parse(InputSource input) throws IOException, SAXException { + if (input instanceof ProjectTeamInputSource) { + parse(((ProjectTeamInputSource)input).getProjectTeam()); + } else { + throw new SAXException("Unsupported InputSource specified. " + + "Must be a ProjectTeamInputSource"); + } + } + + + /** + * Starts parsing the ProjectTeam object. + * @param projectTeam The object to parse + * @throws SAXException In case of a problem during SAX event generation + */ + public void parse(ProjectTeam projectTeam) throws SAXException { + if (projectTeam == null) { + throw new NullPointerException("Parameter projectTeam must not be null"); + } + if (handler == null) { + throw new IllegalStateException("ContentHandler not set"); + } + + //Start the document + handler.startDocument(); + + //Generate SAX events for the ProjectTeam + generateFor(projectTeam); + + //End the document + handler.endDocument(); + } + + + /** + * Generates SAX events for a ProjectTeam object. + * @param projectTeam ProjectTeam object to use + * @throws SAXException In case of a problem during SAX event generation + */ + protected void generateFor(ProjectTeam projectTeam) throws SAXException { + if (projectTeam == null) { + throw new NullPointerException("Parameter projectTeam must not be null"); + } + if (handler == null) { + throw new IllegalStateException("ContentHandler not set"); + } + + handler.startElement("projectteam"); + handler.element("projectname", projectTeam.getProjectName()); + Iterator i = projectTeam.getMembers().iterator(); + while (i.hasNext()) { + ProjectMember member = (ProjectMember)i.next(); + generateFor(member); + } + handler.endElement("projectteam"); + } + + /** + * Generates SAX events for a ProjectMember object. + * @param projectMember ProjectMember object to use + * @throws SAXException In case of a problem during SAX event generation + */ + protected void generateFor(ProjectMember projectMember) throws SAXException { + if (projectMember == null) { + throw new NullPointerException("Parameter projectMember must not be null"); + } + if (handler == null) { + throw new IllegalStateException("ContentHandler not set"); + } + + handler.startElement("member"); + handler.element("name", projectMember.getName()); + handler.element("function", projectMember.getFunction()); + handler.element("email", projectMember.getEmail()); + handler.endElement("member"); + } + +} diff --git a/examples/embedding/java/embedding/tools/AbstractObjectReader.java b/examples/embedding/java/embedding/tools/AbstractObjectReader.java new file mode 100644 index 000000000..e45a13bb7 --- /dev/null +++ b/examples/embedding/java/embedding/tools/AbstractObjectReader.java @@ -0,0 +1,210 @@ +/* + * $Id: AbstractObjectReader.java,v 1.1.2.2 2003/02/25 16:06:34 jeremias Exp $ + * ============================================================================ + * The Apache Software License, Version 1.1 + * ============================================================================ + * + * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowledgment: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, if + * and wherever such third-party acknowledgments normally appear. + * + * 4. The names "FOP" and "Apache Software Foundation" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", nor may + * "Apache" appear in their name, without prior written permission of the + * Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ============================================================================ + * + * This software consists of voluntary contributions made by many individuals + * on behalf of the Apache Software Foundation and was originally created by + * James Tauber . For more information on the Apache + * Software Foundation, please see . + */ +package embedding.tools; + +//Java +import java.io.IOException; +import java.util.Map; + +//SAX +import org.xml.sax.SAXException; +import org.xml.sax.InputSource; +import org.xml.sax.XMLReader; +import org.xml.sax.ContentHandler; +import org.xml.sax.DTDHandler; +import org.xml.sax.ErrorHandler; +import org.xml.sax.EntityResolver; + +/** + * This class can be used as base class for XMLReaders that generate SAX + * events from Java objects. + */ + +public abstract class AbstractObjectReader implements XMLReader { + + private static final String NAMESPACES = + "http://xml.org/sax/features/namespaces"; + private static final String NS_PREFIXES = + "http://xml.org/sax/features/namespace-prefixes"; + + private Map features = new java.util.HashMap(); + private ContentHandler orgHandler; + + /** Proxy for easy SAX event generation */ + protected EasyGenerationContentHandlerProxy handler; + /** Error handler */ + protected ErrorHandler errorHandler; + + + /** + * Constructor for the AbstractObjectReader object + */ + public AbstractObjectReader() { + setFeature(NAMESPACES, false); + setFeature(NS_PREFIXES, false); + } + + /* ============ XMLReader interface ============ */ + + /** + * @see org.xml.sax.XMLReader#getContentHandler() + */ + public ContentHandler getContentHandler() { + return this.orgHandler; + } + + /** + * @see org.xml.sax.XMLReader#setContentHandler(ContentHandler) + */ + public void setContentHandler(ContentHandler handler) { + this.orgHandler = handler; + this.handler = new EasyGenerationContentHandlerProxy(handler); + } + + /** + * @see org.xml.sax.XMLReader#getErrorHandler() + */ + public ErrorHandler getErrorHandler() { + return this.errorHandler; + } + + /** + * @see org.xml.sax.XMLReader#setErrorHandler(ErrorHandler) + */ + public void setErrorHandler(ErrorHandler handler) { + this.errorHandler = handler; + } + + /** + * @see org.xml.sax.XMLReader#getDTDHandler() + */ + public DTDHandler getDTDHandler() { + return null; + } + + /** + * @see org.xml.sax.XMLReader#setDTDHandler(DTDHandler) + */ + public void setDTDHandler(DTDHandler handler) { + } + + /** + * @see org.xml.sax.XMLReader#getEntityResolver() + */ + public EntityResolver getEntityResolver() { + return null; + } + + /** + * @see org.xml.sax.XMLReader#setEntityResolver(EntityResolver) + */ + public void setEntityResolver(EntityResolver resolver) { + } + + /** + * @see org.xml.sax.XMLReader#getProperty(String) + */ + public Object getProperty(java.lang.String name) { + return null; + } + + /** + * @see org.xml.sax.XMLReader#setProperty(String, Object) + */ + public void setProperty(java.lang.String name, java.lang.Object value) { + } + + /** + * @see org.xml.sax.XMLReader#getFeature(String) + */ + public boolean getFeature(java.lang.String name) { + return ((Boolean) features.get(name)).booleanValue(); + } + + /** + * Returns true if the NAMESPACES feature is enabled. + * @return boolean true if enabled + */ + protected boolean isNamespaces() { + return getFeature(NAMESPACES); + } + + /** + * Returns true if the MS_PREFIXES feature is enabled. + * @return boolean true if enabled + */ + protected boolean isNamespacePrefixes() { + return getFeature(NS_PREFIXES); + } + + /** + * @see org.xml.sax.XMLReader#setFeature(String, boolean) + */ + public void setFeature(java.lang.String name, boolean value) { + this.features.put(name, new Boolean(value)); + } + + /** + * @see org.xml.sax.XMLReader#parse(String) + */ + public void parse(String systemId) throws IOException, SAXException { + throw new SAXException( + this.getClass().getName() + + " cannot be used with system identifiers (URIs)"); + } + + /** + * @see org.xml.sax.XMLReader#parse(InputSource) + */ + public abstract void parse(InputSource input) + throws IOException, SAXException; + +} diff --git a/examples/embedding/java/embedding/tools/EasyGenerationContentHandlerProxy.java b/examples/embedding/java/embedding/tools/EasyGenerationContentHandlerProxy.java new file mode 100644 index 000000000..aab5bc2fc --- /dev/null +++ b/examples/embedding/java/embedding/tools/EasyGenerationContentHandlerProxy.java @@ -0,0 +1,245 @@ +/* + * $Id: EasyGenerationContentHandlerProxy.java,v 1.1.2.2 2003/02/25 16:06:34 jeremias Exp $ + * ============================================================================ + * The Apache Software License, Version 1.1 + * ============================================================================ + * + * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowledgment: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, if + * and wherever such third-party acknowledgments normally appear. + * + * 4. The names "FOP" and "Apache Software Foundation" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", nor may + * "Apache" appear in their name, without prior written permission of the + * Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ============================================================================ + * + * This software consists of voluntary contributions made by many individuals + * on behalf of the Apache Software Foundation and was originally created by + * James Tauber . For more information on the Apache + * Software Foundation, please see . + */ +package embedding.tools; + +//SAX +import org.xml.sax.ContentHandler; +import org.xml.sax.Locator; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +/** + * This class is an implementation of ContentHandler which acts as a proxy to + * another ContentHandler and has the purpose to provide a few handy methods + * that make life easier when generating SAX events. + *
+ * Note: This class is only useful for simple cases with no namespaces. + */ + +public class EasyGenerationContentHandlerProxy implements ContentHandler { + + /** An empty Attributes object used when no attributes are needed. */ + public static final Attributes EMPTY_ATTS = new AttributesImpl(); + + private ContentHandler target; + + + /** + * Main constructor. + * @param forwardTo ContentHandler to forward the SAX event to. + */ + public EasyGenerationContentHandlerProxy(ContentHandler forwardTo) { + this.target = forwardTo; + } + + + /** + * Sends the notification of the beginning of an element. + * @param name Name for the element. + * @throws SAXException Any SAX exception, possibly wrapping another exception. + */ + public void startElement(String name) throws SAXException { + startElement(name, EMPTY_ATTS); + } + + + /** + * Sends the notification of the beginning of an element. + * @param name Name for the element. + * @param atts The attributes attached to the element. If there are no + * attributes, it shall be an empty Attributes object. + * @throws SAXException Any SAX exception, possibly wrapping another exception. + */ + public void startElement(String name, Attributes atts) throws SAXException { + startElement(null, name, name, atts); + } + + + /** + * Send a String of character data. + * @param s The content String + * @throws SAXException Any SAX exception, possibly wrapping another exception. + */ + public void characters(String s) throws SAXException { + target.characters(s.toCharArray(), 0, s.length()); + } + + + /** + * Send the notification of the end of an element. + * @param name Name for the element. + * @throws SAXException Any SAX exception, possibly wrapping another exception. + */ + public void endElement(String name) throws SAXException { + endElement(null, name, name); + } + + + /** + * Sends notifications for a whole element with some String content. + * @param name Name for the element. + * @param value Content of the element. + * @throws SAXException Any SAX exception, possibly wrapping another exception. + */ + public void element(String name, String value) throws SAXException { + element(name, value, EMPTY_ATTS); + } + + + /** + * Sends notifications for a whole element with some String content. + * @param name Name for the element. + * @param value Content of the element. + * @param atts The attributes attached to the element. If there are no + * attributes, it shall be an empty Attributes object. + * @throws SAXException Any SAX exception, possibly wrapping another exception. + */ + public void element(String name, String value, Attributes atts) throws SAXException { + startElement(name, atts); + if (value != null) { + characters(value.toCharArray(), 0, value.length()); + } + endElement(name); + } + + /* =========== ContentHandler interface =========== */ + + /** + * @see org.xml.sax.ContentHandler#setDocumentLocator(Locator) + */ + public void setDocumentLocator(Locator locator) { + target.setDocumentLocator(locator); + } + + + /** + * @see org.xml.sax.ContentHandler#startDocument() + */ + public void startDocument() throws SAXException { + target.startDocument(); + } + + + /** + * @see org.xml.sax.ContentHandler#endDocument() + */ + public void endDocument() throws SAXException { + target.endDocument(); + } + + + /** + * @see org.xml.sax.ContentHandler#startPrefixMapping(String, String) + */ + public void startPrefixMapping(String prefix, String uri) throws SAXException { + target.startPrefixMapping(prefix, uri); + } + + + /** + * @see org.xml.sax.ContentHandler#endPrefixMapping(String) + */ + public void endPrefixMapping(String prefix) throws SAXException { + target.endPrefixMapping(prefix); + } + + + /** + * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes) + */ + public void startElement(String namespaceURI, String localName, + String qName, Attributes atts) throws SAXException { + target.startElement(namespaceURI, localName, qName, atts); + } + + + /** + * @see org.xml.sax.ContentHandler#endElement(String, String, String) + */ + public void endElement(String namespaceURI, String localName, String qName) + throws SAXException { + target.endElement(namespaceURI, localName, qName); + } + + + /** + * @see org.xml.sax.ContentHandler#characters(char[], int, int) + */ + public void characters(char[] ch, int start, int length) throws SAXException { + target.characters(ch, start, length); + } + + + /** + * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int) + */ + public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { + target.ignorableWhitespace(ch, start, length); + } + + + /** + * @see org.xml.sax.ContentHandler#processingInstruction(String, String) + */ + public void processingInstruction(String target, String data) throws SAXException { + this.target.processingInstruction(target, data); + } + + + /** + * @see org.xml.sax.ContentHandler#skippedEntity(String) + */ + public void skippedEntity(String name) throws SAXException { + target.skippedEntity(name); + } + +} \ No newline at end of file diff --git a/examples/embedding/xml/fo/helloworld.fo b/examples/embedding/xml/fo/helloworld.fo new file mode 100644 index 000000000..483450028 --- /dev/null +++ b/examples/embedding/xml/fo/helloworld.fo @@ -0,0 +1,13 @@ + + + + + + + + + + Hello World! + + + diff --git a/examples/embedding/xml/xml/projectteam.xml b/examples/embedding/xml/xml/projectteam.xml new file mode 100644 index 000000000..11af078fd --- /dev/null +++ b/examples/embedding/xml/xml/projectteam.xml @@ -0,0 +1,29 @@ + + + The Killer Application + + John Doe + lead + jon.doe@killerapp.fun + + + Paul Coder + dev + paul.coder@killerapp.fun + + + Max Hacker + dev + max.hacker@killerapp.fun + + + Donna Book + doc + donna.book@killerapp.fun + + + Henry Tester + qa + henry.tester@killerapp.fun + + \ No newline at end of file diff --git a/examples/embedding/xml/xslt/projectteam2fo.xsl b/examples/embedding/xml/xslt/projectteam2fo.xsl new file mode 100644 index 000000000..ba205fbf8 --- /dev/null +++ b/examples/embedding/xml/xslt/projectteam2fo.xsl @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + Project: + + + + + + + + + + + + + + + + + + + + + + bold + + + + + + + + + + + + + + + + + + +