1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
|
<HTML><HEAD><TITLE>User Guide</TITLE><LINK href="resources/simple.css" rel="stylesheet" title="Simple Style" type="text/css"></HEAD><BODY><P class="legal">Cocoon Documentation</P><H1 class="title">User Guide</H1><DOCUMENT>
<BODY>
<H1>Introduction</H1><DIV id="s1">
<P>This document assumes the knowledge of the W3C recommendation
or working drafts used in Cocoon (mainly <A href="http://www.w3.org/TR/REC-xml">XML</A>,
<A href="http://www.w3.org/TR/WD-xsl">XSL</A> in both its <A href="http://www.w3.org/TR/WD-xslt">transformation</A>
and <A href="http://www.w3.org/TR/WD-xsl">formatting</A> capabilities). This
document is not intended to be an XML or XSL tutorial but just shows how these
technologies may be used inside the Cocoon framework to create web content.</P>
</DIV>
<H1>General overview</H1><DIV id="s1">
<P>Cocoon is a publishing system that allows you to separate web development into
three different layers: content, style and logic.</P>
<P>In a way, Cocoon does not aim to simplify the creation of web content:
in fact, it is harder to create XML/XSL content than it is to use HTML from
the beginning. So, if you
are happy with the web technology you are using today, don't waste your time
and stick with what you already have. Otherwise, if your troubles are site
management, if your graphics people are always in the way, if your HTML authors
always mess up your page logic, if your managers see no results in hiring new
people to work on the site - go on and make your life easier!</P>
<P>This comment posted on the Cocoon mailing list shows you what we mean:</P>
<PRE>
I've got a site up and running that uses Cocoon.
It rocks, the management loves me (they now treat
me like I walk on water), and a couple of summer
interns that I had helping me on the project are
suddenly getting massively head-hunted by companies
like AT&T now that they can put XML and XSL on
their resumes. In a word: Cocoon simply rocks!
</PRE>
</DIV>
<H1>Hello World</H1><DIV id="s1">
<P>Every good user guide starts with a <CODE>Hello World</CODE> example,
and since we hope to write good documentation (even if it's as hard as hell!),
we'll start from there too. Here is a well-formed XML file that uses a custom
and simple set of tags:</P>
<PRE>
<?xml version="1.0"?>
<page>
<title>Hello World!</title>
<content>
<paragraph>This is my first Cocoon page!</paragraph>
</content>
</page>
</PRE>
<P>Even if this page mimics HTML (in a sense, HTML was born as a simple DTD
for homepages), it is helpful to note that there is no style information
and all the styling and graphic part is missing. Where do I put the title? How
do I format the paragraph? How do I separated the content from the other
elements? All these questions do not have answers because in this context they
don't need one: this file should be created and maintained by people that
don't need to be aware of how this content if further processed to become a
served web document.</P>
<P>On the other hand, we need to indicate how the presentation questions will
be answered. To do this, we must indicate a <EM>stylesheet</EM> that is able to
indicate how to interpret the elements found in this document. Thus, we
follow a <A href="http://www.w3.org/TR/WD-xml-stylesheet">W3C recommendation</A>
and add the XML processing instruction to map a stylesheet to a document:</P>
<PRE><?xml-stylesheet href="hello.xsl" type="text/xsl"?></PRE>
<P>Now that our content layer is done, we need to create a stylesheet to
convert it to a format readable by our web clients. Since most available web
clients use HTML as their <EM>lingua franca</EM>, we'll write a stylesheet to
convert our XML in HTML (More precisely, we convert to XHTML which is the XML
form of HTML, but we don't need to be that precise at this point).</P>
<P>Every valid stylesheet must start with the root element <EM>stylesheet</EM>
and define its own namespace according to the W3C directions. So the
skeleton of your stylesheet is:</P>
<PRE>
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
</xsl:stylesheet>
</PRE>
<P>Once the skeleton is done, you must include your <CODE>template</CODE> elements,
which are the basic units of operation for the XSLT language. Each template is
matched against the occurence of some elements in the original document and
the element is replaced with the child elements of the template,
if they belong to other namespaces, or, if they belong to the XSLT namespace,
they are further processed in a recursive way.</P>
<P>Let's see an example: in our <CODE>HelloWorld.xml</CODE> document
<CODE>page</CODE> is the root element. This must be transformed into all those tags
that identify a good HTML page. Your template becomes:</P>
<PRE>
<xsl:template match="page">
<html>
<head>
<title><xsl:value-of select="title"/></title>
</head>
<body bgcolor="#ffffff">
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
</PRE>
<P>where some elements belong to the standard namespace (which we mentally
associate with HTML) and some others to the <EM>xsl:</EM> namespace.
Here we find two of those
XSLT elements: <CODE>value-of</CODE> and <CODE>apply-templates</CODE>. While
the first <EM>searches</EM> the <CODE>page</CODE> element's direct children for the
<CODE>title</CODE> element and
replaces it with the content of the retrieved element, the second indicates
to the processor that it should continue the processing of the other templates
described in the stylesheet from that point on in the input document
(known as the <EM>context node</EM>).</P>
<P>Some other possible templates are:</P>
<PRE>
<xsl:template match="title">
<h1 align="center">
<xsl:apply-templates/>
</h1>
</xsl:template>
<xsl:template match="paragraph">
<p align="center">
<i><xsl:apply-templates/></i>
</p>
</xsl:template>
</PRE>
<P>After the XSLT processing, the original document is transformed to</P>
<PRE>
<html>
<head>
<title>Hello</title>
</head>
<body bgcolor="#ffffff">
<h1 align="center">Hello</h1>
<p align="center">
<i>This is my first Cocoon page!</i>
</p>
</body>
</html>
</PRE>
</DIV>
<H1>Browser Dependent Styling</H1><DIV id="s1">
<P>When a document is processed by an XSLT processor, its output is exactly the same for every browser that requested
the page. Sometimes it's very helpful to be able to discover the client's
capabilities and transform content layer into different views/formats. This is
extremely useful when we want to serve content do very different types of
clients (fat clients like desktop workstations and thin clients like wireless
PDAs), but we want to use the same information source and create the smallest
possible impact on the site management costs.</P>
<P>Cocoon is able to discriminate between browsers, allowing the different stylesheets to
be applied. This is done by indicating in the stylesheet linking PI the <EM>media</EM>
type. For example, continuing with the HelloWorld.xml document, these PIs</P>
<PRE>
<?xml version="1.0"?>
<?xml-stylesheet href="hello.xsl" type="text/xsl"?>
<?xml-stylesheet href="hello-text.xsl" type="text/xsl" media="lynx"?>
...
</PRE>
<P>would tell Cocoon to apply the <CODE>hello-text.xsl</CODE> stylesheet if the Lynx browser
is requesting the page. This powerful feature allows you to design your content
independently and to choose its presentation depending on the capabilities of the browser
agent.</P>
<P>The media type of each browser is evaluated by Cocoon at request time,
based on their <CODE>User-Agent</CODE> http header information.
Cocoon is preconfigured to handle these browsers:</P>
<DL>
<LI><STRONG>explorer</STRONG> -
any Microsoft Internet Explorer, searches for MSIE (before
searching for Mozilla, since IE pretends to be Mozilla too)
</LI>
<LI><STRONG>opera</STRONG> -
the Opera browser (before searching for Mozilla, since
Opera pretends to be Mozilla too)
</LI>
<LI><STRONG>lynx</STRONG> - the text-only Lynx browser</LI>
<LI><STRONG>java</STRONG> - any Java code using standard URL classes</LI>
<LI><STRONG>wap</STRONG> - the Nokia WAP Toolkit browser</LI>
<LI><STRONG>netscape</STRONG> - any Netscape Navigator or Mozilla, searches for Mozilla</LI>
</DL>
<P>but you can add your own by personalizing the
<CODE>cocoon.properties</CODE> file, modifying the <CODE>browser</CODE>
properties. For example:</P>
<PRE>
browser.0=explorer=MSIE
browser.1=opera=Opera
browser.2=lynx=Lynx
browser.3=java=Java
browser.4=wap=Nokia-WAP-Toolkit
browser.5=netscape=Mozilla
</PRE>
<P>indicates that Cocoon should look for the token <EM>MSIE</EM> inside the
User-Agent HTTP request header first, then <EM>Opera</EM>
and so on, until <EM>Mozilla</EM>.
If you want to recognize different generations of the same browser you should
find the specific string you should look for, and
- this is very important - indicate the order of matching, since
other browsers' User Agent strings may contain the same string
(see examples above).</P>
</DIV>
<H1>Using query parameters during XSL transformation</H1><DIV id="s1">
<P>Quite often you want to create pages that depend on some
user-supplied data. One way to do this is using HTML forms.
Cocoon provides you with a simple way to use this data. Let's
assume you've got the following list and you want the user
to choose a country code and be shown the name of the corresponding
country.
</P>
<PRE>
<?xml version="1.0"?>
<?cocoon-process type="xslt"?>
<?xml-stylesheet href="page.xsl" type="text/xsl"?>
<page>
<country code="ca">Canada</country>
<country code="de">Germany</country>
<country code="fr">France</country>
<country code="uk">United Kingdom</country>
<country code="us">United States</country>
<country code="es">Spain</country>
</page>
</PRE>
<P>You now use the following XSL stylesheet with it</P>
<PRE>
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="countrycode"/>
<xsl:template match="page">
<html>
<body>
<xsl:choose>
<xsl:when test="not($countrycode)">
<p>Choose a country:</p>
<form action="countries.xml" method="get">
<select name="countrycode" size="1">
<xsl:apply-templates select="country" mode="form"/>
</select>
<input type="submit"/>
</form>
</xsl:when>
<xsl:when test="country[@code=$countrycode]">
<xsl:apply-templates select="country[@code=$countrycode]"
mode="selected"/>
</xsl:when>
<xsl:otherwise>
<p>Unknown country code
<em><xsl:value-of select="$countrycode"/></em>.
</p>
</xsl:otherwise>
</xsl:choose>
</body>
</html>
</xsl:template>
<xsl:template match="country" mode="form">
<option><xsl:value-of select="@code"/></option>
</xsl:template>
<xsl:template match="country" mode="selected">
<p><em><xsl:value-of select="@code"/></em> stands for
<xsl:value-of select="text()"/></p>
</xsl:template>
</xsl:stylesheet>
</PRE>
<P>Viewing <CODE>countries.xml</CODE> now will yield different
results. When no parameter is given (i.e. using the URL
<CODE>countries.xml</CODE>) the browser will receive the following page:
</P>
<PRE>
<html>
<body>
<p>Choose a country:</p>
<form action="countries.xml" method="get">
<select name="countrycode" size="1">
<option>ca</option>
<option>de</option>
<option>fr</option>
<option>uk</option>
<option>us</option>
<option>es</option>
</select>
<input type="submit">
</form>
</body>
</html>
</PRE>
<P>Choosing one of the options in the list will result in a request
for a URL like <CODE>countries.xml?countrycode=fr</CODE> and this page
will look like:
</P>
<PRE>
<html>
<body>
<p><em>fr</em> stands for France</p>
</body>
</html>
</PRE>
<P>If for some reason no country element matching the
<CODE>countrycode</CODE> parameter is found (e.g.
<CODE>countries.xml?countrycode=foo</CODE>, you will get the following
page:</P>
<PRE>
<html>
<body>
<p>Unknown country code <em>foo</em>.</p>
</body>
</html>
</PRE>
</DIV>
<H1>Cocoon Internals</H1><DIV id="s1">
<P>The Cocoon publishing system has an engine based on the <EM>reactor</EM> design
pattern which is described in the picture below:</P>
<IMG alt="Cocoon Schema" class="figure" src="images/schema.jpg">
<P>Let's describe the components that appear on the schema:</P>
<DL>
<LI><STRONG>Request</STRONG> -
Wraps around the client's request and
contains all the information needed by the processing engine. The request
must indicate which client generated the request, which URI is being
requested and which producer should handle the request.
</LI>
<LI><STRONG>Producer</STRONG> -
Handles the requested URI and produces an
XML document. Since producers are pluggable, they work like subservlets
for this framework, allowing users to define and implement their own
producers. A producer is responsible for creating the XML document which is
fed into the processing reactor. It's up to the producer implementation to
define the function that produces the document from the request object.
</LI>
<LI><STRONG>Reactor</STRONG> -
Responsible for evaluating which processor should work on the document by
reacting on XML processing
instructions. The reactor pattern is different from a processing pipeline
since it allows the processing path to be dynamically configurable and it
increases performance since only those required processors are called to
handle the document. The reactor is also responsible for forwarding the
document to the appropriate formatter.
</LI>
<LI><STRONG>Formatter</STRONG> -
Transforms the memory representation of
the XML document into a stream that may be interpreted by the requesting
client. Depending on other processing instructions, the document leaves
the reactor and gets formatted for its consumer. The output MIME type of
the generated document depends on the formatter implementation.
</LI>
<LI><STRONG>Response</STRONG> -
Encapsulates the formatted document along
with its properties (such as length, MIME type, etc..)
</LI>
<LI><STRONG>Loader</STRONG> -
Responsible for loading the formatted
document when this is executable code. This part is used for compiled
server pages (principally XSP) where the separation of content and logic
is merged and
compiled into a Producer. When the formatter output is executable code, it
is not sent back to the client directly, but it gets loaded and executed
as a document producer. This guarantees both performance improvement
(since the producers are cached) as well as easier producer development,
following the common compiled server pages model.
</LI>
</DL>
</DIV>
<H1>Cocoon Processing Instructions</H1><DIV id="s1">
<P>The Cocoon reactor uses XML processing instructions to forward the document
to the right processor or formatter. These processing instructions are:</P>
<PRE>
<?cocoon-process type="xxx"?> for processing
and
<?cocoon-format type="yyy"?> for formatting
</PRE>
<P>These PIs are used to indicate the processing and formatting path that the
document should follow to be served. In the example above, we didn't use them
and Cocoon wouldn't know (despite the presence of the XSL PIs) that the
document should be processed by the XSLT processor. To do this, the HelloWorld.xml
document should be modified like this:</P>
<PRE>
<?xml version="1.0"?>
<?cocoon-process type="xslt"?>
<?xml-stylesheet href="hello.xsl" type="text/xsl"?>
<page>
<title>Hello World!</title>
<content>
<paragraph>This is my first Cocoon page!</paragraph>
</content>
</page>
</PRE>
<P>The other processing instruction is used to indicate what formatter should
be used to transform the document tree into a suitable form for the requesting
client. For example, in the document below that uses the XSL formatting object
namespace, the Cocoon PI indicates that this document should be formatted using the
formatter associated to the <CODE>text/xslfo</CODE> document type.</P>
<PRE>
<?xml version="1.0"?>
<?cocoon-format type="text/xslfo"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master
page-master-name="one"
margin-left="100pt"
margin-right="100pt">
<fo:region-body margin-top="50pt"
margin-bottom="50pt"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence>
<fo:sequence-specification>
<fo:sequence-specifier-repeating
page-master-first="one"
page-master-repeating="one"/>
</fo:sequence-specification>
<fo:flow font-size="14pt" line-height="14pt">
<fo:block>Welcome to Cocoon</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</PRE>
</DIV>
<H1>Cocoon Cache System</H1><DIV id="s1">
<P>In a complex server environment like Cocoon, performance and
memory usage are critical issues. Moreover, the processing requirement for
both XML parsing, XSLT transformations, document processing and formatting are
too heavy even for the lightest serving environment based on the fastest
virtual machine. For this reason, a special cache system was designed underneath the Cocoon
engine and its able to cache both static and dynamically created pages.</P>
<P>Its operation is simple but rather powerful:</P>
<UL>
<LI>when the request comes, the cache is searched.</LI>
<UL>
<LI>if the request is found;</LI>
<UL>
<LI>its <EM>changeable</EM> points are evaluated</LI>
<LI>if all changeable points are unchanged</LI>
<UL>
<LI>the page is served directly from the cache</LI>
</UL>
<LI>if a single point has changed and requires reprocessing</LI>
<UL>
<LI>the page is invalidated and continues as if it wasn't found</LI>
</UL>
</UL>
<LI>if the request is not found:</LI>
<UL>
<LI>the page is normally processed</LI>
<LI>it's sent to the client</LI>
<LI>it's stored into the cache</LI>
</UL>
</UL>
</UL>
<P>This special cache system is required since the page is
processed with the help of many components which, independently, may change
over time. For example, a stylesheet or a file template may be updated on
disk. Every processing logic that may change its behavior over time it's
considered <EM>changeable</EM> and checked at request time for change.</P>
<P>Each changeable point is queried at request time and it's up
to the implementation to provide a fast method to check if the stored page is
still valid. This allows even dynamically generated pages (for example,
an XML template page created by querying a database) to be cached and,
assuming that request frequency is higher than the resource changes, it
greatly reduces the total server load.</P>
<P>Moreover, the cache system includes a persistent object
storage system which is able to save stored objects in a persistent state that
outlives the JVM execution. This is mainly used for pages that are very
expensive to generate and last very long without changes, such as compiled
server pages.</P>
<P>The store system is responsible for handling the cached pages
as well as the pre-parsed XML documents. This is mostly used by XSLT
processors which store their stylesheets in a pre-parsed form to speed up
execution in those cases where the original file has changed, but the
stylesheet has not (which is a rather frequent case).</P>
</DIV>
</BODY>
</DOCUMENT><P class="legal">Copyright © 1999-2000 The Apache Software Foundation.<BR>All rights reserved.</P></BODY></HTML>
|