Cocoon Demonstration (XSL FO)
Gerry de Koning

Setting up Cocoon

I cannot cover all the details of setting up Cocoon and a servlet-capable web server, such as Tomcat. Here are some hints which may help you do this for the first time.

  1. Install Tomcat from Apache.
  2. Install Cocoon from Apache.
  3. Create a directory for your web-app (these demos). For this example, I will assume it is "C:\cocoon\demo"
  4. Install the demo files used in this demonstration in this directory.
  5. Edit the Tomcat "server.xml" configuration file to include the following web-app (near the bottom of the file):
    An extract from server.xml
             <Context path="/demo-cocoon"
    	                  docBase="C:/demo/cocoon"
    			  debug="0"
    			  reloadable="true" >
    	 </Context>
    
  6. Edit the "tomcat.bat" or "tomcat.sh" file in Tomcat's bin directory so that the classes necessary for Cocoon are in the classpath.
    tomcat.bat
    ...
    set JAVA_HOME=C:\jdk1.3
    set TOMCAT_HOME=C:\Apache\tomcat\jakarta-tomcat-3.2.1
    ...
    :setClasspath
    set CP=%TOMCAT_HOME%\classes
    
    rem Cocoon classes and libraries
    set COCOON_HOME=c:\Apache\cocoon\cocoon-1.8.2
    set CP=%CP%;%COCOON_HOME%\lib\xerces_1_2.jar
    set CP=%CP%;%COCOON_HOME%\lib\xalan_1_2_D02.jar
    set CP=%CP%;%COCOON_HOME%\lib\fop_0_15_0.jar
    set CP=%CP%;%COCOON_HOME%\lib\turbine-pool.jar
    set CP=%CP%;%COCOON_HOME%\lib\w3c.jar
    set CP=%CP%;%COCOON_HOME%\bin\cocoon.jar
    ...
    
  7. Create a directory within your web-app home called "WEB-INF".
  8. In the WEB-INF directory create a web.xml by copying the web.xml from the Tomcat web-apps examples directory and modifying it to include the following lines:
    web.xml
    ...
    <web-app>
        <servlet>
        	<servlet-name>
        		org.apache.cocoon.Cocoon
        	</servlet-name>
        	<servlet-class>
        		org.apache.cocoon.Cocoon
        	</servlet-class>
        	<init-param>
        		<param-name>
        			properties
        		</param-name>
        		<param-value>
        			WEB-INF/cocoon.properties
        		</param-value>
        	</init-param>
        </servlet>
        <servlet-mapping>
        	<servlet-name>
        		org.apache.cocoon.Cocoon
        	</servlet-name>
        	<url-pattern>
        		*.xml
        	</url-pattern>
        </servlet-mapping>
    ...
    
  9. Copy the cocoon.properties file from the Cocoon install directory to the new WEB-INF directory
  10. Start Tomcat
  11. Start your browser and enter the URL of the demo web-app.

These instructions are far from complete. There is more good information about using Cocoon in Brett McLaughlin's book, "Java and XML." You should also read the documentation you receive with Tomcat and Cocoon. However, be warned: the setup procedure is not simple for the uninitiated. Be prepared to spend some time reading and experimenting.

The XML file

The file, "report.xml," which will be used for this demo is wrapped in the follwing XML wrapper file.

index4.xml
<?xml version="1.0" ?>
<!DOCTYPE page [
<!ENTITY content SYSTEM "report.xml" >
]>

<?cocoon-process type="xslt" ?>
<?xml-stylesheet href="report.xsl" type="text/xsl" ?>

<page>
&content;
</page>

And here is the data file.

report.xml
<?xml version="1.0" encoding="UTF-8" ?>

<report>
	<front>
		<title>Demo Report</title>
		<author>Gerry de Koning</author>
		<date>July 10, 2001</date>
	</front>
	<preface>
		<title>Preface</title>
		<body>
			<p>This report is a short demo for 
			   Comdex Canada 2001.</p>
		</body>
	</preface>
	<chapter>
		<title>Chapter 1 - XML for Developers</title>
		<body>
			<p>A tutorial is being offered on Tuesday, July 10,
			for developers and web designers on XML, a hot new
			technology.  XML is used for exchanging data among
			programs and for managing documents.</p>
			<recommendation>Register today!</recommendation>
		</body>
	</chapter>
</report>

The XSL file

This XML file will be processed by Cocoon using the following XSLT file, "report.xsl."

report.xsl
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format">

  <xsl:template match="/">
    <xsl:processing-instruction name="cocoon-format">
		type="text/xslfo"</xsl:processing-instruction>
		
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <fo:layout-master-set>
      <fo:simple-page-master
        master-name="right"
        margin-top="75pt"
        margin-bottom="25pt"
        margin-left="100pt"
        margin-right="50pt">
        <fo:region-body margin-bottom="50pt"/>
        <fo:region-after extent="25pt"/>
      </fo:simple-page-master>     
      <fo:simple-page-master
        master-name="left"
        margin-top="75pt"
        margin-bottom="25pt"
        margin-left="50pt"
        margin-right="100pt">
        <fo:region-body margin-bottom="50pt"/>
        <fo:region-after extent="25pt"/>
      </fo:simple-page-master>

      <fo:page-sequence-master master-name="psmOddEven">
        <fo:repeatable-page-master-alternatives>
          <fo:conditional-page-master-reference 
          		master-name="right" page-position="first"/>
          <fo:conditional-page-master-reference 
          		master-name="right" odd-or-even="even"/>  
          <fo:conditional-page-master-reference 
          		master-name="left" odd-or-even="odd"/>
		  <!-- recommended fallback procedure -->
          <fo:conditional-page-master-reference 
          		master-name="right"/>
        </fo:repeatable-page-master-alternatives>
      </fo:page-sequence-master>
	  </fo:layout-master-set>

      <fo:page-sequence master-name="psmOddEven">
        <fo:static-content flow-name="xsl-region-after">
          <fo:block text-align-last="center" 
          			font-size="10pt"><fo:page-number/></fo:block>
        </fo:static-content>

        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates/>
        </fo:flow>
      </fo:page-sequence>

    </fo:root>
  </xsl:template>

  <xsl:template match="front/title">
    <fo:block font-size="24pt" 
    		  text-align-last="centered" 
    		  space-before.optimum="24pt"
    		  ><xsl:apply-templates/></fo:block>
  </xsl:template>

  <xsl:template match="front/sub-title">
    <fo:block font-size="18pt" 
    		  text-align-last="centered" 
    		  space-before.optimum="24pt"
    		  ><xsl:apply-templates/></fo:block>
  </xsl:template>

  <xsl:template match="revision">
    <fo:block font-size="18pt" 
    		  text-align-last="centered" 
    		  space-before.optimum="24pt"
    		  ><xsl:apply-templates/></fo:block>
  </xsl:template>

  <xsl:template match="author">
    <fo:block font-size="18pt" 
    		  text-align-last="centered" 
    		  space-before.optimum="48pt"
    		  ><xsl:apply-templates/></fo:block>
  </xsl:template>

  <xsl:template match="preface">
  	<fo:block break-before="odd-page">
    <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="preface/title">
    <fo:block font-size="14pt" 
    		  text-align-last="left" 
    		  space-before.optimum="24pt"
    		  ><xsl:apply-templates/></fo:block>
  </xsl:template>

  <xsl:template match="chapter">
  	<fo:block break-before="odd-page">
    <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="chapter/title">
    <fo:block font-size="14pt" 
    		  font-weight="bold" 
    		  color="blue" 
    		  text-align-last="left" 
    		  space-before.optimum="24pt"
    		  ><xsl:apply-templates/></fo:block>
  </xsl:template>

  <xsl:template match="p">
    <fo:block font-size="12pt" 
    		  font-family="TimesRoman" 
    		  space-before.optimum="12pt" 
    		  text-align="justified">
    	<xsl:apply-templates/> 
    </fo:block>
  </xsl:template>
  
  <xsl:template match="recommendation">
    <fo:block font-size="12pt" 
    		  font-family="TimesRoman" 
    		  font-style="italic"
    		  space-before.optimum="72pt" 
    		  text-align="justified" 
    		  color="red" 
         	  start-indent="40pt" 
         	  end-indent="40pt"
              padding="20pt" 
              space="20pt"
              border-bottom-style="solid"  
              border-bottom-width="1pt"
              border-top-style="solid"  
              border-top-width="1pt" 
              border-color="red" >
         <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
 
</xsl:stylesheet>

The Result

If you have Cocoon running, you will be able to point your browser at the "index4.xml" file and get a PDF file back. It should be identical this PDF file.