From b7683a57f9ebc2a5ff5789a2cbcff5269062e9df Mon Sep 17 00:00:00 2001 From: Ralf Battenfeld Date: Sun, 4 Nov 2012 15:27:21 +0100 Subject: [PATCH 1/8] [SHRINKDESC-130] Provide a way to validate an imported or created descriptor content --- gen/.gitignore | 1 - gen/pom.xml | 2 +- .../descriptor/gen/ResourcerReader.java | 62 + gen/src/main/resources/lib/xdd-printBase.xsl | 714 ----- .../main/resources/lib/xdd-printElement.xsl | 2016 ------------ gen/src/main/resources/lib/xdd-util.xsl | 313 -- gen/src/main/resources/xslt/contributors.xml | 5 - gen/src/main/resources/xslt/copyright.xml | 19 - gen/src/main/resources/xslt/ddJava.xsl | 1049 ------- .../xslt/experiemental/ddJava-ibm.xsl | 2750 ----------------- .../xslt/experiemental/metadata-ibm.xsl | 1078 ------- .../xslt/experiemental/metadataIBMEE5.xml | 740 ----- .../xslt/experiemental/schemasIBMEE5.xml | 30 - gen/src/main/resources/xslt/metadata.xsl | 1412 --------- gen/src/main/resources/xslt/schemasJ2ee13.xml | 17 - gen/src/main/resources/xslt/schemasJ2ee14.xml | 26 - .../main/resources/xslt/schemasJBossEE5.xml | 37 - .../main/resources/xslt/schemasJBossEE6.xml | 31 - .../main/resources/xslt/schemasJavaEE5.xml | 42 - .../main/resources/xslt/schemasJavaEE6.xml | 83 - pom.xml | 1 + schema-validator/.gitignore | 8 + schema-validator/pom.xml | 119 + schema-validator/pom.xml~ | 159 + .../schemavalidator/XmlValidator.java | 396 +++ .../XmlValidatorConstants.java | 52 + .../src/main/resources/META-INF/2001/xml.xsd | 287 ++ .../src/main/resources/META-INF/2004/xml.xsd | 135 + .../src/main/resources/META-INF/2007/xml.xsd | 145 + .../src/main/resources/META-INF/2009/xml.xsd | 286 ++ .../ValidatorEvaluationTest.java | 514 +++ .../schemavalidator/XmlValidatorTestCase.java | 77 + .../resources/test-invalid-application-6.xml | 19 + .../resources/test-valid-application-6.xml | 18 + 34 files changed, 2279 insertions(+), 10364 deletions(-) create mode 100644 gen/src/main/java/org/jboss/shrinkwrap/descriptor/gen/ResourcerReader.java delete mode 100644 gen/src/main/resources/lib/xdd-printBase.xsl delete mode 100644 gen/src/main/resources/lib/xdd-printElement.xsl delete mode 100644 gen/src/main/resources/lib/xdd-util.xsl delete mode 100644 gen/src/main/resources/xslt/contributors.xml delete mode 100644 gen/src/main/resources/xslt/copyright.xml delete mode 100644 gen/src/main/resources/xslt/ddJava.xsl delete mode 100644 gen/src/main/resources/xslt/experiemental/ddJava-ibm.xsl delete mode 100644 gen/src/main/resources/xslt/experiemental/metadata-ibm.xsl delete mode 100644 gen/src/main/resources/xslt/experiemental/metadataIBMEE5.xml delete mode 100644 gen/src/main/resources/xslt/experiemental/schemasIBMEE5.xml delete mode 100644 gen/src/main/resources/xslt/metadata.xsl delete mode 100644 gen/src/main/resources/xslt/schemasJ2ee13.xml delete mode 100644 gen/src/main/resources/xslt/schemasJ2ee14.xml delete mode 100644 gen/src/main/resources/xslt/schemasJBossEE5.xml delete mode 100644 gen/src/main/resources/xslt/schemasJBossEE6.xml delete mode 100644 gen/src/main/resources/xslt/schemasJavaEE5.xml delete mode 100644 gen/src/main/resources/xslt/schemasJavaEE6.xml create mode 100644 schema-validator/.gitignore create mode 100644 schema-validator/pom.xml create mode 100644 schema-validator/pom.xml~ create mode 100644 schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java create mode 100644 schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorConstants.java create mode 100644 schema-validator/src/main/resources/META-INF/2001/xml.xsd create mode 100644 schema-validator/src/main/resources/META-INF/2004/xml.xsd create mode 100644 schema-validator/src/main/resources/META-INF/2007/xml.xsd create mode 100644 schema-validator/src/main/resources/META-INF/2009/xml.xsd create mode 100644 schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/ValidatorEvaluationTest.java create mode 100644 schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorTestCase.java create mode 100644 schema-validator/src/test/resources/test-invalid-application-6.xml create mode 100644 schema-validator/src/test/resources/test-valid-application-6.xml diff --git a/gen/.gitignore b/gen/.gitignore index 32c77dbc..07b10305 100644 --- a/gen/.gitignore +++ b/gen/.gitignore @@ -1,4 +1,3 @@ -src/main/java src/test/java src/main/resources/xslt/metadata*.xml >>>>>>> a98e8f7... [SHRINKDESC-62] Splitting api-javaee api-jboss, adding static tests for coverage diff --git a/gen/pom.xml b/gen/pom.xml index 4f83efee..2a108a04 100644 --- a/gen/pom.xml +++ b/gen/pom.xml @@ -682,7 +682,7 @@ executionJBossEE6 validate - true + false @author diff --git a/gen/src/main/java/org/jboss/shrinkwrap/descriptor/gen/ResourcerReader.java b/gen/src/main/java/org/jboss/shrinkwrap/descriptor/gen/ResourcerReader.java new file mode 100644 index 00000000..500ebc9b --- /dev/null +++ b/gen/src/main/java/org/jboss/shrinkwrap/descriptor/gen/ResourcerReader.java @@ -0,0 +1,62 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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.jboss.shrinkwrap.descriptor.gen; + +import java.io.InputStream; + + +/** + * This class is able to returns input streams. + * + * @author Ralf Battenfeld + */ +public class ResourcerReader { + + + /** + * Returns a resource as input stream. + * @param resourceName + * @return if found, an input stream, otherwise null. + */ + public static InputStream getResourceAsStream(final String resourceName) { + if (resourceName == null) { + throw new IllegalArgumentException("Resource name cannot be null!"); + } + + return findResourceStream("xsd/" + resourceName); + } + + //-----------------------------------------------------------------------|| + //-- Private Methods ----------------------------------------------------|| + //-----------------------------------------------------------------------|| + + private static InputStream findResourceStream(final String ... resourceNames) { + InputStream inputStream = null; + for (String resource: resourceNames) { + inputStream = Thread.currentThread() + .getContextClassLoader() + .getResourceAsStream(resource); + + if (inputStream != null) { + break; + } + } + + return inputStream; + } +} diff --git a/gen/src/main/resources/lib/xdd-printBase.xsl b/gen/src/main/resources/lib/xdd-printBase.xsl deleted file mode 100644 index 18eab389..00000000 --- a/gen/src/main/resources/lib/xdd-printBase.xsl +++ /dev/null @@ -1,714 +0,0 @@ - - - - - - - - - - - package ; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Constructor - // -------------------------------------------------------------------------------------|| - - - - { - - - - - } - - - { - super(descriptorName); - - addDefaultNamespaces(); - } - - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Constructor - // -------------------------------------------------------------------------------------|| - - - { - this.t = t; - - - } - - - - { - this.t = t; - - - } - - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Methods - // -------------------------------------------------------------------------------------|| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public Node getRootNode() - { - - } - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Namespace - // -------------------------------------------------------------------------------------|| - - - - - - - - { - - - - - } - - - - - - - { - - - } - - - - - - public List<String> getNamespaces() - { - - - - - - - - - - - - } - - - - - - - { - - - - - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public - - - - - - - - - - - - - - - - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Instance Members - // -------------------------------------------------------------------------------------|| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/gen/src/main/resources/lib/xdd-printElement.xsl b/gen/src/main/resources/lib/xdd-printElement.xsl deleted file mode 100644 index 6c90206b..00000000 --- a/gen/src/main/resources/lib/xdd-printElement.xsl +++ /dev/null @@ -1,2016 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - package ; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Constructor - // -------------------------------------------------------------------------------------|| - - - - { - - - - - } - - - { - super(descriptorName); - - addDefaultNamespaces(); - } - - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Constructor - // -------------------------------------------------------------------------------------|| - - - { - this.t = t; - - - } - - - - { - this.t = t; - - - } - - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Methods - // -------------------------------------------------------------------------------------|| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public Node getRootNode() - { - - } - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Namespace - // -------------------------------------------------------------------------------------|| - - - - - - - - { - - - - - } - - - - - - - { - - - } - - - - - - public List<String> getNamespaces() - { - - - - - - - - - - - - } - - - - - - - { - - - - - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public - - - - - - - - - - - - - - - - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Instance Members - // -------------------------------------------------------------------------------------|| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/gen/src/main/resources/lib/xdd-util.xsl b/gen/src/main/resources/lib/xdd-util.xsl deleted file mode 100644 index 97d41efa..00000000 --- a/gen/src/main/resources/lib/xdd-util.xsl +++ /dev/null @@ -1,313 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/gen/src/main/resources/xslt/contributors.xml b/gen/src/main/resources/xslt/contributors.xml deleted file mode 100644 index 79b0003e..00000000 --- a/gen/src/main/resources/xslt/contributors.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/gen/src/main/resources/xslt/copyright.xml b/gen/src/main/resources/xslt/copyright.xml deleted file mode 100644 index a29b4ea5..00000000 --- a/gen/src/main/resources/xslt/copyright.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - /* - * JBoss, Home of Professional Open Source - * Copyright 2011, Red Hat Middleware LLC, and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * 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. - */ - diff --git a/gen/src/main/resources/xslt/ddJava.xsl b/gen/src/main/resources/xslt/ddJava.xsl deleted file mode 100644 index a42beccc..00000000 --- a/gen/src/main/resources/xslt/ddJava.xsl +++ /dev/null @@ -1,1049 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - { - - - _ - - (" - - ") - - , - - - - ; - - - private String value; - - - - - (String value) - { this.value = value; } - - - public String toString() {return value;} - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - : : - - - - - - - - - - - - <T> - extends Child<T> - - { - - - - - - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - import org.jboss.shrinkwrap.descriptor.api.Descriptor; - import org.jboss.shrinkwrap.descriptor.api.DescriptorNamespace; - - - - { - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - import org.jboss.shrinkwrap.descriptor.spi.node.Node; - - - - - - - - - <T> - implements Child<T>, - - - { - - - - - - - - - - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - import org.jboss.shrinkwrap.descriptor.api.DescriptorNamespace; - import org.jboss.shrinkwrap.descriptor.spi.node.NodeDescriptorImplBase; - import org.jboss.shrinkwrap.descriptor.impl.base.XMLDate; - import org.jboss.shrinkwrap.descriptor.spi.node.Node; - - - - - - { - - - - - - - - - - - - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - import org.jboss.shrinkwrap.descriptor.spi.node.Node; - import org.jboss.shrinkwrap.descriptor.gen.TestDescriptorImpl; - import org.jboss.shrinkwrap.descriptor.api.Descriptors; - import org.junit.Test; - import static org.junit.Assert.*; - - - - - - - - - - - - - - - - - - - { - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/gen/src/main/resources/xslt/experiemental/ddJava-ibm.xsl b/gen/src/main/resources/xslt/experiemental/ddJava-ibm.xsl deleted file mode 100644 index b2eb3785..00000000 --- a/gen/src/main/resources/xslt/experiemental/ddJava-ibm.xsl +++ /dev/null @@ -1,2750 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - { - - - _ - - (" - - ") - - , - - - - ; - - - private String value; - - - - - (String value) - { this.value = value; } - - - public String toString() {return value;} - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - : : - - - - - - - - - - - - <T> - extends Child<T> - - { - - - - - - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - import org.jboss.shrinkwrap.descriptor.api.Descriptor; - import org.jboss.shrinkwrap.descriptor.api.DescriptorNamespace; - - - - - - { - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - import org.jboss.shrinkwrap.descriptor.spi.node.Node; - - - - - - - - - <T> - implements Child<T>, - - - { - - - - - - - - - - - - - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - import org.jboss.shrinkwrap.descriptor.api.DescriptorNamespace; - import org.jboss.shrinkwrap.descriptor.spi.node.NodeDescriptorImplBase; - import org.jboss.shrinkwrap.descriptor.impl.base.XMLDate; - import org.jboss.shrinkwrap.descriptor.spi.node.Node; - - - - - - - { - - - - - - - - - - - - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - import org.jboss.shrinkwrap.descriptor.spi.node.Node; - import org.jboss.shrinkwrap.descriptor.gen.TestDescriptorImpl; - import org.jboss.shrinkwrap.descriptor.api.Descriptors; - import org.junit.Test; - import static org.junit.Assert.*; - - - - - - - - - - - - - - - - - - - - - - - - { - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - package ; - - - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Constructor - // -------------------------------------------------------------------------------------|| - - - - { - - - - - } - - - { - super(descriptorName); - - } - - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Constructor - // -------------------------------------------------------------------------------------|| - - - { - this.t = t; - - - } - - - - { - this.t = t; - - - } - - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Methods - // -------------------------------------------------------------------------------------|| - - - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Instance Members - // -------------------------------------------------------------------------------------|| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public Node getRootNode() - { - - } - - - - - - - - - - // -------------------------------------------------------------------------------------|| - // Namespace - // -------------------------------------------------------------------------------------|| - - - { - - - - - } - - - { - - - } - - public List<String> getNamespaces() - { - - - - - - - - - - - - } - - - { - - - - - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - { - - - } - - - - { - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - } - - - - - - - - - - - - - - - - } - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - } - - - } - - - - - } - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - } - - - } - - - - - - } - - - - - - - - } - - - - - } - - - - - } - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - } - - - - - - - - - - - - - - - - - } - - - - - - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - } - - - - } - - - - - - } - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - } - - - - } - - - - - - - } - - - - - - - - - } - - - - - - - - - - } - - - - - - - - - } - - - - - - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/gen/src/main/resources/xslt/experiemental/metadata-ibm.xsl b/gen/src/main/resources/xslt/experiemental/metadata-ibm.xsl deleted file mode 100644 index 54c2ba43..00000000 --- a/gen/src/main/resources/xslt/experiemental/metadata-ibm.xsl +++ /dev/null @@ -1,1078 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/gen/src/main/resources/xslt/experiemental/metadataIBMEE5.xml b/gen/src/main/resources/xslt/experiemental/metadataIBMEE5.xml deleted file mode 100644 index 32078a2a..00000000 --- a/gen/src/main/resources/xslt/experiemental/metadataIBMEE5.xml +++ /dev/null @@ -1,740 +0,0 @@ - - - - - - - - - - - - - - - - /* - * JBoss, Home of Professional Open Source - * Copyright 2011, Red Hat Middleware LLC, and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * 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. - */ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - UNSPECIFIED - REMOTE - HOME - LOCAL - LOCAL_HOME - SERVICE_ENDPOINT - - - ACTIVITY_SESSION - BEAN_METHOD - - - APPLICATION - CONTAINER_AT_BOUNDARY - - - ROLLBACK - COMMIT - - - TRANSACTION_NONE - TRANSACTION_READ_UNCOMMITTED - TRANSACTION_READ_COMMITTED - TRANSACTION_REPEATABLE_READ - TRANSACTION_SERIALIZABLE - - - DEFAULT - AGGRESSIVE - NORMAL - - - LOOSE - TIGHT - - - Entity - Session - - - java.lang.Boolean - java.lang.Byte - java.lang.Character - java.lang.String - java.lang.Short - java.lang.Integer - java.lang.Long - java.lang.Float - java.lang.Double - - - true - false - yes - no - - - Consumes - Produces - ConsumesProduces - - - Transaction - Extended - - - Application - Container - - - Shareable - Unshareable - - - 1.x - 2.x - - - java.util.Collection - java.util.Set - - - Home - Remote - LocalHome - Local - ServiceEndpoint - - - One - Many - - - Bean - Container - - - Local - Remote - - - Stateful - Stateless - - - NotSupported - Supports - Required - RequiresNew - Mandatory - Never - - - Bean - Container - - - ONCE - ACTIVITY_SESSION - TRANSACTION - - - CALLER_IDENTITY - SPECIFIED_IDENTITY - SYSTEM_IDENTITY - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/gen/src/main/resources/xslt/experiemental/schemasIBMEE5.xml b/gen/src/main/resources/xslt/experiemental/schemasIBMEE5.xml deleted file mode 100644 index 8ebd86ad..00000000 --- a/gen/src/main/resources/xslt/experiemental/schemasIBMEE5.xml +++ /dev/null @@ -1,30 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/gen/src/main/resources/xslt/metadata.xsl b/gen/src/main/resources/xslt/metadata.xsl deleted file mode 100644 index 83b8af71..00000000 --- a/gen/src/main/resources/xslt/metadata.xsl +++ /dev/null @@ -1,1412 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/gen/src/main/resources/xslt/schemasJ2ee13.xml b/gen/src/main/resources/xslt/schemasJ2ee13.xml deleted file mode 100644 index 3eed9ddc..00000000 --- a/gen/src/main/resources/xslt/schemasJ2ee13.xml +++ /dev/null @@ -1,17 +0,0 @@ - -
- - - - - - - - - - - - - - -
diff --git a/gen/src/main/resources/xslt/schemasJ2ee14.xml b/gen/src/main/resources/xslt/schemasJ2ee14.xml deleted file mode 100644 index 6d85978e..00000000 --- a/gen/src/main/resources/xslt/schemasJ2ee14.xml +++ /dev/null @@ -1,26 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/gen/src/main/resources/xslt/schemasJBossEE5.xml b/gen/src/main/resources/xslt/schemasJBossEE5.xml deleted file mode 100644 index 2055c73f..00000000 --- a/gen/src/main/resources/xslt/schemasJBossEE5.xml +++ /dev/null @@ -1,37 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/gen/src/main/resources/xslt/schemasJBossEE6.xml b/gen/src/main/resources/xslt/schemasJBossEE6.xml deleted file mode 100644 index 26210aee..00000000 --- a/gen/src/main/resources/xslt/schemasJBossEE6.xml +++ /dev/null @@ -1,31 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/gen/src/main/resources/xslt/schemasJavaEE5.xml b/gen/src/main/resources/xslt/schemasJavaEE5.xml deleted file mode 100644 index ea1eb376..00000000 --- a/gen/src/main/resources/xslt/schemasJavaEE5.xml +++ /dev/null @@ -1,42 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/gen/src/main/resources/xslt/schemasJavaEE6.xml b/gen/src/main/resources/xslt/schemasJavaEE6.xml deleted file mode 100644 index a973a20c..00000000 --- a/gen/src/main/resources/xslt/schemasJavaEE6.xml +++ /dev/null @@ -1,83 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/pom.xml b/pom.xml index 97b345fd..3dab25cc 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,7 @@ test-util metadata-parser metadata-parser-test + schema-validator diff --git a/schema-validator/.gitignore b/schema-validator/.gitignore new file mode 100644 index 00000000..41127a89 --- /dev/null +++ b/schema-validator/.gitignore @@ -0,0 +1,8 @@ +.classpath +.project +.settings +target +bin +src/test/java/org/jboss/shrinkwrap/descriptor/api +src/test/java/org/jboss/shrinkwrap/descriptor/impl +log.txt diff --git a/schema-validator/pom.xml b/schema-validator/pom.xml new file mode 100644 index 00000000..d1045c79 --- /dev/null +++ b/schema-validator/pom.xml @@ -0,0 +1,119 @@ + + + + + + org.jboss.shrinkwrap.descriptors + shrinkwrap-descriptors-parent + 2.0.0-alpha-5-SNAPSHOT + ../pom.xml + + + + 4.0.0 + + + shrinkwrap-descriptors-schema-validator + ShrinkWrap Descriptors Schema Validator + XSD and DTD schema validator for the ShrinkWrap Descriptors + + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + true + + + + + + maven-surefire-plugin + + + ${env.JAVA5_HOME}/bin/java + + once + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + + + + + + + org.jboss.shrinkwrap.descriptors + shrinkwrap-descriptors-gen + ${project.version} + + + + commons-logging + commons-logging + 1.1 + + + + junit + junit + test + + + + + + + + + + + + + + + + + msv + msv + 20050913 + + + + msv + relaxngDatatype + 20030225 + + + + com.sun.msv.datatype.xsd + xsdlib + 20060615 + + + + xerces + xercesImpl + 2.10.0 + + + + + + diff --git a/schema-validator/pom.xml~ b/schema-validator/pom.xml~ new file mode 100644 index 00000000..8538e122 --- /dev/null +++ b/schema-validator/pom.xml~ @@ -0,0 +1,159 @@ + + + + + + org.jboss.shrinkwrap.descriptors + shrinkwrap-descriptors-parent + 2.0.0-alpha-5-SNAPSHOT + ../pom.xml + + + + 4.0.0 + + + shrinkwrap-descriptors-metadata-parser + ShrinkWrap Descriptors Metadata Parser + XSD and DTD parser for the ShrinkWrap Descriptors + maven-plugin + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + true + + + + + + + maven-surefire-plugin + + + ${env.JAVA5_HOME}/bin/java + + once + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + + + + + + + org.jboss.shrinkwrap.descriptors + shrinkwrap-descriptors-api-base + ${project.version} + + + + org.jboss.shrinkwrap.descriptors + shrinkwrap-descriptors-impl-base + ${project.version} + + + + org.jboss.shrinkwrap.descriptors + shrinkwrap-descriptors-test-util + ${project.version} + test + + + + org.apache.commons + commons-lang3 + 3.1 + + + + commons-logging + commons-logging + 1.1 + + + + org.apache.maven + maven-plugin-api + 3.0.3 + + + + net.sf.saxon + saxon + 8.7 + + + + net.sf.saxon + saxon-dom + 8.7 + + + + junit + junit + test + + + + com.sun.codemodel + codemodel + 2.4 + + + + com.sun.xml.dtd-parser + dtd-parser + 1.1 + + + + dom4j + dom4j + 1.6.1 + test + + + + org.apache.maven.plugin-testing + maven-plugin-testing-harness + 2.0-alpha-1 + test + + + + org.codehaus.woodstox + stax2-api + 3.1.1 + test + + + + org.codehaus.woodstox + woodstox-core-asl + 4.1.4 + test + + + + + diff --git a/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java b/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java new file mode 100644 index 00000000..96907efd --- /dev/null +++ b/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java @@ -0,0 +1,396 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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.jboss.shrinkwrap.descriptor.schemavalidator; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.util.List; + +import javax.xml.transform.stream.StreamSource; + +import org.apache.xerces.parsers.XIncludeAwareParserConfiguration; +import org.apache.xerces.parsers.XMLGrammarPreparser; +import org.apache.xerces.util.SymbolTable; +import org.apache.xerces.util.XMLGrammarPoolImpl; +import org.apache.xerces.xni.XMLResourceIdentifier; +import org.apache.xerces.xni.XNIException; +import org.apache.xerces.xni.grammars.XMLGrammarDescription; +import org.apache.xerces.xni.grammars.XMLGrammarPool; +import org.apache.xerces.xni.parser.XMLEntityResolver; +import org.apache.xerces.xni.parser.XMLErrorHandler; +import org.apache.xerces.xni.parser.XMLInputSource; +import org.apache.xerces.xni.parser.XMLParseException; +import org.apache.xerces.xni.parser.XMLParserConfiguration; +import org.jboss.shrinkwrap.descriptor.gen.ResourcerReader; + + +/** + * This class is able to validate XML files against corresponding + * DTD and XSD schemas. + *

+ * The implementation is based on the xerces xni library. + * + * @author Ralf Battenfeld + */ +public class XmlValidator { + + /** Defines the schema types */ + public enum SchemaType { + DTD, + XSD + } + + /** schema type we have to know for loading the grammars */ + private final SchemaType schemaType; + + /** preparser and grammar pool */ + private final SymbolTable sym = new SymbolTable(XmlValidatorConstants.BIG_PRIME); + private final XMLGrammarPreparser preparser; + private final XMLGrammarPool grammarPool; + + /** preparser and parser configuration settings */ + private boolean nameSpacesID; + private boolean validationID; + private boolean schemaValidationID; + private boolean schemaFullCheckingID; + private boolean honourAllSchemaID; + + /** used for tracking how many grammars are loaded */ + private int grammarCount = 0; + + /** User specific error handler */ + private XMLErrorHandler errorHandler; + + //-----------------------------------------------------------------------|| + //-- Public Constructors ------------------------------------------------|| + //-----------------------------------------------------------------------|| + + /** + * Default constructor. Initializes the DTD and XSD preparser. + * TODO describe default settings + */ + public XmlValidator(final SchemaType type) { + schemaType = type; + grammarPool = new XMLGrammarPoolImpl(); + preparser = new XMLGrammarPreparser(sym); + + if (schemaType == SchemaType.DTD) { + preparser.registerPreparser(XMLGrammarDescription.XML_DTD, null); + } + else { + preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null); + } + + preparser.setProperty(XmlValidatorConstants.GRAMMAR_POOL, grammarPool); + nameSpacesID = XmlValidatorConstants.DEFAULT_NAMESPACES_FEATURE_ID; + validationID = XmlValidatorConstants.DEFAULT_VALIDATION_FEATURE_ID; + schemaValidationID = XmlValidatorConstants.DEFAULT_SCHEMA_VALIDATION_FEATURE_ID; + schemaFullCheckingID = XmlValidatorConstants.DEFAULT_SCHEMA_FULL_CHECKING; + honourAllSchemaID = XmlValidatorConstants.DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS; + + // apply default settings + initializePreparser(); + } + + /** + * Constructs an individual configured validator. + * @param type defines the schema type. + * @param nameSpacesFeatureID TODO + * @param validationFeatureID TODO + * @param schemaValidationFeatureID TODO + * @param schemaFullCheckingFeatureID TODO + * @param honourAllSchemaLocationsID TODO + */ + public XmlValidator(final SchemaType type, + final boolean nameSpacesFeatureID, + final boolean validationFeatureID, + final boolean schemaValidationFeatureID, + final boolean schemaFullCheckingFeatureID, + final boolean honourAllSchemaLocationsID) { + + schemaType = type; + preparser = new XMLGrammarPreparser(sym); + grammarPool = new XMLGrammarPoolImpl(); + preparser.setProperty(XmlValidatorConstants.GRAMMAR_POOL, grammarPool); + nameSpacesID = nameSpacesFeatureID; + validationID = validationFeatureID; + schemaValidationID = schemaValidationFeatureID; + schemaFullCheckingID = schemaFullCheckingFeatureID; + honourAllSchemaID = honourAllSchemaLocationsID; + + // apply given settings + initializePreparser(); + + if (schemaType == SchemaType.DTD) { + preparser.registerPreparser(XMLGrammarDescription.XML_DTD, null); + } + else { + preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null); + } + } + + /** + * Constructor allowing to define the XMLGrammarPreparser and + * XMLGrammarPoolImpl outside of the class. + * @param type defines the schema type. + * @param preparser the preparser created by the caller. + * @param grammarPool the grammar pool created by the caller. + */ + public XmlValidator( + final SchemaType type, + final XMLGrammarPreparser preparser) { + + if (type == null) { + throw new RuntimeException("The schema type must be initialized"); + } + + if (preparser == null) { + throw new RuntimeException("The preparser must be initialized"); + } + + if (preparser.getGrammarPool() == null) { + throw new RuntimeException("The grammar pool must be initialized"); + } + + this.schemaType = type; + this.preparser = preparser; + this.grammarPool = preparser.getGrammarPool(); + } + + //-----------------------------------------------------------------------|| + //-- Public Grammar Loader Methods --------------------------------------|| + //-----------------------------------------------------------------------|| + + /** + * Installs the user XMLErrorHandler instance. + * @param errorHandler the user specific error handler. + */ + public void setErrorHandler(final XMLErrorHandler errorHandler) { + this.errorHandler = errorHandler; + } + + /** + * Loads the grammar. + * @param schema the grammar to be loaded. + * @throws XNIException + * @throws IOException + */ + public void loadGrammar(final String schema) throws XNIException, IOException { + if (isResourceCandidate(schema)) { + final InputStream inputStream = ResourcerReader.getResourceAsStream(schema); + if (inputStream != null) { + XMLInputSource xmlInputStream = new XMLInputSource(null, + schema, null, inputStream, null); + preparser.preparseGrammar(getGrammarDescription(), xmlInputStream); + } + } else { + preparser.preparseGrammar(getGrammarDescription(), stringToXIS(schema)); + } + grammarCount++; + } + + /** + * Loads the grammars extracted from the given list of schemas. + * @param schemaList + * @throws XNIException + * @throws IOException + */ + public void loadGrammars(final List schemaList) throws XNIException, IOException { + for (String schema: schemaList) { + loadGrammar(schema); + } + } + + //-----------------------------------------------------------------------|| + //-- Public Validator Methods ------------------------------------------|| + //-----------------------------------------------------------------------|| + + public void validate(final String pathToFile) throws XNIException, IOException { + checkGrammarPoolSize(); + final XMLParserConfiguration parserConfiguration = getParserConfiguration(); + parserConfiguration.parse(stringToXIS(pathToFile)); + } + + public void validate(final File xmlFile) throws XNIException, IOException { + checkGrammarPoolSize(); + validate(xmlFile.getAbsolutePath()); + } + + public void validate(final StreamSource xmlSource) throws XNIException, IOException { + checkGrammarPoolSize(); + throw new UnsupportedOperationException(); // TODO + } + + //-----------------------------------------------------------------------|| + //-- Other Public Methods -----------------------------------------------|| + //-----------------------------------------------------------------------|| + + public SchemaType getSchemaType() { + return schemaType; + } + + + //-----------------------------------------------------------------------|| + //-- Private Methods ----------------------------------------------------|| + //-----------------------------------------------------------------------|| + + /** + * Returns the XMLGrammarDescription string based in the schema type. + * @return + */ + private String getGrammarDescription() { + String grammarDescr = null; + if (schemaType == SchemaType.DTD) { + grammarDescr = XMLGrammarDescription.XML_DTD; + } + else if (schemaType == SchemaType.XSD) { + grammarDescr = XMLGrammarDescription.XML_SCHEMA; + } + else { + throw new RuntimeException("Not supported: " + schemaType); + } + return grammarDescr; + } + + /** + * Returns a XMLParserConfiguration based on the preparser configuration. + * @return a XMLParserConfiguration. + */ + private XMLParserConfiguration getParserConfiguration() { + final XMLParserConfiguration parserConfiguration = new XIncludeAwareParserConfiguration(sym, grammarPool); + + // copy over the parser configuration + parserConfiguration.setFeature(XmlValidatorConstants.NAMESPACES_FEATURE_ID, nameSpacesID); + parserConfiguration.setFeature(XmlValidatorConstants.VALIDATION_FEATURE_ID, validationID); + parserConfiguration.setFeature(XmlValidatorConstants.SCHEMA_VALIDATION_FEATURE_ID, schemaValidationID); + parserConfiguration.setFeature(XmlValidatorConstants.SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullCheckingID); + parserConfiguration.setFeature(XmlValidatorConstants.HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaID); + + // install our own error handler in case no external error hander is defined. + if (errorHandler == null) { + parserConfiguration.setErrorHandler(new ErrorHandler()); + } + + return parserConfiguration; + } + + /** + * Checks that at least one grammar is loaded. + */ + private void checkGrammarPoolSize() { + if (grammarCount == 0) { + throw new RuntimeException("No grammar loaded. Please, load the grammars first"); + } + } + + private void initializePreparser() { + preparser.setProperty(XmlValidatorConstants.GRAMMAR_POOL, grammarPool); + preparser.setFeature(XmlValidatorConstants.NAMESPACES_FEATURE_ID, nameSpacesID); + preparser.setFeature(XmlValidatorConstants.VALIDATION_FEATURE_ID, validationID); + preparser.setFeature(XmlValidatorConstants.SCHEMA_VALIDATION_FEATURE_ID, schemaValidationID); + preparser.setFeature(XmlValidatorConstants.SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullCheckingID); + preparser.setFeature(XmlValidatorConstants.HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaID); + preparser.setEntityResolver(new ResourceStreamResolver()); + } + + /** + * Returns a XMLInputSource object. + * @param uri the URI from which the input source is created. + * @return + */ + private static XMLInputSource stringToXIS(final String uri) { + return new XMLInputSource(null, uri, null); + } + + + //-----------------------------------------------------------------------|| + //-- Private Classes ----------------------------------------------------|| + //-----------------------------------------------------------------------|| + + /** + * This our default entity resolver. This class is able to resolve + * system id's as a resource stream preventing HTTP calls. + */ + private class ResourceStreamResolver implements XMLEntityResolver { + + @Override + public XMLInputSource resolveEntity(final XMLResourceIdentifier resourceIdentifier) + throws XNIException, IOException { + + XMLInputSource xmlInputStream = null; + InputStream inputStream = null; + if (resourceIdentifier.getExpandedSystemId().equals("http://www.w3.org/2001/xml.xsd")) { + + inputStream = Thread.currentThread() + .getContextClassLoader() + .getResourceAsStream("META-INF/2001/xml.xsd"); + + } else { + + // try to get the resource from the gen package + inputStream = getInputStream(resourceIdentifier); + } + + if (inputStream != null) { + xmlInputStream = new XMLInputSource(null, + resourceIdentifier.getExpandedSystemId(), null, inputStream, null); + } + + return xmlInputStream; + } + + } + + private boolean isResourceCandidate(final String resourceName) { + if (resourceName.indexOf(File.pathSeparator) >= 0) { + return false; + } + return true; + } + + private InputStream getInputStream(final XMLResourceIdentifier resourceIdentifier) throws MalformedURLException { + final File url = new File(resourceIdentifier.getExpandedSystemId()); + return ResourcerReader.getResourceAsStream(url.getName()); + } + + /** + * This is our default error handler. + */ + private class ErrorHandler implements XMLErrorHandler { + + @Override + public void warning(String domain, String key, + XMLParseException exception) throws XNIException { + // we ignore warnings + } + + @Override + public void error(String domain, String key, + XMLParseException exception) throws XNIException { + throw new XNIException(exception); + } + + @Override + public void fatalError(String domain, String key, + XMLParseException exception) throws XNIException { + throw new XNIException(exception); + } + } +} diff --git a/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorConstants.java b/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorConstants.java new file mode 100644 index 00000000..3e6bc41b --- /dev/null +++ b/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorConstants.java @@ -0,0 +1,52 @@ +package org.jboss.shrinkwrap.descriptor.schemavalidator; + +import org.apache.xerces.impl.Constants; + +public interface XmlValidatorConstants { + + /** Property identifier: symbol table. */ + public static final String SYMBOL_TABLE = Constants.XERCES_PROPERTY_PREFIX + + Constants.SYMBOL_TABLE_PROPERTY; + + /** Property identifier: grammar pool. */ + public static final String GRAMMAR_POOL = Constants.XERCES_PROPERTY_PREFIX + + Constants.XMLGRAMMAR_POOL_PROPERTY; + + /** Namespaces feature id (http://xml.org/sax/features/namespaces). */ + public static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces"; + + /** Validation feature id (http://xml.org/sax/features/validation). */ + public static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation"; + + /** + * Schema validation feature id + * (http://apache.org/xml/features/validation/schema). + */ + public static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema"; + + /** + * Schema full checking feature id + * (http://apache.org/xml/features/validation/schema-full-checking). + */ + public static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking"; + + /** + * Honour all schema locations feature id + * (http://apache.org/xml/features/honour-all-schemaLocations). + */ + public static final String HONOUR_ALL_SCHEMA_LOCATIONS_ID = "http://apache.org/xml/features/honour-all-schemaLocations"; + + // a larg(ish) prime to use for a symbol table to be shared + // among + // potentially man parsers. Start one as close to 2K (20 + // times larger than normal) and see what happens... + public static final int BIG_PRIME = 2039; + + // default settings + public static final boolean DEFAULT_NAMESPACES_FEATURE_ID = true; + public static final boolean DEFAULT_VALIDATION_FEATURE_ID = true; + public static final boolean DEFAULT_SCHEMA_VALIDATION_FEATURE_ID = true; + public static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false; + public static final boolean DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS = false; + +} diff --git a/schema-validator/src/main/resources/META-INF/2001/xml.xsd b/schema-validator/src/main/resources/META-INF/2001/xml.xsd new file mode 100644 index 00000000..aea7d0db --- /dev/null +++ b/schema-validator/src/main/resources/META-INF/2001/xml.xsd @@ -0,0 +1,287 @@ + + + + + + +

+

About the XML namespace

+ +
+

+ This schema document describes the XML namespace, in a form + suitable for import by other schema documents. +

+

+ See + http://www.w3.org/XML/1998/namespace.html and + + http://www.w3.org/TR/REC-xml for information + about this namespace. +

+

+ Note that local names in this namespace are intended to be + defined only by the World Wide Web Consortium or its subgroups. + The names currently defined in this namespace are listed below. + They should not be used with conflicting semantics by any Working + Group, specification, or document instance. +

+

+ See further below in this document for more information about how to refer to this schema document from your own + XSD schema documents and about the + namespace-versioning policy governing this schema document. +

+
+
+ + + + + + +
+ +

lang (as an attribute name)

+

+ denotes an attribute whose value + is a language code for the natural language of the content of + any element; its value is inherited. This name is reserved + by virtue of its definition in the XML specification.

+ +
+
+

Notes

+

+ Attempting to install the relevant ISO 2- and 3-letter + codes as the enumerated possible values is probably never + going to be a realistic possibility. +

+

+ See BCP 47 at + http://www.rfc-editor.org/rfc/bcp/bcp47.txt + and the IANA language subtag registry at + + http://www.iana.org/assignments/language-subtag-registry + for further information. +

+

+ The union allows for the 'un-declaration' of xml:lang with + the empty string. +

+
+
+
+ + + + + + + + + +
+ + + + +
+ +

space (as an attribute name)

+

+ denotes an attribute whose + value is a keyword indicating what whitespace processing + discipline is intended for the content of the element; its + value is inherited. This name is reserved by virtue of its + definition in the XML specification.

+ +
+
+
+ + + + + + +
+ + + +
+ +

base (as an attribute name)

+

+ denotes an attribute whose value + provides a URI to be used as the base for interpreting any + relative URIs in the scope of the element on which it + appears; its value is inherited. This name is reserved + by virtue of its definition in the XML Base specification.

+ +

+ See http://www.w3.org/TR/xmlbase/ + for information about this attribute. +

+
+
+
+
+ + + + +
+ +

id (as an attribute name)

+

+ denotes an attribute whose value + should be interpreted as if declared to be of type ID. + This name is reserved by virtue of its definition in the + xml:id specification.

+ +

+ See http://www.w3.org/TR/xml-id/ + for information about this attribute. +

+
+
+
+
+ + + + + + + + + + +
+ +

Father (in any context at all)

+ +
+

+ denotes Jon Bosak, the chair of + the original XML Working Group. This name is reserved by + the following decision of the W3C XML Plenary and + XML Coordination groups: +

+
+

+ In appreciation for his vision, leadership and + dedication the W3C XML Plenary on this 10th day of + February, 2000, reserves for Jon Bosak in perpetuity + the XML name "xml:Father". +

+
+
+
+
+
+ + + +
+

About this schema document

+ +
+

+ This schema defines attributes and an attribute group suitable + for use by schemas wishing to allow xml:base, + xml:lang, xml:space or + xml:id attributes on elements they define. +

+

+ To enable this, such a schema must import this schema for + the XML namespace, e.g. as follows: +

+
+          <schema . . .>
+           . . .
+           <import namespace="http://www.w3.org/XML/1998/namespace"
+                      schemaLocation="http://www.w3.org/2001/xml.xsd"/>
+     
+

+ or +

+
+           <import namespace="http://www.w3.org/XML/1998/namespace"
+                      schemaLocation="http://www.w3.org/2009/01/xml.xsd"/>
+     
+

+ Subsequently, qualified reference to any of the attributes or the + group defined below will have the desired effect, e.g. +

+
+          <type . . .>
+           . . .
+           <attributeGroup ref="xml:specialAttrs"/>
+     
+

+ will define a type which will schema-validate an instance element + with any of those attributes. +

+
+
+
+
+ + + +
+

Versioning policy for this schema document

+
+

+ In keeping with the XML Schema WG's standard versioning + policy, this schema document will persist at + + http://www.w3.org/2009/01/xml.xsd. +

+

+ At the date of issue it can also be found at + + http://www.w3.org/2001/xml.xsd. +

+

+ The schema document at that URI may however change in the future, + in order to remain compatible with the latest version of XML + Schema itself, or with the XML namespace itself. In other words, + if the XML Schema or XML namespaces change, the version of this + document at + http://www.w3.org/2001/xml.xsd + + will change accordingly; the version at + + http://www.w3.org/2009/01/xml.xsd + + will not change. +

+

+ Previous dated (and unchanging) versions of this schema + document are at: +

+ +
+
+
+
+ + + diff --git a/schema-validator/src/main/resources/META-INF/2004/xml.xsd b/schema-validator/src/main/resources/META-INF/2004/xml.xsd new file mode 100644 index 00000000..9bf636de --- /dev/null +++ b/schema-validator/src/main/resources/META-INF/2004/xml.xsd @@ -0,0 +1,135 @@ + + + + + + + See http://www.w3.org/XML/1998/namespace.html and + http://www.w3.org/TR/REC-xml for information about this namespace. + + This schema document describes the XML namespace, in a form + suitable for import by other schema documents. + + Note that local names in this namespace are intended to be defined + only by the World Wide Web Consortium or its subgroups. The + following names are currently defined in this namespace and should + not be used with conflicting semantics by any Working Group, + specification, or document instance: + + base (as an attribute name): denotes an attribute whose value + provides a URI to be used as the base for interpreting any + relative URIs in the scope of the element on which it + appears; its value is inherited. This name is reserved + by virtue of its definition in the XML Base specification. + + id (as an attribute name): denotes an attribute whose value + should be interpreted as if declared to be of type ID. + The xml:id specification is not yet a W3C Recommendation, + but this attribute is included here to facilitate experimentation + with the mechanisms it proposes. Note that it is _not_ included + in the specialAttrs attribute group. + + lang (as an attribute name): denotes an attribute whose value + is a language code for the natural language of the content of + any element; its value is inherited. This name is reserved + by virtue of its definition in the XML specification. + + space (as an attribute name): denotes an attribute whose + value is a keyword indicating what whitespace processing + discipline is intended for the content of the element; its + value is inherited. This name is reserved by virtue of its + definition in the XML specification. + + Father (in any context at all): denotes Jon Bosak, the chair of + the original XML Working Group. This name is reserved by + the following decision of the W3C XML Plenary and + XML Coordination groups: + + In appreciation for his vision, leadership and dedication + the W3C XML Plenary on this 10th day of February, 2000 + reserves for Jon Bosak in perpetuity the XML name + xml:Father + + + + + This schema defines attributes and an attribute group + suitable for use by + schemas wishing to allow xml:base, xml:lang or xml:space attributes + on elements they define. + + To enable this, such a schema must import this schema + for the XML namespace, e.g. as follows: + <schema . . .> + . . . + <import namespace="http://www.w3.org/XML/1998/namespace" + schemaLocation="http://www.w3.org/2001/03/xml.xsd"/> + + Subsequently, qualified reference to any of the attributes + or the group defined below will have the desired effect, e.g. + + <type . . .> + . . . + <attributeGroup ref="xml:specialAttrs"/> + + will define a type which will schema-validate an instance + element with any of those attributes + + + + In keeping with the XML Schema WG's standard versioning + policy, this schema document will persist at + http://www.w3.org/2004/10/xml.xsd. + At the date of issue it can also be found at + http://www.w3.org/2001/xml.xsd. + The schema document at that URI may however change in the future, + in order to remain compatible with the latest version of XML Schema + itself, or with the XML namespace itself. In other words, if the XML + Schema or XML namespaces change, the version of this document at + http://www.w3.org/2001/xml.xsd will change + accordingly; the version at + http://www.w3.org/2004/10/xml.xsd will not change. + + + + + + Attempting to install the relevant ISO 2- and 3-letter + codes as the enumerated possible values is probably never + going to be a realistic possibility. See + RFC 3066 at http://www.ietf.org/rfc/rfc3066.txt and the IANA registry + at http://www.iana.org/assignments/lang-tag-apps.htm for + further information. + + + + + + + + + + + + + + + See http://www.w3.org/TR/xmlbase/ for + information about this attribute. + + + + + + See http://www.w3.org/TR/xml-id/ for + information about this attribute. + + + + + + + + + + diff --git a/schema-validator/src/main/resources/META-INF/2007/xml.xsd b/schema-validator/src/main/resources/META-INF/2007/xml.xsd new file mode 100644 index 00000000..eeb9db56 --- /dev/null +++ b/schema-validator/src/main/resources/META-INF/2007/xml.xsd @@ -0,0 +1,145 @@ + + + + + + See http://www.w3.org/XML/1998/namespace.html and + http://www.w3.org/TR/REC-xml for information about this namespace. + + This schema document describes the XML namespace, in a form + suitable for import by other schema documents. + + Note that local names in this namespace are intended to be defined + only by the World Wide Web Consortium or its subgroups. The + following names are currently defined in this namespace and should + not be used with conflicting semantics by any Working Group, + specification, or document instance: + + base (as an attribute name): denotes an attribute whose value + provides a URI to be used as the base for interpreting any + relative URIs in the scope of the element on which it + appears; its value is inherited. This name is reserved + by virtue of its definition in the XML Base specification. + + id (as an attribute name): denotes an attribute whose value + should be interpreted as if declared to be of type ID. + This name is reserved by virtue of its definition in the + xml:id specification. + + lang (as an attribute name): denotes an attribute whose value + is a language code for the natural language of the content of + any element; its value is inherited. This name is reserved + by virtue of its definition in the XML specification. + + space (as an attribute name): denotes an attribute whose + value is a keyword indicating what whitespace processing + discipline is intended for the content of the element; its + value is inherited. This name is reserved by virtue of its + definition in the XML specification. + + Father (in any context at all): denotes Jon Bosak, the chair of + the original XML Working Group. This name is reserved by + the following decision of the W3C XML Plenary and + XML Coordination groups: + + In appreciation for his vision, leadership and dedication + the W3C XML Plenary on this 10th day of February, 2000 + reserves for Jon Bosak in perpetuity the XML name + xml:Father + + + + + This schema defines attributes and an attribute group + suitable for use by + schemas wishing to allow xml:base, xml:lang, xml:space or xml:id + attributes on elements they define. + + To enable this, such a schema must import this schema + for the XML namespace, e.g. as follows: + <schema . . .> + . . . + <import namespace="http://www.w3.org/XML/1998/namespace" + schemaLocation="http://www.w3.org/2001/xml.xsd"/> + + Subsequently, qualified reference to any of the attributes + or the group defined below will have the desired effect, e.g. + + <type . . .> + . . . + <attributeGroup ref="xml:specialAttrs"/> + + will define a type which will schema-validate an instance + element with any of those attributes + + + + In keeping with the XML Schema WG's standard versioning + policy, this schema document will persist at + http://www.w3.org/2007/08/xml.xsd. + At the date of issue it can also be found at + http://www.w3.org/2001/xml.xsd. + The schema document at that URI may however change in the future, + in order to remain compatible with the latest version of XML Schema + itself, or with the XML namespace itself. In other words, if the XML + Schema or XML namespaces change, the version of this document at + http://www.w3.org/2001/xml.xsd will change + accordingly; the version at + http://www.w3.org/2007/08/xml.xsd will not change. + + + + + + Attempting to install the relevant ISO 2- and 3-letter + codes as the enumerated possible values is probably never + going to be a realistic possibility. See + RFC 3066 at http://www.ietf.org/rfc/rfc3066.txt and the IANA registry + at http://www.iana.org/assignments/lang-tag-apps.htm for + further information. + + The union allows for the 'un-declaration' of xml:lang with + the empty string. + + + + + + + + + + + + + + + + + + + + + + + + See http://www.w3.org/TR/xmlbase/ for + information about this attribute. + + + + + + See http://www.w3.org/TR/xml-id/ for + information about this attribute. + + + + + + + + + + + diff --git a/schema-validator/src/main/resources/META-INF/2009/xml.xsd b/schema-validator/src/main/resources/META-INF/2009/xml.xsd new file mode 100644 index 00000000..bd291f3d --- /dev/null +++ b/schema-validator/src/main/resources/META-INF/2009/xml.xsd @@ -0,0 +1,286 @@ + + + + + + +
+

About the XML namespace

+ +
+

+ This schema document describes the XML namespace, in a form + suitable for import by other schema documents. +

+

+ See + http://www.w3.org/XML/1998/namespace.html and + + http://www.w3.org/TR/REC-xml for information + about this namespace. +

+

+ Note that local names in this namespace are intended to be + defined only by the World Wide Web Consortium or its subgroups. + The names currently defined in this namespace are listed below. + They should not be used with conflicting semantics by any Working + Group, specification, or document instance. +

+

+ See further below in this document for more information about how to refer to this schema document from your own + XSD schema documents and about the + namespace-versioning policy governing this schema document. +

+
+
+
+
+ + + + +
+ +

lang (as an attribute name)

+

+ denotes an attribute whose value + is a language code for the natural language of the content of + any element; its value is inherited. This name is reserved + by virtue of its definition in the XML specification.

+ +
+
+

Notes

+

+ Attempting to install the relevant ISO 2- and 3-letter + codes as the enumerated possible values is probably never + going to be a realistic possibility. +

+

+ See BCP 47 at + http://www.rfc-editor.org/rfc/bcp/bcp47.txt + and the IANA language subtag registry at + + http://www.iana.org/assignments/language-subtag-registry + for further information. +

+

+ The union allows for the 'un-declaration' of xml:lang with + the empty string. +

+
+
+
+ + + + + + + + + +
+ + + + +
+ +

space (as an attribute name)

+

+ denotes an attribute whose + value is a keyword indicating what whitespace processing + discipline is intended for the content of the element; its + value is inherited. This name is reserved by virtue of its + definition in the XML specification.

+ +
+
+
+ + + + + + +
+ + + +
+ +

base (as an attribute name)

+

+ denotes an attribute whose value + provides a URI to be used as the base for interpreting any + relative URIs in the scope of the element on which it + appears; its value is inherited. This name is reserved + by virtue of its definition in the XML Base specification.

+ +

+ See http://www.w3.org/TR/xmlbase/ + for information about this attribute. +

+
+
+
+
+ + + + +
+ +

id (as an attribute name)

+

+ denotes an attribute whose value + should be interpreted as if declared to be of type ID. + This name is reserved by virtue of its definition in the + xml:id specification.

+ +

+ See http://www.w3.org/TR/xml-id/ + for information about this attribute. +

+
+
+
+
+ + + + + + + + + + +
+ +

Father (in any context at all)

+ +
+

+ denotes Jon Bosak, the chair of + the original XML Working Group. This name is reserved by + the following decision of the W3C XML Plenary and + XML Coordination groups: +

+
+

+ In appreciation for his vision, leadership and + dedication the W3C XML Plenary on this 10th day of + February, 2000, reserves for Jon Bosak in perpetuity + the XML name "xml:Father". +

+
+
+
+
+
+ + + +
+

About this schema document

+ +
+

+ This schema defines attributes and an attribute group suitable + for use by schemas wishing to allow xml:base, + xml:lang, xml:space or + xml:id attributes on elements they define. +

+

+ To enable this, such a schema must import this schema for + the XML namespace, e.g. as follows: +

+
+          <schema . . .>
+           . . .
+           <import namespace="http://www.w3.org/XML/1998/namespace"
+                      schemaLocation="http://www.w3.org/2001/xml.xsd"/>
+     
+

+ or +

+
+           <import namespace="http://www.w3.org/XML/1998/namespace"
+                      schemaLocation="http://www.w3.org/2009/01/xml.xsd"/>
+     
+

+ Subsequently, qualified reference to any of the attributes or the + group defined below will have the desired effect, e.g. +

+
+          <type . . .>
+           . . .
+           <attributeGroup ref="xml:specialAttrs"/>
+     
+

+ will define a type which will schema-validate an instance element + with any of those attributes. +

+
+
+
+
+ + + +
+

Versioning policy for this schema document

+
+

+ In keeping with the XML Schema WG's standard versioning + policy, this schema document will persist at + + http://www.w3.org/2009/01/xml.xsd. +

+

+ At the date of issue it can also be found at + + http://www.w3.org/2001/xml.xsd. +

+

+ The schema document at that URI may however change in the future, + in order to remain compatible with the latest version of XML + Schema itself, or with the XML namespace itself. In other words, + if the XML Schema or XML namespaces change, the version of this + document at + http://www.w3.org/2001/xml.xsd + + will change accordingly; the version at + + http://www.w3.org/2009/01/xml.xsd + + will not change. +

+

+ Previous dated (and unchanging) versions of this schema + document are at: +

+ +
+
+
+
+ +
diff --git a/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/ValidatorEvaluationTest.java b/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/ValidatorEvaluationTest.java new file mode 100644 index 00000000..2bb8a242 --- /dev/null +++ b/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/ValidatorEvaluationTest.java @@ -0,0 +1,514 @@ +package org.jboss.shrinkwrap.descriptor.schemavalidator; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.ObjectOutput; +import java.io.ObjectOutputStream; +import java.io.Reader; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.transform.Source; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import javax.xml.validation.Validator; + +import org.apache.xerces.impl.Constants; +import org.apache.xerces.parsers.XIncludeAwareParserConfiguration; +import org.apache.xerces.parsers.XMLGrammarPreparser; +import org.apache.xerces.util.SymbolTable; +import org.apache.xerces.util.XMLGrammarPoolImpl; +import org.apache.xerces.xni.XMLResourceIdentifier; +import org.apache.xerces.xni.XNIException; +import org.apache.xerces.xni.grammars.Grammar; +import org.apache.xerces.xni.grammars.XMLGrammarDescription; +import org.apache.xerces.xni.parser.XMLEntityResolver; +import org.apache.xerces.xni.parser.XMLErrorHandler; +import org.apache.xerces.xni.parser.XMLInputSource; +import org.apache.xerces.xni.parser.XMLParseException; +import org.apache.xerces.xni.parser.XMLParserConfiguration; +import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.ls.LSInput; +import org.w3c.dom.ls.LSResourceResolver; + +import com.sun.msv.grammar.xmlschema.XMLSchemaGrammar; + +public class ValidatorEvaluationTest { + + /** Property identifier: symbol table. */ + public static final String SYMBOL_TABLE = Constants.XERCES_PROPERTY_PREFIX + + Constants.SYMBOL_TABLE_PROPERTY; + + /** Property identifier: grammar pool. */ + public static final String GRAMMAR_POOL = Constants.XERCES_PROPERTY_PREFIX + + Constants.XMLGRAMMAR_POOL_PROPERTY; + + // feature ids + + /** Namespaces feature id (http://xml.org/sax/features/namespaces). */ + protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces"; + + /** Validation feature id (http://xml.org/sax/features/validation). */ + protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation"; + + /** + * Schema validation feature id + * (http://apache.org/xml/features/validation/schema). + */ + protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema"; + + /** + * Schema full checking feature id + * (http://apache.org/xml/features/validation/schema-full-checking). + */ + protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking"; + + /** + * Honour all schema locations feature id + * (http://apache.org/xml/features/honour-all-schemaLocations). + */ + protected static final String HONOUR_ALL_SCHEMA_LOCATIONS_ID = "http://apache.org/xml/features/honour-all-schemaLocations"; + + // a larg(ish) prime to use for a symbol table to be shared + // among + // potentially man parsers. Start one as close to 2K (20 + // times larger than normal) and see what happens... + public static final int BIG_PRIME = 2039; + + // default settings + + /** Default Schema full checking support (false). */ + protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false; + + /** Default honour all schema locations (false). */ + protected static final boolean DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS = false; + + + static final String JAXP_SCHEMA_LANGUAGE + = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; + static final String W3C_XML_SCHEMA + = "http://www.w3.org/2001/XMLSchema"; + static final String JAXP_SCHEMA_SOURCE + = "http://java.sun.com/xml/jaxp/properties/schemaSource"; + + @Test + public void testJaxpValidator() throws Exception { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + factory.setValidating(true); + + factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); + + // Set the schema file + factory.setAttribute(JAXP_SCHEMA_SOURCE, new File("../gen/src/main/resources/xsd/application_6.xsd")); + + try { + DocumentBuilder parser = factory.newDocumentBuilder(); + + // Parse the file. If errors found, they will be printed. + parser.parse("../test/src/test/resources/test-gen-application6.xml"); + parser.getSchema(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testXMLValidator() throws Exception { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + + // read the XML file + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(new File("../test/src/test/resources/test-gen-application6.xml")); + + // create a SchemaFactory and a Schema + SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + Source schemaFile = new StreamSource(new File("../gen/src/main/resources/xsd/application_6.xsd")); + Schema schema = schemaFactory.newSchema(schemaFile); + + // create a Validator object and validate the XML file + Validator validator = schema.newValidator(); + validator.validate(new DOMSource(doc)); + +// XStream xstream = new XStream(); +// String xml = xstream.toXML(validator); +// +// Object newNamedMap = (Object)xstream.fromXML(xml); +// +// SchemaGrammarSerializer.serialize(serializableSchema, "/tmp/schema.ser"); +// final SerializableSchema newSchema = SchemaGrammarSerializer.deserialize(SerializableSchema.class, "/tmp/schema.ser"); + } + + @Test + public void testResolver() throws XMLStreamException { +// +// final XMLMapper mapper = XMLMapper.Factory.create(); +// mapper.parseDocument(null, null); +// +// try { +// DOMParser parser = new DOMParser(); +//// parser. +// parser.setFeature("http://xml.org/sax/features/validation", true); +// parser.setProperty( +// "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", +// "../gen/src/main/resources/xsd/application_6.xsd"); +// // ErrorChecker errors = new ErrorChecker(); +// // parser.setErrorHandler(errors); +// parser.parse("../test/src/test/resources/test-gen-application6.xml"); +// } catch (Exception e) { +// System.out.print("Problem parsing the file."); +// } + + // + // SchemaFactory schemaFactory = + // SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + // + // schemaFactory.setResourceResolver(new ClasspathResourceResolver()); + } + + + @Test + public void testXerces() throws Exception { + final boolean externalDTDs = false; + final SymbolTable sym = new SymbolTable(BIG_PRIME); + final XMLGrammarPreparser preparser = new XMLGrammarPreparser(sym); + final XMLGrammarPoolImpl grammarPool = new XMLGrammarPoolImpl(); + String grammarDescr = null; + + boolean isDTD = false; + if (externalDTDs) { + preparser.registerPreparser(XMLGrammarDescription.XML_DTD, null); + grammarDescr = XMLGrammarDescription.XML_DTD; + isDTD = true; + } + else { + preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null); + grammarDescr = XMLGrammarDescription.XML_SCHEMA; + isDTD = false; + } + + final List schemas = new ArrayList(); +// schemas.add("../gen/src/main/resources/xsd/2001/xml.xsd"); +// schemas.add("../gen/src/main/resources/xsd/javaee_6.xsd"); +// schemas.add("../gen/src/main/resources/xsd/javaee_web_services_client_1_3.xsd"); + schemas.add("../gen/src/main/resources/xsd/application_6.xsd"); + + final List xmlFiles = new ArrayList(); + xmlFiles.add("src/test/resources/test-invalid-application-6.xml"); + + boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING; + boolean honourAllSchemaLocations = DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS; + + preparser.setProperty(GRAMMAR_POOL, grammarPool); + preparser.setFeature(NAMESPACES_FEATURE_ID, true); + preparser.setFeature(VALIDATION_FEATURE_ID, true); + + // note we can set schema features just in case... + preparser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true); + preparser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking); + preparser.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaLocations); + + preparser.setEntityResolver(new XMLEntityResolver() { + + @Override + public XMLInputSource resolveEntity( + XMLResourceIdentifier resourceIdentifier) + throws XNIException, IOException { + + if (resourceIdentifier.getExpandedSystemId().equals("http://www.w3.org/2001/xml.xsd")) { + return stringToXIS("../gen/src/main/resources/xsd/2001/xml.xsd"); + } + return null; + } + + }); + + // parse the grammar... + + try { + for (String schema: schemas) { + final Grammar g = preparser.preparseGrammar(grammarDescr, stringToXIS(schema)); + } + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + + XMLParserConfiguration parserConfiguration = null; + + // Now we have a grammar pool and a SymbolTable; just + // build a configuration and we're on our way! + if (parserConfiguration == null) { + parserConfiguration = new XIncludeAwareParserConfiguration(sym, grammarPool); + } else { + // set GrammarPool and SymbolTable... + parserConfiguration.setProperty(SYMBOL_TABLE, sym); + parserConfiguration.setProperty(GRAMMAR_POOL, grammarPool); + } + + // now must reset features, unfortunately: + try { + parserConfiguration.setFeature(NAMESPACES_FEATURE_ID, true); + parserConfiguration.setFeature(VALIDATION_FEATURE_ID, true); + parserConfiguration.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true); + parserConfiguration.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking); + parserConfiguration.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaLocations); + } + catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + + parserConfiguration.setErrorHandler(new XMLErrorHandler() { + + @Override + public void warning(String domain, String key, + XMLParseException exception) throws XNIException { + + } + + @Override + public void error(String domain, String key, + XMLParseException exception) throws XNIException { + throw new XNIException(exception); + } + + @Override + public void fatalError(String domain, String key, + XMLParseException exception) throws XNIException { + throw new XNIException(exception); + } + + }); + + + // then for each instance file, try to validate it + if (xmlFiles != null) { +// try { + for (String xmlFile: xmlFiles) { + parserConfiguration.parse(stringToXIS(xmlFile)); + } +// } catch (Exception e) { +// e.printStackTrace(); +// System.exit(1); +// } + } + } + + private static XMLInputSource stringToXIS(String uri) { + return new XMLInputSource(null, uri, null); + } + + @Test + public void testWoodstock() throws Exception { +// final XMLValidationSchemaFactory sf = XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA); +// final File schemaFile = new File("../gen/src/main/resources/xsd/persistence_2_0.xsd"); +// XMLValidationSchema dtd = null; +// +// try { +// dtd = sf.createSchema(schemaFile); +// } catch (XMLStreamException xe) { +// System.err.println("Failed to process the XSD file ('" + schemaFile +// + "'): " + xe); +// System.exit(1); +// } +// +// W3CSchema w3cSchema = (W3CSchema)dtd; +// XMLSchemaGrammar grammar = getGrammar(w3cSchema); +// +// File inputFile = new File( +// "../test/src/test/resources/test-gen-persistence20.xml"); +// try { +// XMLInputFactory2 ifact = (XMLInputFactory2) XMLInputFactory.newInstance(); +// XMLStreamReader2 sr = ifact.createXMLStreamReader(inputFile); +// +// try { +// sr.validateAgainst(dtd); +// /* +// * Document validation is done as document is read through (ie. +// * it's fully streaming as well as parsing), so just need to +// * traverse the contents. +// */ +// while (sr.hasNext()) { +// sr.next(); +// } +// } catch (XMLValidationException vex) { +// System.err.println("Document '" + inputFile +// + "' failed validation: " + vex); +// System.exit(1); +// } +// } catch (XMLStreamException xse) { +// System.err.println("Failed parse the input document ('" + inputFile +// + "'): " + xse); +// System.exit(1); +// } +// System.out.println("Document '" + inputFile +// + "' succesfully validated."); + } + + private void serialize(final Object obj) throws IOException { + try { + // Serialize to a file + ObjectOutput out = new ObjectOutputStream(new FileOutputStream("filename.ser")); + out.writeObject(obj); + out.close(); + + // Serialize to a byte array + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + out = new ObjectOutputStream(bos); + out.writeObject(obj); + out.close(); + + // Get the bytes of the serialized object + byte[] buf = bos.toByteArray(); + } catch (IOException e) { + throw(e); + } + } + + private class ClasspathResourceResolver implements LSResourceResolver { + + @Override + public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { + + LSInputImpl input = new LSInputImpl(); + + InputStream stream = getClass().getClassLoader().getResourceAsStream(systemId); + + input.setPublicId(publicId); + input.setSystemId(systemId); + input.setBaseURI(baseURI); + input.setCharacterStream(new InputStreamReader(stream)); + + return input; + } + } + + private class LSInputImpl implements LSInput { + + private Reader characterStream; + private InputStream byteStream; + private String stringData; + private String systemId; + private String publicId; + private String baseURI; + private String encoding; + private boolean certifiedText; + + @Override + public String getBaseURI() { + // TODO Auto-generated method stub + return null; + } + + @Override + public InputStream getByteStream() { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean getCertifiedText() { + // TODO Auto-generated method stub + return false; + } + + @Override + public Reader getCharacterStream() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getEncoding() { + // TODO Auto-generated method stub + return null; + } + @Override + public String getPublicId() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getStringData() { + // TODO Auto-generated method stub + return null; + } + @Override + public String getSystemId() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setBaseURI(String arg0) { + // TODO Auto-generated method stub + + } + @Override + public void setByteStream(InputStream arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void setCertifiedText(boolean arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void setCharacterStream(Reader arg0) { + // TODO Auto-generated method stub + + } + @Override + public void setEncoding(String arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void setPublicId(String arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void setStringData(String arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void setSystemId(String arg0) { + // TODO Auto-generated method stub + + } + + // Getters and setters here + } + +// private XMLSchemaGrammar getGrammar(final W3CSchema schema) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { +// final Field f = schema.getClass().getDeclaredField("mGrammar"); +// f.setAccessible(true); +// final XMLSchemaGrammar grammar = (XMLSchemaGrammar) f.get(schema); +// return grammar; +// } +} diff --git a/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorTestCase.java b/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorTestCase.java new file mode 100644 index 00000000..04fa2668 --- /dev/null +++ b/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorTestCase.java @@ -0,0 +1,77 @@ +package org.jboss.shrinkwrap.descriptor.schemavalidator; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; + +import org.apache.xerces.parsers.XMLGrammarPreparser; +import org.apache.xerces.util.SymbolTable; +import org.apache.xerces.xni.XNIException; +import org.jboss.shrinkwrap.descriptor.schemavalidator.XmlValidator.SchemaType; +import org.junit.Test; + +public class XmlValidatorTestCase { + + @Test + public void testXmlValidatorSchemaTypeXSD() { + final XmlValidator validator = new XmlValidator(SchemaType.XSD); + Assert.assertTrue(validator.getSchemaType() == SchemaType.XSD); + } + + @Test + public void testXmlValidatorSchemaTypeDTD() { + final XmlValidator validator = new XmlValidator(SchemaType.DTD); + Assert.assertTrue(validator.getSchemaType() == SchemaType.DTD); + } + + @Test(expected = RuntimeException.class) + public void testXmlValidatorSchemaWithNoConfig() { + new XmlValidator(null, null); + } + + @Test(expected = RuntimeException.class) + public void testXmlValidatorSchemaWithNoParser() { + new XmlValidator(SchemaType.DTD, null); + } + + @Test(expected = RuntimeException.class) + public void testXmlValidatorSchemaWithNoGrammarPool() { + final SymbolTable sym = new SymbolTable(XmlValidatorConstants.BIG_PRIME); + final XMLGrammarPreparser preparser = new XMLGrammarPreparser(sym); + new XmlValidator(SchemaType.DTD, preparser); + } + + @Test + public void testValidApplicationXML() throws Exception { + final XmlValidator validator = new XmlValidator(SchemaType.XSD); + validator.loadGrammar("application_6.xsd"); + validator.validate("src/test/resources/test-valid-application-6.xml"); + } + + @Test(expected = XNIException.class) + public void testInvalidApplicationXML() throws Exception { + final XmlValidator validator = new XmlValidator(SchemaType.XSD); + validator.loadGrammar("application_6.xsd"); + validator.validate("src/test/resources/test-invalid-application-6.xml"); + } + + @Test + public void testValidApplicationXMLFromList() throws Exception { + final XmlValidator validator = new XmlValidator(SchemaType.XSD); + final List schemas = new ArrayList(); + schemas.add("application_6.xsd"); + validator.loadGrammars(schemas); + validator.validate("src/test/resources/test-valid-application-6.xml"); + } + + @Test(expected = XNIException.class) + public void testInvalidApplicationXMLFromList() throws Exception { + final XmlValidator validator = new XmlValidator(SchemaType.XSD); + final List schemas = new ArrayList(); + schemas.add("application_6.xsd"); + validator.loadGrammars(schemas); + validator.validate("src/test/resources/test-invalid-application-6.xml"); + } + +} diff --git a/schema-validator/src/test/resources/test-invalid-application-6.xml b/schema-validator/src/test/resources/test-invalid-application-6.xml new file mode 100644 index 00000000..7cac3d07 --- /dev/null +++ b/schema-validator/src/test/resources/test-invalid-application-6.xml @@ -0,0 +1,19 @@ + + + + test-app + true + + + test-web.war + test + + + + +
rere
+ test-ejb.jar +
+ lib +
\ No newline at end of file diff --git a/schema-validator/src/test/resources/test-valid-application-6.xml b/schema-validator/src/test/resources/test-valid-application-6.xml new file mode 100644 index 00000000..54702440 --- /dev/null +++ b/schema-validator/src/test/resources/test-valid-application-6.xml @@ -0,0 +1,18 @@ + + + + test-app + true + + + test-web.war + test + + + + + test-ejb.jar + + lib + \ No newline at end of file From e8e7458b02c1e2a77ab9998121d4d37b85d7c9f9 Mon Sep 17 00:00:00 2001 From: Andrew Lee Rubinger Date: Mon, 12 Nov 2012 17:04:25 -0700 Subject: [PATCH 2/8] [SHRINKDESC-130] Remove extra pom.xml~ file --- schema-validator/pom.xml~ | 159 -------------------------------------- 1 file changed, 159 deletions(-) delete mode 100644 schema-validator/pom.xml~ diff --git a/schema-validator/pom.xml~ b/schema-validator/pom.xml~ deleted file mode 100644 index 8538e122..00000000 --- a/schema-validator/pom.xml~ +++ /dev/null @@ -1,159 +0,0 @@ - - - - - - org.jboss.shrinkwrap.descriptors - shrinkwrap-descriptors-parent - 2.0.0-alpha-5-SNAPSHOT - ../pom.xml - - - - 4.0.0 - - - shrinkwrap-descriptors-metadata-parser - ShrinkWrap Descriptors Metadata Parser - XSD and DTD parser for the ShrinkWrap Descriptors - maven-plugin - - - - - - org.apache.maven.plugins - maven-jar-plugin - - - - true - - - - - - - maven-surefire-plugin - - - ${env.JAVA5_HOME}/bin/java - - once - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.3.2 - - 1.6 - 1.6 - - - - - - - - - - org.jboss.shrinkwrap.descriptors - shrinkwrap-descriptors-api-base - ${project.version} - - - - org.jboss.shrinkwrap.descriptors - shrinkwrap-descriptors-impl-base - ${project.version} - - - - org.jboss.shrinkwrap.descriptors - shrinkwrap-descriptors-test-util - ${project.version} - test - - - - org.apache.commons - commons-lang3 - 3.1 - - - - commons-logging - commons-logging - 1.1 - - - - org.apache.maven - maven-plugin-api - 3.0.3 - - - - net.sf.saxon - saxon - 8.7 - - - - net.sf.saxon - saxon-dom - 8.7 - - - - junit - junit - test - - - - com.sun.codemodel - codemodel - 2.4 - - - - com.sun.xml.dtd-parser - dtd-parser - 1.1 - - - - dom4j - dom4j - 1.6.1 - test - - - - org.apache.maven.plugin-testing - maven-plugin-testing-harness - 2.0-alpha-1 - test - - - - org.codehaus.woodstox - stax2-api - 3.1.1 - test - - - - org.codehaus.woodstox - woodstox-core-asl - 4.1.4 - test - - - - - From 2ebc9c42610d2362bb6b00363cc8b48cefe08c31 Mon Sep 17 00:00:00 2001 From: Andrew Lee Rubinger Date: Mon, 12 Nov 2012 17:57:53 -0700 Subject: [PATCH 3/8] [SHRINKDESC-130] Reduce scope of getResourceStream lookups, enclose in secured fashion, and do not rely upon TCCL. --- .../descriptor/gen/ResourcerReader.java | 62 ------------------- .../schemavalidator/XmlValidator.java | 27 ++++++-- 2 files changed, 21 insertions(+), 68 deletions(-) delete mode 100644 gen/src/main/java/org/jboss/shrinkwrap/descriptor/gen/ResourcerReader.java diff --git a/gen/src/main/java/org/jboss/shrinkwrap/descriptor/gen/ResourcerReader.java b/gen/src/main/java/org/jboss/shrinkwrap/descriptor/gen/ResourcerReader.java deleted file mode 100644 index 500ebc9b..00000000 --- a/gen/src/main/java/org/jboss/shrinkwrap/descriptor/gen/ResourcerReader.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011, Red Hat Middleware LLC, and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * 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.jboss.shrinkwrap.descriptor.gen; - -import java.io.InputStream; - - -/** - * This class is able to returns input streams. - * - * @author Ralf Battenfeld - */ -public class ResourcerReader { - - - /** - * Returns a resource as input stream. - * @param resourceName - * @return if found, an input stream, otherwise null. - */ - public static InputStream getResourceAsStream(final String resourceName) { - if (resourceName == null) { - throw new IllegalArgumentException("Resource name cannot be null!"); - } - - return findResourceStream("xsd/" + resourceName); - } - - //-----------------------------------------------------------------------|| - //-- Private Methods ----------------------------------------------------|| - //-----------------------------------------------------------------------|| - - private static InputStream findResourceStream(final String ... resourceNames) { - InputStream inputStream = null; - for (String resource: resourceNames) { - inputStream = Thread.currentThread() - .getContextClassLoader() - .getResourceAsStream(resource); - - if (inputStream != null) { - break; - } - } - - return inputStream; - } -} diff --git a/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java b/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java index 96907efd..e7bbe1c6 100644 --- a/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java +++ b/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java @@ -21,6 +21,8 @@ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.List; import javax.xml.transform.stream.StreamSource; @@ -38,7 +40,6 @@ import org.apache.xerces.xni.parser.XMLInputSource; import org.apache.xerces.xni.parser.XMLParseException; import org.apache.xerces.xni.parser.XMLParserConfiguration; -import org.jboss.shrinkwrap.descriptor.gen.ResourcerReader; /** @@ -194,7 +195,7 @@ public void setErrorHandler(final XMLErrorHandler errorHandler) { */ public void loadGrammar(final String schema) throws XNIException, IOException { if (isResourceCandidate(schema)) { - final InputStream inputStream = ResourcerReader.getResourceAsStream(schema); + final InputStream inputStream = getClassLoaderForClass(this.getClass()).getResourceAsStream(schema); if (inputStream != null) { XMLInputSource xmlInputStream = new XMLInputSource(null, schema, null, inputStream, null); @@ -338,9 +339,7 @@ public XMLInputSource resolveEntity(final XMLResourceIdentifier resourceIdentifi InputStream inputStream = null; if (resourceIdentifier.getExpandedSystemId().equals("http://www.w3.org/2001/xml.xsd")) { - inputStream = Thread.currentThread() - .getContextClassLoader() - .getResourceAsStream("META-INF/2001/xml.xsd"); + inputStream = getClassLoaderForClass(this.getClass()).getResourceAsStream("META-INF/2001/xml.xsd"); } else { @@ -367,7 +366,7 @@ private boolean isResourceCandidate(final String resourceName) { private InputStream getInputStream(final XMLResourceIdentifier resourceIdentifier) throws MalformedURLException { final File url = new File(resourceIdentifier.getExpandedSystemId()); - return ResourcerReader.getResourceAsStream(url.getName()); + return getClassLoaderForClass(this.getClass()).getResourceAsStream(url.getName()); } /** @@ -393,4 +392,20 @@ public void fatalError(String domain, String key, throw new XNIException(exception); } } + + + private static ClassLoader getClassLoaderForClass(final Class clazz){ + assert clazz!=null:"Class must be specified"; + if(System.getSecurityManager()==null){ + return clazz.getClassLoader(); + } + else{ + return AccessController.doPrivileged(new PrivilegedAction(){ + + @Override + public ClassLoader run() { + return clazz.getClassLoader(); + }}); + } + } } From a796a0608e80fd3199bd44f233a068abce761058 Mon Sep 17 00:00:00 2001 From: Andrew Lee Rubinger Date: Mon, 12 Nov 2012 17:58:32 -0700 Subject: [PATCH 4/8] [SHRINKDESC-130] Remove extra import --- .../descriptor/schemavalidator/ValidatorEvaluationTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/ValidatorEvaluationTest.java b/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/ValidatorEvaluationTest.java index 2bb8a242..0ab2552d 100644 --- a/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/ValidatorEvaluationTest.java +++ b/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/ValidatorEvaluationTest.java @@ -9,7 +9,6 @@ import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.Reader; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; @@ -43,8 +42,6 @@ import org.w3c.dom.ls.LSInput; import org.w3c.dom.ls.LSResourceResolver; -import com.sun.msv.grammar.xmlschema.XMLSchemaGrammar; - public class ValidatorEvaluationTest { /** Property identifier: symbol table. */ From 4e79e90bda9660ad533683fef8489b9a002f28c1 Mon Sep 17 00:00:00 2001 From: Ralf Battenfeld Date: Fri, 16 Nov 2012 18:40:43 +0100 Subject: [PATCH 5/8] [SHRINKDESC-130] Changed resource path to absolute path --- .../schemavalidator/XmlValidator.java | 50 ++++++++++++++----- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java b/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java index e7bbe1c6..19d8dd12 100644 --- a/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java +++ b/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java @@ -49,6 +49,7 @@ * The implementation is based on the xerces xni library. * * @author Ralf Battenfeld + * @author Andrew Lee Rubinger */ public class XmlValidator { @@ -94,8 +95,7 @@ public XmlValidator(final SchemaType type) { if (schemaType == SchemaType.DTD) { preparser.registerPreparser(XMLGrammarDescription.XML_DTD, null); - } - else { + } else { preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null); } @@ -141,8 +141,7 @@ public XmlValidator(final SchemaType type, if (schemaType == SchemaType.DTD) { preparser.registerPreparser(XMLGrammarDescription.XML_DTD, null); - } - else { + } else { preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null); } } @@ -195,7 +194,7 @@ public void setErrorHandler(final XMLErrorHandler errorHandler) { */ public void loadGrammar(final String schema) throws XNIException, IOException { if (isResourceCandidate(schema)) { - final InputStream inputStream = getClassLoaderForClass(this.getClass()).getResourceAsStream(schema); + final InputStream inputStream = getFirstValidInputStream("xsd/" + schema, schema); if (inputStream != null) { XMLInputSource xmlInputStream = new XMLInputSource(null, schema, null, inputStream, null); @@ -260,11 +259,9 @@ private String getGrammarDescription() { String grammarDescr = null; if (schemaType == SchemaType.DTD) { grammarDescr = XMLGrammarDescription.XML_DTD; - } - else if (schemaType == SchemaType.XSD) { + } else if (schemaType == SchemaType.XSD) { grammarDescr = XMLGrammarDescription.XML_SCHEMA; - } - else { + } else { throw new RuntimeException("Not supported: " + schemaType); } return grammarDescr; @@ -338,8 +335,7 @@ public XMLInputSource resolveEntity(final XMLResourceIdentifier resourceIdentifi XMLInputSource xmlInputStream = null; InputStream inputStream = null; if (resourceIdentifier.getExpandedSystemId().equals("http://www.w3.org/2001/xml.xsd")) { - - inputStream = getClassLoaderForClass(this.getClass()).getResourceAsStream("META-INF/2001/xml.xsd"); + inputStream = getFirstValidInputStream("META-INF/2001/xml.xsd", "xml.xsd"); } else { @@ -357,6 +353,14 @@ public XMLInputSource resolveEntity(final XMLResourceIdentifier resourceIdentifi } + /** + * Checks if the given resource name starts with a path seperator. If yes, + * then the resource is a File resource. If not, then + * we assume that the resource can be loaded via resource stream. + * @param resourceName + * @return true, if the resource can be loaded via resources stream, otherwise + * it is assumed that the resource is file on the file system. + */ private boolean isResourceCandidate(final String resourceName) { if (resourceName.indexOf(File.pathSeparator) >= 0) { return false; @@ -366,7 +370,23 @@ private boolean isResourceCandidate(final String resourceName) { private InputStream getInputStream(final XMLResourceIdentifier resourceIdentifier) throws MalformedURLException { final File url = new File(resourceIdentifier.getExpandedSystemId()); - return getClassLoaderForClass(this.getClass()).getResourceAsStream(url.getName()); + return getFirstValidInputStream("xsd/" + url.getName(), url.getName()); + } + + /** + * Returns the first not nullable InputStream. + * @param resources + * @return the first resolvable InputStream, or null if non of the resources are found. + */ + private InputStream getFirstValidInputStream(String ... resources) { + InputStream inputStream = null; + for (String resource: resources) { + inputStream = getClassLoaderForClass(this.getClass()).getResourceAsStream(resource); + if (inputStream != null) { + break; + } + } + return inputStream; } /** @@ -393,7 +413,11 @@ public void fatalError(String domain, String key, } } - + /** + * Returns the right ClassLoader. + * @param clazz + * @return + */ private static ClassLoader getClassLoaderForClass(final Class clazz){ assert clazz!=null:"Class must be specified"; if(System.getSecurityManager()==null){ From 109f7e385a39121b6d29c33047275d6e0db85987 Mon Sep 17 00:00:00 2001 From: Ralf Battenfeld Date: Thu, 22 Nov 2012 11:39:35 +0100 Subject: [PATCH 6/8] [SHRINKDESC-130] Finalized - Integrated into the descriptors --- .gitignore | 3 + api-javaee/pom.xml | 8 + api-jboss/pom.xml | 7 + api-misc/pom.xml | 7 + gen/pom.xml | 31 ++ gen/src/main/resources/xsd/2001/xml.xsd | 287 ++++++++++ impl-base/pom.xml | 6 + metadata-parser-test/pom.xml | 9 + .../test/ironjacamar/DtdParserTest.java | 3 +- .../ironjacamar/ResourceCopyTestCase.java | 25 + metadata-parser/pom.xml | 12 - .../descriptor/metadata/MetadataParser.java | 97 ++-- .../metadata/MetadataParserPath.java | 16 + .../descriptor/metadata/codegen/CodeGen.java | 42 +- .../descriptor/metadata/dom/DomWriter.java | 12 +- .../src/main/resources/META-INF/ddJavaAll.xsl | 63 ++- .../metadata/MetadataParserTest.java | 4 - schema-validator/pom.xml | 45 +- .../schemavalidator/XmlValidator.java | 46 +- .../ValidatorEvaluationTest.java | 511 ------------------ .../schemavalidator/XmlValidatorTestCase.java | 59 +- .../ApplicationDescriptorTestCase.java | 2 + .../test/beans10/BeansDescriptorTestCase.java | 15 +- .../test/resources/test-invalid-beans10.xml | 26 + 24 files changed, 672 insertions(+), 664 deletions(-) create mode 100644 gen/src/main/resources/xsd/2001/xml.xsd create mode 100644 metadata-parser-test/src/teststatic/java/org/jboss/shrinkwrap/descriptor/test/ironjacamar/ResourceCopyTestCase.java delete mode 100644 schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/ValidatorEvaluationTest.java create mode 100644 test/src/test/resources/test-invalid-beans10.xml diff --git a/.gitignore b/.gitignore index 1b15e100..4d5686eb 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ api-javaee/src/ api-misc/src/ impl-jboss/src/main/java/ impl-misc/src/main/java/ +impl-javaee/src/main/resources/META-INF/xsd/ +impl-jboss/src/main/resources/META-INF/xsd/ +impl-misc/src/main/resources/META-INF/xsd/ diff --git a/api-javaee/pom.xml b/api-javaee/pom.xml index d033340d..e76409d7 100644 --- a/api-javaee/pom.xml +++ b/api-javaee/pom.xml @@ -24,12 +24,20 @@ shrinkwrap-descriptors-api-base ${project.version} + org.jboss.shrinkwrap.descriptors shrinkwrap-descriptors-gen ${project.version} provided + + + xerces + xercesImpl + 2.10.0 + provided + diff --git a/api-jboss/pom.xml b/api-jboss/pom.xml index 1c3e7f29..6914f7d3 100644 --- a/api-jboss/pom.xml +++ b/api-jboss/pom.xml @@ -30,6 +30,13 @@ ${project.version} provided + + + xerces + xercesImpl + 2.10.0 + provided + diff --git a/api-misc/pom.xml b/api-misc/pom.xml index 4873eb39..bb2ed1d8 100644 --- a/api-misc/pom.xml +++ b/api-misc/pom.xml @@ -30,6 +30,13 @@ ${project.version} provided + + + xerces + xercesImpl + 2.10.0 + provided + diff --git a/gen/pom.xml b/gen/pom.xml index 2a108a04..480d5718 100644 --- a/gen/pom.xml +++ b/gen/pom.xml @@ -39,6 +39,14 @@ false + + ../impl-javaee/src/main/resources/META-INF/xsd + + **/*.xsd + **/*.dtd + + false + ../impl-javaee/src/test/java @@ -60,6 +68,14 @@ false + + ../impl-jboss/src/main/resources/META-INF/xsd + + **/*.xsd + **/*.dtd + + false + ../impl-jboss/src/test/java @@ -81,6 +97,14 @@ false + + ../impl-misc/src/main/resources/META-INF/xsd + + **/*.xsd + **/*.dtd + + false + ../impl-misc/src/test/java @@ -125,6 +149,7 @@ ${basedir}/../api-javaee/src/main/java ${basedir}/../impl-javaee/src/main/java + ${basedir}/../impl-javaee/src/main/resources/META-INF/xsd @@ -161,6 +186,7 @@ ${basedir}/../api-javaee/src/main/java ${basedir}/../impl-javaee/src/main/java + ${basedir}/../impl-javaee/src/main/resources/META-INF/xsd @@ -250,6 +276,7 @@ ${basedir}/../api-javaee/src/main/java ${basedir}/../impl-javaee/src/main/java + ${basedir}/../impl-javaee/src/main/resources/META-INF/xsd @@ -444,6 +471,7 @@ ${basedir}/../api-javaee/src/main/java ${basedir}/../impl-javaee/src/main/java + ${basedir}/../impl-javaee/src/main/resources/META-INF/xsd @@ -623,6 +651,7 @@ ${basedir}/../api-jboss/src/main/java ${basedir}/../impl-jboss/src/main/java + ${basedir}/../impl-jboss/src/main/resources/META-INF/xsd @@ -696,6 +725,7 @@ ${basedir}/../api-jboss/src/main/java ${basedir}/../impl-jboss/src/main/java + ${basedir}/../impl-jboss/src/main/resources/META-INF/xsd @@ -770,6 +800,7 @@ ${basedir}/../api-misc/src/main/java ${basedir}/../impl-misc/src/main/java ${basedir}/../impl-misc/src/main/resources/META-INF/services + ${basedir}/../impl-misc/src/main/resources/META-INF/xsd diff --git a/gen/src/main/resources/xsd/2001/xml.xsd b/gen/src/main/resources/xsd/2001/xml.xsd new file mode 100644 index 00000000..aea7d0db --- /dev/null +++ b/gen/src/main/resources/xsd/2001/xml.xsd @@ -0,0 +1,287 @@ + + + + + + +
+

About the XML namespace

+ +
+

+ This schema document describes the XML namespace, in a form + suitable for import by other schema documents. +

+

+ See + http://www.w3.org/XML/1998/namespace.html and + + http://www.w3.org/TR/REC-xml for information + about this namespace. +

+

+ Note that local names in this namespace are intended to be + defined only by the World Wide Web Consortium or its subgroups. + The names currently defined in this namespace are listed below. + They should not be used with conflicting semantics by any Working + Group, specification, or document instance. +

+

+ See further below in this document for more information about how to refer to this schema document from your own + XSD schema documents and about the + namespace-versioning policy governing this schema document. +

+
+
+
+
+ + + + +
+ +

lang (as an attribute name)

+

+ denotes an attribute whose value + is a language code for the natural language of the content of + any element; its value is inherited. This name is reserved + by virtue of its definition in the XML specification.

+ +
+
+

Notes

+

+ Attempting to install the relevant ISO 2- and 3-letter + codes as the enumerated possible values is probably never + going to be a realistic possibility. +

+

+ See BCP 47 at + http://www.rfc-editor.org/rfc/bcp/bcp47.txt + and the IANA language subtag registry at + + http://www.iana.org/assignments/language-subtag-registry + for further information. +

+

+ The union allows for the 'un-declaration' of xml:lang with + the empty string. +

+
+
+
+ + + + + + + + + +
+ + + + +
+ +

space (as an attribute name)

+

+ denotes an attribute whose + value is a keyword indicating what whitespace processing + discipline is intended for the content of the element; its + value is inherited. This name is reserved by virtue of its + definition in the XML specification.

+ +
+
+
+ + + + + + +
+ + + +
+ +

base (as an attribute name)

+

+ denotes an attribute whose value + provides a URI to be used as the base for interpreting any + relative URIs in the scope of the element on which it + appears; its value is inherited. This name is reserved + by virtue of its definition in the XML Base specification.

+ +

+ See http://www.w3.org/TR/xmlbase/ + for information about this attribute. +

+
+
+
+
+ + + + +
+ +

id (as an attribute name)

+

+ denotes an attribute whose value + should be interpreted as if declared to be of type ID. + This name is reserved by virtue of its definition in the + xml:id specification.

+ +

+ See http://www.w3.org/TR/xml-id/ + for information about this attribute. +

+
+
+
+
+ + + + + + + + + + +
+ +

Father (in any context at all)

+ +
+

+ denotes Jon Bosak, the chair of + the original XML Working Group. This name is reserved by + the following decision of the W3C XML Plenary and + XML Coordination groups: +

+
+

+ In appreciation for his vision, leadership and + dedication the W3C XML Plenary on this 10th day of + February, 2000, reserves for Jon Bosak in perpetuity + the XML name "xml:Father". +

+
+
+
+
+
+ + + +
+

About this schema document

+ +
+

+ This schema defines attributes and an attribute group suitable + for use by schemas wishing to allow xml:base, + xml:lang, xml:space or + xml:id attributes on elements they define. +

+

+ To enable this, such a schema must import this schema for + the XML namespace, e.g. as follows: +

+
+          <schema . . .>
+           . . .
+           <import namespace="http://www.w3.org/XML/1998/namespace"
+                      schemaLocation="http://www.w3.org/2001/xml.xsd"/>
+     
+

+ or +

+
+           <import namespace="http://www.w3.org/XML/1998/namespace"
+                      schemaLocation="http://www.w3.org/2009/01/xml.xsd"/>
+     
+

+ Subsequently, qualified reference to any of the attributes or the + group defined below will have the desired effect, e.g. +

+
+          <type . . .>
+           . . .
+           <attributeGroup ref="xml:specialAttrs"/>
+     
+

+ will define a type which will schema-validate an instance element + with any of those attributes. +

+
+
+
+
+ + + +
+

Versioning policy for this schema document

+
+

+ In keeping with the XML Schema WG's standard versioning + policy, this schema document will persist at + + http://www.w3.org/2009/01/xml.xsd. +

+

+ At the date of issue it can also be found at + + http://www.w3.org/2001/xml.xsd. +

+

+ The schema document at that URI may however change in the future, + in order to remain compatible with the latest version of XML + Schema itself, or with the XML namespace itself. In other words, + if the XML Schema or XML namespaces change, the version of this + document at + http://www.w3.org/2001/xml.xsd + + will change accordingly; the version at + + http://www.w3.org/2009/01/xml.xsd + + will not change. +

+

+ Previous dated (and unchanging) versions of this schema + document are at: +

+ +
+
+
+
+ +
+ diff --git a/impl-base/pom.xml b/impl-base/pom.xml index 29014c53..911b79b0 100644 --- a/impl-base/pom.xml +++ b/impl-base/pom.xml @@ -25,6 +25,12 @@ ${project.version} + + org.jboss.shrinkwrap.descriptors + shrinkwrap-descriptors-schema-validator + ${project.version} + + junit junit diff --git a/metadata-parser-test/pom.xml b/metadata-parser-test/pom.xml index c866af61..d5e1e437 100644 --- a/metadata-parser-test/pom.xml +++ b/metadata-parser-test/pom.xml @@ -45,6 +45,14 @@
false
+ + ${basedir}/src/main/resources/META-INF/xsd + + **/*.xsd + **/*.dtd + + false +
@@ -63,6 +71,7 @@ ${basedir}/src/main/java ${basedir}/src/main/java ${basedir}/src/main/resources/META-INF/services + ${basedir}/src/main/resources/META-INF/xsd diff --git a/metadata-parser-test/src/teststatic/java/org/jboss/shrinkwrap/descriptor/test/ironjacamar/DtdParserTest.java b/metadata-parser-test/src/teststatic/java/org/jboss/shrinkwrap/descriptor/test/ironjacamar/DtdParserTest.java index 1552786c..3bac7f21 100644 --- a/metadata-parser-test/src/teststatic/java/org/jboss/shrinkwrap/descriptor/test/ironjacamar/DtdParserTest.java +++ b/metadata-parser-test/src/teststatic/java/org/jboss/shrinkwrap/descriptor/test/ironjacamar/DtdParserTest.java @@ -6,6 +6,7 @@ import org.jboss.shrinkwrap.descriptor.metadata.MetadataDescriptor; import org.jboss.shrinkwrap.descriptor.metadata.MetadataElement; import org.jboss.shrinkwrap.descriptor.metadata.MetadataParser; +import org.jboss.shrinkwrap.descriptor.metadata.MetadataParserPath; import org.jboss.shrinkwrap.descriptor.metadata.MetadataUtil; import org.jboss.shrinkwrap.descriptor.metadata.dom.DomWriter; import org.junit.Ignore; @@ -108,7 +109,7 @@ public void testConnector() throws Exception metadata.getMetadataDescriptorList().add(metadataDescriptor); metadata.preResolveDataTypes(); new MetadataUtil().log(metadata); - new DomWriter().write(metadata, "/tmp/connector_1_0.xml", null); + new DomWriter().write(metadata, "/tmp/connector_1_0.xml", null, new MetadataParserPath()); // // if (verbose) { // new MetadataUtil().print(metadata); diff --git a/metadata-parser-test/src/teststatic/java/org/jboss/shrinkwrap/descriptor/test/ironjacamar/ResourceCopyTestCase.java b/metadata-parser-test/src/teststatic/java/org/jboss/shrinkwrap/descriptor/test/ironjacamar/ResourceCopyTestCase.java new file mode 100644 index 00000000..6446a539 --- /dev/null +++ b/metadata-parser-test/src/teststatic/java/org/jboss/shrinkwrap/descriptor/test/ironjacamar/ResourceCopyTestCase.java @@ -0,0 +1,25 @@ +package org.jboss.shrinkwrap.descriptor.test.ironjacamar; + +import java.io.File; + +import junit.framework.Assert; + +import org.junit.Test; + + +public class ResourceCopyTestCase +{ + @Test + public void testResourceCopy() throws Exception { + final File connectorDtdFile = new File("src/main/resources/META-INF/xsd/connector_1_0.dtd"); + final File dataSourcesXsdFile = new File("src/main/resources/META-INF/xsd/datasources_1_0.xsd"); + final File ironJacamarXsdFile = new File("src/main/resources/META-INF/xsd/ironjacamar_1_0.xsd"); + final File resourceAdapaterXsdFile = new File("src/main/resources/META-INF/xsd/resource-adapters_1_0.xsd"); + + Assert.assertTrue(connectorDtdFile.exists()); + Assert.assertTrue(dataSourcesXsdFile.exists()); + Assert.assertTrue(ironJacamarXsdFile.exists()); + Assert.assertTrue(resourceAdapaterXsdFile.exists()); + } + +} diff --git a/metadata-parser/pom.xml b/metadata-parser/pom.xml index 9ba6fd43..d626c50f 100644 --- a/metadata-parser/pom.xml +++ b/metadata-parser/pom.xml @@ -58,18 +58,6 @@ - - org.jboss.shrinkwrap.descriptors - shrinkwrap-descriptors-api-base - ${project.version} - - - - org.jboss.shrinkwrap.descriptors - shrinkwrap-descriptors-impl-base - ${project.version} - - org.jboss.shrinkwrap.descriptors shrinkwrap-descriptors-test-util diff --git a/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/MetadataParser.java b/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/MetadataParser.java index 7fd5fa00..408ec862 100644 --- a/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/MetadataParser.java +++ b/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/MetadataParser.java @@ -18,9 +18,12 @@ package org.jboss.shrinkwrap.descriptor.metadata; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; +import java.nio.channels.FileChannel; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -74,8 +77,7 @@ public Metadata getMetadata() * @throws Exception */ @SuppressWarnings("unchecked") -public void parse(final MetadataParserPath path, final List confList, final List javadocTags, final boolean verbose) throws Exception - { + public void parse(final MetadataParserPath path, final List confList, final List javadocTags, final boolean verbose) throws Exception { checkArguments(path, confList); pathToMetadata = createTempFile(verbose); @@ -84,8 +86,7 @@ public void parse(final MetadataParserPath path, final List confList, final L log.fine("Path to temporary metadata file: " + pathToMetadata); } - for (int i = 0; i < confList.size(); i++) - { + for (int i = 0; i < confList.size(); i++) { final MetadataParserConfiguration metadataConf = (MetadataParserConfiguration) confList.get(i); metadata.setCurrentNamespace(metadataConf.getNameSpace()); @@ -104,25 +105,22 @@ public void parse(final MetadataParserPath path, final List confList, final L metadataDescriptor.setGenerateClasses(metadataConf.generateClasses); metadata.getMetadataDescriptorList().add(metadataDescriptor); - if(log.isLoggable(Level.FINE)){ + if (log.isLoggable(Level.FINE)){ log.fine(metadataConf.getPathToXsd()); } - if (metadataConf.getPathToXsd().endsWith(".dtd")) - { + if (metadataConf.getPathToXsd().endsWith(".dtd")) { final InputSource in = new InputSource(new FileReader(metadataConf.getPathToXsd())); final MetadataDtdEventListener dtdEventListener = new MetadataDtdEventListener(metadata, verbose); final DTDParser parser = new DTDParser(); parser.setDtdHandler(dtdEventListener); parser.parse(in); - } - else - { + } else { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder loader = factory.newDocumentBuilder(); final Document document = loader.parse(metadataConf.getPathToXsd()); - if(log.isLoggable(Level.FINE)){ + if (log.isLoggable(Level.FINE)) { log.fine(document.getDocumentURI()); } @@ -134,7 +132,7 @@ public void parse(final MetadataParserPath path, final List confList, final L filterChain.traverseAndFilter(walker, "", metadata, sb); - if(sb!=null){ + if (sb!=null) { log.info(sb.toString()); } } @@ -146,19 +144,21 @@ public void parse(final MetadataParserPath path, final List confList, final L */ metadata.preResolveDataTypes(); - if (pathToMetadata != null) - { - new DomWriter().write(metadata, pathToMetadata, (List)javadocTags); + if (pathToMetadata != null) { + new DomWriter().write(metadata, pathToMetadata, (List)javadocTags, path); } - if (verbose) - { + if (verbose) { new MetadataUtil().log(metadata); } - if (path.getPathToApi() != null && path.getPathToImpl() != null) - { + if (path.getPathToApi() != null && path.getPathToImpl() != null) { generateCode(path, verbose); + if (path.getPathToResources() != null) { + for (MetadataDescriptor metadataDescriptor: metadata.getMetadataDescriptorList()) { + copyFile(new File(metadataDescriptor.getSchemaName()), path.getPathToResources()); + } + } } } @@ -166,8 +166,8 @@ public void parse(final MetadataParserPath path, final List confList, final L * Generates source code by applying the ddJavaAll.xsl XSLT extracted from the resource stream. * @throws TransformerException */ - public void generateCode(final MetadataParserPath path, final boolean verbose) throws TransformerException - { + public void generateCode(final MetadataParserPath path, final boolean verbose) throws TransformerException { + /** initialize the map which will overwrite global parameters as defined in metadata.xsl/ddJava.xsl */ final Map xsltParameters = new HashMap(); xsltParameters.put("gOutputFolder", path.getPathToImpl()); @@ -189,8 +189,7 @@ public void generateCode(final MetadataParserPath path, final boolean verbose) t * method is executed successfully. * @return full path of the generated metadata XML file. */ - public String getPathToMetadataFile() - { + public String getPathToMetadataFile() { return pathToMetadata; } @@ -203,12 +202,10 @@ public String getPathToMetadataFile() * @return absolute path of the temporary file. * @throws IOException */ - private String createTempFile(final boolean verbose) throws IOException - { + private String createTempFile(final boolean verbose) throws IOException { final File tempFile = File.createTempFile("tempMetadata", ".xml"); - if (!verbose) - { + if (!verbose) { tempFile.deleteOnExit(); } @@ -220,17 +217,45 @@ private String createTempFile(final boolean verbose) throws IOException * @param path * @param confList */ - private void checkArguments(final MetadataParserPath path, final List confList) - { + private void checkArguments(final MetadataParserPath path, final List confList) { if (path == null) { throw new IllegalArgumentException("Invalid configuration. The 'path' element missing!"); - } - else if (confList == null) { + } else if (confList == null) { throw new IllegalArgumentException("Invalid configuration. At least one 'descriptor' element has to be defined!"); - } - else if (confList.isEmpty()) { + } else if (confList.isEmpty()) { throw new IllegalArgumentException("Invalid configuration. At least one 'descriptor' element has to be defined!"); - } - - } + } + } + + + /** + * Copies a file by using NIO. + * @param sourceFile the source file. + * @param destFile the target file location. + * @throws IOException + */ + private void copyFile(File sourceFile, String destPath) throws IOException { + final File destFolder = new File(destPath); + if(!destFolder.exists()) { + destFolder.mkdir(); + } + + FileChannel source = null; + FileChannel destination = null; + + try { + final File destFile = new File(destPath + File.separatorChar + sourceFile.getName()); + source = new FileInputStream(sourceFile).getChannel(); + destination = new FileOutputStream(destFile).getChannel(); + destination.transferFrom(source, 0, source.size()); + } + finally { + if (source != null) { + source.close(); + } + if (destination != null) { + destination.close(); + } + } + } } diff --git a/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/MetadataParserPath.java b/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/MetadataParserPath.java index 2cc7ad94..2bf686c8 100644 --- a/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/MetadataParserPath.java +++ b/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/MetadataParserPath.java @@ -50,6 +50,11 @@ public class MetadataParserPath */ protected String pathToServices; + /** + * Full path to the resource folder in which schemas are copied. + * + */ + protected String pathToResources; public String getPathToApi() @@ -91,4 +96,15 @@ public void setPathToServices(final String pathToServices) { this.pathToServices = pathToServices; } + + public String getPathToResources() + { + return pathToResources; + } + + public void setPathToResources(final String pathToResources) + { + this.pathToResources = pathToResources; + } + } diff --git a/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/codegen/CodeGen.java b/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/codegen/CodeGen.java index a1d7e8e2..94c7fc0a 100644 --- a/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/codegen/CodeGen.java +++ b/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/codegen/CodeGen.java @@ -19,11 +19,9 @@ import java.io.File; import java.io.IOException; -import java.util.List; import java.util.Map; import org.apache.commons.lang3.text.WordUtils; -import org.jboss.shrinkwrap.descriptor.api.Child; import org.jboss.shrinkwrap.descriptor.metadata.Metadata; import org.jboss.shrinkwrap.descriptor.metadata.MetadataElement; import org.jboss.shrinkwrap.descriptor.metadata.MetadataEnum; @@ -37,8 +35,6 @@ import com.sun.codemodel.JDocComment; import com.sun.codemodel.JExpr; import com.sun.codemodel.JMethod; -import com.sun.codemodel.JType; -import com.sun.codemodel.JTypeVar; /** * Experimental class which may replaced the current xslt based java code generation step. @@ -116,25 +112,25 @@ public void generateInferfaces() throws JClassAlreadyExistsException, IOExceptio // JTypeVar typeVat = new JTypeVar(cm, "T"); // final JDefinedClass dcChild = cm._class(Child.class.getCanonicalName(), ClassType.INTERFACE); - final JClass child = cm.ref(Child.class); - if (child.isParameterized()) - { - List types = child.getTypeParameters(); - for (JClass jclass: types) - { - jclass.toString(); - } - } - - dc._extends(childInterface); - JTypeVar type = dc.generify("T"); - child.narrow(type); - JType erasureType = dc.erasure(); - List types = dc.getTypeParameters(); - for (JClass jclass: types) - { - child.narrow(erasureType); - } +// final JClass child = cm.ref(Child.class); +// if (child.isParameterized()) +// { +// List types = child.getTypeParameters(); +// for (JClass jclass: types) +// { +// jclass.toString(); +// } +// } +// +// dc._extends(childInterface); +// JTypeVar type = dc.generify("T"); +// child.narrow(type); +// JType erasureType = dc.erasure(); +// List types = dc.getTypeParameters(); +// for (JClass jclass: types) +// { +// child.narrow(erasureType); +// } final JDocComment javaDocComment = dc.javadoc(); final Map part = javaDocComment.addXdoclet("author"); diff --git a/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/dom/DomWriter.java b/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/dom/DomWriter.java index 9fedda1b..e8db2307 100644 --- a/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/dom/DomWriter.java +++ b/metadata-parser/src/main/java/org/jboss/shrinkwrap/descriptor/metadata/dom/DomWriter.java @@ -39,6 +39,7 @@ import org.jboss.shrinkwrap.descriptor.metadata.MetadataEnum; import org.jboss.shrinkwrap.descriptor.metadata.MetadataItem; import org.jboss.shrinkwrap.descriptor.metadata.MetadataJavaDoc; +import org.jboss.shrinkwrap.descriptor.metadata.MetadataParserPath; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -61,8 +62,8 @@ public class DomWriter * @param metadata * @param pathToMetadata */ - public void write(final Metadata metadata, final String pathToMetadata, final List javadocTags) - { + public void write(final Metadata metadata, final String pathToMetadata, + final List javadocTags, final MetadataParserPath path) { try { final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); @@ -130,6 +131,13 @@ public void write(final Metadata metadata, final String pathToMetadata, final Li final Attr generateClassImpl = doc.createAttribute("generateClass"); generateClassImpl.setValue(Boolean.toString(descriptor.isGenerateClasses())); packageImpl.setAttributeNode(generateClassImpl); + + if (path.getPathToResources() != null && !path.getPathToResources().isEmpty()) { + final Attr pathToResource = doc.createAttribute("pathToResource"); + pathToResource.setValue(path.getPathToResources()); + packageImpl.setAttributeNode(pathToResource); + } + packages.appendChild(packageImpl); } diff --git a/metadata-parser/src/main/resources/META-INF/ddJavaAll.xsl b/metadata-parser/src/main/resources/META-INF/ddJavaAll.xsl index 389a1887..e52db194 100644 --- a/metadata-parser/src/main/resources/META-INF/ddJavaAll.xsl +++ b/metadata-parser/src/main/resources/META-INF/ddJavaAll.xsl @@ -355,8 +355,8 @@ - - + + @@ -374,12 +374,17 @@ import org.jboss.shrinkwrap.descriptor.api.Descriptor; import org.jboss.shrinkwrap.descriptor.api.DescriptorNamespace; - + import org.apache.xerces.xni.XNIException; + import java.io.IOException; + { + + + @@ -458,11 +463,9 @@ + - - - + @@ -485,8 +488,14 @@ import org.jboss.shrinkwrap.descriptor.spi.node.NodeDescriptorImplBase; import org.jboss.shrinkwrap.descriptor.impl.base.XMLDate; import org.jboss.shrinkwrap.descriptor.spi.node.Node; + import org.apache.xerces.xni.XNIException; + import org.jboss.shrinkwrap.descriptor.schemavalidator.XmlValidator; + import org.jboss.shrinkwrap.descriptor.schemavalidator.XmlValidator.SchemaType; + import javax.xml.transform.stream.StreamSource; + import java.io.StringReader; + import java.io.IOException; - + @@ -501,6 +510,9 @@ + + + @@ -2663,6 +2675,41 @@ } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/metadata-parser/src/test/java/org/jboss/shrinkwrap/descriptor/metadata/MetadataParserTest.java b/metadata-parser/src/test/java/org/jboss/shrinkwrap/descriptor/metadata/MetadataParserTest.java index 5078759e..cb15aa97 100644 --- a/metadata-parser/src/test/java/org/jboss/shrinkwrap/descriptor/metadata/MetadataParserTest.java +++ b/metadata-parser/src/test/java/org/jboss/shrinkwrap/descriptor/metadata/MetadataParserTest.java @@ -1,7 +1,6 @@ package org.jboss.shrinkwrap.descriptor.metadata; import java.io.BufferedReader; -import java.io.File; import java.io.FileReader; import java.net.URL; import java.util.ArrayList; @@ -10,11 +9,8 @@ import junit.framework.Assert; -import org.custommonkey.xmlunit.Difference; -import org.custommonkey.xmlunit.DifferenceListener; import org.jboss.shrinkwrap.descriptor.test.util.XmlAssert; import org.junit.Test; -import org.w3c.dom.Node; public class MetadataParserTest { diff --git a/schema-validator/pom.xml b/schema-validator/pom.xml index d1045c79..66cdc061 100644 --- a/schema-validator/pom.xml +++ b/schema-validator/pom.xml @@ -57,13 +57,14 @@ - + - org.jboss.shrinkwrap.descriptors - shrinkwrap-descriptors-gen - ${project.version} - - + org.jboss.shrinkwrap.descriptors + shrinkwrap-descriptors-gen + ${project.version} + test + + commons-logging commons-logging @@ -76,44 +77,12 @@ test - - - - - - - - - - - - - - - msv - msv - 20050913 - - - - msv - relaxngDatatype - 20030225 - - - - com.sun.msv.datatype.xsd - xsdlib - 20060615 - - xerces xercesImpl 2.10.0 - diff --git a/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java b/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java index 19d8dd12..dcd96503 100644 --- a/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java +++ b/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java @@ -20,13 +20,13 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.StringReader; import java.net.MalformedURLException; import java.security.AccessController; import java.security.PrivilegedAction; +import java.util.ArrayList; import java.util.List; -import javax.xml.transform.stream.StreamSource; - import org.apache.xerces.parsers.XIncludeAwareParserConfiguration; import org.apache.xerces.parsers.XMLGrammarPreparser; import org.apache.xerces.util.SymbolTable; @@ -59,6 +59,16 @@ public enum SchemaType { XSD } + /** contains all search locations used by getResourceAsStream() */ + private static List searchLocationList = new ArrayList(); + + static { + searchLocationList.add("META-INF/xsd/"); + searchLocationList.add("xsd/"); + searchLocationList.add(""); + searchLocationList.add("META-INF/2001/"); + } + /** schema type we have to know for loading the grammars */ private final SchemaType schemaType; @@ -186,6 +196,10 @@ public void setErrorHandler(final XMLErrorHandler errorHandler) { this.errorHandler = errorHandler; } + public void addSearchLocation(final String location) { + searchLocationList.add(location); + } + /** * Loads the grammar. * @param schema the grammar to be loaded. @@ -194,7 +208,7 @@ public void setErrorHandler(final XMLErrorHandler errorHandler) { */ public void loadGrammar(final String schema) throws XNIException, IOException { if (isResourceCandidate(schema)) { - final InputStream inputStream = getFirstValidInputStream("xsd/" + schema, schema); + final InputStream inputStream = getFirstValidInputStream(schema); if (inputStream != null) { XMLInputSource xmlInputStream = new XMLInputSource(null, schema, null, inputStream, null); @@ -222,20 +236,21 @@ public void loadGrammars(final List schemaList) throws XNIException, IOE //-- Public Validator Methods ------------------------------------------|| //-----------------------------------------------------------------------|| - public void validate(final String pathToFile) throws XNIException, IOException { + public void validateFile(final String pathToFile) throws XNIException, IOException { checkGrammarPoolSize(); final XMLParserConfiguration parserConfiguration = getParserConfiguration(); parserConfiguration.parse(stringToXIS(pathToFile)); } - public void validate(final File xmlFile) throws XNIException, IOException { + public void validateFile(final File xmlFile) throws XNIException, IOException { checkGrammarPoolSize(); - validate(xmlFile.getAbsolutePath()); + validateFile(xmlFile.getAbsolutePath()); } - public void validate(final StreamSource xmlSource) throws XNIException, IOException { + public void validateContent(final String xmlString) throws XNIException, IOException { checkGrammarPoolSize(); - throw new UnsupportedOperationException(); // TODO + final XMLParserConfiguration parserConfiguration = getParserConfiguration(); + parserConfiguration.parse(new XMLInputSource(null, null, null, new StringReader(xmlString), null)); } //-----------------------------------------------------------------------|| @@ -335,11 +350,10 @@ public XMLInputSource resolveEntity(final XMLResourceIdentifier resourceIdentifi XMLInputSource xmlInputStream = null; InputStream inputStream = null; if (resourceIdentifier.getExpandedSystemId().equals("http://www.w3.org/2001/xml.xsd")) { - inputStream = getFirstValidInputStream("META-INF/2001/xml.xsd", "xml.xsd"); - - } else { + inputStream = getFirstValidInputStream("xml.xsd"); - // try to get the resource from the gen package + } else { + // try to get the resource from a resource inputStream = getInputStream(resourceIdentifier); } @@ -370,7 +384,7 @@ private boolean isResourceCandidate(final String resourceName) { private InputStream getInputStream(final XMLResourceIdentifier resourceIdentifier) throws MalformedURLException { final File url = new File(resourceIdentifier.getExpandedSystemId()); - return getFirstValidInputStream("xsd/" + url.getName(), url.getName()); + return getFirstValidInputStream(url.getName()); } /** @@ -378,10 +392,10 @@ private InputStream getInputStream(final XMLResourceIdentifier resourceIdentifie * @param resources * @return the first resolvable InputStream, or null if non of the resources are found. */ - private InputStream getFirstValidInputStream(String ... resources) { + private InputStream getFirstValidInputStream(final String schema) { InputStream inputStream = null; - for (String resource: resources) { - inputStream = getClassLoaderForClass(this.getClass()).getResourceAsStream(resource); + for (String location: searchLocationList) { + inputStream = getClassLoaderForClass(this.getClass()).getResourceAsStream(location + schema); if (inputStream != null) { break; } diff --git a/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/ValidatorEvaluationTest.java b/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/ValidatorEvaluationTest.java deleted file mode 100644 index 0ab2552d..00000000 --- a/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/ValidatorEvaluationTest.java +++ /dev/null @@ -1,511 +0,0 @@ -package org.jboss.shrinkwrap.descriptor.schemavalidator; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.ObjectOutput; -import java.io.ObjectOutputStream; -import java.io.Reader; -import java.util.ArrayList; -import java.util.List; - -import javax.xml.XMLConstants; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.stream.XMLStreamException; -import javax.xml.transform.Source; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamSource; -import javax.xml.validation.Schema; -import javax.xml.validation.SchemaFactory; -import javax.xml.validation.Validator; - -import org.apache.xerces.impl.Constants; -import org.apache.xerces.parsers.XIncludeAwareParserConfiguration; -import org.apache.xerces.parsers.XMLGrammarPreparser; -import org.apache.xerces.util.SymbolTable; -import org.apache.xerces.util.XMLGrammarPoolImpl; -import org.apache.xerces.xni.XMLResourceIdentifier; -import org.apache.xerces.xni.XNIException; -import org.apache.xerces.xni.grammars.Grammar; -import org.apache.xerces.xni.grammars.XMLGrammarDescription; -import org.apache.xerces.xni.parser.XMLEntityResolver; -import org.apache.xerces.xni.parser.XMLErrorHandler; -import org.apache.xerces.xni.parser.XMLInputSource; -import org.apache.xerces.xni.parser.XMLParseException; -import org.apache.xerces.xni.parser.XMLParserConfiguration; -import org.junit.Test; -import org.w3c.dom.Document; -import org.w3c.dom.ls.LSInput; -import org.w3c.dom.ls.LSResourceResolver; - -public class ValidatorEvaluationTest { - - /** Property identifier: symbol table. */ - public static final String SYMBOL_TABLE = Constants.XERCES_PROPERTY_PREFIX - + Constants.SYMBOL_TABLE_PROPERTY; - - /** Property identifier: grammar pool. */ - public static final String GRAMMAR_POOL = Constants.XERCES_PROPERTY_PREFIX - + Constants.XMLGRAMMAR_POOL_PROPERTY; - - // feature ids - - /** Namespaces feature id (http://xml.org/sax/features/namespaces). */ - protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces"; - - /** Validation feature id (http://xml.org/sax/features/validation). */ - protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation"; - - /** - * Schema validation feature id - * (http://apache.org/xml/features/validation/schema). - */ - protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema"; - - /** - * Schema full checking feature id - * (http://apache.org/xml/features/validation/schema-full-checking). - */ - protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking"; - - /** - * Honour all schema locations feature id - * (http://apache.org/xml/features/honour-all-schemaLocations). - */ - protected static final String HONOUR_ALL_SCHEMA_LOCATIONS_ID = "http://apache.org/xml/features/honour-all-schemaLocations"; - - // a larg(ish) prime to use for a symbol table to be shared - // among - // potentially man parsers. Start one as close to 2K (20 - // times larger than normal) and see what happens... - public static final int BIG_PRIME = 2039; - - // default settings - - /** Default Schema full checking support (false). */ - protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false; - - /** Default honour all schema locations (false). */ - protected static final boolean DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS = false; - - - static final String JAXP_SCHEMA_LANGUAGE - = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; - static final String W3C_XML_SCHEMA - = "http://www.w3.org/2001/XMLSchema"; - static final String JAXP_SCHEMA_SOURCE - = "http://java.sun.com/xml/jaxp/properties/schemaSource"; - - @Test - public void testJaxpValidator() throws Exception { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); - factory.setValidating(true); - - factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); - - // Set the schema file - factory.setAttribute(JAXP_SCHEMA_SOURCE, new File("../gen/src/main/resources/xsd/application_6.xsd")); - - try { - DocumentBuilder parser = factory.newDocumentBuilder(); - - // Parse the file. If errors found, they will be printed. - parser.parse("../test/src/test/resources/test-gen-application6.xml"); - parser.getSchema(); - - } catch (Exception e) { - e.printStackTrace(); - } - } - - - @Test - public void testXMLValidator() throws Exception { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); - - // read the XML file - DocumentBuilder builder = factory.newDocumentBuilder(); - Document doc = builder.parse(new File("../test/src/test/resources/test-gen-application6.xml")); - - // create a SchemaFactory and a Schema - SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); - Source schemaFile = new StreamSource(new File("../gen/src/main/resources/xsd/application_6.xsd")); - Schema schema = schemaFactory.newSchema(schemaFile); - - // create a Validator object and validate the XML file - Validator validator = schema.newValidator(); - validator.validate(new DOMSource(doc)); - -// XStream xstream = new XStream(); -// String xml = xstream.toXML(validator); -// -// Object newNamedMap = (Object)xstream.fromXML(xml); -// -// SchemaGrammarSerializer.serialize(serializableSchema, "/tmp/schema.ser"); -// final SerializableSchema newSchema = SchemaGrammarSerializer.deserialize(SerializableSchema.class, "/tmp/schema.ser"); - } - - @Test - public void testResolver() throws XMLStreamException { -// -// final XMLMapper mapper = XMLMapper.Factory.create(); -// mapper.parseDocument(null, null); -// -// try { -// DOMParser parser = new DOMParser(); -//// parser. -// parser.setFeature("http://xml.org/sax/features/validation", true); -// parser.setProperty( -// "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", -// "../gen/src/main/resources/xsd/application_6.xsd"); -// // ErrorChecker errors = new ErrorChecker(); -// // parser.setErrorHandler(errors); -// parser.parse("../test/src/test/resources/test-gen-application6.xml"); -// } catch (Exception e) { -// System.out.print("Problem parsing the file."); -// } - - // - // SchemaFactory schemaFactory = - // SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); - // - // schemaFactory.setResourceResolver(new ClasspathResourceResolver()); - } - - - @Test - public void testXerces() throws Exception { - final boolean externalDTDs = false; - final SymbolTable sym = new SymbolTable(BIG_PRIME); - final XMLGrammarPreparser preparser = new XMLGrammarPreparser(sym); - final XMLGrammarPoolImpl grammarPool = new XMLGrammarPoolImpl(); - String grammarDescr = null; - - boolean isDTD = false; - if (externalDTDs) { - preparser.registerPreparser(XMLGrammarDescription.XML_DTD, null); - grammarDescr = XMLGrammarDescription.XML_DTD; - isDTD = true; - } - else { - preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null); - grammarDescr = XMLGrammarDescription.XML_SCHEMA; - isDTD = false; - } - - final List schemas = new ArrayList(); -// schemas.add("../gen/src/main/resources/xsd/2001/xml.xsd"); -// schemas.add("../gen/src/main/resources/xsd/javaee_6.xsd"); -// schemas.add("../gen/src/main/resources/xsd/javaee_web_services_client_1_3.xsd"); - schemas.add("../gen/src/main/resources/xsd/application_6.xsd"); - - final List xmlFiles = new ArrayList(); - xmlFiles.add("src/test/resources/test-invalid-application-6.xml"); - - boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING; - boolean honourAllSchemaLocations = DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS; - - preparser.setProperty(GRAMMAR_POOL, grammarPool); - preparser.setFeature(NAMESPACES_FEATURE_ID, true); - preparser.setFeature(VALIDATION_FEATURE_ID, true); - - // note we can set schema features just in case... - preparser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true); - preparser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking); - preparser.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaLocations); - - preparser.setEntityResolver(new XMLEntityResolver() { - - @Override - public XMLInputSource resolveEntity( - XMLResourceIdentifier resourceIdentifier) - throws XNIException, IOException { - - if (resourceIdentifier.getExpandedSystemId().equals("http://www.w3.org/2001/xml.xsd")) { - return stringToXIS("../gen/src/main/resources/xsd/2001/xml.xsd"); - } - return null; - } - - }); - - // parse the grammar... - - try { - for (String schema: schemas) { - final Grammar g = preparser.preparseGrammar(grammarDescr, stringToXIS(schema)); - } - } catch (Exception e) { - e.printStackTrace(); - System.exit(1); - } - - XMLParserConfiguration parserConfiguration = null; - - // Now we have a grammar pool and a SymbolTable; just - // build a configuration and we're on our way! - if (parserConfiguration == null) { - parserConfiguration = new XIncludeAwareParserConfiguration(sym, grammarPool); - } else { - // set GrammarPool and SymbolTable... - parserConfiguration.setProperty(SYMBOL_TABLE, sym); - parserConfiguration.setProperty(GRAMMAR_POOL, grammarPool); - } - - // now must reset features, unfortunately: - try { - parserConfiguration.setFeature(NAMESPACES_FEATURE_ID, true); - parserConfiguration.setFeature(VALIDATION_FEATURE_ID, true); - parserConfiguration.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true); - parserConfiguration.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking); - parserConfiguration.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaLocations); - } - catch (Exception e) { - e.printStackTrace(); - System.exit(1); - } - - parserConfiguration.setErrorHandler(new XMLErrorHandler() { - - @Override - public void warning(String domain, String key, - XMLParseException exception) throws XNIException { - - } - - @Override - public void error(String domain, String key, - XMLParseException exception) throws XNIException { - throw new XNIException(exception); - } - - @Override - public void fatalError(String domain, String key, - XMLParseException exception) throws XNIException { - throw new XNIException(exception); - } - - }); - - - // then for each instance file, try to validate it - if (xmlFiles != null) { -// try { - for (String xmlFile: xmlFiles) { - parserConfiguration.parse(stringToXIS(xmlFile)); - } -// } catch (Exception e) { -// e.printStackTrace(); -// System.exit(1); -// } - } - } - - private static XMLInputSource stringToXIS(String uri) { - return new XMLInputSource(null, uri, null); - } - - @Test - public void testWoodstock() throws Exception { -// final XMLValidationSchemaFactory sf = XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA); -// final File schemaFile = new File("../gen/src/main/resources/xsd/persistence_2_0.xsd"); -// XMLValidationSchema dtd = null; -// -// try { -// dtd = sf.createSchema(schemaFile); -// } catch (XMLStreamException xe) { -// System.err.println("Failed to process the XSD file ('" + schemaFile -// + "'): " + xe); -// System.exit(1); -// } -// -// W3CSchema w3cSchema = (W3CSchema)dtd; -// XMLSchemaGrammar grammar = getGrammar(w3cSchema); -// -// File inputFile = new File( -// "../test/src/test/resources/test-gen-persistence20.xml"); -// try { -// XMLInputFactory2 ifact = (XMLInputFactory2) XMLInputFactory.newInstance(); -// XMLStreamReader2 sr = ifact.createXMLStreamReader(inputFile); -// -// try { -// sr.validateAgainst(dtd); -// /* -// * Document validation is done as document is read through (ie. -// * it's fully streaming as well as parsing), so just need to -// * traverse the contents. -// */ -// while (sr.hasNext()) { -// sr.next(); -// } -// } catch (XMLValidationException vex) { -// System.err.println("Document '" + inputFile -// + "' failed validation: " + vex); -// System.exit(1); -// } -// } catch (XMLStreamException xse) { -// System.err.println("Failed parse the input document ('" + inputFile -// + "'): " + xse); -// System.exit(1); -// } -// System.out.println("Document '" + inputFile -// + "' succesfully validated."); - } - - private void serialize(final Object obj) throws IOException { - try { - // Serialize to a file - ObjectOutput out = new ObjectOutputStream(new FileOutputStream("filename.ser")); - out.writeObject(obj); - out.close(); - - // Serialize to a byte array - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - out = new ObjectOutputStream(bos); - out.writeObject(obj); - out.close(); - - // Get the bytes of the serialized object - byte[] buf = bos.toByteArray(); - } catch (IOException e) { - throw(e); - } - } - - private class ClasspathResourceResolver implements LSResourceResolver { - - @Override - public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { - - LSInputImpl input = new LSInputImpl(); - - InputStream stream = getClass().getClassLoader().getResourceAsStream(systemId); - - input.setPublicId(publicId); - input.setSystemId(systemId); - input.setBaseURI(baseURI); - input.setCharacterStream(new InputStreamReader(stream)); - - return input; - } - } - - private class LSInputImpl implements LSInput { - - private Reader characterStream; - private InputStream byteStream; - private String stringData; - private String systemId; - private String publicId; - private String baseURI; - private String encoding; - private boolean certifiedText; - - @Override - public String getBaseURI() { - // TODO Auto-generated method stub - return null; - } - - @Override - public InputStream getByteStream() { - // TODO Auto-generated method stub - return null; - } - - @Override - public boolean getCertifiedText() { - // TODO Auto-generated method stub - return false; - } - - @Override - public Reader getCharacterStream() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getEncoding() { - // TODO Auto-generated method stub - return null; - } - @Override - public String getPublicId() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getStringData() { - // TODO Auto-generated method stub - return null; - } - @Override - public String getSystemId() { - // TODO Auto-generated method stub - return null; - } - - @Override - public void setBaseURI(String arg0) { - // TODO Auto-generated method stub - - } - @Override - public void setByteStream(InputStream arg0) { - // TODO Auto-generated method stub - - } - - @Override - public void setCertifiedText(boolean arg0) { - // TODO Auto-generated method stub - - } - - @Override - public void setCharacterStream(Reader arg0) { - // TODO Auto-generated method stub - - } - @Override - public void setEncoding(String arg0) { - // TODO Auto-generated method stub - - } - - @Override - public void setPublicId(String arg0) { - // TODO Auto-generated method stub - - } - - @Override - public void setStringData(String arg0) { - // TODO Auto-generated method stub - - } - - @Override - public void setSystemId(String arg0) { - // TODO Auto-generated method stub - - } - - // Getters and setters here - } - -// private XMLSchemaGrammar getGrammar(final W3CSchema schema) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { -// final Field f = schema.getClass().getDeclaredField("mGrammar"); -// f.setAccessible(true); -// final XMLSchemaGrammar grammar = (XMLSchemaGrammar) f.get(schema); -// return grammar; -// } -} diff --git a/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorTestCase.java b/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorTestCase.java index 04fa2668..36395d7a 100644 --- a/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorTestCase.java +++ b/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorTestCase.java @@ -1,5 +1,8 @@ package org.jboss.shrinkwrap.descriptor.schemavalidator; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -13,6 +16,18 @@ public class XmlValidatorTestCase { + final XmlValidator defaultValidator = new XmlValidator(SchemaType.XSD); + + public XmlValidatorTestCase() { + try { + defaultValidator.loadGrammar("application_6.xsd"); + } catch (XNIException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + @Test public void testXmlValidatorSchemaTypeXSD() { final XmlValidator validator = new XmlValidator(SchemaType.XSD); @@ -43,17 +58,13 @@ public void testXmlValidatorSchemaWithNoGrammarPool() { } @Test - public void testValidApplicationXML() throws Exception { - final XmlValidator validator = new XmlValidator(SchemaType.XSD); - validator.loadGrammar("application_6.xsd"); - validator.validate("src/test/resources/test-valid-application-6.xml"); + public void testValidApplicationXML() throws Exception { + defaultValidator.validateFile("src/test/resources/test-valid-application-6.xml"); } @Test(expected = XNIException.class) - public void testInvalidApplicationXML() throws Exception { - final XmlValidator validator = new XmlValidator(SchemaType.XSD); - validator.loadGrammar("application_6.xsd"); - validator.validate("src/test/resources/test-invalid-application-6.xml"); + public void testInvalidApplicationXML() throws Exception { + defaultValidator.validateFile("src/test/resources/test-invalid-application-6.xml"); } @Test @@ -62,7 +73,7 @@ public void testValidApplicationXMLFromList() throws Exception { final List schemas = new ArrayList(); schemas.add("application_6.xsd"); validator.loadGrammars(schemas); - validator.validate("src/test/resources/test-valid-application-6.xml"); + validator.validateFile("src/test/resources/test-valid-application-6.xml"); } @Test(expected = XNIException.class) @@ -71,7 +82,35 @@ public void testInvalidApplicationXMLFromList() throws Exception { final List schemas = new ArrayList(); schemas.add("application_6.xsd"); validator.loadGrammars(schemas); - validator.validate("src/test/resources/test-invalid-application-6.xml"); + validator.validateFile("src/test/resources/test-invalid-application-6.xml"); + } + + @Test + public void testValidApplicationXMLFromStreamSource() throws Exception { + final String resource = getResourceContents("src/test/resources/test-valid-application-6.xml"); + defaultValidator.validateContent(resource); + } + + @Test(expected = XNIException.class) + public void testInvalidApplicationXMLFromStreamSource() throws Exception { + final String resource = getResourceContents("src/test/resources/test-invalid-application-6.xml"); + defaultValidator.validateContent(resource); } + // -------------------------------------------------------------------------------------|| + // Helper Methods ----------------------------------------------------------------------|| + // -------------------------------------------------------------------------------------|| + + private String getResourceContents(String resource) throws Exception { + assert resource != null && resource.length() > 0 : "Resource must be specified"; + final BufferedReader reader = new BufferedReader(new FileReader(resource)); + final StringBuilder builder = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + builder.append(line); + builder.append("\n"); + } + return builder.toString(); + } + } diff --git a/test/src/test/java/org/jboss/shrinkwrap/descriptor/test/application6/ApplicationDescriptorTestCase.java b/test/src/test/java/org/jboss/shrinkwrap/descriptor/test/application6/ApplicationDescriptorTestCase.java index 257f3eac..42cdc6fb 100644 --- a/test/src/test/java/org/jboss/shrinkwrap/descriptor/test/application6/ApplicationDescriptorTestCase.java +++ b/test/src/test/java/org/jboss/shrinkwrap/descriptor/test/application6/ApplicationDescriptorTestCase.java @@ -197,6 +197,8 @@ public void testImportGeneratedApplicationXml() throws Exception String appXmlGenerated = app6Descr.exportAsString(); XmlAssert.assertIdentical(appXmlOriginal, appXmlGenerated); + + app6Descr.validate(); } diff --git a/test/src/test/java/org/jboss/shrinkwrap/descriptor/test/beans10/BeansDescriptorTestCase.java b/test/src/test/java/org/jboss/shrinkwrap/descriptor/test/beans10/BeansDescriptorTestCase.java index 8cf1f05a..11935f9a 100644 --- a/test/src/test/java/org/jboss/shrinkwrap/descriptor/test/beans10/BeansDescriptorTestCase.java +++ b/test/src/test/java/org/jboss/shrinkwrap/descriptor/test/beans10/BeansDescriptorTestCase.java @@ -17,10 +17,10 @@ package org.jboss.shrinkwrap.descriptor.test.beans10; import java.io.BufferedReader; +import java.io.File; import java.io.FileReader; -import junit.framework.Assert; - +import org.apache.xerces.xni.XNIException; import org.jboss.shrinkwrap.descriptor.api.Descriptors; import org.jboss.shrinkwrap.descriptor.api.beans10.BeansDescriptor; import org.jboss.shrinkwrap.descriptor.test.util.XmlAssert; @@ -64,7 +64,16 @@ public void testGeneratedXml() throws Exception String webXmlGenerated = beansDescr.exportAsString(); String webXmlOriginal = getResourceContents("src/test/resources/test-gen-beans10.xml"); - XmlAssert.assertIdentical(webXmlOriginal, webXmlGenerated); + XmlAssert.assertIdentical(webXmlOriginal, webXmlGenerated); + + beansDescr.validate(); + } + + @Test(expected = XNIException.class) + public void testInvalidXml() throws Exception + { + final BeansDescriptor beansDD = Descriptors.importAs(BeansDescriptor.class).fromFile(new File("src/test/resources/test-invalid-beans10.xml")); + beansDD.validate(); } //-------------------------------------------------------------------------------------|| diff --git a/test/src/test/resources/test-invalid-beans10.xml b/test/src/test/resources/test-invalid-beans10.xml new file mode 100644 index 00000000..112f91fb --- /dev/null +++ b/test/src/test/resources/test-invalid-beans10.xml @@ -0,0 +1,26 @@ + + + + class0 + class1 + class2 + class3 + class4 + + + class10 + class11 + stereotype0 + stereotype1 + stereotype2 + + + class5 + class6 + class7 + class8 + class9 + + From 863fe1da9ceee8d137077e537ff56838a88b10a3 Mon Sep 17 00:00:00 2001 From: Ralf Battenfeld Date: Thu, 22 Nov 2012 18:28:27 +0100 Subject: [PATCH 7/8] [SHRINKDESC-130] Corrected not thread safe list access --- .../schemavalidator/XmlValidator.java | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java b/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java index dcd96503..ea683e4a 100644 --- a/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java +++ b/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java @@ -60,15 +60,8 @@ public enum SchemaType { } /** contains all search locations used by getResourceAsStream() */ - private static List searchLocationList = new ArrayList(); + private List searchLocationList = new ArrayList(); - static { - searchLocationList.add("META-INF/xsd/"); - searchLocationList.add("xsd/"); - searchLocationList.add(""); - searchLocationList.add("META-INF/2001/"); - } - /** schema type we have to know for loading the grammars */ private final SchemaType schemaType; @@ -95,8 +88,8 @@ public enum SchemaType { //-----------------------------------------------------------------------|| /** - * Default constructor. Initializes the DTD and XSD preparser. - * TODO describe default settings + * Default constructor. Initializes the DTD and XSD preparser with + * default settings. */ public XmlValidator(final SchemaType type) { schemaType = type; @@ -118,16 +111,17 @@ public XmlValidator(final SchemaType type) { // apply default settings initializePreparser(); + initializeSearchLocations(); } /** * Constructs an individual configured validator. * @param type defines the schema type. - * @param nameSpacesFeatureID TODO - * @param validationFeatureID TODO - * @param schemaValidationFeatureID TODO - * @param schemaFullCheckingFeatureID TODO - * @param honourAllSchemaLocationsID TODO + * @param nameSpacesFeatureID the namespaces feature id + * @param validationFeatureID the validation feature id + * @param schemaValidationFeatureID the validation feature id + * @param schemaFullCheckingFeatureID the schema full checking feature id + * @param honourAllSchemaLocationsID the honour all schema locations feature id */ public XmlValidator(final SchemaType type, final boolean nameSpacesFeatureID, @@ -148,6 +142,7 @@ public XmlValidator(final SchemaType type, // apply given settings initializePreparser(); + initializeSearchLocations(); if (schemaType == SchemaType.DTD) { preparser.registerPreparser(XMLGrammarDescription.XML_DTD, null); @@ -182,6 +177,8 @@ public XmlValidator( this.schemaType = type; this.preparser = preparser; this.grammarPool = preparser.getGrammarPool(); + + initializeSearchLocations(); } //-----------------------------------------------------------------------|| @@ -196,8 +193,16 @@ public void setErrorHandler(final XMLErrorHandler errorHandler) { this.errorHandler = errorHandler; } + /** + * Adds a new search location to the existing locations. + * A location is an absolute path on the classpath, .e.g 'META-INF/xsd' + * without a leading slash ('/'). + * @param location + */ public void addSearchLocation(final String location) { - searchLocationList.add(location); + synchronized (searchLocationList) { + searchLocationList.add(location); + } } /** @@ -403,6 +408,16 @@ private InputStream getFirstValidInputStream(final String schema) { return inputStream; } + /** + * Initializes the location list. + */ + private void initializeSearchLocations() { + searchLocationList.add("META-INF/xsd/"); + searchLocationList.add("xsd/"); + searchLocationList.add(""); + searchLocationList.add("META-INF/2001/"); + } + /** * This is our default error handler. */ From e2361eda8ec00ffd454d9b3f53bd439aa0c7a1fd Mon Sep 17 00:00:00 2001 From: Ralf Battenfeld Date: Sat, 24 Nov 2012 16:44:40 +0100 Subject: [PATCH 8/8] [SHRINKDESC-130] Replaced ArrayList with CopyOnWriteArrayList --- .../schemavalidator/XmlValidator.java | 35 +++++++++++++++---- .../schemavalidator/XmlValidatorTestCase.java | 13 +++++++ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java b/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java index ea683e4a..f6ecb779 100644 --- a/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java +++ b/schema-validator/src/main/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidator.java @@ -24,8 +24,9 @@ import java.net.MalformedURLException; import java.security.AccessController; import java.security.PrivilegedAction; -import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import org.apache.xerces.parsers.XIncludeAwareParserConfiguration; import org.apache.xerces.parsers.XMLGrammarPreparser; @@ -41,12 +42,21 @@ import org.apache.xerces.xni.parser.XMLParseException; import org.apache.xerces.xni.parser.XMLParserConfiguration; - /** * This class is able to validate XML files against corresponding * DTD and XSD schemas. *

* The implementation is based on the xerces xni library. + *

+ * In order to make validations fast, the required schemas should be + * available on the classpath. The implementation searches the resources + * at the following locations: + *

    + *
  1. META-INF/xsd/
  2. + *
  3. xsd/
  4. + *
  5. ./.
  6. + *
  7. META-INF/2001//
  8. + *
* * @author Ralf Battenfeld * @author Andrew Lee Rubinger @@ -60,7 +70,7 @@ public enum SchemaType { } /** contains all search locations used by getResourceAsStream() */ - private List searchLocationList = new ArrayList(); + private List searchLocationList = new CopyOnWriteArrayList(); /** schema type we have to know for loading the grammars */ private final SchemaType schemaType; @@ -194,15 +204,28 @@ public void setErrorHandler(final XMLErrorHandler errorHandler) { } /** - * Adds a new search location to the existing locations. + * Adds a new search location to the existing locations by inserting the + * new location at the first position. + *

* A location is an absolute path on the classpath, .e.g 'META-INF/xsd' * without a leading slash ('/'). * @param location */ public void addSearchLocation(final String location) { - synchronized (searchLocationList) { - searchLocationList.add(location); + if (!searchLocationList.isEmpty()) { + searchLocationList.add(0, location); } + else { + searchLocationList.add(location); + } + } + + /** + * Returns the current search locations. + * @return a unmodifiable list of the current search locations. + */ + public List getSearchLocations() { + return Collections.unmodifiableList(searchLocationList); } /** diff --git a/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorTestCase.java b/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorTestCase.java index 36395d7a..34516529 100644 --- a/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorTestCase.java +++ b/schema-validator/src/test/java/org/jboss/shrinkwrap/descriptor/schemavalidator/XmlValidatorTestCase.java @@ -97,6 +97,19 @@ public void testInvalidApplicationXMLFromStreamSource() throws Exception { defaultValidator.validateContent(resource); } + @Test + public void testDefaultSearchLocation() throws Exception { + Assert.assertTrue(defaultValidator.getSearchLocations().size() == 4); + } + + @Test + public void testAddSearchLocation() throws Exception { + final XmlValidator validator = new XmlValidator(SchemaType.XSD); + validator.addSearchLocation("META-INF/schema"); + Assert.assertTrue(validator.getSearchLocations().size() == 5); + Assert.assertEquals("META-INF/schema", validator.getSearchLocations().get(0)); + } + // -------------------------------------------------------------------------------------|| // Helper Methods ----------------------------------------------------------------------|| // -------------------------------------------------------------------------------------||