1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.xml.xsd.commons;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25 import javax.xml.namespace.QName;
26 import javax.xml.transform.Source;
27 import javax.xml.transform.dom.DOMSource;
28 import javax.xml.transform.stream.StreamSource;
29
30 import org.apache.ws.commons.schema.XmlSchema;
31 import org.apache.ws.commons.schema.XmlSchemaCollection;
32 import org.apache.ws.commons.schema.XmlSchemaSerializer;
33 import org.w3c.dom.Document;
34
35 import org.springframework.beans.BeanInstantiationException;
36 import org.springframework.beans.BeanUtils;
37 import org.springframework.core.io.Resource;
38 import org.springframework.core.io.UrlResource;
39 import org.springframework.util.Assert;
40 import org.springframework.xml.validation.XmlValidator;
41 import org.springframework.xml.validation.XmlValidatorFactory;
42 import org.springframework.xml.xsd.XsdSchema;
43
44
45
46
47
48
49
50
51 public class CommonsXsdSchema implements XsdSchema {
52
53 private final XmlSchema schema;
54
55 private final XmlSchemaCollection collection;
56
57
58
59
60
61
62
63 protected CommonsXsdSchema(XmlSchema schema) {
64 this(schema, null);
65 }
66
67
68
69
70
71
72
73
74
75 protected CommonsXsdSchema(XmlSchema schema, XmlSchemaCollection collection) {
76 Assert.notNull(schema, "'schema' must not be null");
77 this.schema = schema;
78 this.collection = collection;
79 }
80
81 public String getTargetNamespace() {
82 return schema.getTargetNamespace();
83 }
84
85 public QName[] getElementNames() {
86 List result = new ArrayList();
87 Iterator iterator = schema.getElements().getNames();
88 while (iterator.hasNext()) {
89 QName name = (QName) iterator.next();
90 result.add(name);
91 }
92 return (QName[]) result.toArray(new QName[result.size()]);
93 }
94
95 public Source getSource() {
96
97 try {
98 XmlSchemaSerializer serializer = (XmlSchemaSerializer) BeanUtils.instantiateClass(XmlSchemaSerializer.class)
99 ;
100 if (collection != null) {
101 serializer.setExtReg(collection.getExtReg());
102 }
103 Document[] serializedSchemas = serializer.serializeSchema(schema, false);
104 return new DOMSource(serializedSchemas[0]);
105 }
106 catch (BeanInstantiationException ex) {
107
108 }
109 catch (XmlSchemaSerializer.XmlSchemaSerializerException ex) {
110
111 }
112 ByteArrayOutputStream bos = new ByteArrayOutputStream();
113 schema.write(bos);
114 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
115 return new StreamSource(bis);
116 }
117
118 public XmlValidator createValidator() throws IOException {
119 Resource resource = new UrlResource(schema.getSourceURI());
120 return XmlValidatorFactory.createValidator(resource, XmlValidatorFactory.SCHEMA_W3C_XML);
121 }
122
123
124 public XmlSchema getSchema() {
125 return schema;
126 }
127
128 public String toString() {
129 StringBuffer buffer = new StringBuffer("CommonsXsdSchema");
130 buffer.append('{');
131 buffer.append(getTargetNamespace());
132 buffer.append('}');
133 return buffer.toString();
134 }
135
136 }