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