KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > LocalStringsManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.admin.util;
24
25 import java.util.ResourceBundle JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28
29
30 /**
31  * Implementation of a local string manager.
32  * Provides access to i18n messages for classes that need them.
33  * Now emulates the proper sun.enterprise.util.LocalStringManagerImpl
34  * with few essential differences:
35  * <br>1. It uses package hierarchy, but not classes hierarchy for resource files searching(it allows to place resources separatly from source files)
36  * <br>2. You can choose different resource file names (not only LocalName.properties);
37  * <br>3. New optional mode - with fixedResourceBundle allows to exclude bundle file search repeating during the subsequent getString() calls;
38  */

39
40 public class LocalStringsManager
41 {
42     static final String JavaDoc DEFAULT_PROPERTY_FILE_NAME = "LocalStrings";
43
44     private String JavaDoc m_propertyFileName;
45     private String JavaDoc m_basePackage;
46     private ResourceBundle JavaDoc m_FixedResourceBundle;
47     
48     /**
49      * Create a string manager that looks for LocalStrings.properties in
50      * the package of the basePackage.
51      * @param basePackage Most high level package Class path with localized strings
52      */

53     public LocalStringsManager(String JavaDoc basePackage, String JavaDoc propertyFileName)
54     {
55         m_basePackage = basePackage;
56         m_propertyFileName = propertyFileName;
57     }
58     
59     public LocalStringsManager(String JavaDoc basePackage)
60     {
61         this(basePackage, DEFAULT_PROPERTY_FILE_NAME);
62     }
63
64     /**
65      * Get a localized string.
66      * @param startPackage The package name which defines the starting directory for resource bundle serach.
67      * @param key The name of the resource to fetch
68      * @param defaultValue The default return value if not found
69      * @return The localized value for the resource
70      */

71     public String JavaDoc getString(String JavaDoc startPackage, String JavaDoc key, String JavaDoc defaultValue)
72     {
73         if(m_FixedResourceBundle!=null)
74         {
75             String JavaDoc value = m_FixedResourceBundle.getString(key);
76             if(value!=null)
77                return value;
78             return defaultValue;
79         }
80                
81         
82         String JavaDoc stopPackage = m_basePackage;
83         if(startPackage==null)
84             startPackage = stopPackage;
85         else
86             if(!startPackage.startsWith(m_basePackage))
87                stopPackage = startPackage; //maybe = ""
88
ResourceBundle JavaDoc resources = null;
89
90         while (startPackage.length()>0 && startPackage.length()>=stopPackage.length())
91         {
92             try
93             {
94                 String JavaDoc resFileName = startPackage+"."+m_propertyFileName;
95                 startPackage = startPackage.substring(0, startPackage.lastIndexOf("."));
96                 resources = ResourceBundle.getBundle(resFileName);
97                 if ( resources != null )
98                 {
99                     String JavaDoc value = resources.getString(key);
100                     if ( value != null )
101                         return value;
102                 }
103             } catch (Exception JavaDoc ex)
104             {
105             }
106         }
107         
108         // Look for a global resource (defined by defaultClass)
109
if ( !stopPackage.equals(m_basePackage) )
110         {
111             return getString(null, key, defaultValue);
112         }
113
114         return defaultValue;
115     }
116     
117     /**
118      * Get a localized string from the base package.
119      * @param key The name of the resource to fetch
120      * @param defaultValue The default return value if not found
121      * @return The localized string
122      */

123     public String JavaDoc getString(String JavaDoc key, String JavaDoc defaultValue)
124     {
125         return getString(null, key, defaultValue);
126     }
127     
128     /**
129      * Get a local string for the caller and format the arguments accordingly.
130      * @param startPackage The package name which defines the starting directory for resource bundle serach.
131      * @param key The key to the local format string
132      * @param fmt The default format if not found in the resources
133      * @param arguments The set of arguments to provide to the formatter
134      * @return A formatted localized string
135      */

136     
137     public String JavaDoc getString(
138     String JavaDoc startPackage,
139     String JavaDoc key,
140     String JavaDoc defaultFormat,
141     Object JavaDoc arguments[]
142     )
143     {
144         MessageFormat JavaDoc f = new MessageFormat JavaDoc(
145         getString(startPackage, key, defaultFormat));
146         for (int i = 0; i < arguments.length; i++)
147         {
148             if ( arguments[i] == null )
149             {
150                 arguments[i] = "null";
151             } else if ( !(arguments[i] instanceof String JavaDoc) &&
152             !(arguments[i] instanceof Number JavaDoc) &&
153             !(arguments[i] instanceof java.util.Date JavaDoc))
154             {
155                 arguments[i] = arguments[i].toString();
156             }
157         }
158         return f.format(arguments);
159     }
160     
161     /**
162      * Get a local string from the base package and
163      * format the arguments accordingly.
164      * @param key The key to the local format string
165      * @param defaultFormat The default format if not found in the resources
166      * @param arguments The set of arguments to provide to the formatter
167      * @return A formatted localized string
168      */

169     public String JavaDoc getString(
170     String JavaDoc key,
171     String JavaDoc defaultFormat,
172     Object JavaDoc arguments[]
173     )
174     {
175         return getString(null, key, defaultFormat, arguments);
176     }
177
178     protected void setFixedResourceBundle(String JavaDoc startPackage)
179     {
180         if(startPackage==null)
181         {
182             m_FixedResourceBundle = null;
183             return;
184         }
185         
186         String JavaDoc stopPackage = m_basePackage;
187         if(startPackage==null)
188             startPackage = stopPackage;
189         else
190             if(!startPackage.startsWith(m_basePackage))
191                stopPackage = startPackage; //maybe = ""
192
ResourceBundle JavaDoc resources = null;
193
194         while (startPackage.length()>0 && startPackage.length()>=stopPackage.length())
195         {
196             try
197             {
198                 String JavaDoc resFileName = startPackage+"."+m_propertyFileName;
199                 startPackage = startPackage.substring(0, startPackage.lastIndexOf("."));
200                 resources = ResourceBundle.getBundle(resFileName);
201                 if ( resources != null )
202                 {
203                    m_FixedResourceBundle = resources;
204                    return;
205                 }
206             } catch (Exception JavaDoc ex)
207             {
208             }
209         }
210         
211         // Look for a global resource (defined by defaultClass)
212
if ( !stopPackage.equals(m_basePackage) )
213         {
214             setFixedResourceBundle(m_basePackage);
215         }
216
217     }
218 }
219
220
Popular Tags