View Javadoc
1   /*
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2000-2004 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
44   */
45  package org.exolab.castor.xml.descriptors;
46  
47  import java.util.Locale;
48  
49  import org.exolab.castor.mapping.AccessMode;
50  import org.exolab.castor.mapping.ClassDescriptor;
51  import org.exolab.castor.mapping.FieldDescriptor;
52  import org.exolab.castor.xml.NodeType;
53  import org.exolab.castor.xml.TypeValidator;
54  import org.exolab.castor.xml.UnmarshalState;
55  import org.exolab.castor.xml.ValidationException;
56  import org.exolab.castor.xml.XMLClassDescriptor;
57  import org.exolab.castor.xml.XMLFieldDescriptor;
58  import org.exolab.castor.xml.XMLFieldHandler;
59  import org.exolab.castor.xml.util.XMLFieldDescriptorImpl;
60  
61  /**
62   * A ClassDescriptor for java.util.Locale.
63   *
64   * @author <a href="kvisco-at-intalio.com">Keith Visco</a>
65   * @version $Revision$ $Date: 2004-12-16 22:45:54 -0700 (Thu, 16 Dec 2004) $
66   */
67  public class LocaleDescriptor extends BaseDescriptor {
68  
69      /** Used for returning no attribute and no element fields. */
70      private static final XMLFieldDescriptor[]   NO_FIELDS  = new XMLFieldDescriptor[0];
71      /** Our field descriptor for our "content". */
72      private static final XMLFieldDescriptorImpl NO_CONTENT = null;
73      /** The name of the XML element. */
74      private static final String                 XML_NAME   = "locale";
75      /** Our field descriptor array. Lists the fields we describe. */
76      private static final XMLFieldDescriptor[]   FIELDS;
77      /** The TypeValidator to use for validation of the described class. */
78      private static final TypeValidator          VALIDATOR  = null;
79  
80      static {
81          // -- Create FieldDescriptor for language
82          XMLFieldDescriptorImpl fdLanguage = new XMLFieldDescriptorImpl(
83                  String.class, "language", "language", NodeType.Attribute);
84  
85          fdLanguage.setConstructorArgumentIndex(0);
86          fdLanguage.setHandler(new XMLFieldHandler() {
87  
88              /**
89               * {@inheritDoc}
90               */
91              public Object getValue(final Object object) throws IllegalStateException {
92                  return ((java.util.Locale) object).getLanguage();
93              }
94  
95              /**
96               * {@inheritDoc}
97               */
98              public void setValue(final Object object, final Object value)
99                          throws IllegalStateException, IllegalArgumentException {
100                 //-- do nothing, can only be set via constructor
101             }
102 
103             /**
104              * {@inheritDoc}
105              */
106             public Object newInstance(final Object parent) {
107                 //-- not applicable
108                 return null;
109             }
110         });
111 
112         // -- Create FieldDescriptor for country
113         XMLFieldDescriptorImpl fdCountry = new XMLFieldDescriptorImpl(
114                 String.class, "country", "country", NodeType.Attribute);
115 
116         fdCountry.setConstructorArgumentIndex(1);
117         fdCountry.setHandler(new XMLFieldHandler() {
118 
119             /**
120              * {@inheritDoc}
121              */
122             public Object getValue(final Object object) throws IllegalStateException {
123                 return ((java.util.Locale) object).getCountry();
124             }
125 
126             /**
127              * {@inheritDoc}
128              */
129             public void setValue(final Object object, final Object value)
130                     throws IllegalStateException, IllegalArgumentException {
131                 // -- do nothing, can only be set via constructor
132             }
133 
134             /**
135              * {@inheritDoc}
136              */
137             public Object newInstance(final Object parent) {
138                 // -- not applicable
139                 return null;
140             }
141         });
142 
143 //        // -- Create FieldDescriptor for language variant
144 //        XMLFieldDescriptorImpl fdVariant = new XMLFieldDescriptorImpl(
145 //                String.class, "variant", "variant", NodeType.Attribute);
146 //
147 //        fdVariant.setConstructorArgumentIndex(2);
148 //        fdVariant.setRequired(false);
149 //        fdVariant.setHandler(new XMLFieldHandler() {
150 //
151 //            /**
152 //             * {@inheritDoc}
153 //             */
154 //            public Object getValue(final Object object) throws IllegalStateException {
155 //                String variant = ((Locale) object).getVariant();
156 //                if (variant == null || variant.length() == 0) {
157 //                    return null;
158 //                }
159 //
160 //                return variant;
161 //            }
162 //
163 //            /**
164 //             * {@inheritDoc}
165 //             */
166 //            public void setValue(final Object object, final Object value)
167 //                        throws IllegalStateException, IllegalArgumentException {
168 //                // -- do nothing, can only be set via constructor
169 //            }
170 //
171 //            /**
172 //             * {@inheritDoc}
173 //             */
174 //            public Object newInstance(final Object parent) {
175 //                // -- not applicable
176 //                return null;
177 //            }
178 //        });
179 
180         FIELDS = new XMLFieldDescriptor[2];
181         FIELDS[0] = fdCountry;
182         FIELDS[1] = fdLanguage;
183         // _fields[2] = fdVariant;
184     }
185 
186     //----------------/
187     //- Constructors -/
188     //----------------/
189 
190     /**
191      * No-arg constructor.
192      */
193     public LocaleDescriptor() {
194         super();
195     } //-- LocaleDescriptor
196 
197     //------------------/
198     //- Public Methods -/
199     //------------------/
200 
201     /**
202      * Returns the set of XMLFieldDescriptors for all members that should be
203      * marshaled as XML attributes.
204      *
205      * @return an array of XMLFieldDescriptors for all members that should be
206      *         marshaled as XML attributes.
207      */
208     public XMLFieldDescriptor[]  getAttributeDescriptors() {
209         return FIELDS;
210     } // getAttributeDescriptors
211 
212     /**
213      * Returns the XMLFieldDescriptor for the member that should be marshaled
214      * as text content.
215      *
216      * @return the XMLFieldDescriptor for the member that should be marshaled
217      *         as text content.
218      */
219     public XMLFieldDescriptor getContentDescriptor() {
220         return NO_CONTENT;
221     } // getContentDescriptor
222 
223     /**
224      * Returns the set of XMLFieldDescriptors for all members that should be
225      * marshaled as XML elements.
226      *
227      * @return an array of XMLFieldDescriptors for all members that should be
228      *         marshaled as XML elements.
229      */
230     public XMLFieldDescriptor[]  getElementDescriptors() {
231         return NO_FIELDS;
232     } // getElementDescriptors
233 
234     /**
235      * Returns the XML field descriptor matching the given xml name and
236      * nodeType. If NodeType is null, then either an AttributeDescriptor, or
237      * ElementDescriptor may be returned. Null is returned if no matching
238      * descriptor is available.
239      *
240      * @param name the xml name to match against
241      * @param namespace the namespace uri
242      * @param nodeType the NodeType to match against, or null if the node type
243      *        is not known.
244      * @return the matching descriptor, or null if no matching descriptor is
245      *         available.
246      */
247     public XMLFieldDescriptor getFieldDescriptor(final String name, final String namespace,
248             final NodeType nodeType) {
249         return null;
250     } //-- getFieldDescriptor
251 
252     /**
253      * @return the namespace prefix to use when marshaling as XML.
254      */
255     public String getNameSpacePrefix() {
256         return null;
257     } //-- getNameSpacePrefix
258 
259     /**
260      * @return the namespace URI used when marshaling and unmarshaling as XML.
261      */
262     public String getNameSpaceURI() {
263         return null;
264     } //-- getNameSpaceURI
265 
266     /**
267      * Returns a specific validator for the class described by this
268      * ClassDescriptor. A null value may be returned if no specific validator
269      * exists.
270      *
271      * @return the type validator for the class described by this
272      *         ClassDescriptor.
273      */
274     public TypeValidator getValidator() {
275         return VALIDATOR;
276     } //-- getValidator
277 
278     /**
279      * Returns the XML Name for the Class being described.
280      *
281      * @return the XML name.
282      */
283     public String getXMLName() {
284         return XML_NAME;
285     } //-- getXMLName
286 
287     /**
288      * Returns the String representation of this XMLClassDescriptor.
289      * @return the String representation of this XMLClassDescriptor.
290      */
291     public String toString() {
292         return super.toString() + "; descriptor for class: "
293                 + Locale.class.getName() + "; xml name: " + XML_NAME;
294     } //-- toString
295 
296     //-------------------------------------/
297     //- Implementation of ClassDescriptor -/
298     //-------------------------------------/
299 
300     /**
301      * Returns the Java class represented by this descriptor.
302      *
303      * @return The Java class
304      */
305     public Class getJavaClass() {
306         return Locale.class;
307     } //-- getJavaClass
308 
309     /**
310      * Returns a list of fields represented by this descriptor.
311      *
312      * @return A list of fields
313      */
314     public FieldDescriptor[] getFields() {
315         return FIELDS;
316     } //-- getFields
317 
318     /**
319      * Returns the class descriptor of the class extended by this class.
320      *
321      * @return The extended class descriptor
322      */
323     public ClassDescriptor getExtends() {
324         return null;
325     } //-- getExtends
326 
327     /**
328      * Returns the identity field, null if this class has no identity.
329      *
330      * @return The identity field
331      */
332     public FieldDescriptor getIdentity() {
333         return null;
334     } //-- getIdentity
335 
336     /**
337      * Returns the access mode specified for this class.
338      *
339      * @return The access mode
340      */
341     public AccessMode getAccessMode() {
342         return null;
343     } //-- getAccessMode
344 
345 } //-- class: LocaleDescriptor