Moved xsd to schema package.

This commit is contained in:
Arjen Poutsma
2007-10-28 14:24:45 +00:00
parent 4dd158a1d6
commit 2912ae3afa
18 changed files with 559 additions and 15 deletions

View File

@@ -0,0 +1,31 @@
/*
* 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

@@ -0,0 +1,36 @@
/*
* 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

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

View File

@@ -0,0 +1,161 @@
/*
* 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

@@ -0,0 +1,176 @@
/*
* 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

@@ -0,0 +1,33 @@
/*
* 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

@@ -0,0 +1,33 @@
/*
* 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

@@ -0,0 +1,27 @@
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

@@ -0,0 +1,36 @@
/*
* 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

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

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.xml.xsd;
package org.springframework.xml.schema.xsd;
import java.io.IOException;
import java.io.InputStream;
@@ -72,9 +72,9 @@ public abstract class AbstractXsdSchemaTestCase extends XMLTestCase {
assertEquals("Invalid target namespace", "http://www.springframework.org/spring-ws/single",
document.getTargetNamespace());
verifyDocument("single.xsd", document);
XsdElementDeclaration[] declarations = document.getElementDeclarations();
XsdElement[] declarations = document.getElements();
assertEquals("Invalid amount of element declarations", 1, declarations.length);
XsdElementDeclaration declaration = declarations[0];
XsdElement declaration = declarations[0];
assertEquals("Invalid element declaration name",
new QName("http://www.springframework.org/spring-ws/single", "root"), declaration.getName());
}
@@ -89,24 +89,24 @@ public abstract class AbstractXsdSchemaTestCase extends XMLTestCase {
for (int i = 0; i < documents.length; i++) {
XsdSchemaDocument document = documents[i];
assertNotNull("Document is null", document);
XsdElementDeclaration[] elementDeclarations = document.getElementDeclarations();
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, elementDeclarations.length);
assertEquals("Invalid amount of element declarations", 1, elements.length);
assertEquals("Invalid element declaration name",
new QName("http://www.springframework.org/spring-ws/include", "customElement"),
elementDeclarations[0].getName());
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, elementDeclarations.length);
assertEquals("Invalid amount of element declarations", 1, elements.length);
assertEquals("Invalid element declaration name",
new QName("http://www.springframework.org/spring-ws/include", "stringElement"),
elementDeclarations[0].getName());
elements[0].getName());
}
else {
fail("Invalid filename: " + document.getFilename());
@@ -124,24 +124,24 @@ public abstract class AbstractXsdSchemaTestCase extends XMLTestCase {
for (int i = 0; i < documents.length; i++) {
XsdSchemaDocument document = documents[i];
assertNotNull("Document is null", document);
XsdElementDeclaration[] elementDeclarations = document.getElementDeclarations();
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, elementDeclarations.length);
assertEquals("Invalid amount of element declarations", 1, elements.length);
assertEquals("Invalid element declaration name",
new QName("http://www.springframework.org/spring-ws/importing", "customElement"),
elementDeclarations[0].getName());
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, elementDeclarations.length);
assertEquals("Invalid amount of element declarations", 1, elements.length);
assertEquals("Invalid element declaration name",
new QName("http://www.springframework.org/spring-ws/imported", "stringElement"),
elementDeclarations[0].getName());
elements[0].getName());
}
else {
fail("Invalid filename: " + document.getFilename());

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.xml.xsd;
package org.springframework.xml.schema.xsd;
import org.springframework.core.io.Resource;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.xml.xsd;
package org.springframework.xml.schema.xsd;
import java.io.IOException;