You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2007/02/21 17:01:53 UTC

svn commit: r510073 - in /lenya/trunk/src/modules/editors: java/src/org/apache/lenya/cms/editors/forms/OneFormEditor.java sitemap.xmap usecases/forms/oneform.jx

Author: andreas
Date: Wed Feb 21 08:01:52 2007
New Revision: 510073

URL: http://svn.apache.org/viewvc?view=rev&rev=510073
Log:
Fixed some 1-form-editor issues.

Modified:
    lenya/trunk/src/modules/editors/java/src/org/apache/lenya/cms/editors/forms/OneFormEditor.java
    lenya/trunk/src/modules/editors/sitemap.xmap
    lenya/trunk/src/modules/editors/usecases/forms/oneform.jx

Modified: lenya/trunk/src/modules/editors/java/src/org/apache/lenya/cms/editors/forms/OneFormEditor.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/editors/java/src/org/apache/lenya/cms/editors/forms/OneFormEditor.java?view=diff&rev=510073&r1=510072&r2=510073
==============================================================================
--- lenya/trunk/src/modules/editors/java/src/org/apache/lenya/cms/editors/forms/OneFormEditor.java (original)
+++ lenya/trunk/src/modules/editors/java/src/org/apache/lenya/cms/editors/forms/OneFormEditor.java Wed Feb 21 08:01:52 2007
@@ -18,16 +18,22 @@
 package org.apache.lenya.cms.editors.forms;
 
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
+import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
 import org.apache.cocoon.components.ContextHelper;
 import org.apache.cocoon.environment.Request;
-import org.apache.excalibur.source.Source;
-import org.apache.excalibur.source.SourceResolver;
+import org.apache.lenya.cms.cocoon.source.SourceUtil;
 import org.apache.lenya.cms.publication.ResourceType;
 import org.apache.lenya.cms.usecase.DocumentUsecase;
 import org.apache.lenya.cms.usecase.UsecaseException;
@@ -47,6 +53,8 @@
  */
 public class OneFormEditor extends DocumentUsecase {
 
+    private static final String REFORMAT_XSLT_URI = "fallback://lenya/modules/editors/usecases/forms/prettyprint.xsl";
+
     /**
      * @see org.apache.lenya.cms.usecase.AbstractUsecase#getNodesToLock()
      */
@@ -60,7 +68,9 @@
      */
     protected void doCheckPreconditions() throws Exception {
         super.doCheckPreconditions();
-        UsecaseWorkflowHelper.checkWorkflow(this.manager, this, getEvent(), getSourceDocument(), getLogger());
+        UsecaseWorkflowHelper.checkWorkflow(this.manager, this, getEvent(), getSourceDocument(),
+                getLogger());
+        setParameter("executable", Boolean.valueOf(!hasErrors()));
     }
 
     /**
@@ -69,55 +79,121 @@
     protected void doExecute() throws Exception {
         super.doExecute();
 
+        String encoding = getEncoding();
+        String content = getXmlString(encoding);
+        saveDocument(encoding, content);
+    }
+
+    protected String getEncoding() {
+        Request request = ContextHelper.getRequest(this.context);
+        String encoding = request.getCharacterEncoding();
+        return encoding;
+    }
+
+    protected String getXmlString(String encoding) {
         // Get namespaces
         String namespaces = removeRedundantNamespaces(getParameterAsString("namespaces"));
         if (getLogger().isDebugEnabled()) {
             getLogger().debug(namespaces);
         }
-
         // Aggregate content
-
-        Request request = ContextHelper.getRequest(this.context);
-        String encoding = request.getCharacterEncoding();
         String content = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n"
                 + addNamespaces(namespaces, getParameterAsString("content"));
+        return content;
+    }
 
-        saveDocument(encoding, content);
+    public void advance() throws UsecaseException {
+        
+        clearErrorMessages();
+        try {
+            Document xml = getXml();
+            if (xml != null) {
+                validate(xml);
+            }
+            if (!hasErrors()) {
+                SourceUtil.writeDOM(xml, getSourceDocument().getOutputStream());
+                deleteParameter("content");
+            }
+        } catch (Exception e) {
+            throw new UsecaseException(e);
+        }
+
+        /*
+        if (getParameter("reformat") != null) {
+            clearErrorMessages();
+            try {
+                Document xml = getXml();
+                if (xml != null) {
+                    validate(xml);
+                }
+                if (!hasErrors()) {
+                    Document xslt = SourceUtil.readDOM(REFORMAT_XSLT_URI, this.manager);
+                    DOMSource xsltSource = new DOMSource(xslt);
+
+                    TransformerFactory factory = TransformerFactory.newInstance();
+                    Transformer transformer = factory.newTransformer(xsltSource);
+                    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+                    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+
+                    DOMSource source = new DOMSource(xml);
+                    StringWriter writer = new StringWriter();
+                    StreamResult result = new StreamResult(writer);
+                    transformer.transform(source, result);
+
+                    setParameter("content", writer.toString());
+                }
+            } catch (Exception e) {
+                throw new UsecaseException(e);
+            }
+        }
+        */
     }
 
-    /**
-     * Save the content to the document source. After saving, the XML is validated. If validation
-     * errors occur, the usecase transaction is rolled back, so the changes are not persistent. If
-     * the validation succeeded, the workflow event is invoked.
-     * @param encoding The encoding to use.
-     * @param content The content to save.
-     * @throws Exception if an error occurs.
-     */
-    protected void saveDocument(String encoding, String content) throws Exception {
-        saveXMLFile(encoding, content, getSourceDocument());
+    protected void doCheckExecutionConditions() throws Exception {
+        super.doCheckExecutionConditions();
+        if (hasErrors()) {
+            return;
+        }
+
+        Document xml = getXml();
+        if (xml != null) {
+            validate(xml);
+        }
+    }
+
+    protected void validate(Document xml) throws Exception {
+        ResourceType resourceType = getSourceDocument().getResourceType();
+        Schema schema = resourceType.getSchema();
+        ValidationUtil.validate(this.manager, xml, schema, new UsecaseErrorHandler(this));
+    }
 
-        Document xmlDoc = null;
+    protected Document getXml() throws ParserConfigurationException, IOException {
+        String encoding = getEncoding();
+        String xmlString = getXmlString(encoding);
 
+        Document xml = null;
         try {
-            xmlDoc = DocumentHelper.readDocument(getSourceDocument().getInputStream());
+            xml = DocumentHelper.readDocument(xmlString, encoding);
         } catch (SAXException e) {
             addErrorMessage("error-document-form", new String[] { e.getMessage() });
         }
+        return xml;
+    }
 
-        if (xmlDoc != null) {
-            ResourceType resourceType = getSourceDocument().getResourceType();
-            Schema schema = resourceType.getSchema();
-
-            ValidationUtil.validate(this.manager, xmlDoc, schema, new UsecaseErrorHandler(this));
+    /**
+     * Save the content to the document source. After saving, the XML is
+     * validated. If validation errors occur, the usecase transaction is rolled
+     * back, so the changes are not persistent. If the validation succeeded, the
+     * workflow event is invoked.
+     * @param encoding The encoding to use.
+     * @param content The content to save.
+     * @throws Exception if an error occurs.
+     */
+    protected void saveDocument(String encoding, String content) throws Exception {
+        saveXMLFile(encoding, content, getSourceDocument());
 
-            if (!hasErrors()) {
-                WorkflowUtil.invoke(this.manager,
-                        getSession(),
-                        getLogger(),
-                        getSourceDocument(),
-                        getEvent());
-            }
-        }
+        WorkflowUtil.invoke(this.manager, getSession(), getLogger(), getSourceDocument(),
+                getEvent());
     }
 
     /**
@@ -130,8 +206,8 @@
      * @throws IOException if an IO error occurs
      */
     private void saveXMLFile(String encoding, String content,
-            org.apache.lenya.cms.publication.Document document)
-            throws FileNotFoundException, UnsupportedEncodingException, IOException {
+            org.apache.lenya.cms.publication.Document document) throws FileNotFoundException,
+            UnsupportedEncodingException, IOException {
         Writer writer = null;
 
         try {

Modified: lenya/trunk/src/modules/editors/sitemap.xmap
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/editors/sitemap.xmap?view=diff&rev=510073&r1=510072&r2=510073
==============================================================================
--- lenya/trunk/src/modules/editors/sitemap.xmap (original)
+++ lenya/trunk/src/modules/editors/sitemap.xmap Wed Feb 21 08:01:52 2007
@@ -56,7 +56,7 @@
       </map:match>
       
       <map:match pattern="oneform">
-        <map:generate src="lenyadoc:/{page-envelope:document-language}/{page-envelope:document-uuid}"/>
+        <map:generate src="lenya-document:{page-envelope:document-uuid},lang={page-envelope:document-language}?session=usecase"/>
         <map:select type="parameter">
           <map:parameter name="parameter-selector-test" value="{request-param:reformat}"/>
           <map:when test=""/>

Modified: lenya/trunk/src/modules/editors/usecases/forms/oneform.jx
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/editors/usecases/forms/oneform.jx?view=diff&rev=510073&r1=510072&r2=510073
==============================================================================
--- lenya/trunk/src/modules/editors/usecases/forms/oneform.jx (original)
+++ lenya/trunk/src/modules/editors/usecases/forms/oneform.jx Wed Feb 21 08:01:52 2007
@@ -33,7 +33,7 @@
       <input type="hidden" name="lenya.continuation" value="${continuation.id}"/>
       <input type="hidden" name="lenya.usecase" value="${usecase.getName()}"/>
       <jx:choose>
-        <jx:when test="${! usecase.hasErrors()}">
+        <jx:when test="${usecase.getParameter('executable').booleanValue()}">
           
           <div class="lenya-box-toggled" style="float: left; margin-right: 10px;">
             <div class="lenya-box-title">Information</div>
@@ -81,9 +81,16 @@
           </div>
 
           <div style="clear: both;">
-            <cinclude:includexml>
-              <cinclude:src>cocoon://modules/editors/oneform</cinclude:src>
-            </cinclude:includexml>
+            <jx:choose>
+              <jx:when test="${usecase.getParameter('content')}">
+                <textarea name="content" cols="120" rows="80"><jx:out value="${usecase.getParameter('content')}"/></textarea>
+              </jx:when>
+              <jx:otherwise>
+                <cinclude:includexml>
+                  <cinclude:src>cocoon://modules/editors/oneform</cinclude:src>
+                </cinclude:includexml>
+              </jx:otherwise>
+            </jx:choose>
           </div>
 
           <div>



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org