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

changeset 136
8eafba4f61be
parent 1
9a66ca7c79fa
child 141
83ffdd1a6294
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/util/JavacMessages.java	Thu Oct 09 16:07:38 2008 +0100
     1.3 @@ -0,0 +1,194 @@
     1.4 +/*
     1.5 + * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.util;
    1.30 +
    1.31 +import com.sun.tools.javac.api.Messages;
    1.32 +import java.lang.ref.SoftReference;
    1.33 +import java.util.ResourceBundle;
    1.34 +import java.util.MissingResourceException;
    1.35 +import java.text.MessageFormat;
    1.36 +import java.util.HashMap;
    1.37 +import java.util.Locale;
    1.38 +import java.util.Map;
    1.39 +
    1.40 +/**
    1.41 + *  Support for formatted localized messages.
    1.42 + *
    1.43 + *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    1.44 + *  you write code that depends on this, you do so at your own risk.
    1.45 + *  This code and its internal interfaces are subject to change or
    1.46 + *  deletion without notice.</b>
    1.47 + */
    1.48 +public class JavacMessages implements Messages {
    1.49 +    /** The context key for the JavacMessages object. */
    1.50 +    protected static final Context.Key<JavacMessages> messagesKey =
    1.51 +        new Context.Key<JavacMessages>();
    1.52 +
    1.53 +    /** Get the JavacMessages instance for this context. */
    1.54 +    public static JavacMessages instance(Context context) {
    1.55 +        JavacMessages instance = context.get(messagesKey);
    1.56 +        if (instance == null)
    1.57 +            instance = new JavacMessages(context);
    1.58 +        return instance;
    1.59 +    }
    1.60 +
    1.61 +    private Map<Locale, SoftReference<List<ResourceBundle>>> bundleCache;
    1.62 +
    1.63 +    private List<String> bundleNames;
    1.64 +
    1.65 +    private Locale currentLocale;
    1.66 +    private List<ResourceBundle> currentBundles;
    1.67 +
    1.68 +    public Locale getCurrentLocale() {
    1.69 +        return currentLocale;
    1.70 +    }
    1.71 +
    1.72 +    public void setCurrentLocale(Locale locale) {
    1.73 +        if (locale == null) {
    1.74 +            locale = Locale.getDefault();
    1.75 +        }
    1.76 +        this.currentBundles = getBundles(locale);
    1.77 +        this.currentLocale = locale;
    1.78 +    }
    1.79 +
    1.80 +    /** Creates a JavacMessages object.
    1.81 +     */
    1.82 +    public JavacMessages(Context context) {
    1.83 +        this(defaultBundleName);
    1.84 +        context.put(messagesKey, this);
    1.85 +    }
    1.86 +
    1.87 +    /** Creates a JavacMessages object.
    1.88 +     * @param bundleName the name to identify the resource buundle of localized messages.
    1.89 +     */
    1.90 +    public JavacMessages(String bundleName) throws MissingResourceException {
    1.91 +        bundleNames = List.nil();
    1.92 +        bundleCache = new HashMap<Locale, SoftReference<List<ResourceBundle>>>();
    1.93 +        add(bundleName);
    1.94 +        setCurrentLocale(Locale.getDefault());
    1.95 +    }
    1.96 +
    1.97 +    public JavacMessages() throws MissingResourceException {
    1.98 +        this(defaultBundleName);
    1.99 +    }
   1.100 +
   1.101 +    public void add(String bundleName) throws MissingResourceException {
   1.102 +        bundleNames = bundleNames.prepend(bundleName);
   1.103 +        if (!bundleCache.isEmpty())
   1.104 +            bundleCache.clear();
   1.105 +    }
   1.106 +
   1.107 +    public List<ResourceBundle> getBundles(Locale locale) {
   1.108 +        if (locale == currentLocale)
   1.109 +            return currentBundles;
   1.110 +        SoftReference<List<ResourceBundle>> bundles = bundleCache.get(locale);
   1.111 +        List<ResourceBundle> bundleList = bundles == null ? null : bundles.get();
   1.112 +        if (bundleList == null) {
   1.113 +            bundleList = List.nil();
   1.114 +            for (String bundleName : bundleNames) {
   1.115 +                try {
   1.116 +                    ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale);
   1.117 +                    bundleList = bundleList.prepend(rb);
   1.118 +                } catch (MissingResourceException e) {
   1.119 +                    throw new InternalError("Cannot find javac resource bundle for locale " + locale);
   1.120 +                }
   1.121 +            }
   1.122 +            bundleCache.put(locale, new SoftReference<List<ResourceBundle>>(bundleList));
   1.123 +        }
   1.124 +        return bundleList;
   1.125 +    }
   1.126 +
   1.127 +    /** Gets the localized string corresponding to a key, formatted with a set of args.
   1.128 +     */
   1.129 +    public String getLocalizedString(String key, Object... args) {
   1.130 +        return getLocalizedString(currentLocale, key, args);
   1.131 +    }
   1.132 +
   1.133 +    public String getLocalizedString(Locale l, String key, Object... args) {
   1.134 +        if (l == null)
   1.135 +            l = getCurrentLocale();
   1.136 +        return getLocalizedString(getBundles(l), key, args);
   1.137 +    }
   1.138 +
   1.139 +    /* Static access:
   1.140 +     * javac has a firmly entrenched notion of a default message bundle
   1.141 +     * which it can access from any static context. This is used to get
   1.142 +     * easy access to simple localized strings.
   1.143 +     */
   1.144 +
   1.145 +    private static final String defaultBundleName =
   1.146 +        "com.sun.tools.javac.resources.compiler";
   1.147 +    private static ResourceBundle defaultBundle;
   1.148 +    private static JavacMessages defaultMessages;
   1.149 +
   1.150 +
   1.151 +    /**
   1.152 +     * Gets a localized string from the compiler's default bundle.
   1.153 +     */
   1.154 +    // used to support legacy Log.getLocalizedString
   1.155 +    static String getDefaultLocalizedString(String key, Object... args) {
   1.156 +        return getLocalizedString(List.of(getDefaultBundle()), key, args);
   1.157 +    }
   1.158 +
   1.159 +    // used to support legacy static Diagnostic.fragment
   1.160 +    @Deprecated
   1.161 +    static JavacMessages getDefaultMessages() {
   1.162 +        if (defaultMessages == null)
   1.163 +            defaultMessages = new JavacMessages(defaultBundleName);
   1.164 +        return defaultMessages;
   1.165 +    }
   1.166 +
   1.167 +    public static ResourceBundle getDefaultBundle() {
   1.168 +        try {
   1.169 +            if (defaultBundle == null)
   1.170 +                defaultBundle = ResourceBundle.getBundle(defaultBundleName);
   1.171 +            return defaultBundle;
   1.172 +        }
   1.173 +        catch (MissingResourceException e) {
   1.174 +            throw new Error("Fatal: Resource for compiler is missing", e);
   1.175 +        }
   1.176 +    }
   1.177 +
   1.178 +    private static String getLocalizedString(List<ResourceBundle> bundles,
   1.179 +                                             String key,
   1.180 +                                             Object... args) {
   1.181 +       String msg = null;
   1.182 +        for (List<ResourceBundle> l = bundles; l.nonEmpty() && msg == null; l = l.tail) {
   1.183 +            ResourceBundle rb = l.head;
   1.184 +            try {
   1.185 +                msg = rb.getString(key);
   1.186 +            }
   1.187 +            catch (MissingResourceException e) {
   1.188 +                // ignore, try other bundles in list
   1.189 +            }
   1.190 +        }
   1.191 +        if (msg == null) {
   1.192 +            msg = "compiler message file broken: key=" + key +
   1.193 +                " arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}";
   1.194 +        }
   1.195 +        return MessageFormat.format(msg, args);
   1.196 +    }
   1.197 +}

mercurial