KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > jdo > I18NHelper


1 // Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
2
// Jad home page: http://www.geocities.com/kpdus/jad.html
3
// Decompiler options: packimports(3) noctor space
4
// Source File Name: I18NHelper.java
5

6 package javax.jdo;
7
8 import java.security.AccessController JavaDoc;
9 import java.security.PrivilegedAction JavaDoc;
10 import java.text.MessageFormat JavaDoc;
11 import java.util.*;
12
13 /**
14  * @deprecated Class I18NHelper is deprecated
15  */

16
17 public class I18NHelper {
18
19     private I18NHelper() {
20         bundle = null;
21     }
22
23     private I18NHelper(String JavaDoc bundleName, ClassLoader JavaDoc loader) {
24         bundle = null;
25         bundle = loadBundle(bundleName, loader);
26     }
27
28     public static I18NHelper getInstance(String JavaDoc bundleName) {
29         return getInstance(bundleName, (javax.jdo.I18NHelper.class)
30                 .getClassLoader());
31     }
32
33     public static I18NHelper getInstance(final Class JavaDoc cls) {
34         ClassLoader JavaDoc classLoader = (ClassLoader JavaDoc) AccessController
35                 .doPrivileged(new PrivilegedAction JavaDoc() {
36
37                     public Object JavaDoc run() {
38                         return cls.getClassLoader();
39                     }
40
41                 });
42         String JavaDoc bundle = getPackageName(cls.getName()) + ".Bundle";
43         return getInstance(bundle, classLoader);
44     }
45
46     public static I18NHelper getInstance(String JavaDoc bundleName, ClassLoader JavaDoc loader) {
47         I18NHelper helper = (I18NHelper) helpers.get(bundleName);
48         if (helper != null) {
49             return helper;
50         } else {
51             helper = new I18NHelper(bundleName, loader);
52             helpers.put(bundleName, helper);
53             return (I18NHelper) helpers.get(bundleName);
54         }
55     }
56
57     public String JavaDoc msg(String JavaDoc messageKey) {
58         return getMessage(bundle, messageKey);
59     }
60
61     public String JavaDoc msg(String JavaDoc messageKey, Object JavaDoc arg1) {
62         return getMessage(bundle, messageKey, arg1);
63     }
64
65     public String JavaDoc msg(String JavaDoc messageKey, Object JavaDoc arg1, Object JavaDoc arg2) {
66         return getMessage(bundle, messageKey, arg1, arg2);
67     }
68
69     public String JavaDoc msg(String JavaDoc messageKey, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3) {
70         return getMessage(bundle, messageKey, arg1, arg2, arg3);
71     }
72
73     public String JavaDoc msg(String JavaDoc messageKey, Object JavaDoc args[]) {
74         return getMessage(bundle, messageKey, args);
75     }
76
77     public String JavaDoc msg(String JavaDoc messageKey, int arg) {
78         return getMessage(bundle, messageKey, arg);
79     }
80
81     public String JavaDoc msg(String JavaDoc messageKey, boolean arg) {
82         return getMessage(bundle, messageKey, arg);
83     }
84
85     private static final ResourceBundle loadBundle(String JavaDoc bundleName,
86             ClassLoader JavaDoc loader) {
87         ResourceBundle messages = (ResourceBundle) bundles.get(bundleName);
88         if (messages == null) {
89             messages = ResourceBundle.getBundle(bundleName, locale, loader);
90             bundles.put(bundleName, messages);
91         }
92         return messages;
93     }
94
95     private static final String JavaDoc getMessage(ResourceBundle messages,
96             String JavaDoc messageKey) {
97         return messages.getString(messageKey);
98     }
99
100     private static final String JavaDoc getMessage(ResourceBundle messages,
101             String JavaDoc messageKey, Object JavaDoc msgArgs[]) {
102         for (int i = 0; i < msgArgs.length; i++)
103             if (msgArgs[i] == null)
104                 msgArgs[i] = "";
105
106         MessageFormat JavaDoc formatter = new MessageFormat JavaDoc(messages
107                 .getString(messageKey));
108         return formatter.format(((Object JavaDoc) (msgArgs)));
109     }
110
111     private static final String JavaDoc getMessage(ResourceBundle messages,
112             String JavaDoc messageKey, Object JavaDoc arg) {
113         Object JavaDoc args[] = { arg };
114         return getMessage(messages, messageKey, args);
115     }
116
117     private static final String JavaDoc getMessage(ResourceBundle messages,
118             String JavaDoc messageKey, Object JavaDoc arg1, Object JavaDoc arg2) {
119         Object JavaDoc args[] = { arg1, arg2 };
120         return getMessage(messages, messageKey, args);
121     }
122
123     private static final String JavaDoc getMessage(ResourceBundle messages,
124             String JavaDoc messageKey, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3) {
125         Object JavaDoc args[] = { arg1, arg2, arg3 };
126         return getMessage(messages, messageKey, args);
127     }
128
129     private static final String JavaDoc getMessage(ResourceBundle messages,
130             String JavaDoc messageKey, int arg) {
131         Object JavaDoc args[] = { new Integer JavaDoc(arg) };
132         return getMessage(messages, messageKey, args);
133     }
134
135     private static final String JavaDoc getMessage(ResourceBundle messages,
136             String JavaDoc messageKey, boolean arg) {
137         Object JavaDoc args[] = { String.valueOf(arg) };
138         return getMessage(messages, messageKey, args);
139     }
140
141     private static final String JavaDoc getPackageName(String JavaDoc className) {
142         int index = className.lastIndexOf('.');
143         return index == -1 ? "" : className.substring(0, index);
144     }
145
146     private static Hashtable bundles = new Hashtable();
147
148     private static Hashtable helpers = new Hashtable();
149
150     private static Locale locale = Locale.getDefault();
151
152     private ResourceBundle bundle;
153
154     private static final String JavaDoc bundleSuffix = ".Bundle";
155
156 }
Popular Tags