Skip to content

Commit ec66469

Browse files
committed
Trying to reproduce SWS-176
1 parent b3c9cb7 commit ec66469

File tree

7 files changed

+63
-25
lines changed

7 files changed

+63
-25
lines changed

xml/src/main/java/org/springframework/xml/validation/Jaxp10ValidatorFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import javax.xml.transform.stream.StreamSource;
3535

3636
import org.springframework.core.io.Resource;
37+
import org.springframework.xml.sax.SaxUtils;
3738
import org.xml.sax.InputSource;
3839
import org.xml.sax.SAXException;
3940
import org.xml.sax.SAXParseException;
@@ -54,8 +55,7 @@ abstract class Jaxp10ValidatorFactory {
5455
static XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws IOException {
5556
InputSource[] inputSources = new InputSource[schemaResources.length];
5657
for (int i = 0; i < schemaResources.length; i++) {
57-
inputSources[i] = new InputSource(schemaResources[i].getInputStream());
58-
inputSources[i].setSystemId(SchemaLoaderUtils.getSystemId(schemaResources[i]));
58+
inputSources[i] = SaxUtils.createInputSource(schemaResources[i]);
5959
}
6060
return new Jaxp10Validator(inputSources, schemaLanguage);
6161
}

xml/src/main/java/org/springframework/xml/validation/SchemaLoaderUtils.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@
1717
package org.springframework.xml.validation;
1818

1919
import java.io.IOException;
20-
import java.io.InputStream;
21-
import javax.xml.transform.stream.StreamSource;
20+
import javax.xml.transform.Source;
2221
import javax.xml.validation.Schema;
2322
import javax.xml.validation.SchemaFactory;
2423

2524
import org.springframework.core.io.Resource;
2625
import org.springframework.util.Assert;
26+
import org.springframework.xml.transform.ResourceSource;
2727
import org.xml.sax.SAXException;
2828

2929
/**
30-
* Convenient utility methods for loading of <code>javax.xml.validation.Schema</code> objects, performing standard
31-
* handling of input streams.
30+
* Convenient utility methods for loading of {@link Schema} objects, performing standard handling of input streams.
3231
*
3332
* @author Arjen Poutsma
3433
* @since 1.0.0
@@ -64,26 +63,14 @@ public static Schema loadSchema(Resource resource, String schemaLanguage) throws
6463
public static Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
6564
Assert.notEmpty(resources, "No resources given");
6665
Assert.hasLength(schemaLanguage, "No schema language provided");
67-
StreamSource[] schemaSources = new StreamSource[resources.length];
68-
try {
69-
for (int i = 0; i < resources.length; i++) {
70-
Assert.notNull(resources[i], "Resource is null");
71-
Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
72-
schemaSources[i] = new StreamSource(resources[i].getInputStream(), getSystemId(resources[i]));
73-
}
74-
SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
75-
return schemaFactory.newSchema(schemaSources);
76-
}
77-
finally {
78-
for (int i = 0; i < schemaSources.length; i++) {
79-
if (schemaSources[i] != null) {
80-
InputStream inputStream = schemaSources[i].getInputStream();
81-
if (inputStream != null) {
82-
inputStream.close();
83-
}
84-
}
85-
}
66+
Source[] schemaSources = new Source[resources.length];
67+
for (int i = 0; i < resources.length; i++) {
68+
Assert.notNull(resources[i], "Resource is null");
69+
Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
70+
schemaSources[i] = new ResourceSource(resources[i]);
8671
}
72+
SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
73+
return schemaFactory.newSchema(schemaSources);
8774
}
8875

8976
/** Retrieves the URL from the given resource as System ID. Returns <code>null</code> if it cannot be openened. */

xml/src/test/java/org/springframework/xml/validation/AbstractValidatorFactoryTestCase.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818

1919
import java.io.InputStream;
2020
import javax.xml.parsers.DocumentBuilderFactory;
21+
import javax.xml.transform.Source;
2122
import javax.xml.transform.dom.DOMSource;
2223
import javax.xml.transform.sax.SAXSource;
2324
import javax.xml.transform.stream.StreamSource;
2425

2526
import junit.framework.TestCase;
2627
import org.springframework.core.io.ClassPathResource;
2728
import org.springframework.core.io.Resource;
29+
import org.springframework.xml.transform.ResourceSource;
2830
import org.w3c.dom.Document;
2931
import org.xml.sax.InputSource;
3032
import org.xml.sax.SAXParseException;
@@ -102,4 +104,21 @@ public void testHandleInvalidMessageDom() throws Exception {
102104
assertEquals("ValidationErrors returned", 3, errors.length);
103105
}
104106

107+
public void testMultipleSchemasValidMessage() throws Exception {
108+
Resource[] schemaResources = new Resource[]{
109+
new ClassPathResource("multipleSchemas1.xsd", AbstractValidatorFactoryTestCase.class),
110+
new ClassPathResource("multipleSchemas2.xsd", AbstractValidatorFactoryTestCase.class)};
111+
validator = createValidator(schemaResources, XmlValidatorFactory.SCHEMA_W3C_XML);
112+
113+
Source document = new ResourceSource(
114+
new ClassPathResource("multipleSchemas1.xml", AbstractValidatorFactoryTestCase.class));
115+
SAXParseException[] errors = validator.validate(document);
116+
assertEquals("ValidationErrors returned", 0, errors.length);
117+
validator = createValidator(schemaResources, XmlValidatorFactory.SCHEMA_W3C_XML);
118+
document = new ResourceSource(
119+
new ClassPathResource("multipleSchemas2.xml", AbstractValidatorFactoryTestCase.class));
120+
errors = validator.validate(document);
121+
assertEquals("ValidationErrors returned", 0, errors.length);
122+
}
123+
105124
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<multipleSchemas1 xmlns="http://www.springframework.org/spring-ws/test/multipleSchemas1">abc</multipleSchemas1>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<schema xmlns="http://www.w3.org/2001/XMLSchema"
3+
targetNamespace="http://www.springframework.org/spring-ws/test/multipleSchemas1"
4+
elementFormDefault="qualified">
5+
6+
<element name="multipleSchemas1">
7+
<simpleType>
8+
<restriction base="string">
9+
<pattern value="[a-c]+"/>
10+
</restriction>
11+
</simpleType>
12+
</element>
13+
14+
</schema>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<multipleSchemas2 xmlns="http://www.springframework.org/spring-ws/test/multipleSchemas2">123</multipleSchemas2>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<schema xmlns="http://www.w3.org/2001/XMLSchema"
3+
targetNamespace="http://www.springframework.org/spring-ws/test/multipleSchemas2"
4+
elementFormDefault="qualified">
5+
6+
<element name="multipleSchemas2">
7+
<simpleType>
8+
<restriction base="string">
9+
<pattern value="[1-3]+"/>
10+
</restriction>
11+
</simpleType>
12+
</element>
13+
14+
</schema>

0 commit comments

Comments
 (0)