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 3

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
Using Pipelines in Cocoon
Cocoon utilizes the concept of pipelines. You have XML to publish and XSL to transform it, now you need to create the sitemap file that Cocoon requires. This file has the ability to select the pipeline based on the browser request. Create the sample sitemap.xmap file that appears below. This file tells Cocoon how to map requests. For example, using this file, HTML requests would have an XML source and would be rendered as HTML. Similarly, PDF requests would have either an XML data source or a JSP data source and Cocoon would render them as PDF.

<!-- sitemap.xmap --> <?xml version="1.0"?> <map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0"> <map:pipelines> <map:pipeline> <map:match pattern="pageOne.html"> <map:generate src="pageOne.xml"/> <map:transform src="doc2html.xsl"/> <map:serialize type="html"/> </map:match> <map:match pattern="pageTwo.html"> <map:generate src="http://localhost:8080/jsptopdf/pageTwo.jsp"/> <map:transform src="doc2html.xsl"/> <map:serialize type="html"/> </map:match> <map:match pattern="pageOne.pdf"> <map:generate src="pageOne.xml"/> <map:transform src="doc2pdf.xsl"/> <map:serialize type="fo2pdf"/> </map:match> <map:match pattern="pageTwo.pdf"> <map:generate src="http://localhost:8080/jsptopdf/pageTwo.jsp"/> <map:transform src="doc2pdf.xsl"/> <map:serialize type="fo2pdf"/> </map:match> </map:pipeline> </map:pipelines> </map:sitemap>

Another advantage of defining the request mappings is that if you don't want users to be able to browse to an XML file directly, you just don't define the mapping for it. To let users see the page either in PDF or HTML, you provide them with hyperlinks to the desired file type, and they would see HTML or PDF, regardless of whether the actual data source is JSP or XML.

Cocoon uses FO to generate PDF. If you regenerate and deploy the war file again at this point, the HTML transformation would work—but PDF transformation requires some more work. To accomplish that transformation, define a file called doc2pdf.xsl (see Listing 1).

The file in Listing 1 is similar to a normal XSL file. The heading section can pretty much be reused, and the section which defines how to display PDF is also very similar to the way you would normally define an XSL file. To get more detail about how to write XSL-FO files, refer to Apache FOP project. At this point you're ready to regenerate the war file. Follow the same steps as before to build (use the command build war) and then deploy the war file.



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