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 package org.exolab.castor.xml.schema;
38
39 import org.exolab.castor.xml.ValidationException;
40
41 import java.util.Vector;
42 import java.util.Enumeration;
43
44
45
46
47
48
49
50 public final class AttributeGroupDecl extends AttributeGroup {
51
52 private static final long serialVersionUID = -5401452412514803353L;
53
54
55
56
57 private static String NULL_ARGUMENT =
58 "A null argument was passed to the constructor of " + AttributeDecl.class.getName();
59
60
61
62
63
64 private String _name = null;
65
66
67
68
69 private final Schema _schema;
70
71
72
73
74
75 private final Vector<AttributeDecl> _attributes = new Vector<>();
76
77
78
79
80 private final Vector<AttributeGroupReference> _references = new Vector<>();
81
82
83
84
85 private Wildcard _anyAttribute = null;
86
87
88
89
90 private boolean _redefinition = false;
91
92
93
94
95
96
97 public AttributeGroupDecl(Schema schema) {
98 if (schema == null) {
99 String err = NULL_ARGUMENT + "; 'schema' must not be null.";
100 throw new IllegalArgumentException(err);
101 }
102 _schema = schema;
103 }
104
105
106
107
108
109
110 public void addAttribute(AttributeDecl attrDecl) {
111
112 if (attrDecl == null)
113 return;
114
115
116
117
118 _attributes.add(attrDecl);
119
120 attrDecl.setParent(this);
121
122 }
123
124
125
126
127
128
129 public void addReference(AttributeGroupReference attrGroup) {
130
131 if (attrGroup == null)
132 return;
133
134
135
136
137 _references.add(attrGroup);
138
139 }
140
141
142
143
144
145 public Enumeration<AttributeDecl> getLocalAttributes() {
146 return _attributes.elements();
147 }
148
149
150
151
152 public Enumeration<AttributeGroupReference> getLocalAttributeGroupReferences() {
153 return _references.elements();
154 }
155
156
157
158
159
160
161 public Wildcard getAnyAttribute() {
162 return _anyAttribute;
163 }
164
165
166
167
168
169
170
171 public AttributeDecl getAttribute(String name) {
172
173 if (name == null)
174 return null;
175
176 for (int i = 0; i < _attributes.size(); i++) {
177 AttributeDecl attr = (AttributeDecl) _attributes.elementAt(i);
178 if (name.equals(attr.getName()))
179 return attr;
180 }
181
182 for (int i = 0; i < _references.size(); i++) {
183 AttributeGroupReference ref = (AttributeGroupReference) _references.elementAt(i);
184
185 AttributeDecl attr = ref.getAttribute(name);
186 if (attr != null)
187 return attr;
188 }
189
190 return null;
191 }
192
193
194
195
196
197
198
199 public Enumeration<AttributeDecl> getAttributes() {
200 return new AttributeGroupEnumeration(_attributes, _references);
201 }
202
203
204
205
206
207
208
209 public String getName() {
210 return _name;
211 }
212
213
214
215
216
217
218 public Schema getSchema() {
219 return _schema;
220 }
221
222
223
224
225
226
227
228
229 public boolean isEmpty() {
230
231 if (!_attributes.isEmpty())
232 return false;
233
234 if (_references.isEmpty())
235 return true;
236
237 for (AttributeGroup attributeGroup : _references) {
238 if (!attributeGroup.isEmpty())
239 return false;
240 }
241 return true;
242
243 }
244
245
246
247
248
249
250 public boolean isRedefined() {
251 return _redefinition;
252 }
253
254
255
256
257
258
259 public boolean removeAttribute(AttributeDecl attr) {
260 if (attr == null)
261 return false;
262 return _attributes.remove(attr);
263 }
264
265
266
267
268
269
270 public boolean removeReference(AttributeGroupReference attrGroupReference) {
271 if (attrGroupReference == null)
272 return false;
273 return _references.remove(attrGroupReference);
274 }
275
276
277
278
279
280
281
282 public void setAnyAttribute(Wildcard wildcard) throws SchemaException {
283 if (wildcard != null) {
284 if (_anyAttribute != null) {
285 String err = "<anyAttribute> already set in this AttributeGroup: " + this.getName();
286 throw new SchemaException(err);
287 }
288
289 if (!wildcard.isAttributeWildcard()) {
290 String err =
291 "In AttributeGroup, " + this.getName() + "the wildcard must be an <anyAttribute>";
292 throw new SchemaException(err);
293 }
294 }
295 _anyAttribute = wildcard;
296
297 }
298
299
300
301
302
303
304 public void setName(String name) {
305
306 if (name == null)
307 throw new IllegalArgumentException("name must not be null");
308
309
310 int idx = name.indexOf(':');
311 if (idx >= 0)
312 this._name = name.substring(idx + 1);
313 else
314 this._name = name;
315
316 }
317
318
319
320
321
322 public void setRedefined() {
323 _redefinition = true;
324 }
325
326
327
328
329
330
331
332
333
334 public short getStructureType() {
335 return Structure.ATTRIBUTE_GROUP;
336 }
337
338
339
340
341
342
343 public void validate() throws ValidationException {
344
345
346 }
347
348
349 }
350
351
352
353
354
355 class AttributeGroupEnumeration implements Enumeration<AttributeDecl> {
356
357 private Vector<AttributeGroupReference> references = null;
358 int index = 0;
359
360 private Enumeration<AttributeDecl> enumeration = null;
361
362
363 AttributeGroupEnumeration(Vector<AttributeDecl> definitions,
364 Vector<AttributeGroupReference> references) {
365 enumeration = definitions.elements();
366 if (!enumeration.hasMoreElements())
367 enumeration = null;
368 this.references = references;
369 }
370
371 public boolean hasMoreElements() {
372 if (enumeration != null)
373 return true;
374
375 int i = index;
376 while (i < references.size()) {
377 AttributeGroupReference ref = references.elementAt(i);
378 ++i;
379 if (!ref.isEmpty())
380 return true;
381 }
382 return false;
383
384 }
385
386 public AttributeDecl nextElement() {
387
388 if (enumeration != null) {
389 AttributeDecl obj = enumeration.nextElement();
390 if (!enumeration.hasMoreElements())
391 enumeration = null;
392 return obj;
393 }
394
395 while (index < references.size()) {
396 AttributeGroupReference ref = references.elementAt(index);
397
398 ++index;
399
400 enumeration = ref.getAttributes();
401 if (enumeration.hasMoreElements()) {
402 AttributeDecl obj = enumeration.nextElement();
403 if (!enumeration.hasMoreElements())
404 enumeration = null;
405 return obj;
406 }
407 }
408
409 return null;
410 }
411 }