1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
package org.webmacro.servlet; |
25 | |
|
26 | |
import org.webmacro.Context; |
27 | |
import org.webmacro.ContextTool; |
28 | |
import org.webmacro.PropertyException; |
29 | |
import org.webmacro.UnsettableException; |
30 | |
import org.webmacro.util.Bag; |
31 | |
|
32 | |
import java.lang.reflect.Field; |
33 | |
import java.util.HashMap; |
34 | |
import java.util.Locale; |
35 | |
import java.util.NoSuchElementException; |
36 | |
import java.util.StringTokenizer; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class LocaleTool extends ContextTool implements Bag |
43 | |
{ |
44 | |
|
45 | |
public static final String RCS = "$Id: LocaleTool.java,v 1.10 2003/07/30 04:51:17 briangoetz Exp $"; |
46 | |
|
47 | 0 | private static HashMap cache = new HashMap(); |
48 | |
|
49 | |
public Object init (Context context) |
50 | |
throws PropertyException |
51 | |
{ |
52 | 0 | return this; |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
final public Locale getDefault () |
60 | |
{ |
61 | 0 | return Locale.getDefault(); |
62 | |
} |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
final public Locale getLocale (String country) |
69 | |
{ |
70 | 0 | return getLocale(country, "", ""); |
71 | |
} |
72 | |
|
73 | |
final public Locale getLocale (String country, String language) |
74 | |
{ |
75 | 0 | return getLocale(country, language, ""); |
76 | |
|
77 | |
} |
78 | |
|
79 | |
final public Locale getLocale (String country, String language, String variant) |
80 | |
{ |
81 | 0 | String key = country + "_" + language + "_" + variant; |
82 | 0 | Locale locale = (Locale) cache.get(key); |
83 | 0 | if (locale == null) |
84 | |
{ |
85 | 0 | locale = new Locale(country, language, ""); |
86 | 0 | cache.put(key, locale); |
87 | |
} |
88 | 0 | return locale; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
final public Object get (String field) |
95 | |
{ |
96 | 0 | return buildLocale(field); |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
final public static Locale buildLocale (String field) |
115 | |
{ |
116 | |
|
117 | 0 | Locale locale = (Locale) cache.get(field); |
118 | 0 | if (locale == null) |
119 | |
{ |
120 | |
try |
121 | |
{ |
122 | 0 | Field f = Locale.class.getField(field); |
123 | 0 | locale = (Locale) f.get(null); |
124 | |
} |
125 | 0 | catch (Exception ne) |
126 | |
{ |
127 | 0 | StringTokenizer st = new StringTokenizer(field, "_"); |
128 | 0 | String[] parts = new String[]{"", "", ""}; |
129 | |
try |
130 | |
{ |
131 | 0 | for (int i = 0; i < 3; i++) |
132 | |
{ |
133 | 0 | parts[i] = st.nextToken(); |
134 | |
} |
135 | |
} |
136 | 0 | catch (NoSuchElementException e) |
137 | |
{ |
138 | 0 | } |
139 | |
|
140 | |
|
141 | 0 | locale = new Locale(parts[0], parts[1], parts[2]); |
142 | 0 | } |
143 | |
|
144 | 0 | cache.put(field, locale); |
145 | |
} |
146 | 0 | return locale; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
final public void put (String key, Object value) |
153 | |
throws UnsettableException |
154 | |
{ |
155 | 0 | throw new UnsettableException("Cannot set a form property"); |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
final public void remove (String key) |
163 | |
throws UnsettableException |
164 | |
{ |
165 | 0 | throw new UnsettableException("Cannot unset a form property"); |
166 | |
} |
167 | |
|
168 | |
} |