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-2000 (C) Intalio Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  
36  package org.exolab.castor.xml.schema;
37  
38  /**
39   * The base type for XML Schema types, such as complex types and simple types. <BR />
40   * 
41   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
42   * @version $Revision$ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
43   **/
44  public abstract class XMLType extends Annotated {
45    /**
46     * Error message for a null argument
47     */
48    protected static String NULL_ARGUMENT =
49        "A null argument was passed to " + XMLType.class.getName();
50  
51    /**
52     * The ID of this datatype
53     **/
54    private String _id;
55  
56    /**
57     * The name of this type
58     */
59    private String name;
60  
61    /**
62     * The base datatype reference
63     **/
64    private XMLType baseType;
65  
66    /**
67     * The owning Schema to which this type belongs
68     **/
69    private Schema schema;
70  
71    /**
72     * The name of the derivation method (if any)
73     */
74    private String derivationMethod;
75  
76    /**
77     * Default constructor
78     **/
79    public XMLType() {
80      super();
81    } // -- XMLType
82  
83    /**
84     * Returns the Id for this ComplexType, or null if no Id has been set.
85     * 
86     * @return the Id for this ComplexType, or null if no Id has been set.
87     **/
88    public String getId() {
89      return _id;
90    } // -- getId
91  
92    /**
93     * Returns the name of this type (null if none was defined)
94     * 
95     * @return the name of this type (null if none was defined)
96     **/
97    public String getName() {
98      return name;
99    }
100 
101   /**
102    * Sets the name of this type
103    * 
104    * @param name of the type
105    **/
106   public synchronized void setName(String name) {
107     this.name = name;
108 
109     // -- Patch to handle name changes....we should
110     // -- change this in the future to something
111     // -- less heavy.
112     if (schema != null) {
113       try {
114         if (isComplexType()) {
115           if (schema.removeComplexType((ComplexType) this)) {
116             schema.addComplexType((ComplexType) this);
117           }
118         } else if (isSimpleType()) {
119           if (schema.removeSimpleType((SimpleType) this)) {
120             schema.addSimpleType((SimpleType) this);
121           }
122         }
123       } catch (SchemaException ex) {
124         // -- If this is ever thrown then we've
125         // -- had some nasty synchronization error! :-(
126         throw new IllegalStateException(ex.toString());
127       }
128     }
129     // -- end name-change patch
130 
131   } // -- setName
132 
133   /**
134    * Returns true if this XMLType is an AnyType
135    * 
136    * @return true if this XMLType is an AnyType
137    **/
138   public final boolean isAnyType() {
139     return (getStructureType() == Structure.ANYTYPE);
140   } // -- isComplexType
141 
142   /**
143    * Returns true if this XMLType is a ComplexType
144    * 
145    * @return true if this XMLType is a ComplexType
146    **/
147   public final boolean isComplexType() {
148     return (getStructureType() == Structure.COMPLEX_TYPE);
149   } // -- isComplexType
150 
151   /**
152    * Returns true if this XMLType is a SimpleType
153    * 
154    * @return true if this XMLType is a SimpleType
155    **/
156   public final boolean isSimpleType() {
157     return ((getStructureType() == Structure.SIMPLE_TYPE) || (getStructureType() == Structure.UNION)
158         || (getStructureType() == Structure.LIST));
159   } // -- isSimpleType
160 
161   /**
162    * Returns the schema to which this type belongs
163    * 
164    * @return the Schema to which this type belongs
165    **/
166   public Schema getSchema() {
167     return schema;
168   } // -- getSchema
169 
170   /**
171    * Sets the name of this SimpleType
172    * 
173    * @param schema the Schema to which this Simpletype belongs
174    **/
175   public void setSchema(Schema schema) {
176     if (schema == null) {
177       String err = NULL_ARGUMENT + "; 'schema' must not be null.";
178       throw new IllegalArgumentException(err);
179     }
180     this.schema = schema;
181   }
182 
183   /**
184    * Returns the base type that this type inherits from. If this type is a Simpletype that is a
185    * built in primitive type then null is returned.
186    * 
187    * @return the parent type.
188    **/
189   public XMLType getBaseType() {
190     return baseType;
191   } // -- getBaseType
192 
193   /**
194    * Sets the base type for this datatype
195    * 
196    * @param baseType the base type which this datatype inherits from
197    **/
198   public void setBaseType(XMLType baseType) {
199     this.baseType = baseType;
200   } // -- setBaseType
201 
202   /**
203    * Gets the name of the derivation method used to derive this type from its parent. null for
204    * primitive types.
205    */
206   public String getDerivationMethod() {
207     return derivationMethod;
208   }
209 
210   /**
211    * Sets the derivation method name
212    */
213   public void setDerivationMethod(String derivationMethod) {
214     this.derivationMethod = derivationMethod;
215   }
216 
217   /**
218    * Sets the Id for this XMLType. The Id must be globally unique within the Schema. Use a null
219    * value to remove the Id.
220    * 
221    * @param id the unique Id for this XMLType
222    **/
223   public void setId(String id) {
224     _id = id;
225   } // -- setId
226 
227   /**
228    * Sets the parent for this XMLType
229    * 
230    * @param parent the parent Structure for this XMLType
231    **/
232   protected abstract void setParent(Structure parent);
233 
234   /**
235    * Returns the type this type "really" represents ("this" in most cases), provides the indirection
236    * needed by references and forward declarations.
237    * 
238    * @return the type this type "really" represents
239    */
240   XMLType getType() {
241     return this;
242   }
243 
244   /**
245    * If this type has a base type, this returns its name.<br/>
246    * Returns null otherwise.
247    * 
248    * @return Base type's name if available, null otherwise.
249    */
250   public String getBaseTypeName() {
251     return baseType != null ? baseType.getName() : null;
252   }
253 } // -- XMLType