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 2005/12/02 12:00:09 UTC

svn commit: r351677 - in /lenya/trunk/src: modules/jcr/java/src/org/apache/lenya/cms/jcr/ modules/jcr/java/test/org/apache/lenya/cms/jcr/ targets/

Author: andreas
Date: Fri Dec  2 02:59:55 2005
New Revision: 351677

URL: http://svn.apache.org/viewcvs?rev=351677&view=rev
Log:
Repo layer: implemented document content validation

Added:
    lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/invalid.xml
    lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/schema.xml
    lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/valid.xml
Modified:
    lenya/trunk/src/modules/jcr/java/src/org/apache/lenya/cms/jcr/ResourceProxy.java
    lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/JCRRepositoryTest.java
    lenya/trunk/src/targets/webapp-build.xml

Modified: lenya/trunk/src/modules/jcr/java/src/org/apache/lenya/cms/jcr/ResourceProxy.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/modules/jcr/java/src/org/apache/lenya/cms/jcr/ResourceProxy.java?rev=351677&r1=351676&r2=351677&view=diff
==============================================================================
--- lenya/trunk/src/modules/jcr/java/src/org/apache/lenya/cms/jcr/ResourceProxy.java (original)
+++ lenya/trunk/src/modules/jcr/java/src/org/apache/lenya/cms/jcr/ResourceProxy.java Fri Dec  2 02:59:55 2005
@@ -16,16 +16,30 @@
  */
 package org.apache.lenya.cms.jcr;
 
+import java.io.BufferedWriter;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.util.GregorianCalendar;
 
 import org.apache.lenya.cms.jcr.mapping.AbstractNodeProxy;
 import org.apache.lenya.cms.jcr.mapping.PathElement;
+import org.apache.lenya.cms.repo.Document;
+import org.apache.lenya.cms.repo.DocumentType;
 import org.apache.lenya.cms.repo.RepositoryException;
+import org.apache.lenya.xml.Schema;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+import com.thaiopensource.util.PropertyMapBuilder;
+import com.thaiopensource.validate.SchemaReader;
+import com.thaiopensource.validate.ValidateProperty;
+import com.thaiopensource.validate.ValidationDriver;
+import com.thaiopensource.validate.auto.AutoSchemaReader;
+import com.thaiopensource.xml.sax.ErrorHandlerImpl;
 
 /**
  * Resource node proxy.
@@ -38,6 +52,10 @@
     protected static final String LAST_MODIFIED_PROPERTY = "jcr:lastModified";
     protected static final String MIME_TYPE_PROPERTY = "jcr:mimeType";
 
+    protected DocumentProxy getDocumentProxy() throws RepositoryException {
+        return (DocumentProxy) getParentProxy();
+    }
+
     /**
      * @return The input stream.
      * @throws RepositoryException if an error occurs.
@@ -73,7 +91,7 @@
     public long getLastModified() throws RepositoryException {
         return getPropertyLong(LAST_MODIFIED_PROPERTY);
     }
-    
+
     /**
      * Initializes the node after it has been created.
      * @throws RepositoryException if an error occurs.
@@ -109,13 +127,61 @@
                 try {
                     byte[] bytes = this.toByteArray();
                     InputStream stream = new ByteArrayInputStream(bytes);
-                    this.proxy.setProperty(ResourceProxy.DATA_PROPERTY, stream);
-                    this.proxy.setProperty(ResourceProxy.LAST_MODIFIED_PROPERTY,
-                            new GregorianCalendar());
+
+                    boolean valid = validate(stream);
+                    if (valid) {
+                        stream = new ByteArrayInputStream(bytes);
+                        this.proxy.setProperty(ResourceProxy.DATA_PROPERTY, stream);
+                        this.proxy.setProperty(ResourceProxy.LAST_MODIFIED_PROPERTY,
+                                new GregorianCalendar());
+                        if (this.proxy.getContentLength() != bytes.length) {
+                            throw new IOException("The content length is not correct: ["
+                                    + this.proxy.getContentLength() + " != " + bytes.length + "]");
+                        }
+                    } else {
+                        throw new IOException("The content to write is not valid!");
+                    }
                 } catch (RepositoryException e) {
                     throw new RuntimeException(e);
                 }
             }
+        }
+
+        protected boolean validate(InputStream stream) throws RepositoryException, IOException {
+            Document doc = this.proxy.getDocumentProxy();
+            DocumentType doctype = doc.getContentNode().getDocumentType();
+            if (doctype.isValidating()) {
+                Schema schema = doctype.getSchema();
+                if (schema == null) {
+                    throw new IOException("The doctype [" + doctype.getName()
+                            + "] is validating but does not provide a schema!");
+                }
+
+                try {
+                    InputSource in = ValidationDriver.uriOrFileInputSource(schema.getURI());
+                    PropertyMapBuilder properties = new PropertyMapBuilder();
+                    ByteArrayOutputStream error = new ByteArrayOutputStream();
+                    ErrorHandlerImpl eh = new ErrorHandlerImpl(new BufferedWriter(new OutputStreamWriter(error)));
+                    ValidateProperty.ERROR_HANDLER.put(properties, eh);
+                    SchemaReader schemaReader = new AutoSchemaReader();
+                    ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(),
+                            schemaReader);
+                    if (driver.loadSchema(in)) {
+                        if (driver.validate(new InputSource(stream))) {
+                            System.out.println("" + error);
+                            return true;
+                        } else {
+                            return false;
+                        }
+                    } else {
+                        throw new RepositoryException("Could not load schema!\n" + error);
+                    }
+                } catch (SAXException e) {
+                    throw new RepositoryException(e);
+                }
+            }
+
+            return true;
         }
 
     }

Modified: lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/JCRRepositoryTest.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/JCRRepositoryTest.java?rev=351677&r1=351676&r2=351677&view=diff
==============================================================================
--- lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/JCRRepositoryTest.java (original)
+++ lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/JCRRepositoryTest.java Fri Dec  2 02:59:55 2005
@@ -16,10 +16,10 @@
  */
 package org.apache.lenya.cms.jcr;
 
-import java.io.File;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
+import java.net.URL;
 
 import javax.jcr.ItemExistsException;
 
@@ -45,7 +45,6 @@
 import org.apache.lenya.cms.repo.metadata.MetaData;
 import org.apache.lenya.cms.repo.metadata.impl.MetaDataRegistryImpl;
 import org.apache.lenya.xml.DocumentHelper;
-import org.apache.lenya.xml.NamespaceHelper;
 import org.apache.lenya.xml.Schema;
 
 /**
@@ -105,10 +104,8 @@
         this.repo = RepositoryManager.getRepository(getWebappDirectory(), getRepositoryFactory());
         Session session = repo.createSession();
 
-        File webappDir = new File(getWebappDirectory());
-        String path = "lenya/modules/xhtml/schemas/xhtml.rng".replace('/', File.separatorChar);
-        File schemaFile = new File(webappDir, path);
-        Schema schema = new Schema(Validator.GRAMMAR_RELAX_NG, schemaFile.toURI().toString());
+        URL schemaUrl = getClass().getResource("schema.xml");
+        Schema schema = new Schema(Validator.GRAMMAR_RELAX_NG, schemaUrl.toString());
         DocumentType doctype = new DocumentTypeImpl("xhtml", schema, true, "application/xhtml+xml");
         DocumentTypeRegistryImpl registry = (DocumentTypeRegistryImpl) repo.getDocumentTypeRegistry();
         registry.register(doctype);
@@ -139,8 +136,7 @@
     protected void doTestSite(Site site, ContentNode contentNode) throws RepositoryException {
         SiteNode foo = site.addChild("foo", contentNode);
         SiteNode bar = site.addChild("bar", contentNode);
-        
-        
+
         RepositoryException ex = null;
         try {
             site.move(foo.getPath(), bar.getPath());
@@ -149,7 +145,7 @@
         }
         assertTrue(ex.getCause() instanceof ItemExistsException);
         site.move("/foo", "/bar/baz");
-        
+
         SiteNode barBaz = bar.getChild("baz");
         assertSame(foo.getContentNode().getNodeId(), barBaz.getContentNode().getNodeId());
 
@@ -158,13 +154,35 @@
     protected void doTestDocument(ContentNode node1) throws Exception {
         Document doc = node1.addDocument("de", "hello");
 
+        String validXmlResource = "valid.xml";
+        String invalidXmlResource = "invalid.xml";
         String localName = "test";
-        NamespaceHelper helper = new NamespaceHelper("http://foo.bar", "", localName);
+
+        org.w3c.dom.Document validXml = DocumentHelper.readDocument(getClass().getResourceAsStream(validXmlResource));
+        org.w3c.dom.Document invalidXml = DocumentHelper.readDocument(getClass().getResourceAsStream(invalidXmlResource));
+
         OutputStream out = doc.getOutputStream();
         Writer writer = new OutputStreamWriter(out);
 
         try {
-            DocumentHelper.writeDocument(helper.getDocument(), writer);
+            DocumentHelper.writeDocument(invalidXml, writer);
+        } finally {
+            if (out != null) {
+                Exception ex = null;
+                try {
+                    out.close();
+                } catch (Exception e) {
+                    ex = e;
+                }
+                assertNotNull(ex);
+            }
+        }
+
+        out = doc.getOutputStream();
+        writer = new OutputStreamWriter(out);
+
+        try {
+            DocumentHelper.writeDocument(validXml, writer);
         } finally {
             if (out != null) {
                 out.close();

Added: lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/invalid.xml
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/invalid.xml?rev=351677&view=auto
==============================================================================
--- lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/invalid.xml (added)
+++ lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/invalid.xml Fri Dec  2 02:59:55 2005
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<test xmlns="http://foo/bar"/>
\ No newline at end of file

Added: lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/schema.xml
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/schema.xml?rev=351677&view=auto
==============================================================================
--- lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/schema.xml (added)
+++ lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/schema.xml Fri Dec  2 02:59:55 2005
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<grammar 
+  xmlns="http://relaxng.org/ns/structure/1.0"
+  xmlns:xsp="http://apache.org/xsp/core/v1"
+  xmlns:s="http://www.ascc.net/xml/schematron"
+  xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
+  xmlns:f="http://axkit.org/NS/xsp/perform/v1"
+  ns="http://apache.org/lenya/jcr/test"
+  datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
+  <start>
+    <element name="test">
+      <empty/>
+    </element>
+  </start>
+</grammar>
\ No newline at end of file

Added: lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/valid.xml
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/valid.xml?rev=351677&view=auto
==============================================================================
--- lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/valid.xml (added)
+++ lenya/trunk/src/modules/jcr/java/test/org/apache/lenya/cms/jcr/valid.xml Fri Dec  2 02:59:55 2005
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<test xmlns="http://apache.org/lenya/jcr/test"/>
\ No newline at end of file

Modified: lenya/trunk/src/targets/webapp-build.xml
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/targets/webapp-build.xml?rev=351677&r1=351676&r2=351677&view=diff
==============================================================================
--- lenya/trunk/src/targets/webapp-build.xml (original)
+++ lenya/trunk/src/targets/webapp-build.xml Fri Dec  2 02:59:55 2005
@@ -75,6 +75,7 @@
         <include name="**/*.properties"/>
         <include name="**/*.roles"/>
         <include name="**/*.xml"/>
+        <include name="**/*.rng"/>
       </fileset>
       <fileset dir="${java.dir}">
         <include name="**/*.roles"/>



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