src/share/classes/com/sun/tools/doclets/internal/toolkit/util/MessageRetriever.java

changeset 1
9a66ca7c79fa
child 192
42f9d392159d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/MessageRetriever.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,455 @@
     1.4 +/*
     1.5 + * Copyright 1998-2006 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 +package com.sun.tools.doclets.internal.toolkit.util;
    1.29 +
    1.30 +import com.sun.javadoc.*;
    1.31 +import com.sun.tools.doclets.internal.toolkit.Configuration;
    1.32 +import java.util.*;
    1.33 +import java.text.MessageFormat;
    1.34 +
    1.35 +
    1.36 +/**
    1.37 + * Retrieve and format messages stored in a resource.
    1.38 + *
    1.39 + * This code is not part of an API.
    1.40 + * It is implementation that is subject to change.
    1.41 + * Do not use it as an API
    1.42 + *
    1.43 + * @since 1.2
    1.44 + * @author Atul M Dambalkar
    1.45 + * @author Robert Field
    1.46 + */
    1.47 +public class MessageRetriever {
    1.48 +    /**
    1.49 +     * The global configuration information for this run.
    1.50 +     */
    1.51 +    private final Configuration configuration;
    1.52 +
    1.53 +    /**
    1.54 +     * The location from which to lazily fetch the resource..
    1.55 +     */
    1.56 +    private final String resourcelocation;
    1.57 +
    1.58 +    /**
    1.59 +     * The lazily fetched resource..
    1.60 +     */
    1.61 +    private ResourceBundle messageRB;
    1.62 +
    1.63 +    /**
    1.64 +     * Initilize the ResourceBundle with the given resource.
    1.65 +     *
    1.66 +     * @param rb the esource bundle to read.
    1.67 +     */
    1.68 +    public MessageRetriever(ResourceBundle rb) {
    1.69 +        this.configuration = null;
    1.70 +        this.messageRB = rb;
    1.71 +        this.resourcelocation = null;
    1.72 +    }
    1.73 +
    1.74 +    /**
    1.75 +     * Initilize the ResourceBundle with the given resource.
    1.76 +     *
    1.77 +     * @param configuration the configuration
    1.78 +     * @param resourcelocation Resource.
    1.79 +     */
    1.80 +    public MessageRetriever(Configuration configuration,
    1.81 +                            String resourcelocation) {
    1.82 +        this.configuration = configuration;
    1.83 +        this.resourcelocation = resourcelocation;
    1.84 +    }
    1.85 +
    1.86 +    /**
    1.87 +     * get and format message string from resource
    1.88 +     *
    1.89 +     * @param key selects message from resource
    1.90 +     */
    1.91 +    public String getText(String key) {
    1.92 +        return getText(key, (String)null);
    1.93 +    }
    1.94 +
    1.95 +    /**
    1.96 +     * Get and format message string from resource
    1.97 +     *
    1.98 +     * @param key selects message from resource
    1.99 +     * @param a1 Argument, to be repalced in the message.
   1.100 +     */
   1.101 +    public String getText(String key, String a1) {
   1.102 +        return getText(key, a1, null);
   1.103 +    }
   1.104 +
   1.105 +    /**
   1.106 +     * Get and format message string from resource
   1.107 +     *
   1.108 +     * @param key selects message from resource
   1.109 +     * @param a1 first argument to be replaced in the message.
   1.110 +     * @param a2 second argument to be replaced in the message.
   1.111 +     */
   1.112 +    public String getText(String key, String a1, String a2) {
   1.113 +        return getText(key, a1, a2, null);
   1.114 +    }
   1.115 +
   1.116 +    /**
   1.117 +     * Get and format message string from resource
   1.118 +     *
   1.119 +     * @param key selects message from resource
   1.120 +     * @param a1 first argument to be replaced in the message.
   1.121 +     * @param a2 second argument to be replaced in the message.
   1.122 +     * @param a3 third argument to be replaced in the message.
   1.123 +     * @throws MissingResourceException when the key does not
   1.124 +     * exist in the properties file.
   1.125 +     */
   1.126 +    public String getText(String key, String a1, String a2, String a3) throws MissingResourceException {
   1.127 +        if (messageRB == null) {
   1.128 +            try {
   1.129 +                messageRB = ResourceBundle.getBundle(resourcelocation);
   1.130 +            } catch (MissingResourceException e) {
   1.131 +                throw new Error("Fatal: Resource (" + resourcelocation +
   1.132 +                                    ") for javadoc doclets is missing.");
   1.133 +            }
   1.134 +        }
   1.135 +        String message = messageRB.getString(key);
   1.136 +        return MessageFormat.format(message, a1, a2, a3);
   1.137 +    }
   1.138 +
   1.139 +    /**
   1.140 +     * Print error message, increment error count.
   1.141 +     *
   1.142 +     * @param pos the position of the source
   1.143 +     * @param msg message to print
   1.144 +     */
   1.145 +    private void printError(SourcePosition pos, String msg) {
   1.146 +        configuration.root.printError(pos, msg);
   1.147 +    }
   1.148 +
   1.149 +    /**
   1.150 +     * Print error message, increment error count.
   1.151 +     *
   1.152 +     * @param msg message to print
   1.153 +     */
   1.154 +    private void printError(String msg) {
   1.155 +        configuration.root.printError(msg);
   1.156 +    }
   1.157 +
   1.158 +    /**
   1.159 +     * Print warning message, increment warning count.
   1.160 +     *
   1.161 +     * @param pos the position of the source
   1.162 +     * @param msg message to print
   1.163 +     */
   1.164 +    private void printWarning(SourcePosition pos, String msg) {
   1.165 +        configuration.root.printWarning(pos, msg);
   1.166 +    }
   1.167 +
   1.168 +    /**
   1.169 +     * Print warning message, increment warning count.
   1.170 +     *
   1.171 +     * @param msg message to print
   1.172 +     */
   1.173 +    private void printWarning(String msg) {
   1.174 +        configuration.root.printWarning(msg);
   1.175 +    }
   1.176 +
   1.177 +    /**
   1.178 +     * Print a message.
   1.179 +     *
   1.180 +     * @param pos the position of the source
   1.181 +     * @param msg message to print
   1.182 +     */
   1.183 +    private void printNotice(SourcePosition pos, String msg) {
   1.184 +        configuration.root.printNotice(pos, msg);
   1.185 +    }
   1.186 +
   1.187 +    /**
   1.188 +     * Print a message.
   1.189 +     *
   1.190 +     * @param msg message to print
   1.191 +     */
   1.192 +    private void printNotice(String msg) {
   1.193 +        configuration.root.printNotice(msg);
   1.194 +    }
   1.195 +
   1.196 +    /**
   1.197 +     * Print error message, increment error count.
   1.198 +     *
   1.199 +     * @param pos the position of the source
   1.200 +     * @param key selects message from resource
   1.201 +     */
   1.202 +    public void error(SourcePosition pos, String key) {
   1.203 +        printError(pos, getText(key));
   1.204 +    }
   1.205 +
   1.206 +    /**
   1.207 +     * Print error message, increment error count.
   1.208 +     *
   1.209 +     * @param key selects message from resource
   1.210 +     */
   1.211 +    public void error(String key) {
   1.212 +        printError(getText(key));
   1.213 +    }
   1.214 +
   1.215 +    /**
   1.216 +     * Print error message, increment error count.
   1.217 +     *
   1.218 +     * @param pos the position of the source
   1.219 +     * @param key selects message from resource
   1.220 +     * @param a1 first argument to be replaced in the message.
   1.221 +     */
   1.222 +    public void error(SourcePosition pos, String key, String a1) {
   1.223 +        printError(pos, getText(key, a1));
   1.224 +    }
   1.225 +
   1.226 +    /**
   1.227 +     * Print error message, increment error count.
   1.228 +     *
   1.229 +     * @param key selects message from resource
   1.230 +     * @param a1 first argument to be replaced in the message.
   1.231 +     */
   1.232 +    public void error(String key, String a1) {
   1.233 +        printError(getText(key, a1));
   1.234 +    }
   1.235 +
   1.236 +    /**
   1.237 +     * Print error message, increment error count.
   1.238 +     *
   1.239 +     * @param pos the position of the source
   1.240 +     * @param key selects message from resource
   1.241 +     * @param a1 first argument to be replaced in the message.
   1.242 +     * @param a2 second argument to be replaced in the message.
   1.243 +     */
   1.244 +    public void error(SourcePosition pos, String key, String a1, String a2) {
   1.245 +        printError(pos, getText(key, a1, a2));
   1.246 +    }
   1.247 +
   1.248 +    /**
   1.249 +     * Print error message, increment error count.
   1.250 +     *
   1.251 +     * @param key selects message from resource
   1.252 +     * @param a1 first argument to be replaced in the message.
   1.253 +     * @param a2 second argument to be replaced in the message.
   1.254 +     */
   1.255 +    public void error(String key, String a1, String a2) {
   1.256 +        printError(getText(key, a1, a2));
   1.257 +    }
   1.258 +
   1.259 +    /**
   1.260 +     * Print error message, increment error count.
   1.261 +     *
   1.262 +     * @param pos the position of the source
   1.263 +     * @param key selects message from resource
   1.264 +     * @param a1 first argument to be replaced in the message.
   1.265 +     * @param a2 second argument to be replaced in the message.
   1.266 +     * @param a3 third argument to be replaced in the message.
   1.267 +     */
   1.268 +    public void error(SourcePosition pos, String key, String a1, String a2, String a3) {
   1.269 +        printError(pos, getText(key, a1, a2, a3));
   1.270 +    }
   1.271 +
   1.272 +    /**
   1.273 +     * Print error message, increment error count.
   1.274 +     *
   1.275 +     * @param key selects message from resource
   1.276 +     * @param a1 first argument to be replaced in the message.
   1.277 +     * @param a2 second argument to be replaced in the message.
   1.278 +     * @param a3 third argument to be replaced in the message.
   1.279 +     */
   1.280 +    public void error(String key, String a1, String a2, String a3) {
   1.281 +        printError(getText(key, a1, a2, a3));
   1.282 +    }
   1.283 +
   1.284 +    /**
   1.285 +     * Print warning message, increment warning count.
   1.286 +     *
   1.287 +     * @param pos the position of the source
   1.288 +     * @param key selects message from resource
   1.289 +     */
   1.290 +    public void warning(SourcePosition pos, String key) {
   1.291 +        printWarning(pos, getText(key));
   1.292 +    }
   1.293 +
   1.294 +    /**
   1.295 +     * Print warning message, increment warning count.
   1.296 +     *
   1.297 +     * @param key selects message from resource
   1.298 +     */
   1.299 +    public void warning(String key) {
   1.300 +        printWarning(getText(key));
   1.301 +    }
   1.302 +
   1.303 +    /**
   1.304 +     * Print warning message, increment warning count.
   1.305 +     *
   1.306 +     * @param pos the position of the source
   1.307 +     * @param key selects message from resource
   1.308 +     * @param a1 first argument to be replaced in the message.
   1.309 +     */
   1.310 +    public void warning(SourcePosition pos, String key, String a1) {
   1.311 +        printWarning(pos, getText(key, a1));
   1.312 +    }
   1.313 +
   1.314 +    /**
   1.315 +     * Print warning message, increment warning count.
   1.316 +     *
   1.317 +     * @param key selects message from resource
   1.318 +     * @param a1 first argument to be replaced in the message.
   1.319 +     */
   1.320 +    public void warning(String key, String a1) {
   1.321 +        printWarning(getText(key, a1));
   1.322 +    }
   1.323 +
   1.324 +    /**
   1.325 +     * Print warning message, increment warning count.
   1.326 +     *
   1.327 +     * @param pos the position of the source
   1.328 +     * @param key selects message from resource
   1.329 +     * @param a1 first argument to be replaced in the message.
   1.330 +     * @param a2 second argument to be replaced in the message.
   1.331 +     */
   1.332 +    public void warning(SourcePosition pos, String key, String a1, String a2) {
   1.333 +        printWarning(pos, getText(key, a1, a2));
   1.334 +    }
   1.335 +
   1.336 +    /**
   1.337 +     * Print warning message, increment warning count.
   1.338 +     *
   1.339 +     * @param key selects message from resource
   1.340 +     * @param a1 first argument to be replaced in the message.
   1.341 +     * @param a2 second argument to be replaced in the message.
   1.342 +     */
   1.343 +    public void warning(String key, String a1, String a2) {
   1.344 +        printWarning(getText(key, a1, a2));
   1.345 +    }
   1.346 +
   1.347 +    /**
   1.348 +     * Print warning message, increment warning count.
   1.349 +     *
   1.350 +     * @param pos the position of the source
   1.351 +     * @param key selects message from resource
   1.352 +     * @param a1 first argument to be replaced in the message.
   1.353 +     * @param a2 second argument to be replaced in the message.
   1.354 +     * @param a3 third argument to be replaced in the message.
   1.355 +     */
   1.356 +    public void warning(SourcePosition pos, String key, String a1, String a2, String a3) {
   1.357 +        printWarning(pos, getText(key, a1, a2, a3));
   1.358 +    }
   1.359 +
   1.360 +    /**
   1.361 +     * Print warning message, increment warning count.
   1.362 +     *
   1.363 +     * @param key selects message from resource
   1.364 +     * @param a1 first argument to be replaced in the message.
   1.365 +     * @param a2 second argument to be replaced in the message.
   1.366 +     * @param a3 third argument to be replaced in the message.
   1.367 +     */
   1.368 +    public void warning(String key, String a1, String a2, String a3) {
   1.369 +        printWarning(getText(key, a1, a2, a3));
   1.370 +    }
   1.371 +
   1.372 +    /**
   1.373 +     * Print a message.
   1.374 +     *
   1.375 +     * @param pos the position of the source
   1.376 +     * @param key selects message from resource
   1.377 +     */
   1.378 +    public void notice(SourcePosition pos, String key) {
   1.379 +        printNotice(pos, getText(key));
   1.380 +    }
   1.381 +
   1.382 +    /**
   1.383 +     * Print a message.
   1.384 +     *
   1.385 +     * @param key selects message from resource
   1.386 +     */
   1.387 +    public void notice(String key) {
   1.388 +        printNotice(getText(key));
   1.389 +    }
   1.390 +
   1.391 +    /**
   1.392 +     * Print a message.
   1.393 +     * @param pos the position of the source
   1.394 +     * @param key selects message from resource
   1.395 +     * @param a1 first argument to be replaced in the message.
   1.396 +     */
   1.397 +    public void notice(SourcePosition pos, String key, String a1) {
   1.398 +        printNotice(pos, getText(key, a1));
   1.399 +    }
   1.400 +
   1.401 +    /**
   1.402 +     * Print a message.
   1.403 +     *
   1.404 +     * @param key selects message from resource
   1.405 +     * @param a1 first argument to be replaced in the message.
   1.406 +     */
   1.407 +    public void notice(String key, String a1) {
   1.408 +        printNotice(getText(key, a1));
   1.409 +    }
   1.410 +
   1.411 +    /**
   1.412 +     * Print a message.
   1.413 +     *
   1.414 +     * @param pos the position of the source
   1.415 +     * @param key selects message from resource
   1.416 +     * @param a1 first argument to be replaced in the message.
   1.417 +     * @param a2 second argument to be replaced in the message.
   1.418 +     */
   1.419 +    public void notice(SourcePosition pos, String key, String a1, String a2) {
   1.420 +        printNotice(pos, getText(key, a1, a2));
   1.421 +    }
   1.422 +
   1.423 +    /**
   1.424 +     * Print a message.
   1.425 +     *
   1.426 +     * @param key selects message from resource
   1.427 +     * @param a1 first argument to be replaced in the message.
   1.428 +     * @param a2 second argument to be replaced in the message.
   1.429 +     */
   1.430 +    public void notice(String key, String a1, String a2) {
   1.431 +        printNotice(getText(key, a1, a2));
   1.432 +    }
   1.433 +
   1.434 +    /**
   1.435 +     * Print a message.
   1.436 +     *
   1.437 +     * @param pos the position of the source
   1.438 +     * @param key selects message from resource
   1.439 +     * @param a1 first argument to be replaced in the message.
   1.440 +     * @param a2 second argument to be replaced in the message.
   1.441 +     * @param a3 third argument to be replaced in the message.
   1.442 +     */
   1.443 +    public void notice(SourcePosition pos, String key, String a1, String a2, String a3) {
   1.444 +        printNotice(pos, getText(key, a1, a2, a3));
   1.445 +    }
   1.446 +
   1.447 +    /**
   1.448 +     * Print a message.
   1.449 +     *
   1.450 +     * @param key selects message from resource
   1.451 +     * @param a1 first argument to be replaced in the message.
   1.452 +     * @param a2 second argument to be replaced in the message.
   1.453 +     * @param a3 third argument to be replaced in the message.
   1.454 +     */
   1.455 +    public void notice(String key, String a1, String a2, String a3) {
   1.456 +        printNotice(getText(key, a1, a2, a3));
   1.457 +    }
   1.458 +}

mercurial