Trying to reproduce SWS-176
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 <code>javax.xml.validation.Schema</code> 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 <code>null</code> if it cannot be openened. */
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<multipleSchemas1 xmlns="http://www.springframework.org/spring-ws/test/multipleSchemas1">abc</multipleSchemas1>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://www.springframework.org/spring-ws/test/multipleSchemas1"
|
||||
elementFormDefault="qualified">
|
||||
|
||||
<element name="multipleSchemas1">
|
||||
<simpleType>
|
||||
<restriction base="string">
|
||||
<pattern value="[a-c]+"/>
|
||||
</restriction>
|
||||
</simpleType>
|
||||
</element>
|
||||
|
||||
</schema>
|
||||
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<multipleSchemas2 xmlns="http://www.springframework.org/spring-ws/test/multipleSchemas2">123</multipleSchemas2>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://www.springframework.org/spring-ws/test/multipleSchemas2"
|
||||
elementFormDefault="qualified">
|
||||
|
||||
<element name="multipleSchemas2">
|
||||
<simpleType>
|
||||
<restriction base="string">
|
||||
<pattern value="[1-3]+"/>
|
||||
</restriction>
|
||||
</simpleType>
|
||||
</element>
|
||||
|
||||
</schema>
|
||||
Reference in New Issue
Block a user