cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Pascal Davoust" <Pascal.Davo...@wanadoo.fr>
Subject Logic and Layout TagLibs basic examples
Date Thu, 29 Jun 2000 22:25:44 GMT
At least one person seems interested in this little example, so let's go...

Here's my own view of how Logic and Layout TagLibs could be implemented with
Cocoon, with a clear separation between content, logic, and layout...
Many posts already explain what's a TagLib, so I'll just say that it's a
reusable set of custom tags, either for logic or layout (at least in my mind
;-).
The full examples are at the end of this post, so that people who are not
interested may leave now ;o).

With that example, I wanted to :
* define of a logic TagLib
* define a layout TagLib
* be able to use more that one (logic and/or layout) TagLib at the same time

As a result, the generation pipe for that is as follow :

content
    v
    v - Logic
    v
dynamic content
    v
    v - Layout
    v
final document

The logic stage is of course XSP-based, and is implemented using a single
XSL file (should I say XSP file ?) that includes one or more Logic TagLib
files (actually XSL files). All these files (content and logic taglibs) are
automatically compiled into a single producer in Cocoon by the XSP
processor.

The layout stage can be implemented by using one or two steps transforms :

* Two steps transform :
A first XSL sheet transforms the dynamically generated document into a
document using both (for example) HTML tags and custom tags (from one or
more layout taglibs).
A second XSL sheet, which includes one or more Layout TagLib (actually XSL
files), is applied on this document, and transforms the remaining custom
tags into (same example) HTML code.

* One step transform :
A single XSL sheet includes the Layout TagLibs (same XSL files), and
transforms the dynamically generated document into the final document,
directly calling the TagLib named templates (see the one-step example for
more information).

Of course the one-step transform is more efficient, but the use of the
custom tags are less intuitive...
Note that the 'final' document in that example can of course be processed
again by Cocoon if you want !

Sorry for this long post, but I believe it could have saved me some time if
I could find something similar when I began with this topic... ;o)

                Pascal.



And finally, here're the examples !

======================================================================
Two-step transform
======================================================================
----------------------------------------------------------------------
File : content.xml
-----------------------------------------
<?xml version="1.0"?>

<?cocoon-process type="xsp"?>
<?xml-logicsheet href="logic.xsl"?>

<?cocoon-process type="xslt"?>
<?xml-stylesheet href="layout.xsl" type="text/xsl"?>

<document
  xmlns:logic-example="http://www.reboot.org/DTD/XSP/LogicExample">

  <page title="TagLib Examples">

    <paragraph>
      It's now
      <logic-example:time-of-day format="yy/MM/dd hh:mm:ss aa"/> !
    </paragraph>

    <author>
      <first-name>Pascal</first-name>
      <last-name>Davoust</last-name>
      <email>Pascal.Davoust@wanadoo.fr</email>
    </author>
  </page>
</document>
----------------------------------------------------------------------
File : logic.xsl
----------------------------------
<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xsp="http://www.apache.org/1999/XSP/Core"
   xmlns:logic-example="http://www.reboot.org/DTD/XSP/LogicExample">

  <!--
  // Imports the Identity transform, so that any element that does not
  // match is copied without modification to the result tree.
  -->
  <xsl:import href="identity.xsl"/>

  <!--
  // Imports the various Logic TagLibs (only one in this example).
  -->
  <xsl:import href="logic-taglib.xsl"/>

  <xsl:template match="document">
    <!--
    // Replaces the document root (document) by an XSP root (xsp:page)
    // with all its attributes unchanged.
    -->
    <xsp:page>
      <xsl:apply-templates select="@*"/>
      <!--
      // Adds the language declaration for XSP.
      -->
      <xsl:attribute name="language">java</xsl:attribute>

      <!--
      // Calls the templates for global structures and class-wide member
      // functions definitions for each included Logic TagLib (only one
      // in this example).
      -->
      <xsl:call-template name="logic-example:globals"/>

      <!--
      // Applies the remaining templates.
      -->
      <xsl:apply-templates/>
    </xsp:page>
  </xsl:template>

</xsl:stylesheet>
----------------------------------------------------------------------
File : identity.xsl
----------------------------
<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <!--
 // This template copies any descendant (attribute or element) of the
 // root node from the source tree and copies it to the result tree.
 -->
  <xsl:template match="@*|node()" priority="-1">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
----------------------------------------------------------------------
File : logic-taglib.xsl
-----------------------------
<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsp="http://www.apache.org/1999/XSP/Core"
  xmlns:logic-example="http://www.reboot.org/DTD/XSP/LogicExample">

  <!--
  // Defines the global class level logic of the TagLib.
  //
  // Note : the logic code is directly copied from the Cocoon examples.
  // I just changed the global template (match="xsp;page") to a named
  // template that can be called (with <xsl:call-template>), because
  // I needed to be able to use more than one Logic TagLib at the same
  // time. A few modification has been made to the logic of this template,
  // too.
  // For the same reason, the TagLibs I define use namespaces
  // to avoid collision.
  -->
  <xsl:template name="logic-example:globals">
    <xsp:structure>
      <xsp:include>java.util.Date</xsp:include>
      <xsp:include>java.text.SimpleDateFormat</xsp:include>
    </xsp:structure>
    <xsp:logic>

      /* "Example" Class Level Logic */
      private static String formatDate(Date date, String pattern) {
        if (pattern == null || pattern.length() == 0) {
          pattern = "yyyy/MM/dd hh:mm:ss aa";
        }
        return (new SimpleDateFormat(pattern)).format(date);
      }

    </xsp:logic>
  </xsl:template>

  <!--
  // Defines a Logic tag which can be used whenever this file is included
  // into an XSL sheet.
  -->
  <xsl:template match="logic-example:time-of-day">
    <xsp:expr>
      formatDate(new Date(), "<xsl:value-of select="@format"/>")
    </xsp:expr>
  </xsl:template>

</xsl:stylesheet>
----------------------------------------------------------------------
File : layout.xsl
-------------------------
<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:layout-example="http://www.reboot.org/DTD/XSP/LayoutExample">

  <xsl:template match="page">
    <!--
    // Indicates to Cocoon that the resulting tree is an XML tree which
    // needs XSLT translation, and which stylesheet to use for that.
    -->
    <xsl:processing-instruction name="cocoon-process">
      type="xslt"
    </xsl:processing-instruction>
    <xsl:processing-instruction name="xml-stylesheet">
      href="taglib-html.xsl" type="text/xsl"
    </xsl:processing-instruction>

    <html>
      <head>
        <title>
         <xsl:value-of select="@title"/>
        </title>
      </head>

      <body>
        <center>
         <xsl:apply-templates/>
        </center>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="paragraph">
    <p>
      <!--
      // Uses the tag of the Layout TagLib in the result tree,
      // which will be processed by the second transformation.
      -->
      <layout-example:ascii-frame>
        <xsl:apply-templates/>
      </layout-example:ascii-frame>
    </p>
  </xsl:template>

  <xsl:template match="author">
    <p>
      <i>By
        <xsl:apply-templates select="first-name"/>
        <xsl:apply-templates select="last-name"/>
      </i>
    </p>
  </xsl:template>

</xsl:stylesheet>
----------------------------------------------------------------------
File : taglib-html.xsl
-----------------------------
<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:layout-example="http://www.reboot.org/DTD/XSP/LayoutExample">

  <!--
  // Imports the Identity Transform for copying unmodified any tag which
  // does not belong to a Layout TagLib.
  -->
  <xsl:import href="identity.xsl"/>

  <!--
  // Includes the sheets that defines the Layout TagLibs (only one here).
  -->
  <xsl:import href="layout-taglib.xsl"/>

  <!--
  // This template copies the root element to the result tree.
  // The basic idea for that template is that, if it's not here,
  // the identity transform will also copy the "xml-stylesheet"
  // processing instruction at the very beginning of the resulting
  // document.
  // The best thing to see what it does is to compare the source
  // of the result with and without it !!! ;o)
  -->
  <xsl:template match="/">
    <xsl:copy>
      <xsl:apply-templates select="/*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="html">
    <!--
    // Indicates to Cocoon that the resulting tree is an HTML 4.0 tree.
    -->
    <xsl:processing-instruction name="cocoon-format">
      type="text/html"
    </xsl:processing-instruction>

    <xsl:copy>
      <!-- Applies the transforms for tags from the Layout TagLibs. -->
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
----------------------------------------------------------------------
File : layout-taglib.xsl
--------------------------------------
<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:layout-example="http://www.reboot.org/DTD/XSP/LayoutExample">

  <!--
  // Defines a Layout tag which can be used whenever this file is included
  // in an XSL sheet.
  -->
  <xsl:template
    name="layout-example:ascii-frame"
    match="layout-example:ascii-frame">
    <table border="0">
      <tr>
        <td>/</td>
        <td>---------------------------------------------------------</td>
        <td>\</td>
      </tr>
      <tr>
        <td>|</td>
        <td></td>
        <td>|</td>
      </tr>
      <tr>
        <td>|</td>
        <td>
          <center>
            <xsl:apply-templates/>
          </center>
        </td>
        <td>|</td>
      </tr>
      <tr>
        <td>|</td>
        <td></td>
        <td>|</td>
      </tr>
      <tr>
        <td>\</td>
        <td>---------------------------------------------------------</td>
        <td>/</td>
      </tr>
    </table>
  </xsl:template>

</xsl:stylesheet>
----------------------------------------------------------------------
======================================================================


======================================================================
One-step transform example
======================================================================
----------------------------------------------------------------------
Files : content.xml, logic.xsl, logic-taglib.xsl, identity.xsl,
layout-taglib.xsl
-----------------------------------------
Identical
----------------------------------------------------------------------
Files : layout.xsl
-----------------------------------------
<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:layout-example="http://www.reboot.org/DTD/XSP/LayoutExample">

  <!-- Includes the XSL sheet that defines the Layout TagLib -->
  <xsl:import href="layout-taglib.xsl"/>

  <xsl:template match="page">
    <!--
    // Indicates to Cocoon that the resulting tree is an HTML 4.0 tree.
    -->
    <xsl:processing-instruction name="cocoon-format">
      type="text/html"
    </xsl:processing-instruction>

    <html>
      <head>
        <title>
         <xsl:value-of select="@title"/>
        </title>
      </head>

      <body>
        <center>
         <xsl:apply-templates/>
        </center>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="paragraph">
    <p>
      <!-- Layout TagLib direct call -->
      <xsl:call-template name="layout-example:ascii-frame"/>
    </p>
  </xsl:template>

  <xsl:template match="author">
    <p>
      <i>By
        <xsl:apply-templates select="first-name"/>
        <xsl:apply-templates select="last-name"/>
      </i>
    </p>
  </xsl:template>

</xsl:stylesheet>
----------------------------------------------------------------------
======================================================================



Mime
View raw message