KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > msg > MessageFormat


1 /*******************************************************************************
2  * Copyright (c) 2003, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.msg;
13
14 import java.security.AccessController JavaDoc;
15 import java.security.PrivilegedAction JavaDoc;
16 import java.util.*;
17
18 public class MessageFormat {
19
20     // ResourceBundle holding the messages.
21
protected ResourceBundle bundle;
22     protected Locale locale;
23
24     public MessageFormat(String JavaDoc bundleName) {
25         init(bundleName, Locale.getDefault(), this.getClass());
26     }
27
28     public MessageFormat(String JavaDoc bundleName, Locale locale) {
29         init(bundleName, locale, this.getClass());
30     }
31
32     public MessageFormat(String JavaDoc bundleName, Locale locale, Class JavaDoc clazz) {
33         init(bundleName, locale, clazz);
34     }
35
36     protected void init(final String JavaDoc bundleName, final Locale locale, final Class JavaDoc clazz) {
37         bundle = (ResourceBundle) AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
38             public Object JavaDoc run() {
39                 ClassLoader JavaDoc loader = clazz.getClassLoader();
40
41                 if (loader == null) {
42                     loader = ClassLoader.getSystemClassLoader();
43                 }
44
45                 try {
46                     return ResourceBundle.getBundle(bundleName, locale, loader);
47                 } catch (MissingResourceException e) {
48                     return null;
49                 }
50             }
51         });
52
53         this.locale = locale;
54     }
55
56     /**
57      * Return the Locale object used for this MessageFormat object.
58      * @return Locale of this object.
59      */

60     public Locale getLocale() {
61         return locale;
62     }
63
64     /**
65      * Retrieves a message which has no arguments.
66      * @param msg String
67      * the key to look up.
68      * @return String
69      * the message for that key in the system
70      * message bundle.
71      */

72     public String JavaDoc getString(String JavaDoc msg) {
73         if (bundle == null) {
74             return msg;
75         }
76
77         try {
78             return bundle.getString(msg);
79         } catch (MissingResourceException e) {
80             return msg;
81         }
82     }
83
84     /**
85      * Retrieves a message which takes 1 argument.
86      * @param msg String
87      * the key to look up.
88      * @param arg Object
89      * the object to insert in the formatted output.
90      * @return String
91      * the message for that key in the system
92      * message bundle.
93      */

94     public String JavaDoc getString(String JavaDoc msg, Object JavaDoc arg) {
95         return getString(msg, new Object JavaDoc[] {arg});
96     }
97
98     /**
99      * Retrieves a message which takes 1 integer argument.
100      * @param msg String
101      * the key to look up.
102      * @param arg int
103      * the integer to insert in the formatted output.
104      * @return String
105      * the message for that key in the system
106      * message bundle.
107      */

108     public String JavaDoc getString(String JavaDoc msg, int arg) {
109         return getString(msg, new Object JavaDoc[] {Integer.toString(arg)});
110     }
111
112     /**
113      * Retrieves a message which takes 1 character argument.
114      * @param msg String
115      * the key to look up.
116      * @param arg char
117      * the character to insert in the formatted output.
118      * @return String
119      * the message for that key in the system
120      * message bundle.
121      */

122     public String JavaDoc getString(String JavaDoc msg, char arg) {
123         return getString(msg, new Object JavaDoc[] {String.valueOf(arg)});
124     }
125
126     /**
127      * Retrieves a message which takes 2 arguments.
128      * @param msg String
129      * the key to look up.
130      * @param arg1 Object
131      * an object to insert in the formatted output.
132      * @param arg2 Object
133      * another object to insert in the formatted output.
134      * @return String
135      * the message for that key in the system
136      * message bundle.
137      */

138     public String JavaDoc getString(String JavaDoc msg, Object JavaDoc arg1, Object JavaDoc arg2) {
139         return getString(msg, new Object JavaDoc[] {arg1, arg2});
140     }
141
142     /**
143      * Retrieves a message which takes several arguments.
144      * @param msg String
145      * the key to look up.
146      * @param args Object[]
147      * the objects to insert in the formatted output.
148      * @return String
149      * the message for that key in the system
150      * message bundle.
151      */

152     public String JavaDoc getString(String JavaDoc msg, Object JavaDoc[] args) {
153         String JavaDoc format = msg;
154
155         if (bundle != null) {
156             try {
157                 format = bundle.getString(msg);
158             } catch (MissingResourceException e) {
159             }
160         }
161
162         return format(format, args);
163     }
164
165     /**
166      * Generates a formatted text string given a source string
167      * containing "argument markers" of the form "{argNum}"
168      * where each argNum must be in the range 0..9. The result
169      * is generated by inserting the toString of each argument
170      * into the position indicated in the string.
171      * <p>
172      * To insert the "{" character into the output, use a single
173      * backslash character to escape it (i.e. "\{"). The "}"
174      * character does not need to be escaped.
175      * @param format String
176      * the format to use when printing.
177      * @param args Object[]
178      * the arguments to use.
179      * @return String
180      * the formatted message.
181      */

182     public static String JavaDoc format(String JavaDoc format, Object JavaDoc[] args) {
183         StringBuffer JavaDoc answer = new StringBuffer JavaDoc();
184         String JavaDoc[] argStrings = new String JavaDoc[args.length];
185
186         for (int i = 0; i < args.length; ++i) {
187             argStrings[i] = args[i] == null ? "<null>" : args[i].toString(); //$NON-NLS-1$
188
}
189
190         int lastI = 0;
191
192         for (int i = format.indexOf('{', 0); i >= 0; i = format.indexOf('{', lastI)) {
193             if (i != 0 && format.charAt(i - 1) == '\\') {
194                 // It's escaped, just print and loop.
195
if (i != 1) {
196                     answer.append(format.substring(lastI, i - 1));
197                 }
198                 answer.append('{');
199                 lastI = i + 1;
200             } else {
201                 // It's a format character.
202
if (i > format.length() - 3) {
203                     // Bad format, just print and loop.
204
answer.append(format.substring(lastI, format.length()));
205                     lastI = format.length();
206                 } else {
207                     int argnum = (byte) Character.digit(format.charAt(i + 1), 10);
208                     if (argnum < 0 || format.charAt(i + 2) != '}') {
209                         // Bad format, just print and loop.
210
answer.append(format.substring(lastI, i + 1));
211                         lastI = i + 1;
212                     } else {
213                         // Got a good one!
214
answer.append(format.substring(lastI, i));
215                         if (argnum >= argStrings.length) {
216                             answer.append("<missing argument>"); //$NON-NLS-1$
217
} else {
218                             answer.append(argStrings[argnum]);
219                         }
220                         lastI = i + 3;
221                     }
222                 }
223             }
224         }
225
226         if (lastI < format.length()) {
227             answer.append(format.substring(lastI, format.length()));
228         }
229
230         return answer.toString();
231     }
232 }
Popular Tags