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 ecf6ed4d..cc2e2bcf 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 @@ -28,6 +28,7 @@ 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.XmlSchemaCollection; import org.apache.ws.commons.schema.XmlSchemaSerializer; import org.w3c.dom.Document; @@ -51,6 +52,8 @@ public class CommonsXsdSchema implements XsdSchema { private final XmlSchema schema; + private final XmlSchemaCollection collection; + /** * Create a new instance of the {@link CommonsXsdSchema} class with the specified {@link XmlSchema} reference. * @@ -58,8 +61,21 @@ public class CommonsXsdSchema implements XsdSchema { * @throws IllegalArgumentException if the supplied schema is null */ protected CommonsXsdSchema(XmlSchema schema) { + this(schema, null); + } + + /** + * Create a new instance of the {@link CommonsXsdSchema} class with the specified {@link XmlSchema} and {@link + * XmlSchemaCollection} reference. + * + * @param schema the Commons XmlSchema object; must not be null + * @param collection the Commons XmlSchemaCollection object; can be null + * @throws IllegalArgumentException if the supplied schema is null + */ + protected CommonsXsdSchema(XmlSchema schema, XmlSchemaCollection collection) { Assert.notNull(schema, "'schema' must not be null"); this.schema = schema; + this.collection = collection; } public String getTargetNamespace() { @@ -79,10 +95,13 @@ 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]); + XmlSchemaSerializer serializer = (XmlSchemaSerializer) BeanUtils.instantiateClass(XmlSchemaSerializer.class) + ; + if (collection != null) { + serializer.setExtReg(collection.getExtReg()); + } + Document[] serializedSchemas = serializer.serializeSchema(schema, false); + return new DOMSource(serializedSchemas[0]); } catch (BeanInstantiationException ex) { // ignore diff --git a/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollection.java b/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollection.java index 8856b7ad..e43a984d 100644 --- a/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollection.java +++ b/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollection.java @@ -144,8 +144,8 @@ public class CommonsXsdSchemaCollection implements XsdSchemaCollection, Initiali Resource xsdResource = xsdResources[i]; Assert.isTrue(xsdResource.exists(), xsdResource + " does not exit"); try { - XmlSchema xmlSchema = schemaCollection - .read(SaxUtils.createInputSource(xsdResource), validationEventHandler); + XmlSchema xmlSchema = + schemaCollection.read(SaxUtils.createInputSource(xsdResource), validationEventHandler); xmlSchemas.add(xmlSchema); if (inline) { @@ -167,7 +167,7 @@ public class CommonsXsdSchemaCollection implements XsdSchemaCollection, Initiali XsdSchema[] result = new XsdSchema[xmlSchemas.size()]; for (int i = 0; i < xmlSchemas.size(); i++) { XmlSchema xmlSchema = (XmlSchema) xmlSchemas.get(i); - result[i] = new CommonsXsdSchema(xmlSchema); + result[i] = new CommonsXsdSchema(xmlSchema, schemaCollection); } return result; } @@ -185,16 +185,14 @@ public class CommonsXsdSchemaCollection implements XsdSchemaCollection, Initiali processedIncludes.add(schema); XmlSchemaObjectCollection includes = schema.getIncludes(); for (int i = 0; i < includes.getCount(); i++) { - XmlSchemaExternal external = (XmlSchemaExternal) includes - .getItem(i); + XmlSchemaExternal external = (XmlSchemaExternal) includes.getItem(i); if (external instanceof XmlSchemaInclude) { XmlSchema includedSchema = external.getSchema(); XmlSchemaObjectCollection items = schema.getItems(); if (!processedIncludes.contains(includedSchema)) { inlineIncludes(includedSchema, processedIncludes, processedImports); findImports(includedSchema, processedImports, processedIncludes); - XmlSchemaObjectCollection includeItems = includedSchema - .getItems(); + XmlSchemaObjectCollection includeItems = includedSchema.getItems(); for (int j = 0; j < includeItems.getCount(); j++) { XmlSchemaObject includedItem = includeItems.getItem(j); items.add(includedItem); diff --git a/xml/src/test/java/org/springframework/xml/xsd/AbstractXsdSchemaTestCase.java b/xml/src/test/java/org/springframework/xml/xsd/AbstractXsdSchemaTestCase.java index 65fc3e79..abe36745 100644 --- a/xml/src/test/java/org/springframework/xml/xsd/AbstractXsdSchemaTestCase.java +++ b/xml/src/test/java/org/springframework/xml/xsd/AbstractXsdSchemaTestCase.java @@ -21,10 +21,12 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.dom.DOMSource; import org.custommonkey.xmlunit.XMLTestCase; import org.custommonkey.xmlunit.XMLUnit; import org.w3c.dom.Document; +import org.w3c.dom.Element; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; @@ -98,6 +100,18 @@ public abstract class AbstractXsdSchemaTestCase extends XMLTestCase { assertXMLEqual("Invalid Source returned", expected, result); } + public void testXmime() throws Exception { + Resource resource = new ClassPathResource("xmime.xsd", AbstractXsdSchemaTestCase.class); + XsdSchema schema = createSchema(resource); + String namespace = "urn:test"; + assertEquals("Invalid target namespace", namespace, schema.getTargetNamespace()); + Document result = (Document) ((DOMSource) schema.getSource()).getNode(); + Element schemaElement = result.getDocumentElement(); + Element elementElement = (Element) schemaElement.getFirstChild(); + assertNotNull("No expectedContentTypes found", + elementElement.getAttributeNS("http://www.w3.org/2005/05/xmlmime", "expectedContentTypes")); + } + public void testCreateValidator() throws Exception { Resource resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class); XsdSchema single = createSchema(resource); diff --git a/xml/src/test/resources/org/springframework/xml/xsd/xmime.xsd b/xml/src/test/resources/org/springframework/xml/xsd/xmime.xsd new file mode 100644 index 00000000..394b4871 --- /dev/null +++ b/xml/src/test/resources/org/springframework/xml/xsd/xmime.xsd @@ -0,0 +1,11 @@ + + + + +