1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 2001-2004 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package com.sun.org.apache.xerces.internal.impl.xs.identity; 22 23 import com.sun.org.apache.xerces.internal.xs.XSIDCDefinition; 24 import com.sun.org.apache.xerces.internal.xs.StringList; 25 import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem; 26 import com.sun.org.apache.xerces.internal.xs.XSObjectList; 27 import com.sun.org.apache.xerces.internal.xs.XSConstants; 28 import com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl; 29 import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl; 30 import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl; 31 32 /** 33 * Base class of Schema identity constraint. 34 * 35 * @xerces.internal 36 * 37 * @author Andy Clark, IBM 38 */ 39 public abstract class IdentityConstraint implements XSIDCDefinition { 40 41 // 42 // Data 43 // 44 45 /** type */ 46 protected short type; 47 48 /** target namespace */ 49 protected String fNamespace; 50 51 /** Identity constraint name. */ 52 protected String fIdentityConstraintName; 53 54 /** name of owning element */ 55 protected String fElementName; 56 57 /** Selector. */ 58 protected Selector fSelector; 59 60 /** Field count. */ 61 protected int fFieldCount; 62 63 /** Fields. */ 64 protected Field[] fFields; 65 66 // optional annotations 67 protected XSAnnotationImpl [] fAnnotations = null; 68 69 // number of annotations in this identity constraint 70 protected int fNumAnnotations; 71 72 // 73 // Constructors 74 // 75 76 /** Default constructor. */ IdentityConstraint(String namespace, String identityConstraintName, String elemName)77 protected IdentityConstraint(String namespace, String identityConstraintName, String elemName) { 78 fNamespace = namespace; 79 fIdentityConstraintName = identityConstraintName; 80 fElementName = elemName; 81 } // <init>(String,String) 82 83 // 84 // Public methods 85 // 86 87 /** Returns the identity constraint name. */ getIdentityConstraintName()88 public String getIdentityConstraintName() { 89 return fIdentityConstraintName; 90 } // getIdentityConstraintName():String 91 92 /** Sets the selector. */ setSelector(Selector selector)93 public void setSelector(Selector selector) { 94 fSelector = selector; 95 } // setSelector(Selector) 96 97 /** Returns the selector. */ getSelector()98 public Selector getSelector() { 99 return fSelector; 100 } // getSelector():Selector 101 102 /** Adds a field. */ addField(Field field)103 public void addField(Field field) { 104 if (fFields == null) 105 fFields = new Field[4]; 106 else if (fFieldCount == fFields.length) 107 fFields = resize(fFields, fFieldCount*2); 108 fFields[fFieldCount++] = field; 109 } // addField(Field) 110 111 /** Returns the field count. */ getFieldCount()112 public int getFieldCount() { 113 return fFieldCount; 114 } // getFieldCount():int 115 116 /** Returns the field at the specified index. */ getFieldAt(int index)117 public Field getFieldAt(int index) { 118 return fFields[index]; 119 } // getFieldAt(int):Field 120 121 // get the name of the owning element getElementName()122 public String getElementName () { 123 return fElementName; 124 } // getElementName(): String 125 126 // 127 // Object methods 128 // 129 130 /** Returns a string representation of this object. */ toString()131 public String toString() { 132 String s = super.toString(); 133 int index1 = s.lastIndexOf('$'); 134 if (index1 != -1) { 135 return s.substring(index1 + 1); 136 } 137 int index2 = s.lastIndexOf('.'); 138 if (index2 != -1) { 139 return s.substring(index2 + 1); 140 } 141 return s; 142 } // toString():String 143 144 // equals: returns true if and only if the String 145 // representations of all members of both objects (except for 146 // the elenemtName field) are equal. equals(IdentityConstraint id)147 public boolean equals(IdentityConstraint id) { 148 boolean areEqual = fIdentityConstraintName.equals(id.fIdentityConstraintName); 149 if(!areEqual) return false; 150 areEqual = fSelector.toString().equals(id.fSelector.toString()); 151 if(!areEqual) return false; 152 areEqual = (fFieldCount == id.fFieldCount); 153 if(!areEqual) return false; 154 for(int i=0; i<fFieldCount; i++) 155 if(!fFields[i].toString().equals(id.fFields[i].toString())) return false; 156 return true; 157 } // equals 158 resize(Field[] oldArray, int newSize)159 static final Field[] resize(Field[] oldArray, int newSize) { 160 Field[] newArray = new Field[newSize]; 161 System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); 162 return newArray; 163 } 164 165 /** 166 * Get the type of the object, i.e ELEMENT_DECLARATION. 167 */ getType()168 public short getType() { 169 return XSConstants.IDENTITY_CONSTRAINT; 170 } 171 172 /** 173 * The <code>name</code> of this <code>XSObject</code> depending on the 174 * <code>XSObject</code> type. 175 */ getName()176 public String getName() { 177 return fIdentityConstraintName; 178 } 179 180 /** 181 * The namespace URI of this node, or <code>null</code> if it is 182 * unspecified. defines how a namespace URI is attached to schema 183 * components. 184 */ getNamespace()185 public String getNamespace() { 186 return fNamespace; 187 } 188 189 /** 190 * {identity-constraint category} One of key, keyref or unique. 191 */ getCategory()192 public short getCategory() { 193 return type; 194 } 195 196 /** 197 * {selector} A restricted XPath ([XPath]) expression 198 */ getSelectorStr()199 public String getSelectorStr() { 200 return (fSelector != null) ? fSelector.toString() : null; 201 } 202 203 /** 204 * {fields} A non-empty list of restricted XPath ([XPath]) expressions. 205 */ getFieldStrs()206 public StringList getFieldStrs() { 207 String[] strs = new String[fFieldCount]; 208 for (int i = 0; i < fFieldCount; i++) 209 strs[i] = fFields[i].toString(); 210 return new StringListImpl(strs, fFieldCount); 211 } 212 213 /** 214 * {referenced key} Required if {identity-constraint category} is keyref, 215 * forbidden otherwise. An identity-constraint definition with 216 * {identity-constraint category} equal to key or unique. 217 */ getRefKey()218 public XSIDCDefinition getRefKey() { 219 return null; 220 } 221 222 /** 223 * Optional. Annotation. 224 */ getAnnotations()225 public XSObjectList getAnnotations() { 226 return new XSObjectListImpl(fAnnotations, fNumAnnotations); 227 } 228 229 /** 230 * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem() 231 */ getNamespaceItem()232 public XSNamespaceItem getNamespaceItem() { 233 // REVISIT: implement 234 return null; 235 } 236 addAnnotation(XSAnnotationImpl annotation)237 public void addAnnotation(XSAnnotationImpl annotation) { 238 if(annotation == null) 239 return; 240 if(fAnnotations == null) { 241 fAnnotations = new XSAnnotationImpl[2]; 242 } else if(fNumAnnotations == fAnnotations.length) { 243 XSAnnotationImpl[] newArray = new XSAnnotationImpl[fNumAnnotations << 1]; 244 System.arraycopy(fAnnotations, 0, newArray, 0, fNumAnnotations); 245 fAnnotations = newArray; 246 } 247 fAnnotations[fNumAnnotations++] = annotation; 248 } 249 250 } // class IdentityConstraint 251