KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-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.impl.dv.ValidatedInfo;
20 import org.apache.xerces.impl.xs.identity.IdentityConstraint;
21 import org.apache.xerces.xs.ShortList;
22 import org.apache.xerces.xs.XSAnnotation;
23 import org.apache.xerces.xs.XSComplexTypeDefinition;
24 import org.apache.xerces.xs.XSConstants;
25 import org.apache.xerces.xs.XSElementDeclaration;
26 import org.apache.xerces.xs.XSNamedMap;
27 import org.apache.xerces.xs.XSNamespaceItem;
28 import org.apache.xerces.xs.XSTypeDefinition;
29 import org.apache.xerces.impl.xs.util.XSNamedMapImpl;
30
31 /**
32  * The XML representation for an element declaration
33  * schema component is an <element> element information item
34  *
35  * @xerces.internal
36  *
37  * @author Elena Litani, IBM
38  * @author Sandy Gao, IBM
39  * @version $Id: XSElementDecl.java,v 1.19 2004/12/07 18:11:27 sandygao Exp $
40  */

41 public class XSElementDecl implements XSElementDeclaration {
42
43     // scopes
44
public final static short SCOPE_ABSENT = 0;
45     public final static short SCOPE_GLOBAL = 1;
46     public final static short SCOPE_LOCAL = 2;
47
48     // name of the element
49
public String JavaDoc fName = null;
50     // target namespace of the element
51
public String JavaDoc fTargetNamespace = null;
52     // type of the element
53
public XSTypeDefinition fType = null;
54     // misc flag of the element: nillable/abstract/fixed
55
short fMiscFlags = 0;
56     public short fScope = XSConstants.SCOPE_ABSENT;
57     // enclosing complex type, when the scope is local
58
XSComplexTypeDecl fEnclosingCT = null;
59     // block set (disallowed substitutions) of the element
60
public short fBlock = XSConstants.DERIVATION_NONE;
61     // final set (substitution group exclusions) of the element
62
public short fFinal = XSConstants.DERIVATION_NONE;
63     // optional annotation
64
public XSAnnotationImpl fAnnotation = null;
65     // value constraint value
66
public ValidatedInfo fDefault = null;
67     // the substitution group affiliation of the element
68
public XSElementDecl fSubGroup = null;
69     // identity constraints
70
static final int INITIAL_SIZE = 2;
71     int fIDCPos = 0;
72     IdentityConstraint[] fIDConstraints = new IdentityConstraint[INITIAL_SIZE];
73
74     private static final short CONSTRAINT_MASK = 3;
75     private static final short NILLABLE = 4;
76     private static final short ABSTRACT = 8;
77
78     // methods to get/set misc flag
79
public void setConstraintType(short constraintType) {
80         // first clear the bits
81
fMiscFlags ^= (fMiscFlags & CONSTRAINT_MASK);
82         // then set the proper one
83
fMiscFlags |= (constraintType & CONSTRAINT_MASK);
84     }
85     public void setIsNillable() {
86         fMiscFlags |= NILLABLE;
87     }
88     public void setIsAbstract() {
89         fMiscFlags |= ABSTRACT;
90     }
91     public void setIsGlobal() {
92         fScope = SCOPE_GLOBAL;
93     }
94     public void setIsLocal(XSComplexTypeDecl enclosingCT) {
95         fScope = SCOPE_LOCAL;
96         fEnclosingCT = enclosingCT;
97     }
98
99     public void addIDConstraint(IdentityConstraint idc) {
100         if (fIDCPos == fIDConstraints.length) {
101             fIDConstraints = resize(fIDConstraints, fIDCPos*2);
102         }
103         fIDConstraints[fIDCPos++] = idc;
104     }
105
106     public IdentityConstraint[] getIDConstraints() {
107         if (fIDCPos == 0) {
108             return null;
109         }
110         if (fIDCPos < fIDConstraints.length) {
111             fIDConstraints = resize(fIDConstraints, fIDCPos);
112         }
113         return fIDConstraints;
114     }
115
116     static final IdentityConstraint[] resize(IdentityConstraint[] oldArray, int newSize) {
117         IdentityConstraint[] newArray = new IdentityConstraint[newSize];
118         System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
119         return newArray;
120     }
121
122     /**
123      * get the string description of this element
124      */

125     private String JavaDoc fDescription = null;
126     public String JavaDoc toString() {
127         if (fDescription == null) {
128             if (fTargetNamespace != null) {
129                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(
130                     fTargetNamespace.length() +
131                     ((fName != null) ? fName.length() : 4) + 3);
132                 buffer.append('"');
133                 buffer.append(fTargetNamespace);
134                 buffer.append('"');
135                 buffer.append(':');
136                 buffer.append(fName);
137                 fDescription = buffer.toString();
138             }
139             else {
140                 fDescription = fName;
141             }
142         }
143         return fDescription;
144     }
145
146     /**
147      * get the hash code
148      */

149     public int hashCode() {
150         int code = fName.hashCode();
151         if (fTargetNamespace != null)
152             code = (code<<16)+fTargetNamespace.hashCode();
153         return code;
154     }
155
156     /**
157      * whether two decls are the same
158      */

159     public boolean equals(Object JavaDoc o) {
160         return o == this;
161     }
162
163     /**
164       * Reset current element declaration
165       */

166     public void reset(){
167
168         fName = null;
169         fTargetNamespace = null;
170         fType = null;
171         fMiscFlags = 0;
172         fBlock = XSConstants.DERIVATION_NONE;
173         fFinal = XSConstants.DERIVATION_NONE;
174         fDefault = null;
175         fAnnotation = null;
176         fSubGroup = null;
177         // reset identity constraints
178
for (int i=0;i<fIDCPos;i++) {
179             fIDConstraints[i] = null;
180         }
181
182         fIDCPos = 0;
183     }
184
185     /**
186      * Get the type of the object, i.e ELEMENT_DECLARATION.
187      */

188     public short getType() {
189         return XSConstants.ELEMENT_DECLARATION;
190     }
191
192     /**
193      * The <code>name</code> of this <code>XSObject</code> depending on the
194      * <code>XSObject</code> type.
195      */

196     public String JavaDoc getName() {
197         return fName;
198     }
199
200     /**
201      * The namespace URI of this node, or <code>null</code> if it is
202      * unspecified. defines how a namespace URI is attached to schema
203      * components.
204      */

205     public String JavaDoc getNamespace() {
206         return fTargetNamespace;
207     }
208
209     /**
210      * Either a simple type definition or a complex type definition.
211      */

212     public XSTypeDefinition getTypeDefinition() {
213         return fType;
214     }
215
216     /**
217      * Optional. Either global or a complex type definition (
218      * <code>ctDefinition</code>). This property is absent in the case of
219      * declarations within named model groups: their scope will be
220      * determined when they are used in the construction of complex type
221      * definitions.
222      */

223     public short getScope() {
224         return fScope;
225     }
226
227     /**
228      * Locally scoped declarations are available for use only within the
229      * complex type definition identified by the <code>scope</code>
230      * property.
231      */

232     public XSComplexTypeDefinition getEnclosingCTDefinition() {
233         return fEnclosingCT;
234     }
235
236     /**
237      * A value constraint: one of default, fixed.
238      */

239     public short getConstraintType() {
240         return (short)(fMiscFlags & CONSTRAINT_MASK);
241     }
242
243     /**
244      * A value constraint: The actual value (with respect to the {type
245      * definition})
246      */

247     public String JavaDoc getConstraintValue() {
248         // REVISIT: SCAPI: what's the proper representation
249
return getConstraintType() == XSConstants.VC_NONE ?
250                null :
251                fDefault.stringValue();
252     }
253
254     /**
255      * If {nillable} is true, then an element may also be valid if it carries
256      * the namespace qualified attribute with [local name] nil from
257      * namespace http://www.w3.org/2001/XMLSchema-instance and value true
258      * (see xsi:nil (2.6.2)) even if it has no text or element content
259      * despite a {content type} which would otherwise require content.
260      */

261     public boolean getNillable() {
262         return ((fMiscFlags & NILLABLE) != 0);
263     }
264
265     /**
266      * {identity-constraint definitions} A set of constraint definitions.
267      */

268     public XSNamedMap getIdentityConstraints() {
269         return new XSNamedMapImpl(fIDConstraints, fIDCPos);
270     }
271
272     /**
273      * {substitution group affiliation} Optional. A top-level element
274      * definition.
275      */

276     public XSElementDeclaration getSubstitutionGroupAffiliation() {
277         return fSubGroup;
278     }
279
280     /**
281      * Convenience method. Check if <code>exclusion</code> is a substitution
282      * group exclusion for this element declaration.
283      * @param exclusion Extension, restriction or none. Represents final
284      * set for the element.
285      * @return True if <code>exclusion</code> is a part of the substitution
286      * group exclusion subset.
287      */

288     public boolean isSubstitutionGroupExclusion(short exclusion) {
289         return (fFinal & exclusion) != 0;
290     }
291
292     /**
293      * Specifies if this declaration can be nominated as
294      * the {substitution group affiliation} of other
295      * element declarations having the same {type definition}
296      * or types derived therefrom.
297      *
298      * @return A bit flag representing {extension, restriction} or NONE.
299      */

300     public short getSubstitutionGroupExclusions() {
301         return fFinal;
302     }
303
304     /**
305      * Convenience method. Check if <code>disallowed</code> is a disallowed
306      * substitution for this element declaration.
307      * @param disallowed Substitution, extension, restriction or none.
308      * Represents a block set for the element.
309      * @return True if <code>disallowed</code> is a part of the substitution
310      * group exclusion subset.
311      */

312     public boolean isDisallowedSubstitution(short disallowed) {
313         return (fBlock & disallowed) != 0;
314     }
315
316     /**
317      * The supplied values for {disallowed substitutions}
318      *
319      * @return A bit flag representing {substitution, extension, restriction} or NONE.
320      */

321     public short getDisallowedSubstitutions() {
322         return fBlock;
323     }
324
325     /**
326      * {abstract} A boolean.
327      */

328     public boolean getAbstract() {
329         return ((fMiscFlags & ABSTRACT) != 0);
330     }
331
332     /**
333      * Optional. Annotation.
334      */

335     public XSAnnotation getAnnotation() {
336         return fAnnotation;
337     }
338     
339
340     /**
341      * @see org.apache.xerces.xs.XSObject#getNamespaceItem()
342      */

343     public XSNamespaceItem getNamespaceItem() {
344         // REVISIT: implement
345
return null;
346     }
347
348     public Object JavaDoc getActualVC() {
349         return getConstraintType() == XSConstants.VC_NONE ?
350                null :
351                fDefault.actualValue;
352     }
353
354     public short getActualVCType() {
355         return getConstraintType() == XSConstants.VC_NONE ?
356                XSConstants.UNAVAILABLE_DT :
357                fDefault.actualValueType;
358     }
359
360     public ShortList getItemValueTypes() {
361         return getConstraintType() == XSConstants.VC_NONE ?
362                null :
363                fDefault.itemValueTypes;
364     }
365
366 } // class XSElementDecl
367
Popular Tags