This commit is contained in:
Arjen Poutsma
2008-12-19 12:26:16 +00:00
parent 0e53f82179
commit 0efc706c6b
4 changed files with 53 additions and 11 deletions

View File

@@ -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 <code>schema</code> is <code>null</code>
*/
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 <code>XmlSchema</code> object; must not be <code>null</code>
* @param collection the Commons <code>XmlSchemaCollection</code> object; can be <code>null</code>
* @throws IllegalArgumentException if the supplied <code>schema</code> is <code>null</code>
*/
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

View File

@@ -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);

View File

@@ -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);

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:test"
xmlns="urn:test"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:xmime="http://www.w3.org/2005/05/xmlmime">
<xs:element name="BinaryData" type="xs:base64Binary" xmime:expectedContentTypes="application/octet-stream"/>
</xs:schema>