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