duke@1: /* jjg@944: * Copyright (c) 2005, 2011, 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. */
jjg@944: public static final Context.Key> bundles = bundleCache.get(locale);
mcimadamore@136: List
>(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