/** * Feel free to use this code as a starting point for your own Application. * * This code is provided 'as is' with no warranty, either express or implied, of * any kind. * * We don't recemmend the use of this code in Nuclear Reactors or any space * related application which requires the calculation of a coefficient * for gravity. * * Please report any errors or suggestions to support@luansil.com. * * @author Greg Gaffney, Lunasil Ltd. */ import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.net.URL; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.stream.StreamSource; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; import org.apache.commons.logging.impl.SimpleLog; import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.XMLReaderFactory; import com.lunasil.xf.XincEngine; /** * This Class shows how to use XincEngine to create * PDF documents from Java. * * In this sample the setUp() method is called first to * do things like loading configuration and creating a logger. * Documents are then formatted and finally tearDown() is called * to do any final clean up work. */ public class ApiDemo2 { public void setUp() throws Exception { SimpleLog logger = new SimpleLog("Test"); logger.setLevel(SimpleLog.LOG_LEVEL_WARN); XincEngine.setLogger(logger); } public void tearDown() throws Exception { } public XMLReader createXMLReader() throws SAXException { XMLReader xReader = null; try { xReader = XMLReaderFactory.createXMLReader(); } catch (SAXException e) { // This should work in JRE 1.4.1 try { xReader = XMLReaderFactory.createXMLReader( "org.apache.crimson.parser.XMLReaderImpl"); } catch (SAXException e2) { } // This should work if we're using xalan-j 2.5.1 if (xReader == null) { try { xReader = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser"); } catch (SAXException e1) { } } // If all else fails, throw the original exception... if (xReader == null) throw e; } // These features are required. xReader.setFeature( "http://xml.org/sax/features/namespaces", true); xReader.setFeature( "http://xml.org/sax/features/namespace-prefixes", true); return xReader; } /** * This is an example of using XincEngine to do the formatting of * a simple Fo file. * * @throws Exception */ public void test1() throws Exception { String foFileName = "http://www.lunasil.com/samples/helloworld.fo"; String outputFileName = "/helloworld.pdf"; URL baseUrl = new URL(foFileName); XincEngine x = new XincEngine(); // This is optional. Pdf is the default. x.setRenderer("Pdf"); // A base URL needs to be set so that relative URL's can // be resolved. Relative URL's are typically encountered in // external-graphic elements. x.setBaseUrl(baseUrl); // Uncomment these lines if you want to specify your own // distinct configuration. // DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); // Configuration config = builder.buildFromFile("xincconfig.xml"); // formatter.setConfiguration(config); FileOutputStream fos = new FileOutputStream(outputFileName); BufferedOutputStream bos = new BufferedOutputStream(fos, 0x4000); x.setOutputStream(bos); // Don't create the Content Handler until you have finished // configuring the XincEngine instance. ContentHandler ch = x.createContentHandler(); XMLReader xReader = createXMLReader(); xReader.setContentHandler(ch); xReader.parse(foFileName); bos.close(); fos.close(); } /** * This is an example of using the XincEngine to do the formatting of * an XML document using a stylesheet. * * @throws Exception */ public void test2() throws Exception { String xmlFileName = "http://www.lunasil.com/samples/ReportData.xml"; String styleFileName = "http://www.lunasil.com/samples/ReportStyle.xsl"; String outputFileName = "/ReportStyle.pdf"; URL baseUrl = new URL(xmlFileName); XincEngine x = new XincEngine(); x.setBaseUrl(baseUrl); FileOutputStream fos = new FileOutputStream(outputFileName); BufferedOutputStream bos = new BufferedOutputStream(fos, 0x4000); x.setOutputStream(bos); StreamSource docSource = new StreamSource(xmlFileName); Source styleSource = new StreamSource(styleFileName); ContentHandler ch = x.createContentHandler(); Result saxResult = new SAXResult(ch); TransformerFactory fact = TransformerFactory.newInstance(); Transformer transformer = fact.newTransformer(styleSource); transformer.transform(docSource, saxResult); bos.close(); fos.close(); } public static void main(String[] args) throws Exception { ApiDemo2 demo = new ApiDemo2(); demo.setUp(); demo.test1(); demo.test2(); demo.tearDown(); } }