KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > taglib > core > LoadBundleTag


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.taglib.core;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.MissingResourceException JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.faces.component.UIViewRoot;
30 import javax.faces.context.FacesContext;
31 import javax.faces.webapp.UIComponentTag;
32 import javax.servlet.jsp.JspException JavaDoc;
33 import javax.servlet.jsp.tagext.Tag JavaDoc;
34 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 /**
40  * TODO:
41  * We should find a way to save loaded bundles in the state, because otherwise
42  * on the next request the bundle map will not be present before the render phase
43  * and value bindings that reference to the bundle will always log annoying
44  * "Variable 'xxx' could not be resolved" error messages.
45  *
46  * @author Manfred Geiler (latest modification by $Author: matze $)
47  * @version $Revision: 1.10 $ $Date: 2004/10/13 11:51:00 $
48  * $Log: LoadBundleTag.java,v $
49  * Revision 1.10 2004/10/13 11:51:00 matze
50  * renamed packages to org.apache
51  *
52  * Revision 1.9 2004/09/01 18:32:54 mwessendorf
53  * Organize Imports
54  *
55  * Revision 1.8 2004/08/22 10:38:54 mwessendorf
56  * bug #1013489
57  *
58  * Revision 1.7 2004/08/04 18:45:41 grantsmith
59  * renamed 'enum' to 'enumer' to allow compile in JDK 1.5
60  *
61  * Revision 1.6 2004/07/01 22:05:03 mwessendorf
62  * ASF switch
63  *
64  * Revision 1.5 2004/06/14 12:55:23 manolito
65  * Added missing CVS Log comment
66  *
67  */

68 public class LoadBundleTag
69         extends TagSupport JavaDoc
70 {
71     private static final Log log = LogFactory.getLog(LoadBundleTag.class);
72
73     private String JavaDoc _basename;
74     private String JavaDoc _var;
75
76     public void setBasename(String JavaDoc basename)
77     {
78         _basename = basename;
79     }
80
81     public void setVar(String JavaDoc var)
82     {
83         _var = var;
84     }
85
86     public int doStartTag() throws JspException JavaDoc
87     {
88         FacesContext facesContext = FacesContext.getCurrentInstance();
89         if (facesContext == null)
90         {
91             throw new JspException JavaDoc("No faces context?!");
92         }
93
94         UIViewRoot viewRoot = facesContext.getViewRoot();
95         if (viewRoot == null)
96         {
97             throw new JspException JavaDoc("No view root! LoadBundle must be nested inside <f:view> action.");
98         }
99
100         Locale JavaDoc locale = viewRoot.getLocale();
101         if (locale == null)
102         {
103             locale = facesContext.getApplication().getDefaultLocale();
104         }
105         
106         String JavaDoc basename = null;
107         
108         if (_basename!=null) {
109             if (UIComponentTag.isValueReference(_basename)) {
110                 basename = (String JavaDoc)facesContext.getApplication().createValueBinding(_basename).getValue(facesContext);
111             } else {
112                 basename = _basename;
113             }
114         }
115
116         final ResourceBundle JavaDoc bundle;
117         try
118         {
119             bundle = ResourceBundle.getBundle(basename, locale);
120         }
121         catch (MissingResourceException JavaDoc e)
122         {
123             log.error("Resource bundle '" + basename + "' could not be found.");
124             return Tag.SKIP_BODY;
125         }
126
127         facesContext.getExternalContext().getRequestMap().put(_var,
128                                                               new BundleMap(bundle));
129         return Tag.SKIP_BODY;
130     }
131
132
133     private static class BundleMap implements Map JavaDoc
134     {
135         private ResourceBundle JavaDoc _bundle;
136         private List JavaDoc _values;
137
138         public BundleMap(ResourceBundle JavaDoc bundle)
139         {
140             _bundle = bundle;
141         }
142
143         //Optimized methods
144

145         public Object JavaDoc get(Object JavaDoc key)
146         {
147             try {
148                 return _bundle.getObject(key.toString());
149             } catch (Exception JavaDoc e) {
150                 return "MISSING: " + key + " :MISSING";
151             }
152         }
153
154         public boolean isEmpty()
155         {
156             return !_bundle.getKeys().hasMoreElements();
157         }
158
159         public boolean containsKey(Object JavaDoc key)
160         {
161             return _bundle.getObject(key.toString()) != null;
162         }
163
164
165         //Unoptimized methods
166

167         public Collection JavaDoc values()
168         {
169             if (_values == null)
170             {
171                 _values = new ArrayList JavaDoc();
172                 for (Enumeration JavaDoc enumer = _bundle.getKeys(); enumer.hasMoreElements(); )
173                 {
174                     String JavaDoc v = _bundle.getString((String JavaDoc)enumer.nextElement());
175                     _values.add(v);
176                 }
177             }
178             return _values;
179         }
180
181         public int size()
182         {
183             return values().size();
184         }
185
186         public boolean containsValue(Object JavaDoc value)
187         {
188             return values().contains(value);
189         }
190
191         public Set JavaDoc entrySet()
192         {
193             Set JavaDoc set = new HashSet JavaDoc();
194             for (Enumeration JavaDoc enumer = _bundle.getKeys(); enumer.hasMoreElements(); )
195             {
196                 final String JavaDoc k = (String JavaDoc)enumer.nextElement();
197                 set.add(new Map.Entry JavaDoc() {
198                     public Object JavaDoc getKey()
199                     {
200                         return k;
201                     }
202
203                     public Object JavaDoc getValue()
204                     {
205                         return _bundle.getObject(k);
206                     }
207
208                     public Object JavaDoc setValue(Object JavaDoc value)
209                     {
210                         throw new UnsupportedOperationException JavaDoc(this.getClass().getName() + " UnsupportedOperationException");
211                     }
212                 });
213             }
214             return set;
215         }
216
217         public Set JavaDoc keySet()
218         {
219             Set JavaDoc set = new HashSet JavaDoc();
220             for (Enumeration JavaDoc enumer = _bundle.getKeys(); enumer.hasMoreElements(); )
221             {
222                 set.add(enumer.nextElement());
223             }
224             return set;
225         }
226
227
228         //Unsupported methods
229

230         public Object JavaDoc remove(Object JavaDoc key)
231         {
232             throw new UnsupportedOperationException JavaDoc(this.getClass().getName() + " UnsupportedOperationException");
233         }
234
235         public void putAll(Map JavaDoc t)
236         {
237             throw new UnsupportedOperationException JavaDoc(this.getClass().getName() + " UnsupportedOperationException");
238         }
239
240         public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
241         {
242             throw new UnsupportedOperationException JavaDoc(this.getClass().getName() + " UnsupportedOperationException");
243         }
244
245         public void clear()
246         {
247             throw new UnsupportedOperationException JavaDoc(this.getClass().getName() + " UnsupportedOperationException");
248         }
249
250     }
251
252 }
253
Popular Tags