Working on XsdSchema
This commit is contained in:
@@ -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();
|
||||
}
|
||||
@@ -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 <a href="http://ws.apache.org/commons/XmlSchema/">Commons XML Schema</a>
|
||||
* @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.
|
||||
* <p/>
|
||||
* 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 <code>null</code>
|
||||
* @throws IllegalArgumentException if the supplied <code>xsdResource</code> is <code>null</code>
|
||||
*/
|
||||
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 <code>XmlSchema</code> object; must not be <code>null</code>
|
||||
* @throws IllegalArgumentException if the supplied <code>schema</code> is <code>null</code>
|
||||
*/
|
||||
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");
|
||||
|
||||
@@ -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 <a href="http://ws.apache.org/commons/XmlSchema/">Commons XML Schema</a>
|
||||
* @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 <code>CommonsXsdSchemaCollection</code>.
|
||||
* <p/>
|
||||
* A subsequent call to the {@link #setXsds(Resource[])} is required.
|
||||
*/
|
||||
public CommonsXsdSchemaCollection() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the <code>CommonsXsdSchemaCollection</code> 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.
|
||||
* <p/>
|
||||
* Defaults to <code>false</code>.
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
Reference in New Issue
Block a user