1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001-2003 The Apache Software Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.impl.xs.identity;
  58. import com.sun.org.apache.xerces.internal.xs.XSIDCDefinition;
  59. import com.sun.org.apache.xerces.internal.xs.StringList;
  60. import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem;
  61. import com.sun.org.apache.xerces.internal.xs.XSObjectList;
  62. import com.sun.org.apache.xerces.internal.xs.XSConstants;
  63. import com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl;
  64. import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
  65. import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl;
  66. /**
  67. * Base class of Schema identity constraint.
  68. *
  69. * @author Andy Clark, IBM
  70. * @version $Id: IdentityConstraint.java,v 1.9 2003/11/11 20:14:59 sandygao Exp $
  71. */
  72. public abstract class IdentityConstraint implements XSIDCDefinition {
  73. //
  74. // Data
  75. //
  76. /** type */
  77. protected short type;
  78. /** target namespace */
  79. protected String fNamespace;
  80. /** Identity constraint name. */
  81. protected String fIdentityConstraintName;
  82. /** name of owning element */
  83. protected String fElementName;
  84. /** Selector. */
  85. protected Selector fSelector;
  86. /** Field count. */
  87. protected int fFieldCount;
  88. /** Fields. */
  89. protected Field[] fFields;
  90. // optional annotations
  91. protected XSAnnotationImpl [] fAnnotations = null;
  92. // number of annotations in this identity constraint
  93. protected int fNumAnnotations;
  94. //
  95. // Constructors
  96. //
  97. /** Default constructor. */
  98. protected IdentityConstraint(String namespace, String identityConstraintName, String elemName) {
  99. fNamespace = namespace;
  100. fIdentityConstraintName = identityConstraintName;
  101. fElementName = elemName;
  102. } // <init>(String,String)
  103. //
  104. // Public methods
  105. //
  106. /** Returns the identity constraint name. */
  107. public String getIdentityConstraintName() {
  108. return fIdentityConstraintName;
  109. } // getIdentityConstraintName():String
  110. /** Sets the selector. */
  111. public void setSelector(Selector selector) {
  112. fSelector = selector;
  113. } // setSelector(Selector)
  114. /** Returns the selector. */
  115. public Selector getSelector() {
  116. return fSelector;
  117. } // getSelector():Selector
  118. /** Adds a field. */
  119. public void addField(Field field) {
  120. if (fFields == null)
  121. fFields = new Field[4];
  122. else if (fFieldCount == fFields.length)
  123. fFields = resize(fFields, fFieldCount*2);
  124. fFields[fFieldCount++] = field;
  125. } // addField(Field)
  126. /** Returns the field count. */
  127. public int getFieldCount() {
  128. return fFieldCount;
  129. } // getFieldCount():int
  130. /** Returns the field at the specified index. */
  131. public Field getFieldAt(int index) {
  132. return fFields[index];
  133. } // getFieldAt(int):Field
  134. // get the name of the owning element
  135. public String getElementName () {
  136. return fElementName;
  137. } // getElementName(): String
  138. //
  139. // Object methods
  140. //
  141. /** Returns a string representation of this object. */
  142. public String toString() {
  143. String s = super.toString();
  144. int index1 = s.lastIndexOf('$');
  145. if (index1 != -1) {
  146. return s.substring(index1 + 1);
  147. }
  148. int index2 = s.lastIndexOf('.');
  149. if (index2 != -1) {
  150. return s.substring(index2 + 1);
  151. }
  152. return s;
  153. } // toString():String
  154. // equals: returns true if and only if the String
  155. // representations of all members of both objects (except for
  156. // the elenemtName field) are equal.
  157. public boolean equals(IdentityConstraint id) {
  158. boolean areEqual = fIdentityConstraintName.equals(id.fIdentityConstraintName);
  159. if(!areEqual) return false;
  160. areEqual = fSelector.toString().equals(id.fSelector.toString());
  161. if(!areEqual) return false;
  162. areEqual = (fFieldCount == id.fFieldCount);
  163. if(!areEqual) return false;
  164. for(int i=0; i<fFieldCount; i++)
  165. if(!fFields[i].toString().equals(id.fFields[i].toString())) return false;
  166. return true;
  167. } // equals
  168. static final Field[] resize(Field[] oldArray, int newSize) {
  169. Field[] newArray = new Field[newSize];
  170. System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
  171. return newArray;
  172. }
  173. /**
  174. * Get the type of the object, i.e ELEMENT_DECLARATION.
  175. */
  176. public short getType() {
  177. return XSConstants.IDENTITY_CONSTRAINT;
  178. }
  179. /**
  180. * The <code>name</code> of this <code>XSObject</code> depending on the
  181. * <code>XSObject</code> type.
  182. */
  183. public String getName() {
  184. return fIdentityConstraintName;
  185. }
  186. /**
  187. * The namespace URI of this node, or <code>null</code> if it is
  188. * unspecified. defines how a namespace URI is attached to schema
  189. * components.
  190. */
  191. public String getNamespace() {
  192. return fNamespace;
  193. }
  194. /**
  195. * {identity-constraint category} One of key, keyref or unique.
  196. */
  197. public short getCategory() {
  198. return type;
  199. }
  200. /**
  201. * {selector} A restricted XPath ([XPath]) expression
  202. */
  203. public String getSelectorStr() {
  204. return fSelector.toString();
  205. }
  206. /**
  207. * {fields} A non-empty list of restricted XPath ([XPath]) expressions.
  208. */
  209. public StringList getFieldStrs() {
  210. String[] strs = new String[fFieldCount];
  211. for (int i = 0; i < fFieldCount; i++)
  212. strs[i] = fFields[i].toString();
  213. return new StringListImpl(strs, fFieldCount);
  214. }
  215. /**
  216. * {referenced key} Required if {identity-constraint category} is keyref,
  217. * forbidden otherwise. An identity-constraint definition with
  218. * {identity-constraint category} equal to key or unique.
  219. */
  220. public XSIDCDefinition getRefKey() {
  221. return null;
  222. }
  223. /**
  224. * Optional. Annotation.
  225. */
  226. public XSObjectList getAnnotations() {
  227. return new XSObjectListImpl(fAnnotations, fNumAnnotations);
  228. }
  229. /**
  230. * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
  231. */
  232. public XSNamespaceItem getNamespaceItem() {
  233. // REVISIT: implement
  234. return null;
  235. }
  236. public void addAnnotation(XSAnnotationImpl annotation) {
  237. if(annotation == null)
  238. return;
  239. if(fAnnotations == null) {
  240. fAnnotations = new XSAnnotationImpl[2];
  241. } else if(fNumAnnotations == fAnnotations.length) {
  242. XSAnnotationImpl[] newArray = new XSAnnotationImpl[fNumAnnotations << 1];
  243. System.arraycopy(fAnnotations, 0, newArray, 0, fNumAnnotations);
  244. fAnnotations = newArray;
  245. }
  246. fAnnotations[fNumAnnotations++] = annotation;
  247. }
  248. } // class IdentityConstraint