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

changeset 1
9a66ca7c79fa
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/util/Messages.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,157 @@
     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 java.util.ResourceBundle;
    1.32 +import java.util.MissingResourceException;
    1.33 +import java.text.MessageFormat;
    1.34 +
    1.35 +/**
    1.36 + *  Support for localized messages.
    1.37 + *
    1.38 + *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    1.39 + *  you write code that depends on this, you do so at your own risk.
    1.40 + *  This code and its internal interfaces are subject to change or
    1.41 + *  deletion without notice.</b>
    1.42 + */
    1.43 +public class Messages {
    1.44 +    /** The context key for the Messages object. */
    1.45 +    protected static final Context.Key<Messages> messagesKey =
    1.46 +        new Context.Key<Messages>();
    1.47 +
    1.48 +    /** Get the Messages instance for this context. */
    1.49 +    public static Messages instance(Context context) {
    1.50 +        Messages instance = context.get(messagesKey);
    1.51 +        if (instance == null)
    1.52 +            instance = new Messages(context);
    1.53 +        return instance;
    1.54 +    }
    1.55 +
    1.56 +    private List<ResourceBundle> bundles = List.nil();
    1.57 +
    1.58 +    /** Creates a Messages object.
    1.59 +     */
    1.60 +    public Messages(Context context) {
    1.61 +        context.put(messagesKey, this);
    1.62 +        add(getDefaultBundle());
    1.63 +    }
    1.64 +
    1.65 +    /** Creates a Messages object.
    1.66 +     * @param bundle the name to identify the resource buundle of localized messages.
    1.67 +     */
    1.68 +    public Messages(String bundleName) throws MissingResourceException {
    1.69 +        add(bundleName);
    1.70 +    }
    1.71 +
    1.72 +    /** Creates a Messages object.
    1.73 +     * @param bundle the name to identif the resource buundle of localized messages.
    1.74 +     */
    1.75 +    public Messages(ResourceBundle bundle) throws MissingResourceException {
    1.76 +        add(bundle);
    1.77 +    }
    1.78 +
    1.79 +    /** Add a new resource bundle to the list that is searched for localized messages.
    1.80 +     * @param bundle the name to identify the resource bundle of localized messages.
    1.81 +     */
    1.82 +    public void add(String bundleName) throws MissingResourceException {
    1.83 +        add(ResourceBundle.getBundle(bundleName));
    1.84 +    }
    1.85 +
    1.86 +    /** Add a new resource bundle to the list that is searched for localized messages.
    1.87 +     * Resource bundles will be searched in reverse order in which they are added.
    1.88 +     * @param bundle the bundle of localized messages.
    1.89 +     */
    1.90 +    public void add(ResourceBundle bundle) {
    1.91 +        bundles = bundles.prepend(bundle);
    1.92 +    }
    1.93 +
    1.94 +    /** Gets the localized string corresponding to a key, formatted with a set of args.
    1.95 +     */
    1.96 +    public String getLocalizedString(String key, Object... args) {
    1.97 +        return getLocalizedString(bundles, key, args);
    1.98 +    }
    1.99 +
   1.100 +
   1.101 +    /* Static access:
   1.102 +     * javac has a firmly entrenched notion of a default message bundle
   1.103 +     * which it can access from any static context. This is used to get
   1.104 +     * easy access to simple localized strings.
   1.105 +     */
   1.106 +
   1.107 +    private static final String defaultBundleName =
   1.108 +        "com.sun.tools.javac.resources.compiler";
   1.109 +    private static ResourceBundle defaultBundle;
   1.110 +    private static Messages defaultMessages;
   1.111 +
   1.112 +
   1.113 +    /**
   1.114 +     * Gets a localized string from the compiler's default bundle.
   1.115 +     */
   1.116 +    // used to support legacy Log.getLocalizedString
   1.117 +    static String getDefaultLocalizedString(String key, Object... args) {
   1.118 +        return getLocalizedString(List.of(getDefaultBundle()), key, args);
   1.119 +    }
   1.120 +
   1.121 +    // used to support legacy static Diagnostic.fragment
   1.122 +    static Messages getDefaultMessages() {
   1.123 +        if (defaultMessages == null)
   1.124 +            defaultMessages = new Messages(getDefaultBundle());
   1.125 +        return defaultMessages;
   1.126 +    }
   1.127 +
   1.128 +    public static ResourceBundle getDefaultBundle() {
   1.129 +        try {
   1.130 +            if (defaultBundle == null)
   1.131 +                defaultBundle = ResourceBundle.getBundle(defaultBundleName);
   1.132 +            return defaultBundle;
   1.133 +        }
   1.134 +        catch (MissingResourceException e) {
   1.135 +            throw new Error("Fatal: Resource for compiler is missing", e);
   1.136 +        }
   1.137 +    }
   1.138 +
   1.139 +    private static String getLocalizedString(List<ResourceBundle> bundles,
   1.140 +                                             String key,
   1.141 +                                             Object... args) {
   1.142 +       String msg = null;
   1.143 +        for (List<ResourceBundle> l = bundles; l.nonEmpty() && msg == null; l = l.tail) {
   1.144 +            ResourceBundle rb = l.head;
   1.145 +            try {
   1.146 +                msg = rb.getString(key);
   1.147 +            }
   1.148 +            catch (MissingResourceException e) {
   1.149 +                // ignore, try other bundles in list
   1.150 +            }
   1.151 +        }
   1.152 +        if (msg == null) {
   1.153 +            msg = "compiler message file broken: key=" + key +
   1.154 +                " arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}";
   1.155 +        }
   1.156 +        return MessageFormat.format(msg, args);
   1.157 +    }
   1.158 +
   1.159 +
   1.160 +}

mercurial