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 package org.exolab.castor.xml.schema; 47 48 import java.util.Enumeration; 49 import java.util.Vector; 50 51 /** 52 * A class that represents the XML Schema Union simple-type. 53 * 54 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a> 55 * @version $Revision$ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $ 56 **/ 57 public class Union extends SimpleType { 58 /** SerialVersionUID */ 59 private static final long serialVersionUID = -1389185884142578168L; 60 61 /** 62 * The local annotation for this Union 63 **/ 64 private Annotation _annotation = null; 65 66 /** 67 * The id attribute for this Union 68 **/ 69 private String _id = null; 70 71 /** 72 * The simpleType members of this Union 73 **/ 74 private Vector _simpleTypes = null; 75 76 private boolean _hasReferencedTypes = false; 77 78 /** 79 * Creates a new Union type. 80 * 81 * @param schema the Schema for this Union (Cannot be null) 82 **/ 83 public Union(Schema schema) 84 throws SchemaException 85 { 86 super(); 87 88 if (schema == null) { 89 String err = "The Schema argument to the constructor of Union "+ 90 "may not be null."; 91 throw new IllegalArgumentException(err); 92 } 93 super.setSchema(schema); 94 //-- No sense in having a union of only one (1), but 95 //-- we choose a low number, like two (2) or three (3) 96 //-- since most unions will only have at most a 97 //-- few members. 98 _simpleTypes = new Vector(3); 99 } //-- Union 100 101 /** 102 * Adds the given SimpleType reference as a member of this 103 * Union. An exception will be thrown during a call to 104 * #getMemberTypes if this reference cannot be resolved. 105 * 106 * @param typeName the name of the SimpleType to add. 107 **/ 108 public void addMemberType(String typeName) { 109 if (typeName == null) return; 110 addMemberType(new SimpleTypeReference(getSchema(), typeName)); 111 } //-- addMemberType 112 113 /** 114 * Adds the given SimpleType as a member of this Union 115 * 116 * @param simpleType the SimpleType to add to this Union. 117 **/ 118 public void addMemberType(SimpleType simpleType) { 119 if (simpleType == null) return; 120 121 if (simpleType instanceof SimpleTypeReference) { 122 if (simpleType.getType() != null) { 123 simpleType = (SimpleType)simpleType.getType(); 124 } 125 else _hasReferencedTypes = true; 126 } 127 _simpleTypes.add(simpleType); 128 } //-- addMemberType 129 130 /** 131 * Returns the id for this Union, or null if no id has been set. 132 * 133 * @return the id for this Union, or null if no id has been set.. 134 **/ 135 public String getId() { 136 return _id; 137 } //-- getId 138 139 /** 140 * Returns the annotation which appears local to this Union, or 141 * null if no local annotation has been set. 142 * 143 * @return the annotation which is local to this Union. 144 **/ 145 public Annotation getLocalAnnotation() { 146 return _annotation; 147 } //-- getLocalAnnotation 148 149 /** 150 * Returns an Enumeration of all the SimpleTypes that are members of 151 * this Union. 152 * 153 * @return an Enumeration of all member SimpleTypes. 154 **/ 155 public Enumeration getMemberTypes() { 156 //-- clear any referenced types (if necessary) 157 if (_hasReferencedTypes) { 158 _hasReferencedTypes = false; 159 for (int i = 0; i < _simpleTypes.size(); i++) { 160 Object obj = _simpleTypes.elementAt(i); 161 if (obj instanceof SimpleTypeReference) { 162 SimpleType simpleType = (SimpleType)obj; 163 if (simpleType.getType() != null) { 164 _simpleTypes.setElementAt(simpleType.getType(), i); 165 } 166 else { 167 //-- XXXX 168 //-- we should really throw an exception here 169 //-- but for now...just re-mark as having 170 //-- an unresolved referred type. 171 _hasReferencedTypes = true; 172 } 173 } 174 } 175 } 176 return _simpleTypes.elements(); 177 } //-- getMemberTypes; 178 179 /** 180 * Returns the type of this Schema Structure 181 * @return the type of this Schema Structure 182 **/ 183 public short getStructureType() { 184 return Structure.UNION; 185 } //-- getStructureType 186 187 /** 188 * Sets the Schema for this Union. This method overloads the 189 * SimpleType#setSchema method to prevent the Schema from being 190 * changed. 191 * 192 * @param schema the schema that this Union belongs to. 193 **/ 194 public void setSchema(Schema schema) { 195 if (schema != getSchema()) { 196 String err = "The Schema of an Union cannot be changed."; 197 throw new IllegalStateException(err); 198 } 199 } //-- void setSchema 200 201 /** 202 * Sets the id for this Union. 203 * 204 * @param id the unique id for this Union. Must be globally unique 205 * within the scope of the Schema. 206 **/ 207 public void setId(String id) { 208 _id = id; 209 } //-- setId 210 211 /** 212 * Sets an annotation which is local to this Union. 213 * 214 * @param annotation the local annotation to set for this Union. 215 **/ 216 public void setLocalAnnotation(Annotation annotation) { 217 _annotation = annotation; 218 } //-- setLocalAnnotation 219 220 } //-- class: Union