Return-Path: Delivered-To: apmail-cocoon-cvs-archive@www.apache.org Received: (qmail 10469 invoked from network); 6 Oct 2008 08:04:23 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 6 Oct 2008 08:04:23 -0000 Received: (qmail 40616 invoked by uid 500); 6 Oct 2008 08:04:22 -0000 Delivered-To: apmail-cocoon-cvs-archive@cocoon.apache.org Received: (qmail 40569 invoked by uid 500); 6 Oct 2008 08:04:22 -0000 Mailing-List: contact cvs-help@cocoon.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@cocoon.apache.org list-help: list-unsubscribe: List-Post: List-Id: Delivered-To: mailing list cvs@cocoon.apache.org Received: (qmail 40560 invoked by uid 99); 6 Oct 2008 08:04:22 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 06 Oct 2008 01:04:22 -0700 X-ASF-Spam-Status: No, hits=-1999.9 required=10.0 tests=ALL_TRUSTED,DNS_FROM_SECURITYSAGE X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 06 Oct 2008 08:03:27 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id C76BF2388879; Mon, 6 Oct 2008 01:04:02 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: cvs@cocoon.apache.org From: reinhard@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081006080402.C76BF2388879@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: reinhard Date: Mon Oct 6 01:04:01 2008 New Revision: 701981 URL: http://svn.apache.org/viewvc?rev=701981&view=rev Log: [cocoon-pipeline] Add a SchemaProcessorTransformer that validates the SAX event stream against an XML schema. 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 @@ + [cocoon-pipeline] Add a SchemaProcessorTransformer that validates the SAX event + stream against an XML schema. + [all] Set serialVersionUID on all serializeable classes. [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 -> validator -> + * serializer + */ + public void testPipelineWithValidation() throws Exception { + Pipeline pipeline = this.createValidatingPipeline(""); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + pipeline.setup(baos); + pipeline.execute(); + + String expected = ""; + 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 -> validator -> + * serializer. An error is expected performing execute method due to not valid xml input. + */ + public void testPipelineWithWrongValidation() { + Pipeline pipeline = this.createValidatingPipeline(""); + + 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. --> + \ 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 @@ + + + + + + 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. --> +