KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > identity > IdentityConstraint


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.xs.identity;
18
19 import org.apache.xerces.xs.XSIDCDefinition;
20 import org.apache.xerces.xs.StringList;
21 import org.apache.xerces.xs.XSNamespaceItem;
22 import org.apache.xerces.xs.XSObjectList;
23 import org.apache.xerces.xs.XSConstants;
24 import org.apache.xerces.impl.xs.util.StringListImpl;
25 import org.apache.xerces.impl.xs.util.XSObjectListImpl;
26 import org.apache.xerces.impl.xs.XSAnnotationImpl;
27
28 /**
29  * Base class of Schema identity constraint.
30  *
31  * @xerces.internal
32  *
33  * @author Andy Clark, IBM
34  * @version $Id: IdentityConstraint.java,v 1.12 2005/06/23 19:16:20 mrglavas Exp $
35  */

36 public abstract class IdentityConstraint implements XSIDCDefinition {
37
38     //
39
// Data
40
//
41

42     /** type */
43     protected short type;
44
45     /** target namespace */
46     protected String JavaDoc fNamespace;
47     
48     /** Identity constraint name. */
49     protected String JavaDoc fIdentityConstraintName;
50
51     /** name of owning element */
52     protected String JavaDoc fElementName;
53
54     /** Selector. */
55     protected Selector fSelector;
56
57     /** Field count. */
58     protected int fFieldCount;
59
60     /** Fields. */
61     protected Field[] fFields;
62
63     // optional annotations
64
protected XSAnnotationImpl [] fAnnotations = null;
65
66     // number of annotations in this identity constraint
67
protected int fNumAnnotations;
68
69     //
70
// Constructors
71
//
72

73     /** Default constructor. */
74     protected IdentityConstraint(String JavaDoc namespace, String JavaDoc identityConstraintName, String JavaDoc elemName) {
75         fNamespace = namespace;
76         fIdentityConstraintName = identityConstraintName;
77         fElementName = elemName;
78     } // <init>(String,String)
79

80     //
81
// Public methods
82
//
83

84     /** Returns the identity constraint name. */
85     public String JavaDoc getIdentityConstraintName() {
86         return fIdentityConstraintName;
87     } // getIdentityConstraintName():String
88

89     /** Sets the selector. */
90     public void setSelector(Selector selector) {
91         fSelector = selector;
92     } // setSelector(Selector)
93

94     /** Returns the selector. */
95     public Selector getSelector() {
96         return fSelector;
97     } // getSelector():Selector
98

99     /** Adds a field. */
100     public void addField(Field field) {
101         if (fFields == null)
102             fFields = new Field[4];
103         else if (fFieldCount == fFields.length)
104             fFields = resize(fFields, fFieldCount*2);
105         fFields[fFieldCount++] = field;
106     } // addField(Field)
107

108     /** Returns the field count. */
109     public int getFieldCount() {
110         return fFieldCount;
111     } // getFieldCount():int
112

113     /** Returns the field at the specified index. */
114     public Field getFieldAt(int index) {
115         return fFields[index];
116     } // getFieldAt(int):Field
117

118     // get the name of the owning element
119
public String JavaDoc getElementName () {
120         return fElementName;
121     } // getElementName(): String
122

123     //
124
// Object methods
125
//
126

127     /** Returns a string representation of this object. */
128     public String JavaDoc toString() {
129         String JavaDoc s = super.toString();
130         int index1 = s.lastIndexOf('$');
131         if (index1 != -1) {
132             return s.substring(index1 + 1);
133         }
134         int index2 = s.lastIndexOf('.');
135         if (index2 != -1) {
136             return s.substring(index2 + 1);
137         }
138         return s;
139     } // toString():String
140

141     // equals: returns true if and only if the String
142
// representations of all members of both objects (except for
143
// the elenemtName field) are equal.
144
public boolean equals(IdentityConstraint id) {
145         boolean areEqual = fIdentityConstraintName.equals(id.fIdentityConstraintName);
146         if(!areEqual) return false;
147         areEqual = fSelector.toString().equals(id.fSelector.toString());
148         if(!areEqual) return false;
149         areEqual = (fFieldCount == id.fFieldCount);
150         if(!areEqual) return false;
151         for(int i=0; i<fFieldCount; i++)
152             if(!fFields[i].toString().equals(id.fFields[i].toString())) return false;
153         return true;
154     } // equals
155

156     static final Field[] resize(Field[] oldArray, int newSize) {
157         Field[] newArray = new Field[newSize];
158         System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
159         return newArray;
160     }
161
162     /**
163      * Get the type of the object, i.e ELEMENT_DECLARATION.
164      */

165     public short getType() {
166         return XSConstants.IDENTITY_CONSTRAINT;
167     }
168
169     /**
170      * The <code>name</code> of this <code>XSObject</code> depending on the
171      * <code>XSObject</code> type.
172      */

173     public String JavaDoc getName() {
174         return fIdentityConstraintName;
175     }
176
177     /**
178      * The namespace URI of this node, or <code>null</code> if it is
179      * unspecified. defines how a namespace URI is attached to schema
180      * components.
181      */

182     public String JavaDoc getNamespace() {
183         return fNamespace;
184     }
185
186     /**
187      * {identity-constraint category} One of key, keyref or unique.
188      */

189     public short getCategory() {
190         return type;
191     }
192
193     /**
194      * {selector} A restricted XPath ([XPath]) expression
195      */

196     public String JavaDoc getSelectorStr() {
197         return (fSelector != null) ? fSelector.toString() : null;
198     }
199
200     /**
201      * {fields} A non-empty list of restricted XPath ([XPath]) expressions.
202      */

203     public StringList getFieldStrs() {
204         String JavaDoc[] strs = new String JavaDoc[fFieldCount];
205         for (int i = 0; i < fFieldCount; i++)
206             strs[i] = fFields[i].toString();
207         return new StringListImpl(strs, fFieldCount);
208     }
209
210     /**
211      * {referenced key} Required if {identity-constraint category} is keyref,
212      * forbidden otherwise. An identity-constraint definition with
213      * {identity-constraint category} equal to key or unique.
214      */

215     public XSIDCDefinition getRefKey() {
216         return null;
217     }
218
219     /**
220      * Optional. Annotation.
221      */

222     public XSObjectList getAnnotations() {
223         return new XSObjectListImpl(fAnnotations, fNumAnnotations);
224     }
225     
226     /**
227      * @see org.apache.xerces.xs.XSObject#getNamespaceItem()
228      */

229     public XSNamespaceItem getNamespaceItem() {
230         // REVISIT: implement
231
return null;
232     }
233
234     public void addAnnotation(XSAnnotationImpl annotation) {
235         if(annotation == null)
236             return;
237         if(fAnnotations == null) {
238             fAnnotations = new XSAnnotationImpl[2];
239         } else if(fNumAnnotations == fAnnotations.length) {
240             XSAnnotationImpl[] newArray = new XSAnnotationImpl[fNumAnnotations << 1];
241             System.arraycopy(fAnnotations, 0, newArray, 0, fNumAnnotations);
242             fAnnotations = newArray;
243         }
244         fAnnotations[fNumAnnotations++] = annotation;
245     }
246
247 } // class IdentityConstraint
248
Popular Tags