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