Skip to content

Commit 917f939

Browse files
committed
SWS-281
1 parent 2d974e7 commit 917f939

File tree

8 files changed

+152
-203
lines changed

8 files changed

+152
-203
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@
662662
<link>http://jakarta.apache.org/commons/httpclient/apidocs/</link>
663663
<link>http://ws.apache.org/wss4j/apidocs/</link>
664664
<link>http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/</link>
665+
<link>http://ws.apache.org/commons/XmlSchema/apidocs/</link>
665666
</links>
666667
</configuration>
667668
</plugin>

sandbox/src/test/java/org/springframework/ws/wsdl/wsdl11/CommonXsdTest.java

Lines changed: 0 additions & 101 deletions
This file was deleted.

xml/src/main/java/org/springframework/xml/xsd/XsdSchema.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
public interface XsdSchema {
3030

3131
/**
32-
* Returns the target namespace of theis schema.
32+
* Returns the target namespace of this schema.
3333
*
3434
* @return the target namespace
3535
*/
3636
String getTargetNamespace();
3737

3838
/**
39-
* Returns the qualified names of all top-level elements declared in the schema. This excludes elements declared as child of
40-
* another <code>element</code>, <code>simplyType</code>, or <code>complexType</code>.
39+
* Returns the qualified names of all top-level elements declared in the schema. This excludes elements declared as
40+
* child of another <code>element</code>, <code>simplyType</code>, or <code>complexType</code>.
4141
*
4242
* @return the top-level element names
4343
*/

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

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.ByteArrayInputStream;
2020
import java.io.ByteArrayOutputStream;
21-
import java.io.IOException;
2221
import java.util.ArrayList;
2322
import java.util.Iterator;
2423
import java.util.List;
@@ -27,9 +26,6 @@
2726
import javax.xml.transform.stream.StreamSource;
2827

2928
import org.apache.ws.commons.schema.XmlSchema;
30-
import org.apache.ws.commons.schema.XmlSchemaCollection;
31-
import org.apache.ws.commons.schema.XmlSchemaObjectCollection;
32-
import org.xml.sax.SAXException;
3329

3430
import org.springframework.util.Assert;
3531
import org.springframework.xml.xsd.XsdSchema;
@@ -43,7 +39,7 @@
4339
*/
4440
public class CommonsXsdSchema implements XsdSchema {
4541

46-
private XmlSchema schema;
42+
private final XmlSchema schema;
4743

4844
/**
4945
* Create a new instance of the {@link CommonsXsdSchema} class with the specified {@link XmlSchema} reference.
@@ -70,17 +66,6 @@ public QName[] getElementNames() {
7066
return (QName[]) result.toArray(new QName[result.size()]);
7167
}
7268

73-
public void merge(XsdSchema o) {
74-
Assert.isInstanceOf(CommonsXsdSchema.class, o);
75-
XmlSchema otherSchema = ((CommonsXsdSchema) o).schema;
76-
Assert.isTrue(this.schema.getTargetNamespace().equals(otherSchema.getTargetNamespace()),
77-
"Schema does not have same namespace");
78-
XmlSchemaObjectCollection otherItems = otherSchema.getItems();
79-
for (int i = 0; i < otherItems.getCount(); i++) {
80-
schema.getItems().add(otherItems.getItem(i));
81-
}
82-
}
83-
8469
public Source getSource() {
8570
ByteArrayOutputStream bos = new ByteArrayOutputStream();
8671
schema.write(bos);
@@ -93,51 +78,6 @@ public XmlSchema getSchema() {
9378
return schema;
9479
}
9580

96-
private void loadSchema() throws SAXException, IOException {
97-
XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
98-
// this.schema = schemaCollection.read(SaxUtils.createInputSource(xsdResource), null);
99-
}
100-
101-
/*
102-
public XsdSchema[] inline() {
103-
XmlSchema clone = cloneSchema(schema);
104-
inlineIncludes(clone, new ArrayList());
105-
return new XsdSchema[]{new CommonsXsdSchema(clone)};
106-
}
107-
108-
private static XmlSchema cloneSchema(XmlSchema schema) {
109-
XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
110-
XmlSchema clone = new XmlSchema(schemaCollection);
111-
XmlSchemaObjectCollection originalItems = schema.getItems();
112-
XmlSchemaObjectCollection cloneItems = clone.getItems();
113-
for (int i = 0; i < originalItems.getCount(); i++) {
114-
cloneItems.add(originalItems.getItem(i));
115-
}
116-
return clone;
117-
}
118-
119-
private static void inlineIncludes(XmlSchema schema, List processedSchemas) {
120-
processedSchemas.add(schema);
121-
XmlSchemaObjectCollection includes = schema.getIncludes();
122-
for (int i = 0; i < includes.getCount(); i++) {
123-
if (includes.getItem(i) instanceof XmlSchemaInclude) {
124-
XmlSchemaInclude include = (XmlSchemaInclude) includes.getItem(i);
125-
XmlSchema includedSchema = include.getSchema();
126-
XmlSchemaObjectCollection items = schema.getItems();
127-
if (!processedSchemas.contains(includedSchema)) {
128-
inlineIncludes(includedSchema, processedSchemas);
129-
XmlSchemaObjectCollection includesItems = includedSchema.getItems();
130-
for (int j = 0; j < includesItems.getCount(); j++) {
131-
XmlSchemaObject includedItem = includesItems.getItem(j);
132-
items.add(includedItem);
133-
}
134-
}
135-
// remove the <include/>
136-
items.remove(include);
137-
}
138-
}
139-
}*/
140-
14181
public String toString() {
14282
StringBuffer buffer = new StringBuffer("CommonsXsdSchema");
14383
buffer.append('{');

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

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616

1717
package org.springframework.xml.xsd.commons;
1818

19+
import java.io.IOException;
1920
import java.util.ArrayList;
2021
import java.util.List;
2122

22-
import org.apache.commons.logging.Log;
23-
import org.apache.commons.logging.LogFactory;
23+
import org.apache.ws.commons.schema.ValidationEventHandler;
2424
import org.apache.ws.commons.schema.XmlSchema;
2525
import org.apache.ws.commons.schema.XmlSchemaCollection;
26-
import org.apache.ws.commons.schema.constants.Constants;
26+
import org.apache.ws.commons.schema.XmlSchemaExternal;
27+
import org.apache.ws.commons.schema.XmlSchemaImport;
28+
import org.apache.ws.commons.schema.XmlSchemaInclude;
29+
import org.apache.ws.commons.schema.XmlSchemaObject;
30+
import org.apache.ws.commons.schema.XmlSchemaObjectCollection;
2731

2832
import org.springframework.beans.factory.InitializingBean;
2933
import org.springframework.core.io.Resource;
@@ -34,22 +38,26 @@
3438

3539
/**
3640
* Implementation of the {@link XsdSchemaCollection} that uses Apache WS-Commons XML Schema.
41+
* <p/>
42+
* Setting the {@link #setInline(boolean) inline} flag to <code>true</code> will result in all referenced schemas
43+
* (included and imported) being merged into the referred schema. When including the schemas into a WSDL, this greatly
44+
* simplifies the deloyment of the schemas.
3745
*
3846
* @author Arjen Poutsma
3947
* @see <a href="http://ws.apache.org/commons/XmlSchema/">Commons XML Schema</a>
4048
* @since 1.5.0
4149
*/
4250
public class CommonsXsdSchemaCollection implements XsdSchemaCollection, InitializingBean {
4351

44-
private static final Log logger = LogFactory.getLog(CommonsXsdSchemaCollection.class);
45-
4652
private final XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
4753

4854
private final List xmlSchemas = new ArrayList();
4955

5056
private Resource[] xsdResources;
5157

52-
private boolean inlineIncludes = false;
58+
private boolean inline = false;
59+
60+
private ValidationEventHandler validationEventHandler;
5361

5462
/**
5563
* Constructs a new, empty instance of the <code>CommonsXsdSchemaCollection</code>.
@@ -82,15 +90,26 @@ public void setXsds(Resource[] xsdResources) {
8290
* <p/>
8391
* Defaults to <code>false</code>.
8492
*/
85-
public void setInlineIncludes(boolean inlineIncludes) {
86-
this.inlineIncludes = inlineIncludes;
93+
public void setInline(boolean inline) {
94+
this.inline = inline;
8795
}
8896

89-
public void afterPropertiesSet() throws Exception {
97+
/** Sets the WS-Commons validation event handler to use while parsing schemas. */
98+
public void setValidationEventHandler(ValidationEventHandler validationEventHandler) {
99+
this.validationEventHandler = validationEventHandler;
100+
}
101+
102+
public void afterPropertiesSet() throws IOException {
90103
Assert.notEmpty(xsdResources, "'xsds' must not be empty");
91104
for (int i = 0; i < xsdResources.length; i++) {
92105
Assert.isTrue(xsdResources[i].exists(), xsdResources[i] + " does not exit");
93-
xmlSchemas.add(schemaCollection.read(SaxUtils.createInputSource(xsdResources[i]), null));
106+
XmlSchema xmlSchema =
107+
schemaCollection.read(SaxUtils.createInputSource(xsdResources[i]), validationEventHandler);
108+
xmlSchemas.add(xmlSchema);
109+
if (inline) {
110+
inlineIncludes(xmlSchema, new ArrayList());
111+
findImports(xmlSchema, new ArrayList());
112+
}
94113
}
95114
}
96115

@@ -103,16 +122,55 @@ public XsdSchema[] getXsdSchemas() {
103122
return result;
104123
}
105124

125+
private void inlineIncludes(XmlSchema schema, List processedSchemas) {
126+
processedSchemas.add(schema);
127+
XmlSchemaObjectCollection includes = schema.getIncludes();
128+
for (int i = 0; i < includes.getCount(); i++) {
129+
XmlSchemaExternal external = (XmlSchemaExternal) includes.getItem(i);
130+
if (external instanceof XmlSchemaInclude) {
131+
XmlSchema includedSchema = external.getSchema();
132+
XmlSchemaObjectCollection items = schema.getItems();
133+
if (!processedSchemas.contains(includedSchema)) {
134+
inlineIncludes(includedSchema, processedSchemas);
135+
findImports(includedSchema, new ArrayList());
136+
XmlSchemaObjectCollection includeItems = includedSchema.getItems();
137+
for (int j = 0; j < includeItems.getCount(); j++) {
138+
XmlSchemaObject includedItem = includeItems.getItem(j);
139+
items.add(includedItem);
140+
}
141+
}
142+
// remove the <include/>
143+
items.remove(external);
144+
}
145+
}
146+
}
147+
148+
private void findImports(XmlSchema schema, List processedSchemas) {
149+
processedSchemas.add(schema);
150+
XmlSchemaObjectCollection includes = schema.getIncludes();
151+
for (int i = 0; i < includes.getCount(); i++) {
152+
XmlSchemaExternal external = (XmlSchemaExternal) includes.getItem(i);
153+
if (external instanceof XmlSchemaImport) {
154+
XmlSchema importedSchema = external.getSchema();
155+
if (!processedSchemas.contains(importedSchema)) {
156+
inlineIncludes(importedSchema, processedSchemas);
157+
findImports(importedSchema, processedSchemas);
158+
xmlSchemas.add(importedSchema);
159+
}
160+
// remove the schemaLocation
161+
external.setSchemaLocation(null);
162+
}
163+
}
164+
}
165+
106166
public String toString() {
107167
StringBuffer buffer = new StringBuffer("CommonsXsdSchemaCollection");
108168
buffer.append('{');
109-
XmlSchema[] schemas = schemaCollection.getXmlSchemas();
110-
for (int i = 0; i < schemas.length; i++) {
111-
if (!Constants.URI_2001_SCHEMA_XSD.equals(schemas[i].getTargetNamespace())) {
112-
buffer.append(schemas[i].getTargetNamespace());
113-
if (i < schemas.length - 1) {
114-
buffer.append(',');
115-
}
169+
for (int i = 0; i < xmlSchemas.size(); i++) {
170+
XmlSchema schema = (XmlSchema) xmlSchemas.get(i);
171+
buffer.append(schema.getTargetNamespace());
172+
if (i < xmlSchemas.size() - 1) {
173+
buffer.append(',');
116174
}
117175
}
118176
buffer.append('}');

0 commit comments

Comments
 (0)