svn commit: r738839 - in /lenya/trunk/src: java/org/apache/lenya/xml/ modules/editors/java/src/org/apache/lenya/cms/editors/forms/ modules/editors/resources/i18n/

[email protected]
Newsgroups gmane.comp.cms.lenya.cvs
Message-ID <[email protected]>
Author: andreas
Date: Thu Jan 29 12:38:17 2009
New Revision: 738839

URL: http://svn.apache.org/viewvc?rev=738839&view=rev
Log:
Show line and column numbers in source editor. The numbers aren't correct ATM because of the omitted XML and namespace declarations.

Added:
    lenya/trunk/src/java/org/apache/lenya/xml/ChainErrorHandler.java
Modified:
    lenya/trunk/src/java/org/apache/lenya/xml/ValidationUtil.java
    lenya/trunk/src/modules/editors/java/src/org/apache/lenya/cms/editors/forms/OneFormEditor.java
    lenya/trunk/src/modules/editors/resources/i18n/cmsui.xml
    lenya/trunk/src/modules/editors/resources/i18n/cmsui_de.xml

Added: lenya/trunk/src/java/org/apache/lenya/xml/ChainErrorHandler.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/java/org/apache/lenya/xml/ChainErrorHandler.java?rev=738839&view=auto
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/xml/ChainErrorHandler.java (added)
+++ lenya/trunk/src/java/org/apache/lenya/xml/ChainErrorHandler.java Thu Jan 29 12:38:17 2009
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lenya.xml;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.lenya.util.Assert;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+public class ChainErrorHandler implements ErrorHandler {
+    
+    private List handlers = new ArrayList();
+    
+    public void add(ErrorHandler handler) {
+        Assert.notNull("handler", handler);
+        this.handlers.add(handler);
+    }
+
+    public void error(SAXParseException e) throws SAXException {
+        for (Iterator i = this.handlers.iterator(); i.hasNext(); ) {
+            ((ErrorHandler) i.next()).error(e);
+        }
+    }
+
+    public void fatalError(SAXParseException e) throws SAXException {
+        for (Iterator i = this.handlers.iterator(); i.hasNext(); ) {
+            ((ErrorHandler) i.next()).fatalError(e);
+        }
+    }
+
+    public void warning(SAXParseException e) throws SAXException {
+        for (Iterator i = this.handlers.iterator(); i.hasNext(); ) {
+            ((ErrorHandler) i.next()).warning(e);
+        }
+    }
+
+}

Modified: lenya/trunk/src/java/org/apache/lenya/xml/ValidationUtil.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/java/org/apache/lenya/xml/ValidationUtil.java?rev=738839&r1=738838&r2=738839&view=diff
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/xml/ValidationUtil.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/xml/ValidationUtil.java Thu Jan 29 12:38:17 2009
@@ -17,6 +17,11 @@
  */
 package org.apache.lenya.xml;
 
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXResult;
+
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.cocoon.components.validation.Validator;
 import org.apache.cocoon.xml.dom.DOMStreamer;
@@ -60,6 +65,7 @@
                     handler);
 
             DOMStreamer streamer = new DOMStreamer(validatorHandler);
+            streamer.setNormalizeNamespaces(false);
             streamer.stream(xmlDoc);
 
         } finally {
@@ -69,4 +75,31 @@
         }
     }
 
+    /**
+     * @param manager The service manager.
+     * @param source The source to validate.
+     * @param schema The schema to use.
+     * @param handler The SAX error handler.
+     * @throws Exception if an error occurs.
+     */
+    public static void validate(ServiceManager manager, Source source, Schema schema,
+            ErrorHandler handler) throws Exception {
+
+        Validator validator = null;
+        try {
+            validator = (Validator) manager.lookup(Validator.ROLE);
+            ContentHandler validatorHandler = validator.getValidationHandler(schema.getURI(),
+                    handler);
+
+            Transformer transformer = TransformerFactory.newInstance().newTransformer();
+            SAXResult result = new SAXResult(validatorHandler);
+            transformer.transform(source, result);
+
+        } finally {
+            if (validator != null) {
+                manager.release(validator);
+            }
+        }
+    }
+
 }

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?rev=738839&r1=738838&r2=738839&view=diff
==============================================================================
--- 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 Thu Jan 29 12:38:17 2009
@@ -17,11 +17,15 @@
  */
 package org.apache.lenya.cms.editors.forms;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.stream.StreamSource;
 
 import org.apache.cocoon.components.ContextHelper;
 import org.apache.cocoon.environment.Request;
@@ -34,18 +38,23 @@
 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;
 import org.w3c.dom.Document;
+import org.xml.sax.ErrorHandler;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 
 /**
  * One form editor.
  * 
  * @version $Id$
  */
-public class OneFormEditor extends DocumentUsecase {
+public class OneFormEditor extends DocumentUsecase implements ErrorHandler {
+    
+    protected static final String PARAM_VALIDATION_ERRORS = "validationErrors";
 
     /**
      * @see org.apache.lenya.cms.usecase.AbstractUsecase#getNodesToLock()
@@ -80,14 +89,12 @@
      */
     protected void doExecute() throws Exception {
         super.doExecute();
-        Document xml = getXml();
-        saveDocument(xml);
+        saveDocument(getXml());
     }
 
     protected String getEncoding() {
         Request request = ContextHelper.getRequest(this.context);
-        String encoding = request.getCharacterEncoding();
-        return encoding;
+        return request.getCharacterEncoding();
     }
 
     protected String getXmlString(String encoding) {
@@ -97,9 +104,8 @@
             getLogger().debug(namespaces);
         }
         // Aggregate content
-        String content = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n"
+        return "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n"
                 + addNamespaces(namespaces, getParameterAsString("content"));
-        return content;
     }
 
     public void advance() throws UsecaseException {
@@ -107,7 +113,7 @@
         try {
             Document xml = getXml();
             if (xml != null) {
-                validate(xml);
+                validate();
             }
             if (!hasErrors()) {
                 SourceUtil.writeDOM(xml, getSourceDocument().getOutputStream());
@@ -124,13 +130,16 @@
             return;
         }
 
-        Document xml = getXml();
-        if (xml != null) {
-            validate(xml);
+        // check document form
+        getXml();
+        
+        if (!hasErrors()) {
+            validate();
         }
+
     }
 
-    protected void validate(Document xml) throws Exception {
+    protected void validate() throws Exception {
         ResourceType resourceType = getSourceDocument().getResourceType();
         Schema schema = resourceType.getSchema();
         if (schema == null) {
@@ -139,7 +148,16 @@
                             + "], skipping validation.");
         }
         else {
-            ValidationUtil.validate(this.manager, xml, schema, new UsecaseErrorHandler(this));
+            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);
         }
     }
 
@@ -147,13 +165,12 @@
         String encoding = getEncoding();
         String xmlString = getXmlString(encoding);
 
-        Document xml = null;
         try {
-            xml = DocumentHelper.readDocument(xmlString, encoding);
+            return DocumentHelper.readDocument(xmlString, encoding);
         } catch (SAXException e) {
             addErrorMessage("error-document-form", new String[] { e.getMessage() });
+            return null;
         }
-        return xml;
     }
 
     /**
@@ -228,5 +245,59 @@
     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;
+        }
+
+        public int getColumn() {
+            return this.column;
+        }
+
+        public String getMessage() {
+            return this.message;
+        }
+
+        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) {
+            errors = new ArrayList();
+            setParameter(PARAM_VALIDATION_ERRORS, errors);
+        }
+        return errors;
+    }
+
+    public void error(SAXParseException e) throws SAXException {
+        getValidationErrors().add(new ValidationError(ValidationError.SEVERITY_ERROR, e));
+    }
+
+    public void fatalError(SAXParseException e) throws SAXException {
+        getValidationErrors().add(new ValidationError(ValidationError.SEVERITY_FATAL, e));
+    }
+
+    public void warning(SAXParseException e) throws SAXException {
+        getValidationErrors().add(new ValidationError(ValidationError.SEVERITY_WARNING, e));
+    }
 
 }

Modified: lenya/trunk/src/modules/editors/resources/i18n/cmsui.xml
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/editors/resources/i18n/cmsui.xml?rev=738839&r1=738838&r2=738839&view=diff
==============================================================================
--- lenya/trunk/src/modules/editors/resources/i18n/cmsui.xml (original)
+++ lenya/trunk/src/modules/editors/resources/i18n/cmsui.xml Thu Jan 29 12:38:17 2009
@@ -52,4 +52,6 @@
   
   <message key="Choose element ...">Choose element ...</message>
   <message key="upload-disabled">Upload is not enabled. Please check local.build.properties!</message>
+  
+  <message key="editors.editSource">With Source Editor</message>
 </catalogue>

Modified: lenya/trunk/src/modules/editors/resources/i18n/cmsui_de.xml
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/editors/resources/i18n/cmsui_de.xml?rev=738839&r1=738838&r2=738839&view=diff
==============================================================================
--- lenya/trunk/src/modules/editors/resources/i18n/cmsui_de.xml (original)
+++ lenya/trunk/src/modules/editors/resources/i18n/cmsui_de.xml Thu Jan 29 12:38:17 2009
@@ -55,4 +55,6 @@
     Das Hochladen von Dateien ist nicht erlaubt.
     Bitte prüfen Sie die Einstellungen in local.build.properties!
   </message>
+  
+  <message key="editors.editSource">Mit Quellcode-Editor</message>
 </catalogue>
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.