KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > util > bundle > BundleUtils


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.util.bundle;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import javax.faces.context.FacesContext;
22 import javax.faces.el.VariableResolver;
23 import java.util.MissingResourceException JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25
26 /**
27  * DOCUMENT ME!
28  * @author Manfred Geiler (latest modification by $Author: matze $)
29  * @version $Revision: 1.13 $ $Date: 2004/10/13 11:51:01 $
30  */

31 public class BundleUtils
32 {
33     private static final Log log = LogFactory.getLog(BundleUtils.class);
34
35     private BundleUtils()
36     {
37         // hide from public access
38
}
39
40     public static ResourceBundle JavaDoc findResourceBundle(FacesContext facesContext,
41                                                     String JavaDoc bundleName)
42     {
43         //TODO: Could be JSTL LocalizationContext bundle?
44

45         //Lookup as attribute (try different scopes)
46
VariableResolver vr = facesContext.getApplication().getVariableResolver();
47         ResourceBundle JavaDoc bundle = (ResourceBundle JavaDoc)vr.resolveVariable(facesContext, bundleName);
48
49         return bundle;
50     }
51
52     public static String JavaDoc getString(FacesContext facesContext,
53                                    String JavaDoc bundleName, String JavaDoc key)
54     {
55         ResourceBundle JavaDoc bundle = findResourceBundle(facesContext, bundleName);
56         if (bundle != null)
57         {
58             try
59             {
60                 return bundle.getString(key);
61             }
62             catch (MissingResourceException JavaDoc e)
63             {
64                 log.warn("Resource string '" + key + "' in bundle '" + bundleName + "' could not be found.");
65                 return key;
66             }
67         }
68         else
69         {
70             return key;
71         }
72     }
73
74 }
75
Popular Tags