001 /*
002 * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package com.sun.tools.javac.util;
027
028 import com.sun.tools.javac.api.Messages;
029 import java.lang.ref.SoftReference;
030 import java.util.ResourceBundle;
031 import java.util.MissingResourceException;
032 import java.text.MessageFormat;
033 import java.util.HashMap;
034 import java.util.Locale;
035 import java.util.Map;
036
037 /**
038 * Support for formatted localized messages.
039 *
040 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
041 * you write code that depends on this, you do so at your own risk.
042 * This code and its internal interfaces are subject to change or
043 * deletion without notice.</b>
044 */
045 public class JavacMessages implements Messages {
046 /** The context key for the JavacMessages object. */
047 protected static final Context.Key<JavacMessages> messagesKey =
048 new Context.Key<JavacMessages>();
049
050 /** Get the JavacMessages instance for this context. */
051 public static JavacMessages instance(Context context) {
052 JavacMessages instance = context.get(messagesKey);
053 if (instance == null)
054 instance = new JavacMessages(context);
055 return instance;
056 }
057
058 private Map<Locale, SoftReference<List<ResourceBundle>>> bundleCache;
059
060 private List<String> bundleNames;
061
062 private Locale currentLocale;
063 private List<ResourceBundle> currentBundles;
064
065 public Locale getCurrentLocale() {
066 return currentLocale;
067 }
068
069 public void setCurrentLocale(Locale locale) {
070 if (locale == null) {
071 locale = Locale.getDefault();
072 }
073 this.currentBundles = getBundles(locale);
074 this.currentLocale = locale;
075 }
076
077 /** Creates a JavacMessages object.
078 */
079 public JavacMessages(Context context) {
080 this(defaultBundleName);
081 context.put(messagesKey, this);
082 }
083
084 /** Creates a JavacMessages object.
085 * @param bundleName the name to identify the resource buundle of localized messages.
086 */
087 public JavacMessages(String bundleName) throws MissingResourceException {
088 bundleNames = List.nil();
089 bundleCache = new HashMap<Locale, SoftReference<List<ResourceBundle>>>();
090 add(bundleName);
091 setCurrentLocale(Locale.getDefault());
092 }
093
094 public JavacMessages() throws MissingResourceException {
095 this(defaultBundleName);
096 }
097
098 public void add(String bundleName) throws MissingResourceException {
099 bundleNames = bundleNames.prepend(bundleName);
100 if (!bundleCache.isEmpty())
101 bundleCache.clear();
102 currentBundles = null;
103 }
104
105 public List<ResourceBundle> getBundles(Locale locale) {
106 if (locale == currentLocale && currentBundles != null)
107 return currentBundles;
108 SoftReference<List<ResourceBundle>> bundles = bundleCache.get(locale);
109 List<ResourceBundle> bundleList = bundles == null ? null : bundles.get();
110 if (bundleList == null) {
111 bundleList = List.nil();
112 for (String bundleName : bundleNames) {
113 try {
114 ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale);
115 bundleList = bundleList.prepend(rb);
116 } catch (MissingResourceException e) {
117 throw new InternalError("Cannot find javac resource bundle for locale " + locale);
118 }
119 }
120 bundleCache.put(locale, new SoftReference<List<ResourceBundle>>(bundleList));
121 }
122 return bundleList;
123 }
124
125 /** Gets the localized string corresponding to a key, formatted with a set of args.
126 */
127 public String getLocalizedString(String key, Object... args) {
128 return getLocalizedString(currentLocale, key, args);
129 }
130
131 public String getLocalizedString(Locale l, String key, Object... args) {
132 if (l == null)
133 l = getCurrentLocale();
134 return getLocalizedString(getBundles(l), key, args);
135 }
136
137 /* Static access:
138 * javac has a firmly entrenched notion of a default message bundle
139 * which it can access from any static context. This is used to get
140 * easy access to simple localized strings.
141 */
142
143 private static final String defaultBundleName =
144 "com.sun.tools.javac.resources.compiler";
145 private static ResourceBundle defaultBundle;
146 private static JavacMessages defaultMessages;
147
148
149 /**
150 * Gets a localized string from the compiler's default bundle.
151 */
152 // used to support legacy Log.getLocalizedString
153 static String getDefaultLocalizedString(String key, Object... args) {
154 return getLocalizedString(List.of(getDefaultBundle()), key, args);
155 }
156
157 // used to support legacy static Diagnostic.fragment
158 @Deprecated
159 static JavacMessages getDefaultMessages() {
160 if (defaultMessages == null)
161 defaultMessages = new JavacMessages(defaultBundleName);
162 return defaultMessages;
163 }
164
165 public static ResourceBundle getDefaultBundle() {
166 try {
167 if (defaultBundle == null)
168 defaultBundle = ResourceBundle.getBundle(defaultBundleName);
169 return defaultBundle;
170 }
171 catch (MissingResourceException e) {
172 throw new Error("Fatal: Resource for compiler is missing", e);
173 }
174 }
175
176 private static String getLocalizedString(List<ResourceBundle> bundles,
177 String key,
178 Object... args) {
179 String msg = null;
180 for (List<ResourceBundle> l = bundles; l.nonEmpty() && msg == null; l = l.tail) {
181 ResourceBundle rb = l.head;
182 try {
183 msg = rb.getString(key);
184 }
185 catch (MissingResourceException e) {
186 // ignore, try other bundles in list
187 }
188 }
189 if (msg == null) {
190 msg = "compiler message file broken: key=" + key +
191 " arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}";
192 }
193 return MessageFormat.format(msg, args);
194 }
195 }