KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > backup > StringHelper


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 package com.sun.enterprise.config.backup;
25
26 import java.util.ResourceBundle JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28
29 /**
30  *
31  * @author bnevins
32  */

33 class StringHelper
34 {
35     private StringHelper()
36     {
37     }
38     
39     /**
40      * @return the String from LocalStrings or the supplied String if it doesn't exist
41      */

42     
43     static String JavaDoc get(String JavaDoc s)
44     {
45         try
46         {
47             return bundle.getString(s);
48         }
49         catch (Exception JavaDoc e)
50         {
51             // it is not an error to have no key...
52
return s;
53         }
54     }
55
56     /**
57      * Convenience method which calls get(String, Object[])
58      * @return the String from LocalStrings or the supplied String if it doesn't exist --
59      * using the one supplied argument
60      * @see get(String, Object[])
61      */

62     static String JavaDoc get(String JavaDoc s, Object JavaDoc o)
63     {
64         return get(s, new Object JavaDoc[] { o });
65     }
66
67     /**
68      * Convenience method which calls get(String, Object[])
69      * @return the String from LocalStrings or the supplied String if it doesn't exist --
70      * using the two supplied arguments
71      * @see get(String, Object[])
72      */

73     static String JavaDoc get(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2)
74     {
75         return get(s, new Object JavaDoc[] { o1, o2 });
76     }
77     
78     /**
79      * Convenience method which calls get(String, Object[])
80      * @return the String from LocalStrings or the supplied String if it doesn't exist --
81      * using the three supplied arguments
82      * @see get(String, Object[])
83      */

84     static String JavaDoc get(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3)
85     {
86         return get(s, new Object JavaDoc[] { o1, o2, o3 });
87     }
88     
89     /**
90      * Get and format a String from LocalStrings.properties
91      * @return the String from LocalStrings or the supplied String if it doesn't exist --
92      * using the array of supplied Object arguments
93      */

94     static String JavaDoc get(String JavaDoc s, Object JavaDoc[] objects)
95     {
96         s = get(s);
97         
98         try
99         {
100             MessageFormat JavaDoc mf = new MessageFormat JavaDoc(s);
101             return mf.format(objects);
102         }
103         catch(Exception JavaDoc e)
104         {
105             return s;
106         }
107     }
108
109     ///////////////////////////////////////////////////////////////////////////
110

111     private static ResourceBundle JavaDoc bundle;
112
113     static
114     {
115         try
116         {
117             String JavaDoc props = StringHelper.class.getPackage().getName() + ".LocalStrings";
118             bundle = ResourceBundle.getBundle(props);
119         }
120         catch (Exception JavaDoc e)
121         {
122             LoggerHelper.warning("No resource bundle found: " + Constants.exceptionResourceBundle, e);
123             bundle = null;
124         }
125     }
126     
127     static void main(String JavaDoc[] notUsed)
128     {
129         System.out.println("key=backup-res.BadProjectBackupDir, value =" + get("backup-res.BadProjectBackupDir"));
130     }
131 }
132
133
134
Popular Tags