/ojc-core/component-common/xsdmodel/src/org/exolab/castor/xml/schema/IdentityConstraint.java

https://bitbucket.org/pymma/openesb-components · Java · 272 lines · 91 code · 37 blank · 144 comment · 15 complexity · 60ec0b6daa292a6b641e1c37240dded6 MD5 · raw file

  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. *
  44. */
  45. package org.exolab.castor.xml.schema;
  46. import org.exolab.castor.xml.*;
  47. import org.exolab.castor.xml.validators.ValidationUtils;
  48. import java.util.Enumeration;
  49. import java.util.Vector;
  50. import java.util.Hashtable;
  51. /**
  52. * The base class for the XML Schema Identity Constraints
  53. * (key, keyref, unique).
  54. *
  55. * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
  56. * @version
  57. **/
  58. public abstract class IdentityConstraint extends Annotated {
  59. //----------------/
  60. //Attributes
  61. //---------------/
  62. public static final String ATTR_ID = "id";
  63. public static final String ATTR_NAME = "xpath";
  64. public static final String ATTR_SELECTOR = "selector";
  65. /**
  66. * Identity Constraint id
  67. **/
  68. private String _id = null;
  69. /**
  70. * Identity Constraint name
  71. **/
  72. private String _name = null;
  73. /**
  74. * Identity Constraint Selector
  75. **/
  76. private IdentitySelector _selector = null;
  77. /**
  78. * The fields of this Identity Constraint
  79. **/
  80. private Vector _fields = null;
  81. /**
  82. * Constructor used by sub-classes. Creates a new IdentityConstraint.
  83. *
  84. * @param name, the name for the IdentityConstraint. Must not be null.
  85. **/
  86. protected IdentityConstraint(String name)
  87. throws SchemaException
  88. {
  89. setName(name);
  90. _fields = new Vector(3);
  91. } //-- IdentityConstraint
  92. /**
  93. * Adds the given IdentityField to this IdentityConstraint
  94. *
  95. * @param field the IdentityField to add.
  96. **/
  97. public void addField(IdentityField field) {
  98. if (field != null) {
  99. _fields.addElement(field);
  100. //set parent
  101. field.setParent(this);
  102. //fire event
  103. fireStructureAdded(field);
  104. }
  105. } //-- addField
  106. /**
  107. * Returns an Enumeration of the IdentityFields contained within this
  108. * IdentityConstraint.
  109. *
  110. * @return an Enumeration of the IdentityField objects contain within
  111. * this IdentityConstraint.
  112. **/
  113. public Enumeration getFields() {
  114. return _fields.elements();
  115. } //-- getFields
  116. /**
  117. * Returns the Id of this IdentityConstraint, or null if no
  118. * Id has been set.
  119. *
  120. * @return the Id of this IdentityConstraint, or null if no
  121. * Id has been set.
  122. **/
  123. public String getId() {
  124. return _id;
  125. } //-- getId
  126. /**
  127. * Returns the name of this IdentityConstraint. This value will
  128. * never be null.
  129. *
  130. * @return the name of this IdentityConstraint
  131. **/
  132. public String getName() {
  133. return _name;
  134. } //-- getName
  135. /**
  136. * Returns the selector of this IdentityConstraint.
  137. *
  138. * @return the IdentitySelector of this IdentityConstraint
  139. **/
  140. public IdentitySelector getSelector() {
  141. return _selector;
  142. } //-- getSelector
  143. /**
  144. * Removes the given IdentityField from this IdentityConstraint.
  145. *
  146. * @return true if the IdentityField was contained within this
  147. * IdentityConstraint, otherwise false.
  148. **/
  149. public boolean removeField(IdentityField field) {
  150. boolean removed = false;
  151. if(_fields.contains(field)) {
  152. removed = true;
  153. _fields.removeElement(field);
  154. field.setParent(null);
  155. //fire event
  156. fireStructureRemoved(field);
  157. }
  158. return removed;
  159. } //-- removeField
  160. /**
  161. * Sets the Id for this IdentityConstraint.
  162. *
  163. * @param id the Id for this IdentityConstraint.
  164. **/
  165. public void setId(String id) {
  166. String oldValue = _id;
  167. _id = id;
  168. //fire event
  169. fireAttributeEvent(ATTR_ID, oldValue, id);
  170. } //-- setId
  171. /**
  172. * Sets the name for this IdentityConstraint.
  173. *
  174. * @param name the name for this IdentityConstraint. Must not be null.
  175. * @exception SchemaException if name is null.
  176. **/
  177. public void setName(String name)
  178. throws SchemaException
  179. {
  180. if (name == null)
  181. throw new SchemaException("The name of an IdentityConstraint must not be null.");
  182. String oldValue = _name;
  183. _name = name;
  184. //fire event
  185. fireAttributeEvent(ATTR_NAME, oldValue, name);
  186. } //-- setName
  187. /**
  188. * Sets the selector for this IdentityConstraint.
  189. *
  190. * @param selector the Selector for this IdentityConstraint. Must not be
  191. * null.
  192. * @exception SchemaException if selector is null.
  193. **/
  194. public void setSelector(IdentitySelector selector)
  195. throws SchemaException
  196. {
  197. if (selector == null)
  198. throw new SchemaException("The selector of an IdentityConstraint must not be null.");
  199. IdentitySelector oldValue = _selector;
  200. _selector = selector;
  201. //set parent
  202. _selector.setParent(selector);
  203. //fire event
  204. fireAttributeEvent(ATTR_SELECTOR, oldValue, selector);
  205. } //-- setSelector
  206. /**
  207. * Returns the type of this Schema Structure
  208. * @return the type of this Schema Structure
  209. **/
  210. public abstract short getStructureType();
  211. /**
  212. * Checks the validity of this Schema defintion.
  213. * @exception ValidationException when this Schema definition
  214. * is invalid.
  215. **/
  216. public void validate()
  217. throws ValidationException
  218. {
  219. String err = null;
  220. //-- name must be a NCName
  221. if (!ValidationUtils.isNCName(_name)) {
  222. err = "The name of an IdentityConstraint must be an NCName.";
  223. }
  224. //-- selector must exist
  225. else if (_selector == null) {
  226. err = "Selector for IdentityConstraint cannot be null.";
  227. }
  228. //-- at least 1 (one) field must exist
  229. else if (_fields.size() < 1) {
  230. err = "There must be at least one 'field' in an "
  231. + "identity constraint.";
  232. }
  233. if (err != null) throw new ValidationException(err);
  234. } //-- validate
  235. } //-- class IdentityConstraint