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.Locale;
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.UnmarshalState;
45  import org.exolab.castor.xml.ValidationException;
46  import org.exolab.castor.xml.XMLClassDescriptor;
47  import org.exolab.castor.xml.XMLFieldDescriptor;
48  import org.exolab.castor.xml.XMLFieldHandler;
49  import org.exolab.castor.xml.util.XMLFieldDescriptorImpl;
50  
51  /**
52   * A ClassDescriptor for java.util.Locale.
53   *
54   * @author <a href="kvisco-at-intalio.com">Keith Visco</a>
55   * @version $Revision$ $Date: 2004-12-16 22:45:54 -0700 (Thu, 16 Dec 2004) $
56   */
57  public class LocaleDescriptor extends BaseDescriptor {
58  
59    /** Used for returning no attribute and no element fields. */
60    private static final XMLFieldDescriptor[] NO_FIELDS = new XMLFieldDescriptor[0];
61    /** Our field descriptor for our "content". */
62    private static final XMLFieldDescriptorImpl NO_CONTENT = null;
63    /** The name of the XML element. */
64    private static final String XML_NAME = "locale";
65    /** Our field descriptor array. Lists the fields we describe. */
66    private static final XMLFieldDescriptor[] FIELDS;
67    /** The TypeValidator to use for validation of the described class. */
68    private static final TypeValidator VALIDATOR = null;
69  
70    static {
71      // -- Create FieldDescriptor for language
72      XMLFieldDescriptorImpl fdLanguage =
73          new XMLFieldDescriptorImpl(String.class, "language", "language", NodeType.Attribute);
74  
75      fdLanguage.setConstructorArgumentIndex(0);
76      fdLanguage.setHandler(new XMLFieldHandler() {
77  
78        /**
79         * {@inheritDoc}
80         */
81        public Object getValue(final Object object) throws IllegalStateException {
82          return ((java.util.Locale) object).getLanguage();
83        }
84  
85        /**
86         * {@inheritDoc}
87         */
88        public void setValue(final Object object, final Object value)
89            throws IllegalStateException, IllegalArgumentException {
90          // -- do nothing, can only be set via constructor
91        }
92  
93        /**
94         * {@inheritDoc}
95         */
96        public Object newInstance(final Object parent) {
97          // -- not applicable
98          return null;
99        }
100     });
101 
102     // -- Create FieldDescriptor for country
103     XMLFieldDescriptorImpl fdCountry =
104         new XMLFieldDescriptorImpl(String.class, "country", "country", NodeType.Attribute);
105 
106     fdCountry.setConstructorArgumentIndex(1);
107     fdCountry.setHandler(new XMLFieldHandler() {
108 
109       /**
110        * {@inheritDoc}
111        */
112       public Object getValue(final Object object) throws IllegalStateException {
113         return ((java.util.Locale) object).getCountry();
114       }
115 
116       /**
117        * {@inheritDoc}
118        */
119       public void setValue(final Object object, final Object value)
120           throws IllegalStateException, IllegalArgumentException {
121         // -- do nothing, can only be set via constructor
122       }
123 
124       /**
125        * {@inheritDoc}
126        */
127       public Object newInstance(final Object parent) {
128         // -- not applicable
129         return null;
130       }
131     });
132 
133     // // -- Create FieldDescriptor for language variant
134     // XMLFieldDescriptorImpl fdVariant = new XMLFieldDescriptorImpl(
135     // String.class, "variant", "variant", NodeType.Attribute);
136     //
137     // fdVariant.setConstructorArgumentIndex(2);
138     // fdVariant.setRequired(false);
139     // fdVariant.setHandler(new XMLFieldHandler() {
140     //
141     // /**
142     // * {@inheritDoc}
143     // */
144     // public Object getValue(final Object object) throws IllegalStateException {
145     // String variant = ((Locale) object).getVariant();
146     // if (variant == null || variant.length() == 0) {
147     // return null;
148     // }
149     //
150     // return variant;
151     // }
152     //
153     // /**
154     // * {@inheritDoc}
155     // */
156     // public void setValue(final Object object, final Object value)
157     // throws IllegalStateException, IllegalArgumentException {
158     // // -- do nothing, can only be set via constructor
159     // }
160     //
161     // /**
162     // * {@inheritDoc}
163     // */
164     // public Object newInstance(final Object parent) {
165     // // -- not applicable
166     // return null;
167     // }
168     // });
169 
170     FIELDS = new XMLFieldDescriptor[2];
171     FIELDS[0] = fdCountry;
172     FIELDS[1] = fdLanguage;
173     // _fields[2] = fdVariant;
174   }
175 
176   // ----------------/
177   // - Constructors -/
178   // ----------------/
179 
180   /**
181    * No-arg constructor.
182    */
183   public LocaleDescriptor() {
184     super();
185   } // -- LocaleDescriptor
186 
187   // ------------------/
188   // - Public Methods -/
189   // ------------------/
190 
191   /**
192    * Returns the set of XMLFieldDescriptors for all members that should be marshaled as XML
193    * attributes.
194    *
195    * @return an array of XMLFieldDescriptors for all members that should be marshaled as XML
196    *         attributes.
197    */
198   public XMLFieldDescriptor[] getAttributeDescriptors() {
199     return FIELDS;
200   } // getAttributeDescriptors
201 
202   /**
203    * Returns the XMLFieldDescriptor for the member that should be marshaled as text content.
204    *
205    * @return the XMLFieldDescriptor for the member that should be marshaled as text content.
206    */
207   public XMLFieldDescriptor getContentDescriptor() {
208     return NO_CONTENT;
209   } // getContentDescriptor
210 
211   /**
212    * Returns the set of XMLFieldDescriptors for all members that should be marshaled as XML
213    * elements.
214    *
215    * @return an array of XMLFieldDescriptors for all members that should be marshaled as XML
216    *         elements.
217    */
218   public XMLFieldDescriptor[] getElementDescriptors() {
219     return NO_FIELDS;
220   } // getElementDescriptors
221 
222   /**
223    * Returns the XML field descriptor matching the given xml name and nodeType. If NodeType is null,
224    * then either an AttributeDescriptor, or ElementDescriptor may be returned. Null is returned if
225    * no matching descriptor is available.
226    *
227    * @param name the xml name to match against
228    * @param namespace the namespace uri
229    * @param nodeType the NodeType to match against, or null if the node type is not known.
230    * @return the matching descriptor, or null if no matching descriptor is available.
231    */
232   public XMLFieldDescriptor getFieldDescriptor(final String name, final String namespace,
233       final NodeType nodeType) {
234     return null;
235   } // -- getFieldDescriptor
236 
237   /**
238    * @return the namespace prefix to use when marshaling as XML.
239    */
240   public String getNameSpacePrefix() {
241     return null;
242   } // -- getNameSpacePrefix
243 
244   /**
245    * @return the namespace URI used when marshaling and unmarshaling as XML.
246    */
247   public String getNameSpaceURI() {
248     return null;
249   } // -- getNameSpaceURI
250 
251   /**
252    * Returns a specific validator for the class described by this ClassDescriptor. A null value may
253    * be returned if no specific validator exists.
254    *
255    * @return the type validator for the class described by this ClassDescriptor.
256    */
257   public TypeValidator getValidator() {
258     return VALIDATOR;
259   } // -- getValidator
260 
261   /**
262    * Returns the XML Name for the Class being described.
263    *
264    * @return the XML name.
265    */
266   public String getXMLName() {
267     return XML_NAME;
268   } // -- getXMLName
269 
270   /**
271    * Returns the String representation of this XMLClassDescriptor.
272    * 
273    * @return the String representation of this XMLClassDescriptor.
274    */
275   public String toString() {
276     return super.toString() + "; descriptor for class: " + Locale.class.getName() + "; xml name: "
277         + XML_NAME;
278   } // -- toString
279 
280   // -------------------------------------/
281   // - Implementation of ClassDescriptor -/
282   // -------------------------------------/
283 
284   /**
285    * Returns the Java class represented by this descriptor.
286    *
287    * @return The Java class
288    */
289   public Class getJavaClass() {
290     return Locale.class;
291   } // -- getJavaClass
292 
293   /**
294    * Returns a list of fields represented by this descriptor.
295    *
296    * @return A list of fields
297    */
298   public FieldDescriptor[] getFields() {
299     return FIELDS;
300   } // -- getFields
301 
302   /**
303    * Returns the class descriptor of the class extended by this class.
304    *
305    * @return The extended class descriptor
306    */
307   public ClassDescriptor getExtends() {
308     return null;
309   } // -- getExtends
310 
311   /**
312    * Returns the identity field, null if this class has no identity.
313    *
314    * @return The identity field
315    */
316   public FieldDescriptor getIdentity() {
317     return null;
318   } // -- getIdentity
319 
320   /**
321    * Returns the access mode specified for this class.
322    *
323    * @return The access mode
324    */
325   public AccessMode getAccessMode() {
326     return null;
327   } // -- getAccessMode
328 
329 } // -- class: LocaleDescriptor