langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/MessageRetriever.java
author jjg
Wed, 10 Oct 2012 16:48:21 -0700
changeset 14260 727a84636f12
parent 14258 8d2148961366
child 15354 52a04c670c05
permissions -rw-r--r--
8000665: fix "internal API" comments on javadoc files Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
     2
 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
10
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
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2212
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2212
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2212
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2212
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2212
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
package com.sun.tools.doclets.internal.toolkit.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    27
import java.text.MessageFormat;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    28
import java.util.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    29
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.doclets.internal.toolkit.Configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 * Retrieve and format messages stored in a resource.
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    37
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    38
 *  If you write code that depends on this, you do so at your own risk.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    39
 *  This code and its internal interfaces are subject to change or
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    40
 *  deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * @since 1.2
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * @author Atul M Dambalkar
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * @author Robert Field
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
public class MessageRetriever {
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
     * The global configuration information for this run.
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    private final Configuration configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
     * The location from which to lazily fetch the resource..
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    private final String resourcelocation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     * The lazily fetched resource..
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private ResourceBundle messageRB;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * Initilize the ResourceBundle with the given resource.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     * @param rb the esource bundle to read.
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    public MessageRetriever(ResourceBundle rb) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        this.configuration = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        this.messageRB = rb;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        this.resourcelocation = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * Initilize the ResourceBundle with the given resource.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     * @param configuration the configuration
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     * @param resourcelocation Resource.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    public MessageRetriever(Configuration configuration,
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
                            String resourcelocation) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        this.configuration = configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        this.resourcelocation = resourcelocation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * Get and format message string from resource
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     * @param key selects message from resource
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
    89
     * @param args arguments to be replaced in the message.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * @throws MissingResourceException when the key does not
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * exist in the properties file.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     */
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
    93
    public String getText(String key, Object... args) throws MissingResourceException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        if (messageRB == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
                messageRB = ResourceBundle.getBundle(resourcelocation);
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
            } catch (MissingResourceException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
                throw new Error("Fatal: Resource (" + resourcelocation +
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
                                    ") for javadoc doclets is missing.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        String message = messageRB.getString(key);
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   103
        return MessageFormat.format(message, args);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     * Print error message, increment error count.
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * @param pos the position of the source
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * @param msg message to print
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    private void printError(SourcePosition pos, String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        configuration.root.printError(pos, msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     * Print error message, increment error count.
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     * @param msg message to print
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    private void printError(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        configuration.root.printError(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     * Print warning message, increment warning count.
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     * @param pos the position of the source
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
     * @param msg message to print
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    private void printWarning(SourcePosition pos, String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        configuration.root.printWarning(pos, msg);
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
     * Print warning message, increment warning count.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     * @param msg message to print
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    private void printWarning(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        configuration.root.printWarning(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
     * Print a message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
     * @param pos the position of the source
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     * @param msg message to print
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    private void printNotice(SourcePosition pos, String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        configuration.root.printNotice(pos, msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
     * Print a message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     * @param msg message to print
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    private void printNotice(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        configuration.root.printNotice(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     * Print error message, increment error count.
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
     * @param pos the position of the source
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     * @param key selects message from resource
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   168
     * @param args arguments to be replaced in the message.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     */
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   170
    public void error(SourcePosition pos, String key, Object... args) {
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   171
        printError(pos, getText(key, args));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
     * Print error message, increment error count.
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     * @param key selects message from resource
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   178
     * @param args arguments to be replaced in the message.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     */
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   180
    public void error(String key, Object... args) {
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   181
        printError(getText(key, args));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
     * Print warning message, increment warning count.
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
     * @param pos the position of the source
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
     * @param key selects message from resource
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   189
     * @param args arguments to be replaced in the message.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
     */
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   191
    public void warning(SourcePosition pos, String key, Object... args) {
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   192
        printWarning(pos, getText(key, args));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
     * Print warning message, increment warning count.
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
     * @param key selects message from resource
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   199
     * @param args arguments to be replaced in the message.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     */
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   201
    public void warning(String key, Object... args) {
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   202
        printWarning(getText(key, args));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
     * Print a message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
     * @param pos the position of the source
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     * @param key selects message from resource
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   210
     * @param args arguments to be replaced in the message.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     */
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   212
    public void notice(SourcePosition pos, String key, Object... args) {
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   213
        printNotice(pos, getText(key, args));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
     * Print a message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
     * @param key selects message from resource
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   220
     * @param args arguments to be replaced in the message.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
     */
1864
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   222
    public void notice(String key, Object... args) {
c17be9084212 6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents: 10
diff changeset
   223
        printNotice(getText(key, args));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
}