KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-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.impl.dv.ValidatedInfo;
61 import com.sun.org.apache.xerces.internal.impl.xs.identity.IdentityConstraint;
62 import com.sun.org.apache.xerces.internal.xs.ShortList;
63 import com.sun.org.apache.xerces.internal.xs.XSAnnotation;
64 import com.sun.org.apache.xerces.internal.xs.XSComplexTypeDefinition;
65 import com.sun.org.apache.xerces.internal.xs.XSConstants;
66 import com.sun.org.apache.xerces.internal.xs.XSElementDeclaration;
67 import com.sun.org.apache.xerces.internal.xs.XSNamedMap;
68 import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem;
69 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
70 import com.sun.org.apache.xerces.internal.impl.xs.util.XSNamedMapImpl;
71
72 /**
73  * The XML representation for an element declaration
74  * schema component is an <element> element information item
75  *
76  * @author Elena Litani, IBM
77  * @author Sandy Gao, IBM
78  * @version $Id: XSElementDecl.java,v 1.14 2003/11/12 23:17:33 sandygao Exp $
79  */

80 public class XSElementDecl implements XSElementDeclaration {
81
82     // scopes
83
public final static short SCOPE_ABSENT = 0;
84     public final static short SCOPE_GLOBAL = 1;
85     public final static short SCOPE_LOCAL = 2;
86
87     // name of the element
88
public String JavaDoc fName = null;
89     // target namespace of the element
90
public String JavaDoc fTargetNamespace = null;
91     // type of the element
92
public XSTypeDefinition fType = null;
93     // misc flag of the element: nillable/abstract/fixed
94
short fMiscFlags = 0;
95     public short fScope = XSConstants.SCOPE_ABSENT;
96     // enclosing complex type, when the scope is local
97
XSComplexTypeDecl fEnclosingCT = null;
98     // block set (disallowed substitutions) of the element
99
public short fBlock = XSConstants.DERIVATION_NONE;
100     // final set (substitution group exclusions) of the element
101
public short fFinal = XSConstants.DERIVATION_NONE;
102     // optional annotation
103
public XSAnnotationImpl fAnnotation = null;
104     // value constraint value
105
public ValidatedInfo fDefault = null;
106     // the substitution group affiliation of the element
107
public XSElementDecl fSubGroup = null;
108     // identity constraints
109
static final int INITIAL_SIZE = 2;
110     int fIDCPos = 0;
111     IdentityConstraint[] fIDConstraints = new IdentityConstraint[INITIAL_SIZE];
112
113     private static final short CONSTRAINT_MASK = 3;
114     private static final short NILLABLE = 4;
115     private static final short ABSTRACT = 8;
116
117     // methods to get/set misc flag
118
public void setConstraintType(short constraintType) {
119         // first clear the bits
120
fMiscFlags ^= (fMiscFlags & CONSTRAINT_MASK);
121         // then set the proper one
122
fMiscFlags |= (constraintType & CONSTRAINT_MASK);
123     }
124     public void setIsNillable() {
125         fMiscFlags |= NILLABLE;
126     }
127     public void setIsAbstract() {
128         fMiscFlags |= ABSTRACT;
129     }
130     public void setIsGlobal() {
131         fScope = SCOPE_GLOBAL;
132     }
133     public void setIsLocal(XSComplexTypeDecl enclosingCT) {
134         fScope = SCOPE_LOCAL;
135         fEnclosingCT = enclosingCT;
136     }
137
138     public void addIDConstraint(IdentityConstraint idc) {
139         if (fIDCPos == fIDConstraints.length) {
140             fIDConstraints = resize(fIDConstraints, fIDCPos*2);
141         }
142         fIDConstraints[fIDCPos++] = idc;
143     }
144
145     public IdentityConstraint[] getIDConstraints() {
146         if (fIDCPos == 0) {
147             return null;
148         }
149         if (fIDCPos < fIDConstraints.length) {
150             fIDConstraints = resize(fIDConstraints, fIDCPos);
151         }
152         return fIDConstraints;
153     }
154
155     static final IdentityConstraint[] resize(IdentityConstraint[] oldArray, int newSize) {
156         IdentityConstraint[] newArray = new IdentityConstraint[newSize];
157         System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
158         return newArray;
159     }
160
161     /**
162      * get the string description of this element
163      */

164     private String JavaDoc fDescription = null;
165     public String JavaDoc toString() {
166         if (fDescription == null) {
167             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
168             buffer.append("\"");
169             if (fTargetNamespace != null)
170                 buffer.append(fTargetNamespace);
171             buffer.append("\"");
172             buffer.append(":");
173             buffer.append(fName);
174             fDescription = buffer.toString();
175         }
176         return fDescription;
177     }
178
179     /**
180      * get the hash code
181      */

182     public int hashCode() {
183         int code = fName.hashCode();
184         if (fTargetNamespace != null)
185             code = (code<<16)+fTargetNamespace.hashCode();
186         return code;
187     }
188
189     /**
190      * whether two decls are the same
191      */

192     public boolean equals(Object JavaDoc o) {
193         return o == this;
194     }
195
196     /**
197       * Reset current element declaration
198       */

199     public void reset(){
200
201         fName = null;
202         fTargetNamespace = null;
203         fType = null;
204         fMiscFlags = 0;
205         fBlock = XSConstants.DERIVATION_NONE;
206         fFinal = XSConstants.DERIVATION_NONE;
207         fDefault = null;
208         fAnnotation = null;
209         fSubGroup = null;
210         // reset identity constraints
211
for (int i=0;i<fIDCPos;i++) {
212             fIDConstraints[i] = null;
213         }
214
215         fIDCPos = 0;
216     }
217
218     /**
219      * Get the type of the object, i.e ELEMENT_DECLARATION.
220      */

221     public short getType() {
222         return XSConstants.ELEMENT_DECLARATION;
223     }
224
225     /**
226      * The <code>name</code> of this <code>XSObject</code> depending on the
227      * <code>XSObject</code> type.
228      */

229     public String JavaDoc getName() {
230         return fName;
231     }
232
233     /**
234      * The namespace URI of this node, or <code>null</code> if it is
235      * unspecified. defines how a namespace URI is attached to schema
236      * components.
237      */

238     public String JavaDoc getNamespace() {
239         return fTargetNamespace;
240     }
241
242     /**
243      * Either a simple type definition or a complex type definition.
244      */

245     public XSTypeDefinition getTypeDefinition() {
246         return fType;
247     }
248
249     /**
250      * Optional. Either global or a complex type definition (
251      * <code>ctDefinition</code>). This property is absent in the case of
252      * declarations within named model groups: their scope will be
253      * determined when they are used in the construction of complex type
254      * definitions.
255      */

256     public short getScope() {
257         return fScope;
258     }
259
260     /**
261      * Locally scoped declarations are available for use only within the
262      * complex type definition identified by the <code>scope</code>
263      * property.
264      */

265     public XSComplexTypeDefinition getEnclosingCTDefinition() {
266         return fEnclosingCT;
267     }
268
269     /**
270      * A value constraint: one of default, fixed.
271      */

272     public short getConstraintType() {
273         return (short)(fMiscFlags & CONSTRAINT_MASK);
274     }
275
276     /**
277      * A value constraint: The actual value (with respect to the {type
278      * definition})
279      */

280     public String JavaDoc getConstraintValue() {
281         // REVISIT: SCAPI: what's the proper representation
282
return getConstraintType() == XSConstants.VC_NONE ?
283                null :
284                fDefault.stringValue();
285     }
286
287     /**
288      * If {nillable} is true, then an element may also be valid if it carries
289      * the namespace qualified attribute with [local name] nil from
290      * namespace http://www.w3.org/2001/XMLSchema-instance and value true
291      * (see xsi:nil (2.6.2)) even if it has no text or element content
292      * despite a {content type} which would otherwise require content.
293      */

294     public boolean getNillable() {
295         return ((fMiscFlags & NILLABLE) != 0);
296     }
297
298     /**
299      * {identity-constraint definitions} A set of constraint definitions.
300      */

301     public XSNamedMap getIdentityConstraints() {
302         return new XSNamedMapImpl(fIDConstraints, fIDCPos);
303     }
304
305     /**
306      * {substitution group affiliation} Optional. A top-level element
307      * definition.
308      */

309     public XSElementDeclaration getSubstitutionGroupAffiliation() {
310         return fSubGroup;
311     }
312
313     /**
314      * Convenience method. Check if <code>exclusion</code> is a substitution
315      * group exclusion for this element declaration.
316      * @param exclusion Extension, restriction or none. Represents final
317      * set for the element.
318      * @return True if <code>exclusion</code> is a part of the substitution
319      * group exclusion subset.
320      */

321     public boolean isSubstitutionGroupExclusion(short exclusion) {
322         return (fFinal & exclusion) != 0;
323     }
324
325     /**
326      * Specifies if this declaration can be nominated as
327      * the {substitution group affiliation} of other
328      * element declarations having the same {type definition}
329      * or types derived therefrom.
330      *
331      * @return A bit flag representing {extension, restriction} or NONE.
332      */

333     public short getSubstitutionGroupExclusions() {
334         return fFinal;
335     }
336
337     /**
338      * Convenience method. Check if <code>disallowed</code> is a disallowed
339      * substitution for this element declaration.
340      * @param disallowed Substitution, extension, restriction or none.
341      * Represents a block set for the element.
342      * @return True if <code>disallowed</code> is a part of the substitution
343      * group exclusion subset.
344      */

345     public boolean isDisallowedSubstitution(short disallowed) {
346         return (fBlock & disallowed) != 0;
347     }
348
349     /**
350      * The supplied values for {disallowed substitutions}
351      *
352      * @return A bit flag representing {substitution, extension, restriction} or NONE.
353      */

354     public short getDisallowedSubstitutions() {
355         return fBlock;
356     }
357
358     /**
359      * {abstract} A boolean.
360      */

361     public boolean getAbstract() {
362         return ((fMiscFlags & ABSTRACT) != 0);
363     }
364
365     /**
366      * Optional. Annotation.
367      */

368     public XSAnnotation getAnnotation() {
369         return fAnnotation;
370     }
371     
372
373     /**
374      * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
375      */

376     public XSNamespaceItem getNamespaceItem() {
377         // REVISIT: implement
378
return null;
379     }
380
381     public Object JavaDoc getActualVC() {
382         return fDefault.actualValue;
383     }
384
385     public short getActualVCType() {
386         return fDefault.actualValueType;
387     }
388
389     public ShortList getItemValueTypes() {
390         return fDefault.itemValueTypes;
391     }
392
393 } // class XSElementDecl
394
Popular Tags