%@ page language="java" import="java.util.*,java.io.*,org.apache.xalan.xslt.*" errorPage="error.jsp" %> <%! public final static String LIASON_CLASS = "org.apache.xalan.xpath.xdom.XercesLiaison"; public static org.w3c.dom.Document getXMLDocument(String xmlSourceURL) throws java.io.IOException, org.xml.sax.SAXException { org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser(); parser.parse(new org.xml.sax.InputSource( new FileInputStream(xmlSourceURL) )); return parser.getDocument(); } public static ByteArrayOutputStream transformToHTML(String xmlSourceURL, String xslURL) throws java.io.IOException, java.net.MalformedURLException, org.xml.sax.SAXException, javax.xml.transform.TransformerConfigurationException, javax.xml.transform.TransformerException { // Just in case you want to produce your own DOM input... //DOMParserWrapper parser = // (DOMParserWrapper)Class.forName(dom.wrappers.DOMParser).newInstance(); //org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser(); //parser.parse(new org.xml.sax.InputSource( new FileInputStream(xmlSourceURL) )); //parser.parse( xmlSourceURL ); //org.w3c.dom.Document doc = parser.getDocument(); org.w3c.dom.Document doc = getXMLDocument( xmlSourceURL ); // Set up the XSLTProcessor to use XercesLiaison. //XSLTProcessor xsltProc = XSLTProcessorFactory.getProcessor // (new org.apache.xalan.xpath.xdom.XercesLiaison()); // or you can use XSLTProcessorFactory.getProcessorUsingLiaisonName // ("org.apache.xalan.xpath.xdom.XercesLiaison"); //XSLTProcessor processor = XSLTProcessorFactory.getProcessorUsingLiaisonName(org.apache.xalan.xslt.Constants.LIAISON_CLASS); // 1. Instantiate a TransformerFactory. javax.xml.transform.TransformerFactory tFactory = javax.xml.transform.TransformerFactory.newInstance(); // 2. Use the TransformerFactory to process the stylesheet Source and // generate a Transformer. javax.xml.transform.Transformer transformer = tFactory.newTransformer (new javax.xml.transform.stream.StreamSource(new FileInputStream(xslURL))); // 3. Create Result based on ByteSream ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); // 4. Use the Transformer to transform an XML Source and send the // output to a Result object. transformer.transform (new javax.xml.transform.dom.DOMSource(doc), new javax.xml.transform.stream.StreamResult( byteStream ) ); /* org.apache.xalan.xslt.XSLTProcessor processor = org.apache.xalan.xslt.XSLTProcessorFactory.getProcessorUsingLiaisonName(LIASON_CLASS); org.apache.xalan.xslt.StylesheetRoot style = processor.processStylesheet(new org.apache.xalan.xslt.XSLTInputSource(new FileInputStream(xslURL) )); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); org.apache.xalan.xslt.XSLTResultTarget xmlResult = new org.apache.xalan.xslt.XSLTResultTarget(byteStream); processor.setStylesheet(style); processor.process(new XSLTInputSource(doc), null, // using pre-compiled style xmlResult); */ return byteStream; } public static ByteArrayOutputStream transformToPDF(String foSourceURL) throws java.io.IOException, ServletException, org.apache.fop.apps.FOPException { FileInputStream file = new FileInputStream("z:\\Fop-0.19.0-CVS\\docs\\examples\\" + foSourceURL); ByteArrayOutputStream out = new ByteArrayOutputStream(); org.apache.fop.apps.Driver driver = new org.apache.fop.apps.Driver(new org.xml.sax.InputSource(file), out); driver.run(); return out; } public static ByteArrayOutputStream transformToPDF(String xmlSourceURL, String xsl2foURL) throws java.io.IOException, ServletException, org.xml.sax.SAXException, javax.xml.transform.TransformerConfigurationException, javax.xml.transform.TransformerException, org.apache.fop.apps.FOPException { // input org.w3c.dom.Document doc = getXMLDocument( xmlSourceURL ); // output ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); // 1. Instantiate a TransformerFactory. javax.xml.transform.TransformerFactory tFactory = javax.xml.transform.TransformerFactory.newInstance(); // 2. Use the TransformerFactory to process the stylesheet Source and generate a Transformer. javax.xml.transform.Transformer transformer = tFactory.newTransformer (new javax.xml.transform.stream.StreamSource(new FileInputStream(xsl2foURL))); // 3. Create FOP driver and set rendering mode and output stream org.apache.fop.apps.Driver driver = new org.apache.fop.apps.Driver(); driver.setRenderer(driver.RENDER_PDF); driver.setOutputStream( byteStream ); // 4. Create SAXResult based on FOP Driver content handler which will accept SAX events and build FOP tree javax.xml.transform.sax.SAXResult saxResult = new javax.xml.transform.sax.SAXResult( driver.getContentHandler() ); // 5. Use the Transformer to transform an XML Source and send the output to a Result object. // Implicitely it will create the FOP tree by firing SAX events transformer.transform( new javax.xml.transform.dom.DOMSource(doc), saxResult ); // 6. Perform formatting and rendering of the PDF output driver.format(); driver.render(); return byteStream; } %> <% String xmlURL = "z:\\Dev\\equity\\foo.xml"; String xslURL = "z:\\Dev\\equity\\foo.xsl"; String xsl2foURL = "z:\\Dev\\equity\\foo2fo.xsl"; String foURL = request.getParameter("fo"); try { boolean print = (request.getParameter("print") != null); if( foURL == null && !print ) { ByteArrayOutputStream byteStream = transformToHTML(xmlURL, xslURL); out.println("