1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.menu;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Enumeration;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Locale;
27 import java.util.Map;
28 import java.util.MissingResourceException;
29 import java.util.ResourceBundle;
30 import java.util.Set;
31
32 import javax.faces.context.FacesContext;
33
34 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
35 import org.apache.myfaces.trinidad.util.ContainerUtils;
36
37
38
39
40
41
42 public class MenuUtils
43 {
44 MenuUtils() {}
45
46
47
48
49
50
51
52
53
54
55
56
57
58 static String stringReplaceFirst(String fullstring, String str1, String str2)
59 {
60 if (fullstring == null)
61 return null;
62
63 StringBuffer returnStr =
64 stringBufferReplaceFirst(new StringBuffer(fullstring), str1, str2);
65 return returnStr.toString();
66 }
67
68
69
70
71
72
73
74
75
76
77 static StringBuffer stringBufferReplaceFirst(StringBuffer fullBuf, String str1,
78 String str2)
79 {
80 if (fullBuf == null)
81 return null;
82
83 String fullstr = fullBuf.toString();
84
85
86 if (str1 == null || str2 == null)
87 return fullBuf;
88 if ("".equals(fullstr) && !"".equals(str1))
89 return fullBuf;
90
91
92 int startIdx = fullBuf.indexOf(str1);
93 if (startIdx == -1)
94 return fullBuf;
95
96
97 int foundLen = str1.length();
98 int endIdx = startIdx + foundLen;
99
100 StringBuffer returnBuf = fullBuf.replace(startIdx, endIdx, str2);
101 return returnBuf;
102 }
103
104
105
106
107
108
109
110
111
112
113 static public <T> T getBoundValue(String elExpression, Class<T> desiredClass)
114 {
115 try
116 {
117 if (desiredClass == null)
118 throw new NullPointerException();
119
120 FacesContext ctx = FacesContext.getCurrentInstance();
121 return (T) ctx.getApplication().evaluateExpressionGet(ctx,
122 elExpression,
123 desiredClass);
124 }
125 catch (Exception ex)
126 {
127 _LOG.severe("EL Expression " + elExpression +
128 " is invalid or returned a bad value.\n", ex);
129 _LOG.severe(ex);
130 return null;
131 }
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 static boolean evalBoolean (String boolStr, boolean defaultVal)
151 {
152 if ( boolStr != null
153 && ContainerUtils.isValueReference(boolStr)
154 )
155 {
156 Boolean bValue = getBoundValue(boolStr, Boolean.class);
157 return bValue.booleanValue();
158 }
159 else
160 {
161 if ("true".equals(boolStr) || "false".equals(boolStr))
162 return (Boolean.valueOf(boolStr)).booleanValue();
163 else
164 return defaultVal;
165 }
166 }
167
168
169
170
171
172
173
174 static String evalString(String propVal)
175 {
176 if ( propVal != null
177 && ContainerUtils.isValueReference(propVal)
178 )
179 {
180 String elVal = getBoundValue(propVal, String.class);
181 return elVal;
182 }
183 return propVal;
184 }
185
186
187
188
189
190
191
192 static int evalInt(String propVal)
193 {
194 if ( propVal != null
195 && ContainerUtils.isValueReference(propVal)
196 )
197 {
198 Integer elVal = getBoundValue(propVal, Integer.class);
199 return elVal.intValue();
200 }
201 return Integer.parseInt(propVal);
202 }
203
204
205
206
207
208
209
210
211 @SuppressWarnings("unchecked")
212 static public void loadBundle(String resBundleName, String resBundleKey)
213 {
214 FacesContext facesContext = FacesContext.getCurrentInstance();
215 Map<String, Object> applicationMap =
216 facesContext.getExternalContext().getApplicationMap();
217
218
219 Locale requestLocale = facesContext.getViewRoot().getLocale();
220
221
222 if (requestLocale == null)
223 {
224 requestLocale = facesContext.getApplication().getDefaultLocale();
225 }
226
227
228 _BundleMap bundleMap = (_BundleMap) applicationMap.get(resBundleKey);
229
230
231
232 if (bundleMap != null)
233 {
234 Locale bundleLocale = bundleMap.getLocale();
235
236 if (bundleLocale == null)
237 {
238 ResourceBundle rb = bundleMap.getBundle();
239 bundleLocale = rb.getLocale();
240 }
241
242 if (requestLocale == bundleLocale)
243 {
244
245 return;
246 }
247 }
248
249 String bundleName = null;
250
251 if (resBundleName != null)
252 {
253
254 if (ContainerUtils.isValueReference(resBundleName))
255 {
256 bundleName = MenuUtils.getBoundValue(resBundleName, String.class);
257 }
258 else
259 {
260 bundleName = resBundleName ;
261 }
262 }
263
264 final ResourceBundle bundle;
265
266 try
267 {
268 bundle = ResourceBundle.getBundle(bundleName, requestLocale);
269 }
270 catch (MissingResourceException e)
271 {
272 _LOG.severe("RESOURCE_BUNDLE_NOT_FOUND", bundleName);
273 _LOG.severe(e);
274 return;
275 }
276
277
278
279
280 applicationMap.put(resBundleKey, new _BundleMap(bundle, requestLocale));
281 }
282
283
284
285
286
287
288
289 @SuppressWarnings("unchecked")
290 static private class _BundleMap implements Map<String, String>
291 {
292 private ResourceBundle _bundle;
293 private Locale _locale;
294 private List<String> _values;
295
296 public _BundleMap(ResourceBundle bundle)
297 {
298 _bundle = bundle;
299 _locale = bundle.getLocale();
300 }
301
302 public _BundleMap(ResourceBundle bundle, Locale locale)
303 {
304 _bundle = bundle;
305 _locale = locale;
306 }
307
308
309 public String get(Object key)
310 {
311 try
312 {
313 return _bundle.getString(key.toString());
314 }
315 catch (Exception e)
316 {
317 return "!!!" + key + "!!!";
318 }
319 }
320
321 public boolean isEmpty()
322 {
323 return !_bundle.getKeys().hasMoreElements();
324 }
325
326 public boolean containsKey(Object key)
327 {
328 return _bundle.getObject(key.toString()) != null;
329 }
330
331
332 public Collection<String> values()
333 {
334 if (_values == null)
335 {
336 _values = new ArrayList<String>();
337 for (Enumeration<String> enumer = _bundle.getKeys();
338 enumer.hasMoreElements(); )
339 {
340 String v = _bundle.getString(enumer.nextElement());
341 _values.add(v);
342 }
343 }
344 return _values;
345 }
346
347 public int size()
348 {
349 return values().size();
350 }
351
352 public boolean containsValue(Object value)
353 {
354 return values().contains(value);
355 }
356
357 public Set<Map.Entry<String, String>> entrySet()
358 {
359 Set<Map.Entry<String, String>> set = new HashSet<Map.Entry<String, String>>();
360
361 for (Enumeration<String> enumer = _bundle.getKeys(); enumer.hasMoreElements(); )
362 {
363 final String k = enumer.nextElement();
364 set.add(new Map.Entry<String, String>()
365 {
366 public String getKey()
367 {
368 return k;
369 }
370
371 public String getValue()
372 {
373 return _bundle.getString(k);
374 }
375
376 public String setValue(String value)
377 {
378 throw new UnsupportedOperationException();
379 }
380 });
381 }
382 return set;
383 }
384
385 public Set<String> keySet()
386 {
387 Set<String> set = new HashSet<String>();
388 for (Enumeration<String> enumer = _bundle.getKeys(); enumer.hasMoreElements(); )
389 {
390 set.add(enumer.nextElement());
391 }
392 return set;
393 }
394
395
396 public String remove(Object key)
397 {
398 throw new UnsupportedOperationException();
399 }
400
401 public void putAll(Map<? extends String, ? extends String> t)
402 {
403 throw new UnsupportedOperationException();
404 }
405
406 public String put(String key, String value)
407 {
408 throw new UnsupportedOperationException();
409 }
410
411 public void clear()
412 {
413 throw new UnsupportedOperationException();
414 }
415
416 public ResourceBundle getBundle()
417 {
418 return _bundle;
419 }
420
421 public Locale getLocale()
422 {
423 return _locale;
424 }
425 }
426
427 private final static TrinidadLogger _LOG =
428 TrinidadLogger.createTrinidadLogger(MenuUtils.class);
429 }