diff --git a/xml/src/main/java/org/springframework/xml/validation/Jaxp10ValidatorFactory.java b/xml/src/main/java/org/springframework/xml/validation/Jaxp10ValidatorFactory.java index 88443ab5..192cb6ce 100644 --- a/xml/src/main/java/org/springframework/xml/validation/Jaxp10ValidatorFactory.java +++ b/xml/src/main/java/org/springframework/xml/validation/Jaxp10ValidatorFactory.java @@ -34,6 +34,7 @@ import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.springframework.core.io.Resource; +import org.springframework.xml.sax.SaxUtils; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; @@ -54,8 +55,7 @@ abstract class Jaxp10ValidatorFactory { static XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws IOException { InputSource[] inputSources = new InputSource[schemaResources.length]; for (int i = 0; i < schemaResources.length; i++) { - inputSources[i] = new InputSource(schemaResources[i].getInputStream()); - inputSources[i].setSystemId(SchemaLoaderUtils.getSystemId(schemaResources[i])); + inputSources[i] = SaxUtils.createInputSource(schemaResources[i]); } return new Jaxp10Validator(inputSources, schemaLanguage); } diff --git a/xml/src/main/java/org/springframework/xml/validation/SchemaLoaderUtils.java b/xml/src/main/java/org/springframework/xml/validation/SchemaLoaderUtils.java index 7864f12f..09def490 100644 --- a/xml/src/main/java/org/springframework/xml/validation/SchemaLoaderUtils.java +++ b/xml/src/main/java/org/springframework/xml/validation/SchemaLoaderUtils.java @@ -17,18 +17,17 @@ package org.springframework.xml.validation; import java.io.IOException; -import java.io.InputStream; -import javax.xml.transform.stream.StreamSource; +import javax.xml.transform.Source; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import org.springframework.core.io.Resource; import org.springframework.util.Assert; +import org.springframework.xml.transform.ResourceSource; import org.xml.sax.SAXException; /** - * Convenient utility methods for loading of javax.xml.validation.Schema objects, performing standard - * handling of input streams. + * Convenient utility methods for loading of {@link Schema} objects, performing standard handling of input streams. * * @author Arjen Poutsma * @since 1.0.0 @@ -64,26 +63,14 @@ public abstract class SchemaLoaderUtils { public static Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException { Assert.notEmpty(resources, "No resources given"); Assert.hasLength(schemaLanguage, "No schema language provided"); - StreamSource[] schemaSources = new StreamSource[resources.length]; - try { - for (int i = 0; i < resources.length; i++) { - Assert.notNull(resources[i], "Resource is null"); - Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist"); - schemaSources[i] = new StreamSource(resources[i].getInputStream(), getSystemId(resources[i])); - } - SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage); - return schemaFactory.newSchema(schemaSources); - } - finally { - for (int i = 0; i < schemaSources.length; i++) { - if (schemaSources[i] != null) { - InputStream inputStream = schemaSources[i].getInputStream(); - if (inputStream != null) { - inputStream.close(); - } - } - } + Source[] schemaSources = new Source[resources.length]; + for (int i = 0; i < resources.length; i++) { + Assert.notNull(resources[i], "Resource is null"); + Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist"); + schemaSources[i] = new ResourceSource(resources[i]); } + SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage); + return schemaFactory.newSchema(schemaSources); } /** Retrieves the URL from the given resource as System ID. Returns null if it cannot be openened. */ diff --git a/xml/src/test/java/org/springframework/xml/validation/AbstractValidatorFactoryTestCase.java b/xml/src/test/java/org/springframework/xml/validation/AbstractValidatorFactoryTestCase.java index 656533db..3d0f81f2 100644 --- a/xml/src/test/java/org/springframework/xml/validation/AbstractValidatorFactoryTestCase.java +++ b/xml/src/test/java/org/springframework/xml/validation/AbstractValidatorFactoryTestCase.java @@ -18,6 +18,7 @@ package org.springframework.xml.validation; import java.io.InputStream; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamSource; @@ -25,6 +26,7 @@ import javax.xml.transform.stream.StreamSource; import junit.framework.TestCase; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; +import org.springframework.xml.transform.ResourceSource; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXParseException; @@ -102,4 +104,21 @@ public abstract class AbstractValidatorFactoryTestCase extends TestCase { assertEquals("ValidationErrors returned", 3, errors.length); } + public void testMultipleSchemasValidMessage() throws Exception { + Resource[] schemaResources = new Resource[]{ + new ClassPathResource("multipleSchemas1.xsd", AbstractValidatorFactoryTestCase.class), + new ClassPathResource("multipleSchemas2.xsd", AbstractValidatorFactoryTestCase.class)}; + validator = createValidator(schemaResources, XmlValidatorFactory.SCHEMA_W3C_XML); + + Source document = new ResourceSource( + new ClassPathResource("multipleSchemas1.xml", AbstractValidatorFactoryTestCase.class)); + SAXParseException[] errors = validator.validate(document); + assertEquals("ValidationErrors returned", 0, errors.length); + validator = createValidator(schemaResources, XmlValidatorFactory.SCHEMA_W3C_XML); + document = new ResourceSource( + new ClassPathResource("multipleSchemas2.xml", AbstractValidatorFactoryTestCase.class)); + errors = validator.validate(document); + assertEquals("ValidationErrors returned", 0, errors.length); + } + } diff --git a/xml/src/test/resources/org/springframework/xml/validation/multipleSchemas1.xml b/xml/src/test/resources/org/springframework/xml/validation/multipleSchemas1.xml new file mode 100644 index 00000000..9d4dac66 --- /dev/null +++ b/xml/src/test/resources/org/springframework/xml/validation/multipleSchemas1.xml @@ -0,0 +1,2 @@ + +abc \ No newline at end of file diff --git a/xml/src/test/resources/org/springframework/xml/validation/multipleSchemas1.xsd b/xml/src/test/resources/org/springframework/xml/validation/multipleSchemas1.xsd new file mode 100644 index 00000000..5d474711 --- /dev/null +++ b/xml/src/test/resources/org/springframework/xml/validation/multipleSchemas1.xsd @@ -0,0 +1,14 @@ + + + + + + + + + + + + diff --git a/xml/src/test/resources/org/springframework/xml/validation/multipleSchemas2.xml b/xml/src/test/resources/org/springframework/xml/validation/multipleSchemas2.xml new file mode 100644 index 00000000..de67c5ad --- /dev/null +++ b/xml/src/test/resources/org/springframework/xml/validation/multipleSchemas2.xml @@ -0,0 +1,2 @@ + +123 \ No newline at end of file diff --git a/xml/src/test/resources/org/springframework/xml/validation/multipleSchemas2.xsd b/xml/src/test/resources/org/springframework/xml/validation/multipleSchemas2.xsd new file mode 100644 index 00000000..e6ecc97e --- /dev/null +++ b/xml/src/test/resources/org/springframework/xml/validation/multipleSchemas2.xsd @@ -0,0 +1,14 @@ + + + + + + + + + + + +