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 1999-2003 (C) Intalio Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  
36  package org.exolab.castor.xml.schema.simpletypes;
37  
38  import org.exolab.castor.xml.schema.Annotation;
39  import org.exolab.castor.xml.schema.SimpleType;
40  import org.exolab.castor.xml.schema.Schema;
41  import org.exolab.castor.xml.schema.SchemaException;
42  import org.exolab.castor.xml.schema.Structure;
43  
44  /**
45   * Represents a SimpleType that is a "list" of a given SimpleType.
46   *
47   * @author <a href="mailto:berry@intalio.com">Arnaud Berry</a>
48   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
49   * @version $Revision$ $Id$
50   **/
51  public class ListType extends SimpleType {
52    /** SerialVersionUID */
53    private static final long serialVersionUID = 5907869367277661845L;
54  
55    /** The local annotation for this ListType. */
56    private Annotation _annotation = null;
57  
58    /** The SimpleType the list is based on. */
59    private SimpleType _itemType = null;
60  
61    private boolean _hasReference = false;
62  
63    /**
64     * Creates a new ListType.
65     *
66     * @param schema the Schema for this ListType (Cannot be null)
67     */
68    public ListType(final Schema schema) throws SchemaException {
69      super();
70  
71      if (schema == null) {
72        String err = "The Schema argument to the constructor of ListType " + "may not be null.";
73        throw new IllegalArgumentException(err);
74      }
75      super.setSchema(schema);
76    } // -- ListType
77  
78    /**
79     * Returns the simpleType for the items of this ListType.
80     *
81     * @return the simpleType for the items of this ListType.
82     */
83    public SimpleType getItemType() {
84      if (_hasReference) {
85        SimpleType simpleType = resolveReference(_itemType);
86        if (simpleType == null) {
87          String err = "Unable to resolve type: " + _itemType.getName();
88          throw new IllegalStateException(err);
89        }
90        _hasReference = false;
91        _itemType = simpleType;
92      }
93      return _itemType;
94    } // -- getItemType
95  
96    /**
97     * Returns the annotation which appears local to this Union, or null if no local annotation has
98     * been set.
99     *
100    * @return the annotation which is local to this Union.
101    */
102   public Annotation getLocalAnnotation() {
103     return _annotation;
104   } // -- getLocalAnnotation
105 
106   /**
107    * Returns the type of this Schema Structure
108    * 
109    * @return the type of this Schema Structure
110    */
111   public short getStructureType() {
112     return Structure.LIST;
113   } // -- getStructureType
114 
115   /**
116    * Sets the SimpleType for this ListType (the type of item that instances of this list holds).
117    *
118    * @param type the SimpleType for this ListType.
119    */
120   public void setItemType(SimpleType type) {
121     _itemType = type;
122     _hasReference = false;
123     // -- If no base type is set, then constructing a derivation by
124     // -- restriction of this list will be impossible since
125     // -- SimpleTypeFactory will not like it and throw a
126     // -- 'schema.noBuiltInParent' error, so we set the base
127     // -- type to be the same as the list type
128     setBaseType(_itemType);
129   } // -- setItemType
130 
131   /**
132    * Sets the SimpleType for this ListType (the type of item that instances of this list holds).
133    *
134    * @param typeName the name of the SimpleType for this ListType.
135    */
136   public void setItemType(String typeName) {
137     if (typeName == null) {
138       _itemType = null;
139       _hasReference = false;
140     } else {
141       _itemType = createReference(typeName);
142       _hasReference = true;
143     }
144     // -- Make sure we set baseType to support derivations
145     // -- of this list
146     setBaseType(_itemType);
147   } // -- setItemType
148 
149   /**
150    * Sets an annotation which is local to this Union.
151    *
152    * @param annotation the local annotation to set for this Union.
153    */
154   public void setLocalAnnotation(Annotation annotation) {
155     _annotation = annotation;
156   } // -- setLocalAnnotation
157 
158   /**
159    * Sets the Schema for this Union. This method overloads the SimpleType#setSchema method to
160    * prevent the Schema from being changed.
161    *
162    * @param schema the schema that this Union belongs to.
163    */
164   public void setSchema(Schema schema) {
165     if (schema != getSchema()) {
166       String err = "The Schema of an Union cannot be changed.";
167       throw new IllegalStateException(err);
168     }
169   } // -- void setSchema
170 } // -- ListType
171 
172