langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/Resources.java
author ksrini
Mon, 05 Jun 2017 13:45:34 -0700
changeset 45417 f7479ee8de69
parent 40303 96a1226aca18
permissions -rw-r--r--
8177848: Rename Configuration(Impl) classes Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
     1
/*
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40303
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
     4
 *
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    10
 *
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    15
 * accompanied this code).
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    16
 *
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    20
 *
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    23
 * questions.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    24
 */
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    25
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    26
package jdk.javadoc.internal.doclets.toolkit;
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    27
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    28
import java.text.MessageFormat;
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    29
import java.util.Locale;
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    30
import java.util.MissingResourceException;
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    31
import java.util.ResourceBundle;
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    32
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    33
/**
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    34
 * Access to the localizable resources used by a doclet.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    35
 * The resources are split across two resource bundles:
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    36
 * one that contains format-neutral strings common to
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    37
 * all supported formats, and one that contains strings
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    38
 * specific to the selected doclet, such as the standard
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    39
 * HTML doclet.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    40
 */
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    41
public class Resources {
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40303
diff changeset
    42
    private final BaseConfiguration configuration;
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    43
    private final String commonBundleName;
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    44
    private final String docletBundleName;
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    45
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    46
    protected ResourceBundle commonBundle;
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    47
    protected ResourceBundle docletBundle;
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    48
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    49
    /**
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    50
     * Creates a {@code Resources} to provide access the resource
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    51
     * bundles used by a doclet.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    52
     *
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    53
     * @param configuration the configuration for the doclet,
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    54
     *  to provide access the locale to be used when accessing the
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    55
     *  names resource bundles.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    56
     * @param commonBundleName the name of the bundle containing the strings
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    57
     *  common to all output formats
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    58
     * @param docletBundleName the name of the bundle containing the strings
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    59
     *  specific to a particular format
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    60
     */
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40303
diff changeset
    61
    public Resources(BaseConfiguration configuration, String commonBundleName, String docletBundleName) {
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    62
        this.configuration = configuration;
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    63
        this.commonBundleName = commonBundleName;
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    64
        this.docletBundleName = docletBundleName;
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    65
    }
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    66
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    67
    /**
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    68
     * Gets the string for the given key from one of the doclet's
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    69
     * resource bundles.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    70
     *
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    71
     * The more specific bundle is checked first;
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    72
     * if it is not there, the common bundle is then checked.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    73
     *
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    74
     * @param key the key for the desired string
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    75
     * @return the string for the given key
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    76
     * @throws MissingResourceException if the key is not found in either
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    77
     *  bundle.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    78
     */
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    79
    public String getText(String key) throws MissingResourceException {
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    80
        initBundles();
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    81
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    82
        if (docletBundle.containsKey(key))
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    83
            return docletBundle.getString(key);
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    84
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    85
        return commonBundle.getString(key);
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    86
    }
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    87
    /**
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    88
     * Gets the string for the given key from one of the doclet's
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    89
     * resource bundles, substituting additional arguments into
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    90
     * into the resulting string with {@link MessageFormat#format}.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    91
     *
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    92
     * The more specific bundle is checked first;
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    93
     * if it is not there, the common bundle is then checked.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    94
     *
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    95
     * @param key the key for the desired string
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    96
     * @param args values to be substituted into the resulting string
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    97
     * @return the string for the given key
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    98
     * @throws MissingResourceException if the key is not found in either
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
    99
     *  bundle.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   100
     */
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   101
    public String getText(String key, Object... args) throws MissingResourceException {
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   102
        return MessageFormat.format(getText(key), args);
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   103
    }
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   104
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   105
    /**
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   106
     * Lazily initializes the bundles. This is (currently) necessary because
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   107
     * this object may be created before the locale to be used is known.
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   108
     */
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   109
    protected void initBundles() {
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   110
        if (commonBundle == null) {
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   111
            Locale locale = configuration.getLocale();
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   112
            this.commonBundle = ResourceBundle.getBundle(commonBundleName, locale);
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   113
            this.docletBundle = ResourceBundle.getBundle(docletBundleName, locale);
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   114
        }
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   115
    }
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents:
diff changeset
   116
}