blob: 22395416934a8ba7790d706b5caaa086fd825511 [file] [log] [blame]
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2001 The Apache Software Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 1999, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.xerces.impl.xs.identity;
/**
* Base class of Schema identity constraint.
*
* @author Andy Clark, IBM
* @version $Id$
*/
public abstract class IdentityConstraint {
//
// Constants
//
/** Type: unique. */
public static final short UNIQUE = 0;
/** Type: key. */
public static final short KEY = 1;
/** Type: key reference. */
public static final short KEYREF = 2;
//
// Data
//
/** type */
protected short type;
/** Identity constraint name. */
protected String fIdentityConstraintName;
/** name of owning element */
protected String fElementName;
/** Selector. */
protected Selector fSelector;
/** Field count. */
protected int fFieldCount;
/** Fields. */
protected Field[] fFields;
//
// Constructors
//
/** Default constructor. */
protected IdentityConstraint(String identityConstraintName, String elemName) {
fIdentityConstraintName = identityConstraintName;
fElementName = elemName;
} // <init>(String,String)
//
// Public methods
//
/** Returns the identity constraint type. */
public short getType() {
return type;
} // getType(): short
/** Returns the identity constraint name. */
public String getIdentityConstraintName() {
return fIdentityConstraintName;
} // getIdentityConstraintName():String
/** Sets the selector. */
public void setSelector(Selector selector) {
fSelector = selector;
} // setSelector(Selector)
/** Returns the selector. */
public Selector getSelector() {
return fSelector;
} // getSelector():Selector
/** Adds a field. */
public void addField(Field field) {
if (fFields == null)
fFields = new Field[4];
else if (fFieldCount == fFields.length)
fFields = resize(fFields, fFieldCount*2);
fFields[fFieldCount++] = field;
} // addField(Field)
/** Returns the field count. */
public int getFieldCount() {
return fFieldCount;
} // getFieldCount():int
/** Returns the field at the specified index. */
public Field getFieldAt(int index) {
return fFields[index];
} // getFieldAt(int):Field
// get the name of the owning element
public String getElementName () {
return fElementName;
} // getElementName(): String
//
// Object methods
//
/** Returns a string representation of this object. */
public String toString() {
String s = super.toString();
int index1 = s.lastIndexOf('$');
if (index1 != -1) {
return s.substring(index1 + 1);
}
int index2 = s.lastIndexOf('.');
if (index2 != -1) {
return s.substring(index2 + 1);
}
return s;
} // toString():String
// equals: returns true if and only if the String
// representations of all members of both objects (except for
// the elenemtName field) are equal.
public boolean equals(IdentityConstraint id) {
boolean areEqual = fIdentityConstraintName.equals(id.fIdentityConstraintName);
if(!areEqual) return false;
areEqual = fSelector.toString().equals(id.fSelector.toString());
if(!areEqual) return false;
areEqual = (fFieldCount == id.fFieldCount);
if(!areEqual) return false;
for(int i=0; i<fFieldCount; i++)
if(!fFields[i].toString().equals(id.fFields[i].toString())) return false;
return true;
} // equals
static final Field[] resize(Field[] oldArray, int newSize) {
Field[] newArray = new Field[newSize];
System.arraycopy(oldArray, 0, newArray, 0, newSize);
return newArray;
}
} // class IdentityConstraint