src/share/classes/com/sun/tools/javac/util/JavacMessages.java

Thu, 09 Oct 2008 16:07:38 +0100

author
mcimadamore
date
Thu, 09 Oct 2008 16:07:38 +0100
changeset 136
8eafba4f61be
parent 1
src/share/classes/com/sun/tools/javac/util/Messages.java@9a66ca7c79fa
child 141
83ffdd1a6294
permissions
-rw-r--r--

6406133: JCDiagnostic.getMessage ignores locale argument
Summary: Compiler API should take into account locale settings
Reviewed-by: jjg

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

mercurial