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 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  package org.exolab.castor.xml.descriptors;
36  
37  import java.util.Date;
38  
39  import org.exolab.castor.mapping.AccessMode;
40  import org.exolab.castor.mapping.ClassDescriptor;
41  import org.exolab.castor.mapping.FieldDescriptor;
42  import org.exolab.castor.xml.NodeType;
43  import org.exolab.castor.xml.TypeValidator;
44  import org.exolab.castor.xml.XMLFieldDescriptor;
45  import org.exolab.castor.xml.XMLFieldHandler;
46  import org.exolab.castor.xml.handlers.DateFieldHandler;
47  import org.exolab.castor.xml.util.XMLFieldDescriptorImpl;
48  
49  /**
50   * A ClassDescriptor for java.util.Date.
51   *
52   * @author <a href="kvisco-at-intalio.com">Keith Visco</a>
53   * @version $Revision$ $Date: 2004-12-16 22:49:25 -0700 (Thu, 16 Dec 2004) $
54   */
55  public class DateClassDescriptor extends BaseDescriptor {
56  
57    /** Used for returning no attribute and no element fields. */
58    private static final XMLFieldDescriptor[] NO_FIELDS = new XMLFieldDescriptor[0];
59    /** The name of the XML element. */
60    private static final String XML_NAME = "date";
61    /** Our field descriptor. */
62    private static final XMLFieldDescriptorImpl CONTENT_DESCRIPTOR;
63    /** Our field descriptor array. Lists the fields we describe. */
64    private static final FieldDescriptor[] FIELDS;
65    /** The TypeValidator to use for validation of the described class. */
66    private static final TypeValidator VALIDATOR = null;
67  
68    static {
69      CONTENT_DESCRIPTOR =
70          new XMLFieldDescriptorImpl(String.class, "content", "content", NodeType.Text);
71  
72      CONTENT_DESCRIPTOR.setImmutable(true);
73      CONTENT_DESCRIPTOR.setHandler(new DateFieldHandler(new XMLFieldHandler() {
74  
75        /**
76         * {@inheritDoc}
77         */
78        public Object getValue(final Object object) throws IllegalStateException {
79          return object;
80        }
81  
82        /**
83         * {@inheritDoc}
84         */
85        public void setValue(final Object object, final Object value)
86            throws IllegalStateException, IllegalArgumentException {
87          if (object.getClass() == java.util.Date.class) {
88            Date target = (Date) object;
89            if (value.getClass() == java.util.Date.class) {
90              target.setTime(((Date) value).getTime());
91            }
92          }
93        }
94  
95        /**
96         * {@inheritDoc}
97         */
98        public Object newInstance(final Object parent) {
99          return null;
100       }
101     }));
102 
103     FIELDS = new FieldDescriptor[1];
104     FIELDS[0] = CONTENT_DESCRIPTOR;
105   }
106 
107   // ----------------/
108   // - Constructors -/
109   // ----------------/
110 
111   /**
112    * No-arg constructor.
113    */
114   public DateClassDescriptor() {
115     super();
116   } // -- DateDescriptor
117 
118   // ------------------/
119   // - Public Methods -/
120   // ------------------/
121 
122   /**
123    * Returns the set of XMLFieldDescriptors for all members that should be marshaled as XML
124    * attributes.
125    *
126    * @return an array of XMLFieldDescriptors for all members that should be marshaled as XML
127    *         attributes.
128    */
129   public XMLFieldDescriptor[] getAttributeDescriptors() {
130     return NO_FIELDS;
131   } // getAttributeDescriptors
132 
133   /**
134    * Returns the XMLFieldDescriptor for the member that should be marshaled as text content.
135    *
136    * @return the XMLFieldDescriptor for the member that should be marshaled as text content.
137    */
138   public XMLFieldDescriptor getContentDescriptor() {
139     return CONTENT_DESCRIPTOR;
140   } // getContentDescriptor
141 
142   /**
143    * Returns the set of XMLFieldDescriptors for all members that should be marshaled as XML
144    * elements.
145    *
146    * @return an array of XMLFieldDescriptors for all members that should be marshaled as XML
147    *         elements.
148    */
149   public XMLFieldDescriptor[] getElementDescriptors() {
150     return NO_FIELDS;
151   } // getElementDescriptors
152 
153   /**
154    * Returns the XML field descriptor matching the given xml name and nodeType. If NodeType is null,
155    * then either an AttributeDescriptor, or ElementDescriptor may be returned. Null is returned if
156    * no matching descriptor is available.
157    *
158    * @param name the xml name to match against
159    * @param namespace the namespace of the element. This may be null. Note: A null namespace is not
160    *        the same as the default namespace unless the default namespace is also null.
161    * @param nodeType the NodeType to match against, or null if the node type is not known.
162    * @return the matching descriptor, or null if no matching descriptor is available.
163    */
164   public XMLFieldDescriptor getFieldDescriptor(final String name, final String namespace,
165       final NodeType nodeType) {
166     return null;
167   } // -- getFieldDescriptor
168 
169   /**
170    * @return the namespace prefix to use when marshaling as XML.
171    */
172   public String getNameSpacePrefix() {
173     return null;
174   } // -- getNameSpacePrefix
175 
176   /**
177    * @return the namespace URI used when marshaling and unmarshaling as XML.
178    */
179   public String getNameSpaceURI() {
180     return null;
181   } // -- getNameSpaceURI
182 
183   /**
184    * Returns a specific validator for the class described by this ClassDescriptor. A null value may
185    * be returned if no specific validator exists.
186    *
187    * @return the type validator for the class described by this ClassDescriptor.
188    */
189   public TypeValidator getValidator() {
190     return VALIDATOR;
191   } // -- getValidator
192 
193   /**
194    * Returns the XML Name for the Class being described.
195    *
196    * @return the XML name.
197    */
198   public String getXMLName() {
199     return XML_NAME;
200   } // -- getXMLName
201 
202   /**
203    * Returns the String representation of this XMLClassDescriptor.
204    * 
205    * @return the String representation of this XMLClassDescriptor.
206    */
207   public String toString() {
208     return super.toString() + "; descriptor for class: Date" + "; xml name: " + XML_NAME;
209   } // -- toString
210 
211   // -------------------------------------/
212   // - Implementation of ClassDescriptor -/
213   // -------------------------------------/
214 
215   /**
216    * Returns the Java class represented by this descriptor.
217    *
218    * @return The Java class
219    */
220   public Class getJavaClass() {
221     return java.util.Date.class;
222   } // -- getJavaClass
223 
224   /**
225    * Returns a list of fields represented by this descriptor.
226    *
227    * @return A list of fields
228    */
229   public FieldDescriptor[] getFields() {
230     return FIELDS;
231   } // -- getFields
232 
233   /**
234    * Returns the class descriptor of the class extended by this class.
235    *
236    * @return The extended class descriptor
237    */
238   public ClassDescriptor getExtends() {
239     return null;
240   } // -- getExtends
241 
242   /**
243    * Returns the identity field, null if this class has no identity.
244    *
245    * @return The identity field
246    */
247   public FieldDescriptor getIdentity() {
248     return null;
249   } // -- getIdentity
250 
251   /**
252    * Returns the access mode specified for this class.
253    *
254    * @return The access mode
255    */
256   public AccessMode getAccessMode() {
257     return null;
258   } // -- getAccessMode
259 
260 } // -- class: DateClassDescriptor