001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.xs;
019:
020: import org.apache.xerces.impl.dv.ValidatedInfo;
021: import org.apache.xerces.impl.xs.identity.IdentityConstraint;
022: import org.apache.xerces.xs.ShortList;
023: import org.apache.xerces.xs.XSAnnotation;
024: import org.apache.xerces.xs.XSComplexTypeDefinition;
025: import org.apache.xerces.xs.XSConstants;
026: import org.apache.xerces.xs.XSElementDeclaration;
027: import org.apache.xerces.xs.XSNamedMap;
028: import org.apache.xerces.xs.XSNamespaceItem;
029: import org.apache.xerces.xs.XSObjectList;
030: import org.apache.xerces.xs.XSTypeDefinition;
031: import org.apache.xerces.impl.xs.util.XSNamedMapImpl;
032: import org.apache.xerces.impl.xs.util.XSObjectListImpl;
033:
034: /**
035: * The XML representation for an element declaration
036: * schema component is an <element> element information item
037: *
038: * @xerces.internal
039: *
040: * @author Elena Litani, IBM
041: * @author Sandy Gao, IBM
042: * @version $Id: XSElementDecl.java 449424 2006-09-24 16:22:30Z mrglavas $
043: */
044: public class XSElementDecl implements XSElementDeclaration {
045:
046: // scopes
047: public final static short SCOPE_ABSENT = 0;
048: public final static short SCOPE_GLOBAL = 1;
049: public final static short SCOPE_LOCAL = 2;
050:
051: // name of the element
052: public String fName = null;
053: // target namespace of the element
054: public String fTargetNamespace = null;
055: // type of the element
056: public XSTypeDefinition fType = null;
057: // misc flag of the element: nillable/abstract/fixed
058: short fMiscFlags = 0;
059: public short fScope = XSConstants.SCOPE_ABSENT;
060: // enclosing complex type, when the scope is local
061: XSComplexTypeDecl fEnclosingCT = null;
062: // block set (disallowed substitutions) of the element
063: public short fBlock = XSConstants.DERIVATION_NONE;
064: // final set (substitution group exclusions) of the element
065: public short fFinal = XSConstants.DERIVATION_NONE;
066: // optional annotation
067: public XSObjectList fAnnotations = null;
068: // value constraint value
069: public ValidatedInfo fDefault = null;
070: // the substitution group affiliation of the element
071: public XSElementDecl fSubGroup = null;
072: // identity constraints
073: static final int INITIAL_SIZE = 2;
074: int fIDCPos = 0;
075: IdentityConstraint[] fIDConstraints = new IdentityConstraint[INITIAL_SIZE];
076:
077: private static final short CONSTRAINT_MASK = 3;
078: private static final short NILLABLE = 4;
079: private static final short ABSTRACT = 8;
080:
081: // methods to get/set misc flag
082: public void setConstraintType(short constraintType) {
083: // first clear the bits
084: fMiscFlags ^= (fMiscFlags & CONSTRAINT_MASK);
085: // then set the proper one
086: fMiscFlags |= (constraintType & CONSTRAINT_MASK);
087: }
088:
089: public void setIsNillable() {
090: fMiscFlags |= NILLABLE;
091: }
092:
093: public void setIsAbstract() {
094: fMiscFlags |= ABSTRACT;
095: }
096:
097: public void setIsGlobal() {
098: fScope = SCOPE_GLOBAL;
099: }
100:
101: public void setIsLocal(XSComplexTypeDecl enclosingCT) {
102: fScope = SCOPE_LOCAL;
103: fEnclosingCT = enclosingCT;
104: }
105:
106: public void addIDConstraint(IdentityConstraint idc) {
107: if (fIDCPos == fIDConstraints.length) {
108: fIDConstraints = resize(fIDConstraints, fIDCPos * 2);
109: }
110: fIDConstraints[fIDCPos++] = idc;
111: }
112:
113: public IdentityConstraint[] getIDConstraints() {
114: if (fIDCPos == 0) {
115: return null;
116: }
117: if (fIDCPos < fIDConstraints.length) {
118: fIDConstraints = resize(fIDConstraints, fIDCPos);
119: }
120: return fIDConstraints;
121: }
122:
123: static final IdentityConstraint[] resize(
124: IdentityConstraint[] oldArray, int newSize) {
125: IdentityConstraint[] newArray = new IdentityConstraint[newSize];
126: System.arraycopy(oldArray, 0, newArray, 0, Math.min(
127: oldArray.length, newSize));
128: return newArray;
129: }
130:
131: /**
132: * get the string description of this element
133: */
134: private String fDescription = null;
135:
136: public String toString() {
137: if (fDescription == null) {
138: if (fTargetNamespace != null) {
139: StringBuffer buffer = new StringBuffer(fTargetNamespace
140: .length()
141: + ((fName != null) ? fName.length() : 4) + 3);
142: buffer.append('"');
143: buffer.append(fTargetNamespace);
144: buffer.append('"');
145: buffer.append(':');
146: buffer.append(fName);
147: fDescription = buffer.toString();
148: } else {
149: fDescription = fName;
150: }
151: }
152: return fDescription;
153: }
154:
155: /**
156: * get the hash code
157: */
158: public int hashCode() {
159: int code = fName.hashCode();
160: if (fTargetNamespace != null)
161: code = (code << 16) + fTargetNamespace.hashCode();
162: return code;
163: }
164:
165: /**
166: * whether two decls are the same
167: */
168: public boolean equals(Object o) {
169: return o == this ;
170: }
171:
172: /**
173: * Reset current element declaration
174: */
175: public void reset() {
176:
177: fName = null;
178: fTargetNamespace = null;
179: fType = null;
180: fMiscFlags = 0;
181: fBlock = XSConstants.DERIVATION_NONE;
182: fFinal = XSConstants.DERIVATION_NONE;
183: fDefault = null;
184: fAnnotations = null;
185: fSubGroup = null;
186: // reset identity constraints
187: for (int i = 0; i < fIDCPos; i++) {
188: fIDConstraints[i] = null;
189: }
190:
191: fIDCPos = 0;
192: }
193:
194: /**
195: * Get the type of the object, i.e ELEMENT_DECLARATION.
196: */
197: public short getType() {
198: return XSConstants.ELEMENT_DECLARATION;
199: }
200:
201: /**
202: * The <code>name</code> of this <code>XSObject</code> depending on the
203: * <code>XSObject</code> type.
204: */
205: public String getName() {
206: return fName;
207: }
208:
209: /**
210: * The namespace URI of this node, or <code>null</code> if it is
211: * unspecified. defines how a namespace URI is attached to schema
212: * components.
213: */
214: public String getNamespace() {
215: return fTargetNamespace;
216: }
217:
218: /**
219: * Either a simple type definition or a complex type definition.
220: */
221: public XSTypeDefinition getTypeDefinition() {
222: return fType;
223: }
224:
225: /**
226: * Optional. Either global or a complex type definition (
227: * <code>ctDefinition</code>). This property is absent in the case of
228: * declarations within named model groups: their scope will be
229: * determined when they are used in the construction of complex type
230: * definitions.
231: */
232: public short getScope() {
233: return fScope;
234: }
235:
236: /**
237: * Locally scoped declarations are available for use only within the
238: * complex type definition identified by the <code>scope</code>
239: * property.
240: */
241: public XSComplexTypeDefinition getEnclosingCTDefinition() {
242: return fEnclosingCT;
243: }
244:
245: /**
246: * A value constraint: one of default, fixed.
247: */
248: public short getConstraintType() {
249: return (short) (fMiscFlags & CONSTRAINT_MASK);
250: }
251:
252: /**
253: * A value constraint: The actual value (with respect to the {type
254: * definition})
255: */
256: public String getConstraintValue() {
257: // REVISIT: SCAPI: what's the proper representation
258: return getConstraintType() == XSConstants.VC_NONE ? null
259: : fDefault.stringValue();
260: }
261:
262: /**
263: * If {nillable} is true, then an element may also be valid if it carries
264: * the namespace qualified attribute with [local name] nil from
265: * namespace http://www.w3.org/2001/XMLSchema-instance and value true
266: * (see xsi:nil (2.6.2)) even if it has no text or element content
267: * despite a {content type} which would otherwise require content.
268: */
269: public boolean getNillable() {
270: return ((fMiscFlags & NILLABLE) != 0);
271: }
272:
273: /**
274: * {identity-constraint definitions} A set of constraint definitions.
275: */
276: public XSNamedMap getIdentityConstraints() {
277: return new XSNamedMapImpl(fIDConstraints, fIDCPos);
278: }
279:
280: /**
281: * {substitution group affiliation} Optional. A top-level element
282: * definition.
283: */
284: public XSElementDeclaration getSubstitutionGroupAffiliation() {
285: return fSubGroup;
286: }
287:
288: /**
289: * Convenience method. Check if <code>exclusion</code> is a substitution
290: * group exclusion for this element declaration.
291: * @param exclusion Extension, restriction or none. Represents final
292: * set for the element.
293: * @return True if <code>exclusion</code> is a part of the substitution
294: * group exclusion subset.
295: */
296: public boolean isSubstitutionGroupExclusion(short exclusion) {
297: return (fFinal & exclusion) != 0;
298: }
299:
300: /**
301: * Specifies if this declaration can be nominated as
302: * the {substitution group affiliation} of other
303: * element declarations having the same {type definition}
304: * or types derived therefrom.
305: *
306: * @return A bit flag representing {extension, restriction} or NONE.
307: */
308: public short getSubstitutionGroupExclusions() {
309: return fFinal;
310: }
311:
312: /**
313: * Convenience method. Check if <code>disallowed</code> is a disallowed
314: * substitution for this element declaration.
315: * @param disallowed Substitution, extension, restriction or none.
316: * Represents a block set for the element.
317: * @return True if <code>disallowed</code> is a part of the substitution
318: * group exclusion subset.
319: */
320: public boolean isDisallowedSubstitution(short disallowed) {
321: return (fBlock & disallowed) != 0;
322: }
323:
324: /**
325: * The supplied values for {disallowed substitutions}
326: *
327: * @return A bit flag representing {substitution, extension, restriction} or NONE.
328: */
329: public short getDisallowedSubstitutions() {
330: return fBlock;
331: }
332:
333: /**
334: * {abstract} A boolean.
335: */
336: public boolean getAbstract() {
337: return ((fMiscFlags & ABSTRACT) != 0);
338: }
339:
340: /**
341: * Optional. Annotation.
342: */
343: public XSAnnotation getAnnotation() {
344: return (fAnnotations != null) ? (XSAnnotation) fAnnotations
345: .item(0) : null;
346: }
347:
348: /**
349: * Optional. Annotations.
350: */
351: public XSObjectList getAnnotations() {
352: return (fAnnotations != null) ? fAnnotations
353: : XSObjectListImpl.EMPTY_LIST;
354: }
355:
356: /**
357: * @see org.apache.xerces.xs.XSObject#getNamespaceItem()
358: */
359: public XSNamespaceItem getNamespaceItem() {
360: // REVISIT: implement
361: return null;
362: }
363:
364: public Object getActualVC() {
365: return getConstraintType() == XSConstants.VC_NONE ? null
366: : fDefault.actualValue;
367: }
368:
369: public short getActualVCType() {
370: return getConstraintType() == XSConstants.VC_NONE ? XSConstants.UNAVAILABLE_DT
371: : fDefault.actualValueType;
372: }
373:
374: public ShortList getItemValueTypes() {
375: return getConstraintType() == XSConstants.VC_NONE ? null
376: : fDefault.itemValueTypes;
377: }
378:
379: } // class XSElementDecl
|