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

Thu, 10 Jun 2010 16:08:01 -0700

author
jjg
date
Thu, 10 Jun 2010 16:08:01 -0700
changeset 581
f2fdd52e4e87
parent 554
9d9f26857129
child 944
83260b3305ac
permissions
-rw-r--r--

6944312: Potential rebranding issues in openjdk/langtools repository sources
Reviewed-by: darcy

duke@1 1 /*
ohair@554 2 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. 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
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle 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 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * 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 *
jjg@581 40 * <p><b>This is NOT part of any supported API.
jjg@581 41 * If 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@141 102 currentBundles = null;
mcimadamore@136 103 }
mcimadamore@136 104
mcimadamore@136 105 public List<ResourceBundle> getBundles(Locale locale) {
mcimadamore@141 106 if (locale == currentLocale && currentBundles != null)
mcimadamore@136 107 return currentBundles;
mcimadamore@136 108 SoftReference<List<ResourceBundle>> bundles = bundleCache.get(locale);
mcimadamore@136 109 List<ResourceBundle> bundleList = bundles == null ? null : bundles.get();
mcimadamore@136 110 if (bundleList == null) {
mcimadamore@136 111 bundleList = List.nil();
mcimadamore@136 112 for (String bundleName : bundleNames) {
mcimadamore@136 113 try {
mcimadamore@136 114 ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale);
mcimadamore@136 115 bundleList = bundleList.prepend(rb);
mcimadamore@136 116 } catch (MissingResourceException e) {
mcimadamore@136 117 throw new InternalError("Cannot find javac resource bundle for locale " + locale);
mcimadamore@136 118 }
mcimadamore@136 119 }
mcimadamore@136 120 bundleCache.put(locale, new SoftReference<List<ResourceBundle>>(bundleList));
mcimadamore@136 121 }
mcimadamore@136 122 return bundleList;
duke@1 123 }
duke@1 124
duke@1 125 /** Gets the localized string corresponding to a key, formatted with a set of args.
duke@1 126 */
duke@1 127 public String getLocalizedString(String key, Object... args) {
mcimadamore@136 128 return getLocalizedString(currentLocale, key, args);
duke@1 129 }
duke@1 130
mcimadamore@136 131 public String getLocalizedString(Locale l, String key, Object... args) {
mcimadamore@136 132 if (l == null)
mcimadamore@136 133 l = getCurrentLocale();
mcimadamore@136 134 return getLocalizedString(getBundles(l), key, args);
mcimadamore@136 135 }
duke@1 136
duke@1 137 /* Static access:
duke@1 138 * javac has a firmly entrenched notion of a default message bundle
duke@1 139 * which it can access from any static context. This is used to get
duke@1 140 * easy access to simple localized strings.
duke@1 141 */
duke@1 142
duke@1 143 private static final String defaultBundleName =
duke@1 144 "com.sun.tools.javac.resources.compiler";
duke@1 145 private static ResourceBundle defaultBundle;
mcimadamore@136 146 private static JavacMessages defaultMessages;
duke@1 147
duke@1 148
duke@1 149 /**
duke@1 150 * Gets a localized string from the compiler's default bundle.
duke@1 151 */
duke@1 152 // used to support legacy Log.getLocalizedString
duke@1 153 static String getDefaultLocalizedString(String key, Object... args) {
duke@1 154 return getLocalizedString(List.of(getDefaultBundle()), key, args);
duke@1 155 }
duke@1 156
duke@1 157 // used to support legacy static Diagnostic.fragment
mcimadamore@136 158 @Deprecated
mcimadamore@136 159 static JavacMessages getDefaultMessages() {
duke@1 160 if (defaultMessages == null)
mcimadamore@136 161 defaultMessages = new JavacMessages(defaultBundleName);
duke@1 162 return defaultMessages;
duke@1 163 }
duke@1 164
duke@1 165 public static ResourceBundle getDefaultBundle() {
duke@1 166 try {
duke@1 167 if (defaultBundle == null)
duke@1 168 defaultBundle = ResourceBundle.getBundle(defaultBundleName);
duke@1 169 return defaultBundle;
duke@1 170 }
duke@1 171 catch (MissingResourceException e) {
duke@1 172 throw new Error("Fatal: Resource for compiler is missing", e);
duke@1 173 }
duke@1 174 }
duke@1 175
duke@1 176 private static String getLocalizedString(List<ResourceBundle> bundles,
duke@1 177 String key,
duke@1 178 Object... args) {
duke@1 179 String msg = null;
duke@1 180 for (List<ResourceBundle> l = bundles; l.nonEmpty() && msg == null; l = l.tail) {
duke@1 181 ResourceBundle rb = l.head;
duke@1 182 try {
duke@1 183 msg = rb.getString(key);
duke@1 184 }
duke@1 185 catch (MissingResourceException e) {
duke@1 186 // ignore, try other bundles in list
duke@1 187 }
duke@1 188 }
duke@1 189 if (msg == null) {
duke@1 190 msg = "compiler message file broken: key=" + key +
duke@1 191 " arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}";
duke@1 192 }
duke@1 193 return MessageFormat.format(msg, args);
duke@1 194 }
duke@1 195 }

mercurial