View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2001 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
44   */
45  
46  
47  package org.exolab.castor.xml.schema;
48  
49  import java.util.Enumeration;
50  import java.util.Vector;
51  
52  import org.exolab.castor.xml.ValidationException;
53  import org.exolab.castor.xml.validators.ValidationUtils;
54  
55  /**
56   * The base class for the XML Schema Identity Constraints 
57   * (key, keyref, unique).
58   *
59   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
60   * @version $Revision$ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
61  **/
62  public abstract class IdentityConstraint extends Annotated {
63      
64      
65      /**
66       * Identity Constraint id
67      **/
68      private String _id   = null;
69      
70      /**
71       * Identity Constraint name
72      **/
73      private String _name = null;
74      
75      /**
76       * Identity Constraint Selector
77      **/
78      private IdentitySelector _selector = null;
79      
80      /**
81       * The fields of this Identity Constraint
82      **/
83      private Vector _fields = null;
84      
85      /**
86       * Constructor used by sub-classes. Creates a new IdentityConstraint.
87       *
88       * @param name the name for the IdentityConstraint. Must not be null.
89      **/
90      protected IdentityConstraint(String name) 
91          throws SchemaException
92      {
93          setName(name);
94          _fields = new Vector(3);
95      } //-- IdentityConstraint
96  
97      /**
98       * Adds the given IdentityField to this IdentityConstraint
99       *
100      * @param field the IdentityField to add.
101     **/
102     public void addField(IdentityField field) {
103         if (field != null)
104             _fields.addElement(field);
105     } //-- addField
106     
107     /**
108      * Returns an Enumeration of the IdentityFields contained within this
109      * IdentityConstraint. 
110      *
111      * @return an Enumeration of the IdentityField objects contain within
112      * this IdentityConstraint.
113     **/
114     public Enumeration getFields() {
115         return _fields.elements();
116     } //-- getFields
117     
118     /**
119      * Returns the Id of this IdentityConstraint, or null if no
120      * Id has been set.
121      *
122      * @return the Id of this IdentityConstraint, or null if no
123      * Id has been set.
124     **/
125     public String getId() {
126         return _id;
127     } //-- getId
128     
129     /**
130      * Returns the name of this IdentityConstraint. This value will
131      * never be null.
132      *
133      * @return the name of this IdentityConstraint
134     **/
135     public String getName() {
136         return _name;
137     } //-- getName
138 
139     /**
140      * Returns the selector of this IdentityConstraint. 
141      *
142      * @return the IdentitySelector of this IdentityConstraint
143     **/
144     public IdentitySelector getSelector() {
145         return _selector;
146     } //-- getSelector
147     
148     /**
149      * Removes the given IdentityField from this IdentityConstraint.
150      *
151      * @return true if the IdentityField was contained within this 
152      * IdentityConstraint, otherwise false.
153     **/
154     public boolean removeField(IdentityField field) {
155         return _fields.removeElement(field);
156     } //-- removeField
157     
158      
159     /**
160      * Sets the Id for this IdentityConstraint.
161      *
162      * @param id the Id for this IdentityConstraint. 
163     **/
164     public void setId(String id) {
165         _id = id;
166     } //-- setId
167     
168     /**
169      * Sets the name for this IdentityConstraint.
170      *
171      * @param name the name for this IdentityConstraint. Must not be null.
172      * @exception SchemaException if name is null.
173     **/
174     public void setName(String name) 
175         throws SchemaException
176     {
177         if (name == null) 
178             throw new SchemaException("The name of an IdentityConstraint must not be null.");
179             
180         _name = name;
181     } //-- setName
182     
183     /**
184      * Sets the selector for this IdentityConstraint.
185      *
186      * @param selector the Selector for this IdentityConstraint. Must not be 
187      * null.
188      * @exception SchemaException if selector is null.
189     **/
190     public void setSelector(IdentitySelector selector) 
191         throws SchemaException
192     {
193         if (selector == null) 
194             throw new SchemaException("The selector of an IdentityConstraint must not be null.");
195         _selector = selector;
196     } //-- setSelector
197     
198     /**
199      * Returns the type of this Schema Structure
200      * @return the type of this Schema Structure
201     **/
202     public abstract short getStructureType();
203 
204     /**
205      * Checks the validity of this Schema defintion.
206      * @exception ValidationException when this Schema definition
207      * is invalid.
208     **/
209     public void validate()
210         throws ValidationException
211     {
212         String err = null;
213         
214         //-- name must be a NCName
215         if (!ValidationUtils.isNCName(_name)) {
216             err = "The name of an IdentityConstraint must be an NCName.";
217         }
218         //-- selector must exist
219         else if (_selector == null) {
220             err = "Selector for IdentityConstraint cannot be null.";
221         }
222         //-- at least 1 (one) field must exist
223         else if (_fields.size() < 1) {
224             err = "There must be at least one 'field' in an "
225                 + "identity constraint.";
226         }
227         
228         if (err != null) throw new ValidationException(err);
229         
230     } //-- validate
231     
232 } //-- class IdentityConstraint