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