KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.ws.jaxme.xs.XSModelGroup;
24 import org.apache.ws.jaxme.xs.XSParticle;
25 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException;
26 import org.xml.sax.Locator JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28
29
30 /**
31  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
32  */

33 public class XSModelGroupImpl implements XSModelGroup {
34   private final XSModelGroup.Compositor compositor;
35   private final List JavaDoc particles = new ArrayList JavaDoc();
36   private final Locator JavaDoc locator;
37
38   public XSModelGroupImpl(XSModelGroup.Compositor pCompositor, Locator JavaDoc pLocator) {
39     if (pCompositor == null) {
40       throw new NullPointerException JavaDoc("The model group compositor must not be null.");
41     }
42     compositor = pCompositor;
43     locator = pLocator;
44   }
45
46   public Compositor getCompositor() {
47     return compositor;
48   }
49
50   public boolean isSequence() {
51     return XSModelGroup.SEQUENCE.equals(compositor);
52   }
53
54   public boolean isChoice() {
55     return XSModelGroup.CHOICE.equals(compositor);
56   }
57
58   public boolean isAll() {
59     return XSModelGroup.ALL.equals(compositor);
60   }
61
62   public void addParticle(XSParticle pParticle) throws SAXException {
63     if (isAll()) {
64       if (pParticle.getMaxOccurs() == -1 || pParticle.getMaxOccurs() > 1) {
65         throw new LocSAXException("Illegal 'maxOccurs' value inside 'all' group: " + pParticle.getMaxOccurs(),
66                                      pParticle.getLocator());
67       }
68     }
69     if (pParticle.getMaxOccurs() != -1 && pParticle.getMaxOccurs() < pParticle.getMinOccurs()) {
70       throw new LocSAXException("Illegal 'maxOccurs' value, which is lower than 'minOccurs' value: " +
71                                    pParticle.getMaxOccurs() + " < " + pParticle.getMinOccurs(),
72                                    pParticle.getLocator());
73     }
74     particles.add(pParticle);
75   }
76
77   public XSParticle[] getParticles() {
78     return (XSParticle[]) particles.toArray(new XSParticle[particles.size()]);
79   }
80
81   public void validate() throws SAXException {
82     if (isChoice() && particles.size() == 0) {
83       throw new LocSAXException("A 'choice' model group must have at least one 'group', 'any', or 'element'.",
84                                    getLocator());
85     }
86     for (Iterator JavaDoc iter = particles.iterator(); iter.hasNext(); ) {
87       XSParticle particle = (XSParticle) iter.next();
88       if (particle.isElement()) {
89         particle.getElement().validate();
90       } else if (particle.isGroup()) {
91         particle.getGroup().validate();
92       } else if (particle.isWildcard()) {
93         particle.getWildcard().validate();
94       } else {
95         throw new IllegalStateException JavaDoc("Invalid particle");
96       }
97     }
98   }
99
100   public Locator JavaDoc getLocator() {
101     return locator;
102   }
103 }
104
Popular Tags