Generates an XML directory listing.

The root node of the generated document will normally be a directory node, and a directory node can contain zero or more file or directory nodes. A file node has no children. Each node will contain the following attributes:

Configuration options:

Definition

This is how you have to define a DirectoryGenerator (as defined in sitemap.xmap of the cocoon webapp directory):

    <map:generator label="content" 
                   logger="sitemap.generator.directory" 
                   name="directory" 
                   pool-grow="2" pool-max="16" pool-min="2"
                   src="org.apache.cocoon.generation.DirectoryGenerator"/>

Usage

Example of a simple usage of the DirectoryGenerator:

    <map:match pattern="directory-listing">
        <map:generate type="directory" src="../directory/to/list/">
            <map:parameter name="depth" value="2"/>
        </map:generate>
        <map:serialize type="xml"/>
    </map:match>

Result

This is an example of what you can expect as result of the DirectoryGenerator.

    <dir:directory name="dir1" lastModified="1067545661156" date="30/10/03 21:27" 
                   size="0" sort="name" reverse="false" requested="true">
        <dir:directory name="dir11" lastModified="1067295952187" 
                       date="28/10/03 0:05" size="0"/>
        <dir:directory name="dir12" lastModified="1067295971203" 
                       date="28/10/03 0:06" size="0"/>
        <dir:directory name="dir13" lastModified="1067543010781" 
                       date="30/10/03 20:43" size="0"/>
        <dir:directory name="dir14" lastModified="1067545661156" 
                       date="30/10/03 21:27" size="0"/>
        <dir:file name="file.xml" lastModified="1067541801328" 
                       date="30/10/03 20:23" size="79"/>
    </dir:directory>

The Namespace

As mentioned in the user documentation, DirectoryGenerator will generate all elements with the namespace http://apache.org/cocoon/directory/2.0, so that the afore mentioned example will read like:

    <dir:directory name="dir1" lastModified="1067545661156" date="30/10/03 21:27" 
                   xmlns:dir="http://apache.org/cocoon/directory/2.0
                   size="0" sort="name" reverse="false" requested="true">
        <dir:directory name="dir11" lastModified="1067295952187" 
                       date="28/10/03 0:05" size="0"/>
        <dir:directory name="dir12" lastModified="1067295971203" 
                       date="28/10/03 0:06" size="0"/>
        <dir:directory name="dir13" lastModified="1067543010781" 
                       date="30/10/03 20:43" size="0"/>
        <dir:directory name="dir14" lastModified="1067545661156" 
                       date="30/10/03 21:27" size="0"/>
        <dir:file name="file.xml" lastModified="1067541801328" 
                       date="30/10/03 20:23" size="79"/>
    </dir:directory>

Beginners, when converting this kind of output to HTML, sometimes are facing problems on dealing with this namespace. Matching the elements is one of them and removing that namespace from the resulting HTML documents is the other. I've even seen some Cocoon Live Sites still bearing this namespace within HTML pages, even though (non-X)HTML standards do not allow any namespace.

The solution is quite easy. The XSLT Standard says:

As an example, the following stylesheet will transform DirectoryGenerator's output into an HTML unordered list with no such superfluous namespace:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:dir="http://apache.org/cocoon/directory/2.0" exclude-result-prefixes="dir"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="dir:directory|dir:file">
        <li>
                <xsl:value-of select="@name"/>
                <xsl:if test="name(.)='dir:directory'">
                        <!-- creating subdirectories -->
                        <ul><xsl:apply-templates/></ul>
                </xsl:if>
        </li>
</xsl:template>

<xsl:template match="/dir:directory">
        <ul>
                <xsl:value-of select="@name"/>
                <!-- creating subdirectories -->
                <xsl:apply-templates/>
        </ul>
</xsl:template>

</xsl:stylesheet>

All you need to get rid of the namespace is in line 3.

DirectoryGenerator (last edited 2009-09-20 23:43:02 by localhost)