[Yanel-commits] rev 22443 - public/yanel/trunk/src/resources/odt/src/java/org/wyona/yanel/impl/resources

josias at wyona.com josias at wyona.com
Mon Feb 5 20:17:35 CET 2007


Author: josias
Date: 2007-02-05 20:17:33 +0100 (Mon, 05 Feb 2007)
New Revision: 22443

Modified:
   public/yanel/trunk/src/resources/odt/src/java/org/wyona/yanel/impl/resources/ODTResource.java
Log:
upgraded odt resource to V2 interfaces to make it work with nested realms. fixes bug #5170, thanks to simon.

Modified: public/yanel/trunk/src/resources/odt/src/java/org/wyona/yanel/impl/resources/ODTResource.java
===================================================================
--- public/yanel/trunk/src/resources/odt/src/java/org/wyona/yanel/impl/resources/ODTResource.java	2007-02-05 17:38:14 UTC (rev 22442)
+++ public/yanel/trunk/src/resources/odt/src/java/org/wyona/yanel/impl/resources/ODTResource.java	2007-02-05 19:17:33 UTC (rev 22443)
@@ -19,11 +19,13 @@
 import org.wyona.yanel.core.Path;
 import org.wyona.yanel.core.Resource;
 import org.wyona.yanel.core.Topic;
-import org.wyona.yanel.core.api.attributes.ModifiableV1;
-import org.wyona.yanel.core.api.attributes.ViewableV1;
+import org.wyona.yanel.core.api.attributes.ModifiableV2;
+import org.wyona.yanel.core.api.attributes.ViewableV2;
 import org.wyona.yanel.core.attributes.viewable.View;
 import org.wyona.yanel.core.attributes.viewable.ViewDescriptor;
+import org.wyona.yanel.core.util.PathUtil;
 
+import org.wyona.yarep.core.Node;
 import org.wyona.yarep.core.Repository;
 import org.wyona.yarep.core.RepositoryFactory;
 import org.wyona.yarep.util.RepoPath;
@@ -42,58 +44,62 @@
 import java.io.Writer;
 import java.util.jar.JarEntry;
 import java.util.jar.JarInputStream;
+import java.io.InputStreamReader;
 
 import org.apache.log4j.Category;
 
 /**
- *
+ * 
  */
-public class ODTResource extends Resource implements ViewableV1, ModifiableV1 {
+public class ODTResource extends Resource implements ViewableV2, ModifiableV2 {
 
     private static Category log = Category.getInstance(ODTResource.class);
 
     /**
-     *
+     * 
      */
     public ODTResource() {
     }
 
     /**
-     *
+     * 
      */
     public ViewDescriptor[] getViewDescriptors() {
         return null;
     }
 
     /**
-     *
+     * 
      */
-    public View getView(Path path, String viewId) {
+    public View getView(String viewId) {
         View defaultView = new View();
-        String mimeType = getMimeType(path, viewId);
+        String mimeType = getMimeType(viewId);
         defaultView.setMimeType(mimeType);
 
         try {
-            RepoPath rp = new org.wyona.yarep.util.YarepUtil().getRepositoryPath(new org.wyona.yarep.core.Path(path.toString()), getRepositoryFactory());
+            Repository dataRepo = getRealm().getRepository();
 
             if (mimeType.equals("application/xml")) {
-                defaultView.setInputStream(getContentXML(rp));
+                defaultView.setInputStream(getContentXML(dataRepo));
                 return defaultView;
-	    } else if (mimeType.equals("application/xhtml+xml")) {
-                File xsltFile = org.wyona.commons.io.FileUtil.file(rtd.getConfigFile().getParentFile().getAbsolutePath(), "document2xhtml.xsl");
+            } else if (mimeType.equals("application/xhtml+xml")) {
+                File xsltFile = org.wyona.commons.io.FileUtil.file(rtd.getConfigFile()
+                        .getParentFile()
+                        .getAbsolutePath(), "document2xhtml.xsl");
                 log.debug("XSLT file: " + xsltFile);
-                Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile));
-                transformer.setParameter("yanel.path.name", path.getName());
-                 // TODO: Is this the best way to generate an InputStream from an OutputStream?
+                Transformer transformer = TransformerFactory.newInstance()
+                        .newTransformer(new StreamSource(xsltFile));
+                transformer.setParameter("yanel.path.name", PathUtil.getName(getPath()));
+                // TODO: Is this the best way to generate an InputStream from an OutputStream?
                 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
-                transformer.transform(new StreamSource(getContentXML(rp)), new StreamResult(baos));
+                transformer.transform(new StreamSource(getContentXML(dataRepo)), new StreamResult(baos));
                 defaultView.setInputStream(new java.io.ByteArrayInputStream(baos.toByteArray()));
                 return defaultView;
             } else {
                 log.debug("Mime-Type: " + mimeType);
-                defaultView.setInputStream(rp.getRepo().getInputStream(new org.wyona.yarep.core.Path(rp.getPath().toString())));
+                defaultView.setInputStream(dataRepo.getInputStream(new org.wyona.yarep.core.Path(getPath())));
             }
-        } catch(Exception e) {
+        } catch (Exception e) {
             log.error(e);
         }
 
@@ -101,49 +107,41 @@
     }
 
     /**
-     *
+     * 
      */
-    private InputStream getContentXML(RepoPath rp) throws Exception {
-        JarInputStream jarStream = new JarInputStream((rp.getRepo().getInputStream(new org.wyona.yarep.core.Path(rp.getPath().toString()))));
+    private InputStream getContentXML(Repository dataRepo) throws Exception {
+        JarInputStream jarStream = new JarInputStream(dataRepo.getInputStream(new org.wyona.yarep.core.Path(getPath())));
         JarEntry jarEntry;
         while ((jarEntry = jarStream.getNextJarEntry()) != null) {
             log.debug("Jar Entry Name: " + jarEntry.getName());
             if (jarEntry.getName().equals("content.xml")) {
                 return jarStream;
             }
-        // TODO: What if zip does not contain a "content.xml"?!
+            // TODO: What if zip does not contain a "content.xml"?!
         }
         return null;
     }
 
     /**
-     *
+     * 
      */
-    private String getMimeType(Path path, String viewId) {
-        String mimeType = null;
-        try {
-            // TODO: Get yanel RTI yarep properties file name from framework resp. use MapFactory ...!
-            RepoPath rpRTI = new org.wyona.yarep.util.YarepUtil().getRepositoryPath(new org.wyona.yarep.core.Path(path.toString()), yanel.getRepositoryFactory("RTIRepositoryFactory"));
-            java.io.BufferedReader br = new java.io.BufferedReader(rpRTI.getRepo().getReader(new org.wyona.yarep.core.Path(new Path(rpRTI.getPath().toString()).getRTIPath().toString())));
-            br.readLine();
-            mimeType = br.readLine();
-            if (mimeType != null) {
-                if (mimeType.indexOf("mime-type:") == 0) {
-                    mimeType = mimeType.substring(11);
-                    log.info("*" + mimeType + "*");
-                    // TODO: Maybe validate mime-type ...
-                    return mimeType;
-                }
+    private String getMimeType(String viewId) {
+        String mimeType = getRTI().getProperty("mime-type");
+        if (mimeType != null) {
+            if (mimeType.indexOf("mime-type:") == 0) {
+                mimeType = mimeType.substring(11);
+                log.info("*" + mimeType + "*");
+                // TODO: Maybe validate mime-type ...
+                return mimeType;
             }
-        } catch(Exception e) {
-            log.warn(e);
+
         }
 
-        String suffix = path.getSuffix();
+        String suffix = PathUtil.getSuffix(getPath());
         if (suffix != null) {
             log.debug("SUFFIX: " + suffix);
             if (suffix.equals("html")) {
-                //mimeType = "text/html";
+                // mimeType = "text/html";
                 mimeType = "application/xhtml+xml";
             } else if (suffix.equals("xhtml")) {
                 mimeType = "application/xhtml+xml";
@@ -159,27 +157,21 @@
     }
 
     /**
-     *
+     * 
      */
-    public View getView(HttpServletRequest request, String viewId) {
-        return getView(new Path(request.getServletPath()), viewId);
-    }
-
-    /**
-     *
-     */
     public Reader getReader(Path path) {
         try {
-            RepoPath rp = new org.wyona.yarep.util.YarepUtil().getRepositoryPath(new org.wyona.yarep.core.Path(path.toString()), getRepositoryFactory());
+            RepoPath rp = new org.wyona.yarep.util.YarepUtil().getRepositoryPath(new org.wyona.yarep.core.Path(path.toString()),
+                    getRepositoryFactory());
             return rp.getRepo().getReader(new org.wyona.yarep.core.Path(rp.getPath().toString()));
-        } catch(Exception e) {
+        } catch (Exception e) {
             log.error(e);
         }
         return null;
     }
 
     /**
-     *
+     * 
      */
     public Reader getReader(Topic topic) {
         log.error("Not implemented yet!");
@@ -187,7 +179,7 @@
     }
 
     /**
-     *
+     * 
      */
     public Writer getWriter(Path path) {
         log.error("Not implemented yet!");
@@ -195,7 +187,7 @@
     }
 
     /**
-     *
+     * 
      */
     public Writer getWriter(Topic topic) {
         log.error("Not implemented yet!");
@@ -203,21 +195,100 @@
     }
 
     /**
-     *
+     * 
      */
     public OutputStream getOutputStream(Path path) {
         try {
-            RepoPath rp = new org.wyona.yarep.util.YarepUtil().getRepositoryPath(new org.wyona.yarep.core.Path(path.toString()), getRepositoryFactory());
-            return rp.getRepo().getOutputStream(new org.wyona.yarep.core.Path(rp.getPath().toString()));
-        } catch(Exception e) {
+            RepoPath rp = new org.wyona.yarep.util.YarepUtil().getRepositoryPath(new org.wyona.yarep.core.Path(path.toString()),
+                    getRepositoryFactory());
+            return rp.getRepo().getOutputStream(new org.wyona.yarep.core.Path(rp.getPath()
+                    .toString()));
+        } catch (Exception e) {
             log.error(e);
         }
         return null;
     }
-    
+
     protected RepositoryFactory getRepositoryFactory() {
         return yanel.getRepositoryFactory("DefaultRepositoryFactory");
     }
-    
 
+    /**
+     * 
+     */
+    public InputStream getInputStream() throws Exception {
+        return getRealm().getRepository().getNode(getPath()).getInputStream();
+    }
+
+    /**
+     * 
+     */
+    public OutputStream getOutputStream() throws Exception {
+        return getRealm().getRepository().getNode(getPath()).getOutputStream();
+    }
+
+    /**
+     * 
+     */
+    public void write(InputStream in) throws Exception {
+        log.warn("Not implemented yet!");
+    }
+
+    public boolean exists() throws Exception {
+        log.warn("Not implemented yet!");
+        return true;
+    }
+
+    /**
+     * Get size of generated page
+     */
+    public long getSize() throws Exception {
+        Node node = getRealm().getRepository().getNode(getPath());
+        long size;
+        if (node.isResource()) {
+            size = node.getSize();
+        } else {
+            size = 0;
+        }
+        return size;
+    }
+
+    /**
+     * 
+     */
+    public long getLastModified() throws Exception {
+        Node node = getRealm().getRepository().getNode(getPath());
+        long lastModified;
+        if (node.isResource()) {
+            lastModified = node.getLastModified();
+        } else {
+            lastModified = 0;
+        }
+
+        return lastModified;
+    }
+
+    /**
+     * 
+     */
+    public Writer getWriter() throws Exception {
+        log.error("Not implemented yet!");
+        return null;
+    }
+
+    /**
+     * 
+     */
+    public Reader getReader() throws Exception {
+        return new InputStreamReader(getInputStream(), "UTF-8");
+    }
+
+    /**
+     * 
+     */
+    public boolean delete() throws Exception {
+        getRealm().getRepository().getNode(getPath()).remove();
+        return true;
+    }
+
 }




More information about the Yanel-commits mailing list