01: /**********************************************************************
02: Copyright (c) 2003 Erik Bengtson and others. All rights reserved.
03: Licensed under the Apache License, Version 2.0 (the "License");
04: you may not use this file except in compliance with the License.
05: You may obtain a copy of the License at
06:
07: http://www.apache.org/licenses/LICENSE-2.0
08:
09: Unless required by applicable law or agreed to in writing, software
10: distributed under the License is distributed on an "AS IS" BASIS,
11: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: See the License for the specific language governing permissions and
13: limitations under the License.
14:
15:
16: Contributors:
17: 2003 Andy Jefferson - coding standards
18: ...
19: **********************************************************************/package org.jpox.store.mapping;
20:
21: import java.util.Locale;
22:
23: import org.jpox.ClassLoaderResolver;
24: import org.jpox.util.I18nUtils;
25:
26: /**
27: * Mapping for Locale type.
28: *
29: * Locale should be stored in colums from 2 to 20 characters. Normaly, we will have a string no longer than
30: * 5 characters, but variants, in general, are vendor specific and can be longer than expected.
31: * The Variant codes are vendor and browser-specific. For example, use WIN for Windows, MAC for Macintosh,
32: * and POSIX for POSIX. Where there are two variants, separate them with an underscore, and put the most
33: * important one first. For example, a Traditional Spanish collation might construct a locale with
34: * parameters for language, country and variant as: "es", "ES", "Traditional_WIN".
35: * language_country_variant
36: * Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr_MAC"
37: * @see java.util.Locale
38: *
39: * @version $Revision: 1.18 $
40: **/
41: public class LocaleMapping extends ObjectAsStringMapping {
42: private static Locale mappingSampleValue = Locale.getDefault();
43:
44: public Object getSampleValue(ClassLoaderResolver clr) {
45: return mappingSampleValue;
46: }
47:
48: public Class getJavaType() {
49: return Locale.class;
50: }
51:
52: /**
53: * Method to return the default length of this type in the datastore.
54: * Locales require 20 characters.
55: * @param index The index position
56: * @return The default length
57: */
58: public int getDefaultLength(int index) {
59: return 20;
60: }
61:
62: /**
63: * Method to set the datastore string value based on the object value.
64: * @param object The object
65: * @return The string value to pass to the datastore
66: */
67: protected String objectToString(Object object) {
68: String locale;
69: if (object instanceof Locale) {
70: locale = ((Locale) object).toString();
71: } else {
72: locale = (String) object;
73: }
74: return locale;
75: }
76:
77: /**
78: * Method to extract the objects value from the datastore string value.
79: * @param datastoreValue Value obtained from the datastore
80: * @return The value of this object (derived from the datastore string value)
81: */
82: protected Object stringToObject(String datastoreValue) {
83: return I18nUtils.getLocaleFromString(datastoreValue);
84: }
85: }
|