KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > xs > XSModelGroupImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002,2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.impl.xs;
59
60 import com.sun.org.apache.xerces.internal.xs.*;
61 import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
62
63 /**
64  * Store schema model group declaration.
65  *
66  * @author Sandy Gao, IBM
67  *
68  * @version $Id: XSModelGroupImpl.java,v 1.7 2004/04/12 20:15:35 sandygao Exp $
69  */

70 public class XSModelGroupImpl implements XSModelGroup {
71
72     // types of model groups
73
// REVISIT: can't use same constants as those for particles, because
74
// there are place where the constants are used together. For example,
75
// to check whether the content is an element or a sequence.
76
public static final short MODELGROUP_CHOICE = 101;
77     public static final short MODELGROUP_SEQUENCE = 102;
78     public static final short MODELGROUP_ALL = 103;
79
80     // compositor of the model group
81
public short fCompositor;
82
83     // particles
84
public XSParticleDecl[] fParticles = null;
85     public int fParticleCount = 0;
86
87     // this particle's optional annotation
88
public XSAnnotationImpl fAnnotation;
89
90     // whether this model group contains nothing
91
public boolean isEmpty() {
92         for (int i = 0; i < fParticleCount; i++) {
93             if (!fParticles[i].isEmpty())
94                 return false;
95         }
96         return true;
97     }
98
99     /**
100      * 3.8.6 Effective Total Range (all and sequence) and
101      * Effective Total Range (choice)
102      * The following methods are used to return min/max range for a particle.
103      * They are not exactly the same as it's described in the spec, but all the
104      * values from the spec are retrievable by these methods.
105      */

106     public int minEffectiveTotalRange() {
107         if (fCompositor == MODELGROUP_CHOICE)
108             return minEffectiveTotalRangeChoice();
109         else
110             return minEffectiveTotalRangeAllSeq();
111     }
112
113     // return the sum of all min values of the particles
114
private int minEffectiveTotalRangeAllSeq() {
115         int total = 0;
116         for (int i = 0; i < fParticleCount; i++)
117             total += fParticles[i].minEffectiveTotalRange();
118         return total;
119     }
120
121     // return the min of all min values of the particles
122
private int minEffectiveTotalRangeChoice() {
123         int min = 0, one;
124         if (fParticleCount > 0)
125             min = fParticles[0].minEffectiveTotalRange();
126
127         for (int i = 1; i < fParticleCount; i++) {
128             one = fParticles[i].minEffectiveTotalRange();
129             if (one < min)
130                 min = one;
131         }
132
133         return min;
134     }
135
136     public int maxEffectiveTotalRange() {
137         if (fCompositor == MODELGROUP_CHOICE)
138             return maxEffectiveTotalRangeChoice();
139         else
140             return maxEffectiveTotalRangeAllSeq();
141     }
142
143     // if one of the max value of the particles is unbounded, return unbounded;
144
// otherwise return the sum of all max values
145
private int maxEffectiveTotalRangeAllSeq() {
146         int total = 0, one;
147         for (int i = 0; i < fParticleCount; i++) {
148             one = fParticles[i].maxEffectiveTotalRange();
149             if (one == SchemaSymbols.OCCURRENCE_UNBOUNDED)
150                 return SchemaSymbols.OCCURRENCE_UNBOUNDED;
151             total += one;
152         }
153         return total;
154     }
155
156     // if one of the max value of the particles is unbounded, return unbounded;
157
// otherwise return the max of all max values
158
private int maxEffectiveTotalRangeChoice() {
159         int max = 0, one;
160         if (fParticleCount > 0) {
161             max = fParticles[0].maxEffectiveTotalRange();
162             if (max == SchemaSymbols.OCCURRENCE_UNBOUNDED)
163                 return SchemaSymbols.OCCURRENCE_UNBOUNDED;
164         }
165
166         for (int i = 1; i < fParticleCount; i++) {
167             one = fParticles[i].maxEffectiveTotalRange();
168             if (one == SchemaSymbols.OCCURRENCE_UNBOUNDED)
169                 return SchemaSymbols.OCCURRENCE_UNBOUNDED;
170             if (one > max)
171                 max = one;
172         }
173         return max;
174     }
175
176     /**
177      * get the string description of this particle
178      */

179     private String JavaDoc fDescription = null;
180     public String JavaDoc toString() {
181         if (fDescription == null) {
182             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
183             if (fCompositor == MODELGROUP_ALL)
184                 buffer.append("all(");
185             else
186                 buffer.append('(');
187             if (fParticleCount > 0)
188                 buffer.append(fParticles[0].toString());
189             for (int i = 1; i < fParticleCount; i++) {
190                 if (fCompositor == MODELGROUP_CHOICE)
191                     buffer.append('|');
192                 else
193                     buffer.append(',');
194                 buffer.append(fParticles[i].toString());
195             }
196             buffer.append(')');
197             fDescription = buffer.toString();
198         }
199         return fDescription;
200     }
201
202     public void reset(){
203         fCompositor = MODELGROUP_SEQUENCE;
204         fParticles = null;
205         fParticleCount = 0;
206         fDescription = null;
207         fAnnotation = null;
208     }
209
210     /**
211      * Get the type of the object, i.e ELEMENT_DECLARATION.
212      */

213     public short getType() {
214         return XSConstants.MODEL_GROUP;
215     }
216
217     /**
218      * The <code>name</code> of this <code>XSObject</code> depending on the
219      * <code>XSObject</code> type.
220      */

221     public String JavaDoc getName() {
222         return null;
223     }
224
225     /**
226      * The namespace URI of this node, or <code>null</code> if it is
227      * unspecified. defines how a namespace URI is attached to schema
228      * components.
229      */

230     public String JavaDoc getNamespace() {
231         return null;
232     }
233
234     /**
235      * {compositor} One of all, choice or sequence. The valid constants values
236      * are: ALL, CHOICE, SEQUENCE.
237      */

238     public short getCompositor() {
239         if (fCompositor == MODELGROUP_CHOICE)
240             return XSModelGroup.COMPOSITOR_CHOICE;
241         else if (fCompositor == MODELGROUP_SEQUENCE)
242             return XSModelGroup.COMPOSITOR_SEQUENCE;
243         else
244             return XSModelGroup.COMPOSITOR_ALL;
245     }
246
247     /**
248      * {particles} A list of particles
249      */

250     public XSObjectList getParticles() {
251         return new XSObjectListImpl(fParticles, fParticleCount);
252     }
253
254     /**
255      * Optional. Annotation.
256      */

257     public XSAnnotation getAnnotation() {
258         return fAnnotation;
259     }
260
261     /**
262      * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
263      */

264     public XSNamespaceItem getNamespaceItem() {
265         return null;
266     }
267
268 } // class XSModelGroupImpl
269
Popular Tags