morrijr 02/05/17 04:11:03 Modified: src/documentation/xdocs/userdocs/generators directory-generator.xml src/java/org/apache/cocoon/generation DirectoryGenerator.java src/webapp/stylesheets/system directory2html.xsl Log: Patch for directory-generator. Based on patch by Frank Ridderbusch [BUGZILLA 1728] Revision Changes Path 1.2 +114 -86 xml-cocoon2/src/documentation/xdocs/userdocs/generators/directory-generator.xml Index: directory-generator.xml =================================================================== RCS file: /home/cvs/xml-cocoon2/src/documentation/xdocs/userdocs/generators/directory-generator.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- directory-generator.xml 3 Jan 2002 12:31:04 -0000 1.1 +++ directory-generator.xml 17 May 2002 11:11:03 -0000 1.2 @@ -2,97 +2,125 @@ <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.0//EN" "../../dtd/document-v10.dtd"> <document> - <header> - <title>Directory Generator</title> - <version>0.9</version> - <type>Technical document</type> - <authors> - <person name="Carsten Ziegeler" email="[EMAIL PROTECTED]"/> - </authors> - <abstract>This document describes the directory generator of Cocoon.</abstract> - </header> - <body> - <s1 title="Directory Generator"> - <p>Generates an XML directory listing.</p> - <p> - The root node of the generated document will normally be a - <code>directory</code> node, and a directory node can contain zero - or more <code>file</code> or <code>directory</code> nodes. A file node has no - children. Each node will contain the following attributes: - </p> - <ul> - <li>name : the name of the file or directory</li> - <li>lastModified : the time the file was last modified, measured as the number of - milliseconds since the epoch (as in java.io.File.lastModified)</li> - <li>date (optional) : the time the file was last modified in human-readable form</li> - </ul> - <p>All generated elements have the namespace - <code>http://apache.org/cocoon/directory/2.0</code>. - The root <code>directory</code> - node has the attribute <code>requested</code> with the value <code>true</code>. - </p> - <ul> - <li>Name : directory</li> - <li>Class: org.apache.cocoon.generation.DirectoryGenerator</li> - <li>Cacheable: no.</li> - </ul> -<source><![CDATA[ - <map:generate type="directory" src="the_directory"/> -]]></source> - </s1> - <s1 title="Configuration"> - <p>The following parameter can be specified in the pipeline for - the generate command: - </p> - <ul> - <li>depth (optional) : Sets how deep Directory Generator should delve into the - directory structure. If set to 1 (the default), only the starting - directory's immediate contents will be returned.</li> - <li>dateFormat (optional) : Sets the format for the date attribute of each node, as - described in java.text.SimpleDateFormat. If unset, the default - format for the current locale will be used.</li> - <li>root (optional) : The root pattern</li> - <li>include (optional) : The include pattern</li> - <li>exclude (optional) : The exclude pattern</li> - </ul> - </s1> - <s1 title="DTD"> - <p>XML generated by directory generator uses namespace - <code>http://apache.org/cocoon/status/2.0</code>. The DTD - of XML generated by directory generator: - </p> -<source><![CDATA[ - <!ELEMENT directory (directory|file)*> - <!ATTLIST directroy + <header> + <title>Directory Generator</title> + <version>0.9</version> + <type>Technical document</type> + <authors> + <person name="Carsten Ziegeler" email="[EMAIL PROTECTED]"/> + <person name="Frank Ridderbusch" email="[EMAIL PROTECTED]"/> + <person name="John Morrison" email="[EMAIL PROTECTED]"/> + </authors> + <abstract>This document describes the directory generator of + Cocoon.</abstract> + </header> + <body> + <s1 title="Directory Generator"> + <p>Generates an XML directory listing.</p> + <p>The root node of the generated document will normally be a + <code>directory</code> node, and a directory node can contain + zero or more <code>file</code> or <code>directory</code> nodes. + A file node has no children. Each node will contain the + following attributes: + </p> + <ul> + <li>name : the name of the file or directory</li> + <li>lastModified : the time the file was last modified, + measured as the number of milliseconds since the epoch (as + in java.io.File.lastModified)</li> + <li>size : the file size in bytes (as returned by + java.io.File.length)</li> + <li>date (optional) : the time the file was last modified in + human-readable form</li> + </ul> + <p>All generated elements have the namespace + <code>http://apache.org/cocoon/directory/2.0</code>. The root + <code>directory</code> node has the attribute + <code>requested</code> with the value <code>true</code>. The + sort order is described by the attribute <code>sort</code>, + which defaults to sorting according to <code>name</code>. + </p> + <ul> + <li>Name : directory</li> + <li>Class: org.apache.cocoon.generation.DirectoryGenerator</li> + <li>Cacheable: no.</li> + </ul> + <source><![CDATA[ +<map:generate type="directory" src="the_directory"/>]]></source> + </s1> + <s1 title="Configuration"> + <p>The following parameter can be specified in the pipeline for the + generate command: + </p> + <ul> + <li>depth (optional) : Sets how deep Directory Generator should + delve into the directory structure. If set to 1 (the + default), only the starting directory's immediate contents + will be returned.</li> + <li>dateFormat (optional) : Sets the format for the date + attribute of each node, as described in + java.text.SimpleDateFormat. If unset, the default format + for the current locale will be used.</li> + <li>root (optional) : The root pattern. This is a regular + expression as described + <link href="http://jakarta.apache.org/regexp/apidocs/org/apache/regexp/RE.html">here.</link></li> + <li>include (optional) : The include pattern. Also a + regular expression.</li> + <li>exclude (optional) : The exclude pattern. Also a + regular expression.</li> + <li>sort (optional) : This parameter determines the order in + which the <code>file</code and <code>directory</code> nodes + are returned. Default sort order is by name. Possible + values are "name", "size", "time", and "directory". + "directory" is the same as "name", except that directory + entries are listed first.</li> + <li>reverse (optional) : This parameter reverses the sort order. + reverse="false" is the default.</li> + </ul> + </s1> + <s1 title="DTD"> + <p>XML generated by directory generator uses namespace + <code>http://apache.org/cocoon/status/2.0</code>. The DTD + of XML generated by directory generator: + </p> + <source><![CDATA[ +<!ELEMENT directory (directory|file)*> +<!ATTLIST directory name CDATA #REQUIRED lastModified CDATA #REQUIRED date CDATA #IMPLIED + size CDATA #IMPLIED + sort CDATA #IMPLIED + reverse CDATA #IMPLIED requested CDATA #IMPLIED> - - <!ELEMENt file #EMPTY> - <!ATTLIST file + +<!ELEMENT file #EMPTY> +<!ATTLIST file name CDATA #REQUIRED lastModified CDATA #REQUIRED - date CDATA #IMPLIED> -]]></source> - </s1> - <s1 title="Example"> - <p> - The current directory generator may generate following xml: - </p> -<source><![CDATA[ -<directory xmlns="http://apache.org/cocoon/directory/2.0" - name="stylesheets" lastModified="999425490000" - date="02.09.01 12:11" - requested="true"> - <directory name="sites" - lastModified="999425490000" date="02.09.01 12:11"/> - <file name="dynamic-page2html.xsl" - lastModified="999425490000" date="02.09.01 12:11"/> - <file name="simple-xml2html.xsl" - lastModified="999425490000" date="02.09.01 12:11"/> -</directory> + date CDATA #IMPLIED + size CDATA #IMPLIED> ]]></source> - </s1> -</body> + </s1> + <s1 title="Example"> + <p>The current directory generator may generate following xml:</p> + <source><![CDATA[ +<dir:directory xmlns:dir="http://apache.org/cocoon/directory/2.0" + name="stylesheets" + lastModified="1019666489000" + date="24.04.02 18:41" + size="461" + sort="name" + reverse="false" + requested="true"> + <dir:directory name="sites" lastModified="1019666489000" + date="24.04.02 18:41" size="118"/> + <dir:file name="dynamic-page2html.xsl" lastModified="1019666489000" + date="24.04.02 18:41" size="1832"/> + <dir:file name="simple-xml2html.xsl" lastModified="1019666489000" + date="24.04.02 18:41" size="12676"/> +</dir:directory> + ]]></source> + </s1> + </body> </document> 1.8 +87 -2 xml-cocoon2/src/java/org/apache/cocoon/generation/DirectoryGenerator.java Index: DirectoryGenerator.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/generation/DirectoryGenerator.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- DirectoryGenerator.java 22 Feb 2002 07:03:51 -0000 1.7 +++ DirectoryGenerator.java 17 May 2002 11:11:03 -0000 1.8 @@ -67,6 +67,8 @@ import java.util.Date; import java.util.Map; import java.util.Stack; +import java.util.Arrays; +import java.util.Comparator; /** * Generates an XML directory listing. @@ -82,6 +84,8 @@ * <dt> lastModified * <dd> the time the file was last modified, measured as the number of * milliseconds since the epoch (as in java.io.File.lastModified) + * <dt> size + * <dd> the file size, in bytes (as in java.io.FIle.length) * <dt> date (optional) * <dd> the time the file was last modified in human-readable form * </dl> @@ -93,6 +97,12 @@ * <dd> Sets how deep DirectoryGenerator should delve into the * directory structure. If set to 1 (the default), only the starting * directory's immediate contents will be returned. + * <dt> <i>sort</i> (optional) + * <dd> Sort order in which the nodes are returned. Possible values are + * name (default), size, time, directory. directory is the same as name, + * except that the directory entries are listed first. + * <dt> <i>reverse</i> (optional) + * <dd> * <dt> <i>dateFormat</i> (optional) * <dd> Sets the format for the date attribute of each node, as * described in java.text.SimpleDateFormat. If unset, the default @@ -103,7 +113,7 @@ * (Apache Software Foundation, Exoffice Technologies) * @author <a href="mailto:[EMAIL PROTECTED]">Conny Krappatsch</a> * (SMB GmbH) for Virbus AG - * @version CVS $Id: DirectoryGenerator.java,v 1.7 2002/02/22 07:03:51 cziegeler Exp $ + * @version CVS $Id: DirectoryGenerator.java,v 1.8 2002/05/17 11:11:03 morrijr Exp $ */ public class DirectoryGenerator extends ComposerGenerator { private static final String FILE = "file:"; @@ -122,6 +132,7 @@ protected static final String FILENAME_ATTR_NAME = "name"; protected static final String LASTMOD_ATTR_NAME = "lastModified"; protected static final String DATE_ATTR_NAME = "date"; + protected static final String SIZE_ATTR_NAME = "size"; /* * Variables set per-request @@ -131,6 +142,8 @@ protected int depth; protected AttributesImpl attributes = new AttributesImpl(); protected SimpleDateFormat dateFormatter; + protected String sort; + protected boolean reverse; protected RE rootRE; protected RE includeRE; @@ -166,6 +179,12 @@ this.depth = par.getParameterAsInteger("depth", 1); getLogger().debug("depth: " + this.depth); + this.sort = par.getParameter("sort", "name"); + getLogger().debug("sort: " + this.sort); + + this.reverse = par.getParameterAsBoolean("reverse", false); + getLogger().debug("reverse: " + this.reverse); + String rePattern = par.getParameter("root", null); try { getLogger().debug("root pattern: " + rePattern); @@ -288,6 +307,66 @@ startNode(DIR_NODE_NAME, path); if (depth>0) { File contents[] = path.listFiles(); + + if(sort.equals("size")) { + Arrays.sort(contents, + new Comparator() { + public int compare(Object o1, Object o2) { + if(reverse) { + return new Long(((File) o2).length()) + .compareTo(new Long(((File) o1).length())); + } + return new Long(((File) o1).length()) + .compareTo(new Long(((File) o2).length())); + } + }); + } else if(sort.equals("lastmodified")) { + Arrays.sort(contents, + new Comparator() { + public int compare(Object o1, Object o2) { + if(reverse) { + return new Long(((File) o2).lastModified()) + .compareTo(new Long(((File) o1).lastModified())); + } + return new Long(((File) o1).lastModified()) + .compareTo(new Long(((File) o2).lastModified())); + } + }); + } else if(sort.equals("directory")) { + Arrays.sort(contents, + new Comparator() { + public int compare(Object o1, Object o2) { + File f1 = (File) o1; + File f2 = (File) o2; + + if(reverse) { + if(f2.isDirectory() && f1.isFile()) + return -1; + if(f2.isFile() && f1.isDirectory()) + return 1; + return f2.getName().compareTo(f1.getName()); + } + if(f2.isDirectory() && f1.isFile()) + return 1; + if(f2.isFile() && f1.isDirectory()) + return -1; + return f1.getName().compareTo(f2.getName()); + } + }); + } else { // sort.equals("name") + Arrays.sort(contents, + new Comparator() { + public int compare(Object o1, Object o2) { + if(reverse) { + return ((File) o2).getName() + .compareTo(((File) o1).getName()); + } + return ((File) o1).getName() + .compareTo(((File) o2).getName()); + } + }); + } + for (int i=0; i<contents.length; i++) { if (isIncluded(contents[i]) && !isExcluded(contents[i])) { addPath(contents[i], depth-1); @@ -346,10 +425,16 @@ attributes.addAttribute("", DATE_ATTR_NAME, DATE_ATTR_NAME, "CDATA", dateFormatter.format(new Date(lastModified))); + attributes.addAttribute("", SIZE_ATTR_NAME, + SIZE_ATTR_NAME, "CDATA", + Long.toString(path.length())); if (this.isRequestedDirectory) { + attributes.addAttribute("", "sort", "sort", "CDATA", this.sort); + attributes.addAttribute("", "reverse", "reverse", "CDATA", + String.valueOf(this.reverse)); attributes.addAttribute("", "requested", "requested", "CDATA", - "true"); + "true"); this.isRequestedDirectory = false; } } 1.2 +14 -21 xml-cocoon2/src/webapp/stylesheets/system/directory2html.xsl Index: directory2html.xsl =================================================================== RCS file: /home/cvs/xml-cocoon2/src/webapp/stylesheets/system/directory2html.xsl,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- directory2html.xsl 3 Jan 2002 12:31:47 -0000 1.1 +++ directory2html.xsl 17 May 2002 11:11:03 -0000 1.2 @@ -6,21 +6,22 @@ <xsl:template match="/"> <html> - <head> - <title><xsl:value-of select="dir:directory/@name"/></title> + <head> + <title><xsl:value-of select="dir:directory/@name"/></title> + <style> + <xsl:comment> + body { background-color: #ffffff } + </xsl:comment> + </style> </head> - <body bgcolor="#ffffff"> + <body> <h1>Directory Listing of <xsl:value-of select="dir:directory/@name"/></h1> <table border="0"> <tr> - <td> - <a href="../"><i>parent directory</i></a> - </td> + <td><a href="../"><i>parent directory</i></a></td> </tr> <tr> - <td> -   - </td> + <td> </td> </tr> <xsl:apply-templates/> </table> @@ -30,23 +31,15 @@ <xsl:template match="dir:directory/dir:directory"> <tr> - <td> - <a href="{@name}/"><i><xsl:value-of select="@name"/></i></a> - </td> - <td> - <xsl:value-of select="@date"/> - </td> + <td><a href="{@name}/"><i><xsl:value-of select="@name"/></i></a></td> + <td><xsl:value-of select="@date"/></td> </tr> </xsl:template> <xsl:template match="dir:file"> <tr> - <td> - <a href="{@name}"><xsl:value-of select="@name"/></a> - </td> - <td> - <xsl:value-of select="@date"/> - </td> + <td><a href="{@name}"><xsl:value-of select="@name"/></a></td> + <td><xsl:value-of select="@date"/></td> </tr> </xsl:template>
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]