KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > impl > XSGroupImpl


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.xs.impl;
18
19 import org.apache.ws.jaxme.xs.XSAnnotation;
20 import org.apache.ws.jaxme.xs.XSAny;
21 import org.apache.ws.jaxme.xs.XSElement;
22 import org.apache.ws.jaxme.xs.XSGroup;
23 import org.apache.ws.jaxme.xs.XSModelGroup;
24 import org.apache.ws.jaxme.xs.XSObject;
25 import org.apache.ws.jaxme.xs.XSParticle;
26 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException;
27 import org.apache.ws.jaxme.xs.xml.XsAGOccurs;
28 import org.apache.ws.jaxme.xs.xml.XsEAnnotation;
29 import org.apache.ws.jaxme.xs.xml.XsEAny;
30 import org.apache.ws.jaxme.xs.xml.XsEChoice;
31 import org.apache.ws.jaxme.xs.xml.XsESchema;
32 import org.apache.ws.jaxme.xs.xml.XsESequence;
33 import org.apache.ws.jaxme.xs.xml.XsNCName;
34 import org.apache.ws.jaxme.xs.xml.XsObject;
35 import org.apache.ws.jaxme.xs.xml.XsQName;
36 import org.apache.ws.jaxme.xs.xml.XsTAll;
37 import org.apache.ws.jaxme.xs.xml.XsTGroupRef;
38 import org.apache.ws.jaxme.xs.xml.XsTLocalElement;
39 import org.apache.ws.jaxme.xs.xml.XsTNamedGroup;
40 import org.apache.ws.jaxme.xs.xml.XsTNestedParticle;
41 import org.apache.ws.jaxme.xs.xml.XsTParticle;
42 import org.apache.ws.jaxme.xs.xml.impl.XsTGroupRefImpl;
43 import org.xml.sax.SAXException JavaDoc;
44
45
46 /**
47  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
48  */

49 public class XSGroupImpl extends XSOpenAttrsImpl implements XSGroup {
50   private final XsQName name;
51   private final XsTNestedParticle[] nestedParticles;
52   private final XsTParticle[] particles;
53   private final XsEAnnotation xsAnnotation;
54   private XSAnnotation[] annotations;
55   private boolean isGlobal;
56   private XSModelGroup modelGroup;
57   private final XSModelGroupImpl modelGroupImpl;
58   private boolean isValidated;
59
60   protected XSGroupImpl(XSObject pParent, XsTGroupRef pBaseGroup)
61       throws SAXException {
62     super(pParent, pBaseGroup);
63     XsQName qName = pBaseGroup.getRef();
64     if (qName == null) {
65       qName = getQName(pBaseGroup.getName());
66     } else {
67       setGlobal(true);
68     }
69     name = qName;
70     modelGroupImpl = null;
71     nestedParticles = null;
72     particles = null;
73     xsAnnotation = pBaseGroup.getAnnotation();
74   }
75
76   protected XSGroupImpl(XSObject pParent, XsTNamedGroup pBaseGroup) throws SAXException {
77     super(pParent, pBaseGroup);
78     name = getQName(pBaseGroup.getName());
79     XSModelGroupImpl myModelGroup;
80     XsTNamedGroup group = (XsTNamedGroup) getXsObject();
81     if (group.getSequence() != null) {
82       myModelGroup = modelGroupImpl = new XSModelGroupImpl(XSModelGroup.SEQUENCE, pBaseGroup.getLocator());
83       nestedParticles = group.getSequence().getParticles();
84       particles = null;
85     } else if (group.getChoice() != null) {
86       myModelGroup = modelGroupImpl = new XSModelGroupImpl(XSModelGroup.CHOICE, pBaseGroup.getLocator());
87       nestedParticles = group.getChoice().getParticles();
88       particles = null;
89     } else if (group.getAll() != null) {
90       myModelGroup = modelGroupImpl = new XSModelGroupImpl(XSModelGroup.ALL, pBaseGroup.getLocator());
91       nestedParticles = group.getAll().getElements();
92       particles = null;
93     } else {
94       throw new IllegalStateException JavaDoc("Invalid group: Neither of 'sequence', 'choice'. or 'all' elements is set.");
95     }
96     this.modelGroup = myModelGroup;
97     xsAnnotation = pBaseGroup.getAnnotation();
98   }
99
100   protected XSGroupImpl(XSObject pParent, XsEChoice pChoice) throws SAXException {
101     super(pParent, pChoice);
102     name = null;
103     modelGroup = modelGroupImpl = new XSModelGroupImpl(XSModelGroup.CHOICE, pChoice.getLocator());
104     particles = pChoice.getParticles();
105     nestedParticles = null;
106     xsAnnotation = pChoice.getAnnotation();
107   }
108
109   protected XSGroupImpl(XSObject pParent, XsESequence pSequence) throws SAXException {
110     super(pParent, pSequence);
111     name = null;
112     modelGroup = modelGroupImpl = new XSModelGroupImpl(XSModelGroup.SEQUENCE, pSequence.getLocator());
113     particles = pSequence.getParticles();
114     nestedParticles = null;
115     xsAnnotation = pSequence.getAnnotation();
116   }
117
118   protected XSGroupImpl(XSObject pParent, XsTAll pAll) throws SAXException {
119     super(pParent, pAll);
120     name = null;
121     modelGroup = modelGroupImpl = new XSModelGroupImpl(XSModelGroup.ALL, pAll.getLocator());
122     particles = pAll.getParticles();
123     nestedParticles = null;
124     xsAnnotation = pAll.getAnnotation();
125   }
126
127   protected XsQName getQName(XsNCName pName) throws SAXException {
128     if (pName == null) {
129       throw new LocSAXException("Invalid group: Either of its 'ref' or 'name' attributes must be set.",
130                                    getLocator());
131     }
132     XsESchema syntaxSchema = getXsObject().getXsESchema();
133     return new XsQName(syntaxSchema.getTargetNamespace(), pName.toString(), syntaxSchema.getTargetNamespacePrefix());
134   }
135
136   protected void initParticles(XSModelGroupImpl pModelGroup, XsTNestedParticle[] pParticles) throws SAXException {
137     for (int i = 0; i < pParticles.length; i++) {
138       addParticle(pModelGroup, pParticles[i]);
139     }
140   }
141
142   protected void initParticles(XSModelGroupImpl pModelGroup, XsTParticle[] pParticles) throws SAXException {
143     for (int i = 0; i < pParticles.length; i++) {
144       addParticle(pModelGroup, pParticles[i]);
145     }
146   }
147
148   protected void addParticle(XSModelGroupImpl pModelGroup, XsTParticle pParticle) throws SAXException {
149     XsAGOccurs occurs;
150     XSParticleImpl p;
151     if (pParticle instanceof XsTAll) {
152       XsTAll all = (XsTAll) pParticle;
153       occurs = all;
154       XSGroup group = getXSSchema().getXSObjectFactory().newXSGroup(this, all);
155       group.validate();
156       p = new XSParticleImpl(group);
157     } else if (pParticle instanceof XsTNestedParticle) {
158       addParticle(pModelGroup, (XsTNestedParticle) pParticle);
159       return;
160     } else {
161       throw new IllegalStateException JavaDoc("Unknown particle type: " + pParticle.getClass().getName());
162     }
163     pModelGroup.addParticle(p);
164     p.setMaxOccurs(occurs.getMaxOccurs());
165     p.setMinOccurs(occurs.getMinOccurs());
166   }
167
168   protected void addParticle(XSModelGroupImpl pModelGroup, XsTNestedParticle pParticle) throws SAXException {
169     XsAGOccurs occurs;
170     XSParticleImpl p;
171     if (pParticle instanceof XsEAny) {
172       XsEAny any = (XsEAny) pParticle;
173       occurs = any;
174       XSAny wildcard = getXSSchema().getXSObjectFactory().newXSAny(this, any);
175       wildcard.validate();
176       p = new XSParticleImpl(wildcard);
177     } else if (pParticle instanceof XsEChoice) {
178       XsEChoice choice = (XsEChoice) pParticle;
179       occurs = choice;
180       XSGroup group = getXSSchema().getXSObjectFactory().newXSGroup(this, choice);
181       group.validate();
182       p = new XSParticleImpl(group);
183     } else if (pParticle instanceof XsESequence) {
184       XsESequence sequence = (XsESequence) pParticle;
185       occurs = sequence;
186       XSGroup group = getXSSchema().getXSObjectFactory().newXSGroup(this, sequence);
187       group.validate();
188       p = new XSParticleImpl(group);
189     } else {
190       XsTGroupRef groupRef = null;
191       XsTLocalElement localElement = null;
192       if (pParticle instanceof XsTGroupRef) {
193         groupRef = (XsTGroupRef) pParticle;
194       } else if (pParticle instanceof XsTLocalElement) {
195         localElement = (XsTLocalElement) pParticle;
196         // May be the referenced element is the head of a substitution group
197
XsQName ref = localElement.getRef();
198         if (ref != null) {
199           XSElement referencedElement = getXSSchema().getElement(ref);
200           if (referencedElement == null) {
201             throw new LocSAXException("The referenced element " + ref + " is undefined.", localElement.getLocator());
202           }
203           XSGroup substitutedGroup = referencedElement.getSubstitutionGroup();
204           if (substitutedGroup != null && substitutedGroup != this) {
205             XsObject parent = localElement.getParentObject();
206             XsTGroupRefImpl groupRefImpl = (XsTGroupRefImpl) localElement.getObjectFactory().newXsTGroupRef(parent);
207             groupRefImpl.setRef(substitutedGroup.getName());
208             int maxOccurs = localElement.getMaxOccurs();
209             if (maxOccurs == -1) {
210               groupRefImpl.setMaxOccurs("unbounded");
211             } else {
212               groupRefImpl.setMaxOccurs(Integer.toString(maxOccurs));
213             }
214             groupRefImpl.setMinOccurs(localElement.getMinOccurs());
215             groupRef = groupRefImpl;
216           }
217         }
218       } else {
219         throw new IllegalStateException JavaDoc("Unknown particle type: " + pParticle.getClass().getName());
220       }
221       if (groupRef == null) {
222         occurs = localElement;
223         XSElement element = getXSSchema().getXSObjectFactory().newXSElement(this, localElement);
224         element.validate();
225         p = new XSParticleImpl(element);
226       } else {
227         occurs = groupRef;
228         XSGroup group = getXSSchema().getXSObjectFactory().newXSGroup(this, groupRef);
229         group.validate();
230         p = new XSParticleImpl(group);
231       }
232     }
233     pModelGroup.addParticle(p);
234     p.setMaxOccurs(occurs.getMaxOccurs());
235     p.setMinOccurs(occurs.getMinOccurs());
236   }
237
238   public boolean isGlobal() {
239     return isGlobal;
240   }
241
242   public void setGlobal(boolean pGlobal) {
243     isGlobal = pGlobal;
244   }
245
246   public XsQName getName() {
247     return name;
248   }
249
250   protected boolean isValidated() {
251     return isValidated;
252   }
253
254   public void validate() throws SAXException {
255     if (isValidated()) {
256       return;
257     } else {
258       isValidated = true;
259     }
260
261     if (xsAnnotation == null) {
262       annotations = new XSAnnotation[0];
263     } else {
264       XSAnnotation result = getXSSchema().getXSObjectFactory().newXSAnnotation(this, xsAnnotation);
265       result.validate();
266       annotations = new XSAnnotation[]{result};
267     }
268
269     if (modelGroup == null) {
270       XsQName myName = getName();
271       if (myName == null) {
272         throw new NullPointerException JavaDoc("Missing group name");
273       }
274       XSGroup group = getXSSchema().getGroup(myName);
275       if (group == null) {
276         throw new LocSAXException("Unknown group: " + myName, getLocator());
277       }
278       modelGroup = group;
279     }
280
281     if (particles != null) {
282       initParticles(modelGroupImpl, particles);
283     } else if (nestedParticles != null) {
284       initParticles(modelGroupImpl, nestedParticles);
285     }
286
287     modelGroup.validate();
288   }
289
290   public Compositor getCompositor() {
291     return modelGroup.getCompositor();
292   }
293
294   public XSParticle[] getParticles() {
295     return modelGroup.getParticles();
296   }
297
298   public boolean isSequence() {
299     return modelGroup.isSequence();
300   }
301
302   public boolean isChoice() {
303     return modelGroup.isChoice();
304   }
305
306   public boolean isAll() {
307     return modelGroup.isAll();
308   }
309
310   public XSAnnotation[] getAnnotations() {
311     return annotations;
312   }
313 }
314
Popular Tags