SDSU Emerging Technology
Fall Semester, 2004
Generators
Previous     Lecture Notes Index     Next     
© 2004, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 28-Sep-04

CS 683 Emerging Technologies Fall Semester, 2004 Doc 15 Generators

Contents

References

XSL

XSLT With out Files

Generators

VelocityGenerator

Copyright ©, All rights reserved. 2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent ( http://www.opencontent.org/opl.shtml) license defines the copyright on this document.

References

Cocoon Developer’s Handbook, Moczar & Aston, Sams Publishing, 2003

XSL

VisualWorks

Load the parcel XSL.pcl in parcels directory in your VW installation.

Read the XSL stylesheet processing (page 529) section of chapter 24 (XML Framework) of the VisualWorks Application Developer’s Guide

Java

Download Xalan-Java ( http://xml.apache.org/xalan-j/ )

Read the docs and examples

Warning about Files & Web pages

If a server generates a file called foo to help process a request

What happens if the server gets two such requests at the same time?

XSLT With out Files

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
   
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
   
public class SimpleTransform
   {
   public static void main(String[] args) throws 
      TransformerException, TransformerConfigurationException, 
      FileNotFoundException, IOException
      {  
      TransformerFactory tFactory = 
         TransformerFactory.newInstance();
      String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:template match=\"/\"><html> <title> <xsl:value-of select=\"greetings\"/></title> <body><h1><xsl:value-of select=\"greetings\"/></h1> </body></html></xsl:template></xsl:stylesheet>";

Example Continued

      Transformer transformer = 
         tFactory.newTransformer(
            new StreamSource(new StringReader(xsl)));
   
      String xml = "<?xml version=\"1.0\"?><?xml-stylesheet type=\"text/xsl\" href=\"hello.xsl\"?><greetings>Hello World</greetings>";
   
      StringWriter result = new StringWriter();
      transformer.transform(
         new StreamSource( new StringReader( xml)), 
         new StreamResult(result));
      System.out.println(result.toString());
      }
   }

Generators

DirectoryGenerator

ErrorGenerator

HTMLGenerator

JspGenerator

RequestGenerator

ScriptGenerator

VelocityGenerator

VelocityGenerator

Processing without the tags

http://jakarta.apache.org/velocity/

velocity/templates/login.vm

<?xml version="1.0"?>
   
#set($user = $request.getParameter("user"))
#set($pass = $request.getParameter("pass"))
   
<page>
   <title>Login Results</title>
   <content>
      #if ($pass == 'hi')
         <paragraph>Your In</paragraph>
      #else
         <paragraph>You missed</paragraph>
      #end
      <h3>Parameters</h3>
      #foreach ($name in $request.getParameterNames())
         <paragraph>
         $name = $request.getParameter($name)
         </paragraph>
      #end
   </content>
</page>

http://bismarck.sdsu.edu:9006/cocoon/velocity/index.html?user=foo&pass=hi&less=more

http://bismarck.sdsu.edu:9006/cocoon/velocity/index.html?pass=no

velocity/style/main.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   
   <xsl:template match="page">
      <html>
         <head>
            <title>
               <xsl:value-of select="title"/>
            </title>
   
         </head>
         <body>
            <xsl:apply-templates/>
         </body>
      </html>
   </xsl:template>
   
   <xsl:template match="title">
      <h2><xsl:apply-templates/></h2>
   </xsl:template>
   
   <xsl:template match="paragraph">
      <p><xsl:apply-templates/></p>
   </xsl:template>
   
</xsl:stylesheet>

velocity/sitemap.xmap

<?xml version="1.0"?>
<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
   <map:components>
      <map:generators default="file" />
      <map:transformers default="xslt" />
      <map:readers default="resources" />
      <map:serializers default="html" />
      <map:selectors default="browser" />
      <map:matchers default="wildcard" />
   </map:components>
   <map:pipelines>
      <map:pipeline>
         <map:match pattern="index.html">
            <map:generate type="velocity" src="templates/login.vm" />
            <map:transform type="xslt" src="style/main.xsl" />
            <map:serialize type="html" />
         </map:match>
      </map:pipeline>
   </map:pipelines>
</map:sitemap>

Added to Cocoon main site map

<map:pipeline>
     <map:match pattern="velocity/**">
        <map:mount uri-prefix="velocity" src="velocity/sitemap.xmap" 
           check-reload="yes" reload-method="synchron"/>
     </map:match>
  </map:pipeline>
Previous     visitors since 28-Sep-04     Next