/** * 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 javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import org.apache.commons.logging.impl.SimpleLog; import org.xml.sax.InputSource; import com.lunasil.xf.XincFormatter; /** * This Class shows how to use the XincFormatter to create * PDF documents from Java. If you need a lower level API you * should look at ApiDemo2.java instead. It demonstrates the * use of XincEngine which will allow for more fine grained control * over the formatting process. * * 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 ApiDemo1 { public void setUp() throws Exception { SimpleLog logger = new SimpleLog("Test"); logger.setLevel(SimpleLog.LOG_LEVEL_WARN); XincFormatter.setLogger(logger); } public void tearDown() throws Exception { } /** * This is an example of using the XincFormatter 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"; XincFormatter formatter = new XincFormatter(); // This is optional. Pdf is the default. formatter.setRenderer("Pdf"); // 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); formatter.format(foFileName, null, bos); bos.close(); fos.close(); } /** * This is an example of using the XincFormatter to do the formatting of * an XML document using a stylesheet. The document and stylesheet are * specified by passing an InputSource and a StreamSource. * * @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"; XincFormatter formatter = new XincFormatter(); // 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); InputSource docSource = new InputSource(xmlFileName); Source styleSource = new StreamSource(styleFileName); formatter.format(docSource, styleSource, bos); bos.close(); fos.close(); } public static void main(String[] args) throws Exception { ApiDemo1 demo = new ApiDemo1(); demo.setUp(); demo.test1(); demo.test2(); demo.tearDown(); } }