Upload
alexandra-armstrong
View
213
Download
0
Embed Size (px)
Citation preview
SDPL 2001 Notes 7.2: Apache Cocoon 1
7.2 Apache Cocoon7.2 Apache Cocoon
An example of a Web publishing architecture An example of a Web publishing architecture based on XML technologybased on XML technology
http://xml.apache.org/cocoonhttp://xml.apache.org/cocoon
– Apache: "Java publishing framework"Apache: "Java 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)
– "XML application server" in Bourret's categorisation "XML application server" in Bourret's categorisation (discussed in Section 7.1)(discussed in Section 7.1)
» "can publish XML to the Web but generally cannot receive "can publish XML to the Web but generally cannot receive data from the Web in the form of XML documents"data from the Web in the form of XML documents"
SDPL 2001 Notes 7.2: Apache Cocoon 2
Cocoon PhilosophyCocoon Philosophy
Separation of Separation of content, processing logiccontent, processing logic and and stylestyle– in different filesin different files– their creation/specification distinct activitiestheir creation/specification distinct activities– to enhance system modularity and extensibility to enhance system modularity and extensibility – to support co-operative site maintenance by groups to support co-operative site maintenance by groups
with specialised skillswith specialised skills– to allow re-use of solutions to different tasksto allow re-use of solutions to different tasks
Content, logic and style merged using XSL Content, logic and style merged using XSL transformationstransformations
SDPL 2001 Notes 7.2: Apache Cocoon 3
Typical UseTypical Use
Automatic generation of HTML through Automatic generation of HTML through processing (static or dynamically generated) XML processing (static or dynamically generated) XML filesfiles– 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
– more sophisticated formatting (FOP more sophisticated formatting (FOP PDF) also PDF) also possiblepossible
SDPL 2001 Notes 7.2: Apache Cocoon 4
Simple ExampleSimple Example
Different versions of content for different clients:Different versions 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. -->
<?cocoon-process type = "xslt"?><?cocoon-process type = "xslt"?>
<?xml-stylesheet href = "welcome.xsl"<?xml-stylesheet href = "welcome.xsl" type = "text/xsl"?> type = "text/xsl"?>
<?xml-stylesheet href = "welcome-wml.xsl"<?xml-stylesheet href = "welcome-wml.xsl"
type = "text/xsl" type = "text/xsl" media = "wap"media = "wap"?>?>
<myMessage><myMessage>
<message>Welcome to XML!</message><message>Welcome to XML!</message>
</myMessage></myMessage>
SDPL 2001 Notes 7.2: Apache Cocoon 5
Simple Example (2)Simple Example (2)
Cocoon is preconfigured to handle various Cocoon is preconfigured to handle various User-User-AgentsAgents of the HTTP request of the HTTP request– explorer, opera, lynx, java, wap, netscapeexplorer, opera, lynx, java, wap, netscape
Style sheet Style sheet welcome.xslwelcome.xsl specifies the default specifies the default processing, processing, and and welcome-wml.xsl welcome-wml.xsl (See next)(See next) processing processing for WAP devicesfor WAP devices
SDPL 2001 Notes 7.2: Apache Cocoon 6
Style sheet Style sheet welcome-wml.xslwelcome-wml.xsl
<?xml version = "1.0"?><?xml version = "1.0"?>
<!-- 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">
<!-- Specify an additional Cocoon step to <!-- Specify an additional Cocoon step to
format the result as WML(?): -->format the result as WML(?): -->
<xsl:processing-instruction <xsl:processing-instruction name = "cocoon-format">name = "cocoon-format">type = "text/wml"type = "text/wml"
</xsl:processing-instruction></xsl:processing-instruction>
… …
SDPL 2001 Notes 7.2: Apache Cocoon 7
Style sheet Style sheet welcome-wml.xsl welcome-wml.xsl (2) (2)
<wml><card><wml><card>
<p><xsl:value-of select = "message"/></p><p><xsl:value-of select = "message"/></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></xsl:template>
</xsl:stylesheet></xsl:stylesheet>
SDPL 2001 Notes 7.2: Apache Cocoon 8
Cocoon InternalsCocoon Internals
Based on the Based on the reactorreactor design pattern: design pattern:
SDPL 2001 Notes 7.2: Apache Cocoon 9
Components of Cocoon ArchitectureComponents of Cocoon Architecture
RequestRequest: client's HTTP request: client's HTTP request ProducerProducer: produces an XML document from the : produces an XML document from the
request for the process request for the process – dynamically, or by loading a filedynamically, or by loading a file
Reactor: Reactor: chooses processors/formatters to work chooses processors/formatters to work on the document, based on PIson the document, based on PIs
<?cocoon-process type="xxx" ?><?cocoon-process type="xxx" ?> and and
<?cocoon-format type="xxx" ?><?cocoon-format type="xxx" ?>
SDPL 2001 Notes 7.2: Apache Cocoon 10
Components of Cocoon Architecture (2)Components of Cocoon Architecture (2)
ProcessorProcessor: transforms given XML document into : transforms given XML document into something else. Examplessomething else. Examples– the XSLT processorthe XSLT processor– the XSP processorthe XSP processor
» evaluates XSP pages and compiles them into producersevaluates XSP pages and compiles them into producers» considered a Producer in Cocoon 2considered a Producer in Cocoon 2
FormatterFormatter: transforms the document into a : transforms the document into a stream interpretable by the clientstream interpretable by the client
SDPL 2001 Notes 7.2: Apache Cocoon 11
Components of Cocoon Architecture (3)Components of Cocoon Architecture (3)
ResponseResponse: encapsulates the formatted document : encapsulates the formatted document and its properties (length, MIME type, …) and its properties (length, MIME type, …)
Loader: Loader: loads the formatted result when it is loads the formatted result when it is executable codeexecutable code– The same process used for creating The same process used for creating producerproducer
components! components! (compiled server pages, written with XSP; more later)(compiled server pages, written with XSP; more later)
SDPL 2001 Notes 7.2: Apache Cocoon 12
OptimisationsOptimisations
Performance and memory usage a major concernPerformance and memory usage a major concern Processing requirements of XML parsing, XSLT Processing requirements of XML parsing, XSLT
transformations and formatting are heavytransformations and formatting are heavy Dynamic server pages Dynamic server pages compiledcompiled to executable to executable
Java classesJava classes A special A special cache systemcache system
– both static and dynamically generated pages served both static and dynamically generated pages served from the cache (if they are up-to-date)from the cache (if they are up-to-date)
Efficient enough? Efficient enough? – 57 Cocoon-based Web sites mentioned (April 2001)57 Cocoon-based Web sites mentioned (April 2001)
SDPL 2001 Notes 7.2: Apache Cocoon 13
XSP (Extensible Server Pages)XSP (Extensible Server Pages)
Cocoon's technology for building dynamic Web Cocoon's technology for building dynamic Web applicationsapplications
XSP pageXSP page – an XML document containing tag-based directives that an XML document containing tag-based directives that
specify generation of dynamic content at request timespecify generation of dynamic content at request time– transformed into Cocoon producerstransformed into Cocoon producers
Supports embedding procedural code into static document Supports embedding procedural code into static document content, similarly to other server pages like JSP or ASPcontent, similarly to other server pages like JSP or ASP– See example nextSee example next
SDPL 2001 Notes 7.2: Apache Cocoon 14
XSP: Embedding code into pagesXSP: Embedding code into pages
Generate a greeting based on the time of day:Generate a greeting based on the time of day:. . .. . .<p> <p> GoodGood <xsp:logic><xsp:logic>
String timeOfDay = (String timeOfDay = ( new SimpleDateFormat("aa")new SimpleDateFormat("aa") ).format(new Date());).format(new Date()); if (timeOfDay.equals("AM")) {if (timeOfDay.equals("AM")) { <xsp:content><xsp:content>MorningMorning</xsp:content></xsp:content> } else {} else { <xsp:content><xsp:content>AfternoonAfternoon</xsp:content></xsp:content> } } </xsp:logic></xsp:logic>!!</p> . . .</p> . . .
OK, but we can also avoid intertwining code and OK, but we can also avoid intertwining code and content (see next)content (see next)
SDPL 2001 Notes 7.2: Apache Cocoon 15
XSP: Library TagsXSP: Library Tags
Could replace the preceding withCould replace the preceding with
. . . <p> Good . . . <p> Good <util:time-of-day /><util:time-of-day />!</p> . . .!</p> . . .
<util:time-of-day /> <util:time-of-day /> is a is a library-taglibrary-tag– Extensibility!Extensibility!– Supports division of labour:Supports division of labour:
» 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» presentation designerspresentation designers develop XSL style sheets develop XSL style sheets
SDPL 2001 Notes 7.2: Apache Cocoon 16
Example: Full XSP pageExample: Full XSP page
<?xml version="1.0"?><?xml version="1.0"?><!-- Generate a producer out of this page:--><!-- Generate a producer out of this page:--><?cocoon-process type="xsp"?><?cocoon-process type="xsp"?><!-- Apply style sheet to the result of producer: --><!-- Apply style sheet to the result of producer: --><?cocoon-process type="xslt"?><?cocoon-process type="xslt"?><?xml-stylesheet href="sample.xsl" type="text/xsl"?><?xml-stylesheet href="sample.xsl" type="text/xsl"?><<xsp:pagexsp:page language="java" language="java"
xmlns:xsp="http://www.apache.org/1999/XSP/Core">xmlns:xsp="http://www.apache.org/1999/XSP/Core"> <page title="Time of Day"> <!-- root of document --><page title="Time of Day"> <!-- root of document --> <<xsp:logicxsp:logic> //Variable for the time of day:> //Variable for the time of day: Date now = new Date();Date now = new Date(); </</xsp:logicxsp:logic>> <p>The time is now <<p>The time is now <xsp:exprxsp:expr>now</>now</xsp:exprxsp:expr></p>></p> </page></page></xsp:page></xsp:page>
SDPL 2001 Notes 7.2: Apache Cocoon 17
Example: ResultExample: Result
Upon Cocoon processing (formatted according to Upon Cocoon processing (formatted according to sample.xslsample.xsl)), the page could be:, the page could be:
<html><html><head><title>Time of Day</title></head><head><title>Time of Day</title></head><body><body> <h3 style="color: navy; text-align: <h3 style="color: navy; text-align:
center">Time of Day</h3>center">Time of Day</h3> <p>It's now Thu Dec 23 20:11:18 PST 1999</p><p>It's now Thu Dec 23 20:11:18 PST 1999</p></body></body></html></html>
SDPL 2001 Notes 7.2: Apache Cocoon 18
Implementation of Tag LibrariesImplementation of Tag Libraries
Libraries of dynamic tags are implemented as Libraries of dynamic tags are implemented as corresponding style sheets. For example, corresponding style sheets. For example,
<util:time-of-day format="yy/MM/dd hh:mm:ss aa" /><util:time-of-day format="yy/MM/dd hh:mm:ss aa" />
could be implemented by templatecould be implemented by template
<xsl:template match="util:time-of-day" ><xsl:template match="util:time-of-day" > <<xsp:exprxsp:expr>> formatDate(new Date(), "<xsl:value-of formatDate(new Date(), "<xsl:value-of
select='@format' />");select='@format' />"); </</xsp:exprxsp:expr>>
</xsl:template></xsl:template>
SDPL 2001 Notes 7.2: Apache Cocoon 19
Implementation of Tag Libraries (2)Implementation of Tag Libraries (2)
Built-in XSP directives implemented by replacing Built-in XSP directives implemented by replacing them (via an XSLT stylesheet) by corresponding them (via an XSLT stylesheet) by corresponding source codesource code
Compare: Compare: – Separation of document structure and Separation of document structure and presentationpresentation by by
style sheets that give style sheets that give presentation semanticspresentation semantics to tags to tags– Separation of document structure and Separation of document structure and processingprocessing by by
style sheets that give style sheets that give procedural semanticsprocedural semantics to tags to tags
"Logic sheets"!"Logic sheets"!