1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.taglib.core; |
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.component.UIViewRoot; |
33 | |
import javax.faces.context.FacesContext; |
34 | |
import javax.servlet.jsp.JspException; |
35 | |
import javax.servlet.jsp.tagext.Tag; |
36 | |
import javax.servlet.jsp.tagext.TagSupport; |
37 | |
import javax.el.ValueExpression; |
38 | |
|
39 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspAttribute; |
40 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspTag; |
41 | |
import org.apache.myfaces.shared.util.ClassUtils; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
@JSFJspTag(name = "f:loadBundle", bodyContent = "empty") |
58 | 0 | public class LoadBundleTag extends TagSupport |
59 | |
{ |
60 | |
private static final long serialVersionUID = -8892145684062838928L; |
61 | |
|
62 | |
private ValueExpression _basename; |
63 | |
private String _var; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
@JSFJspAttribute(className="javax.el.ValueExpression", |
69 | |
deferredValueType="java.lang.String") |
70 | |
public void setBasename(ValueExpression basename) |
71 | |
{ |
72 | 0 | _basename = basename; |
73 | 0 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
@JSFJspAttribute(required = true) |
79 | |
public void setVar(String var) |
80 | |
{ |
81 | 0 | _var = var; |
82 | 0 | } |
83 | |
|
84 | |
@Override |
85 | |
public int doStartTag() throws JspException |
86 | |
{ |
87 | 0 | if (null == _var) |
88 | |
{ |
89 | 0 | throw new NullPointerException("LoadBundle: 'var' must not be null"); |
90 | |
} |
91 | |
|
92 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
93 | 0 | if (facesContext == null) |
94 | |
{ |
95 | 0 | throw new JspException("No faces context?!"); |
96 | |
} |
97 | |
|
98 | 0 | UIViewRoot viewRoot = facesContext.getViewRoot(); |
99 | 0 | if (viewRoot == null) |
100 | |
{ |
101 | 0 | throw new JspException("No view root! LoadBundle must be nested inside <f:view> action."); |
102 | |
} |
103 | |
|
104 | 0 | Locale locale = viewRoot.getLocale(); |
105 | 0 | if (locale == null) |
106 | |
{ |
107 | 0 | locale = facesContext.getApplication().getDefaultLocale(); |
108 | |
} |
109 | |
|
110 | 0 | String basename = null; |
111 | 0 | if (_basename != null) |
112 | |
{ |
113 | 0 | if (_basename.isLiteralText()) |
114 | |
{ |
115 | 0 | basename = _basename.getExpressionString(); |
116 | |
} |
117 | |
else |
118 | |
{ |
119 | 0 | basename = (String)_basename.getValue(facesContext.getELContext()); |
120 | |
} |
121 | |
} |
122 | |
|
123 | 0 | if (null == basename) |
124 | |
{ |
125 | 0 | throw new NullPointerException("LoadBundle: 'basename' must not be null"); |
126 | |
} |
127 | |
|
128 | |
ResourceBundle bundle; |
129 | |
try |
130 | |
{ |
131 | 0 | bundle = ResourceBundle.getBundle(basename, |
132 | |
locale, |
133 | |
ClassUtils.getContextClassLoader()); |
134 | |
} |
135 | 0 | catch (MissingResourceException e) |
136 | |
{ |
137 | |
try |
138 | |
{ |
139 | 0 | bundle = ResourceBundle.getBundle(basename, |
140 | |
locale, |
141 | |
this.getClass().getClassLoader()); |
142 | |
} |
143 | 0 | catch (MissingResourceException e1) |
144 | |
{ |
145 | 0 | throw new JspException("Resource bundle '" + basename + "' could not be found.", e1); |
146 | 0 | } |
147 | 0 | } |
148 | |
|
149 | 0 | facesContext.getExternalContext().getRequestMap().put(_var, new BundleMap(bundle)); |
150 | 0 | return Tag.SKIP_BODY; |
151 | |
} |
152 | |
|
153 | 0 | private static class BundleMap implements Map<String, String> |
154 | |
{ |
155 | |
private ResourceBundle _bundle; |
156 | |
private List<String> _values; |
157 | |
|
158 | |
public BundleMap(ResourceBundle bundle) |
159 | 0 | { |
160 | 0 | _bundle = bundle; |
161 | 0 | } |
162 | |
|
163 | |
|
164 | |
|
165 | |
public String get(Object key) |
166 | |
{ |
167 | |
try |
168 | |
{ |
169 | 0 | return (String)_bundle.getObject(key.toString()); |
170 | |
} |
171 | 0 | catch (Exception e) |
172 | |
{ |
173 | 0 | return "???" + key + "???"; |
174 | |
} |
175 | |
} |
176 | |
|
177 | |
public boolean isEmpty() |
178 | |
{ |
179 | 0 | return !_bundle.getKeys().hasMoreElements(); |
180 | |
} |
181 | |
|
182 | |
public boolean containsKey(Object key) |
183 | |
{ |
184 | |
try |
185 | |
{ |
186 | 0 | return _bundle.getObject(key.toString()) != null; |
187 | |
} |
188 | 0 | catch (MissingResourceException e) |
189 | |
{ |
190 | 0 | return false; |
191 | |
} |
192 | |
} |
193 | |
|
194 | |
|
195 | |
|
196 | |
public Collection<String> values() |
197 | |
{ |
198 | 0 | if (_values == null) |
199 | |
{ |
200 | 0 | _values = new ArrayList<String>(); |
201 | 0 | for (Enumeration<String> enumer = _bundle.getKeys(); enumer.hasMoreElements();) |
202 | |
{ |
203 | 0 | String v = _bundle.getString(enumer.nextElement()); |
204 | 0 | _values.add(v); |
205 | 0 | } |
206 | |
} |
207 | 0 | return _values; |
208 | |
} |
209 | |
|
210 | |
public int size() |
211 | |
{ |
212 | 0 | return values().size(); |
213 | |
} |
214 | |
|
215 | |
public boolean containsValue(Object value) |
216 | |
{ |
217 | 0 | return values().contains(value); |
218 | |
} |
219 | |
|
220 | |
public Set<Map.Entry<String, String>> entrySet() |
221 | |
{ |
222 | 0 | Set<Entry<String, String>> set = new HashSet<Entry<String, String>>(); |
223 | 0 | for (Enumeration<String> enumer = _bundle.getKeys(); enumer.hasMoreElements();) |
224 | |
{ |
225 | 0 | final String k = enumer.nextElement(); |
226 | 0 | set.add(new Map.Entry<String, String>() |
227 | 0 | { |
228 | |
public String getKey() |
229 | |
{ |
230 | 0 | return k; |
231 | |
} |
232 | |
|
233 | |
public String getValue() |
234 | |
{ |
235 | 0 | return (String)_bundle.getObject(k); |
236 | |
} |
237 | |
|
238 | |
public String setValue(String value) |
239 | |
{ |
240 | 0 | throw new UnsupportedOperationException(this.getClass().getName() |
241 | |
+ " UnsupportedOperationException"); |
242 | |
} |
243 | |
}); |
244 | 0 | } |
245 | 0 | return set; |
246 | |
} |
247 | |
|
248 | |
public Set<String> keySet() |
249 | |
{ |
250 | 0 | Set<String> set = new HashSet<String>(); |
251 | 0 | for (Enumeration<String> enumer = _bundle.getKeys(); enumer.hasMoreElements();) |
252 | |
{ |
253 | 0 | set.add(enumer.nextElement()); |
254 | |
} |
255 | 0 | return set; |
256 | |
} |
257 | |
|
258 | |
|
259 | |
|
260 | |
public String remove(Object key) |
261 | |
{ |
262 | 0 | throw new UnsupportedOperationException(this.getClass().getName() + " UnsupportedOperationException"); |
263 | |
} |
264 | |
|
265 | |
public void putAll(Map<? extends String, ? extends String> t) |
266 | |
{ |
267 | 0 | throw new UnsupportedOperationException(this.getClass().getName() + " UnsupportedOperationException"); |
268 | |
} |
269 | |
|
270 | |
public String put(String key, String value) |
271 | |
{ |
272 | 0 | throw new UnsupportedOperationException(this.getClass().getName() + " UnsupportedOperationException"); |
273 | |
} |
274 | |
|
275 | |
public void clear() |
276 | |
{ |
277 | 0 | throw new UnsupportedOperationException(this.getClass().getName() + " UnsupportedOperationException"); |
278 | |
} |
279 | |
|
280 | |
} |
281 | |
|
282 | |
} |