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 2000-2004 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  package org.exolab.castor.xml.descriptors;
36  
37  import java.util.Enumeration;
38  import java.util.Locale;
39  import java.util.Vector;
40  
41  import org.exolab.castor.xml.XMLClassDescriptor;
42  
43  /**
44   * The default set of built-in ClassDescriptors. A static utility class.
45   *
46   * @author <a href="mailto:kvisco-at-intalio.com">Keith Visco</a>
47   * @version $Revision$ $Date: 2004-11-04 15:55:27 -0700 (Thu, 04 Nov 2004) $
48   */
49  public final class CoreDescriptors {
50  
51    /** Fully-qualified name of the List class from Java 1.2. */
52    private static final String LIST_CLASS_NAME = "java.util.List";
53    /** Fully-qualified name of our List Class Descriptor. */
54    private static final String LIST_DESCRIPTOR_NAME =
55        "org.exolab.castor.xml.descriptors.ListClassDescriptor";
56    /** If not null, the Java 1.2 or later list class. */
57    private static final Class LIST_CLASS = getListClass();
58  
59    /** The Date ClassDescriptor. */
60    private static final XMLClassDescriptor DATE_DESCRIPTOR = new DateClassDescriptor();
61    /**
62     * The java.util.Enumeration ClassDescriptor, really only useful for marshaling since Enumerations
63     * are read-only.
64     */
65    private static final XMLClassDescriptor ENUMERATION_DESCRIPTOR = new EnumerationDescriptor();
66    /** The List ClassDescriptor (only loaded for JDK 1.2+). */
67    private static final XMLClassDescriptor LIST_DESCRIPTOR = getListClassDescriptor();
68    /** The java.util.Locale ClassDescriptor. */
69    private static final XMLClassDescriptor LOCALE_DESCRIPTOR = new LocaleDescriptor();
70    /** The java.sql.Date ClassDescriptor. */
71    private static final XMLClassDescriptor SQL_DATE_DESCRIPTOR = new SQLDateClassDescriptor();
72    /** The java.sql.Time ClassDescriptor. */
73    private static final XMLClassDescriptor SQL_TIME_DESCRIPTOR = new SQLTimeClassDescriptor();
74    /** The java.sql.Timestamp ClassDescriptor. */
75    private static final XMLClassDescriptor SQL_TIMESTAMP_DESCRIPTOR =
76        new SQLTimestampClassDescriptor();
77    /** The String ClassDescriptor. */
78    private static final XMLClassDescriptor STRING_DESCRIPTOR = new StringClassDescriptor();
79    /** The Vector ClassDescriptor. */
80    private static final XMLClassDescriptor VECTOR_DESCRIPTOR = new VectorClassDescriptor();
81  
82    /**
83     * Default constructor. Intentionally private.
84     */
85    private CoreDescriptors() {
86      super();
87    } // -- CoreDescriptors
88  
89    /**
90     * Returns the XMLClassDescriptor for the given Class. This method will return null if there is no
91     * built-in XMLClassDescriptor.
92     *
93     * @param clazz the Class to return the XMLClassDescriptor for.
94     * @return the XMLClassDescriptor for the given class, or null.
95     */
96    public static XMLClassDescriptor getDescriptor(final Class clazz) {
97      if (clazz == null) {
98        return null;
99      }
100 
101     if (clazz == String.class) {
102       return STRING_DESCRIPTOR;
103     }
104 
105     if (clazz == java.util.Date.class) {
106       return DATE_DESCRIPTOR;
107     }
108 
109     if (Enumeration.class.isAssignableFrom(clazz)) {
110       return ENUMERATION_DESCRIPTOR;
111     }
112 
113     if (clazz == Vector.class || Vector.class.isAssignableFrom(clazz)) {
114       return VECTOR_DESCRIPTOR;
115     }
116 
117     // -- JDK 1.2
118     if (LIST_DESCRIPTOR != null && (LIST_CLASS == clazz || LIST_CLASS.isAssignableFrom(clazz))) {
119       return LIST_DESCRIPTOR;
120     }
121 
122     if (clazz == Locale.class) {
123       return LOCALE_DESCRIPTOR;
124     }
125 
126     // -- java.sql Date/Time classes
127     if (clazz == java.sql.Date.class) {
128       return SQL_DATE_DESCRIPTOR;
129     }
130 
131     if (clazz == java.sql.Time.class) {
132       return SQL_TIME_DESCRIPTOR;
133     }
134 
135     if (clazz == java.sql.Timestamp.class) {
136       return SQL_TIMESTAMP_DESCRIPTOR;
137     }
138 
139     return null;
140   } // -- getDescriptor
141 
142   /**
143    * Loads and returns the list class class descriptor. Used during static initialization.
144    *
145    * @return the list class class descriptor.
146    */
147   private static XMLClassDescriptor getListClassDescriptor() {
148     // If null, then not JDK 1.2 or greater
149     if (LIST_CLASS == null) {
150       return null;
151     }
152 
153     ClassLoader loader = CoreDescriptors.class.getClassLoader();
154     Class descriptorClass = null;
155     try {
156       if (loader == null) {
157         descriptorClass = Class.forName(LIST_DESCRIPTOR_NAME);
158       } else {
159         descriptorClass = loader.loadClass(LIST_DESCRIPTOR_NAME);
160       }
161     } catch (ClassNotFoundException cnfe) {
162       // -- do nothing...handled below
163     }
164 
165     XMLClassDescriptor listDescriptor = null;
166     if (descriptorClass != null) {
167       try {
168         listDescriptor = (XMLClassDescriptor) descriptorClass.newInstance();
169       } catch (InstantiationException ie) {
170         // -- just ignore
171       } catch (IllegalAccessException iae) {
172         // -- just ignore
173       }
174     } else {
175       listDescriptor = null;
176     }
177     return listDescriptor;
178   }
179 
180   /**
181    * Looks for the JDK 1.2 List class Class object and return it. Used during static initialization.
182    *
183    * @return the Class object for the JDK 1.2 List class.
184    */
185   private static Class getListClass() {
186     Class listClass = null;
187 
188     // Look for JDK 1.2 Collections
189     ClassLoader loader = null;
190     try {
191       loader = java.util.Vector.class.getClassLoader();
192       if (loader == null) {
193         // probably JDK 1.1 if loader is null, but we can double-check anyway
194         listClass = Class.forName(LIST_CLASS_NAME);
195       } else {
196         listClass = loader.loadClass(LIST_CLASS_NAME);
197       }
198     } catch (ClassNotFoundException cnfe) {
199       // -- do nothing...handled below
200     }
201 
202     return listClass;
203   }
204 
205 } // -- CoreDescriptors