1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.view.facelets.tag.jsf.core; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.util.Collection; |
23 | |
import java.util.Enumeration; |
24 | |
import java.util.HashSet; |
25 | |
import java.util.Locale; |
26 | |
import java.util.Map; |
27 | |
import java.util.MissingResourceException; |
28 | |
import java.util.ResourceBundle; |
29 | |
import java.util.Set; |
30 | |
|
31 | |
import javax.el.ELException; |
32 | |
import javax.faces.FacesException; |
33 | |
import javax.faces.component.UIComponent; |
34 | |
import javax.faces.component.UIViewRoot; |
35 | |
import javax.faces.context.FacesContext; |
36 | |
import javax.faces.view.facelets.FaceletContext; |
37 | |
import javax.faces.view.facelets.FaceletException; |
38 | |
import javax.faces.view.facelets.TagAttribute; |
39 | |
import javax.faces.view.facelets.TagAttributeException; |
40 | |
import javax.faces.view.facelets.TagConfig; |
41 | |
import javax.faces.view.facelets.TagHandler; |
42 | |
|
43 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag; |
44 | |
import org.apache.myfaces.shared.util.ClassUtils; |
45 | |
import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
@JSFFaceletTag( |
56 | |
name = "f:loadBundle", |
57 | |
bodyContent = "empty", |
58 | |
tagClass="org.apache.myfaces.taglib.core.LoadBundleTag") |
59 | |
public final class LoadBundleHandler extends TagHandler |
60 | |
{ |
61 | |
|
62 | 0 | private final static class ResourceBundleMap implements Map<String, String> |
63 | |
{ |
64 | 0 | private final static class ResourceEntry implements Map.Entry<String, String> |
65 | |
{ |
66 | |
|
67 | |
protected final String key; |
68 | |
|
69 | |
protected final String value; |
70 | |
|
71 | |
public ResourceEntry(String key, String value) |
72 | 0 | { |
73 | 0 | this.key = key; |
74 | 0 | this.value = value; |
75 | 0 | } |
76 | |
|
77 | |
public String getKey() |
78 | |
{ |
79 | 0 | return this.key; |
80 | |
} |
81 | |
|
82 | |
public String getValue() |
83 | |
{ |
84 | 0 | return this.value; |
85 | |
} |
86 | |
|
87 | |
public String setValue(String value) |
88 | |
{ |
89 | 0 | throw new UnsupportedOperationException(); |
90 | |
} |
91 | |
|
92 | |
public int hashCode() |
93 | |
{ |
94 | 0 | return this.key.hashCode(); |
95 | |
} |
96 | |
|
97 | |
public boolean equals(Object obj) |
98 | |
{ |
99 | 0 | return (obj instanceof ResourceEntry && this.hashCode() == obj.hashCode()); |
100 | |
} |
101 | |
} |
102 | |
|
103 | |
protected final ResourceBundle bundle; |
104 | |
|
105 | |
public ResourceBundleMap(ResourceBundle bundle) |
106 | 0 | { |
107 | 0 | this.bundle = bundle; |
108 | 0 | } |
109 | |
|
110 | |
public void clear() |
111 | |
{ |
112 | 0 | throw new UnsupportedOperationException(); |
113 | |
} |
114 | |
|
115 | |
public boolean containsKey(Object key) |
116 | |
{ |
117 | |
try |
118 | |
{ |
119 | 0 | bundle.getString(key.toString()); |
120 | 0 | return true; |
121 | |
} |
122 | 0 | catch (MissingResourceException e) |
123 | |
{ |
124 | 0 | return false; |
125 | |
} |
126 | |
} |
127 | |
|
128 | |
public boolean containsValue(Object value) |
129 | |
{ |
130 | 0 | throw new UnsupportedOperationException(); |
131 | |
} |
132 | |
|
133 | |
public Set<Map.Entry<String, String>> entrySet() |
134 | |
{ |
135 | 0 | Enumeration<String> e = this.bundle.getKeys(); |
136 | 0 | Set<Map.Entry<String, String>> s = new HashSet<Map.Entry<String, String>>(); |
137 | |
String k; |
138 | 0 | while (e.hasMoreElements()) |
139 | |
{ |
140 | 0 | k = e.nextElement(); |
141 | 0 | s.add(new ResourceEntry(k, this.bundle.getString(k))); |
142 | |
} |
143 | 0 | return s; |
144 | |
} |
145 | |
|
146 | |
public String get(Object key) |
147 | |
{ |
148 | |
try |
149 | |
{ |
150 | 0 | return this.bundle.getString((String) key); |
151 | |
} |
152 | 0 | catch (java.util.MissingResourceException mre) |
153 | |
{ |
154 | 0 | return "???" + key + "???"; |
155 | |
} |
156 | |
} |
157 | |
|
158 | |
public boolean isEmpty() |
159 | |
{ |
160 | 0 | return false; |
161 | |
} |
162 | |
|
163 | |
public Set<String> keySet() |
164 | |
{ |
165 | 0 | Enumeration<String> e = this.bundle.getKeys(); |
166 | 0 | Set<String> s = new HashSet<String>(); |
167 | 0 | while (e.hasMoreElements()) |
168 | |
{ |
169 | 0 | s.add(e.nextElement()); |
170 | |
} |
171 | 0 | return s; |
172 | |
} |
173 | |
|
174 | |
public String put(String key, String value) |
175 | |
{ |
176 | 0 | throw new UnsupportedOperationException(); |
177 | |
} |
178 | |
|
179 | |
public void putAll(Map<? extends String, ? extends String> t) |
180 | |
{ |
181 | 0 | throw new UnsupportedOperationException(); |
182 | |
} |
183 | |
|
184 | |
public String remove(Object key) |
185 | |
{ |
186 | 0 | throw new UnsupportedOperationException(); |
187 | |
} |
188 | |
|
189 | |
public int size() |
190 | |
{ |
191 | 0 | return this.keySet().size(); |
192 | |
} |
193 | |
|
194 | |
public Collection<String> values() |
195 | |
{ |
196 | 0 | Enumeration<String> e = this.bundle.getKeys(); |
197 | 0 | Set<String> s = new HashSet<String>(); |
198 | 0 | while (e.hasMoreElements()) |
199 | |
{ |
200 | 0 | s.add(this.bundle.getString(e.nextElement())); |
201 | |
} |
202 | 0 | return s; |
203 | |
} |
204 | |
} |
205 | |
|
206 | |
private final TagAttribute basename; |
207 | |
|
208 | |
private final TagAttribute var; |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
public LoadBundleHandler(TagConfig config) |
214 | |
{ |
215 | 0 | super(config); |
216 | 0 | this.basename = this.getRequiredAttribute("basename"); |
217 | 0 | this.var = this.getRequiredAttribute("var"); |
218 | 0 | } |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, |
226 | |
ELException |
227 | |
{ |
228 | 0 | UIViewRoot root = ComponentSupport.getViewRoot(ctx, parent); |
229 | 0 | ResourceBundle bundle = null; |
230 | |
try |
231 | |
{ |
232 | 0 | String name = this.basename.getValue(ctx); |
233 | 0 | ClassLoader cl = ClassUtils.getContextClassLoader(); |
234 | 0 | if (root != null && root.getLocale() != null) |
235 | |
{ |
236 | 0 | bundle = ResourceBundle.getBundle(name, root.getLocale(), cl); |
237 | |
} |
238 | |
else |
239 | |
{ |
240 | 0 | bundle = ResourceBundle.getBundle(name, Locale.getDefault(), cl); |
241 | |
} |
242 | |
} |
243 | 0 | catch (Exception e) |
244 | |
{ |
245 | 0 | throw new TagAttributeException(this.tag, this.basename, e); |
246 | 0 | } |
247 | 0 | ResourceBundleMap map = new ResourceBundleMap(bundle); |
248 | 0 | FacesContext faces = ctx.getFacesContext(); |
249 | 0 | faces.getExternalContext().getRequestMap().put(this.var.getValue(ctx), map); |
250 | 0 | } |
251 | |
} |