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