View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Load a resource bundle localized for the Locale of the current view, and expose it (as a Map) in the request
49   * attributes of the current request. <p/> See <a target="_new"
50   * href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/tlddocs/f/loadBundle.html">tag documentation</a>.
51   * 
52   * @author Jacob Hookom
53   * @version $Id: LoadBundleHandler.java 1187700 2011-10-22 12:19:37Z bommel $
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      private final static class ResourceBundleMap implements Map<String, String>
63      {
64          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              {
73                  this.key = key;
74                  this.value = value;
75              }
76  
77              public String getKey()
78              {
79                  return this.key;
80              }
81  
82              public String getValue()
83              {
84                  return this.value;
85              }
86  
87              public String setValue(String value)
88              {
89                  throw new UnsupportedOperationException();
90              }
91  
92              public int hashCode()
93              {
94                  return this.key.hashCode();
95              }
96  
97              public boolean equals(Object obj)
98              {
99                  return (obj instanceof ResourceEntry && this.hashCode() == obj.hashCode());
100             }
101         }
102 
103         protected final ResourceBundle bundle;
104 
105         public ResourceBundleMap(ResourceBundle bundle)
106         {
107             this.bundle = bundle;
108         }
109 
110         public void clear()
111         {
112             throw new UnsupportedOperationException();
113         }
114 
115         public boolean containsKey(Object key)
116         {
117             try
118             {
119                 bundle.getString(key.toString());
120                 return true;
121             }
122             catch (MissingResourceException e)
123             {
124                 return false;
125             }
126         }
127 
128         public boolean containsValue(Object value)
129         {
130             throw new UnsupportedOperationException();
131         }
132 
133         public Set<Map.Entry<String, String>> entrySet()
134         {
135             Enumeration<String> e = this.bundle.getKeys();
136             Set<Map.Entry<String, String>> s = new HashSet<Map.Entry<String, String>>();
137             String k;
138             while (e.hasMoreElements())
139             {
140                 k = e.nextElement();
141                 s.add(new ResourceEntry(k, this.bundle.getString(k)));
142             }
143             return s;
144         }
145 
146         public String get(Object key)
147         {
148             try
149             {
150                 return this.bundle.getString((String) key);
151             }
152             catch (java.util.MissingResourceException mre)
153             {
154                 return "???" + key + "???";
155             }
156         }
157 
158         public boolean isEmpty()
159         {
160             return false;
161         }
162 
163         public Set<String> keySet()
164         {
165             Enumeration<String> e = this.bundle.getKeys();
166             Set<String> s = new HashSet<String>();
167             while (e.hasMoreElements())
168             {
169                 s.add(e.nextElement());
170             }
171             return s;
172         }
173 
174         public String put(String key, String value)
175         {
176             throw new UnsupportedOperationException();
177         }
178 
179         public void putAll(Map<? extends String, ? extends String> t)
180         {
181             throw new UnsupportedOperationException();
182         }
183 
184         public String remove(Object key)
185         {
186             throw new UnsupportedOperationException();
187         }
188 
189         public int size()
190         {
191             return this.keySet().size();
192         }
193 
194         public Collection<String> values()
195         {
196             Enumeration<String> e = this.bundle.getKeys();
197             Set<String> s = new HashSet<String>();
198             while (e.hasMoreElements())
199             {
200                 s.add(this.bundle.getString(e.nextElement()));
201             }
202             return s;
203         }
204     }
205 
206     private final TagAttribute basename;
207 
208     private final TagAttribute var;
209 
210     /**
211      * @param config
212      */
213     public LoadBundleHandler(TagConfig config)
214     {
215         super(config);
216         this.basename = this.getRequiredAttribute("basename");
217         this.var = this.getRequiredAttribute("var");
218     }
219 
220     /**
221      * See taglib documentation.
222      * 
223      * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext, javax.faces.component.UIComponent)
224      */
225     public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
226             ELException
227     {
228         UIViewRoot root = ComponentSupport.getViewRoot(ctx, parent);
229         ResourceBundle bundle = null;
230         try
231         {
232             String name = this.basename.getValue(ctx);
233             ClassLoader cl = ClassUtils.getContextClassLoader();
234             if (root != null && root.getLocale() != null)
235             {
236                 bundle = ResourceBundle.getBundle(name, root.getLocale(), cl);
237             }
238             else
239             {
240                 bundle = ResourceBundle.getBundle(name, Locale.getDefault(), cl);
241             }
242         }
243         catch (Exception e)
244         {
245             throw new TagAttributeException(this.tag, this.basename, e);
246         }
247         ResourceBundleMap map = new ResourceBundleMap(bundle);
248         FacesContext faces = ctx.getFacesContext();
249         faces.getExternalContext().getRequestMap().put(this.var.getValue(ctx), map);
250     }
251 }