cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From reinh...@apache.org
Subject svn commit: r701981 - in /cocoon/cocoon3/trunk: cocoon-docs/src/changes/ cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/ cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/ cocoon-pipeline/src/test/java/org/apache/coc...
Date Mon, 06 Oct 2008 08:04:02 GMT
Author: reinhard
Date: Mon Oct  6 01:04:01 2008
New Revision: 701981

URL: http://svn.apache.org/viewvc?rev=701981&view=rev
Log:
<action dev="reinhard" type="add" issue="COCOON3-2" due-to="Simone Tripodi">[cocoon-pipeline]
Add a SchemaProcessorTransformer that validates the SAX event stream against an XML schema.</action>

Added:
    cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaErrorHandler.java
  (with props)
    cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformer.java
  (with props)
    cocoon/cocoon3/trunk/cocoon-pipeline/src/test/java/org/apache/cocoon/pipeline/component/
    cocoon/cocoon3/trunk/cocoon-pipeline/src/test/java/org/apache/cocoon/pipeline/component/sax/
    cocoon/cocoon3/trunk/cocoon-pipeline/src/test/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformerTest.java
  (with props)
    cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xsd   (with props)
Modified:
    cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml
    cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java
    cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xml
    cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xslt

Modified: cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml?rev=701981&r1=701980&r2=701981&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml (original)
+++ cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml Mon Oct  6 01:04:01 2008
@@ -25,6 +25,9 @@
 <document>
   <body>
     <release version="3.0.0-alpha-2" date="2008-00-00" description="unreleased">
+      <action dev="reinhard" type="add" issue="COCOON3-2" due-to="Simone Tripodi">[cocoon-pipeline]
Add a SchemaProcessorTransformer that validates the SAX event 
+        stream against an XML schema.    
+      </action>
       <action dev="reinhard" type="add" issue="COCOON3-1" due-to="Simone Tripodi">[all]
Set serialVersionUID on all serializeable classes.</action>
       <action dev="reinhard" type="add" issue="COCOON3-4" due-to="Simone Tripodi">[cocoon-optional]
Add a BetwixtBeanGenerator that 
         serializes Java classes following the Java Beans conventions.

Added: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaErrorHandler.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaErrorHandler.java?rev=701981&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaErrorHandler.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaErrorHandler.java
Mon Oct  6 01:04:01 2008
@@ -0,0 +1,91 @@
+/*
+ * 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.cocoon.pipeline.component.sax;
+
+import org.apache.commons.logging.Log;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+public final class SchemaErrorHandler implements ErrorHandler {
+
+    private final Log logger;
+
+    private final String uri;
+
+    private SAXParseException exception;
+
+    private SAXParseException warningEx;
+
+    public SchemaErrorHandler(Log logger, String uri) {
+        this.logger = logger;
+        this.uri = uri;
+    }
+
+    public void error(SAXParseException ex) throws SAXException {
+        if (this.warningEx != null && ex.getCause() == null) {
+            ex = this.warningEx;
+        }
+        this.warningEx = null;
+
+        // Keep the exception for later use.
+        this.exception = ex;
+        // and rethrow it
+        throw ex;
+    }
+
+    public void fatalError(SAXParseException ex) throws SAXException {
+        if (this.warningEx != null && ex.getCause() == null) {
+            ex = this.warningEx;
+        }
+        this.warningEx = null;
+
+        this.exception = ex;
+        throw ex;
+    }
+
+    public void warning(SAXParseException ex) throws SAXException {
+        if (this.logger.isWarnEnabled()) {
+            this.logger.warn(ex.getMessage() + " at " + this.uri);
+        }
+
+        // Keep the warning (see below)
+        this.warningEx = ex;
+    }
+
+    /**
+     * Get the exception that was catched by this listener, if any.
+     * 
+     * @return the exception
+     */
+    public Throwable getThrowable() {
+        if (this.exception == null) {
+            return null;
+        }
+
+        // No location: if it's just a wrapper, consider only the wrapped exception
+        if (this.exception.getCause() != null) {
+            return this.exception.getCause();
+        }
+
+        // That's the actual exception!
+        return this.exception;
+    }
+
+}

Propchange: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaErrorHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaErrorHandler.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaErrorHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformer.java?rev=701981&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformer.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformer.java
Mon Oct  6 01:04:01 2008
@@ -0,0 +1,70 @@
+/*
+ * 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.cocoon.pipeline.component.sax;
+
+import java.net.URL;
+
+import javax.xml.XMLConstants;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+import javax.xml.validation.ValidatorHandler;
+
+import org.apache.cocoon.pipeline.util.StringRepresentation;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.xml.sax.SAXException;
+
+public final class SchemaProcessorTransformer extends AbstractTransformer {
+
+    private final static SchemaFactory SCHEMA_FACTORY = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+
+    private final Log logger = LogFactory.getLog(this.getClass());
+
+    private Schema schema;
+
+    private URL source;
+
+    public SchemaProcessorTransformer(URL source) {
+        if (source == null) {
+            throw new IllegalArgumentException("The parameter 'source' mustn't be null.");
+        }
+
+        try {
+            this.schema = SCHEMA_FACTORY.newSchema(source);
+        } catch (SAXException e) {
+            throw new RuntimeException("Could not initialize xschema source", e);
+        }
+
+        this.source = source;
+    }
+
+    @Override
+    protected void setXMLConsumer(XMLConsumer xmlConsumer) {
+        ValidatorHandler validatorHandler = this.schema.newValidatorHandler();
+        validatorHandler.setErrorHandler(new SchemaErrorHandler(this.logger, this.source.toExternalForm()));
+        validatorHandler.setContentHandler(xmlConsumer);
+
+        super.setXMLConsumer(new XMLConsumerAdapter(validatorHandler, xmlConsumer));
+    }
+
+    @Override
+    public String toString() {
+        return StringRepresentation.buildString(this, "src=" + this.source);
+    }
+}

Propchange: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformer.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java?rev=701981&r1=701980&r2=701981&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java
Mon Oct  6 01:04:01 2008
@@ -45,6 +45,9 @@
             inputStream = new BufferedInputStream(inputStream);
             xmlReader.parse(new InputSource(inputStream));
         } catch (Exception e) {
+            // FIXME We have to do something with exception handling. Throwing
+            // plain RuntimeExceptions makes it difficult to react on different
+            // error conditions in your code.
             throw new RuntimeException("Can't parse inputStream.", e);
         }
     }

Added: cocoon/cocoon3/trunk/cocoon-pipeline/src/test/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformerTest.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-pipeline/src/test/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformerTest.java?rev=701981&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-pipeline/src/test/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformerTest.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-pipeline/src/test/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformerTest.java
Mon Oct  6 01:04:01 2008
@@ -0,0 +1,75 @@
+/*
+ * 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.cocoon.pipeline.component.sax;
+
+import java.io.ByteArrayOutputStream;
+
+import junit.framework.TestCase;
+
+import org.apache.cocoon.pipeline.NonCachingPipeline;
+import org.apache.cocoon.pipeline.Pipeline;
+import org.custommonkey.xmlunit.Diff;
+
+public class SchemaProcessorTransformerTest extends TestCase {
+
+    /**
+     * A pipeline that performs an identity transformation, using the validation: generator
-&gt; validator -&gt;
+     * serializer
+     */
+    public void testPipelineWithValidation() throws Exception {
+        Pipeline pipeline = this.createValidatingPipeline("<x></x>");
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        pipeline.setup(baos);
+        pipeline.execute();
+
+        String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><x></x>";
+        String actual = new String(baos.toByteArray());
+
+        Diff diff = new Diff(expected, actual);
+        assertTrue("XSL transformation didn't work as expected " + diff, diff.identical());
+    }
+
+    /**
+     * A pipeline that performs an identity transformation, using the validation: generator
-&gt; validator -&gt;
+     * serializer. An error is expected performing <code>execute</code> method
due to not valid xml input.
+     */
+    public void testPipelineWithWrongValidation() {
+        Pipeline pipeline = this.createValidatingPipeline("<y><z/></y>");
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        pipeline.setup(baos);
+        try {
+            pipeline.execute();
+            fail("A validation error is expected");
+        } catch (Exception e) {
+            e.printStackTrace();
+            // success
+        }
+    }
+
+    private Pipeline createValidatingPipeline(String xmlInput) {
+        Pipeline pipeline = new NonCachingPipeline();
+        pipeline.addComponent(new StringGenerator(xmlInput));
+        pipeline.addComponent(new SchemaProcessorTransformer(this.getClass().getResource("/test.xsd")));
+        pipeline.addComponent(new XMLSerializer());
+
+        return pipeline;
+    }
+}

Propchange: cocoon/cocoon3/trunk/cocoon-pipeline/src/test/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-pipeline/src/test/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformerTest.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/cocoon3/trunk/cocoon-pipeline/src/test/java/org/apache/cocoon/pipeline/component/sax/SchemaProcessorTransformerTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xml?rev=701981&r1=701980&r2=701981&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xml (original)
+++ cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xml Mon Oct  6 01:04:01 2008
@@ -15,4 +15,5 @@
   See the License for the specific language governing permissions and
   limitations under the License.
 -->
+<!-- $Id$ -->
 <test/>
\ No newline at end of file

Added: cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xsd
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xsd?rev=701981&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xsd (added)
+++ cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xsd Mon Oct  6 01:04:01 2008
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<!-- $Id$ -->
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+    <xs:element name="x"/>
+</xs:schema>

Propchange: cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xsd
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xsd
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xsd
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Modified: cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xslt
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xslt?rev=701981&r1=701980&r2=701981&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xslt (original)
+++ cocoon/cocoon3/trunk/cocoon-pipeline/src/test/resources/test.xslt Mon Oct  6 01:04:01
2008
@@ -15,6 +15,7 @@
   See the License for the specific language governing permissions and
   limitations under the License.
 -->
+<!-- $Id$ -->
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 
   <xsl:param name="myParam" />



Mime
View raw message