KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > jsf > core > LoadBundleHandler


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.tag.jsf.core;
16
17 import java.io.IOException JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.MissingResourceException JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import javax.el.ELException;
28 import javax.faces.FacesException;
29 import javax.faces.component.UIComponent;
30 import javax.faces.component.UIViewRoot;
31 import javax.faces.context.FacesContext;
32
33 import com.sun.facelets.FaceletContext;
34 import com.sun.facelets.FaceletException;
35 import com.sun.facelets.tag.TagAttribute;
36 import com.sun.facelets.tag.TagAttributeException;
37 import com.sun.facelets.tag.TagConfig;
38 import com.sun.facelets.tag.TagHandler;
39 import com.sun.facelets.tag.jsf.ComponentSupport;
40
41 /**
42  * Load a resource bundle localized for the Locale of the current view, and
43  * expose it (as a Map) in the request attributes of the current request. <p/>
44  * See <a target="_new"
45  * HREF="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/tlddocs/f/loadBundle.html">tag
46  * documentation</a>.
47  *
48  * @author Jacob Hookom
49  * @version $Id: LoadBundleHandler.java,v 1.2 2005/08/24 04:38:50 jhook Exp $
50  */

51 public final class LoadBundleHandler extends TagHandler {
52
53     private final static class ResourceBundleMap implements Map JavaDoc {
54         private final static class ResourceEntry implements Map.Entry JavaDoc {
55
56             protected final String JavaDoc key;
57
58             protected final String JavaDoc value;
59
60             public ResourceEntry(String JavaDoc key, String JavaDoc value) {
61                 this.key = key;
62                 this.value = value;
63             }
64
65             public Object JavaDoc getKey() {
66                 return this.key;
67             }
68
69             public Object JavaDoc getValue() {
70                 return this.value;
71             }
72
73             public Object JavaDoc setValue(Object JavaDoc value) {
74                 throw new UnsupportedOperationException JavaDoc();
75             }
76
77             public int hashCode() {
78                 return this.key.hashCode();
79             }
80
81             public boolean equals(Object JavaDoc obj) {
82                 return (obj instanceof ResourceEntry && this.hashCode() == obj
83                         .hashCode());
84             }
85         }
86
87         protected final ResourceBundle JavaDoc bundle;
88
89         public ResourceBundleMap(ResourceBundle JavaDoc bundle) {
90             this.bundle = bundle;
91         }
92
93         public void clear() {
94             throw new UnsupportedOperationException JavaDoc();
95         }
96
97         public boolean containsKey(Object JavaDoc key) {
98             try {
99                 bundle.getString(key.toString());
100                 return true;
101             } catch (MissingResourceException JavaDoc e) {
102                 return false;
103             }
104         }
105
106         public boolean containsValue(Object JavaDoc value) {
107             throw new UnsupportedOperationException JavaDoc();
108         }
109
110         public Set JavaDoc entrySet() {
111             Enumeration JavaDoc e = this.bundle.getKeys();
112             Set JavaDoc s = new HashSet JavaDoc();
113             String JavaDoc k;
114             while (e.hasMoreElements()) {
115                 k = (String JavaDoc) e.nextElement();
116                 s.add(new ResourceEntry(k, this.bundle.getString(k)));
117             }
118             return s;
119         }
120
121         public Object JavaDoc get(Object JavaDoc key) {
122             return this.bundle.getObject((String JavaDoc) key);
123         }
124
125         public boolean isEmpty() {
126             return false;
127         }
128
129         public Set JavaDoc keySet() {
130             Enumeration JavaDoc e = this.bundle.getKeys();
131             Set JavaDoc s = new HashSet JavaDoc();
132             while (e.hasMoreElements()) {
133                 s.add(e.nextElement());
134             }
135             return s;
136         }
137
138         public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
139             throw new UnsupportedOperationException JavaDoc();
140         }
141
142         public void putAll(Map JavaDoc t) {
143             throw new UnsupportedOperationException JavaDoc();
144         }
145
146         public Object JavaDoc remove(Object JavaDoc key) {
147             throw new UnsupportedOperationException JavaDoc();
148         }
149
150         public int size() {
151             return this.keySet().size();
152         }
153
154         public Collection JavaDoc values() {
155             Enumeration JavaDoc e = this.bundle.getKeys();
156             Set JavaDoc s = new HashSet JavaDoc();
157             while (e.hasMoreElements()) {
158                 s.add(this.bundle.getObject((String JavaDoc) e.nextElement()));
159             }
160             return s;
161         }
162     }
163
164     private final TagAttribute basename;
165
166     private final TagAttribute var;
167
168     /**
169      * @param config
170      */

171     public LoadBundleHandler(TagConfig config) {
172         super(config);
173         this.basename = this.getRequiredAttribute("basename");
174         this.var = this.getRequiredAttribute("var");
175     }
176
177     /**
178      * See taglib documentation.
179      *
180      * @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
181      * javax.faces.component.UIComponent)
182      */

183     public void apply(FaceletContext ctx, UIComponent parent)
184             throws IOException JavaDoc, FacesException, FaceletException, ELException {
185         UIViewRoot root = ComponentSupport.getViewRoot(ctx, parent);
186         ResourceBundle JavaDoc bundle = null;
187         try {
188             String JavaDoc name = this.basename.getValue(ctx);
189             ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
190             if (root != null && root.getLocale() != null) {
191                 bundle = ResourceBundle.getBundle(name, root.getLocale(), cl);
192             } else {
193                 bundle = ResourceBundle
194                         .getBundle(name, Locale.getDefault(), cl);
195             }
196         } catch (Exception JavaDoc e) {
197             throw new TagAttributeException(this.tag, this.basename, e);
198         }
199         ResourceBundleMap map = new ResourceBundleMap(bundle);
200         FacesContext faces = ctx.getFacesContext();
201         faces.getExternalContext().getRequestMap().put(this.var.getValue(ctx),
202                 map);
203     }
204 }
205
Popular Tags