diff --git a/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchema.java b/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchema.java index c4a9c7d9..368c6db0 100644 --- a/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchema.java +++ b/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchema.java @@ -23,10 +23,15 @@ import java.util.Iterator; import java.util.List; import javax.xml.namespace.QName; import javax.xml.transform.Source; +import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; import org.apache.ws.commons.schema.XmlSchema; +import org.apache.ws.commons.schema.XmlSchemaSerializer; +import org.w3c.dom.Document; +import org.springframework.beans.BeanInstantiationException; +import org.springframework.beans.BeanUtils; import org.springframework.util.Assert; import org.springframework.xml.xsd.XsdSchema; @@ -67,6 +72,19 @@ public class CommonsXsdSchema implements XsdSchema { } public Source getSource() { + // try to use the the package-friendly XmlSchemaSerializer first, fall back to slower stream-based version + try { + XmlSchemaSerializer serializer = + (XmlSchemaSerializer) BeanUtils.instantiateClass(XmlSchemaSerializer.class); + Document[] serializesSchemas = serializer.serializeSchema(schema, false); + return new DOMSource(serializesSchemas[0]); + } + catch (BeanInstantiationException ex) { + // ignore + } + catch (XmlSchemaSerializer.XmlSchemaSerializerException ex) { + // ignore + } ByteArrayOutputStream bos = new ByteArrayOutputStream(); schema.write(bos); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); diff --git a/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaTest.java b/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaTest.java index 3f143a0e..00f19f83 100644 --- a/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaTest.java +++ b/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaTest.java @@ -32,25 +32,4 @@ public class CommonsXsdSchemaTest extends AbstractXsdSchemaTestCase { return new CommonsXsdSchema(schema); } - /* - public void testInline() throws Exception { - Resource resource = new ClassPathResource("A.xsd", AbstractXsdSchemaTestCase.class); - CommonsXsdSchema schema = new CommonsXsdSchema(resource); - XsdSchema[] inlined = schema.inline(); - for (int i = 0; i < inlined.length; i++) { - transformer.transform(inlined[i].getSource(), new StreamResult(System.out)); - System.out.println(); - } - } - - public void testCircular() throws Exception { - Resource resource = new ClassPathResource("circular-1.xsd", AbstractXsdSchemaTestCase.class); - CommonsXsdSchema schema = new CommonsXsdSchema(resource); - XsdSchema[] inlined = schema.inline(); - for (int i = 0; i < inlined.length; i++) { - transformer.transform(inlined[i].getSource(), new StreamResult(System.out)); - System.out.println(); - } - } - */ } \ No newline at end of file