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 package org.exolab.castor.xml.schema;
36
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.Hashtable;
40 import java.util.Map;
41
42 import org.exolab.castor.xml.ValidationException;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public class RedefineSchema extends Annotated {
62
63 private static final long serialVersionUID = -7095458840388436859L;
64
65
66
67
68 private Schema _originalSchema;
69
70
71
72
73 private Schema _parentSchema;
74
75
76
77
78 private String _schemaLocation = null;
79
80
81
82
83 private Map<String, ComplexType> _complexTypes;
84
85
86
87
88 private Map<String, SimpleType> _simpleTypes;
89
90
91
92
93 private Map<String, ModelGroup> _groups;
94
95
96
97
98 private Map<String, AttributeGroup> _attributeGroups;
99
100
101
102
103
104 public RedefineSchema(Schema parentSchema) {
105 super();
106 _parentSchema = parentSchema;
107 _schemaLocation = "";
108 }
109
110
111
112
113 public RedefineSchema(Schema parentSchema, Schema originalSchema) {
114 super();
115 _schemaLocation = originalSchema.getSchemaLocation();
116 _parentSchema = parentSchema;
117 _originalSchema = originalSchema;
118 _complexTypes = new Hashtable<String, ComplexType>();
119 _simpleTypes = new Hashtable<String, SimpleType>();
120 _attributeGroups = new Hashtable<String, AttributeGroup>();
121 _groups = new Hashtable<String, ModelGroup>();
122 }
123
124
125
126
127
128
129 public void addComplexType(ComplexType complexType) throws SchemaException {
130 if (_schemaLocation.length() == 0)
131 throw new IllegalStateException(
132 "A RedefineSchema with no schemaLocation must contain only annotations");
133 String name = complexType.getName();
134
135 if (name == null) {
136 String err = "a global ComplexType must contain a name.";
137 throw new SchemaException(err);
138 }
139
140 if (complexType.getSchema() != _parentSchema) {
141 String err = "invalid attempt to add an ComplexType which ";
142 err += "belongs to a different Schema; type name: " + name;
143 throw new SchemaException(err);
144 }
145
146 if (getComplexType(name) != null) {
147 String err = "In the RedefineSchema:" + _schemaLocation
148 + "a ComplexType already exists with the given name: ";
149 throw new SchemaException(err + name);
150 }
151
152
153 complexType.setRedefined();
154
155 _complexTypes.put(name, complexType);
156 complexType.setParent(_parentSchema);
157 }
158
159
160
161
162
163
164 public Collection<ComplexType> enumerateComplexTypes() {
165 if (_schemaLocation.length() == 0) {
166 return Collections.emptyList();
167 }
168 return _complexTypes.values();
169 }
170
171
172
173
174
175
176
177 public ComplexType getComplexType(String name) {
178 if (_schemaLocation.length() == 0) {
179 return null;
180 }
181
182 return _complexTypes.get(name);
183 }
184
185
186
187
188
189
190
191 public boolean hasComplexTypeRedefinition(String name) {
192 if (_schemaLocation.length() == 0)
193 return false;
194
195 return (_complexTypes.containsKey(name));
196 }
197
198
199
200
201
202
203 public boolean removeComplexType(ComplexType complexType) {
204 if (_schemaLocation.length() == 0)
205 return false;
206
207 boolean result = false;
208 if (_complexTypes.containsValue(complexType)) {
209 _complexTypes.remove(complexType);
210 result = true;
211 }
212 return result;
213 }
214
215
216
217
218
219
220 public void addSimpleType(SimpleType simpleType) throws SchemaException {
221
222 String name = simpleType.getName();
223 if (name == null)
224 throw new IllegalArgumentException("A redefined simpleType must have a name");
225 if (_schemaLocation.length() == 0)
226 throw new IllegalStateException(
227 "A RedefineSchema with no schemaLocation must contain only annotations");
228
229 if (simpleType.getSchema() != _parentSchema) {
230 String err = "invalid attempt to add a SimpleType which ";
231 err += "belongs to a different Schema; type name: " + name;
232 throw new SchemaException(err);
233 }
234
235 if (getSimpleType(name) != null) {
236 String err = "In the RedefineSchema:" + _schemaLocation
237 + "a SimpleType already exists with the given name: ";
238 throw new SchemaException(err + name);
239 }
240
241 simpleType.setRedefined();
242
243 simpleType.setParent(this);
244 _simpleTypes.put(name, simpleType);
245 }
246
247
248
249
250
251
252 public Collection<SimpleType> enumerateSimpleTypes() {
253 if (_schemaLocation.length() == 0) {
254 return Collections.emptyList();
255 }
256
257 return _simpleTypes.values();
258 }
259
260
261
262
263
264
265
266 public SimpleType getSimpleType(String name) {
267 if (_schemaLocation.length() == 0) {
268 return null;
269 }
270
271 return _simpleTypes.get(name);
272 }
273
274
275
276
277
278
279 public boolean hasSimpleTypeRedefinition(String name) {
280 if (_schemaLocation.length() == 0) {
281 return false;
282 }
283
284 return (_simpleTypes.containsKey(name));
285 }
286
287
288
289
290
291
292 public boolean removeSimpleType(SimpleType simpleType) {
293 if (_schemaLocation.length() == 0)
294 return false;
295
296 boolean result = false;
297 if (_simpleTypes.containsValue(simpleType)) {
298 _simpleTypes.remove(simpleType);
299 result = true;
300 }
301 return result;
302 }
303
304
305
306
307 public void addGroup(ModelGroup group) throws SchemaException {
308 if (_schemaLocation.length() == 0) {
309 throw new IllegalStateException(
310 "A RedefineSchema with no schemaLocation must contain only annotations");
311 }
312
313 String name = group.getName();
314
315 if (name == null) {
316 String err = "a group declaration must contain a name.";
317 throw new SchemaException(err);
318 }
319
320 if (getModelGroup(name) != null) {
321 String err = "In the RedefineSchema:" + _schemaLocation
322 + " a group declaration already exists with the given name: ";
323 throw new SchemaException(err + name);
324 }
325
326 group.setRedefined();
327 group.setParent(_parentSchema);
328
329 _groups.put(name, group);
330 }
331
332
333
334
335
336
337
338 public ModelGroup getModelGroup(String name) {
339 if (_schemaLocation.length() == 0) {
340 return null;
341 }
342
343 return _groups.get(name);
344 }
345
346
347
348
349
350
351 public Collection<ModelGroup> enumerateGroups() {
352 if (_schemaLocation.length() == 0) {
353 return Collections.emptyList();
354 }
355
356 return _groups.values();
357 }
358
359
360
361
362
363
364 public boolean hasGroupRedefinition(String name) {
365 if (_schemaLocation.length() == 0)
366 return false;
367
368 return (_groups.containsKey(name));
369 }
370
371
372
373
374
375
376 public boolean removeGroup(ModelGroup group) {
377 if (_schemaLocation.length() == 0)
378 return false;
379
380 boolean result = false;
381 if (_groups.containsValue(group)) {
382 _groups.remove(group);
383 result = true;
384 }
385 return result;
386 }
387
388
389
390
391 public void addAttributeGroup(AttributeGroupDecl attrGroup) throws SchemaException {
392 if (_schemaLocation.length() == 0)
393 throw new IllegalStateException(
394 "A RedefineSchema with no schemaLocation must contain only annotations");
395
396 if (attrGroup == null)
397 return;
398
399 String name = attrGroup.getName();
400
401
402 int idx = name.indexOf(':');
403 if (idx >= 0) {
404 String nsPrefix = name.substring(0, idx);
405 name = name.substring(idx + 1);
406 String ns = _parentSchema.getNamespace(nsPrefix);
407 if (ns == null) {
408 String err = "addAttributeGroup: ";
409 err += "Namespace prefix not recognized '" + nsPrefix + "'";
410 throw new IllegalArgumentException(err);
411 }
412 if (!ns.equals(_parentSchema.getTargetNamespace())) {
413 String err = "AttributeGroup has different namespace " + "than this Schema definition.";
414 throw new IllegalArgumentException(err);
415 }
416 }
417
418 if (attrGroup.getSchema() != _parentSchema) {
419 String err = "invalid attempt to add an AttributeGroup which ";
420 err += "belongs to a different Schema; " + name;
421 throw new SchemaException(err);
422 }
423
424 attrGroup.setRedefined();
425
426 _attributeGroups.put(name, attrGroup);
427 }
428
429
430
431
432
433
434 public AttributeGroupDecl getAttributeGroup(String name) {
435 if (_schemaLocation.length() == 0)
436 return null;
437
438 return (AttributeGroupDecl) _attributeGroups.get(name);
439 }
440
441
442
443
444
445
446 public Collection<AttributeGroup> enumerateAttributeGroups() {
447 if (_schemaLocation.length() == 0) {
448 return Collections.emptyList();
449 }
450
451 return _attributeGroups.values();
452 }
453
454
455
456
457
458
459
460 public boolean hasAttributeGroupRedefinition(String name) {
461 if (_schemaLocation.length() == 0)
462 return false;
463
464 return (_attributeGroups.containsKey(name));
465 }
466
467
468
469
470
471
472 public boolean removeAttributeGroup(AttributeGroupDecl attrGroup) {
473 if (_schemaLocation.length() == 0)
474 return false;
475
476 boolean result = false;
477 if (_attributeGroups.containsValue(attrGroup)) {
478 _attributeGroups.remove(attrGroup);
479 result = true;
480 }
481 return result;
482 }
483
484
485
486
487
488
489 public boolean hasRedefinition() {
490 if (_schemaLocation.length() == 0)
491 return false;
492
493 return (!_complexTypes.isEmpty()) || (!_simpleTypes.isEmpty()) || (!_groups.isEmpty())
494 || (!_attributeGroups.isEmpty());
495 }
496
497
498
499
500
501
502 public String getSchemaLocation() {
503 return _schemaLocation;
504 }
505
506
507
508
509
510
511 public Schema getOriginalSchema() {
512 return _originalSchema;
513 }
514
515
516
517
518
519
520 public Schema getParentSchema() {
521 return _parentSchema;
522 }
523
524
525
526
527
528
529 public short getStructureType() {
530 return Structure.REDEFINE;
531 }
532
533
534 public void validate() throws ValidationException {
535
536
537 }
538
539 }