langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/Resources.java
changeset 40303 96a1226aca18
child 45417 f7479ee8de69
equal deleted inserted replaced
40302:8c0d8d2c3519 40303:96a1226aca18
       
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.javadoc.internal.doclets.toolkit;
       
    27 
       
    28 import java.text.MessageFormat;
       
    29 import java.util.Locale;
       
    30 import java.util.MissingResourceException;
       
    31 import java.util.ResourceBundle;
       
    32 
       
    33 /**
       
    34  * Access to the localizable resources used by a doclet.
       
    35  * The resources are split across two resource bundles:
       
    36  * one that contains format-neutral strings common to
       
    37  * all supported formats, and one that contains strings
       
    38  * specific to the selected doclet, such as the standard
       
    39  * HTML doclet.
       
    40  */
       
    41 public class Resources {
       
    42     private final Configuration configuration;
       
    43     private final String commonBundleName;
       
    44     private final String docletBundleName;
       
    45 
       
    46     protected ResourceBundle commonBundle;
       
    47     protected ResourceBundle docletBundle;
       
    48 
       
    49     /**
       
    50      * Creates a {@code Resources} to provide access the resource
       
    51      * bundles used by a doclet.
       
    52      *
       
    53      * @param configuration the configuration for the doclet,
       
    54      *  to provide access the locale to be used when accessing the
       
    55      *  names resource bundles.
       
    56      * @param commonBundleName the name of the bundle containing the strings
       
    57      *  common to all output formats
       
    58      * @param docletBundleName the name of the bundle containing the strings
       
    59      *  specific to a particular format
       
    60      */
       
    61     public Resources(Configuration configuration, String commonBundleName, String docletBundleName) {
       
    62         this.configuration = configuration;
       
    63         this.commonBundleName = commonBundleName;
       
    64         this.docletBundleName = docletBundleName;
       
    65     }
       
    66 
       
    67     /**
       
    68      * Gets the string for the given key from one of the doclet's
       
    69      * resource bundles.
       
    70      *
       
    71      * The more specific bundle is checked first;
       
    72      * if it is not there, the common bundle is then checked.
       
    73      *
       
    74      * @param key the key for the desired string
       
    75      * @return the string for the given key
       
    76      * @throws MissingResourceException if the key is not found in either
       
    77      *  bundle.
       
    78      */
       
    79     public String getText(String key) throws MissingResourceException {
       
    80         initBundles();
       
    81 
       
    82         if (docletBundle.containsKey(key))
       
    83             return docletBundle.getString(key);
       
    84 
       
    85         return commonBundle.getString(key);
       
    86     }
       
    87     /**
       
    88      * Gets the string for the given key from one of the doclet's
       
    89      * resource bundles, substituting additional arguments into
       
    90      * into the resulting string with {@link MessageFormat#format}.
       
    91      *
       
    92      * The more specific bundle is checked first;
       
    93      * if it is not there, the common bundle is then checked.
       
    94      *
       
    95      * @param key the key for the desired string
       
    96      * @param args values to be substituted into the resulting string
       
    97      * @return the string for the given key
       
    98      * @throws MissingResourceException if the key is not found in either
       
    99      *  bundle.
       
   100      */
       
   101     public String getText(String key, Object... args) throws MissingResourceException {
       
   102         return MessageFormat.format(getText(key), args);
       
   103     }
       
   104 
       
   105     /**
       
   106      * Lazily initializes the bundles. This is (currently) necessary because
       
   107      * this object may be created before the locale to be used is known.
       
   108      */
       
   109     protected void initBundles() {
       
   110         if (commonBundle == null) {
       
   111             Locale locale = configuration.getLocale();
       
   112             this.commonBundle = ResourceBundle.getBundle(commonBundleName, locale);
       
   113             this.docletBundle = ResourceBundle.getBundle(docletBundleName, locale);
       
   114         }
       
   115     }
       
   116 }