sdpl 2002notes 7: apache cocoon1 7 xml web site architecture example: apache cocoon, a web...
TRANSCRIPT
SDPL 2002 Notes 7: Apache Cocoon 1
7 XML Web Site Architecture7 XML Web Site Architecture
Example:Example: Apache Cocoon Apache Cocoon, a Web publishing , a Web publishing architecture based on XML technologyarchitecture based on XML technology
http://xml.apache.org/cocoonhttp://xml.apache.org/cocoon
– Apache: "XML publishing framework"Apache: "XML publishing framework"» runs as a servlet (Java class under the control of a Web runs as a servlet (Java class under the control of a Web
server, to handle incoming HTTP request)server, to handle incoming HTTP request)
– Cocoon 1 (1999 –); not supported anymoreCocoon 1 (1999 –); not supported anymore– Cocoon 2Cocoon 2
» more efficient (based on SAX instead of DOM)more efficient (based on SAX instead of DOM)
» better site administration (with "sitemaps")better site administration (with "sitemaps")
SDPL 2002 Notes 7: Apache Cocoon 2
Cocoon Philosophy (1/2)Cocoon Philosophy (1/2)
Separation of concerns with "pyramid of contracts":Separation of concerns with "pyramid of contracts":
SDPL 2002 Notes 7: Apache Cocoon 3
Cocoon Philosophy (2/2)Cocoon Philosophy (2/2)
Different responsibilities Different responsibilities different groups different groups– Site administrators, Developers, Site administrators, Developers,
Content producers, Style designersContent producers, Style designers– related declarations separated in different filesrelated declarations separated in different files– supports co-operation of specialised groupssupports co-operation of specialised groups– enhances system modularity and extensibility enhances system modularity and extensibility – allows re-use of solutions to different tasksallows re-use of solutions to different tasks
Content, logic and style merged using XSL Content, logic and style merged using XSL transformationstransformations
SDPL 2002 Notes 7: Apache Cocoon 4
Example of typical useExample of typical use
Automatic generation of HTML from (static or generated) Automatic generation of HTML from (static or generated) XML documentsXML documents– client-dependent processing possible, serving, e.g.,client-dependent processing possible, serving, e.g.,
» HTML to "typical" web browsersHTML to "typical" web browsers» WML to WAP-devicesWML to WAP-devices» XML to XML/XSL aware clientsXML to XML/XSL aware clients
– also more sophisticated formatting (FOP also more sophisticated formatting (FOP PDF) PDF) In In Cocoon 1Cocoon 1 processing of a request (by a sequence of processing of a request (by a sequence of
ProcessorsProcessors and a and a FormatterFormatter) controlled by embedded ) controlled by embedded processing instructions (See next)processing instructions (See next)
SDPL 2002 Notes 7: Apache Cocoon 5
Example (of Cocoon Example (of Cocoon 11))
Different views of content for different clients:Different views of content for different clients:<?xml version = "1.0"?><?xml version = "1.0"?>
<!-- Fig. 16.1 in Deitel et al. --><!-- Fig. 16.1 in Deitel et al. -->
<!-- Pass document through an XSLT processor: --><!-- Pass document through an XSLT processor: -->
<?<?cocoon-processcocoon-process type="xslt"?> type="xslt"?>
<?xml-stylesheet href="welcome.xsl"<?xml-stylesheet href="welcome.xsl"type="text/xsl"?> <!-- default view -->type="text/xsl"?> <!-- default view -->
<?xml-stylesheet href = "welcome-wml.xsl"<?xml-stylesheet href = "welcome-wml.xsl"
type="text/xsl" type="text/xsl" media="wap"media="wap"?> <!-- wap-view -->?> <!-- wap-view -->
<myMessage><myMessage>
<message>Welcome to XML!</message><message>Welcome to XML!</message>
</myMessage></myMessage>
SDPL 2002 Notes 7: Apache Cocoon 6
Example (of Cocoon Example (of Cocoon 11) )
Cocoon 1 configured to handle various Cocoon 1 configured to handle various User-User-AgentsAgents of the HTTP request of the HTTP request– explorer, opera, lynx, netscape, java, wap explorer, opera, lynx, netscape, java, wap
Default style sheet Default style sheet welcome.xslwelcome.xsl specifies specifies generation of HTML, and generation of HTML, and welcome-wml.xsl welcome-wml.xsl processing for WAP devices processing for WAP devices – See nextSee next
SDPL 2002 Notes 7: Apache Cocoon 7
Stylesheet Stylesheet welcome-wml.xslwelcome-wml.xsl
<!-- Fig. 16.4 in Deitel et al. --><!-- Fig. 16.4 in Deitel et al. -->
<xsl:stylesheet version = "1.0"<xsl:stylesheet version = "1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">>
<xsl:template match = "myMessage"><xsl:template match = "myMessage">
<!-- Add a Cocoon instruction to <!-- Add a Cocoon instruction to
format the result as WML: -->format the result as WML: -->
<xsl:processing-instruction <xsl:processing-instruction name = "name = "cocoon-formatcocoon-format">">type = "text/wml"type = "text/wml"
</xsl:processing-instruction></xsl:processing-instruction>
… …
SDPL 2002 Notes 7: Apache Cocoon 8
Style sheet Style sheet welcome-wml.xsl welcome-wml.xsl (2) (2)
<wml><card><wml><card>
<p><p><xsl:value-of select="message"/><xsl:value-of select="message"/></p></p>
<p>This page has been transformed<p>This page has been transformed
from XML into WML by Cocoon's XSLT processor. from XML into WML by Cocoon's XSLT processor.
</p></p>
</card></card>
</wml></wml>
</xsl:template> <!-- for 'myMessage' --></xsl:template> <!-- for 'myMessage' -->
</xsl:stylesheet></xsl:stylesheet>
SDPL 2002 Notes 7: Apache Cocoon 9
Cocoon 2 PipelineCocoon 2 Pipeline
PipelinePipeline: sequence of component interactions to process a request: sequence of component interactions to process a request– typical case:typical case:
SDPL 2002 Notes 7: Apache Cocoon 10
Main Components of Cocoon 2Main Components of Cocoon 2 (1/2) (1/2)
GeneratorsGenerators create XML as a SAX stream. E.g: create XML as a SAX stream. E.g:– FileGenerator: XML file FileGenerator: XML file SAX stream SAX stream– HTMLGenerator: HTML file HTMLGenerator: HTML file XHTML (as SAX stream) XHTML (as SAX stream)– … … (and more components can be added)(and more components can be added)
TransformersTransformers manipulate a SAX stream and pass the manipulate a SAX stream and pass the result to the next componentresult to the next component– XSLT Transformer (default)XSLT Transformer (default)– Include Transformers (for XML inclusion via URL)Include Transformers (for XML inclusion via URL)– SQL Transformer (to access a DB, and include results)SQL Transformer (to access a DB, and include results)– … …
SDPL 2002 Notes 7: Apache Cocoon 11
Components of Cocoon 2 Components of Cocoon 2 (2)(2)
SerializersSerializers render a SAX stream to some result render a SAX stream to some result format (interpretable by the client)format (interpretable by the client)– HTML Serializer (default)HTML Serializer (default)– XML Serializer, Text SerializerXML Serializer, Text Serializer– PDF/PS Serializer (based on FOP)PDF/PS Serializer (based on FOP)– … …
Components and pipelines declared in a SitemapComponents and pipelines declared in a Sitemap– In XML file In XML file sitemap.xmapsitemap.xmap
(translated to (translated to sitemap_xmap.javasitemap_xmap.java) )
SDPL 2002 Notes 7: Apache Cocoon 12
Selecting a PipelineSelecting a Pipeline
SDPL 2002 Notes 7: Apache Cocoon 13
Declaring PipelinesDeclaring Pipelines
<!-- Fragment of sitemap.xmap: --><!-- Fragment of sitemap.xmap: --><map:sitemap<map:sitemap
xmlns:map="http://apache.org/cocoon/sitemap/1.0"xmlns:map="http://apache.org/cocoon/sitemap/1.0">><map:components> … </map:components><map:components> … </map:components><!-- Select pipeline based on request URI: --><!-- Select pipeline based on request URI: -->
<map:match pattern="hello.html"><map:match pattern="hello.html"><map:generate src="docs/hello-page.xml" /><map:generate src="docs/hello-page.xml" /><map:transform src="style/simpl2html.xsl"/><map:transform src="style/simpl2html.xsl"/><map:serialize type="html" /><map:serialize type="html" />
</map:match></map:match> … … </map:sitemap></map:sitemap>
SDPL 2002 Notes 7: Apache Cocoon 14
Setting up a pipelineSetting up a pipeline
SDPL 2002 Notes 7: Apache Cocoon 15
XSP (Extensible Server Pages)XSP (Extensible Server Pages)
Cocoon technology for dynamic contentCocoon technology for dynamic content Procedural code within static document content Procedural code within static document content
– similar with other server pages like JSP or ASPsimilar with other server pages like JSP or ASP <<xsp:pagexsp:page xmlns:xsp=" xmlns:xsp="http://apache.org/xsphttp://apache.org/xsp""
language="java"> ... language="java"> ...– XML document containing tag-based directives for XML document containing tag-based directives for
generating dynamic content at request timegenerating dynamic content at request time– translated by XSP processor into Cocoon Generatorstranslated by XSP processor into Cocoon Generators
SDPL 2002 Notes 7: Apache Cocoon 16
XSP: Embedding code into pagesXSP: Embedding code into pages
Example (Example (time.xsptime.xsp); Display the time of day:); Display the time of day:
<xsp:page<xsp:page language="java" language="java" xmlns:xsp="xmlns:xsp="http://www.apache.org/xsphttp://www.apache.org/xsp">">
<xsp:logic><xsp:logic> //Variable for the time of day://Variable for the time of day: Date now = new Date();Date now = new Date(); </xsp:logic></xsp:logic> <!-- root of resulting document: --><!-- root of resulting document: --> <page title="Time of Day"><page title="Time of Day"> <p>It’s now<p>It’s now <xsp:expr><xsp:expr>nownow</xsp:expr></xsp:expr></p></p> </page></page></xsp:page></xsp:page>
XSP processor generates a Generator ...XSP processor generates a Generator ...
SDPL 2002 Notes 7: Apache Cocoon 17
XSP page as Generator (fragments of)XSP page as Generator (fragments of)
public class time_xsp extends XSPGenerator {public class time_xsp extends XSPGenerator { … … //Variable for the time of day://Variable for the time of day:
Date now = new Date();Date now = new Date(); public void generate() throws SAXException {public void generate() throws SAXException {
this.contentHandler.startDocument();this.contentHandler.startDocument();AttributesImpl attrs=new AttributesImpl();AttributesImpl attrs=new AttributesImpl();attrs.addAttribute("", "attrs.addAttribute("", "titletitle", "", "titletitle", ",
"CDATA", ""CDATA", "Time of DayTime of Day");");this.contentHandler.startElement("", this.contentHandler.startElement("",
""pagepage", "", "pagepage", attrs);", attrs);… … this.contentHandler.endDocument();this.contentHandler.endDocument();}}
SDPL 2002 Notes 7: Apache Cocoon 18
XSP to Generated Content to ResultXSP to Generated Content to Result
SDPL 2002 Notes 7: Apache Cocoon 19
Example ResultExample Result
Final HTML page could be:Final HTML page could be:
<html><html><head><title><head><title>Time of DayTime of Day</title></head></title></head><body><body> <h3 style="color: navy; text-align: <h3 style="color: navy; text-align:
center">center">Time of DayTime of Day</h3></h3> <p>It's now <p>It's now Thu Dec 23 20:11:18 PST 1999Thu Dec 23 20:11:18 PST 1999</p></p></body></body></html></html>
OK, but can also separate logic from content OK, but can also separate logic from content (See next)(See next)
SDPL 2002 Notes 7: Apache Cocoon 20
Library TagsLibrary Tags
Could replace the preceding with a Could replace the preceding with a library tag:library tag:
...<p>It's now ...<p>It's now <util:date <util:date format="…"/>format="…"/></p> ...</p> ...
– Separation of concerns:Separation of concerns:» programmersprogrammers encapsulate behaviour in dynamic tags encapsulate behaviour in dynamic tags» content authorscontent authors use application-oriented markup to write use application-oriented markup to write
XML documentsXML documents Tag library: set of custom tags Tag library: set of custom tags
– for including code in documentsfor including code in documents– with its own namespacewith its own namespace
SDPL 2002 Notes 7: Apache Cocoon 21
Implementation of Tag LibrariesImplementation of Tag Libraries
Tag library implemented by a Tag library implemented by a logic sheetlogic sheet– XSLT stylesheet translating XSP XSLT stylesheet translating XSP XSP XSP– registered in configuration file registered in configuration file cocoon.xconfcocoon.xconf
<util:date format="yy/MM/dd hh:mm:ss aa" /><util:date format="yy/MM/dd hh:mm:ss aa" />
could be implemented by templatecould be implemented by template
<xsl:template match="util:date" ><xsl:template match="util:date" > <<xsp:exprxsp:expr>> formatDate(new Date(), "formatDate(new Date(), "<xsl:value-of <xsl:value-of
select='@format' />select='@format' />");"); </</xsp:exprxsp:expr>>
</xsl:template></xsl:template>
SDPL 2002 Notes 7: Apache Cocoon 22
Using Tag LibrariesUsing Tag Libraries
SDPL 2002 Notes 7: Apache Cocoon 23
Logic Sheets and Tag LibrariesLogic Sheets and Tag Libraries
Separation of document structure and … Separation of document structure and … – presentation: presentation: stylesheetsstylesheets give give presentation presentation
semanticssemantics to tags to tags– processing: processing: logic sheetslogic sheets give give procedural semanticsprocedural semantics
to tags to tags Built-in tag librariesBuilt-in tag libraries for various dynamic for various dynamic
information:information:– Request: request method & params, protocol, …Request: request method & params, protocol, …– Session: methods for HTTP SessionSession: methods for HTTP Session– ESQL: ESQL: SQL database queries SQL database queries
SDPL 2002 Notes 7: Apache Cocoon 24
ESQL: Example (1/2)ESQL: Example (1/2)
<?xml version="1.0" ?><?xml version="1.0" ?>
<xsp:page language="java" <xsp:page language="java" xmlns:xsp="http://www.apache.org/xsp"xmlns:xsp="http://www.apache.org/xsp"
xmlns:xmlns:esqlesql="="http://apache.org/cocoon/SQL/v2http://apache.org/cocoon/SQL/v2">"> <esql:connection><esql:connection> <esql:pool><esql:pool>connectionNameconnectionName</esql:pool></esql:pool> <esql:execute-query><esql:execute-query>
<esql:query><esql:query> SELECT name, addr FROM EmpSELECT name, addr FROM Emp</esql:query></esql:query>
… …
SDPL 2002 Notes 7: Apache Cocoon 25
ESQL: Example (2/2)ESQL: Example (2/2)
<esql:results><esql:results><table><table><esql:row-results><esql:row-results>
<tr><td><tr><td><esql:get-string column="<esql:get-string column="namename"/>"/></td></td> <td><td><esql:get-string column="<esql:get-string column="addraddr"/>"/></td></td> </tr></tr></esql:row-results></esql:row-results> </table></table> </esql:results></esql:results> <esql:no-results><esql:no-results> <p>Sorry, no results!</p><p>Sorry, no results!</p> </esql:no-results></esql:no-results> </esql:execute-query></esql:execute-query></esql:connection></esql:connection></xsp:page></xsp:page>
SDPL 2002 Notes 7: Apache Cocoon 26
Cocoon used in practise?Cocoon used in practise?
April 2001:April 2001:– 57 Web sites powered by Cocoon 157 Web sites powered by Cocoon 1
April 2002:April 2002:– 34 Web sites powered by Cocoon 134 Web sites powered by Cocoon 1– 17 Web sites powered by Cocoon 2 17 Web sites powered by Cocoon 2
» (34+17 < 57 !)(34+17 < 57 !)
– 9 service providers offering "Cocoon hosting"9 service providers offering "Cocoon hosting"
SDPL 2002 Notes 7: Apache Cocoon 27
SummarySummary
Apache CocoonApache Cocoon– "Publishing framework" based on XML technology"Publishing framework" based on XML technology
» esp. on XSLT transformationsesp. on XSLT transformations– Configurable architectureConfigurable architecture
» ComponentsComponents» SitemapSitemap
– XSP and tag libraries for dynamic contentXSP and tag libraries for dynamic content Interesting and promising conceptInteresting and promising concept
– Ideas may survive, perhaps in different systemsIdeas may survive, perhaps in different systems» (E.g. IBM ServletManager?) (E.g. IBM ServletManager?)