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.identity;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSIDCDefinition;
286N/Aimport com.sun.org.apache.xerces.internal.xs.StringList;
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.XSConstants;
286N/Aimport com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl;
286N/Aimport com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
286N/Aimport com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl;
286N/A
286N/A/**
286N/A * Base class of Schema identity constraint.
286N/A *
286N/A * @xerces.internal
286N/A *
286N/A * @author Andy Clark, IBM
286N/A */
286N/Apublic abstract class IdentityConstraint implements XSIDCDefinition {
286N/A
286N/A //
286N/A // Data
286N/A //
286N/A
286N/A /** type */
286N/A protected short type;
286N/A
286N/A /** target namespace */
286N/A protected String fNamespace;
286N/A
286N/A /** Identity constraint name. */
286N/A protected String fIdentityConstraintName;
286N/A
286N/A /** name of owning element */
286N/A protected String fElementName;
286N/A
286N/A /** Selector. */
286N/A protected Selector fSelector;
286N/A
286N/A /** Field count. */
286N/A protected int fFieldCount;
286N/A
286N/A /** Fields. */
286N/A protected Field[] fFields;
286N/A
286N/A // optional annotations
286N/A protected XSAnnotationImpl [] fAnnotations = null;
286N/A
286N/A // number of annotations in this identity constraint
286N/A protected int fNumAnnotations;
286N/A
286N/A //
286N/A // Constructors
286N/A //
286N/A
286N/A /** Default constructor. */
286N/A protected IdentityConstraint(String namespace, String identityConstraintName, String elemName) {
286N/A fNamespace = namespace;
286N/A fIdentityConstraintName = identityConstraintName;
286N/A fElementName = elemName;
286N/A } // <init>(String,String)
286N/A
286N/A //
286N/A // Public methods
286N/A //
286N/A
286N/A /** Returns the identity constraint name. */
286N/A public String getIdentityConstraintName() {
286N/A return fIdentityConstraintName;
286N/A } // getIdentityConstraintName():String
286N/A
286N/A /** Sets the selector. */
286N/A public void setSelector(Selector selector) {
286N/A fSelector = selector;
286N/A } // setSelector(Selector)
286N/A
286N/A /** Returns the selector. */
286N/A public Selector getSelector() {
286N/A return fSelector;
286N/A } // getSelector():Selector
286N/A
286N/A /** Adds a field. */
286N/A public void addField(Field field) {
286N/A if (fFields == null)
286N/A fFields = new Field[4];
286N/A else if (fFieldCount == fFields.length)
286N/A fFields = resize(fFields, fFieldCount*2);
286N/A fFields[fFieldCount++] = field;
286N/A } // addField(Field)
286N/A
286N/A /** Returns the field count. */
286N/A public int getFieldCount() {
286N/A return fFieldCount;
286N/A } // getFieldCount():int
286N/A
286N/A /** Returns the field at the specified index. */
286N/A public Field getFieldAt(int index) {
286N/A return fFields[index];
286N/A } // getFieldAt(int):Field
286N/A
286N/A // get the name of the owning element
286N/A public String getElementName () {
286N/A return fElementName;
286N/A } // getElementName(): String
286N/A
286N/A //
286N/A // Object methods
286N/A //
286N/A
286N/A /** Returns a string representation of this object. */
286N/A public String toString() {
286N/A String s = super.toString();
286N/A int index1 = s.lastIndexOf('$');
286N/A if (index1 != -1) {
286N/A return s.substring(index1 + 1);
286N/A }
286N/A int index2 = s.lastIndexOf('.');
286N/A if (index2 != -1) {
286N/A return s.substring(index2 + 1);
286N/A }
286N/A return s;
286N/A } // toString():String
286N/A
286N/A // equals: returns true if and only if the String
286N/A // representations of all members of both objects (except for
286N/A // the elenemtName field) are equal.
286N/A public boolean equals(IdentityConstraint id) {
286N/A boolean areEqual = fIdentityConstraintName.equals(id.fIdentityConstraintName);
286N/A if(!areEqual) return false;
286N/A areEqual = fSelector.toString().equals(id.fSelector.toString());
286N/A if(!areEqual) return false;
286N/A areEqual = (fFieldCount == id.fFieldCount);
286N/A if(!areEqual) return false;
286N/A for(int i=0; i<fFieldCount; i++)
286N/A if(!fFields[i].toString().equals(id.fFields[i].toString())) return false;
286N/A return true;
286N/A } // equals
286N/A
286N/A static final Field[] resize(Field[] oldArray, int newSize) {
286N/A Field[] newArray = new Field[newSize];
286N/A System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
286N/A return newArray;
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.IDENTITY_CONSTRAINT;
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 fIdentityConstraintName;
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 fNamespace;
286N/A }
286N/A
286N/A /**
286N/A * {identity-constraint category} One of key, keyref or unique.
286N/A */
286N/A public short getCategory() {
286N/A return type;
286N/A }
286N/A
286N/A /**
286N/A * {selector} A restricted XPath ([XPath]) expression
286N/A */
286N/A public String getSelectorStr() {
286N/A return (fSelector != null) ? fSelector.toString() : null;
286N/A }
286N/A
286N/A /**
286N/A * {fields} A non-empty list of restricted XPath ([XPath]) expressions.
286N/A */
286N/A public StringList getFieldStrs() {
286N/A String[] strs = new String[fFieldCount];
286N/A for (int i = 0; i < fFieldCount; i++)
286N/A strs[i] = fFields[i].toString();
286N/A return new StringListImpl(strs, fFieldCount);
286N/A }
286N/A
286N/A /**
286N/A * {referenced key} Required if {identity-constraint category} is keyref,
286N/A * forbidden otherwise. An identity-constraint definition with
286N/A * {identity-constraint category} equal to key or unique.
286N/A */
286N/A public XSIDCDefinition getRefKey() {
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * Optional. Annotation.
286N/A */
286N/A public XSObjectList getAnnotations() {
286N/A return new XSObjectListImpl(fAnnotations, fNumAnnotations);
286N/A }
286N/A
286N/A /**
286N/A * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
286N/A */
286N/A public XSNamespaceItem getNamespaceItem() {
286N/A // REVISIT: implement
286N/A return null;
286N/A }
286N/A
286N/A public void addAnnotation(XSAnnotationImpl annotation) {
286N/A if(annotation == null)
286N/A return;
286N/A if(fAnnotations == null) {
286N/A fAnnotations = new XSAnnotationImpl[2];
286N/A } else if(fNumAnnotations == fAnnotations.length) {
286N/A XSAnnotationImpl[] newArray = new XSAnnotationImpl[fNumAnnotations << 1];
286N/A System.arraycopy(fAnnotations, 0, newArray, 0, fNumAnnotations);
286N/A fAnnotations = newArray;
286N/A }
286N/A fAnnotations[fNumAnnotations++] = annotation;
286N/A }
286N/A
286N/A} // class IdentityConstraint