KickJava   Java API By Example, From Geeks To Geeks.

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


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
58 package com.sun.org.apache.xerces.internal.impl.xs.identity;
59
60 import com.sun.org.apache.xerces.internal.xs.XSIDCDefinition;
61 import com.sun.org.apache.xerces.internal.xs.StringList;
62 import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem;
63 import com.sun.org.apache.xerces.internal.xs.XSObjectList;
64 import com.sun.org.apache.xerces.internal.xs.XSConstants;
65 import com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl;
66 import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
67 import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl;
68
69 /**
70  * Base class of Schema identity constraint.
71  *
72  * @author Andy Clark, IBM
73  * @version $Id: IdentityConstraint.java,v 1.9 2003/11/11 20:14:59 sandygao Exp $
74  */

75 public abstract class IdentityConstraint implements XSIDCDefinition {
76
77     //
78
// Data
79
//
80

81     /** type */
82     protected short type;
83
84     /** target namespace */
85     protected String JavaDoc fNamespace;
86     
87     /** Identity constraint name. */
88     protected String JavaDoc fIdentityConstraintName;
89
90     /** name of owning element */
91     protected String JavaDoc fElementName;
92
93     /** Selector. */
94     protected Selector fSelector;
95
96     /** Field count. */
97     protected int fFieldCount;
98
99     /** Fields. */
100     protected Field[] fFields;
101
102     // optional annotations
103
protected XSAnnotationImpl [] fAnnotations = null;
104
105     // number of annotations in this identity constraint
106
protected int fNumAnnotations;
107
108     //
109
// Constructors
110
//
111

112     /** Default constructor. */
113     protected IdentityConstraint(String JavaDoc namespace, String JavaDoc identityConstraintName, String JavaDoc elemName) {
114         fNamespace = namespace;
115         fIdentityConstraintName = identityConstraintName;
116         fElementName = elemName;
117     } // <init>(String,String)
118

119     //
120
// Public methods
121
//
122

123     /** Returns the identity constraint name. */
124     public String JavaDoc getIdentityConstraintName() {
125         return fIdentityConstraintName;
126     } // getIdentityConstraintName():String
127

128     /** Sets the selector. */
129     public void setSelector(Selector selector) {
130         fSelector = selector;
131     } // setSelector(Selector)
132

133     /** Returns the selector. */
134     public Selector getSelector() {
135         return fSelector;
136     } // getSelector():Selector
137

138     /** Adds a field. */
139     public void addField(Field field) {
140         if (fFields == null)
141             fFields = new Field[4];
142         else if (fFieldCount == fFields.length)
143             fFields = resize(fFields, fFieldCount*2);
144         fFields[fFieldCount++] = field;
145     } // addField(Field)
146

147     /** Returns the field count. */
148     public int getFieldCount() {
149         return fFieldCount;
150     } // getFieldCount():int
151

152     /** Returns the field at the specified index. */
153     public Field getFieldAt(int index) {
154         return fFields[index];
155     } // getFieldAt(int):Field
156

157     // get the name of the owning element
158
public String JavaDoc getElementName () {
159         return fElementName;
160     } // getElementName(): String
161

162     //
163
// Object methods
164
//
165

166     /** Returns a string representation of this object. */
167     public String JavaDoc toString() {
168         String JavaDoc s = super.toString();
169         int index1 = s.lastIndexOf('$');
170         if (index1 != -1) {
171             return s.substring(index1 + 1);
172         }
173         int index2 = s.lastIndexOf('.');
174         if (index2 != -1) {
175             return s.substring(index2 + 1);
176         }
177         return s;
178     } // toString():String
179

180     // equals: returns true if and only if the String
181
// representations of all members of both objects (except for
182
// the elenemtName field) are equal.
183
public boolean equals(IdentityConstraint id) {
184         boolean areEqual = fIdentityConstraintName.equals(id.fIdentityConstraintName);
185         if(!areEqual) return false;
186         areEqual = fSelector.toString().equals(id.fSelector.toString());
187         if(!areEqual) return false;
188         areEqual = (fFieldCount == id.fFieldCount);
189         if(!areEqual) return false;
190         for(int i=0; i<fFieldCount; i++)
191             if(!fFields[i].toString().equals(id.fFields[i].toString())) return false;
192         return true;
193     } // equals
194

195     static final Field[] resize(Field[] oldArray, int newSize) {
196         Field[] newArray = new Field[newSize];
197         System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
198         return newArray;
199     }
200
201     /**
202      * Get the type of the object, i.e ELEMENT_DECLARATION.
203      */

204     public short getType() {
205         return XSConstants.IDENTITY_CONSTRAINT;
206     }
207
208     /**
209      * The <code>name</code> of this <code>XSObject</code> depending on the
210      * <code>XSObject</code> type.
211      */

212     public String JavaDoc getName() {
213         return fIdentityConstraintName;
214     }
215
216     /**
217      * The namespace URI of this node, or <code>null</code> if it is
218      * unspecified. defines how a namespace URI is attached to schema
219      * components.
220      */

221     public String JavaDoc getNamespace() {
222         return fNamespace;
223     }
224
225     /**
226      * {identity-constraint category} One of key, keyref or unique.
227      */

228     public short getCategory() {
229         return type;
230     }
231
232     /**
233      * {selector} A restricted XPath ([XPath]) expression
234      */

235     public String JavaDoc getSelectorStr() {
236         return fSelector.toString();
237     }
238
239     /**
240      * {fields} A non-empty list of restricted XPath ([XPath]) expressions.
241      */

242     public StringList getFieldStrs() {
243         String JavaDoc[] strs = new String JavaDoc[fFieldCount];
244         for (int i = 0; i < fFieldCount; i++)
245             strs[i] = fFields[i].toString();
246         return new StringListImpl(strs, fFieldCount);
247     }
248
249     /**
250      * {referenced key} Required if {identity-constraint category} is keyref,
251      * forbidden otherwise. An identity-constraint definition with
252      * {identity-constraint category} equal to key or unique.
253      */

254     public XSIDCDefinition getRefKey() {
255         return null;
256     }
257
258     /**
259      * Optional. Annotation.
260      */

261     public XSObjectList getAnnotations() {
262         return new XSObjectListImpl(fAnnotations, fNumAnnotations);
263     }
264     
265     /**
266      * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
267      */

268     public XSNamespaceItem getNamespaceItem() {
269         // REVISIT: implement
270
return null;
271     }
272
273     public void addAnnotation(XSAnnotationImpl annotation) {
274         if(annotation == null)
275             return;
276         if(fAnnotations == null) {
277             fAnnotations = new XSAnnotationImpl[2];
278         } else if(fNumAnnotations == fAnnotations.length) {
279             XSAnnotationImpl[] newArray = new XSAnnotationImpl[fNumAnnotations << 1];
280             System.arraycopy(fAnnotations, 0, newArray, 0, fNumAnnotations);
281             fAnnotations = newArray;
282         }
283         fAnnotations[fNumAnnotations++] = annotation;
284     }
285
286 } // class IdentityConstraint
287
Popular Tags