diff --git a/xml/src/main/java/org/springframework/xml/xsd/SimpleXsdSchema.java b/xml/src/main/java/org/springframework/xml/xsd/SimpleXsdSchema.java index 1971f845..f3270b27 100644 --- a/xml/src/main/java/org/springframework/xml/xsd/SimpleXsdSchema.java +++ b/xml/src/main/java/org/springframework/xml/xsd/SimpleXsdSchema.java @@ -30,6 +30,8 @@ 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; @@ -61,6 +63,8 @@ public class SimpleXsdSchema implements XsdSchema, InitializingBean { private Element schemaElement; + private XmlValidator validator; + /** * Create a new instance of the {@link SimpleXsdSchema} class. *

@@ -88,6 +92,10 @@ public class SimpleXsdSchema implements XsdSchema, InitializingBean { 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"); @@ -100,6 +108,7 @@ public class SimpleXsdSchema implements XsdSchema, InitializingBean { 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}. */ diff --git a/xml/src/main/java/org/springframework/xml/xsd/WsCommonsXsdSchema.java b/xml/src/main/java/org/springframework/xml/xsd/WsCommonsXsdSchema.java index 5ee2d251..26f5d3fa 100644 --- a/xml/src/main/java/org/springframework/xml/xsd/WsCommonsXsdSchema.java +++ b/xml/src/main/java/org/springframework/xml/xsd/WsCommonsXsdSchema.java @@ -34,6 +34,8 @@ 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 Apache @@ -53,6 +55,8 @@ public class WsCommonsXsdSchema implements XsdSchema, InitializingBean { private XsdSchemaDocument[] schemaDocuments; + private XmlValidator validator; + /** * Create a new instance of the {@link WsCommonsXsdSchema} class. *

@@ -91,6 +95,10 @@ public class WsCommonsXsdSchema implements XsdSchema, InitializingBean { 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(); @@ -108,6 +116,7 @@ public class WsCommonsXsdSchema implements XsdSchema, InitializingBean { } } this.schemaDocuments = (XsdSchemaDocument[]) schemaList.toArray(new XsdSchemaDocument[schemaList.size()]); + validator = XmlValidatorFactory.createValidator(schemaResources, XmlValidatorFactory.SCHEMA_W3C_XML); } public XsdSchemaDocument[] getSchemaDocuments() { diff --git a/xml/src/main/java/org/springframework/xml/xsd/XsdSchema.java b/xml/src/main/java/org/springframework/xml/xsd/XsdSchema.java index a51976c1..7995f47a 100644 --- a/xml/src/main/java/org/springframework/xml/xsd/XsdSchema.java +++ b/xml/src/main/java/org/springframework/xml/xsd/XsdSchema.java @@ -16,6 +16,8 @@ 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. * @@ -28,4 +30,7 @@ public interface XsdSchema { /** Returns the {@link XsdSchemaDocument} objects this schema consists of. */ XsdSchemaDocument[] getSchemaDocuments(); + /** Returns the {@link XmlValidator} based on this schema. */ + XmlValidator getValidator(); + }