|
1 /* |
|
2 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.javac.util; |
|
27 |
|
28 import com.sun.tools.javac.api.Messages; |
|
29 import java.lang.ref.SoftReference; |
|
30 import java.util.ResourceBundle; |
|
31 import java.util.MissingResourceException; |
|
32 import java.text.MessageFormat; |
|
33 import java.util.HashMap; |
|
34 import java.util.Locale; |
|
35 import java.util.Map; |
|
36 |
|
37 /** |
|
38 * Support for formatted localized messages. |
|
39 * |
|
40 * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
|
41 * you write code that depends on this, you do so at your own risk. |
|
42 * This code and its internal interfaces are subject to change or |
|
43 * deletion without notice.</b> |
|
44 */ |
|
45 public class JavacMessages implements Messages { |
|
46 /** The context key for the JavacMessages object. */ |
|
47 protected static final Context.Key<JavacMessages> messagesKey = |
|
48 new Context.Key<JavacMessages>(); |
|
49 |
|
50 /** Get the JavacMessages instance for this context. */ |
|
51 public static JavacMessages instance(Context context) { |
|
52 JavacMessages instance = context.get(messagesKey); |
|
53 if (instance == null) |
|
54 instance = new JavacMessages(context); |
|
55 return instance; |
|
56 } |
|
57 |
|
58 private Map<Locale, SoftReference<List<ResourceBundle>>> bundleCache; |
|
59 |
|
60 private List<String> bundleNames; |
|
61 |
|
62 private Locale currentLocale; |
|
63 private List<ResourceBundle> currentBundles; |
|
64 |
|
65 public Locale getCurrentLocale() { |
|
66 return currentLocale; |
|
67 } |
|
68 |
|
69 public void setCurrentLocale(Locale locale) { |
|
70 if (locale == null) { |
|
71 locale = Locale.getDefault(); |
|
72 } |
|
73 this.currentBundles = getBundles(locale); |
|
74 this.currentLocale = locale; |
|
75 } |
|
76 |
|
77 /** Creates a JavacMessages object. |
|
78 */ |
|
79 public JavacMessages(Context context) { |
|
80 this(defaultBundleName); |
|
81 context.put(messagesKey, this); |
|
82 } |
|
83 |
|
84 /** Creates a JavacMessages object. |
|
85 * @param bundleName the name to identify the resource buundle of localized messages. |
|
86 */ |
|
87 public JavacMessages(String bundleName) throws MissingResourceException { |
|
88 bundleNames = List.nil(); |
|
89 bundleCache = new HashMap<Locale, SoftReference<List<ResourceBundle>>>(); |
|
90 add(bundleName); |
|
91 setCurrentLocale(Locale.getDefault()); |
|
92 } |
|
93 |
|
94 public JavacMessages() throws MissingResourceException { |
|
95 this(defaultBundleName); |
|
96 } |
|
97 |
|
98 public void add(String bundleName) throws MissingResourceException { |
|
99 bundleNames = bundleNames.prepend(bundleName); |
|
100 if (!bundleCache.isEmpty()) |
|
101 bundleCache.clear(); |
|
102 } |
|
103 |
|
104 public List<ResourceBundle> getBundles(Locale locale) { |
|
105 if (locale == currentLocale) |
|
106 return currentBundles; |
|
107 SoftReference<List<ResourceBundle>> bundles = bundleCache.get(locale); |
|
108 List<ResourceBundle> bundleList = bundles == null ? null : bundles.get(); |
|
109 if (bundleList == null) { |
|
110 bundleList = List.nil(); |
|
111 for (String bundleName : bundleNames) { |
|
112 try { |
|
113 ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale); |
|
114 bundleList = bundleList.prepend(rb); |
|
115 } catch (MissingResourceException e) { |
|
116 throw new InternalError("Cannot find javac resource bundle for locale " + locale); |
|
117 } |
|
118 } |
|
119 bundleCache.put(locale, new SoftReference<List<ResourceBundle>>(bundleList)); |
|
120 } |
|
121 return bundleList; |
|
122 } |
|
123 |
|
124 /** Gets the localized string corresponding to a key, formatted with a set of args. |
|
125 */ |
|
126 public String getLocalizedString(String key, Object... args) { |
|
127 return getLocalizedString(currentLocale, key, args); |
|
128 } |
|
129 |
|
130 public String getLocalizedString(Locale l, String key, Object... args) { |
|
131 if (l == null) |
|
132 l = getCurrentLocale(); |
|
133 return getLocalizedString(getBundles(l), key, args); |
|
134 } |
|
135 |
|
136 /* Static access: |
|
137 * javac has a firmly entrenched notion of a default message bundle |
|
138 * which it can access from any static context. This is used to get |
|
139 * easy access to simple localized strings. |
|
140 */ |
|
141 |
|
142 private static final String defaultBundleName = |
|
143 "com.sun.tools.javac.resources.compiler"; |
|
144 private static ResourceBundle defaultBundle; |
|
145 private static JavacMessages defaultMessages; |
|
146 |
|
147 |
|
148 /** |
|
149 * Gets a localized string from the compiler's default bundle. |
|
150 */ |
|
151 // used to support legacy Log.getLocalizedString |
|
152 static String getDefaultLocalizedString(String key, Object... args) { |
|
153 return getLocalizedString(List.of(getDefaultBundle()), key, args); |
|
154 } |
|
155 |
|
156 // used to support legacy static Diagnostic.fragment |
|
157 @Deprecated |
|
158 static JavacMessages getDefaultMessages() { |
|
159 if (defaultMessages == null) |
|
160 defaultMessages = new JavacMessages(defaultBundleName); |
|
161 return defaultMessages; |
|
162 } |
|
163 |
|
164 public static ResourceBundle getDefaultBundle() { |
|
165 try { |
|
166 if (defaultBundle == null) |
|
167 defaultBundle = ResourceBundle.getBundle(defaultBundleName); |
|
168 return defaultBundle; |
|
169 } |
|
170 catch (MissingResourceException e) { |
|
171 throw new Error("Fatal: Resource for compiler is missing", e); |
|
172 } |
|
173 } |
|
174 |
|
175 private static String getLocalizedString(List<ResourceBundle> bundles, |
|
176 String key, |
|
177 Object... args) { |
|
178 String msg = null; |
|
179 for (List<ResourceBundle> l = bundles; l.nonEmpty() && msg == null; l = l.tail) { |
|
180 ResourceBundle rb = l.head; |
|
181 try { |
|
182 msg = rb.getString(key); |
|
183 } |
|
184 catch (MissingResourceException e) { |
|
185 // ignore, try other bundles in list |
|
186 } |
|
187 } |
|
188 if (msg == null) { |
|
189 msg = "compiler message file broken: key=" + key + |
|
190 " arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}"; |
|
191 } |
|
192 return MessageFormat.format(msg, args); |
|
193 } |
|
194 } |