langtools/src/share/classes/com/sun/tools/javac/util/Messages.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 10 06bc494ca11e
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.ResourceBundle;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.MissingResourceException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.text.MessageFormat;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 *  Support for localized messages.
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
public class Messages {
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
    /** The context key for the Messages object. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    protected static final Context.Key<Messages> messagesKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
        new Context.Key<Messages>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    /** Get the Messages instance for this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    public static Messages instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
        Messages instance = context.get(messagesKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
            instance = new Messages(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    private List<ResourceBundle> bundles = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    /** Creates a Messages object.
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    public Messages(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        context.put(messagesKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        add(getDefaultBundle());
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    /** Creates a Messages object.
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * @param bundle the name to identify the resource buundle of localized messages.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    public Messages(String bundleName) throws MissingResourceException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        add(bundleName);
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    /** Creates a Messages object.
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     * @param bundle the name to identif the resource buundle of localized messages.
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    public Messages(ResourceBundle bundle) throws MissingResourceException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        add(bundle);
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    /** Add a new resource bundle to the list that is searched for localized messages.
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     * @param bundle the name to identify the resource bundle of localized messages.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    public void add(String bundleName) throws MissingResourceException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        add(ResourceBundle.getBundle(bundleName));
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    /** Add a new resource bundle to the list that is searched for localized messages.
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     * Resource bundles will be searched in reverse order in which they are added.
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     * @param bundle the bundle of localized messages.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    public void add(ResourceBundle bundle) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        bundles = bundles.prepend(bundle);
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    /** Gets the localized string corresponding to a key, formatted with a set of args.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    public String getLocalizedString(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        return getLocalizedString(bundles, key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    /* Static access:
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * javac has a firmly entrenched notion of a default message bundle
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     * which it can access from any static context. This is used to get
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     * easy access to simple localized strings.
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    private static final String defaultBundleName =
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        "com.sun.tools.javac.resources.compiler";
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    private static ResourceBundle defaultBundle;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    private static Messages defaultMessages;
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * Gets a localized string from the compiler's default bundle.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    // used to support legacy Log.getLocalizedString
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    static String getDefaultLocalizedString(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        return getLocalizedString(List.of(getDefaultBundle()), key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    // used to support legacy static Diagnostic.fragment
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    static Messages getDefaultMessages() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        if (defaultMessages == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            defaultMessages = new Messages(getDefaultBundle());
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        return defaultMessages;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    public static ResourceBundle getDefaultBundle() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            if (defaultBundle == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                defaultBundle = ResourceBundle.getBundle(defaultBundleName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            return defaultBundle;
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        catch (MissingResourceException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
            throw new Error("Fatal: Resource for compiler is missing", e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    private static String getLocalizedString(List<ResourceBundle> bundles,
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                                             String key,
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                                             Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
       String msg = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        for (List<ResourceBundle> l = bundles; l.nonEmpty() && msg == null; l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            ResourceBundle rb = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
                msg = rb.getString(key);
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            catch (MissingResourceException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                // ignore, try other bundles in list
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        if (msg == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            msg = "compiler message file broken: key=" + key +
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                " arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}";
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        return MessageFormat.format(msg, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
}