KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001, 2002 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.XSConstants;
61 import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem;
62 import com.sun.org.apache.xerces.internal.xs.XSParticle;
63 import com.sun.org.apache.xerces.internal.xs.XSTerm;
64
65 /**
66  * Store schema particle declaration.
67  *
68  * @author Sandy Gao, IBM
69  *
70  * @version $Id: XSParticleDecl.java,v 1.12 2003/11/11 20:14:58 sandygao Exp $
71  */

72 public class XSParticleDecl implements XSParticle {
73
74     // types of particles
75
public static final short PARTICLE_EMPTY = 0;
76     public static final short PARTICLE_ELEMENT = 1;
77     public static final short PARTICLE_WILDCARD = 2;
78     public static final short PARTICLE_MODELGROUP = 3;
79     public static final short PARTICLE_ZERO_OR_MORE = 4;
80     public static final short PARTICLE_ZERO_OR_ONE = 5;
81     public static final short PARTICLE_ONE_OR_MORE = 6;
82
83     // type of the particle
84
public short fType = PARTICLE_EMPTY;
85     
86     // term of the particle
87
// for PARTICLE_ELEMENT : the element decl
88
// for PARTICLE_WILDCARD: the wildcard decl
89
// for PARTICLE_MODELGROUP: the model group
90
public XSTerm fValue = null;
91
92     // minimum occurrence of this particle
93
public int fMinOccurs = 1;
94     // maximum occurrence of this particle
95
public int fMaxOccurs = 1;
96
97     // clone this decl
98
public XSParticleDecl makeClone() {
99         XSParticleDecl particle = new XSParticleDecl();
100         particle.fType = fType;
101         particle.fMinOccurs = fMinOccurs;
102         particle.fMaxOccurs = fMaxOccurs;
103         particle.fDescription = fDescription;
104         particle.fValue = fValue;
105         return particle;
106     }
107     
108     /**
109      * 3.9.6 Schema Component Constraint: Particle Emptiable
110      * whether this particle is emptible
111      */

112     public boolean emptiable() {
113         return minEffectiveTotalRange() == 0;
114     }
115
116     // whether this particle contains nothing
117
public boolean isEmpty() {
118         if (fType == PARTICLE_EMPTY)
119              return true;
120         if (fType == PARTICLE_ELEMENT || fType == PARTICLE_WILDCARD)
121             return false;
122
123         return ((XSModelGroupImpl)fValue).isEmpty();
124     }
125
126     /**
127      * 3.8.6 Effective Total Range (all and sequence) and
128      * Effective Total Range (choice)
129      * The following methods are used to return min/max range for a particle.
130      * They are not exactly the same as it's described in the spec, but all the
131      * values from the spec are retrievable by these methods.
132      */

133     public int minEffectiveTotalRange() {
134         if (fType == XSParticleDecl.PARTICLE_EMPTY) {
135             return 0;
136         }
137         if (fType == PARTICLE_MODELGROUP) {
138             return ((XSModelGroupImpl)fValue).minEffectiveTotalRange() * fMinOccurs;
139         }
140         return fMinOccurs;
141     }
142
143     public int maxEffectiveTotalRange() {
144         if (fType == XSParticleDecl.PARTICLE_EMPTY) {
145             return 0;
146         }
147         if (fType == PARTICLE_MODELGROUP) {
148             int max = ((XSModelGroupImpl)fValue).maxEffectiveTotalRange();
149             if (max == SchemaSymbols.OCCURRENCE_UNBOUNDED)
150                 return SchemaSymbols.OCCURRENCE_UNBOUNDED;
151             if (max != 0 && fMaxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED)
152                 return SchemaSymbols.OCCURRENCE_UNBOUNDED;
153             return max * fMaxOccurs;
154         }
155         return fMaxOccurs;
156     }
157
158     /**
159      * get the string description of this particle
160      */

161     private String JavaDoc fDescription = null;
162     public String JavaDoc toString() {
163         if (fDescription == null) {
164             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
165             appendParticle(buffer);
166             if (!(fMinOccurs == 0 && fMaxOccurs == 0 ||
167                   fMinOccurs == 1 && fMaxOccurs == 1)) {
168                 buffer.append("{" + fMinOccurs);
169                 if (fMaxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED)
170                     buffer.append("-UNBOUNDED");
171                 else if (fMinOccurs != fMaxOccurs)
172                     buffer.append("-" + fMaxOccurs);
173                 buffer.append("}");
174             }
175             fDescription = buffer.toString();
176         }
177         return fDescription;
178     }
179
180     /**
181      * append the string description of this particle to the string buffer
182      * this is for error message.
183      */

184     void appendParticle(StringBuffer JavaDoc buffer) {
185         switch (fType) {
186         case PARTICLE_EMPTY:
187             buffer.append("EMPTY");
188             break;
189         case PARTICLE_ELEMENT:
190         case PARTICLE_WILDCARD:
191             buffer.append('(');
192             buffer.append(fValue.toString());
193             buffer.append(')');
194             break;
195         case PARTICLE_MODELGROUP:
196             buffer.append(fValue.toString());
197             break;
198         }
199     }
200
201     public void reset(){
202         fType = PARTICLE_EMPTY;
203         fValue = null;
204         fMinOccurs = 1;
205         fMaxOccurs = 1;
206         fDescription = null;
207     }
208
209     /**
210      * Get the type of the object, i.e ELEMENT_DECLARATION.
211      */

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

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

229     public String JavaDoc getNamespace() {
230         return null;
231     }
232
233     /**
234      * {min occurs} determines the minimum number of terms that can occur.
235      */

236     public int getMinOccurs() {
237         return fMinOccurs;
238     }
239
240     /**
241      * {max occurs} whether the maxOccurs value is unbounded.
242      */

243     public boolean getMaxOccursUnbounded() {
244         return fMaxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED;
245     }
246
247     /**
248      * {max occurs} determines the maximum number of terms that can occur.
249      */

250     public int getMaxOccurs() {
251         return fMaxOccurs;
252     }
253
254     /**
255      * {term} One of a model group, a wildcard, or an element declaration.
256      */

257     public XSTerm getTerm() {
258         return fValue;
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 XSParticle
269
Popular Tags