Skip to content

Commit 2d974e7

Browse files
committed
Working on XsdSchema
1 parent ced2c8b commit 2d974e7

File tree

5 files changed

+211
-68
lines changed

5 files changed

+211
-68
lines changed

xml/src/main/java/org/springframework/xml/xsd/InlineableXsdSchema.java renamed to xml/src/main/java/org/springframework/xml/xsd/XsdSchemaCollection.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@
1717
package org.springframework.xml.xsd;
1818

1919
/**
20-
* Represents an abstraction for XSD schemas.
21-
20+
* Represents an abstraction for a collection of XSD schemas.
21+
*
2222
* @author Arjen Poutsma
2323
* @since 1.5.0
2424
*/
25-
public interface InlineableXsdSchema extends XsdSchema {
25+
public interface XsdSchemaCollection {
2626

2727
/**
28-
* Inlines this schema into a set of self-contained schema's.
29-
* */
30-
XsdSchema[] inline();
28+
* Returns all schema's contained in this collection.
29+
*
30+
* @return the schema's contained in this collection
31+
*/
32+
XsdSchema[] getXsdSchemas();
3133
}

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

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,18 @@
2020
import java.io.ByteArrayOutputStream;
2121
import java.io.IOException;
2222
import java.util.ArrayList;
23-
import java.util.HashMap;
2423
import java.util.Iterator;
2524
import java.util.List;
26-
import java.util.Map;
2725
import javax.xml.namespace.QName;
2826
import javax.xml.transform.Source;
2927
import javax.xml.transform.stream.StreamSource;
3028

3129
import org.apache.ws.commons.schema.XmlSchema;
3230
import org.apache.ws.commons.schema.XmlSchemaCollection;
33-
import org.apache.ws.commons.schema.XmlSchemaInclude;
34-
import org.apache.ws.commons.schema.XmlSchemaObject;
3531
import org.apache.ws.commons.schema.XmlSchemaObjectCollection;
3632
import org.xml.sax.SAXException;
3733

38-
import org.springframework.beans.factory.InitializingBean;
39-
import org.springframework.core.io.Resource;
4034
import org.springframework.util.Assert;
41-
import org.springframework.xml.sax.SaxUtils;
42-
import org.springframework.xml.xsd.InlineableXsdSchema;
4335
import org.springframework.xml.xsd.XsdSchema;
4436

4537
/**
@@ -49,51 +41,21 @@
4941
* @see <a href="http://ws.apache.org/commons/XmlSchema/">Commons XML Schema</a>
5042
* @since 1.5.0
5143
*/
52-
public class CommonsXsdSchema implements InlineableXsdSchema, InitializingBean {
44+
public class CommonsXsdSchema implements XsdSchema {
5345

5446
private XmlSchema schema;
5547

56-
private Resource xsdResource;
57-
58-
/**
59-
* Create a new, empty instance of the {@link CommonsXsdSchema} class.
60-
* <p/>
61-
* A subsequent call to the {@link #setXsd(Resource)} method is required.
62-
*/
63-
public CommonsXsdSchema() {
64-
}
65-
66-
/**
67-
* Create a new instance of the {@link CommonsXsdSchema} class with the specified resource.
68-
*
69-
* @param xsdResource the XSD resource; must not be <code>null</code>
70-
* @throws IllegalArgumentException if the supplied <code>xsdResource</code> is <code>null</code>
71-
*/
72-
public CommonsXsdSchema(Resource xsdResource) {
73-
Assert.notNull(xsdResource, "xsdResource must not be null");
74-
this.xsdResource = xsdResource;
75-
}
76-
7748
/**
7849
* Create a new instance of the {@link CommonsXsdSchema} class with the specified {@link XmlSchema} reference.
7950
*
8051
* @param schema the Commons <code>XmlSchema</code> object; must not be <code>null</code>
8152
* @throws IllegalArgumentException if the supplied <code>schema</code> is <code>null</code>
8253
*/
83-
private CommonsXsdSchema(XmlSchema schema) {
54+
protected CommonsXsdSchema(XmlSchema schema) {
8455
Assert.notNull(schema, "'schema' must not be null");
8556
this.schema = schema;
8657
}
8758

88-
/**
89-
* Set the XSD resource to be exposed by calls to this instances' {@link #getSource()} method.
90-
*
91-
* @param xsdResource the XSD resource
92-
*/
93-
public void setXsd(Resource xsdResource) {
94-
this.xsdResource = xsdResource;
95-
}
96-
9759
public String getTargetNamespace() {
9860
return schema.getTargetNamespace();
9961
}
@@ -131,17 +93,12 @@ public XmlSchema getSchema() {
13193
return schema;
13294
}
13395

134-
public void afterPropertiesSet() throws IOException, SAXException {
135-
Assert.notNull(xsdResource, "'xsd' is required");
136-
Assert.isTrue(this.xsdResource.exists(), "xsd '" + this.xsdResource + "' does not exit");
137-
loadSchema();
138-
}
139-
14096
private void loadSchema() throws SAXException, IOException {
14197
XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
142-
this.schema = schemaCollection.read(SaxUtils.createInputSource(xsdResource), null);
98+
// this.schema = schemaCollection.read(SaxUtils.createInputSource(xsdResource), null);
14399
}
144100

101+
/*
145102
public XsdSchema[] inline() {
146103
XmlSchema clone = cloneSchema(schema);
147104
inlineIncludes(clone, new ArrayList());
@@ -179,7 +136,7 @@ private static void inlineIncludes(XmlSchema schema, List processedSchemas) {
179136
items.remove(include);
180137
}
181138
}
182-
}
139+
}*/
183140

184141
public String toString() {
185142
StringBuffer buffer = new StringBuffer("CommonsXsdSchema");
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2008 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.xml.xsd.commons;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
import org.apache.ws.commons.schema.XmlSchema;
25+
import org.apache.ws.commons.schema.XmlSchemaCollection;
26+
import org.apache.ws.commons.schema.constants.Constants;
27+
28+
import org.springframework.beans.factory.InitializingBean;
29+
import org.springframework.core.io.Resource;
30+
import org.springframework.util.Assert;
31+
import org.springframework.xml.sax.SaxUtils;
32+
import org.springframework.xml.xsd.XsdSchema;
33+
import org.springframework.xml.xsd.XsdSchemaCollection;
34+
35+
/**
36+
* Implementation of the {@link XsdSchemaCollection} that uses Apache WS-Commons XML Schema.
37+
*
38+
* @author Arjen Poutsma
39+
* @see <a href="http://ws.apache.org/commons/XmlSchema/">Commons XML Schema</a>
40+
* @since 1.5.0
41+
*/
42+
public class CommonsXsdSchemaCollection implements XsdSchemaCollection, InitializingBean {
43+
44+
private static final Log logger = LogFactory.getLog(CommonsXsdSchemaCollection.class);
45+
46+
private final XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
47+
48+
private final List xmlSchemas = new ArrayList();
49+
50+
private Resource[] xsdResources;
51+
52+
private boolean inlineIncludes = false;
53+
54+
/**
55+
* Constructs a new, empty instance of the <code>CommonsXsdSchemaCollection</code>.
56+
* <p/>
57+
* A subsequent call to the {@link #setXsds(Resource[])} is required.
58+
*/
59+
public CommonsXsdSchemaCollection() {
60+
}
61+
62+
/**
63+
* Constructs a new instance of the <code>CommonsXsdSchemaCollection</code> based on the given resources.
64+
*
65+
* @param resources the schema resources to load
66+
*/
67+
public CommonsXsdSchemaCollection(Resource[] resources) {
68+
this.xsdResources = resources;
69+
}
70+
71+
/**
72+
* Sets the schema resources to be loaded.
73+
*
74+
* @param xsdResources the schema resources to be loaded
75+
*/
76+
public void setXsds(Resource[] xsdResources) {
77+
this.xsdResources = xsdResources;
78+
}
79+
80+
/**
81+
* Defines whether included schemas should be inlinded into the including schema.
82+
* <p/>
83+
* Defaults to <code>false</code>.
84+
*/
85+
public void setInlineIncludes(boolean inlineIncludes) {
86+
this.inlineIncludes = inlineIncludes;
87+
}
88+
89+
public void afterPropertiesSet() throws Exception {
90+
Assert.notEmpty(xsdResources, "'xsds' must not be empty");
91+
for (int i = 0; i < xsdResources.length; i++) {
92+
Assert.isTrue(xsdResources[i].exists(), xsdResources[i] + " does not exit");
93+
xmlSchemas.add(schemaCollection.read(SaxUtils.createInputSource(xsdResources[i]), null));
94+
}
95+
}
96+
97+
public XsdSchema[] getXsdSchemas() {
98+
XsdSchema[] result = new XsdSchema[xmlSchemas.size()];
99+
for (int i = 0; i < xmlSchemas.size(); i++) {
100+
XmlSchema xmlSchema = (XmlSchema) xmlSchemas.get(i);
101+
result[i] = new CommonsXsdSchema(xmlSchema);
102+
}
103+
return result;
104+
}
105+
106+
public String toString() {
107+
StringBuffer buffer = new StringBuffer("CommonsXsdSchemaCollection");
108+
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+
}
116+
}
117+
}
118+
buffer.append('}');
119+
return buffer.toString();
120+
}
121+
122+
123+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright ${YEAR} the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.xml.xsd.commons;
18+
19+
import junit.framework.TestCase;
20+
21+
import org.springframework.core.io.ClassPathResource;
22+
import org.springframework.core.io.Resource;
23+
import org.springframework.xml.xsd.AbstractXsdSchemaTestCase;
24+
25+
public class CommonsXsdSchemaCollectionTest extends TestCase {
26+
27+
private CommonsXsdSchemaCollection collection;
28+
29+
protected void setUp() throws Exception {
30+
collection = new CommonsXsdSchemaCollection();
31+
}
32+
33+
public void testSingle() throws Exception {
34+
Resource resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class);
35+
collection.setXsds(new Resource[]{resource});
36+
collection.afterPropertiesSet();
37+
assertEquals("Invalid amount of XSDs loaded", 1, collection.getXsdSchemas().length);
38+
}
39+
40+
public void testIncludes() throws Exception {
41+
Resource resource = new ClassPathResource("including.xsd", AbstractXsdSchemaTestCase.class);
42+
collection.setXsds(new Resource[]{resource});
43+
collection.afterPropertiesSet();
44+
assertEquals("Invalid amount of XSDs loaded", 2, collection.getXsdSchemas().length);
45+
}
46+
47+
public void testImports() throws Exception {
48+
Resource resource = new ClassPathResource("importing.xsd", AbstractXsdSchemaTestCase.class);
49+
collection.setXsds(new Resource[]{resource});
50+
collection.afterPropertiesSet();
51+
assertEquals("Invalid amount of XSDs loaded", 2, collection.getXsdSchemas().length);
52+
}
53+
54+
public void testDuplicates() throws Exception {
55+
Resource resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class);
56+
collection.setXsds(new Resource[]{resource, resource});
57+
collection.afterPropertiesSet();
58+
assertEquals("Invalid amount of XSDs loaded", 1, collection.getXsdSchemas().length);
59+
}
60+
61+
public void testComplex() throws Exception {
62+
Resource resource = new ClassPathResource("A.xsd", AbstractXsdSchemaTestCase.class);
63+
collection.setXsds(new Resource[]{resource});
64+
collection.afterPropertiesSet();
65+
assertEquals("Invalid amount of XSDs loaded", 5, collection.getXsdSchemas().length);
66+
}
67+
68+
}

xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaTest.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,41 @@
1616

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

19-
import javax.xml.transform.stream.StreamSource;
20-
import javax.xml.transform.stream.StreamResult;
21-
import javax.xml.transform.Source;
22-
2319
import org.apache.ws.commons.schema.XmlSchema;
2420
import org.apache.ws.commons.schema.XmlSchemaCollection;
25-
import org.xml.sax.XMLReader;
26-
import org.xml.sax.helpers.XMLReaderFactory;
2721

2822
import org.springframework.core.io.Resource;
29-
import org.springframework.core.io.ClassPathResource;
23+
import org.springframework.xml.sax.SaxUtils;
3024
import org.springframework.xml.xsd.AbstractXsdSchemaTestCase;
3125
import org.springframework.xml.xsd.XsdSchema;
32-
import org.springframework.xml.transform.ResourceSource;
3326

3427
public class CommonsXsdSchemaTest extends AbstractXsdSchemaTestCase {
3528

3629
protected XsdSchema createSchema(Resource resource) throws Exception {
37-
CommonsXsdSchema schema = new CommonsXsdSchema(resource);
38-
schema.afterPropertiesSet();
39-
return schema;
30+
XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
31+
XmlSchema schema = schemaCollection.read(SaxUtils.createInputSource(resource), null);
32+
return new CommonsXsdSchema(schema);
4033
}
4134

35+
/*
4236
public void testInline() throws Exception {
4337
Resource resource = new ClassPathResource("A.xsd", AbstractXsdSchemaTestCase.class);
4438
CommonsXsdSchema schema = new CommonsXsdSchema(resource);
45-
schema.afterPropertiesSet();
4639
XsdSchema[] inlined = schema.inline();
4740
for (int i = 0; i < inlined.length; i++) {
4841
transformer.transform(inlined[i].getSource(), new StreamResult(System.out));
4942
System.out.println();
5043
}
5144
}
52-
45+
5346
public void testCircular() throws Exception {
5447
Resource resource = new ClassPathResource("circular-1.xsd", AbstractXsdSchemaTestCase.class);
5548
CommonsXsdSchema schema = new CommonsXsdSchema(resource);
56-
schema.afterPropertiesSet();
5749
XsdSchema[] inlined = schema.inline();
5850
for (int i = 0; i < inlined.length; i++) {
5951
transformer.transform(inlined[i].getSource(), new StreamResult(System.out));
6052
System.out.println();
6153
}
6254
}
55+
*/
6356
}

0 commit comments

Comments
 (0)