KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > LocalStrings


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
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 package com.sun.enterprise.util;
30
31 import java.util.ResourceBundle JavaDoc;
32 import java.text.MessageFormat JavaDoc;
33
34 /**
35  * This class makes getting localized strings super-simple. All methods are static.
36  * The reason is that that makes it much simpler to use -- you never need to create an
37  * instance and store it. You simply call one of the 2 methods directly. However,
38  * there is a performance penalty for this convenience. This class has to figure out
39  * what package your calling code is in (every time). My reasoning is that the emitting
40  * of log messages tends to be much less frequent than other normal processing steps.
41  * If performance is an issue -- use an instance of LocalStringsImpl.
42  * <p>Specifics:
43  * <ul>
44  * <li>Your calling code should have a file named LocalStrings.properties in its
45  * package directory.
46  * <li>If your localized string has no arguments call get(String) to get the localized
47  * String value.
48  * <li>If you have a parameterized string, call get(String, Object...)
49  * </ul>
50  * <p>Note: <b>You can not get an Exception out of calling this code!</b> If the String
51  * or the properties file does not exist, it will return the String that you gave
52  * in the first place as the argument.
53  * <p>Examples:
54  * <ul>
55  * <li>String s = LocalStrings.get("xyz");
56  * <li>String s = LocalStrings.get("xyz", new Date(), 500, "something", 2.00003);
57  * <li>String s = LocalStrings.get("xyz", "something", "foo", "whatever");
58  * </ul>
59  *
60  *
61  *
62  * @author bnevins
63  */

64 public class LocalStrings
65 {
66     private LocalStrings()
67     {
68     }
69     
70     /**
71      * Get a String from the caller's package's LocalStrings.properties
72      * @param indexString The string index into the localized string file
73      * @return the String from LocalStrings or the supplied String if it doesn't exist
74      */

75     
76     public static String JavaDoc get(String JavaDoc indexString)
77     {
78         return new LocalStringsImpl().get(indexString);
79     }
80     
81     /**
82      * Get and format a String from the caller's package's LocalStrings.properties
83      * @param indexString The string index into the localized string file
84      * @param objects The arguments to give to MessageFormat
85      * @return the String from LocalStrings or the supplied String if it doesn't exist --
86      * using the array of supplied Object arguments
87      */

88     public static String JavaDoc get(String JavaDoc indexString, Object JavaDoc... objects)
89     {
90         return new LocalStringsImpl().get(indexString, objects);
91     }
92     
93     /**
94      * Get a String from the caller's package's LocalStrings.properties
95      * @param indexString The string index into the localized string file
96      * @return the String from LocalStrings or the supplied default value if it doesn't exist
97      */

98     public String JavaDoc getString(String JavaDoc indexString, String JavaDoc defaultValue)
99     {
100         return new LocalStringsImpl().get(indexString, defaultValue);
101     }
102     
103     /**
104      * Get an integer from the caller's package's LocalStrings.properties
105      * @param indexString The string index into the localized string file
106      * @return the integer value from LocalStrings or the supplied default if
107      * it doesn't exist or is bad.
108      */

109     
110     public static int getInt(String JavaDoc indexString, int defaultValue)
111     {
112         return new LocalStringsImpl().getInt(indexString, defaultValue);
113     }
114     
115     /**
116      * Get a boolean from the caller's package's LocalStrings.properties
117      * @param indexString The string index into the localized string file
118      * @return the integer value from LocalStrings or the supplied default if
119      * it doesn't exist or is bad.
120      */

121     public boolean getBoolean(String JavaDoc indexString, boolean defaultValue)
122     {
123         return new LocalStringsImpl().getBoolean(indexString, defaultValue);
124     }
125 }
126
Popular Tags