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.util.XSObjectListImpl;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSAnnotation;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSAttributeGroupDefinition;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSAttributeUse;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSConstants;
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.XSWildcard;
286N/A
286N/A/**
286N/A * The XML representation for an attribute group declaration
286N/A * schema component is a global <attributeGroup> element information item
286N/A *
286N/A * @xerces.internal
286N/A *
286N/A * @author Sandy Gao, IBM
286N/A * @author Rahul Srivastava, Sun Microsystems Inc.
286N/A *
286N/A * @version $Id: XSAttributeGroupDecl.java,v 1.7 2010-11-01 04:39:55 joehw Exp $
286N/A */
286N/Apublic class XSAttributeGroupDecl implements XSAttributeGroupDefinition {
286N/A
286N/A // name of the attribute group
286N/A public String fName = null;
286N/A // target namespace of the attribute group
286N/A public String fTargetNamespace = null;
286N/A // number of attribute uses included by this attribute group
286N/A int fAttrUseNum = 0;
286N/A // attribute uses included by this attribute group
286N/A private static final int INITIAL_SIZE = 5;
286N/A XSAttributeUseImpl[] fAttributeUses = new XSAttributeUseImpl[INITIAL_SIZE];
286N/A // attribute wildcard included by this attribute group
286N/A public XSWildcardDecl fAttributeWC = null;
286N/A // whether there is an attribute use whose type is or is derived from ID.
286N/A public String fIDAttrName = null;
286N/A
286N/A // optional annotation
286N/A public XSObjectList fAnnotations;
286N/A
286N/A protected XSObjectListImpl fAttrUses = null;
286N/A
286N/A // The namespace schema information item corresponding to the target namespace
286N/A // of the attribute group definition, if it is globally declared; or null otherwise.
286N/A private XSNamespaceItem fNamespaceItem = null;
286N/A
286N/A // add an attribute use
286N/A // if the type is derived from ID, but there is already another attribute
286N/A // use of type ID, then return the name of the other attribute use;
286N/A // otherwise, return null
286N/A public String addAttributeUse(XSAttributeUseImpl attrUse) {
286N/A
286N/A // if this attribute use is prohibited, then don't check whether it's
286N/A // of type ID
286N/A if (attrUse.fUse != SchemaSymbols.USE_PROHIBITED) {
286N/A if (attrUse.fAttrDecl.fType.isIDType()) {
286N/A // if there is already an attribute use of type ID,
286N/A // return its name (and don't add it to the list, to avoid
286N/A // interruption to instance validation.
286N/A if (fIDAttrName == null)
286N/A fIDAttrName = attrUse.fAttrDecl.fName;
286N/A else
286N/A return fIDAttrName;
286N/A }
286N/A }
286N/A
286N/A if (fAttrUseNum == fAttributeUses.length) {
286N/A fAttributeUses = resize(fAttributeUses, fAttrUseNum*2);
286N/A }
286N/A fAttributeUses[fAttrUseNum++] = attrUse;
286N/A
286N/A return null;
286N/A }
286N/A
286N/A public void replaceAttributeUse(XSAttributeUse oldUse, XSAttributeUseImpl newUse) {
286N/A for (int i=0; i<fAttrUseNum; i++) {
286N/A if (fAttributeUses[i] == oldUse) {
286N/A fAttributeUses[i] = newUse;
286N/A }
286N/A }
286N/A }
286N/A
286N/A public XSAttributeUse getAttributeUse(String namespace, String name) {
286N/A for (int i=0; i<fAttrUseNum; i++) {
286N/A if ( (fAttributeUses[i].fAttrDecl.fTargetNamespace == namespace) &&
286N/A (fAttributeUses[i].fAttrDecl.fName == name) )
286N/A return fAttributeUses[i];
286N/A }
286N/A
286N/A return null;
286N/A }
286N/A
286N/A public XSAttributeUse getAttributeUseNoProhibited(String namespace, String name) {
286N/A for (int i=0; i<fAttrUseNum; i++) {
286N/A if ( (fAttributeUses[i].fAttrDecl.fTargetNamespace == namespace) &&
286N/A (fAttributeUses[i].fAttrDecl.fName == name) &&
286N/A (fAttributeUses[i].fUse != SchemaSymbols.USE_PROHIBITED))
286N/A return fAttributeUses[i];
286N/A }
286N/A
286N/A return null;
286N/A }
286N/A
286N/A public void removeProhibitedAttrs() {
286N/A if (fAttrUseNum == 0) return;
286N/A // Remove all prohibited attributes.
286N/A int count = 0;
286N/A XSAttributeUseImpl[] uses = new XSAttributeUseImpl[fAttrUseNum];
286N/A for (int i = 0; i < fAttrUseNum; i++) {
286N/A if (fAttributeUses[i].fUse != SchemaSymbols.USE_PROHIBITED) {
286N/A uses[count++] = fAttributeUses[i];
286N/A }
286N/A }
286N/A fAttributeUses = uses;
286N/A fAttrUseNum = count;
286N/A
286N/A // Do not remove attributes that have the same name as the prohibited
286N/A // ones, because they are specified at the same level. Prohibited
286N/A // attributes are only to remove attributes from the base type in a
286N/A // restriction.
286N/A// int newCount = 0;
286N/A// if (pCount > 0) {
286N/A// OUTER: for (int i = 0; i < fAttrUseNum; i++) {
286N/A// if (fAttributeUses[i].fUse == SchemaSymbols.USE_PROHIBITED)
286N/A// continue;
286N/A// for (int j = 1; j <= pCount; j++) {
286N/A// if (fAttributeUses[i].fAttrDecl.fName == pUses[fAttrUseNum-pCount].fAttrDecl.fName &&
286N/A// fAttributeUses[i].fAttrDecl.fTargetNamespace == pUses[fAttrUseNum-pCount].fAttrDecl.fTargetNamespace) {
286N/A// continue OUTER;
286N/A// }
286N/A// }
286N/A// pUses[newCount++] = fAttributeUses[i];
286N/A// }
286N/A// fAttributeUses = pUses;
286N/A// fAttrUseNum = newCount;
286N/A// }
286N/A }
286N/A
286N/A /**
286N/A * Check that the attributes in this group validly restrict those from a base group.
286N/A * If an error is found, an Object[] is returned. This contains the arguments for the error message
286N/A * describing the error. The last element in the array (at index arr.length - 1) is the the error code.
286N/A * Returns null if there is no error.
286N/A *
286N/A * REVISIT: is there a better way of returning the appropriate information for the error?
286N/A *
286N/A * @param typeName the name of the type containing this attribute group, used for error reporting purposes
286N/A * @param baseGroup the XSAttributeGroupDecl that is the base we are checking against
286N/A */
286N/A public Object[] validRestrictionOf(String typeName, XSAttributeGroupDecl baseGroup) {
286N/A
286N/A Object[] errorArgs = null;
286N/A XSAttributeUseImpl attrUse = null;
286N/A XSAttributeDecl attrDecl = null;
286N/A XSAttributeUseImpl baseAttrUse = null;
286N/A XSAttributeDecl baseAttrDecl = null;
286N/A
286N/A for (int i=0; i<fAttrUseNum; i++) {
286N/A
286N/A attrUse = fAttributeUses[i];
286N/A attrDecl = attrUse.fAttrDecl;
286N/A
286N/A // Look for a match in the base
286N/A baseAttrUse = (XSAttributeUseImpl)baseGroup.getAttributeUse(attrDecl.fTargetNamespace,attrDecl.fName);
286N/A if (baseAttrUse != null) {
286N/A //
286N/A // derivation-ok-restriction. Constraint 2.1.1
286N/A //
286N/A
286N/A if (baseAttrUse.getRequired() && !attrUse.getRequired()) {
286N/A errorArgs = new Object[]{typeName, attrDecl.fName,
286N/A attrUse.fUse == SchemaSymbols.USE_OPTIONAL ? SchemaSymbols.ATTVAL_OPTIONAL : SchemaSymbols.ATTVAL_PROHIBITED,
286N/A "derivation-ok-restriction.2.1.1"};
286N/A return errorArgs;
286N/A }
286N/A
286N/A // if this attribute is prohibited in the derived type, don't
286N/A // need to check any of the following constraints.
286N/A if (attrUse.fUse == SchemaSymbols.USE_PROHIBITED) {
286N/A continue;
286N/A }
286N/A
286N/A baseAttrDecl = baseAttrUse.fAttrDecl;
286N/A //
286N/A // derivation-ok-restriction. Constraint 2.1.1
286N/A //
286N/A if (! XSConstraints.checkSimpleDerivationOk(attrDecl.fType,
286N/A baseAttrDecl.fType,
286N/A baseAttrDecl.fType.getFinal()) ) {
286N/A errorArgs = new Object[]{typeName, attrDecl.fName, attrDecl.fType.getName(),
286N/A baseAttrDecl.fType.getName(), "derivation-ok-restriction.2.1.2"};
286N/A return errorArgs;
286N/A }
286N/A
286N/A
286N/A //
286N/A // derivation-ok-restriction. Constraint 2.1.3
286N/A //
286N/A int baseConsType=baseAttrUse.fConstraintType!=XSConstants.VC_NONE?
286N/A baseAttrUse.fConstraintType:baseAttrDecl.getConstraintType();
286N/A int thisConstType = attrUse.fConstraintType!=XSConstants.VC_NONE?
286N/A attrUse.fConstraintType:attrDecl.getConstraintType();
286N/A
286N/A if (baseConsType == XSConstants.VC_FIXED) {
286N/A
286N/A if (thisConstType != XSConstants.VC_FIXED) {
286N/A errorArgs = new Object[]{typeName, attrDecl.fName,
286N/A "derivation-ok-restriction.2.1.3.a"};
286N/A return errorArgs;
286N/A } else {
286N/A // check the values are the same.
286N/A ValidatedInfo baseFixedValue=(baseAttrUse.fDefault!=null ?
286N/A baseAttrUse.fDefault: baseAttrDecl.fDefault);
286N/A ValidatedInfo thisFixedValue=(attrUse.fDefault!=null ?
286N/A attrUse.fDefault: attrDecl.fDefault);
286N/A if (!baseFixedValue.actualValue.equals(thisFixedValue.actualValue)) {
286N/A errorArgs = new Object[]{typeName, attrDecl.fName, thisFixedValue.stringValue(),
286N/A baseFixedValue.stringValue(), "derivation-ok-restriction.2.1.3.b"};
286N/A return errorArgs;
286N/A }
286N/A
286N/A }
286N/A
286N/A }
286N/A } else {
286N/A // No matching attribute in base - there should be a matching wildcard
286N/A
286N/A //
286N/A // derivation-ok-restriction. Constraint 2.2
286N/A //
286N/A if (baseGroup.fAttributeWC == null) {
286N/A errorArgs = new Object[]{typeName, attrDecl.fName,
286N/A "derivation-ok-restriction.2.2.a"};
286N/A return errorArgs;
286N/A }
286N/A else if (!baseGroup.fAttributeWC.allowNamespace(attrDecl.fTargetNamespace)) {
286N/A errorArgs = new Object[]{typeName, attrDecl.fName,
286N/A attrDecl.fTargetNamespace==null?"":attrDecl.fTargetNamespace,
286N/A "derivation-ok-restriction.2.2.b"};
286N/A return errorArgs;
286N/A }
286N/A }
286N/A }
286N/A
286N/A //
286N/A // Check that any REQUIRED attributes in the base have matching attributes
286N/A // in this group
286N/A // derivation-ok-restriction. Constraint 3
286N/A //
286N/A for (int i=0; i<baseGroup.fAttrUseNum; i++) {
286N/A
286N/A baseAttrUse = baseGroup.fAttributeUses[i];
286N/A
286N/A if (baseAttrUse.fUse == SchemaSymbols.USE_REQUIRED) {
286N/A
286N/A baseAttrDecl = baseAttrUse.fAttrDecl;
286N/A // Look for a match in this group
286N/A if (getAttributeUse(baseAttrDecl.fTargetNamespace,baseAttrDecl.fName) == null) {
286N/A errorArgs = new Object[]{typeName, baseAttrUse.fAttrDecl.fName,
286N/A "derivation-ok-restriction.3"};
286N/A return errorArgs;
286N/A }
286N/A }
286N/A }
286N/A
286N/A
286N/A // Now, check wildcards
286N/A //
286N/A // derivation-ok-restriction. Constraint 4
286N/A //
286N/A if (fAttributeWC != null) {
286N/A if (baseGroup.fAttributeWC == null) {
286N/A errorArgs = new Object[]{typeName, "derivation-ok-restriction.4.1"};
286N/A return errorArgs;
286N/A }
286N/A if (! fAttributeWC.isSubsetOf(baseGroup.fAttributeWC)) {
286N/A errorArgs = new Object[]{typeName, "derivation-ok-restriction.4.2"};
286N/A return errorArgs;
286N/A }
286N/A if (fAttributeWC.weakerProcessContents(baseGroup.fAttributeWC)) {
286N/A errorArgs = new Object[]{typeName,
286N/A fAttributeWC.getProcessContentsAsString(),
286N/A baseGroup.fAttributeWC.getProcessContentsAsString(),
286N/A "derivation-ok-restriction.4.3"};
286N/A return errorArgs;
286N/A }
286N/A }
286N/A
286N/A return null;
286N/A
286N/A }
286N/A
286N/A static final XSAttributeUseImpl[] resize(XSAttributeUseImpl[] oldArray, int newSize) {
286N/A XSAttributeUseImpl[] newArray = new XSAttributeUseImpl[newSize];
286N/A System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
286N/A return newArray;
286N/A }
286N/A
286N/A // reset the attribute group declaration
286N/A public void reset(){
286N/A fName = null;
286N/A fTargetNamespace = null;
286N/A // reset attribute uses
286N/A for (int i=0;i<fAttrUseNum;i++) {
286N/A fAttributeUses[i] = null;
286N/A }
286N/A fAttrUseNum = 0;
286N/A fAttributeWC = null;
286N/A fAnnotations = null;
286N/A fIDAttrName = null;
286N/A
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.ATTRIBUTE_GROUP;
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 * {attribute uses} A set of attribute uses.
286N/A */
286N/A public XSObjectList getAttributeUses() {
286N/A if (fAttrUses == null){
286N/A fAttrUses = new XSObjectListImpl(fAttributeUses, fAttrUseNum);
286N/A }
286N/A return fAttrUses;
286N/A }
286N/A
286N/A /**
286N/A * {attribute wildcard} Optional. A wildcard.
286N/A */
286N/A public XSWildcard getAttributeWildcard() {
286N/A return fAttributeWC;
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 * @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} // class XSAttributeGroupDecl