18
18
19
19
import java .io .IOException ;
20
20
import java .util .ArrayList ;
21
+ import java .util .HashSet ;
21
22
import java .util .List ;
23
+ import java .util .Set ;
22
24
23
25
import org .apache .ws .commons .schema .ValidationEventHandler ;
24
26
import org .apache .ws .commons .schema .XmlSchema ;
@@ -104,14 +106,19 @@ public void setValidationEventHandler(ValidationEventHandler validationEventHand
104
106
105
107
public void afterPropertiesSet () throws IOException {
106
108
Assert .notEmpty (xsdResources , "'xsds' must not be empty" );
109
+
110
+ Set processedIncludes = new HashSet ();
111
+ Set processedImports = new HashSet ();
112
+
107
113
for (int i = 0 ; i < xsdResources .length ; i ++) {
108
114
Assert .isTrue (xsdResources [i ].exists (), xsdResources [i ] + " does not exit" );
109
- XmlSchema xmlSchema =
110
- schemaCollection .read (SaxUtils .createInputSource (xsdResources [i ]), validationEventHandler );
115
+ XmlSchema xmlSchema = schemaCollection
116
+ .read (SaxUtils .createInputSource (xsdResources [i ]), validationEventHandler );
111
117
xmlSchemas .add (xmlSchema );
118
+
112
119
if (inline ) {
113
- inlineIncludes (xmlSchema , new ArrayList () );
114
- findImports (xmlSchema , new ArrayList () );
120
+ inlineIncludes (xmlSchema , processedIncludes , processedImports );
121
+ findImports (xmlSchema , processedImports , processedIncludes );
115
122
}
116
123
}
117
124
}
@@ -134,18 +141,20 @@ public XmlValidator createValidator() throws IOException {
134
141
return XmlValidatorFactory .createValidator (resources , XmlValidatorFactory .SCHEMA_W3C_XML );
135
142
}
136
143
137
- private void inlineIncludes (XmlSchema schema , List processedSchemas ) {
138
- processedSchemas .add (schema );
144
+ private void inlineIncludes (XmlSchema schema , Set processedIncludes , Set processedImports ) {
145
+ processedIncludes .add (schema );
139
146
XmlSchemaObjectCollection includes = schema .getIncludes ();
140
147
for (int i = 0 ; i < includes .getCount (); i ++) {
141
- XmlSchemaExternal external = (XmlSchemaExternal ) includes .getItem (i );
148
+ XmlSchemaExternal external = (XmlSchemaExternal ) includes
149
+ .getItem (i );
142
150
if (external instanceof XmlSchemaInclude ) {
143
151
XmlSchema includedSchema = external .getSchema ();
144
152
XmlSchemaObjectCollection items = schema .getItems ();
145
- if (!processedSchemas .contains (includedSchema )) {
146
- inlineIncludes (includedSchema , processedSchemas );
147
- findImports (includedSchema , new ArrayList ());
148
- XmlSchemaObjectCollection includeItems = includedSchema .getItems ();
153
+ if (!processedIncludes .contains (includedSchema )) {
154
+ inlineIncludes (includedSchema , processedIncludes , processedImports );
155
+ findImports (includedSchema , processedImports , processedIncludes );
156
+ XmlSchemaObjectCollection includeItems = includedSchema
157
+ .getItems ();
149
158
for (int j = 0 ; j < includeItems .getCount (); j ++) {
150
159
XmlSchemaObject includedItem = includeItems .getItem (j );
151
160
items .add (includedItem );
@@ -157,18 +166,18 @@ private void inlineIncludes(XmlSchema schema, List processedSchemas) {
157
166
}
158
167
}
159
168
160
- private void findImports (XmlSchema schema , List processedSchemas ) {
161
- processedSchemas .add (schema );
162
- XmlSchemaObjectCollection imports = schema .getIncludes ();
163
- for (int i = 0 ; i < imports .getCount (); i ++) {
164
- XmlSchemaExternal external = (XmlSchemaExternal ) imports .getItem (i );
169
+ private void findImports (XmlSchema schema , Set processedImports , Set processedIncludes ) {
170
+ processedImports .add (schema );
171
+ XmlSchemaObjectCollection includes = schema .getIncludes ();
172
+ for (int i = 0 ; i < includes .getCount (); i ++) {
173
+ XmlSchemaExternal external = (XmlSchemaExternal ) includes .getItem (i );
165
174
if (external instanceof XmlSchemaImport ) {
166
175
XmlSchemaImport schemaImport = (XmlSchemaImport ) external ;
167
176
XmlSchema importedSchema = schemaImport .getSchema ();
168
177
if (!"http://www.w3.org/XML/1998/namespace" .equals (schemaImport .getNamespace ()) &&
169
- importedSchema != null && !processedSchemas .contains (importedSchema )) {
170
- inlineIncludes (importedSchema , processedSchemas );
171
- findImports (importedSchema , processedSchemas );
178
+ importedSchema != null && !processedImports .contains (importedSchema )) {
179
+ inlineIncludes (importedSchema , processedIncludes , processedImports );
180
+ findImports (importedSchema , processedImports , processedIncludes );
172
181
xmlSchemas .add (importedSchema );
173
182
}
174
183
// remove the schemaLocation
@@ -191,5 +200,4 @@ public String toString() {
191
200
return buffer .toString ();
192
201
}
193
202
194
-
195
203
}
0 commit comments