KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > view > freemarker > LocalizedObjectWrapper


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * flx
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.view.freemarker;
25
26 import java.util.Locale JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.springframework.core.GenericCollectionTypeResolver;
30 import org.springframework.core.JdkVersion;
31
32 import freemarker.core.Environment;
33 import freemarker.template.DefaultObjectWrapper;
34 import freemarker.template.TemplateModel;
35 import freemarker.template.TemplateModelException;
36
37 /**
38  * ObjectWrapper that supports Maps containing {@link Locale} keys. When the
39  * map is to be wrapped, the entry for the current locale is returned instead
40  * of the map itself.
41  *
42  * @author Felix Gnass [fgnass at neteye dot de]
43  * @since 6.5
44  */

45 public class LocalizedObjectWrapper extends DefaultObjectWrapper {
46
47     private boolean exact = false;
48
49     private Locale JavaDoc fallbackLocale = null;
50
51     /**
52      * Sets whether the current locale must exactly match the key. If set to
53      * <code>false</code> (which is the default), a second lookup is performed
54      * with the language only in case no entry can be found.
55      */

56     public void setExact(boolean exact) {
57         this.exact = exact;
58     }
59
60     /**
61      * Sets a fallback locale that is used in case no entry is found for the
62      * current locale. Default is <code>null</code>.
63      */

64     public void setFallbackLocale(Locale JavaDoc fallbackLocale) {
65         this.fallbackLocale = fallbackLocale;
66     }
67
68     public TemplateModel wrap(Object JavaDoc obj) throws TemplateModelException {
69         if (obj instanceof Map JavaDoc) {
70             Map JavaDoc map = (Map JavaDoc) obj;
71             if (hasLocaleKey(map)) {
72                 return wrapLocalizedEntry(map);
73             }
74         }
75         return super.wrap(obj);
76     }
77
78     private boolean hasLocaleKey(Map JavaDoc map) {
79         if (JdkVersion.isAtLeastJava15()) {
80             Class JavaDoc keyType = GenericCollectionTypeResolver.getMapKeyType(
81                     map.getClass());
82
83             if (keyType != null) {
84                 return keyType.isAssignableFrom(Locale JavaDoc.class);
85             }
86         }
87         if (!map.isEmpty()) {
88             Object JavaDoc firstKey = map.keySet().iterator().next();
89             return firstKey instanceof Locale JavaDoc;
90         }
91         return false;
92     }
93
94     private TemplateModel wrapLocalizedEntry(Map JavaDoc map)
95             throws TemplateModelException {
96
97         Environment env = Environment.getCurrentEnvironment();
98         Locale JavaDoc locale = env.getLocale();
99         return super.wrap(getLocalizedEntry(map, locale));
100     }
101
102     private Object JavaDoc getLocalizedEntry(Map JavaDoc map, Locale JavaDoc locale) {
103         Object JavaDoc value = map.get(locale);
104         if (value == null && !exact) {
105             Locale JavaDoc lang = new Locale JavaDoc(locale.getLanguage());
106             value = map.get(lang);
107         }
108         if (value == null && fallbackLocale != null
109                 && !fallbackLocale.equals(locale)) {
110
111             value = getLocalizedEntry(map, fallbackLocale);
112         }
113         return value;
114     }
115
116 }
117
Popular Tags