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