Working on XsdSchema

This commit is contained in:
Arjen Poutsma
2008-03-02 02:35:42 +00:00
parent 27afa1ab1f
commit 5ed8895ec3
4 changed files with 222 additions and 1 deletions

View File

@@ -3,7 +3,7 @@
<parent>
<artifactId>spring-ws-parent</artifactId>
<groupId>org.springframework.ws</groupId>
<version>1.5.0-m2-SNAPSHOT</version>
<version>1.5.0-rc1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-ws-sandbox</artifactId>
@@ -109,6 +109,15 @@
<artifactId>commons-httpclient</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.schema</groupId>
<artifactId>XmlSchema</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xsom</groupId>
<artifactId>xsom</artifactId>
<version>20070515</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>easymock</groupId>

View File

@@ -0,0 +1,33 @@
/*
* 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;
/**
* Represents an abstraction for a collection of XSD schemas.
*
* @author Arjen Poutsma
* @since 1.5.0
*/
public interface XsdSchemaCollection {
/**
* Returns all schema's contained in this collection.
*
* @return the schema's contained in this collection
*/
XsdSchema[] getXsdSchemas();
}

View File

@@ -0,0 +1,112 @@
/*
* 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.Arrays;
import java.util.List;
import javax.xml.transform.Source;
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.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.xml.transform.ResourceSource;
import org.springframework.xml.xsd.XsdSchema;
import org.springframework.xml.xsd.XsdSchemaCollection;
/**
* @author Arjen Poutsma
* @since 1.5.0
*/
public class CommonsXsdSchemaCollection implements XsdSchemaCollection, InitializingBean {
private static final Log logger = LogFactory.getLog(CommonsXsdSchemaCollection.class);
private XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
private Resource[] xsdResources;
public CommonsXsdSchemaCollection() {
}
public CommonsXsdSchemaCollection(Resource[] xsdResources) {
this.xsdResources = xsdResources;
}
public CommonsXsdSchemaCollection(XmlSchemaCollection schemaCollection) {
this.schemaCollection = schemaCollection;
}
public void setXsds(Resource[] xsdResources) {
this.xsdResources = xsdResources;
}
public void afterPropertiesSet() throws Exception {
if (!ObjectUtils.isEmpty(xsdResources)) {
Assert.notEmpty(xsdResources, "'xsds' must not be empty");
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
for (int i = 0; i < xsdResources.length; i++) {
Assert.isTrue(xsdResources[i].exists(), "xsd '" + xsdResources[i] + "' does not exit");
Source source = new ResourceSource(xmlReader, xsdResources[i]);
schemaCollection.read(source, null);
}
if (logger.isInfoEnabled()) {
logger.info("Loaded " + Arrays.asList(xsdResources));
}
}
}
public XsdSchema[] getXsdSchemas() {
XmlSchema[] schemas = schemaCollection.getXmlSchemas();
List result = new ArrayList(schemas.length - 1);
for (int i = 0; i < schemas.length; i++) {
// Ignore the main XSD schema, which is always loaded
if (!Constants.URI_2001_SCHEMA_XSD.equals(schemas[i].getTargetNamespace())) {
result.add(new CommonsXsdSchema(schemas[i]));
}
}
return (XsdSchema[]) result.toArray(new XsdSchema[result.size()]);
}
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();
}
}

View File

@@ -0,0 +1,67 @@
/*
* 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 javax.xml.transform.dom.DOMResult;
import junit.framework.TestCase;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.xml.xsd.AbstractXsdSchemaTestCase;
import org.springframework.xml.xsd.XsdSchema;
import org.springframework.xml.sax.SaxUtils;
import org.w3c.dom.Document;
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);
}
}