SDSU Emerging Technology
Fall Semester, 2004
More Cocoon Flow
Previous     Lecture Notes Index     Next     
© 2004, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 05-Oct-04

CS 683 Emerging Technologies Fall Semester, 2004 Doc 17 More Cocoon Flow

Contents

References

More Cocoon Flow

Hello World Example

Hello World With Main Sitemap Changed

Using Java with Velocity

Displaying XSLT in Pages

Sequential Login

Optional Login

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 Flow Documentation

http://cocoon.apache.org/2.1/userdocs/flow/index.html

More Cocoon Flow

Cocoon Flow requires Cocoon 2.1.x

All the examples in this document use Cocoon 2.1

Hello World Example

Files

No additions to cocoon main sitemap are needed

As directory structure and url to sitemap match

http://bismarck.sdsu.edu:9006/cocoon/hello2/hello.html

hello2/documents/hello.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
   <title>Hello</title>
</head>
<body>
Hello World
</body>
</html>

hello2/sitemap.xmap

<?xml version="1.0" encoding="UTF-8"?>
<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
   
<map:components>
   <map:generators default="file" />
   <map:transformers default="xslt"/>
   <map:serializers default="html"/>
   <map:matchers default="wildcard"/>
   <map:selectors default="browser" />
   <map:pipes default="caching"/>
</map:components>
   
<map:pipelines>
   <map:pipeline>
      <map:match pattern="hello.html">
         <map:read type="resource" mime-type="text/html" src="documents/hello.html" />
      </map:match>
   </map:pipeline>
</map:pipelines>
</map:sitemap>

Hello World With Main Sitemap Changed

http://bismarck.sdsu.edu:9006/cocoon/goofy/hello.html

Adding a new Pipeline

The new pipeline must be placed before the main pipeline

<map:pipeline>
   <map:match pattern="goofy/**">
      <map:mount uri-prefix="goofy" src="hello2/sitemap.xmap" 
         check-reload="yes" reload-method="synchron"/>
   </map:match>
</map:pipeline> 
   
<!-- main pipeline -->
 <map:pipeline>

Add a new Match to the Main Pipeline

<!-- main pipeline -->
<map:pipeline>
   <map:match pattern="goofy/**">
      <map:mount uri-prefix="goofy" src="hello2/sitemap.xmap" 
            check-reload="yes" reload-method="synchron"/>
    </map:match>

Rest of the Example will not Change Main sitemap

They will map the url to the file structure using default matching

Using Java with Velocity

Files

http://bismarck.sdsu.edu:9006/cocoon/javaNoXslt/simpleJava.html

javaNoXslt/flow/main.js

function simpleJava() {
   var text = "";
   var encoded = "";
   var upperCase = "";
      
   while (true) {
      cocoon.sendPageAndWait( "simpleJava.vm", 
         {text: text, encoded: encoded, upperCase: upperCase});
      text = cocoon.request.get("input");
      var javaString = new java.lang.String( text);
      encoded = javaString.replaceAll( "<", "&lt;");
      upperCase = encoded.toUpperCase();
   }
}

javaNoXslt/documents/simpleJava.vm


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
   <title>Sample Java</title> 
</head>
<body>
<p>No XSLT, Html is ok</p>
#set( $kont = ".kont") 
<form action="$continuation.id$kont" method="post">
<textarea name="input" rows="15" cols="35">$text 
</textarea>
<input type="submit" /> 
</form>
<hr />
You entered: <pre>$encoded </pre>
<hr />
Text as html:$text
<hr />
Text as Uppercase: $upperCase 
</body>
</html>

Warning: make sure that there is an space between a variable ($encoded) and a tag (</pre>)

javaNoXslt/sitemap.xmap

This simplified sitemap works for this example

<?xml version="1.0" encoding="UTF-8"?>
<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
   
<map:components>
   <map:generators default="file"/>
   <map:transformers default="xslt"/>
   <map:serializers default="html"/>
   <map:matchers default="wildcard"/>
   <map:selectors default="browser" />
   <map:actions/>
   <map:pipes default="caching"/>
</map:components>
   
<map:flow language="javascript">
   <map:script src="flow/main.js"/>
</map:flow>
   
<map:pipelines>
   <map:component-configurations>
      <global-variables/>
   </map:component-configurations>
   
   <map:pipeline>
      <map:match pattern="*.html">
         <map:call function="{1}"/>
      </map:match>

javaNoXslt/sitemap.xmap Continued

      <map:match pattern="*.vm">
         <map:generate type="velocity" src="documents/{1}.vm"/>
         <map:serialize type="html"/>
      </map:match>
   
      <map:match pattern="*.kont">
         <map:call continuation="{1}"/>
      </map:match>
   
   </map:pipeline>
   
</map:pipelines>
   
</map:sitemap>

Displaying XSLT in Pages

Files

http://bismarck.sdsu.edu:9006/cocoon/javaXslt/simpleJava.html

javaXslt/flow/main.js

function simpleJava() {
   var text = "";
   var encoded = "";
   var upperCase = "";
      
   while (true) {
      cocoon.sendPageAndWait( "simpleJava.vm", 
         { encoded: encoded});
      text = cocoon.request.get("input");
      var javaString = new java.lang.String( text);
      encoded = javaString.replaceAll( "<", "&lt;");
   }
}

javaXslt/documents/simpleJava.vm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
   <title>Sample Java</title> 
</head>
<body>
<p>XSLT is ok</p>
#set( $kont = ".kont") 
<form action="$continuation.id$kont" method="post">
<textarea name="input" rows="15" cols="35">$encoded 
</textarea>
<input type="submit" /> 
</form>
<hr />
You entered: <pre>$encoded </pre>
<hr />
Can't display XSLT as html tags. 
What would it mean if one could?
   
</body>
</html>

javaXslt/sitemap.xmap

Same as on slides 8 & 9

Sequential Login

Files

http://bismarck.sdsu.edu:9006/cocoon/sequentialLogin/login.html

User must login

sequentialLogin/flow/main.js

function login() {
   var invalidName;
   var name;
   
   while (true) {
      cocoon.sendPageAndWait( "login.vm", 
         { name: name, notValid: invalidName});
      name = cocoon.request.get("name");
      if (name == "sam")
         break;
      invalidName = true;
    }
   
   var encoded = "";
   while (true) {
      cocoon.sendPageAndWait( "main.vm", 
         { encoded: encoded, name: name});
      var text = cocoon.request.get("input");
      var javaString = new java.lang.String( text);
      encoded = javaString.replaceAll( "<", "&lt;");
   }
}

sequentialLogin/documents/login.vm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
   <title>Login</title> 
</head>
<body>
<p>Enter your name</p>
#set( $kont = ".kont") 
<form action="$continuation.id$kont" method="post">
<input type="text" name="name" size="40" />
<input type="submit" /> 
</form>
#if ( $notValid )
   <p>Sorry but $name is not authorized to continue.</p>
#end
</body>
</html>

sequentialLogin/documents/main.vm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
   <title>Main Page for $name </title> 
</head>
<body>
<p>Hello $name. Please enter some text.</p>
#set( $kont = ".kont") 
<form action="$continuation.id$kont" method="post">
<textarea name="input" rows="15" cols="35">$encoded 
</textarea>
<input type="submit" /> 
</form>
<hr />
<p>You entered:</p> <pre>$encoded </pre>
<hr />
</body>
</html>

Optional Login

Files

http://bismarck.sdsu.edu:9006/cocoon/login/main.html

User can login to get more functionality

login/flow/main.js

function main() {
   var name = "";
   while (true) {
      var action = cocoon.request.get("action");
      if (action == null) 
         action = "text";
      if (action == "login")
         name = login();
      else
         displayTextPage(name);
   }
}
   
function displayTextPage(name ) {
      var text = cocoon.request.get("input");
      if (text == null)
         text ="cat";
      var javaString = new java.lang.String( text);
      var encoded = javaString.replaceAll( "<", "&lt;");
      cocoon.sendPageAndWait( "main.vm", 
         { encoded: encoded, name: name});
}

login/flow/main.js Continued

function login() {
   var invalidName;
   
   while (true) {
      cocoon.sendPageAndWait( "login.vm", 
         { name: name, notValid: invalidName});
      var action = cocoon.request.get("action");
      if (action == "Cancel" )
         return "";
      var name = cocoon.request.get("name");
      if (name == "sam")
         return name;
      invalidName = true;
    }
   
}

login/documents/main.vm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
#if ($name == "")
   <title>Main Page</title> 
#else
   <title>Main Page for $name </title> 
#end
   
</head>
<body>
#if ($name == "")
   <p>Please enter some text.</p>
#else
   <p>Hello $name. Please enter some text.</p>
#end
#set( $kont = ".kont") 
<form action="$continuation.id$kont" method="post">
<textarea name="input" rows="15" cols="35">$encoded 
</textarea>
<input type="hidden" name="action" value="text" />
<input type="submit" /> 
</form>

login/documents/main.vm Continued

<form action="$continuation.id$kont" method="post">
<input type="submit" value="Login"/> 
<input type="hidden" name="action" value="login" />
</form>
<hr />
<p>You entered:</p> <pre>$encoded </pre>
<hr />
</body>
</html>

login/documents/login.vm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
   <title>Login</title> 
</head>
<body>
<p>Enter your name</p>
   
#set( $kont = ".kont") 
<form action="$continuation.id$kont" method="post">
<input type="text" name="name" size="40" />
<input type="submit" /> 
<input type="submit" name="action" value="Cancel" /> 
</form>
#if( $notValid )
   <p>Sorry but $name is not authorized.</p>
#end
</body>
</html>

Previous     visitors since 05-Oct-04     Next