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

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
permissions
-rw-r--r--

Initial load

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

mercurial