Skip to content

Commit bc35a34

Browse files
committed
Using DOM-based Source retrieval mechanism
1 parent 3eebcba commit bc35a34

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchema.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@
2323
import java.util.List;
2424
import javax.xml.namespace.QName;
2525
import javax.xml.transform.Source;
26+
import javax.xml.transform.dom.DOMSource;
2627
import javax.xml.transform.stream.StreamSource;
2728

2829
import org.apache.ws.commons.schema.XmlSchema;
30+
import org.apache.ws.commons.schema.XmlSchemaSerializer;
31+
import org.w3c.dom.Document;
2932

33+
import org.springframework.beans.BeanInstantiationException;
34+
import org.springframework.beans.BeanUtils;
3035
import org.springframework.util.Assert;
3136
import org.springframework.xml.xsd.XsdSchema;
3237

@@ -67,6 +72,19 @@ public QName[] getElementNames() {
6772
}
6873

6974
public Source getSource() {
75+
// try to use the the package-friendly XmlSchemaSerializer first, fall back to slower stream-based version
76+
try {
77+
XmlSchemaSerializer serializer =
78+
(XmlSchemaSerializer) BeanUtils.instantiateClass(XmlSchemaSerializer.class);
79+
Document[] serializesSchemas = serializer.serializeSchema(schema, false);
80+
return new DOMSource(serializesSchemas[0]);
81+
}
82+
catch (BeanInstantiationException ex) {
83+
// ignore
84+
}
85+
catch (XmlSchemaSerializer.XmlSchemaSerializerException ex) {
86+
// ignore
87+
}
7088
ByteArrayOutputStream bos = new ByteArrayOutputStream();
7189
schema.write(bos);
7290
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());

xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaTest.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,4 @@ protected XsdSchema createSchema(Resource resource) throws Exception {
3232
return new CommonsXsdSchema(schema);
3333
}
3434

35-
/*
36-
public void testInline() throws Exception {
37-
Resource resource = new ClassPathResource("A.xsd", AbstractXsdSchemaTestCase.class);
38-
CommonsXsdSchema schema = new CommonsXsdSchema(resource);
39-
XsdSchema[] inlined = schema.inline();
40-
for (int i = 0; i < inlined.length; i++) {
41-
transformer.transform(inlined[i].getSource(), new StreamResult(System.out));
42-
System.out.println();
43-
}
44-
}
45-
46-
public void testCircular() throws Exception {
47-
Resource resource = new ClassPathResource("circular-1.xsd", AbstractXsdSchemaTestCase.class);
48-
CommonsXsdSchema schema = new CommonsXsdSchema(resource);
49-
XsdSchema[] inlined = schema.inline();
50-
for (int i = 0; i < inlined.length; i++) {
51-
transformer.transform(inlined[i].getSource(), new StreamResult(System.out));
52-
System.out.println();
53-
}
54-
}
55-
*/
5635
}

0 commit comments

Comments
 (0)