From 2d974e72649409d13da3b4caa06f13a71515c9fa Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 4 Mar 2008 23:40:51 +0000 Subject: [PATCH] Working on XsdSchema --- ...sdSchema.java => XsdSchemaCollection.java} | 14 +- .../xml/xsd/commons/CommonsXsdSchema.java | 53 +------- .../commons/CommonsXsdSchemaCollection.java | 123 ++++++++++++++++++ .../CommonsXsdSchemaCollectionTest.java | 68 ++++++++++ .../xml/xsd/commons/CommonsXsdSchemaTest.java | 21 +-- 5 files changed, 211 insertions(+), 68 deletions(-) rename xml/src/main/java/org/springframework/xml/xsd/{InlineableXsdSchema.java => XsdSchemaCollection.java} (72%) create mode 100644 xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollection.java create mode 100644 xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollectionTest.java diff --git a/xml/src/main/java/org/springframework/xml/xsd/InlineableXsdSchema.java b/xml/src/main/java/org/springframework/xml/xsd/XsdSchemaCollection.java similarity index 72% rename from xml/src/main/java/org/springframework/xml/xsd/InlineableXsdSchema.java rename to xml/src/main/java/org/springframework/xml/xsd/XsdSchemaCollection.java index d44b7321..3fefe614 100644 --- a/xml/src/main/java/org/springframework/xml/xsd/InlineableXsdSchema.java +++ b/xml/src/main/java/org/springframework/xml/xsd/XsdSchemaCollection.java @@ -17,15 +17,17 @@ package org.springframework.xml.xsd; /** - * Represents an abstraction for XSD schemas. - + * Represents an abstraction for a collection of XSD schemas. + * * @author Arjen Poutsma * @since 1.5.0 */ -public interface InlineableXsdSchema extends XsdSchema { +public interface XsdSchemaCollection { /** - * Inlines this schema into a set of self-contained schema's. - * */ - XsdSchema[] inline(); + * Returns all schema's contained in this collection. + * + * @return the schema's contained in this collection + */ + XsdSchema[] getXsdSchemas(); } diff --git a/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchema.java b/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchema.java index 09e01209..8fb1e45e 100644 --- a/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchema.java +++ b/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchema.java @@ -20,26 +20,18 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.Iterator; import java.util.List; -import java.util.Map; import javax.xml.namespace.QName; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import org.apache.ws.commons.schema.XmlSchema; import org.apache.ws.commons.schema.XmlSchemaCollection; -import org.apache.ws.commons.schema.XmlSchemaInclude; -import org.apache.ws.commons.schema.XmlSchemaObject; import org.apache.ws.commons.schema.XmlSchemaObjectCollection; import org.xml.sax.SAXException; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.core.io.Resource; import org.springframework.util.Assert; -import org.springframework.xml.sax.SaxUtils; -import org.springframework.xml.xsd.InlineableXsdSchema; import org.springframework.xml.xsd.XsdSchema; /** @@ -49,51 +41,21 @@ import org.springframework.xml.xsd.XsdSchema; * @see Commons XML Schema * @since 1.5.0 */ -public class CommonsXsdSchema implements InlineableXsdSchema, InitializingBean { +public class CommonsXsdSchema implements XsdSchema { private XmlSchema schema; - private Resource xsdResource; - - /** - * Create a new, empty instance of the {@link CommonsXsdSchema} class. - *

- * A subsequent call to the {@link #setXsd(Resource)} method is required. - */ - public CommonsXsdSchema() { - } - - /** - * Create a new instance of the {@link CommonsXsdSchema} class with the specified resource. - * - * @param xsdResource the XSD resource; must not be null - * @throws IllegalArgumentException if the supplied xsdResource is null - */ - public CommonsXsdSchema(Resource xsdResource) { - Assert.notNull(xsdResource, "xsdResource must not be null"); - this.xsdResource = xsdResource; - } - /** * Create a new instance of the {@link CommonsXsdSchema} class with the specified {@link XmlSchema} reference. * * @param schema the Commons XmlSchema object; must not be null * @throws IllegalArgumentException if the supplied schema is null */ - private CommonsXsdSchema(XmlSchema schema) { + protected CommonsXsdSchema(XmlSchema schema) { Assert.notNull(schema, "'schema' must not be null"); this.schema = schema; } - /** - * Set the XSD resource to be exposed by calls to this instances' {@link #getSource()} method. - * - * @param xsdResource the XSD resource - */ - public void setXsd(Resource xsdResource) { - this.xsdResource = xsdResource; - } - public String getTargetNamespace() { return schema.getTargetNamespace(); } @@ -131,17 +93,12 @@ public class CommonsXsdSchema implements InlineableXsdSchema, InitializingBean { return schema; } - public void afterPropertiesSet() throws IOException, SAXException { - Assert.notNull(xsdResource, "'xsd' is required"); - Assert.isTrue(this.xsdResource.exists(), "xsd '" + this.xsdResource + "' does not exit"); - loadSchema(); - } - private void loadSchema() throws SAXException, IOException { XmlSchemaCollection schemaCollection = new XmlSchemaCollection(); - this.schema = schemaCollection.read(SaxUtils.createInputSource(xsdResource), null); +// this.schema = schemaCollection.read(SaxUtils.createInputSource(xsdResource), null); } + /* public XsdSchema[] inline() { XmlSchema clone = cloneSchema(schema); inlineIncludes(clone, new ArrayList()); @@ -179,7 +136,7 @@ public class CommonsXsdSchema implements InlineableXsdSchema, InitializingBean { items.remove(include); } } - } + }*/ public String toString() { StringBuffer buffer = new StringBuffer("CommonsXsdSchema"); diff --git a/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollection.java b/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollection.java new file mode 100644 index 00000000..45ee2d30 --- /dev/null +++ b/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollection.java @@ -0,0 +1,123 @@ +/* + * Copyright 2008 the original author or authors. + * + * 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.springframework.xml.xsd.commons; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.ws.commons.schema.XmlSchema; +import org.apache.ws.commons.schema.XmlSchemaCollection; +import org.apache.ws.commons.schema.constants.Constants; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.core.io.Resource; +import org.springframework.util.Assert; +import org.springframework.xml.sax.SaxUtils; +import org.springframework.xml.xsd.XsdSchema; +import org.springframework.xml.xsd.XsdSchemaCollection; + +/** + * Implementation of the {@link XsdSchemaCollection} that uses Apache WS-Commons XML Schema. + * + * @author Arjen Poutsma + * @see Commons XML Schema + * @since 1.5.0 + */ +public class CommonsXsdSchemaCollection implements XsdSchemaCollection, InitializingBean { + + private static final Log logger = LogFactory.getLog(CommonsXsdSchemaCollection.class); + + private final XmlSchemaCollection schemaCollection = new XmlSchemaCollection(); + + private final List xmlSchemas = new ArrayList(); + + private Resource[] xsdResources; + + private boolean inlineIncludes = false; + + /** + * Constructs a new, empty instance of the CommonsXsdSchemaCollection. + *

+ * A subsequent call to the {@link #setXsds(Resource[])} is required. + */ + public CommonsXsdSchemaCollection() { + } + + /** + * Constructs a new instance of the CommonsXsdSchemaCollection based on the given resources. + * + * @param resources the schema resources to load + */ + public CommonsXsdSchemaCollection(Resource[] resources) { + this.xsdResources = resources; + } + + /** + * Sets the schema resources to be loaded. + * + * @param xsdResources the schema resources to be loaded + */ + public void setXsds(Resource[] xsdResources) { + this.xsdResources = xsdResources; + } + + /** + * Defines whether included schemas should be inlinded into the including schema. + *

+ * Defaults to false. + */ + public void setInlineIncludes(boolean inlineIncludes) { + this.inlineIncludes = inlineIncludes; + } + + public void afterPropertiesSet() throws Exception { + Assert.notEmpty(xsdResources, "'xsds' must not be empty"); + for (int i = 0; i < xsdResources.length; i++) { + Assert.isTrue(xsdResources[i].exists(), xsdResources[i] + " does not exit"); + xmlSchemas.add(schemaCollection.read(SaxUtils.createInputSource(xsdResources[i]), null)); + } + } + + public XsdSchema[] getXsdSchemas() { + XsdSchema[] result = new XsdSchema[xmlSchemas.size()]; + for (int i = 0; i < xmlSchemas.size(); i++) { + XmlSchema xmlSchema = (XmlSchema) xmlSchemas.get(i); + result[i] = new CommonsXsdSchema(xmlSchema); + } + return result; + } + + public String toString() { + StringBuffer buffer = new StringBuffer("CommonsXsdSchemaCollection"); + buffer.append('{'); + XmlSchema[] schemas = schemaCollection.getXmlSchemas(); + for (int i = 0; i < schemas.length; i++) { + if (!Constants.URI_2001_SCHEMA_XSD.equals(schemas[i].getTargetNamespace())) { + buffer.append(schemas[i].getTargetNamespace()); + if (i < schemas.length - 1) { + buffer.append(','); + } + } + } + buffer.append('}'); + return buffer.toString(); + } + + +} diff --git a/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollectionTest.java b/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollectionTest.java new file mode 100644 index 00000000..83c8ed0f --- /dev/null +++ b/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollectionTest.java @@ -0,0 +1,68 @@ +/* + * Copyright ${YEAR} the original author or authors. + * + * 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.springframework.xml.xsd.commons; + +import junit.framework.TestCase; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.xml.xsd.AbstractXsdSchemaTestCase; + +public class CommonsXsdSchemaCollectionTest extends TestCase { + + private CommonsXsdSchemaCollection collection; + + protected void setUp() throws Exception { + collection = new CommonsXsdSchemaCollection(); + } + + public void testSingle() throws Exception { + Resource resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class); + collection.setXsds(new Resource[]{resource}); + collection.afterPropertiesSet(); + assertEquals("Invalid amount of XSDs loaded", 1, collection.getXsdSchemas().length); + } + + public void testIncludes() throws Exception { + Resource resource = new ClassPathResource("including.xsd", AbstractXsdSchemaTestCase.class); + collection.setXsds(new Resource[]{resource}); + collection.afterPropertiesSet(); + assertEquals("Invalid amount of XSDs loaded", 2, collection.getXsdSchemas().length); + } + + public void testImports() throws Exception { + Resource resource = new ClassPathResource("importing.xsd", AbstractXsdSchemaTestCase.class); + collection.setXsds(new Resource[]{resource}); + collection.afterPropertiesSet(); + assertEquals("Invalid amount of XSDs loaded", 2, collection.getXsdSchemas().length); + } + + public void testDuplicates() throws Exception { + Resource resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class); + collection.setXsds(new Resource[]{resource, resource}); + collection.afterPropertiesSet(); + assertEquals("Invalid amount of XSDs loaded", 1, collection.getXsdSchemas().length); + } + + public void testComplex() throws Exception { + Resource resource = new ClassPathResource("A.xsd", AbstractXsdSchemaTestCase.class); + collection.setXsds(new Resource[]{resource}); + collection.afterPropertiesSet(); + assertEquals("Invalid amount of XSDs loaded", 5, collection.getXsdSchemas().length); + } + +} \ No newline at end of file diff --git a/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaTest.java b/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaTest.java index 52d47832..3f143a0e 100644 --- a/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaTest.java +++ b/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaTest.java @@ -16,48 +16,41 @@ package org.springframework.xml.xsd.commons; -import javax.xml.transform.stream.StreamSource; -import javax.xml.transform.stream.StreamResult; -import javax.xml.transform.Source; - import org.apache.ws.commons.schema.XmlSchema; import org.apache.ws.commons.schema.XmlSchemaCollection; -import org.xml.sax.XMLReader; -import org.xml.sax.helpers.XMLReaderFactory; import org.springframework.core.io.Resource; -import org.springframework.core.io.ClassPathResource; +import org.springframework.xml.sax.SaxUtils; import org.springframework.xml.xsd.AbstractXsdSchemaTestCase; import org.springframework.xml.xsd.XsdSchema; -import org.springframework.xml.transform.ResourceSource; public class CommonsXsdSchemaTest extends AbstractXsdSchemaTestCase { protected XsdSchema createSchema(Resource resource) throws Exception { - CommonsXsdSchema schema = new CommonsXsdSchema(resource); - schema.afterPropertiesSet(); - return schema; + XmlSchemaCollection schemaCollection = new XmlSchemaCollection(); + XmlSchema schema = schemaCollection.read(SaxUtils.createInputSource(resource), null); + return new CommonsXsdSchema(schema); } + /* public void testInline() throws Exception { Resource resource = new ClassPathResource("A.xsd", AbstractXsdSchemaTestCase.class); CommonsXsdSchema schema = new CommonsXsdSchema(resource); - schema.afterPropertiesSet(); XsdSchema[] inlined = schema.inline(); for (int i = 0; i < inlined.length; i++) { transformer.transform(inlined[i].getSource(), new StreamResult(System.out)); System.out.println(); } } - + public void testCircular() throws Exception { Resource resource = new ClassPathResource("circular-1.xsd", AbstractXsdSchemaTestCase.class); CommonsXsdSchema schema = new CommonsXsdSchema(resource); - schema.afterPropertiesSet(); XsdSchema[] inlined = schema.inline(); for (int i = 0; i < inlined.length; i++) { transformer.transform(inlined[i].getSource(), new StreamResult(System.out)); System.out.println(); } } + */ } \ No newline at end of file