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.identity;
019:
020: import org.apache.xerces.xs.XSIDCDefinition;
021: import org.apache.xerces.xs.StringList;
022: import org.apache.xerces.xs.XSNamespaceItem;
023: import org.apache.xerces.xs.XSObjectList;
024: import org.apache.xerces.xs.XSConstants;
025: import org.apache.xerces.impl.xs.util.StringListImpl;
026: import org.apache.xerces.impl.xs.util.XSObjectListImpl;
027: import org.apache.xerces.impl.xs.XSAnnotationImpl;
028:
029: /**
030: * Base class of Schema identity constraint.
031: *
032: * @xerces.internal
033: *
034: * @author Andy Clark, IBM
035: * @version $Id: IdentityConstraint.java 572110 2007-09-02 19:04:44Z mrglavas $
036: */
037: public abstract class IdentityConstraint implements XSIDCDefinition {
038:
039: //
040: // Data
041: //
042:
043: /** type */
044: protected short type;
045:
046: /** target namespace */
047: protected final String fNamespace;
048:
049: /** Identity constraint name. */
050: protected final String fIdentityConstraintName;
051:
052: /** name of owning element */
053: protected final String fElementName;
054:
055: /** Selector. */
056: protected Selector fSelector;
057:
058: /** Field count. */
059: protected int fFieldCount;
060:
061: /** Fields. */
062: protected Field[] fFields;
063:
064: // optional annotations
065: protected XSAnnotationImpl[] fAnnotations = null;
066:
067: // number of annotations in this identity constraint
068: protected int fNumAnnotations;
069:
070: //
071: // Constructors
072: //
073:
074: /** Default constructor. */
075: protected IdentityConstraint(String namespace,
076: String identityConstraintName, String elemName) {
077: fNamespace = namespace;
078: fIdentityConstraintName = identityConstraintName;
079: fElementName = elemName;
080: } // <init>(String,String)
081:
082: //
083: // Public methods
084: //
085:
086: /** Returns the identity constraint name. */
087: public String getIdentityConstraintName() {
088: return fIdentityConstraintName;
089: } // getIdentityConstraintName():String
090:
091: /** Sets the selector. */
092: public void setSelector(Selector selector) {
093: fSelector = selector;
094: } // setSelector(Selector)
095:
096: /** Returns the selector. */
097: public Selector getSelector() {
098: return fSelector;
099: } // getSelector():Selector
100:
101: /** Adds a field. */
102: public void addField(Field field) {
103: if (fFields == null)
104: fFields = new Field[4];
105: else if (fFieldCount == fFields.length)
106: fFields = resize(fFields, fFieldCount * 2);
107: fFields[fFieldCount++] = field;
108: } // addField(Field)
109:
110: /** Returns the field count. */
111: public int getFieldCount() {
112: return fFieldCount;
113: } // getFieldCount():int
114:
115: /** Returns the field at the specified index. */
116: public Field getFieldAt(int index) {
117: return fFields[index];
118: } // getFieldAt(int):Field
119:
120: // get the name of the owning element
121: public String getElementName() {
122: return fElementName;
123: } // getElementName(): String
124:
125: //
126: // Object methods
127: //
128:
129: /** Returns a string representation of this object. */
130: public String toString() {
131: String s = super .toString();
132: int index1 = s.lastIndexOf('$');
133: if (index1 != -1) {
134: return s.substring(index1 + 1);
135: }
136: int index2 = s.lastIndexOf('.');
137: if (index2 != -1) {
138: return s.substring(index2 + 1);
139: }
140: return s;
141: } // toString():String
142:
143: // equals: returns true if and only if the String
144: // representations of all members of both objects (except for
145: // the elenemtName field) are equal.
146: public boolean equals(IdentityConstraint id) {
147: boolean areEqual = fIdentityConstraintName
148: .equals(id.fIdentityConstraintName);
149: if (!areEqual)
150: return false;
151: areEqual = fSelector.toString().equals(id.fSelector.toString());
152: if (!areEqual)
153: return false;
154: areEqual = (fFieldCount == id.fFieldCount);
155: if (!areEqual)
156: return false;
157: for (int i = 0; i < fFieldCount; i++)
158: if (!fFields[i].toString().equals(id.fFields[i].toString()))
159: return false;
160: return true;
161: } // equals
162:
163: static final Field[] resize(Field[] oldArray, int newSize) {
164: Field[] newArray = new Field[newSize];
165: System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
166: return newArray;
167: }
168:
169: /**
170: * Get the type of the object, i.e ELEMENT_DECLARATION.
171: */
172: public short getType() {
173: return XSConstants.IDENTITY_CONSTRAINT;
174: }
175:
176: /**
177: * The <code>name</code> of this <code>XSObject</code> depending on the
178: * <code>XSObject</code> type.
179: */
180: public String getName() {
181: return fIdentityConstraintName;
182: }
183:
184: /**
185: * The namespace URI of this node, or <code>null</code> if it is
186: * unspecified. defines how a namespace URI is attached to schema
187: * components.
188: */
189: public String getNamespace() {
190: return fNamespace;
191: }
192:
193: /**
194: * {identity-constraint category} One of key, keyref or unique.
195: */
196: public short getCategory() {
197: return type;
198: }
199:
200: /**
201: * {selector} A restricted XPath ([XPath]) expression
202: */
203: public String getSelectorStr() {
204: return (fSelector != null) ? fSelector.toString() : null;
205: }
206:
207: /**
208: * {fields} A non-empty list of restricted XPath ([XPath]) expressions.
209: */
210: public StringList getFieldStrs() {
211: String[] strs = new String[fFieldCount];
212: for (int i = 0; i < fFieldCount; i++)
213: strs[i] = fFields[i].toString();
214: return new StringListImpl(strs, fFieldCount);
215: }
216:
217: /**
218: * {referenced key} Required if {identity-constraint category} is keyref,
219: * forbidden otherwise. An identity-constraint definition with
220: * {identity-constraint category} equal to key or unique.
221: */
222: public XSIDCDefinition getRefKey() {
223: return null;
224: }
225:
226: /**
227: * Optional. Annotation.
228: */
229: public XSObjectList getAnnotations() {
230: return new XSObjectListImpl(fAnnotations, fNumAnnotations);
231: }
232:
233: /**
234: * @see org.apache.xerces.xs.XSObject#getNamespaceItem()
235: */
236: public XSNamespaceItem getNamespaceItem() {
237: // REVISIT: implement
238: return null;
239: }
240:
241: public void addAnnotation(XSAnnotationImpl annotation) {
242: if (annotation == null)
243: return;
244: if (fAnnotations == null) {
245: fAnnotations = new XSAnnotationImpl[2];
246: } else if (fNumAnnotations == fAnnotations.length) {
247: XSAnnotationImpl[] newArray = new XSAnnotationImpl[fNumAnnotations << 1];
248: System.arraycopy(fAnnotations, 0, newArray, 0,
249: fNumAnnotations);
250: fAnnotations = newArray;
251: }
252: fAnnotations[fNumAnnotations++] = annotation;
253: }
254:
255: } // class IdentityConstraint
|