001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javac.util;
027:
028: import java.util.ResourceBundle;
029: import java.util.MissingResourceException;
030: import java.text.MessageFormat;
031:
032: /**
033: * Support for localized messages.
034: *
035: * <p><b>This is NOT part of any API supported by Sun Microsystems. If
036: * you write code that depends on this, you do so at your own risk.
037: * This code and its internal interfaces are subject to change or
038: * deletion without notice.</b>
039: */
040: @Version("@(#)Messages.java 1.9 07/05/05")
041: public class Messages {
042: /** The context key for the Messages object. */
043: protected static final Context.Key<Messages> messagesKey = new Context.Key<Messages>();
044:
045: /** Get the Messages instance for this context. */
046: public static Messages instance(Context context) {
047: Messages instance = context.get(messagesKey);
048: if (instance == null)
049: instance = new Messages(context);
050: return instance;
051: }
052:
053: private List<ResourceBundle> bundles = List.nil();
054:
055: /** Creates a Messages object.
056: */
057: public Messages(Context context) {
058: context.put(messagesKey, this );
059: add(getDefaultBundle());
060: }
061:
062: /** Creates a Messages object.
063: * @param bundle the name to identify the resource buundle of localized messages.
064: */
065: public Messages(String bundleName) throws MissingResourceException {
066: add(bundleName);
067: }
068:
069: /** Creates a Messages object.
070: * @param bundle the name to identif the resource buundle of localized messages.
071: */
072: public Messages(ResourceBundle bundle)
073: throws MissingResourceException {
074: add(bundle);
075: }
076:
077: /** Add a new resource bundle to the list that is searched for localized messages.
078: * @param bundle the name to identify the resource bundle of localized messages.
079: */
080: public void add(String bundleName) throws MissingResourceException {
081: add(ResourceBundle.getBundle(bundleName));
082: }
083:
084: /** Add a new resource bundle to the list that is searched for localized messages.
085: * Resource bundles will be searched in reverse order in which they are added.
086: * @param bundle the bundle of localized messages.
087: */
088: public void add(ResourceBundle bundle) {
089: bundles = bundles.prepend(bundle);
090: }
091:
092: /** Gets the localized string corresponding to a key, formatted with a set of args.
093: */
094: public String getLocalizedString(String key, Object... args) {
095: return getLocalizedString(bundles, key, args);
096: }
097:
098: /* Static access:
099: * 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: */
103:
104: private static final String defaultBundleName = "com.sun.tools.javac.resources.compiler";
105: private static ResourceBundle defaultBundle;
106: private static Messages defaultMessages;
107:
108: /**
109: * Gets a localized string from the compiler's default bundle.
110: */
111: // used to support legacy Log.getLocalizedString
112: static String getDefaultLocalizedString(String key, Object... args) {
113: return getLocalizedString(List.of(getDefaultBundle()), key,
114: args);
115: }
116:
117: // used to support legacy static Diagnostic.fragment
118: static Messages getDefaultMessages() {
119: if (defaultMessages == null)
120: defaultMessages = new Messages(getDefaultBundle());
121: return defaultMessages;
122: }
123:
124: public static ResourceBundle getDefaultBundle() {
125: try {
126: if (defaultBundle == null)
127: defaultBundle = ResourceBundle
128: .getBundle(defaultBundleName);
129: return defaultBundle;
130: } catch (MissingResourceException e) {
131: throw new Error("Fatal: Resource for compiler is missing",
132: e);
133: }
134: }
135:
136: private static String getLocalizedString(
137: List<ResourceBundle> bundles, String key, Object... args) {
138: String msg = null;
139: for (List<ResourceBundle> l = bundles; l.nonEmpty()
140: && msg == null; l = l.tail) {
141: ResourceBundle rb = l.head;
142: try {
143: msg = rb.getString(key);
144: } catch (MissingResourceException e) {
145: // ignore, try other bundles in list
146: }
147: }
148: if (msg == null) {
149: msg = "compiler message file broken: key="
150: + key
151: + " arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}";
152: }
153: return MessageFormat.format(msg, args);
154: }
155:
156: }
|