[Yanel-commits] rev 27698 - in public/yanel/trunk/src/resources/directory: src/java/org/wyona/yanel/impl/resources xslt

josias at wyona.com josias at wyona.com
Thu Sep 27 09:53:04 CEST 2007


Author: josias
Date: 2007-09-27 09:53:03 +0200 (Thu, 27 Sep 2007)
New Revision: 27698

Modified:
   public/yanel/trunk/src/resources/directory/src/java/org/wyona/yanel/impl/resources/DirectoryResource.java
   public/yanel/trunk/src/resources/directory/xslt/dir2xhtml.xsl
Log:
added more info (lastmodified, size). see bug #5573, thanks to simon.

Modified: public/yanel/trunk/src/resources/directory/src/java/org/wyona/yanel/impl/resources/DirectoryResource.java
===================================================================
--- public/yanel/trunk/src/resources/directory/src/java/org/wyona/yanel/impl/resources/DirectoryResource.java	2007-09-27 07:46:56 UTC (rev 27697)
+++ public/yanel/trunk/src/resources/directory/src/java/org/wyona/yanel/impl/resources/DirectoryResource.java	2007-09-27 07:53:03 UTC (rev 27698)
@@ -31,7 +31,9 @@
 import org.wyona.yarep.core.RepositoryException;
 import org.wyona.yarep.core.Repository;
 import org.wyona.yarep.core.RepositoryFactory;
+import org.wyona.yarep.core.Node;
 import org.wyona.yarep.util.RepoPath;
+import org.wyona.yanel.core.util.DateUtil;
 import org.wyona.yanel.core.util.PathUtil;
 
 import org.apache.log4j.Category;
@@ -50,33 +52,34 @@
 import javax.xml.transform.sax.SAXResult;
 import javax.xml.transform.sax.SAXSource;
 
+import java.util.Calendar;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
 
 /**
- * 
+ *
  */
 public class DirectoryResource extends Resource implements ViewableV2, CreatableV2 {
 
     private static Category log = Category.getInstance(DirectoryResource.class);
-    
+
     private Environment environment;
 
     /**
-     * 
+     *
      */
     public DirectoryResource() {
     }
 
     /**
-     * 
+     *
      */
     public ViewDescriptor[] getViewDescriptors() {
         return null;
     }
 
     /**
-     * 
+     *
      */
     public View getView(String viewId) {
         environment = getEnvironment();
@@ -87,38 +90,40 @@
         // href=\"yanel/resources/directory/xslt/dir2xhtml.xsl\"?>");
 
         Repository contentRepo = null;
-        org.wyona.yarep.core.Path p = new org.wyona.yarep.core.Path(getPath());
+        //org.wyona.yarep.core.Path p = new org.wyona.yarep.core.Path(getPath());
         try {
             contentRepo = getRealm().getRepository();
-
+            String path = getPath();
             // TODO: This doesn't seem to work ... (check on Yarep ...)
-            if (contentRepo.isResource(p)) {
-                log.warn("Path is a resource instead of a collection: " + p);
+            if (contentRepo.getNode(path).isResource()) {
+                log.warn("Path is a resource instead of a collection: " + path);
                 // p = p.getParent();
             }
 
             // TODO: Implement org.wyona.yarep.core.Path.getParent()
-            if (!contentRepo.isCollection(p)) {
-                log.warn("Path is not a collection: " + p);
-                p = new org.wyona.yarep.core.Path(new org.wyona.commons.io.Path(p.toString()).getParent().toString());
-                log.warn("Use parent of path: " + p);
+            if (!contentRepo.getNode(path).isCollection()) {
+                log.warn("Path is not a collection: " + path);
+                log.warn("Use parent of path: " + contentRepo.getNode(path).getParent().getPath());
             }
 
             // TODO: Add realm prefix, e.g. realm-prefix="ulysses-demo"
             // NOTE: The schema is according to
             // http://cocoon.apache.org/2.1/userdocs/directory-generator.html
-            sb.append("<dir:directory yanel:path=\"" + getPath() + "\" dir:name=\"" + p.getName() + "\" dir:path=\"" + p + "\" xmlns:dir=\"http://apache.org/cocoon/directory/2.0\" xmlns:yanel=\"http://www.wyona.org/yanel/resource/directory/1.0\">");
+            sb.append("<dir:directory yanel:path=\"" + getPath() + "\" dir:name=\"" + contentRepo.getNode(path).getName() + "\" dir:path=\"" + path + "\" xmlns:dir=\"http://apache.org/cocoon/directory/2.0\" xmlns:yanel=\"http://www.wyona.org/yanel/resource/directory/1.0\">");
             // TODO: Do not show the children with suffix .yanel-rti resp. make
             // this configurable!
             // NOTE: Do not hardcode the .yanel-rti, but rather use
             // Path.getRTIPath ...
-            org.wyona.yarep.core.Path[] children = contentRepo.getChildren(p);
+            Node[] children = contentRepo.getNode(getPath()).getNodes();
+            Calendar calendar = Calendar.getInstance();
             if (children != null) {
                 for (int i = 0; i < children.length; i++) {
-                    if (contentRepo.isResource(children[i])) {
-                        sb.append("<dir:file path=\"" + children[i] + "\" name=\"" + children[i].getName() + "\"/>");
-                    } else if (contentRepo.isCollection(children[i])) {
-                        sb.append("<dir:directory path=\"" + children[i] + "\" name=\"" + children[i].getName() + "\"/>");
+                    if (children[i].isResource()) {
+                        calendar.setTimeInMillis(children[i].getLastModified());
+                        String lastModified = DateUtil.format(calendar.getTime());
+                        sb.append("<dir:file path=\"" + children[i].getPath() + "\" name=\"" + children[i].getName() + "\" lastModified=\"" + children[i].getLastModified() + "\" date=\"" + lastModified + "\" size=\"" + children[i].getSize() + "\"/>");
+                    } else if (children[i].isCollection()) {
+                        sb.append("<dir:directory path=\"" + children[i].getPath() + "\" name=\"" + children[i].getName() + "\"/>");
                     } else {
                         sb.append("<yanel:exception yanel:path=\"" + children[i] + "\"/>");
                     }
@@ -143,19 +148,20 @@
         try {
             TransformerFactory tfactory = TransformerFactory.newInstance();
             String[] xsltTransformers = getXSLTprop();
-            
+
             java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
-            
+
             Transformer transformerIntern = tfactory.newTransformer(getXSLTStreamSource(contentRepo));
             StreamSource orig = new StreamSource(new java.io.StringBufferInputStream(sb.toString()));
-            
+
             transformerIntern.setParameter("yanel.path.name", PathUtil.getName(getPath()));
             transformerIntern.setParameter("yanel.path", getPath().toString());
             transformerIntern.setParameter("yanel.back2context", backToContext()+backToRoot());
             transformerIntern.setParameter("yarep.back2realm", backToRoot());
-            transformerIntern.setParameter("yarep.parent", getParent(p.toString()));
+            transformerIntern.setParameter("yarep.parent", getParent(getPath()));
+            transformerIntern.setParameter("yanel.htdocs", PathUtil.getGlobalHtdocsPath(this));
             transformerIntern.transform(orig, new StreamResult(baos));
-            
+
             if (xsltTransformers != null) {
                 //StreamSource orig = new StreamSource(new java.io.StringBufferInputStream(sb.toString()));
                 for (int i = 0; i < xsltTransformers.length; i++) {
@@ -166,7 +172,7 @@
                     transformer.setParameter("yanel.path", getPath().toString());
                     transformer.setParameter("yanel.back2context", backToContext()+backToRoot());
                     transformer.setParameter("yarep.back2realm", backToRoot());
-                    transformer.setParameter("yarep.parent", getParent(p.toString()));
+                    transformer.setParameter("yarep.parent", getParent(getPath()));
                     transformer.transform(orig, new StreamResult(baos));
                 }
             }
@@ -181,15 +187,15 @@
     }
 
     /**
-     * 
+     *
      */
     public boolean exists() throws Exception {
         log.warn("Not implemented yet!");
-        return true; 
+        return true;
     }
-    
+
     /**
-     * 
+     *
      */
     public long getSize() throws Exception {
         // TODO: not implemented yet
@@ -198,7 +204,7 @@
     }
 
     /**
-     * 
+     *
      */
     private StreamSource getXSLTStreamSource(Repository repo) throws RepositoryException {
         File xsltFile = org.wyona.commons.io.FileUtil.file(rtd.getConfigFile().getParentFile().getAbsolutePath(), "xslt" + File.separator + "dir2xhtml.xsl");
@@ -231,7 +237,7 @@
         // NOTE: Assuming fallback re dir2xhtml.xsl ...
         return "application/xhtml+xml";
     }
-    
+
     /**
      * @return a String with as many ../ as it needs to go back to from current realm to context
      */
@@ -243,14 +249,14 @@
         }
         return backToContext;
     }
-    
+
     /**
      * @return a String with as many ../ as it needs to go back to from current resource to the realm-root
      */
     private String backToRoot() {
         String backToRoot = "";
         int steps;
-        
+
         // TODO: Wouldn't it make more sense to use "tokens" and use a URL rewriter at the very end (also see the portlet specificatio http://jcp.org/aboutJava/communityprocess/review/jsr168/)
         String resourceContainerPath = environment.getResourceContainerPath();
         if (log.isDebugEnabled()) {
@@ -270,15 +276,15 @@
                 steps = getPath().split("/").length - 2;
             }
         }
-        
+
         for (int i = 0; i < steps; i++) {
             backToRoot = backToRoot + "../";
         }
         return backToRoot;
     }
-    
+
     /**
-     * @return a String ../ if path ends with a trailing slash. Otherwise a String ./ 
+     * @return a String ../ if path ends with a trailing slash. Otherwise a String ./
      */
     private String getParent(String path) {
         String parentPath = "./";
@@ -287,7 +293,7 @@
         }
         return parentPath;
     }
-    
+
     /**
     *
     */

Modified: public/yanel/trunk/src/resources/directory/xslt/dir2xhtml.xsl
===================================================================
--- public/yanel/trunk/src/resources/directory/xslt/dir2xhtml.xsl	2007-09-27 07:46:56 UTC (rev 27697)
+++ public/yanel/trunk/src/resources/directory/xslt/dir2xhtml.xsl	2007-09-27 07:53:03 UTC (rev 27698)
@@ -1,59 +1,124 @@
 <?xml version="1.0"?>
-
 <xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns="http://www.w3.org/1999/xhtml"
   xmlns:xhtml="http://www.w3.org/1999/xhtml"
   xmlns:dir="http://apache.org/cocoon/directory/2.0"
   xmlns:yanel="http://www.wyona.org/yanel/resource/directory/1.0"
->
+  >
 
-<xsl:output method="xhtml"/>
-<!-- NOTE: Must correspond with the mime-type delivered by the server. See src/java/org/wyona/yanel/impl/resources/DirectoryResource.java -->
-<!--
+  <xsl:output method="xhtml"/>
+
+  <!-- NOTE: Must correspond with the mime-type delivered by the server. See src/java/org/wyona/yanel/impl/resources/DirectoryResource.java -->
+  <!--
 <xsl:output method="html"/>
 -->
-<xsl:param name="yanel.path.name" select="'NAME_IS_NULL'"/>
-<xsl:param name="yanel.path" select="'PATH_IS_NULL'"/>
-<xsl:param name="yanel.back2context" select="'BACK2CONTEXT_IS_NULL'"/>
-<xsl:param name="yarep.back2realm" select="'BACK2REALM_IS_NULL'"/>
-<xsl:param name="yarep.parent" select="'YAREPPARENT_IS_NULL'"/>
 
-<xsl:template match="/">
-<html xmlns="http://www.w3.org/1999/xhtml">
+  <xsl:param name="yanel.path.name" select="'NAME_IS_NULL'"/>
+  <xsl:param name="yanel.path" select="'PATH_IS_NULL'"/>
+  <xsl:param name="yanel.back2context" select="'BACK2CONTEXT_IS_NULL'"/>
+  <xsl:param name="yarep.back2realm" select="'BACK2REALM_IS_NULL'"/>
+  <xsl:param name="yarep.parent" select="'YAREPPARENT_IS_NULL'"/>
+  <xsl:param name="yanel.htdocs" select="'YANELHTDOCS_IS_NULL'"/>
 
-<head>
-  <title>Collection: <xsl:value-of select="/dir:directory/@yanel:path"/></title>
-</head>
+  <xsl:template match="/">
+    <html xmlns="http://www.w3.org/1999/xhtml">
+      <head>
+        <title>Collection: <xsl:value-of select="/dir:directory/@yanel:path"/>
+        </title>
+        <script src="{$yanel.htdocs}yanel-js/sorttable.js" type="text/javascript"/>
+      </head>
+      <body>
+        <div>
+          <p>
+            <b>Yanel Path (with regard to realm):</b>&#160;<xsl:value-of select="/dir:directory/@yanel:path"/>
+          </p>
+          <p>
+            <b>Collection Path:</b>&#160;<xsl:value-of
+            select="/dir:directory/@dir:path"/> (<a
+              href="?yanel.resource.viewid=source">XML view</a>)</p>
+          <p>
+            <table class="sortable">
+              <thead>
+                <tr>
+                  <th>Type</th>
+                  <th class="sorttable_alpha">Name</th>
+                  <th>Lastmodified</th>
+                  <th>Size</th>
+                </tr>
+              </thead>
+              <tbody>
+                <tr>
+                  <td sorttable_customkey="1">Parent </td>
+                  <td>
+                    <a href="{$yarep.parent}" title="directory">..</a>
+                  </td>
+                  <td> - </td>
+                  <td> - </td>
+                </tr>
+                <xsl:apply-templates select="/dir:directory/*"/>
+              </tbody>
+            </table>
+          </p>
+        </div>
+      </body>
+    </html>
+  </xsl:template>
 
-<body>
-<div>
-<p>
-<b>Yanel Path (with regard to realm):</b>&#160;<xsl:value-of select="/dir:directory/@yanel:path"/>
-</p>
+  <xsl:template match="dir:directory">
+    <tr>
+      <td sorttable_customkey="Collection">Collection </td>
+      <td>
+        <a href="{$yarep.back2realm}.{@path}/" title="directory">
+          <xsl:value-of select="@name"/>
+        </a>
+      </td>
+      <td> - </td>
+      <td sorttable_customkey="0"> - </td>
+    </tr>
+  </xsl:template>
 
-<p>
-<b>Collection Path:</b>&#160;<xsl:value-of select="/dir:directory/@dir:path"/> (<a href="?yanel.resource.viewid=source">XML view</a>)
-</p>
+  <xsl:template match="dir:file">
+    <tr>
+      <td sorttable_customkey="Resource">Resource</td>
+      <td>
+        <a href="{$yarep.back2realm}.{@path}" title="file">
+          <xsl:value-of select="@name"/>
+        </a>
+      </td>
+      <td>
+        <xsl:value-of select="@date"/>
+      </td>
+      <td sorttable_customkey="{@size}">
+        <xsl:call-template name="humanbyte">
+        <xsl:with-param name="bytes">
+          <xsl:value-of select="@size"/>
+        </xsl:with-param>
+      </xsl:call-template>
+      </td>
+    </tr>
+  </xsl:template>
 
-<p>
-<b>Parent:</b>&#160;<a href="{$yarep.parent}">..</a>
-</p>
+  <xsl:template name="humanbyte">
+    <xsl:param name="bytes"/>
+    <xsl:choose>
+      <!-- giga -->
+      <xsl:when test="$bytes &gt;= 1073741824">
+        <xsl:value-of select="round($bytes div 1073741824)"/> GB
+      </xsl:when>
+      <!-- mega -->
+      <xsl:when test="$bytes &gt;= 1048576">
+        <xsl:value-of select="round($bytes div 1048576)"/> MB
+      </xsl:when>
+      <!-- kilo -->
+      <xsl:when test="$bytes &gt;= 1024">
+        <xsl:value-of select="round($bytes div 1024)"/> KB
+      </xsl:when>
+      <!-- bytes -->
+      <xsl:otherwise>
+        <xsl:value-of select="$bytes"/> Bytes
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
 
-<p>
-<b>Children:</b><br/>
-<xsl:apply-templates select="/dir:directory/*"/>
-</p>
-</div>
-</body>
-</html>
-</xsl:template>
-
-<xsl:template match="dir:directory">
-Collection: <a href="{$yarep.back2realm}.{@path}/" title="directory"><xsl:value-of select="@name"/>/</a><br/>
-</xsl:template>
-
-<xsl:template match="dir:file">
-Resource: <a href="{$yarep.back2realm}.{@path}" title="file"><xsl:value-of select="@name"/></a><br/>
-</xsl:template>
 </xsl:stylesheet>



More information about the Yanel-commits mailing list