duke@1: /* ohair@554: * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.util; duke@1: mcimadamore@136: import com.sun.tools.javac.api.Messages; mcimadamore@136: import java.lang.ref.SoftReference; duke@1: import java.util.ResourceBundle; duke@1: import java.util.MissingResourceException; duke@1: import java.text.MessageFormat; mcimadamore@136: import java.util.HashMap; mcimadamore@136: import java.util.Locale; mcimadamore@136: import java.util.Map; duke@1: duke@1: /** mcimadamore@136: * Support for formatted localized messages. duke@1: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ mcimadamore@136: public class JavacMessages implements Messages { mcimadamore@136: /** The context key for the JavacMessages object. */ mcimadamore@136: protected static final Context.Key messagesKey = mcimadamore@136: new Context.Key(); duke@1: mcimadamore@136: /** Get the JavacMessages instance for this context. */ mcimadamore@136: public static JavacMessages instance(Context context) { mcimadamore@136: JavacMessages instance = context.get(messagesKey); duke@1: if (instance == null) mcimadamore@136: instance = new JavacMessages(context); duke@1: return instance; duke@1: } duke@1: mcimadamore@136: private Map>> bundleCache; duke@1: mcimadamore@136: private List bundleNames; mcimadamore@136: mcimadamore@136: private Locale currentLocale; mcimadamore@136: private List currentBundles; mcimadamore@136: mcimadamore@136: public Locale getCurrentLocale() { mcimadamore@136: return currentLocale; duke@1: } duke@1: mcimadamore@136: public void setCurrentLocale(Locale locale) { mcimadamore@136: if (locale == null) { mcimadamore@136: locale = Locale.getDefault(); mcimadamore@136: } mcimadamore@136: this.currentBundles = getBundles(locale); mcimadamore@136: this.currentLocale = locale; duke@1: } duke@1: mcimadamore@136: /** Creates a JavacMessages object. duke@1: */ mcimadamore@136: public JavacMessages(Context context) { mcimadamore@136: this(defaultBundleName); mcimadamore@136: context.put(messagesKey, this); duke@1: } duke@1: mcimadamore@136: /** Creates a JavacMessages object. mcimadamore@136: * @param bundleName the name to identify the resource buundle of localized messages. duke@1: */ mcimadamore@136: public JavacMessages(String bundleName) throws MissingResourceException { mcimadamore@136: bundleNames = List.nil(); mcimadamore@136: bundleCache = new HashMap>>(); mcimadamore@136: add(bundleName); mcimadamore@136: setCurrentLocale(Locale.getDefault()); duke@1: } duke@1: mcimadamore@136: public JavacMessages() throws MissingResourceException { mcimadamore@136: this(defaultBundleName); mcimadamore@136: } mcimadamore@136: mcimadamore@136: public void add(String bundleName) throws MissingResourceException { mcimadamore@136: bundleNames = bundleNames.prepend(bundleName); mcimadamore@136: if (!bundleCache.isEmpty()) mcimadamore@136: bundleCache.clear(); mcimadamore@141: currentBundles = null; mcimadamore@136: } mcimadamore@136: mcimadamore@136: public List getBundles(Locale locale) { mcimadamore@141: if (locale == currentLocale && currentBundles != null) mcimadamore@136: return currentBundles; mcimadamore@136: SoftReference> bundles = bundleCache.get(locale); mcimadamore@136: List bundleList = bundles == null ? null : bundles.get(); mcimadamore@136: if (bundleList == null) { mcimadamore@136: bundleList = List.nil(); mcimadamore@136: for (String bundleName : bundleNames) { mcimadamore@136: try { mcimadamore@136: ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale); mcimadamore@136: bundleList = bundleList.prepend(rb); mcimadamore@136: } catch (MissingResourceException e) { mcimadamore@136: throw new InternalError("Cannot find javac resource bundle for locale " + locale); mcimadamore@136: } mcimadamore@136: } mcimadamore@136: bundleCache.put(locale, new SoftReference>(bundleList)); mcimadamore@136: } mcimadamore@136: return bundleList; duke@1: } duke@1: duke@1: /** Gets the localized string corresponding to a key, formatted with a set of args. duke@1: */ duke@1: public String getLocalizedString(String key, Object... args) { mcimadamore@136: return getLocalizedString(currentLocale, key, args); duke@1: } duke@1: mcimadamore@136: public String getLocalizedString(Locale l, String key, Object... args) { mcimadamore@136: if (l == null) mcimadamore@136: l = getCurrentLocale(); mcimadamore@136: return getLocalizedString(getBundles(l), key, args); mcimadamore@136: } duke@1: duke@1: /* Static access: duke@1: * javac has a firmly entrenched notion of a default message bundle duke@1: * which it can access from any static context. This is used to get duke@1: * easy access to simple localized strings. duke@1: */ duke@1: duke@1: private static final String defaultBundleName = duke@1: "com.sun.tools.javac.resources.compiler"; duke@1: private static ResourceBundle defaultBundle; mcimadamore@136: private static JavacMessages defaultMessages; duke@1: duke@1: duke@1: /** duke@1: * Gets a localized string from the compiler's default bundle. duke@1: */ duke@1: // used to support legacy Log.getLocalizedString duke@1: static String getDefaultLocalizedString(String key, Object... args) { duke@1: return getLocalizedString(List.of(getDefaultBundle()), key, args); duke@1: } duke@1: duke@1: // used to support legacy static Diagnostic.fragment mcimadamore@136: @Deprecated mcimadamore@136: static JavacMessages getDefaultMessages() { duke@1: if (defaultMessages == null) mcimadamore@136: defaultMessages = new JavacMessages(defaultBundleName); duke@1: return defaultMessages; duke@1: } duke@1: duke@1: public static ResourceBundle getDefaultBundle() { duke@1: try { duke@1: if (defaultBundle == null) duke@1: defaultBundle = ResourceBundle.getBundle(defaultBundleName); duke@1: return defaultBundle; duke@1: } duke@1: catch (MissingResourceException e) { duke@1: throw new Error("Fatal: Resource for compiler is missing", e); duke@1: } duke@1: } duke@1: duke@1: private static String getLocalizedString(List bundles, duke@1: String key, duke@1: Object... args) { duke@1: String msg = null; duke@1: for (List l = bundles; l.nonEmpty() && msg == null; l = l.tail) { duke@1: ResourceBundle rb = l.head; duke@1: try { duke@1: msg = rb.getString(key); duke@1: } duke@1: catch (MissingResourceException e) { duke@1: // ignore, try other bundles in list duke@1: } duke@1: } duke@1: if (msg == null) { duke@1: msg = "compiler message file broken: key=" + key + duke@1: " arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}"; duke@1: } duke@1: return MessageFormat.format(msg, args); duke@1: } duke@1: }