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 java.util.Enumeration;
49 import java.util.Vector;
50
51
52
53
54
55
56
57
58 class ContentModelGroupImpl implements ContentModelGroup , java.io.Serializable {
59
60 private static final long serialVersionUID = -2477271185972337873L;
61
62
63
64
65 private Vector<Particle> _contentModel = new Vector<Particle>();
66
67 private transient ScopableResolver _resolver = new ScopableResolver();
68
69
70
71
72
73
74 public void addWildcard(final Wildcard wildcard) throws SchemaException {
75 if (wildcard.isAttributeWildcard()) {
76 throw new SchemaException("only <any> should be add in a group.");
77 }
78 _contentModel.addElement(wildcard);
79 }
80
81
82
83
84
85
86 public void addElementDecl(final ElementDecl elementDecl)
87 throws SchemaException {
88
89 if (elementDecl == null) {
90 return;
91 }
92
93 String name = elementDecl.getName();
94
95 if (!elementDecl.isReference()) {
96 String key = "element:" + name;
97
98 if (_resolver.resolve(key) != null) {
99 String err = "An element declaration with the given name, ";
100 err += name + ", already exists in this scope.";
101 throw new SchemaException(err);
102 }
103 _resolver.addResolvable(key, elementDecl);
104 }
105
106
107 _contentModel.addElement(elementDecl);
108
109 }
110
111
112
113
114
115
116 public boolean removeElementDecl(final ElementDecl elementDecl) {
117 if (elementDecl == null) {
118 return false;
119 }
120 int position = _contentModel.indexOf(elementDecl);
121 if (position >= 0) {
122 _contentModel.removeElementAt(position);
123 if (!elementDecl.isReference()) {
124 String key = "element:" + elementDecl.getName();
125 _resolver.removeResolvable(key);
126 }
127 return true;
128 }
129 return false;
130 }
131
132
133
134
135
136
137 public void addGroup(final Group group) throws SchemaException {
138 if (group == null) {
139 return;
140 }
141
142 String name = group.getName();
143 if (name != null) {
144 String key = "group:" + name;
145
146 if (_resolver.resolve(key) != null) {
147 String err = "A group definition with the given name, ";
148 err += name + ", already exists in this scope.";
149 throw new SchemaException(err);
150 }
151
152 _resolver.addResolvable(key, group);
153 }
154
155
156 _contentModel.addElement(group);
157 }
158
159
160
161
162
163
164 public boolean removeGroup(final Group group) {
165 if (group == null) {
166 return false;
167 }
168 int position = _contentModel.indexOf(group);
169 if (position >= 0) {
170 String name = group.getName();
171 if (name != null) {
172 String key = "group:" + name;
173 _resolver.removeResolvable(key);
174 }
175 _contentModel.removeElementAt(position);
176 return true;
177 }
178 return false;
179 }
180
181
182
183
184
185
186 public void addGroup(final ModelGroup group) throws SchemaException {
187 if (group == null) {
188 return;
189 }
190
191 String name = group.getName();
192 if ((name != null) && (!group.isReference())) {
193 String key = "group:" + name;
194
195 if (_resolver.resolve(key) != null) {
196 String err = "An element declaration with the given name, ";
197 err += name + ", already exists in this scope.";
198 throw new SchemaException(err);
199 }
200
201 _resolver.addResolvable(key, group);
202 }
203
204
205 _contentModel.addElement(group);
206 }
207
208
209
210
211
212
213 public boolean removeGroup(final ModelGroup group) {
214 if (group == null) {
215 return false;
216 }
217 int position = _contentModel.indexOf(group);
218 if (position >= 0) {
219 String name = group.getName();
220 if ((name != null) && (!group.isReference())) {
221 String key = "group:" + name;
222 _resolver.removeResolvable(key);
223 }
224 _contentModel.removeElementAt(position);
225 return true;
226 }
227 return false;
228 }
229
230
231
232
233
234
235 public boolean removeWildcard(final Wildcard wildcard) {
236 if (wildcard == null) {
237 return false;
238 }
239 int position = _contentModel.indexOf(wildcard);
240 if (position >= 0) {
241 _contentModel.removeElementAt(position);
242 return true;
243 }
244 return false;
245 }
246
247
248
249
250
251
252 public Enumeration<Particle> enumerate() {
253 return _contentModel.elements();
254 }
255
256
257
258
259
260
261 public ElementDecl getElementDecl(final String name) {
262 if (name == null) {
263 return null;
264 }
265 ElementDecl result = null;
266 if (_resolver != null) {
267 String key = "element:" + name;
268 result = (ElementDecl) _resolver.resolve(key);
269
270
271 if (result != null) {
272 return result;
273 }
274 }
275 for (int i = 0; i < _contentModel.size(); i++) {
276 Particle particle = _contentModel.elementAt(i);
277 switch (particle.getStructureType()) {
278 case Structure.ELEMENT:
279 ElementDecl e = (ElementDecl) particle;
280 if (name.equals(e.getName())) {
281 result = e;
282 }
283 break;
284 case Structure.GROUP:
285 case Structure.MODELGROUP:
286 result = ((ContentModelGroup) particle).getElementDecl(name);
287 break;
288 default:
289 break;
290 }
291 if (result != null) {
292 break;
293 }
294 }
295 return result;
296 }
297
298
299
300
301
302
303 public int getMaxOccurs() {
304 return 1;
305 }
306
307
308
309
310
311
312 public int getMinOccurs() {
313 return 1;
314 }
315
316
317
318
319
320
321 public Particle getParticle(final int index) {
322 return _contentModel.elementAt(index);
323 }
324
325
326
327
328
329
330 public int getParticleCount() {
331 return _contentModel.size();
332 }
333
334 }