KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > XSModelGroupImpl


1 /*
2  * Copyright 2002,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.xerces.impl.xs;
18
19 import org.apache.xerces.xs.*;
20 import org.apache.xerces.impl.xs.util.XSObjectListImpl;
21
22 /**
23  * Store schema model group declaration.
24  *
25  * @xerces.internal
26  *
27  * @author Sandy Gao, IBM
28  *
29  * @version $Id: XSModelGroupImpl.java,v 1.10 2004/10/06 15:14:55 mrglavas Exp $
30  */

31 public class XSModelGroupImpl implements XSModelGroup {
32
33     // types of model groups
34
// REVISIT: can't use same constants as those for particles, because
35
// there are place where the constants are used together. For example,
36
// to check whether the content is an element or a sequence.
37
public static final short MODELGROUP_CHOICE = 101;
38     public static final short MODELGROUP_SEQUENCE = 102;
39     public static final short MODELGROUP_ALL = 103;
40
41     // compositor of the model group
42
public short fCompositor;
43
44     // particles
45
public XSParticleDecl[] fParticles = null;
46     public int fParticleCount = 0;
47
48     // this particle's optional annotation
49
public XSAnnotationImpl fAnnotation;
50
51     // whether this model group contains nothing
52
public boolean isEmpty() {
53         for (int i = 0; i < fParticleCount; i++) {
54             if (!fParticles[i].isEmpty())
55                 return false;
56         }
57         return true;
58     }
59
60     /**
61      * 3.8.6 Effective Total Range (all and sequence) and
62      * Effective Total Range (choice)
63      * The following methods are used to return min/max range for a particle.
64      * They are not exactly the same as it's described in the spec, but all the
65      * values from the spec are retrievable by these methods.
66      */

67     public int minEffectiveTotalRange() {
68         if (fCompositor == MODELGROUP_CHOICE)
69             return minEffectiveTotalRangeChoice();
70         else
71             return minEffectiveTotalRangeAllSeq();
72     }
73
74     // return the sum of all min values of the particles
75
private int minEffectiveTotalRangeAllSeq() {
76         int total = 0;
77         for (int i = 0; i < fParticleCount; i++)
78             total += fParticles[i].minEffectiveTotalRange();
79         return total;
80     }
81
82     // return the min of all min values of the particles
83
private int minEffectiveTotalRangeChoice() {
84         int min = 0, one;
85         if (fParticleCount > 0)
86             min = fParticles[0].minEffectiveTotalRange();
87
88         for (int i = 1; i < fParticleCount; i++) {
89             one = fParticles[i].minEffectiveTotalRange();
90             if (one < min)
91                 min = one;
92         }
93
94         return min;
95     }
96
97     public int maxEffectiveTotalRange() {
98         if (fCompositor == MODELGROUP_CHOICE)
99             return maxEffectiveTotalRangeChoice();
100         else
101             return maxEffectiveTotalRangeAllSeq();
102     }
103
104     // if one of the max value of the particles is unbounded, return unbounded;
105
// otherwise return the sum of all max values
106
private int maxEffectiveTotalRangeAllSeq() {
107         int total = 0, one;
108         for (int i = 0; i < fParticleCount; i++) {
109             one = fParticles[i].maxEffectiveTotalRange();
110             if (one == SchemaSymbols.OCCURRENCE_UNBOUNDED)
111                 return SchemaSymbols.OCCURRENCE_UNBOUNDED;
112             total += one;
113         }
114         return total;
115     }
116
117     // if one of the max value of the particles is unbounded, return unbounded;
118
// otherwise return the max of all max values
119
private int maxEffectiveTotalRangeChoice() {
120         int max = 0, one;
121         if (fParticleCount > 0) {
122             max = fParticles[0].maxEffectiveTotalRange();
123             if (max == SchemaSymbols.OCCURRENCE_UNBOUNDED)
124                 return SchemaSymbols.OCCURRENCE_UNBOUNDED;
125         }
126
127         for (int i = 1; i < fParticleCount; i++) {
128             one = fParticles[i].maxEffectiveTotalRange();
129             if (one == SchemaSymbols.OCCURRENCE_UNBOUNDED)
130                 return SchemaSymbols.OCCURRENCE_UNBOUNDED;
131             if (one > max)
132                 max = one;
133         }
134         return max;
135     }
136
137     /**
138      * get the string description of this particle
139      */

140     private String JavaDoc fDescription = null;
141     public String JavaDoc toString() {
142         // REVISIT: Commented code may help to eliminate redundant parentheses (test first before committing)
143
if (fDescription == null) {
144             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
145             if (fCompositor == MODELGROUP_ALL)
146                 buffer.append("all(");
147             else //if (fMinOccurs != 1 || fMaxOccurs != 1)
148
buffer.append('(');
149             if (fParticleCount > 0)
150                 buffer.append(fParticles[0].toString());
151             for (int i = 1; i < fParticleCount; i++) {
152                 if (fCompositor == MODELGROUP_CHOICE)
153                     buffer.append('|');
154                 else
155                     buffer.append(',');
156                 buffer.append(fParticles[i].toString());
157             }
158             //if (fCompositor == MODELGROUP_ALL || fMinOccurs != 1 || fMaxOccurs != 1)
159
buffer.append(')');
160             fDescription = buffer.toString();
161         }
162         return fDescription;
163     }
164
165     public void reset(){
166         fCompositor = MODELGROUP_SEQUENCE;
167         fParticles = null;
168         fParticleCount = 0;
169         fDescription = null;
170         fAnnotation = null;
171     }
172
173     /**
174      * Get the type of the object, i.e ELEMENT_DECLARATION.
175      */

176     public short getType() {
177         return XSConstants.MODEL_GROUP;
178     }
179
180     /**
181      * The <code>name</code> of this <code>XSObject</code> depending on the
182      * <code>XSObject</code> type.
183      */

184     public String JavaDoc getName() {
185         return null;
186     }
187
188     /**
189      * The namespace URI of this node, or <code>null</code> if it is
190      * unspecified. defines how a namespace URI is attached to schema
191      * components.
192      */

193     public String JavaDoc getNamespace() {
194         return null;
195     }
196
197     /**
198      * {compositor} One of all, choice or sequence. The valid constants values
199      * are: ALL, CHOICE, SEQUENCE.
200      */

201     public short getCompositor() {
202         if (fCompositor == MODELGROUP_CHOICE)
203             return XSModelGroup.COMPOSITOR_CHOICE;
204         else if (fCompositor == MODELGROUP_SEQUENCE)
205             return XSModelGroup.COMPOSITOR_SEQUENCE;
206         else
207             return XSModelGroup.COMPOSITOR_ALL;
208     }
209
210     /**
211      * {particles} A list of particles
212      */

213     public XSObjectList getParticles() {
214         return new XSObjectListImpl(fParticles, fParticleCount);
215     }
216
217     /**
218      * Optional. Annotation.
219      */

220     public XSAnnotation getAnnotation() {
221         return fAnnotation;
222     }
223
224     /**
225      * @see org.apache.xerces.xs.XSObject#getNamespaceItem()
226      */

227     public XSNamespaceItem getNamespaceItem() {
228         return null;
229     }
230
231 } // class XSModelGroupImpl
232
Popular Tags