svn commit: r739399 - in /lenya/branches/BRANCH_2_0_X/src/modules/editors: ./ java/src/org/apache/lenya/cms/editors/forms/ resources/css/ resources/i18n/ resources/javascript/sourceEditor/ usecases/forms/

[email protected]
Newsgroups gmane.comp.cms.lenya.cvs
Message-ID <[email protected]>
Author: andreas
Date: Fri Jan 30 20:13:19 2009
New Revision: 739399

URL: http://svn.apache.org/viewvc?rev=739399&view=rev
Log:
Removed special namespace handling from source editor.

Modified:
    lenya/branches/BRANCH_2_0_X/src/modules/editors/java/src/org/apache/lenya/cms/editors/forms/OneFormEditor.java
    lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/css/sourceEditor.css
    lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/i18n/cmsui.xml
    lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/javascript/sourceEditor/codemirror.js
    lenya/branches/BRANCH_2_0_X/src/modules/editors/sitemap.xmap
    lenya/branches/BRANCH_2_0_X/src/modules/editors/usecases/forms/oneform.jx

Modified: lenya/branches/BRANCH_2_0_X/src/modules/editors/java/src/org/apache/lenya/cms/editors/forms/OneFormEditor.java
URL: http://svn.apache.org/viewvc/lenya/branches/BRANCH_2_0_X/src/modules/editors/java/src/org/apache/lenya/cms/editors/forms/OneFormEditor.java?rev=739399&r1=739398&r2=739399&view=diff
==============================================================================
--- lenya/branches/BRANCH_2_0_X/src/modules/editors/java/src/org/apache/lenya/cms/editors/forms/OneFormEditor.java (original)
+++ lenya/branches/BRANCH_2_0_X/src/modules/editors/java/src/org/apache/lenya/cms/editors/forms/OneFormEditor.java Fri Jan 30 20:13:19 2009
@@ -19,6 +19,8 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
@@ -29,16 +31,15 @@
 
 import org.apache.cocoon.components.ContextHelper;
 import org.apache.cocoon.environment.Request;
+import org.apache.commons.io.IOUtils;
 import org.apache.lenya.cms.cocoon.source.SourceUtil;
 import org.apache.lenya.cms.linking.LinkConverter;
 import org.apache.lenya.cms.publication.ResourceType;
 import org.apache.lenya.cms.usecase.DocumentUsecase;
 import org.apache.lenya.cms.usecase.UsecaseException;
-import org.apache.lenya.cms.usecase.xml.UsecaseErrorHandler;
 import org.apache.lenya.cms.workflow.WorkflowUtil;
 import org.apache.lenya.cms.workflow.usecases.UsecaseWorkflowHelper;
 import org.apache.lenya.util.ServletHelper;
-import org.apache.lenya.xml.ChainErrorHandler;
 import org.apache.lenya.xml.DocumentHelper;
 import org.apache.lenya.xml.Schema;
 import org.apache.lenya.xml.ValidationUtil;
@@ -53,8 +54,10 @@
  * @version $Id$
  */
 public class OneFormEditor extends DocumentUsecase implements ErrorHandler {
-    
+
     protected static final String PARAM_VALIDATION_ERRORS = "validationErrors";
+    protected static final String PARAM_CONTENT = "content";
+    protected static final String DEFAULT_ENCODING = "utf-8";
 
     /**
      * @see org.apache.lenya.cms.usecase.AbstractUsecase#getNodesToLock()
@@ -69,6 +72,16 @@
                 .toArray(new org.apache.lenya.cms.repository.Node[nodes.size()]);
     }
 
+    protected void prepareView() throws Exception {
+        super.prepareView();
+
+        StringWriter writer = new StringWriter();
+        IOUtils.copy(getSourceDocument().getInputStream(), writer, DEFAULT_ENCODING);
+        String xmlString = writer.toString();
+        setParameter(PARAM_CONTENT, xmlString);
+        validate(xmlString, DEFAULT_ENCODING);
+    }
+
     /**
      * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckPreconditions()
      */
@@ -89,35 +102,30 @@
      */
     protected void doExecute() throws Exception {
         super.doExecute();
-        saveDocument(getXml());
+        saveDocument(getXml(getContent(), getRequestEncoding()));
     }
 
-    protected String getEncoding() {
+    protected String getRequestEncoding() {
         Request request = ContextHelper.getRequest(this.context);
         return request.getCharacterEncoding();
     }
 
-    protected String getXmlString(String encoding) {
-        // Get namespaces
-        String namespaces = removeRedundantNamespaces(getParameterAsString("namespaces"));
-        if (getLogger().isDebugEnabled()) {
-            getLogger().debug(namespaces);
-        }
-        // Aggregate content
-        return "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n"
-                + addNamespaces(namespaces, getParameterAsString("content"));
+    protected String getContent() {
+        return getParameterAsString(PARAM_CONTENT);
     }
 
     public void advance() throws UsecaseException {
         clearErrorMessages();
         try {
-            Document xml = getXml();
+            String content = getContent();
+            String encoding = getRequestEncoding();
+            Document xml = getXml(content, encoding);
             if (xml != null) {
-                validate();
+                validate(content, encoding);
             }
             if (!hasErrors()) {
-                SourceUtil.writeDOM(xml, getSourceDocument().getOutputStream());
-                deleteParameter("content");
+                IOUtils.copy(new StringReader(content), getSourceDocument().getOutputStream());
+                deleteParameter(PARAM_CONTENT);
             }
         } catch (Exception e) {
             throw new UsecaseException(e);
@@ -129,42 +137,34 @@
         if (hasErrors()) {
             return;
         }
-
-        // check document form
-        getXml();
-        
-        if (!hasErrors()) {
-            validate();
+        String encoding = getRequestEncoding();
+        Document xml = getXml(getContent(), encoding);
+        if (xml != null) {
+            validate(getContent(), encoding);
         }
-
     }
 
-    protected void validate() throws Exception {
+    protected void validate(String xmlString, String encoding) throws Exception {
         ResourceType resourceType = getSourceDocument().getResourceType();
         Schema schema = resourceType.getSchema();
         if (schema == null) {
             getLogger().info(
                     "No schema declared for resource type [" + resourceType.getName()
                             + "], skipping validation.");
-        }
-        else {
+        } else {
             deleteParameter(PARAM_VALIDATION_ERRORS);
-            ChainErrorHandler handler = new ChainErrorHandler();
-            handler.add(new UsecaseErrorHandler(this));
-            handler.add(this);
-            String encoding = getEncoding();
-            String xmlString = getXmlString(encoding);
             byte bytes[] = xmlString.getBytes(encoding);
             ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
             StreamSource source = new StreamSource(stream);
-            ValidationUtil.validate(this.manager, source, schema, handler);
+            ValidationUtil.validate(this.manager, source, schema, this);
+            if (!getValidationErrors().isEmpty()) {
+                addErrorMessage("editors.validationFailed");
+            }
         }
     }
 
-    protected Document getXml() throws ParserConfigurationException, IOException {
-        String encoding = getEncoding();
-        String xmlString = getXmlString(encoding);
-
+    protected Document getXml(String xmlString, String encoding)
+            throws ParserConfigurationException, IOException {
         try {
             return DocumentHelper.readDocument(xmlString, encoding);
         } catch (SAXException e) {
@@ -174,10 +174,9 @@
     }
 
     /**
-     * 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.
+     * 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 content The content to save.
      * @throws Exception if an error occurs.
@@ -205,55 +204,18 @@
         }
     }
 
-    /**
-     * Remove redundant namespaces
-     * 
-     * @param namespaces The namespaces to remove
-     * @return The namespace string without the removed namespaces
-     */
-    private String removeRedundantNamespaces(String namespaces) {
-        String[] namespace = namespaces.split(" ");
-
-        String ns = "";
-        for (int i = 0; i < namespace.length; i++) {
-            if (ns.indexOf(namespace[i]) < 0) {
-                ns = ns + " " + namespace[i];
-            } else {
-                if (getLogger().isDebugEnabled()) {
-                    getLogger().debug("Redundant namespace: " + namespace[i]);
-                }
-            }
-        }
-        return ns;
-    }
-
-    /**
-     * Add namespaces
-     * 
-     * @param namespaces The namespaces to add
-     * @param content The content to add them to
-     * @return The content with the added namespaces
-     */
-    private String addNamespaces(String namespaces, String content) {
-        String s = content.substring(0, content.indexOf(">"));
-        while (s.endsWith(" ") || s.endsWith("/")) {
-            s = s.substring(0, s.length() - 1);
-        }
-        return s + " " + namespaces + content.substring(s.length());
-    }
-
     protected String getEvent() {
         return "edit";
     }
-    
+
     public static class ValidationError {
-        
+
         protected static final int SEVERITY_WARNING = 0;
         protected static final int SEVERITY_ERROR = 1;
         protected static final int SEVERITY_FATAL = 2;
-        
+
         private int severity;
-        
+
         public int getLine() {
             return this.line;
         }
@@ -269,16 +231,16 @@
         private int line;
         private int column;
         private String message;
-        
+
         public ValidationError(int severity, SAXParseException e) {
             this.message = e.getMessage();
             this.line = e.getLineNumber();
             this.column = e.getColumnNumber();
             this.severity = severity;
         }
-        
+
     }
-    
+
     protected List getValidationErrors() {
         List errors = (List) getParameter(PARAM_VALIDATION_ERRORS);
         if (errors == null) {

Modified: lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/css/sourceEditor.css
URL: http://svn.apache.org/viewvc/lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/css/sourceEditor.css?rev=739399&r1=739398&r2=739399&view=diff
==============================================================================
--- lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/css/sourceEditor.css (original)
+++ lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/css/sourceEditor.css Fri Jan 30 20:13:19 2009
@@ -2,5 +2,5 @@
   clear: both;
   width: auto;
   border: solid 1px #CCCCCC;
-  margin: .5em 0;
+  margin: 0 0 .5em 0;
 }

Modified: lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/i18n/cmsui.xml
URL: http://svn.apache.org/viewvc/lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/i18n/cmsui.xml?rev=739399&r1=739398&r2=739399&view=diff
==============================================================================
--- lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/i18n/cmsui.xml (original)
+++ lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/i18n/cmsui.xml Fri Jan 30 20:13:19 2009
@@ -54,4 +54,7 @@
   <message key="upload-disabled">Upload is not enabled. Please check local.build.properties!</message>
   
   <message key="editors.sourceEditor">With Source Editor</message>
+  <message key="editors.validationFailed">Validation failed</message>
+  <message key="editors.validationErrorLine">line</message>
+  <message key="editors.validationErrorColumn">column</message>
 </catalogue>

Modified: lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/javascript/sourceEditor/codemirror.js
URL: http://svn.apache.org/viewvc/lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/javascript/sourceEditor/codemirror.js?rev=739399&r1=739398&r2=739399&view=diff
==============================================================================
--- lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/javascript/sourceEditor/codemirror.js (original)
+++ lenya/branches/BRANCH_2_0_X/src/modules/editors/resources/javascript/sourceEditor/codemirror.js Fri Jan 30 20:13:19 2009
@@ -28,17 +28,20 @@
 }
 
 function saveContent() {
-    if (editor) {
-        // Firefox
-        document.forms.oneform.content.innerHTML = editor.getCode();
-        // Safari
-        document.forms.oneform.content.value = editor.getCode();
-        
-        document.getElementById("save1").disabled = null;
-        document.getElementById("save2").disabled = null;
-    }
+    // Firefox
+    document.forms.oneform.content.innerHTML = editor.getCode();
+    // Safari
+    document.forms.oneform.content.value = editor.getCode();
+    
+    document.getElementById("save1").disabled = null;
+    document.getElementById("save2").disabled = null;
 }
 
 function indent() {
     editor.reindent();
 }
+
+function gotoPosition(lineNr, columnNr) {
+    var line = editor.nthLine(lineNr);
+    editor.selectLines(line, columnNr);
+}
\ No newline at end of file

Modified: lenya/branches/BRANCH_2_0_X/src/modules/editors/sitemap.xmap
URL: http://svn.apache.org/viewvc/lenya/branches/BRANCH_2_0_X/src/modules/editors/sitemap.xmap?rev=739399&r1=739398&r2=739399&view=diff
==============================================================================
--- lenya/branches/BRANCH_2_0_X/src/modules/editors/sitemap.xmap (original)
+++ lenya/branches/BRANCH_2_0_X/src/modules/editors/sitemap.xmap Fri Jan 30 20:13:19 2009
@@ -63,22 +63,6 @@
         <map:serialize type="xml"/>
       </map:match>
       
-      <map:match pattern="oneform">
-        <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=""/>
-          <map:otherwise>
-            <map:transform src="fallback://lenya/modules/prettyprinting/xslt/xml2nicexml.xsl"/>
-          </map:otherwise>
-        </map:select> 
-        <map:transform src="fallback://lenya/modules/editors/usecases/forms/oneform.xsl">
-          <map:parameter name="docid" value="{page-envelope:document-uuid}"/>
-          <map:parameter name="language" value="{page-envelope:document-language}"/>
-        </map:transform>
-        <map:serialize type="xml"/>
-      </map:match>
-      
       <map:match pattern="stream.xml">
         <map:generate type="stream"/>
         <map:transform type="incoming-proxy"/>

Modified: lenya/branches/BRANCH_2_0_X/src/modules/editors/usecases/forms/oneform.jx
URL: http://svn.apache.org/viewvc/lenya/branches/BRANCH_2_0_X/src/modules/editors/usecases/forms/oneform.jx?rev=739399&r1=739398&r2=739399&view=diff
==============================================================================
--- lenya/branches/BRANCH_2_0_X/src/modules/editors/usecases/forms/oneform.jx (original)
+++ lenya/branches/BRANCH_2_0_X/src/modules/editors/usecases/forms/oneform.jx Fri Jan 30 20:13:19 2009
@@ -33,7 +33,7 @@
     <script type="text/javascript" src="/modules/editors/javascript/sourceEditor/codemirror.js"/>
     <link rel="stylesheet" type="text/css" href="/modules/editors/css/sourceEditor.css"/>
   </page:head>
-  <page:body onload="insertEditor()">
+  <page:body onload="insertEditor();">
     
 
     <jx:import uri="fallback://lenya/modules/usecase/templates/messages.jx"/>
@@ -99,18 +99,27 @@
               onclick="triggerUsecase('insertAsset')"/>
           </div>
 
+          <jx:set var="validationErrors" value="${usecase.getParameter('validationErrors')}"/>
+          <jx:if test="${validationErrors.size() != 0}">
+            <div class="lenyaInfoBox">
+              <h3><i18n:text>editors.validationFailed</i18n:text></h3>
+              <ul class="lenyaMessages">
+                <jx:forEach var="error" items="${validationErrors}">
+                  <li class="lenyaErrorMessage">
+                    ${error.getMessage()}
+                    <a onclick="gotoPosition(${error.getLine()}, ${error.getColumn()})" href="#">
+                      (<i18n:text>editors.validationErrorLine</i18n:text> ${error.getLine()},
+                      <i18n:text>editors.validationErrorColumn</i18n:text> ${error.getColumn()})
+                    </a>
+                  </li>
+                </jx:forEach>
+              </ul>
+            </div>
+          </jx:if>
+          
           <div class="editorContentContainer">
-            <jx:choose>
-              <jx:when test="${usecase.getParameter('content')}">
-                <textarea name="content" style="display: none"/>
-                <textarea id="editorContent" cols="120" rows="80">${usecase.getParameter('content')}</textarea>
-              </jx:when>
-              <jx:otherwise>
-                <cinclude:includexml>
-                  <cinclude:src>cocoon://modules/editors/oneform</cinclude:src>
-                </cinclude:includexml>
-              </jx:otherwise>
-            </jx:choose>
+            <textarea name="content" style="display: none"/>
+            <textarea id="editorContent" cols="120" rows="80">${usecase.getParameter('content')}</textarea>
           </div>
 
           <div>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.