Working on XsdSchema

This commit is contained in:
Arjen Poutsma
2008-03-02 02:32:27 +00:00
parent e4b4cb2124
commit 27afa1ab1f
24 changed files with 889 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
/*
* 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;
import org.springframework.core.io.Resource;
public class SimpleXsdSchemaTest extends AbstractXsdSchemaTestCase {
protected XsdSchema createSchema(Resource resource) throws Exception {
SimpleXsdSchema schema = new SimpleXsdSchema(resource);
schema.afterPropertiesSet();
return schema;
}
}

View File

@@ -0,0 +1,63 @@
/*
* 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.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.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;
}
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();
}
}
}

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:1"
xmlns:tns="urn:1" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:include schemaLocation="B.xsd"/>
<xsd:simpleType name="A">
<xsd:restriction base="tns:B"/>
</xsd:simpleType>
</xsd:schema>

View File

@@ -0,0 +1,105 @@
/*
* 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;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import org.custommonkey.xmlunit.XMLTestCase;
import org.custommonkey.xmlunit.XMLUnit;
import org.w3c.dom.Document;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.xml.sax.SaxUtils;
public abstract class AbstractXsdSchemaTestCase extends XMLTestCase {
private DocumentBuilder documentBuilder;
protected Transformer transformer;
protected final void setUp() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
XMLUnit.setIgnoreWhitespace(true);
}
public void testSingle() throws Exception {
Resource resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class);
XsdSchema single = createSchema(resource);
String namespace = "http://www.springframework.org/spring-ws/single/schema";
assertEquals("Invalid target namespace", namespace, single.getTargetNamespace());
QName[] elementNames = single.getElementNames();
assertQNamesEqual(new QName[]{new QName(namespace, "GetOrderRequest"), new QName(namespace, "GetOrderResponse"),
new QName(namespace, "GetOrderFault")}, elementNames);
resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class);
Document expected = documentBuilder.parse(SaxUtils.createInputSource(resource));
DOMResult domResult = new DOMResult();
transformer.transform(single.getSource(), domResult);
Document result = (Document) domResult.getNode();
assertXMLEqual("Invalid Source returned", expected, result);
}
public void testIncludes() throws Exception {
Resource resource = new ClassPathResource("including.xsd", AbstractXsdSchemaTestCase.class);
XsdSchema including = createSchema(resource);
String namespace = "http://www.springframework.org/spring-ws/include/schema";
assertEquals("Invalid target namespace", namespace, including.getTargetNamespace());
QName[] elementNames = including.getElementNames();
assertQNamesEqual(new QName[]{new QName(namespace, "GetOrderRequest")}, elementNames);
resource = new ClassPathResource("including.xsd", AbstractXsdSchemaTestCase.class);
Document expected = documentBuilder.parse(SaxUtils.createInputSource(resource));
DOMResult domResult = new DOMResult();
transformer.transform(including.getSource(), domResult);
Document result = (Document) domResult.getNode();
assertXMLEqual("Invalid Source returned", expected, result);
}
public void testImports() throws Exception {
Resource resource = new ClassPathResource("importing.xsd", AbstractXsdSchemaTestCase.class);
XsdSchema importing = createSchema(resource);
String namespace = "http://www.springframework.org/spring-ws/importing/schema";
assertEquals("Invalid target namespace", namespace, importing.getTargetNamespace());
QName[] elementNames = importing.getElementNames();
assertQNamesEqual(new QName[]{new QName(namespace, "GetOrderRequest")}, elementNames);
resource = new ClassPathResource("importing.xsd", AbstractXsdSchemaTestCase.class);
Document expected = documentBuilder.parse(SaxUtils.createInputSource(resource));
DOMResult domResult = new DOMResult();
transformer.transform(importing.getSource(), domResult);
Document result = (Document) domResult.getNode();
assertXMLEqual("Invalid Source returned", expected, result);
}
private void assertQNamesEqual(QName[] expected, QName[] result) {
Set expectedSet = new HashSet(Arrays.asList(expected));
Set resultSet = new HashSet(Arrays.asList(result));
assertEquals("Invalid QNames", expectedSet, resultSet);
}
protected abstract XsdSchema createSchema(Resource resource) throws Exception;
}

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:1"
xmlns="urn:1"
xmlns:imported="urn:2" elementFormDefault="qualified">
<xsd:include schemaLocation="C.xsd"/>
<xsd:import schemaLocation="D.xsd" namespace="urn:2"/>
<xsd:complexType name="B">
<xsd:sequence>
<xsd:element type="C" name="c"/>
<xsd:element type="imported:D" name="d"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:simpleType name="C">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:schema>

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:2"
xmlns="urn:2" elementFormDefault="qualified">
<xsd:include schemaLocation="C.xsd"/>
<xsd:simpleType name="D">
<xsd:restriction base="C"/>
</xsd:simpleType>
</xsd:schema>

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:1"
xmlns:tns="urn:1" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:include schemaLocation="circular-2.xsd"/>
<xsd:simpleType name="A1">
<xsd:restriction base="tns:B1"/>
</xsd:simpleType>
<xsd:simpleType name="A2">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:schema>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:1"
xmlns:tns="urn:1" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:include schemaLocation="circular-3.xsd"/>
<xsd:simpleType name="B1">
<xsd:restriction base="tns:C1"/>
</xsd:simpleType>
</xsd:schema>

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:1"
xmlns:tns="urn:1" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:include schemaLocation="circular-1.xsd"/>
<xsd:simpleType name="C1">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:simpleType name="C2">
<xsd:restriction base="tns:A1"/>
</xsd:simpleType>
</xsd:schema>

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.springframework.org/spring-ws/imported/schema"
xmlns="http://www.springframework.org/spring-ws/imported/schema" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:element name="GetOrderResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="child" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.springframework.org/spring-ws/importing/schema"
xmlns="http://www.springframework.org/spring-ws/importing/schema" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/spring-ws/imported/schema" schemaLocation="imported.xsd"/>
<xsd:element name="GetOrderRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="child" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.springframework.org/spring-ws/include/schema"
xmlns="http://www.springframework.org/spring-ws/include/schema" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:element name="GetOrderResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="child" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.springframework.org/spring-ws/include/schema"
xmlns="http://www.springframework.org/spring-ws/include/schema" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:include schemaLocation="included.xsd"/>
<xsd:element name="GetOrderRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="child" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

View File

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.springframework.org/spring-ws/single/schema"
xmlns="http://www.springframework.org/spring-ws/single/schema" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:simpleType name="customType">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:element name="GetOrderRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="child" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="GetOrderResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="child" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="GetOrderFault">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="child" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>