This commit is contained in:
Arjen Poutsma
2007-10-29 01:51:40 +00:00
parent 433dd146df
commit 17196ebdb7
41 changed files with 908 additions and 1522 deletions

View File

@@ -1,31 +0,0 @@
/*
* Copyright 2007 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.schema;
import org.springframework.xml.validation.XmlValidator;
/**
* Represents an abstraction for XML schemas.
*
* @author Arjen Poutsma
* @since 1.0.2
*/
public interface Schema {
/** Returns the {@link XmlValidator} based on this schema. */
XmlValidator getValidator();
}

View File

@@ -1,36 +0,0 @@
/*
* Copyright 2007 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.schema;
import org.springframework.xml.XmlException;
/**
* Base class for all schema exceptions.
*
* @author Arjen Poutsma
* @since 1.0.2
*/
public class SchemaException extends XmlException {
public SchemaException(String message) {
super(message);
}
public SchemaException(String message, Throwable throwable) {
super(message, throwable);
}
}

View File

@@ -1,5 +0,0 @@
<html>
<body>
Provides the schema functionality of the Spring Web Services framework. Contains the Schema interface.
</body>
</html>

View File

@@ -1,161 +0,0 @@
/*
* Copyright 2007 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.schema.xsd;
import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.xml.namespace.QNameUtils;
import org.springframework.xml.sax.SaxUtils;
import org.springframework.xml.validation.XmlValidator;
import org.springframework.xml.validation.XmlValidatorFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Simple implementation of the {@link XsdSchema} interface. Only wraps a single {@link XsdSchemaDocument}, and does not
* follow <code>&lt;xsd:include/&gt;</code> nor <code>&lt;xsd:import/&gt;</code> elements.
* <p/>
* Allows an XSD {@link Resource resource} to be set by the {@link #setSchema(Resource) schema} property, or directly in
* the {@link #SimpleXsdSchema(Resource) constructor}.
*
* @author Arjen Poutsma
* @since 1.0.2
*/
public class SimpleXsdSchema implements XsdSchema, InitializingBean {
private static final String SCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema";
/** The schema qualified name. */
private static final QName SCHEMA_NAME = QNameUtils.createQName(SCHEMA_NAMESPACE, "schema", "xsd");
/** The element declaration qualified name. */
private static final QName ELEMENT_NAME = QNameUtils.createQName(SCHEMA_NAMESPACE, "element", "xsd");
private final XsdSchemaDocument[] schemaDocuments = new XsdSchemaDocument[]{new SimpleXsdSchemaDocument()};
private Resource schemaResource;
private Element schemaElement;
private XmlValidator validator;
/**
* Create a new instance of the {@link SimpleXsdSchema} class.
* <p/>
* A subsequent call to the {@link #setSchema(Resource)} method is required.
*/
public SimpleXsdSchema() {
}
/**
* Create a new instance of the {@link SimpleXsdSchema} class with the specified resource.
*
* @param schemaResource the XSD resource; must not be <code>null</code>
* @throws IllegalArgumentException if the supplied <code>schemaResource</code> is <code>null</code>
*/
public SimpleXsdSchema(Resource schemaResource) {
setSchema(schemaResource);
}
/** Set the XSD resource to be used. */
public void setSchema(Resource schemaResource) {
this.schemaResource = schemaResource;
}
public XsdSchemaDocument[] getSchemaDocuments() {
return schemaDocuments;
}
public XmlValidator getValidator() {
return validator;
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(schemaResource, "'schema' is required");
Assert.isTrue(schemaResource.exists(), "schema '" + schemaResource + "' does not exit");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(SaxUtils.createInputSource(schemaResource));
schemaElement = document.getDocumentElement();
QName name = QNameUtils.getQNameForNode(schemaElement);
Assert.isTrue(SCHEMA_NAME.equals(name),
"schema document has invalid qualified name: " + name + ". " + "Epected " + SCHEMA_NAME);
Assert.hasLength(schemaElement.getAttribute("targetNamespace"), "schema does not define targetNamespace");
validator = XmlValidatorFactory.createValidator(schemaResource, XmlValidatorFactory.SCHEMA_W3C_XML);
}
/** Inner definition of {@link SimpleXsdSchemaDocument}. */
private class SimpleXsdSchemaDocument implements XsdSchemaDocument {
public String getFilename() {
return schemaResource.getFilename();
}
public Source getSource() {
return new DOMSource(schemaElement);
}
public XsdElement[] getElements() {
NodeList children = schemaElement.getChildNodes();
List declarations = new ArrayList(children.getLength());
for (int i = 0; i < children.getLength(); i++) {
if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element childElement = (Element) children.item(i);
QName childName = QNameUtils.getQNameForNode(childElement);
if (ELEMENT_NAME.equals(childName)) {
declarations.add(new SimpleXsdElement(childElement));
}
}
}
return (XsdElement[]) declarations.toArray(new XsdElement[declarations.size()]);
}
public String getTargetNamespace() {
return schemaElement.getAttribute("targetNamespace");
}
/** Inner definition of {@link SimpleXsdElement}. */
private class SimpleXsdElement implements XsdElement {
private final Element element;
private SimpleXsdElement(Element element) {
this.element = element;
}
public QName getName() {
String attributeValue = element.getAttribute("name");
return StringUtils.hasLength(attributeValue) ? new QName(getTargetNamespace(), attributeValue) : null;
}
}
}
}

View File

@@ -1,176 +0,0 @@
/*
* Copyright 2007 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.schema.xsd;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
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.XmlSchemaElement;
import org.apache.ws.commons.schema.XmlSchemaObjectTable;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.xml.sax.SaxUtils;
import org.springframework.xml.validation.XmlValidator;
import org.springframework.xml.validation.XmlValidatorFactory;
/**
* Implementation of the {@link XsdSchema} interface that uses <a href="http://ws.apache.org/commons/XmlSchema/">Apache
* WS Commons XMLSchema</a>. Allows for multiple {@link XsdSchemaDocument}s, and follows
* <code>&lt;xsd:include/&gt;</code> and <code>&lt;xsd:import/&gt;</code> elements.
* <p/>
* Allows an XSD {@link Resource resource} to be set by the {@link #setSchema(Resource) schema} or {@link
* #setSchemas(Resource[]) schemas} properties, or directly in the {@link #WsCommonsXsdSchema(Resource)} or {@link
* #WsCommonsXsdSchema(Resource[])} constructors.
*
* @author Arjen Poutsma
* @since 1.0.2
*/
public class WsCommonsXsdSchema implements XsdSchema, InitializingBean {
private Resource[] schemaResources;
private XsdSchemaDocument[] schemaDocuments;
private XmlValidator validator;
/**
* Create a new instance of the {@link WsCommonsXsdSchema} class.
* <p/>
* A subsequent call to the {@link #setSchema(Resource)} or {@link #setSchemas(Resource[])} method is required.
*/
public WsCommonsXsdSchema() {
}
/**
* Create a new instance of the {@link WsCommonsXsdSchema} class with the specified single resource.
*
* @param schemaResource the XSD resource; must not be <code>null</code>
* @throws IllegalArgumentException if the supplied <code>schemaResource</code> is <code>null</code>
*/
public WsCommonsXsdSchema(Resource schemaResource) {
setSchema(schemaResource);
}
/**
* Create a new instance of the {@link WsCommonsXsdSchema} class with the specified resources.
*
* @param schemaResources the XSD resources; must not be <code>null</code> or empty
* @throws IllegalArgumentException if the supplied <code>schemaResource</code> is <code>null</code>
*/
public WsCommonsXsdSchema(Resource[] schemaResources) {
setSchemas(schemaResources);
}
/** Set the XSD resource to be used. */
public void setSchema(Resource schemaResource) {
this.schemaResources = new Resource[]{schemaResource};
}
/** Set the XSD resources to be used. */
public void setSchemas(Resource[] schemaResources) {
this.schemaResources = schemaResources;
}
public XmlValidator getValidator() {
return validator;
}
public void afterPropertiesSet() throws Exception {
Assert.notEmpty(schemaResources, "'schemaResources' must not be empty");
XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
for (int i = 0; i < schemaResources.length; i++) {
Resource schemaResource = schemaResources[i];
Assert.notNull(schemaResource, "'schemaResource' must not be null");
Assert.isTrue(schemaResource.exists(), "schema \"" + schemaResource + "\" does not exist");
schemaCollection.read(SaxUtils.createInputSource(schemaResource), null);
}
XmlSchema[] schemas = schemaCollection.getXmlSchemas();
List schemaList = new ArrayList(schemaResources.length);
for (int i = 0; i < schemas.length; i++) {
if (!schemas[i].getTargetNamespace().equals("http://www.w3.org/2001/XMLSchema")) {
schemaList.add(new WsCommonsXsdSchemaDocument(schemas[i]));
}
}
this.schemaDocuments = (XsdSchemaDocument[]) schemaList.toArray(new XsdSchemaDocument[schemaList.size()]);
validator = XmlValidatorFactory.createValidator(schemaResources, XmlValidatorFactory.SCHEMA_W3C_XML);
}
public XsdSchemaDocument[] getSchemaDocuments() {
return schemaDocuments;
}
/** Inner definition of {@link WsCommonsXsdSchemaDocument}. */
private static class WsCommonsXsdSchemaDocument implements XsdSchemaDocument {
private final XmlSchema commonsSchema;
private WsCommonsXsdSchemaDocument(XmlSchema commonsSchema) {
this.commonsSchema = commonsSchema;
}
public String getFilename() {
return StringUtils.getFilename(commonsSchema.getSourceURI());
}
public Source getSource() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
commonsSchema.write(bos);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
return new StreamSource(bis);
}
public XsdElement[] getElements() {
XmlSchemaObjectTable elements = commonsSchema.getElements();
XsdElement[] declarations = new XsdElement[elements.getCount()];
int i = 0;
for (Iterator iterator = elements.getValues(); iterator.hasNext();) {
XmlSchemaElement element = (XmlSchemaElement) iterator.next();
declarations[i] = new WsCommonsXsdElement(element);
i++;
}
return declarations;
}
public String getTargetNamespace() {
return commonsSchema.getTargetNamespace();
}
}
private static class WsCommonsXsdElement implements XsdElement {
private final XmlSchemaElement element;
private WsCommonsXsdElement(XmlSchemaElement element) {
this.element = element;
}
public QName getName() {
return element.getQName();
}
}
}

View File

@@ -1,33 +0,0 @@
/*
* Copyright 2007 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.schema.xsd;
import javax.xml.namespace.QName;
/**
* Represents an abstraction for an XSD element declaration.
*
* @author Arjen Poutsma
* @see XsdSchemaDocument#getElements()
* @since 1.0.2
*/
public interface XsdElement {
/** Returns the qualified name of the element declaration. */
QName getName();
}

View File

@@ -1,33 +0,0 @@
/*
* Copyright 2007 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.schema.xsd;
import org.springframework.xml.schema.Schema;
/**
* Represents an abstraction for an XSD schema. An XSD schema is made up of one or more schema documents.
*
* @author Arjen Poutsma
* @see XsdSchemaDocument
* @since 1.0.2
*/
public interface XsdSchema extends Schema {
/** Returns the {@link XsdSchemaDocument} objects this schema consists of. */
XsdSchemaDocument[] getSchemaDocuments();
}

View File

@@ -1,27 +0,0 @@
package org.springframework.xml.schema.xsd;
import javax.xml.transform.Source;
/**
* Represents an abstraction for an individual XSD schema document. A schema is made up of one or more schema
* documents.
*
* @author Arjen Poutsma
* @see XsdSchema
* @since 1.0.2
*/
public interface XsdSchemaDocument {
/** Return a filename for this schema document. For example, <code>schema.xsd</code>. */
String getFilename();
/** Returns the target namespace of the schema document. */
String getTargetNamespace();
/** Returns the {@link Source} of the schema document. */
Source getSource();
/** Returns the {@link XsdElement} objects of the schema document. */
XsdElement[] getElements();
}

View File

@@ -1,36 +0,0 @@
/*
* Copyright 2006 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.schema.xsd;
import org.springframework.xml.schema.SchemaException;
/**
* Base class for all XSD schema exceptions.
*
* @author Arjen Poutsma
* @since 1.0.2
*/
public class XsdSchemaException extends SchemaException {
public XsdSchemaException(String reason) {
super(reason);
}
public XsdSchemaException(String reason, Throwable throwable) {
super(reason, throwable);
}
}

View File

@@ -1,6 +0,0 @@
<html>
<body>
Provides the XSD Schema functionality of the Spring Web Services framework. Contains the XsdSchema and XsdSchemaDocument
interfaces.
</body>
</html>

View File

@@ -1,161 +0,0 @@
/*
* Copyright 2007 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.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.xml.namespace.QNameUtils;
import org.springframework.xml.sax.SaxUtils;
import org.springframework.xml.validation.XmlValidator;
import org.springframework.xml.validation.XmlValidatorFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Simple implementation of the {@link XsdSchema} interface. Only wraps a single {@link XsdSchemaDocument}, and does not
* follow <code>&lt;xsd:include/&gt;</code> nor <code>&lt;xsd:import/&gt;</code> elements.
* <p/>
* Allows an XSD {@link Resource resource} to be set by the {@link #setSchema(Resource) schema} property, or directly in
* the {@link #SimpleXsdSchema(Resource) constructor}.
*
* @author Arjen Poutsma
* @since 1.0.2
*/
public class SimpleXsdSchema implements XsdSchema, InitializingBean {
private static final String SCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema";
/** The schema qualified name. */
private static final QName SCHEMA_NAME = QNameUtils.createQName(SCHEMA_NAMESPACE, "schema", "xsd");
/** The element declaration qualified name. */
private static final QName ELEMENT_NAME = QNameUtils.createQName(SCHEMA_NAMESPACE, "element", "xsd");
private final XsdSchemaDocument[] schemaDocuments = new XsdSchemaDocument[]{new SimpleXsdSchemaDocument()};
private Resource schemaResource;
private Element schemaElement;
private XmlValidator validator;
/**
* Create a new instance of the {@link SimpleXsdSchema} class.
* <p/>
* A subsequent call to the {@link #setSchema(Resource)} method is required.
*/
public SimpleXsdSchema() {
}
/**
* Create a new instance of the {@link SimpleXsdSchema} class with the specified resource.
*
* @param schemaResource the XSD resource; must not be <code>null</code>
* @throws IllegalArgumentException if the supplied <code>schemaResource</code> is <code>null</code>
*/
public SimpleXsdSchema(Resource schemaResource) {
setSchema(schemaResource);
}
/** Set the XSD resource to be used. */
public void setSchema(Resource schemaResource) {
this.schemaResource = schemaResource;
}
public XsdSchemaDocument[] getSchemaDocuments() {
return schemaDocuments;
}
public XmlValidator getValidator() {
return validator;
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(schemaResource, "'schema' is required");
Assert.isTrue(schemaResource.exists(), "schema '" + schemaResource + "' does not exit");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(SaxUtils.createInputSource(schemaResource));
schemaElement = document.getDocumentElement();
QName name = QNameUtils.getQNameForNode(schemaElement);
Assert.isTrue(SCHEMA_NAME.equals(name),
"schema document has invalid qualified name: " + name + ". " + "Epected " + SCHEMA_NAME);
Assert.hasLength(schemaElement.getAttribute("targetNamespace"), "schema does not define targetNamespace");
validator = XmlValidatorFactory.createValidator(schemaResource, XmlValidatorFactory.SCHEMA_W3C_XML);
}
/** Inner definition of {@link SimpleXsdSchemaDocument}. */
private class SimpleXsdSchemaDocument implements XsdSchemaDocument {
public String getFilename() {
return schemaResource.getFilename();
}
public Source getSource() {
return new DOMSource(schemaElement);
}
public XsdElementDeclaration[] getElementDeclarations() {
NodeList children = schemaElement.getChildNodes();
List declarations = new ArrayList(children.getLength());
for (int i = 0; i < children.getLength(); i++) {
if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element childElement = (Element) children.item(i);
QName childName = QNameUtils.getQNameForNode(childElement);
if (ELEMENT_NAME.equals(childName)) {
declarations.add(new SimpleXsdElementDeclaration(childElement));
}
}
}
return (XsdElementDeclaration[]) declarations.toArray(new XsdElementDeclaration[declarations.size()]);
}
public String getTargetNamespace() {
return schemaElement.getAttribute("targetNamespace");
}
/** Inner definition of {@link SimpleXsdElementDeclaration}. */
private class SimpleXsdElementDeclaration implements XsdElementDeclaration {
private final Element element;
private SimpleXsdElementDeclaration(Element element) {
this.element = element;
}
public QName getName() {
String attributeValue = element.getAttribute("name");
return StringUtils.hasLength(attributeValue) ? new QName(getTargetNamespace(), attributeValue) : null;
}
}
}
}

View File

@@ -1,176 +0,0 @@
/*
* Copyright 2007 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.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
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.XmlSchemaElement;
import org.apache.ws.commons.schema.XmlSchemaObjectTable;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.xml.sax.SaxUtils;
import org.springframework.xml.validation.XmlValidator;
import org.springframework.xml.validation.XmlValidatorFactory;
/**
* Implementation of the {@link XsdSchema} interface that uses <a href="http://ws.apache.org/commons/XmlSchema/">Apache
* WS Commons XMLSchema</a>. Allows for multiple {@link XsdSchemaDocument}s, and follows
* <code>&lt;xsd:include/&gt;</code> and <code>&lt;xsd:import/&gt;</code> elements.
* <p/>
* Allows an XSD {@link Resource resource} to be set by the {@link #setSchema(Resource) schema} or {@link
* #setSchemas(Resource[]) schemas} properties, or directly in the {@link #WsCommonsXsdSchema(Resource)} or {@link
* #WsCommonsXsdSchema(Resource[])} constructors.
*
* @author Arjen Poutsma
* @since 1.0.2
*/
public class WsCommonsXsdSchema implements XsdSchema, InitializingBean {
private Resource[] schemaResources;
private XsdSchemaDocument[] schemaDocuments;
private XmlValidator validator;
/**
* Create a new instance of the {@link WsCommonsXsdSchema} class.
* <p/>
* A subsequent call to the {@link #setSchema(Resource)} or {@link #setSchemas(Resource[])} method is required.
*/
public WsCommonsXsdSchema() {
}
/**
* Create a new instance of the {@link WsCommonsXsdSchema} class with the specified single resource.
*
* @param schemaResource the XSD resource; must not be <code>null</code>
* @throws IllegalArgumentException if the supplied <code>schemaResource</code> is <code>null</code>
*/
public WsCommonsXsdSchema(Resource schemaResource) {
setSchema(schemaResource);
}
/**
* Create a new instance of the {@link WsCommonsXsdSchema} class with the specified resources.
*
* @param schemaResources the XSD resources; must not be <code>null</code> or empty
* @throws IllegalArgumentException if the supplied <code>schemaResource</code> is <code>null</code>
*/
public WsCommonsXsdSchema(Resource[] schemaResources) {
setSchemas(schemaResources);
}
/** Set the XSD resource to be used. */
public void setSchema(Resource schemaResource) {
this.schemaResources = new Resource[]{schemaResource};
}
/** Set the XSD resources to be used. */
public void setSchemas(Resource[] schemaResources) {
this.schemaResources = schemaResources;
}
public XmlValidator getValidator() {
return validator;
}
public void afterPropertiesSet() throws Exception {
Assert.notEmpty(schemaResources, "'schemaResources' must not be empty");
XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
for (int i = 0; i < schemaResources.length; i++) {
Resource schemaResource = schemaResources[i];
Assert.notNull(schemaResource, "'schemaResource' must not be null");
Assert.isTrue(schemaResource.exists(), "schema \"" + schemaResource + "\" does not exist");
schemaCollection.read(SaxUtils.createInputSource(schemaResource), null);
}
XmlSchema[] schemas = schemaCollection.getXmlSchemas();
List schemaList = new ArrayList(schemaResources.length);
for (int i = 0; i < schemas.length; i++) {
if (!schemas[i].getTargetNamespace().equals("http://www.w3.org/2001/XMLSchema")) {
schemaList.add(new WsCommonsXsdSchemaDocument(schemas[i]));
}
}
this.schemaDocuments = (XsdSchemaDocument[]) schemaList.toArray(new XsdSchemaDocument[schemaList.size()]);
validator = XmlValidatorFactory.createValidator(schemaResources, XmlValidatorFactory.SCHEMA_W3C_XML);
}
public XsdSchemaDocument[] getSchemaDocuments() {
return schemaDocuments;
}
/** Inner definition of {@link WsCommonsXsdSchemaDocument}. */
private static class WsCommonsXsdSchemaDocument implements XsdSchemaDocument {
private final XmlSchema commonsSchema;
private WsCommonsXsdSchemaDocument(XmlSchema commonsSchema) {
this.commonsSchema = commonsSchema;
}
public String getFilename() {
return StringUtils.getFilename(commonsSchema.getSourceURI());
}
public Source getSource() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
commonsSchema.write(bos);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
return new StreamSource(bis);
}
public XsdElementDeclaration[] getElementDeclarations() {
XmlSchemaObjectTable elements = commonsSchema.getElements();
XsdElementDeclaration[] declarations = new XsdElementDeclaration[elements.getCount()];
int i = 0;
for (Iterator iterator = elements.getValues(); iterator.hasNext();) {
XmlSchemaElement element = (XmlSchemaElement) iterator.next();
declarations[i] = new WsCommonsXsdElementDeclaration(element);
i++;
}
return declarations;
}
public String getTargetNamespace() {
return commonsSchema.getTargetNamespace();
}
}
private static class WsCommonsXsdElementDeclaration implements XsdElementDeclaration {
private final XmlSchemaElement element;
private WsCommonsXsdElementDeclaration(XmlSchemaElement element) {
this.element = element;
}
public QName getName() {
return element.getQName();
}
}
}

View File

@@ -1,33 +0,0 @@
/*
* Copyright 2007 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 javax.xml.namespace.QName;
/**
* Represents an abstraction for an XSD element declaration.
*
* @author Arjen Poutsma
* @see XsdSchemaDocument#getElementDeclarations()
* @since 1.0.2
*/
public interface XsdElementDeclaration {
/** Returns the qualified name of the element declaration. */
QName getName();
}

View File

@@ -1,36 +0,0 @@
/*
* Copyright 2007 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.xml.validation.XmlValidator;
/**
* Represents an abstraction for an XSD schema document. A schema is made up of one or more schema documents.
*
* @author Arjen Poutsma
* @see XsdSchemaDocument
* @since 1.0.2
*/
public interface XsdSchema {
/** Returns the {@link XsdSchemaDocument} objects this schema consists of. */
XsdSchemaDocument[] getSchemaDocuments();
/** Returns the {@link XmlValidator} based on this schema. */
XmlValidator getValidator();
}

View File

@@ -1,30 +0,0 @@
package org.springframework.xml.xsd;
import javax.xml.transform.Source;
/**
* Represents an abstraction for an individual XSD schema document. A schema is made up of one or more schema
* documents.
*
* @author Arjen Poutsma
* @see XsdSchema
* @since 1.0.2
*/
public interface XsdSchemaDocument {
/** Return a filename for this schema document. For example, <code>schema.xsd</code>. */
String getFilename();
/** Returns the target namespace of the schema. */
String getTargetNamespace();
/**
* Returns the <code>Source</code> of the schema.
*
* @return the <code>Source</code> of this XSD schema
*/
Source getSource();
XsdElementDeclaration[] getElementDeclarations();
}

View File

@@ -1,36 +0,0 @@
/*
* Copyright 2006 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.xml.XmlException;
/**
* Base class for all XSD schema exceptions.
*
* @author Arjen Poutsma
* @since 1.0.2
*/
public class XsdSchemaException extends XmlException {
public XsdSchemaException(String reason) {
super(reason);
}
public XsdSchemaException(String reason, Throwable throwable) {
super(reason, throwable);
}
}

View File

@@ -1,6 +0,0 @@
<html>
<body>
Provides the XSD functionality of the Spring Web Services framework. Contains the XsdSchema and XsdSchemaDocument
interfaces.
</body>
</html>

View File

@@ -1,167 +0,0 @@
/*
* Copyright 2007 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.schema.xsd;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import org.custommonkey.xmlunit.XMLTestCase;
import org.custommonkey.xmlunit.XMLUnit;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.xml.validation.XmlValidator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public abstract class AbstractXsdSchemaTestCase extends XMLTestCase {
private DocumentBuilder documentBuilder;
private Transformer transformer;
protected final void setUp() throws Exception {
XMLUnit.setIgnoreWhitespace(true);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
}
protected abstract XsdSchema createSchemaInternal(Resource[] schemaResources) throws Exception;
protected XsdSchema createSchema(Resource[] schemaResources) throws Exception {
XsdSchema schema = createSchemaInternal(schemaResources);
if (schema instanceof InitializingBean) {
((InitializingBean) schema).afterPropertiesSet();
}
return schema;
}
public void testSingleDocument() throws Exception {
XsdSchema schema =
createSchema(new Resource[]{new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class)});
XmlValidator validator = schema.getValidator();
assertNotNull("No validator returned", validator);
XsdSchemaDocument[] documents = schema.getSchemaDocuments();
assertEquals("Invalid amount of schema documents", 1, documents.length);
XsdSchemaDocument document = documents[0];
assertNotNull("Document is null", document);
assertEquals("Invalid target namespace", "http://www.springframework.org/spring-ws/single",
document.getTargetNamespace());
verifyDocument("single.xsd", document);
XsdElement[] declarations = document.getElements();
assertEquals("Invalid amount of element declarations", 1, declarations.length);
XsdElement declaration = declarations[0];
assertEquals("Invalid element declaration name",
new QName("http://www.springframework.org/spring-ws/single", "root"), declaration.getName());
}
public void testInclude() throws Exception {
XsdSchema schema =
createSchema(new Resource[]{new ClassPathResource("including.xsd", AbstractXsdSchemaTestCase.class)});
XmlValidator validator = schema.getValidator();
assertNotNull("No validator returned", validator);
XsdSchemaDocument[] documents = schema.getSchemaDocuments();
assertEquals("Invalid amount of schema documents", 2, documents.length);
for (int i = 0; i < documents.length; i++) {
XsdSchemaDocument document = documents[i];
assertNotNull("Document is null", document);
XsdElement[] elements = document.getElements();
if ("including.xsd".equals(document.getFilename())) {
assertEquals("Invalid target namespace", "http://www.springframework.org/spring-ws/include",
document.getTargetNamespace());
verifyDocument("including.xsd", document);
assertEquals("Invalid amount of element declarations", 1, elements.length);
assertEquals("Invalid element declaration name",
new QName("http://www.springframework.org/spring-ws/include", "customElement"),
elements[0].getName());
}
else if ("included.xsd".equals(document.getFilename())) {
assertEquals("Invalid target namespace", "http://www.springframework.org/spring-ws/include",
document.getTargetNamespace());
verifyDocument("included.xsd", document);
assertEquals("Invalid amount of element declarations", 1, elements.length);
assertEquals("Invalid element declaration name",
new QName("http://www.springframework.org/spring-ws/include", "stringElement"),
elements[0].getName());
}
else {
fail("Invalid filename: " + document.getFilename());
}
}
}
public void testImport() throws Exception {
XsdSchema schema =
createSchema(new Resource[]{new ClassPathResource("importing.xsd", AbstractXsdSchemaTestCase.class)});
XmlValidator validator = schema.getValidator();
assertNotNull("No validator returned", validator);
XsdSchemaDocument[] documents = schema.getSchemaDocuments();
assertEquals("Invalid amount of schema documents", 2, documents.length);
for (int i = 0; i < documents.length; i++) {
XsdSchemaDocument document = documents[i];
assertNotNull("Document is null", document);
XsdElement[] elements = document.getElements();
if ("importing.xsd".equals(document.getFilename())) {
assertEquals("Invalid target namespace", "http://www.springframework.org/spring-ws/importing",
document.getTargetNamespace());
verifyDocument("importing.xsd", document);
assertEquals("Invalid amount of element declarations", 1, elements.length);
assertEquals("Invalid element declaration name",
new QName("http://www.springframework.org/spring-ws/importing", "customElement"),
elements[0].getName());
}
else if ("imported.xsd".equals(document.getFilename())) {
assertEquals("Invalid target namespace", "http://www.springframework.org/spring-ws/imported",
document.getTargetNamespace());
verifyDocument("imported.xsd", document);
assertEquals("Invalid amount of element declarations", 1, elements.length);
assertEquals("Invalid element declaration name",
new QName("http://www.springframework.org/spring-ws/imported", "stringElement"),
elements[0].getName());
}
else {
fail("Invalid filename: " + document.getFilename());
}
}
}
private void verifyDocument(String filename, XsdSchemaDocument document)
throws IOException, SAXException, TransformerException {
assertEquals("Invalid document filename", filename, document.getFilename());
InputStream inputStream = AbstractXsdSchemaTestCase.class.getResourceAsStream(filename);
try {
Document expected = documentBuilder.parse(inputStream);
DOMResult result = new DOMResult();
transformer.transform(document.getSource(), result);
assertXMLEqual("Invalid source returned", expected, (Document) result.getNode());
}
finally {
inputStream.close();
}
}
}

View File

@@ -1,32 +0,0 @@
/*
* 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.schema.xsd;
import org.springframework.core.io.Resource;
public class SimpleXsdSchemaTest extends AbstractXsdSchemaTestCase {
protected XsdSchema createSchemaInternal(Resource[] schemaResources) throws Exception {
return new SimpleXsdSchema(schemaResources[0]);
}
public void testInclude() throws Exception {
}
public void testImport() throws Exception {
}
}

View File

@@ -1,29 +0,0 @@
/*
* 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.schema.xsd;
import java.io.IOException;
import org.springframework.core.io.Resource;
public class WsCommonsXsdSchemaTest extends AbstractXsdSchemaTestCase {
protected XsdSchema createSchemaInternal(Resource[] schemaResources) throws IOException {
return new WsCommonsXsdSchema(schemaResources);
}
}

View File

@@ -1,13 +0,0 @@
<?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"
xmlns="http://www.springframework.org/spring-ws/imported" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:simpleType name="customType">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:element name="stringElement" type="xsd:string"/>
</xsd:schema>

View File

@@ -1,11 +0,0 @@
<?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"
xmlns="http://www.springframework.org/spring-ws/importing" elementFormDefault="qualified"
xmlns:imported="http://www.springframework.org/spring-ws/imported" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/spring-ws/imported" schemaLocation="imported.xsd"/>
<xsd:element name="customElement" type="imported:customType"/>
</xsd:schema>

View File

@@ -1,13 +0,0 @@
<?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"
xmlns="http://www.springframework.org/spring-ws/include" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:simpleType name="customType">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:element name="stringElement" type="xsd:string"/>
</xsd:schema>

View File

@@ -1,11 +0,0 @@
<?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"
xmlns="http://www.springframework.org/spring-ws/include" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:include schemaLocation="included.xsd"/>
<xsd:element name="customElement" type="customType"/>
</xsd:schema>

View File

@@ -1,15 +0,0 @@
<?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"
xmlns="http://www.springframework.org/spring-ws/single" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="child"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>