[Yanel-commits] rev 40887 - in public/yanel/contributions/resources/xinha: htdocs src/java/org/wyona/yanel/impl/resources/xinha

simon at wyona.com simon at wyona.com
Sat Jan 10 01:05:05 CET 2009


Author: simon
Date: 2009-01-10 01:05:05 +0100 (Sat, 10 Jan 2009)
New Revision: 40887

Modified:
   public/yanel/contributions/resources/xinha/htdocs/xinha-fix.jelly
   public/yanel/contributions/resources/xinha/htdocs/xinha.jelly
   public/yanel/contributions/resources/xinha/src/java/org/wyona/yanel/impl/resources/xinha/XinhaResource.java
Log:
[refactoring]
-code reuse
-check for wellformness when open a document
-logging improved


Modified: public/yanel/contributions/resources/xinha/htdocs/xinha-fix.jelly
===================================================================
--- public/yanel/contributions/resources/xinha/htdocs/xinha-fix.jelly	2009-01-09 22:23:17 UTC (rev 40886)
+++ public/yanel/contributions/resources/xinha/htdocs/xinha-fix.jelly	2009-01-10 00:05:05 UTC (rev 40887)
@@ -14,11 +14,12 @@
             <form method="post" action="">
               <input type="hidden" name="edit-path" value="${resource.getEditPath()}"/>
               <textarea id="${resource.getEditPath()}" name="${resource.getEditPath()}" rows="15" cols="80" style="width: 100%">
-                ${resource.getEscapedContent()}
+                ${resource.escapeXML(resource.getContentToFix())}
               </textarea>
               <br />
               <input type="submit" id="submit" name="submit" value="Save" />
               <input type="submit" id="submit-tidy" name="submit-tidy" value="Tidy" />
+              <input type="submit" id="submit-tidy-save" name="submit-tidy-save" value="Tidy Save" />
               <input type="submit" id="submit" name="cancel" value="Cancel" />
             </form>
           </div>

Modified: public/yanel/contributions/resources/xinha/htdocs/xinha.jelly
===================================================================
--- public/yanel/contributions/resources/xinha/htdocs/xinha.jelly	2009-01-09 22:23:17 UTC (rev 40886)
+++ public/yanel/contributions/resources/xinha/htdocs/xinha.jelly	2009-01-10 00:05:05 UTC (rev 40887)
@@ -35,7 +35,7 @@
           <form method="post" action="">
             <input type="hidden" name="edit-path" value="${resource.getEditPath()}"/>
             <textarea id="${resource.getEditPath()}" name="${resource.getEditPath()}" rows="15" cols="80" style="width: 100%">
-              ${resource.getEditorFormContent()}
+              ${resource.escapeXML(resource.getResourceContent())}
             </textarea>
             <br />
             <input type="submit" id="submit" name="submit" value="Save" />

Modified: public/yanel/contributions/resources/xinha/src/java/org/wyona/yanel/impl/resources/xinha/XinhaResource.java
===================================================================
--- public/yanel/contributions/resources/xinha/src/java/org/wyona/yanel/impl/resources/xinha/XinhaResource.java	2009-01-09 22:23:17 UTC (rev 40886)
+++ public/yanel/contributions/resources/xinha/src/java/org/wyona/yanel/impl/resources/xinha/XinhaResource.java	2009-01-10 00:05:05 UTC (rev 40887)
@@ -56,18 +56,30 @@
     private static final String PARAMETER_EDIT_PATH = "edit-path";
     private static final String PARAMETER_CONTINUE_PATH = "continue-path";
     private static final String PARAM_SUBMIT_TIDY = "submit-tidy";
-
+    private static final String PARAM_SUBMIT_TIDY_SAVE = "submit-tidy-save";
     private static final String VIEW_FIX_WELLFORMNESS = "fix-wellformness";
     
-    private String content;
+    private String editorContent;
+    private String resourceContent;
+    private String editPath;
+    private String contentToFix;
+    private Resource resToEdit;
 
+    /* (non-Javadoc)
+     * @see org.wyona.yanel.impl.resources.usecase.ExecutableUsecaseResource#processUsecase(java.lang.String)
+     */
     protected View processUsecase(String viewID) throws UsecaseException {
         if (getParameter(PARAM_SUBMIT) != null) {
-            if (!isWellformed()) {
+            if (!isWellformed(IOUtils.toInputStream(getEditorContent()))) {
+                contentToFix = getEditorContent();
                 return generateView(VIEW_FIX_WELLFORMNESS);
             }
             execute();
             return generateView(VIEW_DONE);
+        } else if (getParameter(PARAM_SUBMIT_TIDY_SAVE) != null) {
+            tidy();
+            execute();
+            return generateView(VIEW_DONE);
         } else if (getParameter(PARAM_SUBMIT_TIDY) != null) {
             tidy();
             return generateView(VIEW_FIX_WELLFORMNESS);
@@ -75,32 +87,113 @@
             cancel();
             return generateView(VIEW_CANCEL);
         } else {
+            if (!isWellformed(IOUtils.toInputStream(getResourceContent()))) {
+                contentToFix = getResourceContent();
+                return generateView(VIEW_FIX_WELLFORMNESS);
+            }
             return generateView(viewID); // this will show the default view if the param is not set
         }
     }
     
+    /* (non-Javadoc)
+     * @see org.wyona.yanel.impl.resources.usecase.ExecutableUsecaseResource#checkPreconditions()
+     */
+    public boolean checkPreconditions() throws UsecaseException {
+        if (!ResourceAttributeHelper.hasAttributeImplemented(getResToEdit(), "Modifiable", "2")) {
+            addError("The resource you wanted to edit does not implement ModifiableV2 and is therefor not editable with this editor.");
+            return false;
+        }
+        return true;
+    }
+    
+    /* (non-Javadoc)
+     * @see org.wyona.yanel.impl.resources.usecase.ExecutableUsecaseResource#execute()
+     */
     public void execute() throws UsecaseException {
-        final String editPath = getEditPath();
-        final String content = getContent();
-
-        if (log.isDebugEnabled()) log.debug("Content while exectuing: " + content);
-        
-        try {
-            Resource resToEdit = getYanel().getResourceManager().getResource(getEnvironment(), getRealm(), editPath);
+        final String content = getEditorContent();
+        final Resource resToEdit = getResToEdit();
+        if (log.isDebugEnabled()) log.debug("saving content: " + content);
             if (ResourceAttributeHelper.hasAttributeImplemented(resToEdit, "Modifiable", "2")) {
-                OutputStream os = ((ModifiableV2) resToEdit).getOutputStream();
-                IOUtils.write(content, os);
+                try {
+                    OutputStream os = ((ModifiableV2) resToEdit).getOutputStream();
+                    IOUtils.write(content, os);
+                } catch (Exception e) {
+                    log.error("Exception: " + e);
+                    throw new UsecaseException(e.getMessage(), e);
+                }
                 addInfoMessage("Successfully saved.");
-                setParameter(PARAMETER_CONTINUE_PATH, PathUtil.backToRealm(getPath()) + editPath.substring(1)); // allow jelly template to show link to new event
             } else {
-                addError("The resource you wanted to edit does not implement VieableV2 and is therefor not editable with this editor.");
+                addError("Could not save the document.");
             }
-        } catch (Exception e) {
-            log.error("Exception: " + e);
-            throw new UsecaseException(e.getMessage(), e);
+            setParameter(PARAMETER_CONTINUE_PATH, PathUtil.backToRealm(getPath()) + getEditPath().substring(1)); // allow jelly template to show link to new event
+    }
+    
+    /* (non-Javadoc)
+     * @see org.wyona.yanel.impl.resources.usecase.ExecutableUsecaseResource#cancel()
+     */
+    public void cancel() throws UsecaseException {
+        addInfoMessage("Cancled.");
+        setParameter(PARAMETER_CONTINUE_PATH, PathUtil.backToRealm(getPath()) + getEditPath().substring(1)); // allow jelly template to show link to new event
+    }
+    
+    /**
+     * Get the String containing the path to the resource which is going to be edited
+     * @return String
+     */
+    public String getEditPath() {
+        if (editPath == null) {
+            if(log.isDebugEnabled()) log.debug("editPath not set yet.");
+            editPath = getEnvironment().getRequest().getParameter(PARAMETER_EDIT_PATH);
         }
+        if(log.isDebugEnabled()) log.debug("editPath set to: " + editPath);
+        return editPath;
     }
+
+    /**
+     * Get the String with the content of the resource which is going to be edited
+     * @return String 
+     */
+    public String getResourceContent() throws UsecaseException {
+        if (resourceContent == null) {
+            if(log.isDebugEnabled()) log.debug("resourceContent not set yet. Path: " + getEditPath());
+            Resource resToEdit = getResToEdit();
+            if (ResourceAttributeHelper.hasAttributeImplemented(resToEdit, "Modifiable", "2")) {
+                try {
+                    InputStream is = ((ModifiableV2) resToEdit).getInputStream();
+                    resourceContent = IOUtils.toString(is);
+                } catch (Exception e) {
+                    log.error("Exception: " + e);
+                    throw new UsecaseException(e.getMessage(), e);
+                }
+            } else {
+                addError("This resource can not be edited.");
+            }
+        }
+        if(log.isDebugEnabled()) log.debug("content set to: " + resourceContent);
+        return resourceContent;
+    }
+
+    /**
+     * Get the content proposed to tidy
+     * @return String
+     */
+    public String getContentToFix() {
+        return contentToFix;
+    }
+
+    /**
+     * escape xml
+     * @param String to escape
+     * @return String escaped
+     */
+    public String escapeXML(String string) {
+        return StringEscapeUtils.escapeXml(string);
+    }
     
+    /**
+     * Tidy content
+     * @throws UsecaseException
+     */
     private void tidy() throws UsecaseException {
         //TODO: tidy should be configured via an external file (e.g. in htdocs)
         ByteArrayOutputStream os = new ByteArrayOutputStream();
@@ -108,20 +201,21 @@
         tidy.setXHTML(true);
         tidy.setInputEncoding("utf-8");
         tidy.setOutputEncoding("utf-8");
-        tidy.parse(IOUtils.toInputStream(getContent()), os);
+        tidy.parse(IOUtils.toInputStream(getEditorContent()), os);
         try {
-            content = os.toString("utf-8");
+            editorContent = os.toString("utf-8");
         } catch (Exception e) {
             throw new UsecaseException(e.getMessage(), e);
         }
+        addInfoMessage("Content cleaned with tidy.");
     }
-    
-    public void cancel() throws UsecaseException {
-        addInfoMessage("Cancled.");
-        setParameter(PARAMETER_CONTINUE_PATH, PathUtil.backToRealm(getPath()) + getEditPath().substring(1)); // allow jelly template to show link to new event
-    }
 
-    public boolean isWellformed() throws UsecaseException {
+    /**
+     * Checks if content is wellformed
+     * @return boolean
+     * @throws UsecaseException
+     */
+    private boolean isWellformed(InputStream is) throws UsecaseException {
         try {
             //TODO: code borrowed from YanelServlet.java r40436. see line 902. 1. maybe there is a better way to do so. 2. this code could maybe be refactored into a some xml.util lib. 
             javax.xml.parsers.DocumentBuilderFactory dbf= javax.xml.parsers.DocumentBuilderFactory.newInstance();
@@ -133,8 +227,7 @@
             //       resp. http://xml.apache.org/commons/components/resolver/
             // TODO: What about a resolver factory?
             parser.setEntityResolver(new CatalogResolver());
-            String content = getContent();
-            parser.parse(IOUtils.toInputStream(content));
+            parser.parse(is);
             return true;
         } catch (org.xml.sax.SAXException e) {
             addError(e.getMessage());
@@ -145,57 +238,36 @@
         }
     }
     
-    public String getEditorFormContent() throws UsecaseException {
-        try {
-            if(log.isDebugEnabled()) log.debug(getEditPath());
-            Resource resToEdit = getYanel().getResourceManager().getResource(getEnvironment(), getRealm(), getEditPath());
-            if (ResourceAttributeHelper.hasAttributeImplemented(resToEdit, "Modifiable", "2")) {
-                InputStream is = ((ModifiableV2) resToEdit).getInputStream();
-                return StringEscapeUtils.escapeXml(IOUtils.toString(is));
-            } else {
-                return"The resource you wanted to edit does not implement VieableV2 and is therefor not editable with this editor.";
-            }
-        } catch (Exception e) {
-            log.error("Exception: " + e);
-            throw new UsecaseException(e.getMessage(), e);
-        }
-    }
-
     /**
-     * TODO: no lookup resource-type yet
-     * @return
-     * @throws UsecaseException
+     * Get the String with the content of the resource which is going to be edited
+     * @return String 
      */
-    public String getLookup() throws UsecaseException {
-        try {
-            SourceResolver resolver = new SourceResolver(this);
-            Source source = resolver.resolve("yanelresource:/usecases/lookup.html", null);
-            InputStream htodoc = ((StreamSource) source).getInputStream();
-            return IOUtils.toString(htodoc);
-        } catch (Exception e) {
-            return "no parameter edit-path found in the request. don't know what to edit";
+    private String getEditorContent() {
+        if (editorContent == null) {
+            if(log.isDebugEnabled()) log.debug("content not set yet.");
+            editorContent = getParameterAsString(getEditPath()); 
         }
+        if(log.isDebugEnabled()) log.debug("content set to: " + editorContent);
+        return editorContent;
     }
     
-    public String getEditPath() {
-        return request.getParameter(PARAMETER_EDIT_PATH);
-    }
+
     
     /**
-     * @return 
+     * Get the Resource which is going to be edited
+     * @return Resource
      */
-    private String getContent() {
-        if(log.isDebugEnabled()) log.debug("Member field content: " + content);
-        if (content != null) {
-            if(log.isDebugEnabled()) log.debug("content is allready set.");
-            return content;
-        } else {
-            content = getEnvironment().getRequest().getParameter(getEditPath()); 
+    private Resource getResToEdit() throws UsecaseException {
+        if (resToEdit == null) {
+            if(log.isDebugEnabled()) log.debug("resToEdit not set yet.");
+            try {
+                resToEdit = getYanel().getResourceManager().getResource(getEnvironment(), getRealm(), getEditPath());
+            } catch (Exception e) {
+                log.error("Exception: " + e);
+                throw new UsecaseException(e.getMessage(), e);
+            }
         }
-        return content;
+        if(log.isDebugEnabled()) log.debug("resToEdit set to: " + resToEdit.getResourceTypeLocalName());
+        return resToEdit;
     }
-    
-    public String getEscapedContent() {
-        return StringEscapeUtils.escapeXml(getContent());
-    }
 }



More information about the Yanel-commits mailing list