Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Publishing XML Documents in PDF and HTML with Cocoon : Page 2

Sure, you can create a custom framework for multi-format publishing, but why bother? It's already been done. Take advantage of Apache Cocoon's ability to deliver PDF and HTML documents on demand.


advertisement
Create the Source Code Files
Navigate to the webapp directory in Cocoon and create an xmltopdf directory in it. Cocoon provides much more than dynamic PDF generation. In this article, you're using it only to generate HTML and PDF from an XML file data source, which you'll do by creating a JSP page. You should understand though that Cocoon actually uses XSP, which is a file format that combines Java and XML to render output. You can avoid having to learn XSP by using the techniques shown in this article, taking a little extra care to write the XML syntax in such a way that Cocoon can still render it. Using a JSP page in this way works fine for outputting HTML and PDF files; however, if you plan to use Cocoon to develop a complete Web site, then you should bite the bullet and learn XSP. Getting back to the example, create a file called pageOne.xml inside the xmltopdf directory that has the following content:

<!-- pageOne.xml --> <?xml version="1.0" encoding="iso-8859-1"?> <page> <title>This is the pageOne.xml example</title> <s1 title="Section one"> <p>This is the text of section one</p> </s1> </page>

You will also create a second source file—a JSP, separately (outside of Cocoon). This way, you can pass any JSP independently as a source to this framework, and Cocoon will serialize it into an HTML or a PDF file. For the purpose of this article, put the following file in a folder called jsptopdf. Next create a war file and deploy it on JBoss.

<!-- pageTwo.jsp --> <?xml version="1.0" encoding="iso-8859-1"?> <%@ page contentType="text/xml;charset=ISO-8859-1" import="java.util.*" %> <page> <title>This is the pageTwo.jsp example</title> <s1 title="Section two"> <p>This is the text of section two. The time now is <%= (new Date()).toString() %></p> </s1> </page>

Now test run the JBoss server, and see if you can view this JSP. Because the file is independent of Cocoon, you should be able to see the page rendered.

Note that you must make sure to build pageTwo.jsp with well-formed XML syntax, otherwise Cocoon will complain. You would keep this JSP page in a Web application directory, not in the Cocoon directory. For this example, put the file in a jsptopdf directory. Make a war file for this too, and deploy it in a similar fashion as the previous Cocoon war file. You keep the file outside of the Cocoon war because Cocoon will only render files based on the requests defined in the sitemap.xmap file (described below).

The XML in the first file you created serves as the data source. Data in the second file can be fetched from any java object or through JDBC. To transform these files, you'll define XSL files now. Make an XSL stylesheet file called doc2html.xsl—you'll use this stylesheet to transform both the files you've already created.



<!-- doc2html.xsl --> <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- generate HTML skeleton on root element --> <xsl:template match="/"> <html> <head> <title><xsl:apply-templates select="page/title"/></title> </head> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="p"> <p><xsl:apply-templates/></p> </xsl:template> <!-- convert sections to HTML headings --> <xsl:template match="s1"> <h1><xsl:apply-templates select="@title"/></h1> <xsl:apply-templates/> </xsl:template> </xsl:stylesheet>



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date