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  import org.exolab.castor.xml.ValidationException;
39  
40  /**
41   * Package private class to handles type references.
42   *
43   * @author <a href="mailto:berry@intalio.com">Arnaud Berry</a>
44   * @version $Revision:
45   **/
46  public class TypeReference extends XMLType {
47    /** SerialVersionUID */
48    private static final long serialVersionUID = -8707313918028332092L;
49  
50    /**
51     * The referred type (stored in the schema)
52     */
53    private XMLType referredType = null;
54  
55    /**
56     * Returns the referred type
57     */
58    XMLType getType() {
59      if (referredType == null)
60        resolveTypeReference();
61  
62      return referredType;
63    }
64  
65    /**
66     * Resolves the type reference.
67     */
68    void resolveTypeReference() {
69  
70      String name = getName();
71      if (name == null)
72        return;
73  
74  
75      Schema schema = getSchema();
76      if (schema == null) {
77        // -- ummm we have a problem.
78        String error = "Schema is null. Reference  cannot be resolved.";
79        throw new IllegalStateException(error);
80      }
81  
82      // -- is AnyType? resolve namespace if necessary
83      // -- and check for 'anyType'.
84      String canonicalName = name;
85      String nsPrefix = "";
86      int colon = name.indexOf(':');
87      if (colon >= 0) {
88        canonicalName = name.substring(colon + 1);
89        nsPrefix = name.substring(0, colon);
90      }
91      String ns = schema.getNamespace(nsPrefix);
92  
93      if (schema.getSchemaNamespace().equals(ns)) {
94        if (canonicalName.equals(SchemaNames.ANYTYPE)) {
95          referredType = new AnyType(schema);
96          return;
97        }
98      }
99  
100 
101     // -- check simpletype first since it has higher precedence
102     IllegalArgumentException exception = null;
103     try {
104       referredType = getSchema().getSimpleType(getName());
105     } catch (IllegalArgumentException iax) {
106       exception = iax;
107     }
108     if (referredType != null) {
109       return;
110     }
111 
112     // -- try to find a complex type
113     referredType = getSchema().getComplexType(getName());
114 
115     if (referredType != null) {
116       return;
117     }
118 
119     // -- rethrow exception if necessary
120     if (exception != null)
121       throw exception;
122 
123   } // - resolveTypeReference
124 
125   /**
126    * Sets the parent for this Schema type
127    *
128    * @param parent the parent Structure for SchemaType
129    **/
130   protected void setParent(Structure parent) {
131     // -- never used by references
132   } // -- setParent
133 
134   /**
135    * Returns Structure.UNKNOWN (This class should not be seen outside ElementDecl anyway)
136    **/
137   public short getStructureType() {
138     return Structure.UNKNOWN;
139   } // -- getStructureType
140 
141   /**
142    * Checks the validity of this type defintion.
143    *
144    * @throws ValidationException when this type definition is invalid.
145    **/
146   public void validate() throws ValidationException {
147     // -- Do nothing
148 
149   } // -- validate
150 
151 }
152 
153