1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 package org.exolab.castor.xml.schema;
37
38 import org.exolab.castor.xml.*;
39
40 import java.util.Vector;
41 import java.util.Enumeration;
42
43
44
45
46
47
48
49 public class ModelGroup extends Group {
50
51 private static final long serialVersionUID = -2057934322265672413L;
52
53
54
55
56 private String _groupRef = null;
57
58
59
60
61 private final Vector<ModelGroup> _modelDefs = new Vector<>();
62
63
64
65
66 private boolean _redefinition = false;
67
68
69
70
71
72 private Schema _schema = null;
73
74
75
76
77 public ModelGroup() {
78 this(null);
79 }
80
81
82
83
84
85
86 public ModelGroup(Schema schema) {
87 this(null, schema);
88 }
89
90
91
92
93
94
95 public ModelGroup(String name, Schema schema) {
96 super(name);
97 _schema = schema;
98 }
99
100
101
102
103
104
105
106 public void addModelGroup(ModelGroup modelGroup) {
107 if (!_modelDefs.contains(modelGroup)) {
108 _modelDefs.add(modelGroup);
109 }
110 }
111
112
113
114
115
116
117 public Enumeration<Particle> enumerate() {
118 return this.getContentModelGroup().enumerate();
119 }
120
121
122
123
124
125 public Enumeration<ModelGroup> getDeclarations() {
126 return _modelDefs.elements();
127 }
128
129
130
131
132
133
134 public ContentModelGroup getContentModelGroup() {
135 if (_groupRef != null)
136 return getReference().getContentModelGroup();
137 return super.getContentModelGroup();
138 }
139
140
141
142
143
144
145 public String getName() {
146 return getName(false);
147 }
148
149
150
151
152
153
154
155 public String getName(boolean ignoreRef) {
156
157 if (ignoreRef == false && _groupRef != null) {
158 String localName = _groupRef;
159
160 int idx = localName.indexOf(':');
161 if (idx > 0) {
162 localName = localName.substring(idx + 1);
163 }
164 return localName;
165 }
166 return super.getName();
167 }
168
169
170
171
172
173
174 public boolean isReference() {
175 return (_groupRef != null);
176 }
177
178
179
180
181 public void setRedefined() {
182 _redefinition = true;
183 }
184
185
186
187
188
189
190 public boolean isRedefined() {
191 return _redefinition;
192 }
193
194
195
196
197
198
199 public void setReference(String reference) {
200 this._groupRef = reference;
201 }
202
203
204
205
206
207
208
209
210
211
212 public short getStructureType() {
213 return Structure.MODELGROUP;
214 }
215
216
217
218
219
220
221
222 public String getReferenceId() {
223 if (this.getName() != null)
224 return "group:" + this.getName();
225 return null;
226 }
227
228
229
230
231
232
233 public ModelGroup getReference() {
234 ModelGroup result = null;
235 if (_groupRef != null) {
236 result = _schema.getModelGroup(_groupRef);
237
238
239 if (_schema.getMasterSchema() != null) {
240 ModelGroup temp = _schema.getMasterSchema().getModelGroup(_groupRef);
241 if (temp != null)
242 result = temp;
243 }
244
245 if (result == null) {
246 String err = "Unable to find group referenced :\" ";
247 err += getName();
248 err += "\"";
249 throw new IllegalStateException(err);
250 }
251 }
252 return result;
253 }
254
255
256
257
258
259
260
261 public boolean hasReference() {
262 return (_groupRef != null) ? (_groupRef.length() != 0) : false;
263 }
264
265
266
267
268
269
270 public void validate() throws ValidationException {
271
272 if (getParent() != null && getParent().getStructureType() != Structure.SCHEMA) {
273 if (getName(true) != null) {
274 String err = "Only top-level model group definition (<group>) can be named.";
275 err += getName() + "is not a valid model group definition.";
276 throw new ValidationException(err);
277 }
278 }
279
280 if (getContentModelGroup() == null) {
281 String err = "<group> should contains :\" ";
282 err += " 'all' or 'sequence' or 'choice'";
283 err += "\"";
284 throw new ValidationException(err);
285 }
286
287
288
289
290
291 for (int i = 0; i < getParticleCount(); i++) {
292 Structure temp = getParticle(i);
293 switch (temp.getStructureType()) {
294 case Structure.MODELGROUP:
295 ModelGroup tempGroup = (ModelGroup) temp;
296 String name = null;
297 if (tempGroup.getReference() != null)
298 name = tempGroup.getReference().getName();
299
300 if (name != null && name.equals(this.getName())) {
301 if (isRedefined()) {
302 if (getMaxOccurs() != 1 || getMinOccurs() != 1) {
303 String err = "in the redefined <group> named:" + this.getName();
304 err +=
305 "\nThe particle information (minOccurs, maxOccurs) of a circular group must be set to 1.\n";
306 throw new ValidationException(err);
307 }
308
309 } else {
310 String err = "in <group> named:" + this.getName();
311 err += "\nCircular groups are disallowed.\n";
312 err +=
313 "That is, within the {particles} of a group there must not be at any depth a particle whose {term} is the group itself.\n";
314 throw new ValidationException(err);
315 }
316 }
317
318 int j = 0;
319 tempGroup = tempGroup.getReference();
320 while (j < tempGroup.getParticleCount()) {
321 if (tempGroup.getParticle(j).getStructureType() == Structure.MODELGROUP) {
322 ModelGroup referencedGroup = ((ModelGroup) tempGroup.getParticle(j)).getReference();
323 if ((referencedGroup != null) && (referencedGroup.equals(this))) {
324 if (isRedefined()) {
325 if (getMaxOccurs() != 1 || getMinOccurs() != 1) {
326 String err = "in the redefined <group> named:" + this.getName();
327 err +=
328 "\nThe particle information (minOccurs, maxOccurs) of a circular group must be set to 1.\n";
329 throw new ValidationException(err);
330 }
331 } else {
332 String err = "Cross reference between <group>:" + this.getName() + " and <group>:"
333 + tempGroup.getName();
334 err += "\nCircular groups are disallowed.\n";
335 err +=
336 "That is, within the {particles} of a group there must not be at any depth a particle whose {term} is the group itself.\n";
337 throw new ValidationException(err);
338 }
339 }
340
341 }
342 j++;
343
344 }
345 break;
346 default:
347 break;
348
349 }
350 }
351 }
352
353
354
355
356 public Schema getSchema() {
357 return _schema;
358 }
359
360
361
362
363
364
365 public void setSchema(Schema schema) {
366 _schema = schema;
367 }
368
369
370
371 }