286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2001-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xerces.internal.impl.xs;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;
286N/Aimport com.sun.org.apache.xerces.internal.impl.xs.identity.IdentityConstraint;
286N/Aimport com.sun.org.apache.xerces.internal.impl.xs.util.XSNamedMapImpl;
286N/Aimport com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
286N/Aimport com.sun.org.apache.xerces.internal.xni.QName;
286N/Aimport com.sun.org.apache.xerces.internal.xs.ShortList;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSAnnotation;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSComplexTypeDefinition;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSConstants;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSElementDeclaration;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSNamedMap;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSNamespaceItem;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSObjectList;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
286N/A
286N/A/**
286N/A * The XML representation for an element declaration
286N/A * schema component is an <element> element information item
286N/A *
286N/A * @xerces.internal
286N/A *
286N/A * @author Elena Litani, IBM
286N/A * @author Sandy Gao, IBM
286N/A * @version $Id: XSElementDecl.java,v 1.7 2010-11-01 04:39:55 joehw Exp $
286N/A */
286N/Apublic class XSElementDecl implements XSElementDeclaration {
286N/A
286N/A // scopes
286N/A public final static short SCOPE_ABSENT = 0;
286N/A public final static short SCOPE_GLOBAL = 1;
286N/A public final static short SCOPE_LOCAL = 2;
286N/A
286N/A // name of the element
286N/A public String fName = null;
286N/A // target namespace of the element
286N/A public String fTargetNamespace = null;
286N/A // type of the element
286N/A public XSTypeDefinition fType = null;
286N/A public QName fUnresolvedTypeName = null;
286N/A // misc flag of the element: nillable/abstract/fixed
286N/A short fMiscFlags = 0;
286N/A public short fScope = XSConstants.SCOPE_ABSENT;
286N/A // enclosing complex type, when the scope is local
286N/A XSComplexTypeDecl fEnclosingCT = null;
286N/A // block set (disallowed substitutions) of the element
286N/A public short fBlock = XSConstants.DERIVATION_NONE;
286N/A // final set (substitution group exclusions) of the element
286N/A public short fFinal = XSConstants.DERIVATION_NONE;
286N/A // optional annotation
286N/A public XSObjectList fAnnotations = null;
286N/A // value constraint value
286N/A public ValidatedInfo fDefault = null;
286N/A // the substitution group affiliation of the element
286N/A public XSElementDecl fSubGroup = null;
286N/A // identity constraints
286N/A static final int INITIAL_SIZE = 2;
286N/A int fIDCPos = 0;
286N/A IdentityConstraint[] fIDConstraints = new IdentityConstraint[INITIAL_SIZE];
286N/A // The namespace schema information item corresponding to the target namespace
286N/A // of the element declaration, if it is globally declared; or null otherwise.
286N/A private XSNamespaceItem fNamespaceItem = null;
286N/A
286N/A private static final short CONSTRAINT_MASK = 3;
286N/A private static final short NILLABLE = 4;
286N/A private static final short ABSTRACT = 8;
286N/A
286N/A // methods to get/set misc flag
286N/A public void setConstraintType(short constraintType) {
286N/A // first clear the bits
286N/A fMiscFlags ^= (fMiscFlags & CONSTRAINT_MASK);
286N/A // then set the proper one
286N/A fMiscFlags |= (constraintType & CONSTRAINT_MASK);
286N/A }
286N/A public void setIsNillable() {
286N/A fMiscFlags |= NILLABLE;
286N/A }
286N/A public void setIsAbstract() {
286N/A fMiscFlags |= ABSTRACT;
286N/A }
286N/A public void setIsGlobal() {
286N/A fScope = SCOPE_GLOBAL;
286N/A }
286N/A public void setIsLocal(XSComplexTypeDecl enclosingCT) {
286N/A fScope = SCOPE_LOCAL;
286N/A fEnclosingCT = enclosingCT;
286N/A }
286N/A
286N/A public void addIDConstraint(IdentityConstraint idc) {
286N/A if (fIDCPos == fIDConstraints.length) {
286N/A fIDConstraints = resize(fIDConstraints, fIDCPos*2);
286N/A }
286N/A fIDConstraints[fIDCPos++] = idc;
286N/A }
286N/A
286N/A public IdentityConstraint[] getIDConstraints() {
286N/A if (fIDCPos == 0) {
286N/A return null;
286N/A }
286N/A if (fIDCPos < fIDConstraints.length) {
286N/A fIDConstraints = resize(fIDConstraints, fIDCPos);
286N/A }
286N/A return fIDConstraints;
286N/A }
286N/A
286N/A static final IdentityConstraint[] resize(IdentityConstraint[] oldArray, int newSize) {
286N/A IdentityConstraint[] newArray = new IdentityConstraint[newSize];
286N/A System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
286N/A return newArray;
286N/A }
286N/A
286N/A /**
286N/A * get the string description of this element
286N/A */
286N/A private String fDescription = null;
286N/A public String toString() {
286N/A if (fDescription == null) {
286N/A if (fTargetNamespace != null) {
286N/A StringBuffer buffer = new StringBuffer(
286N/A fTargetNamespace.length() +
286N/A ((fName != null) ? fName.length() : 4) + 3);
286N/A buffer.append('"');
286N/A buffer.append(fTargetNamespace);
286N/A buffer.append('"');
286N/A buffer.append(':');
286N/A buffer.append(fName);
286N/A fDescription = buffer.toString();
286N/A }
286N/A else {
286N/A fDescription = fName;
286N/A }
286N/A }
286N/A return fDescription;
286N/A }
286N/A
286N/A /**
286N/A * get the hash code
286N/A */
286N/A public int hashCode() {
286N/A int code = fName.hashCode();
286N/A if (fTargetNamespace != null)
286N/A code = (code<<16)+fTargetNamespace.hashCode();
286N/A return code;
286N/A }
286N/A
286N/A /**
286N/A * whether two decls are the same
286N/A */
286N/A public boolean equals(Object o) {
286N/A return o == this;
286N/A }
286N/A
286N/A /**
286N/A * Reset current element declaration
286N/A */
286N/A public void reset(){
286N/A fScope = XSConstants.SCOPE_ABSENT;
286N/A fName = null;
286N/A fTargetNamespace = null;
286N/A fType = null;
286N/A fUnresolvedTypeName = null;
286N/A fMiscFlags = 0;
286N/A fBlock = XSConstants.DERIVATION_NONE;
286N/A fFinal = XSConstants.DERIVATION_NONE;
286N/A fDefault = null;
286N/A fAnnotations = null;
286N/A fSubGroup = null;
286N/A // reset identity constraints
286N/A for (int i=0;i<fIDCPos;i++) {
286N/A fIDConstraints[i] = null;
286N/A }
286N/A
286N/A fIDCPos = 0;
286N/A }
286N/A
286N/A /**
286N/A * Get the type of the object, i.e ELEMENT_DECLARATION.
286N/A */
286N/A public short getType() {
286N/A return XSConstants.ELEMENT_DECLARATION;
286N/A }
286N/A
286N/A /**
286N/A * The <code>name</code> of this <code>XSObject</code> depending on the
286N/A * <code>XSObject</code> type.
286N/A */
286N/A public String getName() {
286N/A return fName;
286N/A }
286N/A
286N/A /**
286N/A * The namespace URI of this node, or <code>null</code> if it is
286N/A * unspecified. defines how a namespace URI is attached to schema
286N/A * components.
286N/A */
286N/A public String getNamespace() {
286N/A return fTargetNamespace;
286N/A }
286N/A
286N/A /**
286N/A * Either a simple type definition or a complex type definition.
286N/A */
286N/A public XSTypeDefinition getTypeDefinition() {
286N/A return fType;
286N/A }
286N/A
286N/A /**
286N/A * Optional. Either global or a complex type definition (
286N/A * <code>ctDefinition</code>). This property is absent in the case of
286N/A * declarations within named model groups: their scope will be
286N/A * determined when they are used in the construction of complex type
286N/A * definitions.
286N/A */
286N/A public short getScope() {
286N/A return fScope;
286N/A }
286N/A
286N/A /**
286N/A * Locally scoped declarations are available for use only within the
286N/A * complex type definition identified by the <code>scope</code>
286N/A * property.
286N/A */
286N/A public XSComplexTypeDefinition getEnclosingCTDefinition() {
286N/A return fEnclosingCT;
286N/A }
286N/A
286N/A /**
286N/A * A value constraint: one of default, fixed.
286N/A */
286N/A public short getConstraintType() {
286N/A return (short)(fMiscFlags & CONSTRAINT_MASK);
286N/A }
286N/A
286N/A /**
286N/A * A value constraint: The actual value (with respect to the {type
286N/A * definition})
286N/A */
286N/A public String getConstraintValue() {
286N/A // REVISIT: SCAPI: what's the proper representation
286N/A return getConstraintType() == XSConstants.VC_NONE ?
286N/A null :
286N/A fDefault.stringValue();
286N/A }
286N/A
286N/A /**
286N/A * If {nillable} is true, then an element may also be valid if it carries
286N/A * the namespace qualified attribute with [local name] nil from
286N/A * namespace http://www.w3.org/2001/XMLSchema-instance and value true
286N/A * (see xsi:nil (2.6.2)) even if it has no text or element content
286N/A * despite a {content type} which would otherwise require content.
286N/A */
286N/A public boolean getNillable() {
286N/A return ((fMiscFlags & NILLABLE) != 0);
286N/A }
286N/A
286N/A /**
286N/A * {identity-constraint definitions} A set of constraint definitions.
286N/A */
286N/A public XSNamedMap getIdentityConstraints() {
286N/A return new XSNamedMapImpl(fIDConstraints, fIDCPos);
286N/A }
286N/A
286N/A /**
286N/A * {substitution group affiliation} Optional. A top-level element
286N/A * definition.
286N/A */
286N/A public XSElementDeclaration getSubstitutionGroupAffiliation() {
286N/A return fSubGroup;
286N/A }
286N/A
286N/A /**
286N/A * Convenience method. Check if <code>exclusion</code> is a substitution
286N/A * group exclusion for this element declaration.
286N/A * @param exclusion Extension, restriction or none. Represents final
286N/A * set for the element.
286N/A * @return True if <code>exclusion</code> is a part of the substitution
286N/A * group exclusion subset.
286N/A */
286N/A public boolean isSubstitutionGroupExclusion(short exclusion) {
286N/A return (fFinal & exclusion) != 0;
286N/A }
286N/A
286N/A /**
286N/A * Specifies if this declaration can be nominated as
286N/A * the {substitution group affiliation} of other
286N/A * element declarations having the same {type definition}
286N/A * or types derived therefrom.
286N/A *
286N/A * @return A bit flag representing {extension, restriction} or NONE.
286N/A */
286N/A public short getSubstitutionGroupExclusions() {
286N/A return fFinal;
286N/A }
286N/A
286N/A /**
286N/A * Convenience method. Check if <code>disallowed</code> is a disallowed
286N/A * substitution for this element declaration.
286N/A * @param disallowed Substitution, extension, restriction or none.
286N/A * Represents a block set for the element.
286N/A * @return True if <code>disallowed</code> is a part of the substitution
286N/A * group exclusion subset.
286N/A */
286N/A public boolean isDisallowedSubstitution(short disallowed) {
286N/A return (fBlock & disallowed) != 0;
286N/A }
286N/A
286N/A /**
286N/A * The supplied values for {disallowed substitutions}
286N/A *
286N/A * @return A bit flag representing {substitution, extension, restriction} or NONE.
286N/A */
286N/A public short getDisallowedSubstitutions() {
286N/A return fBlock;
286N/A }
286N/A
286N/A /**
286N/A * {abstract} A boolean.
286N/A */
286N/A public boolean getAbstract() {
286N/A return ((fMiscFlags & ABSTRACT) != 0);
286N/A }
286N/A
286N/A /**
286N/A * Optional. Annotation.
286N/A */
286N/A public XSAnnotation getAnnotation() {
286N/A return (fAnnotations != null) ? (XSAnnotation) fAnnotations.item(0) : null;
286N/A }
286N/A
286N/A /**
286N/A * Optional. Annotations.
286N/A */
286N/A public XSObjectList getAnnotations() {
286N/A return (fAnnotations != null) ? fAnnotations : XSObjectListImpl.EMPTY_LIST;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * @see org.apache.xerces.xs.XSObject#getNamespaceItem()
286N/A */
286N/A public XSNamespaceItem getNamespaceItem() {
286N/A return fNamespaceItem;
286N/A }
286N/A
286N/A void setNamespaceItem(XSNamespaceItem namespaceItem) {
286N/A fNamespaceItem = namespaceItem;
286N/A }
286N/A
286N/A public Object getActualVC() {
286N/A return getConstraintType() == XSConstants.VC_NONE ?
286N/A null :
286N/A fDefault.actualValue;
286N/A }
286N/A
286N/A public short getActualVCType() {
286N/A return getConstraintType() == XSConstants.VC_NONE ?
286N/A XSConstants.UNAVAILABLE_DT :
286N/A fDefault.actualValueType;
286N/A }
286N/A
286N/A public ShortList getItemValueTypes() {
286N/A return getConstraintType() == XSConstants.VC_NONE ?
286N/A null :
286N/A fDefault.itemValueTypes;
286N/A }
286N/A
286N/A} // class XSElementDecl