2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.shale.clay.component;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Enumeration;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.ResourceBundle;
31 import java.util.Set;
32
33 import javax.faces.component.UIComponentBase;
34 import javax.faces.context.FacesContext;
35
36 import org.apache.shale.util.Tags;
37
38 /***
39 * <p>Component counterpart of the standard loadBundle tag. Since it's a component
40 * it can be used with HTML templates.</p>
41 */
42 public class LoadBundle extends UIComponentBase {
43 /***
44 * Base name of the resource bundle to be loaded.
45 */
46 private String basename = null;
47
48 /***
49 * Name of a request scope attribute under which the resource bundle will be
50 * exposed as a Map.
51 */
52 private String var = null;
53
54 /***
55 * @return component family, <code>null</code>
56 */
57 public String getFamily() {
58 return null;
59 }
60
61
62 /***
63 * <p>Shale tag helper class that contains utility methods for setting
64 * component binding and method properties.</p>
65 */
66 private Tags tagUtils = new Tags();
67
68
69 /***
70 * <p>Sets the base name of the resource bundle to be loaded.</p>
71 *
72 * @param basename resource bundle name
73 */
74 public void setBasename(String basename) {
75 this.basename = basename;
76 if ((var != null) && (basename != null)) {
77 loadBundle();
78 }
79 }
80
81 /***
82 * <p>Sets the name of a request scope attribute under which the resource
83 * bundle will be exposed as a Map.</p>
84 *
85 * @param var session scoped attribute
86 */
87 public void setVar(String var) {
88 this.var = var;
89 if ((var != null) && (basename != null)) {
90 loadBundle();
91 }
92 }
93
94 /***
95 * <p>Load the resource bundle and expose it in the request.</p>
96 */
97 private void loadBundle() {
98
99 FacesContext context = FacesContext.getCurrentInstance();
100 if (context == null) {
101 throw new NullPointerException("No faces context!");
102 }
103
104
105 ClassLoader classLoader = Thread.currentThread()
106 .getContextClassLoader();
107 if (classLoader == null) {
108 classLoader = getClass().getClassLoader();
109 }
110
111
112 String resolvedBasename = tagUtils.evalString(basename);
113
114 if (null == resolvedBasename || null == var) {
115 throw new NullPointerException("null basename or var");
116 }
117
118 final ResourceBundle bundle = ResourceBundle.getBundle(basename,
119 context.getViewRoot().getLocale(), classLoader);
120 if (null == bundle) {
121 throw new NullPointerException("No ResourceBundle for " + basename);
122 }
123
124 context.getExternalContext().getRequestMap().put(var, new BundleMap(bundle));
125
126 }
127
128 /***
129 * <p>Invokes rendering of the component.</p>
130 * @param context faces context
131 * @exception IOException response writer
132 */
133 public void encodeBegin(FacesContext context) throws IOException {
134
135 setTransient(true);
136
137 super.encodeBegin(context);
138 }
139
140 /***
141 * <p>Inner class that wrappers a <code>ResourceBundle</code>.</p>
142 */
143 private static class BundleMap implements Map {
144 /***
145 * <p>Wrappered resource bundle.</p>
146 */
147 private ResourceBundle bundle;
148
149 /***
150 * <p>Stores the bundle keys.</p>
151 */
152 private List values;
153
154 /***
155 * @param bundle decorated resource bundle
156 */
157 public BundleMap(ResourceBundle bundle) {
158 this.bundle = bundle;
159 }
160
161 /***
162 * @param key bundle resource key
163 * @return bundle string resource value
164 */
165 public Object get(Object key) {
166 try {
167 return bundle.getObject(key.toString());
168 } catch (Exception e) {
169 return "???" + key + "???";
170 }
171 }
172
173 /***
174 * @return <code>true</code> if the bundle is empty
175 */
176 public boolean isEmpty() {
177 return !bundle.getKeys().hasMoreElements();
178 }
179
180 /***
181 * @param key bundle resource key
182 * @return <code>true</code> if the bundle contains a key
183 */
184 public boolean containsKey(Object key) {
185 return bundle.getObject(key.toString()) != null;
186 }
187
188 /***
189 * @return collection of bundle resource values
190 */
191 public Collection values() {
192 if (values == null) {
193 values = new ArrayList();
194 for (Enumeration enumer = bundle.getKeys(); enumer
195 .hasMoreElements();) {
196 String v = bundle.getString((String) enumer.nextElement());
197 values.add(v);
198 }
199 }
200 return values;
201 }
202
203 /***
204 * @return number of keys in the bundle
205 */
206 public int size() {
207 return values().size();
208 }
209
210 /***
211 * @param value bundle value string
212 * @return <code>true</code> if value is found in the bundle
213 */
214 public boolean containsValue(Object value) {
215 return values().contains(value);
216 }
217
218 /***
219 * @return set of objects implementing <code>Map.Entry</code>
220 */
221 public Set entrySet() {
222 Set set = new HashSet();
223 for (Enumeration enumer = bundle.getKeys(); enumer
224 .hasMoreElements();) {
225 final String k = (String) enumer.nextElement();
226 set.add(new Map.Entry() {
227 public Object getKey() {
228 return k;
229 }
230
231 public Object getValue() {
232 return bundle.getObject(k);
233 }
234
235 public Object setValue(Object value) {
236 throw new UnsupportedOperationException(this.getClass()
237 .getName()
238 + " UnsupportedOperationException");
239 }
240 });
241 }
242 return set;
243 }
244
245 /***
246 * @return set of resource bundle keys
247 */
248 public Set keySet() {
249 Set set = new HashSet();
250 for (Enumeration enumer = bundle.getKeys(); enumer
251 .hasMoreElements();) {
252 set.add(enumer.nextElement());
253 }
254 return set;
255 }
256
257
258 /***
259 * @param key unsupported
260 * @return unsupported
261 */
262 public Object remove(Object key) {
263 throw new UnsupportedOperationException(this.getClass().getName()
264 + " UnsupportedOperationException");
265 }
266
267 /***
268 * @param t unsupported
269 */
270 public void putAll(Map t) {
271 throw new UnsupportedOperationException(this.getClass().getName()
272 + " UnsupportedOperationException");
273 }
274
275 /***
276 * @param key unsupported
277 * @param value unsupported
278 * @return unsupported
279 */
280 public Object put(Object key, Object value) {
281 throw new UnsupportedOperationException(this.getClass().getName()
282 + " UnsupportedOperationException");
283 }
284
285 /***
286 * Unsupported <code>Map</code> method.
287 */
288 public void clear() {
289 throw new UnsupportedOperationException(this.getClass().getName()
290 + " UnsupportedOperationException");
291 }
292
293 }
294 }