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  /**
40   * Package private class to handle the case where a simple type can't be instanciated because its
41   * parent has not yet been read.
42   *
43   * @author <a href="mailto:berry@intalio.com">Arnaud Berry</a>
44   * @version $Revision:
45   **/
46  class DeferredSimpleType extends SimpleType {
47    /** SerialVersionUID */
48    private static final long serialVersionUID = 1335439479275580848L;
49  
50    /**
51     * An instance of ListType or of a class derived from AtomicType created the first time getType is
52     * called from the information contained in this simple type instance.
53     **/
54    private SimpleType instance = null;
55  
56    /**
57     * The parent's name.
58     **/
59    private String baseTypeName = null;
60  
61    /**
62     * The number of times createInstance was called (to guess when there is a cyclic reference (type
63     * A extends B; type B extends A) in the schema types) TODO: real cycle detection.
64     */
65    private int createInstanceCallsCount = 0;
66  
67    /**
68     * Creates a new default DeferredSimpleType.
69     **/
70    DeferredSimpleType() {
71      super();
72    } // -- DeferredSimpleType
73  
74    /**
75     * Creates a new DeferredSimpleType
76     *
77     * @param name the name of the SimpleType.
78     **/
79    DeferredSimpleType(String name) {
80      super();
81      setName(name);
82    } // -- DeferredSimpleType
83  
84    /**
85     * Creates a new DeferredSimpleType
86     *
87     * @param name the name of the SimpleType.
88     * @param schema the parent Schema of the SimpleType.
89     **/
90    DeferredSimpleType(String name, Schema schema) {
91      setName(name);
92      setSchema(schema);
93    } // -- DeferredSimpleType
94  
95    /**
96     * Sets the name of the base type that couldn't be resolved when this type was created.
97     */
98    public void setBaseTypeName(String baseTypeName) {
99      this.baseTypeName = baseTypeName;
100   }
101 
102   /**
103    * Package private getter of the simpleType instance.
104    */
105   XMLType getType() {
106     if (instance == null)
107       createInstance();
108     return instance;
109   }
110 
111 
112   /**
113    * Resolves the parents of this simple type in order to create the instance of the appropriate
114    * class deriving from simple type and fill it.
115    */
116   protected synchronized void createInstance() {
117     createInstanceCallsCount++;
118     if (createInstanceCallsCount >= 666) {
119       String err = "cyclic type definition involving the type: " + getName();
120       throw new IllegalStateException(err);
121     }
122 
123     // create the type, false means we don't want a DeferredSimpleType to be returned.
124     instance = Schema.getTypeFactory().createUserSimpleType(getSchema(), getName(), baseTypeName,
125         getDerivationMethod(), false);
126 
127     if (instance != null) {
128       copyFacets(instance);
129       instance.setParent(getParent());
130     }
131   }
132 
133   /**
134    * Returns Structure.UNKNOWN (This class should not be seen outside AttributeDecl and ElementDecl
135    * anyway)
136    **/
137   public short getStructureType() {
138     return Structure.UNKNOWN;
139   } // -- getStructureType
140 
141   /**
142    * Overridden to return the local copy of the base type's name we own.
143    * 
144    * @see XMLType#getBaseTypeName()
145    */
146   public String getBaseTypeName() {
147     return baseTypeName;
148   }
149 }
150 
151