You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by pi...@apache.org on 2005/09/12 22:34:09 UTC

svn commit: r280411 - in /cocoon/branches/BRANCH_2_1_X/src/blocks/validation: java/org/apache/cocoon/components/validation/jaxp/ samples/

Author: pier
Date: Mon Sep 12 13:34:01 2005
New Revision: 280411

URL: http://svn.apache.org/viewcvs?rev=280411&view=rev
Log:
Implementation of Validator using the Jaxp validation API shipped with Cocoon (for now, XML-Schema only based on Xerces)

Added:
    cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpInput.java
    cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpResolver.java
    cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpSchema.java
    cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/schema-no.xsd
    cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/schema-ok.xsd
Modified:
    cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpSchemaParser.java

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpInput.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpInput.java?rev=280411&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpInput.java (added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpInput.java Mon Sep 12 13:34:01 2005
@@ -0,0 +1,102 @@
+/* ========================================================================== *
+ * Copyright (C) 2004-2005 Pier Fumagalli <http://www.betaversion.org/~pier/> *
+ *                            All rights reserved.                            *
+ * ========================================================================== *
+ *                                                                            *
+ * Licensed 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.components.validation.jaxp;
+
+import java.io.InputStream;
+import java.io.Reader;
+
+import org.w3c.dom.ls.LSInput;
+import org.xml.sax.InputSource;
+
+final class JaxpInput implements LSInput {
+    
+    private final InputSource input;
+    private boolean cert = false;
+    private String data = null;
+    private String base = null;
+
+    public JaxpInput(InputSource input) {
+        if (input == null) throw new NullPointerException("Null InputSource");
+        this.input = input;
+    }
+
+    public Reader getCharacterStream() {
+        return this.input.getCharacterStream();
+    }
+
+    public void setCharacterStream(Reader reader) {
+        this.input.setCharacterStream(reader);
+    }
+
+    public InputStream getByteStream() {
+        return this.input.getByteStream();
+    }
+
+    public void setByteStream(InputStream stream) {
+        this.input.setByteStream(stream);
+    }
+
+    public String getStringData() {
+        return this.data; 
+    }
+
+    public void setStringData(String data) {
+        this.data = data;
+    }
+
+    public String getSystemId() {
+        return this.input.getSystemId();
+    }
+
+    public void setSystemId(String systemId) {
+        this.input.setSystemId(systemId);
+    }
+
+    public String getPublicId() {
+        return this.input.getPublicId();
+    }
+
+    public void setPublicId(String publicId) {
+        this.input.setPublicId(publicId);
+    }
+
+    public String getBaseURI() {
+        if (this.base != null) return this.base;
+        return this.input.getSystemId();
+    }
+
+    public void setBaseURI(String base) {
+        this.base = base;
+    }
+
+    public String getEncoding() {
+        return this.input.getEncoding();
+    }
+
+    public void setEncoding(String encoding) {
+        this.input.setEncoding(encoding);
+    }
+
+    public boolean getCertifiedText() {
+        return this.cert;
+    }
+
+    public void setCertifiedText(boolean cert) {
+        this.cert = cert;
+    }
+}
\ No newline at end of file

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpResolver.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpResolver.java?rev=280411&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpResolver.java (added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpResolver.java Mon Sep 12 13:34:01 2005
@@ -0,0 +1,49 @@
+/* ========================================================================== *
+ * Copyright (C) 2004-2005 Pier Fumagalli <http://www.betaversion.org/~pier/> *
+ *                            All rights reserved.                            *
+ * ========================================================================== *
+ *                                                                            *
+ * Licensed 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.components.validation.jaxp;
+
+
+import org.apache.cocoon.components.validation.impl.ValidationResolver;
+import org.apache.excalibur.source.SourceResolver;
+import org.w3c.dom.DOMError;
+import org.w3c.dom.ls.LSException;
+import org.w3c.dom.ls.LSInput;
+import org.w3c.dom.ls.LSResourceResolver;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+
+public class JaxpResolver extends ValidationResolver
+implements LSResourceResolver {
+    
+    public JaxpResolver(SourceResolver sourceResolver,
+                        EntityResolver entityResolver) {
+        super(sourceResolver, entityResolver);
+    }
+
+    public LSInput resolveResource(String type, String namespace, String systemId,
+                                   String publicId, String base) {
+        try {
+            final InputSource source = this.resolveEntity(base, publicId, systemId);
+            return new JaxpInput(source);
+        } catch (Exception exception) {
+            String message = "Exception resolving resource " + systemId;
+            Throwable err = new LSException(DOMError.SEVERITY_FATAL_ERROR, message);
+            throw (LSException) err.initCause(exception);
+        }
+    }
+}

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpSchema.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpSchema.java?rev=280411&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpSchema.java (added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpSchema.java Mon Sep 12 13:34:01 2005
@@ -0,0 +1,51 @@
+/* ========================================================================== *
+ * Copyright (C) 2004-2005 Pier Fumagalli <http://www.betaversion.org/~pier/> *
+ *                            All rights reserved.                            *
+ * ========================================================================== *
+ *                                                                            *
+ * Licensed 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.components.validation.jaxp;
+
+import javax.xml.validation.Schema;
+import javax.xml.validation.ValidatorHandler;
+
+import org.apache.cocoon.components.validation.ValidationHandler;
+import org.apache.cocoon.components.validation.impl.AbstractSchema;
+import org.apache.cocoon.components.validation.impl.DefaultValidationHandler;
+import org.apache.excalibur.source.SourceValidity;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+
+/**
+ * <p>TODO: ...</p>
+ *
+ * @author <a href="mailto:pier@betaversion.org">Pier Fumagalli</a>
+ */
+public class JaxpSchema extends AbstractSchema {
+    
+    private final Schema schema;
+    
+    public JaxpSchema(Schema schema, SourceValidity validity) {
+        super(validity);
+        this.schema = schema;
+    }
+
+    public ValidationHandler createValidator(ErrorHandler handler)
+    throws NullPointerException, SAXException {
+        ValidatorHandler validator = this.schema.newValidatorHandler();
+        validator.setErrorHandler(handler);
+        return new DefaultValidationHandler(this.getValidity(), validator);
+    }
+
+}

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpSchemaParser.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpSchemaParser.java?rev=280411&r1=280410&r2=280411&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpSchemaParser.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/jaxp/JaxpSchemaParser.java Mon Sep 12 13:34:01 2005
@@ -22,6 +22,7 @@
 import java.util.Set;
 
 import javax.xml.XMLConstants;
+import javax.xml.transform.sax.SAXSource;
 import javax.xml.validation.SchemaFactory;
 
 import org.apache.avalon.framework.configuration.Configurable;
@@ -31,6 +32,7 @@
 import org.apache.cocoon.components.validation.Schema;
 import org.apache.cocoon.components.validation.Validator;
 import org.apache.cocoon.components.validation.impl.AbstractSchemaParser;
+import org.apache.cocoon.components.validation.impl.DraconianErrorHandler;
 import org.apache.excalibur.source.Source;
 import org.xml.sax.SAXException;
 
@@ -52,7 +54,7 @@
         try {
             fact = (SchemaFactory) Class.forName(this.className).newInstance();
         } catch (Exception exception) {
-            String message = "Unable to instantiate factory " + className;
+            String message = "Unable to instantiate factory " + this.className;
             throw new ConfigurationException(message, conf, exception);
         }
 
@@ -63,9 +65,16 @@
 
             /* If the configuration specified (formally) a list of grammars use it */
             for (int x = 0; x < languages.length; x++) {
-                grammars.add(languages[x].getValue());
+                String language = languages[x].getValue();
+                if (fact.isSchemaLanguageSupported(language)) {
+                    grammars.add(language);
+                    continue;
+                }
+                /* If the configured language is not supported throw an exception */
+                String message = "JAXP SchemaFactory \"" + this.className + "\" " +
+                                 "does not support configured grammar " + language;
+                throw new ConfigurationException(message, languages[x]);
             }
-
         } else {
 
             /* Attempt to detect the languages directly using the JAXP factory */
@@ -86,7 +95,20 @@
 
     public Schema parseSchema(Source source, String grammar)
     throws SAXException, IOException {
-        throw new UnsupportedOperationException();
+        final SchemaFactory factory;
+        try {
+            factory = (SchemaFactory) Class.forName(this.className).newInstance();
+        } catch (Exception exception) {
+            String message = "Unable to instantiate factory " + this.className;
+            throw new SAXException(message, exception);
+        }
+        
+        JaxpResolver r = new JaxpResolver(this.sourceResolver, this.entityResolver);
+        SAXSource s = new SAXSource(r.resolveSource(source));
+        factory.setErrorHandler(DraconianErrorHandler.INSTANCE);
+        factory.setResourceResolver(r);
+        
+        return new JaxpSchema(factory.newSchema(s), r.close());
     }
 
     public String[] getSupportedGrammars() {

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/schema-no.xsd
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/schema-no.xsd?rev=280411&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/schema-no.xsd (added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/schema-no.xsd Mon Sep 12 13:34:01 2005
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+  elementFormDefault="qualified"
+  targetNamespace="http://test/namespace/1.0"
+  xmlns:ns1="http://test/namespace/1.0">
+  
+  <xs:element name="page">
+    <xs:this-invalidates-the-schema/>
+  </xs:element>
+</xs:schema>

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/schema-ok.xsd
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/schema-ok.xsd?rev=280411&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/schema-ok.xsd (added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/samples/schema-ok.xsd Mon Sep 12 13:34:01 2005
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+  elementFormDefault="qualified"
+  targetNamespace="http://test/namespace/1.0"
+  xmlns:ns1="http://test/namespace/1.0">
+  
+  <xs:element name="page">
+    <xs:complexType/>
+  </xs:element>
+</xs:schema>