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