1 /***
2 * Licensed under the Artistic License; you may not use this file
3 * except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://displaytag.sourceforge.net/license.html
7 *
8 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 */
12 package org.displaytag.localization;
13
14 import java.util.Locale;
15 import java.util.MissingResourceException;
16 import java.util.ResourceBundle;
17
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.jsp.PageContext;
20 import javax.servlet.jsp.jstl.core.Config;
21 import javax.servlet.jsp.jstl.fmt.LocalizationContext;
22 import javax.servlet.jsp.tagext.Tag;
23 import javax.servlet.jsp.tagext.TagSupport;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.taglibs.standard.tag.common.fmt.BundleSupport;
28 import org.displaytag.Messages;
29
30
31 /***
32 * JSTL implementation of a resource provider and locale resolver. It will make the <code>titleKey</code> attribute of
33 * column tag works the same as fmt:message's <code>key property</code>. This tag must be the descendant of a
34 * <code>fmt:bundle</code> tag in order to use the titleKey. This is just a shortcut, which makes
35 *
36 * <pre>
37 * <display:column titleKey="bar"/>
38 * </pre>
39 *
40 * behave the same as
41 *
42 * <pre>
43 * <c:set var="foo">
44 * <fmt:message key="bar"/>
45 * </c:set>
46 * <display:column title="${foo}"/>
47 * </pre>
48 *
49 * If you don't define either <code>titleKey</code> or <code>titleKey</code> property on your column, first the tag
50 * will attempt to look up the <code>property</code> property in your ResourceBundle. Failing that, it will fall back
51 * to the parent class's behavior of just using the property name.
52 * @author Fabrizio Giustina
53 * @version $Revision: 1.5 $ ($Author: fgiust $)
54 */
55 public class I18nJstlAdapter implements I18nResourceProvider, LocaleResolver
56 {
57
58 /***
59 * prefix/suffix for missing entries.
60 */
61 public static final String UNDEFINED_KEY = "???";
62
63 /***
64 * logger.
65 */
66 private static Log log = LogFactory.getLog(I18nJstlAdapter.class);
67
68 /***
69 * Instantiates a new I18nJstlAdapter. Throw a NoClassDefFound error if BundleSupport is not available.
70 */
71 public I18nJstlAdapter()
72 {
73
74
75 BundleSupport.class.hashCode();
76 }
77
78 /***
79 * @see LocaleResolver#resolveLocale(HttpServletRequest)
80 */
81 public Locale resolveLocale(HttpServletRequest request)
82 {
83 Locale locale = (Locale) Config.get(request.getSession(), Config.FMT_LOCALE);
84 if (locale == null)
85 {
86 locale = request.getLocale();
87 }
88 return locale;
89 }
90
91 /***
92 * @see I18nResourceProvider#getResource(String, String, Tag, PageContext)
93 */
94 public String getResource(String resourceKey, String defaultValue, Tag tag, PageContext pageContext)
95 {
96
97
98 String key = (resourceKey != null) ? resourceKey : defaultValue;
99 String title = null;
100 ResourceBundle bundle = null;
101
102
103 Tag bundleTag = TagSupport.findAncestorWithClass(tag, BundleSupport.class);
104 if (bundleTag != null)
105 {
106 BundleSupport parent = (BundleSupport) bundleTag;
107 if (key != null)
108 {
109 String prefix = parent.getPrefix();
110 if (prefix != null)
111 {
112 key = prefix + key;
113 }
114 }
115 bundle = parent.getLocalizationContext().getResourceBundle();
116 }
117
118
119 if (bundle == null)
120 {
121 Object cauchoBundle = pageContext.getAttribute("caucho.bundle");
122 if (cauchoBundle != null && cauchoBundle instanceof LocalizationContext)
123 {
124 bundle = ((LocalizationContext) cauchoBundle).getResourceBundle();
125
126
127 String prefix = (String) pageContext.getAttribute("caucho.bundle.prefix");
128 if (prefix != null)
129 {
130 key = prefix + key;
131 }
132 }
133 }
134
135
136 if (bundle == null)
137 {
138
139 LocalizationContext localization = BundleSupport.getLocalizationContext(pageContext);
140
141 if (localization != null)
142 {
143 bundle = localization.getResourceBundle();
144 }
145 }
146
147 if (bundle != null)
148 {
149 try
150 {
151 title = bundle.getString(key);
152 }
153 catch (MissingResourceException e)
154 {
155 log.debug(Messages.getString("Localization.missingkey", key));
156
157
158 if (resourceKey != null)
159 {
160 title = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY;
161 }
162 }
163 }
164
165 return title;
166 }
167 }