KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001, 2002,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.XSConstants;
20 import org.apache.xerces.xs.XSNamespaceItem;
21 import org.apache.xerces.xs.XSParticle;
22 import org.apache.xerces.xs.XSTerm;
23
24 /**
25  * Store schema particle declaration.
26  *
27  * @xerces.internal
28  *
29  * @author Sandy Gao, IBM
30  *
31  * @version $Id: XSParticleDecl.java,v 1.15 2004/10/06 15:14:55 mrglavas Exp $
32  */

33 public class XSParticleDecl implements XSParticle {
34
35     // types of particles
36
public static final short PARTICLE_EMPTY = 0;
37     public static final short PARTICLE_ELEMENT = 1;
38     public static final short PARTICLE_WILDCARD = 2;
39     public static final short PARTICLE_MODELGROUP = 3;
40     public static final short PARTICLE_ZERO_OR_MORE = 4;
41     public static final short PARTICLE_ZERO_OR_ONE = 5;
42     public static final short PARTICLE_ONE_OR_MORE = 6;
43
44     // type of the particle
45
public short fType = PARTICLE_EMPTY;
46     
47     // term of the particle
48
// for PARTICLE_ELEMENT : the element decl
49
// for PARTICLE_WILDCARD: the wildcard decl
50
// for PARTICLE_MODELGROUP: the model group
51
public XSTerm fValue = null;
52
53     // minimum occurrence of this particle
54
public int fMinOccurs = 1;
55     // maximum occurrence of this particle
56
public int fMaxOccurs = 1;
57
58     // clone this decl
59
public XSParticleDecl makeClone() {
60         XSParticleDecl particle = new XSParticleDecl();
61         particle.fType = fType;
62         particle.fMinOccurs = fMinOccurs;
63         particle.fMaxOccurs = fMaxOccurs;
64         particle.fDescription = fDescription;
65         particle.fValue = fValue;
66         return particle;
67     }
68     
69     /**
70      * 3.9.6 Schema Component Constraint: Particle Emptiable
71      * whether this particle is emptible
72      */

73     public boolean emptiable() {
74         return minEffectiveTotalRange() == 0;
75     }
76
77     // whether this particle contains nothing
78
public boolean isEmpty() {
79         if (fType == PARTICLE_EMPTY)
80              return true;
81         if (fType == PARTICLE_ELEMENT || fType == PARTICLE_WILDCARD)
82             return false;
83
84         return ((XSModelGroupImpl)fValue).isEmpty();
85     }
86
87     /**
88      * 3.8.6 Effective Total Range (all and sequence) and
89      * Effective Total Range (choice)
90      * The following methods are used to return min/max range for a particle.
91      * They are not exactly the same as it's described in the spec, but all the
92      * values from the spec are retrievable by these methods.
93      */

94     public int minEffectiveTotalRange() {
95         if (fType == XSParticleDecl.PARTICLE_EMPTY) {
96             return 0;
97         }
98         if (fType == PARTICLE_MODELGROUP) {
99             return ((XSModelGroupImpl)fValue).minEffectiveTotalRange() * fMinOccurs;
100         }
101         return fMinOccurs;
102     }
103
104     public int maxEffectiveTotalRange() {
105         if (fType == XSParticleDecl.PARTICLE_EMPTY) {
106             return 0;
107         }
108         if (fType == PARTICLE_MODELGROUP) {
109             int max = ((XSModelGroupImpl)fValue).maxEffectiveTotalRange();
110             if (max == SchemaSymbols.OCCURRENCE_UNBOUNDED)
111                 return SchemaSymbols.OCCURRENCE_UNBOUNDED;
112             if (max != 0 && fMaxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED)
113                 return SchemaSymbols.OCCURRENCE_UNBOUNDED;
114             return max * fMaxOccurs;
115         }
116         return fMaxOccurs;
117     }
118
119     /**
120      * get the string description of this particle
121      */

122     private String JavaDoc fDescription = null;
123     public String JavaDoc toString() {
124         if (fDescription == null) {
125             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
126             appendParticle(buffer);
127             if (!(fMinOccurs == 0 && fMaxOccurs == 0 ||
128                   fMinOccurs == 1 && fMaxOccurs == 1)) {
129                 buffer.append("{" + fMinOccurs);
130                 if (fMaxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED)
131                     buffer.append("-UNBOUNDED");
132                 else if (fMinOccurs != fMaxOccurs)
133                     buffer.append("-" + fMaxOccurs);
134                 buffer.append("}");
135             }
136             fDescription = buffer.toString();
137         }
138         return fDescription;
139     }
140
141     /**
142      * append the string description of this particle to the string buffer
143      * this is for error message.
144      */

145     void appendParticle(StringBuffer JavaDoc buffer) {
146         switch (fType) {
147         case PARTICLE_EMPTY:
148             buffer.append("EMPTY");
149             break;
150         case PARTICLE_ELEMENT:
151             buffer.append(fValue.toString());
152             break;
153         case PARTICLE_WILDCARD:
154             buffer.append('(');
155             buffer.append(fValue.toString());
156             buffer.append(')');
157             break;
158         case PARTICLE_MODELGROUP:
159             buffer.append(fValue.toString());
160             break;
161         }
162     }
163
164     public void reset(){
165         fType = PARTICLE_EMPTY;
166         fValue = null;
167         fMinOccurs = 1;
168         fMaxOccurs = 1;
169         fDescription = null;
170     }
171
172     /**
173      * Get the type of the object, i.e ELEMENT_DECLARATION.
174      */

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

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

192     public String JavaDoc getNamespace() {
193         return null;
194     }
195
196     /**
197      * {min occurs} determines the minimum number of terms that can occur.
198      */

199     public int getMinOccurs() {
200         return fMinOccurs;
201     }
202
203     /**
204      * {max occurs} whether the maxOccurs value is unbounded.
205      */

206     public boolean getMaxOccursUnbounded() {
207         return fMaxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED;
208     }
209
210     /**
211      * {max occurs} determines the maximum number of terms that can occur.
212      */

213     public int getMaxOccurs() {
214         return fMaxOccurs;
215     }
216
217     /**
218      * {term} One of a model group, a wildcard, or an element declaration.
219      */

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

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