1 /*
   2  * Copyright (c) 2016, 2018, 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     public final String annotationTypeSummary;
  43     public final String classSummary;
  44     private final BaseConfiguration configuration;
  45     private final String commonBundleName;
  46     private final String docletBundleName;
  47     public final String enumSummary;
  48     public final String errorSummary;
  49     public final String exceptionSummary;
  50     public final String interfaceSummary;
  51     public final String packageSummary;
  52 
  53     protected ResourceBundle commonBundle;
  54     protected ResourceBundle docletBundle;
  55 
  56     /**
  57      * Creates a {@code Resources} to provide access the resource
  58      * bundles used by a doclet.
  59      *
  60      * @param configuration the configuration for the doclet,
  61      *  to provide access the locale to be used when accessing the
  62      *  names resource bundles.
  63      * @param commonBundleName the name of the bundle containing the strings
  64      *  common to all output formats
  65      * @param docletBundleName the name of the bundle containing the strings
  66      *  specific to a particular format
  67      */
  68     public Resources(BaseConfiguration configuration, String commonBundleName, String docletBundleName) {
  69         this.configuration = configuration;
  70         this.commonBundleName = commonBundleName;
  71         this.docletBundleName = docletBundleName;
  72         this.annotationTypeSummary = getText("doclet.Annotation_Types_Summary");
  73         this.classSummary = getText("doclet.Class_Summary");
  74         this.enumSummary = getText("doclet.Enum_Summary");
  75         this.errorSummary = getText("doclet.Error_Summary");
  76         this.exceptionSummary = getText("doclet.Exception_Summary");
  77         this.interfaceSummary = getText("doclet.Interface_Summary");
  78         this.packageSummary = getText("doclet.Package_Summary");
  79     }
  80 
  81     /**
  82      * Gets the string for the given key from one of the doclet's
  83      * resource bundles.
  84      *
  85      * The more specific bundle is checked first;
  86      * if it is not there, the common bundle is then checked.
  87      *
  88      * @param key the key for the desired string
  89      * @return the string for the given key
  90      * @throws MissingResourceException if the key is not found in either
  91      *  bundle.
  92      */
  93     public String getText(String key) throws MissingResourceException {
  94         initBundles();
  95 
  96         if (docletBundle.containsKey(key))
  97             return docletBundle.getString(key);
  98 
  99         return commonBundle.getString(key);
 100     }
 101     /**
 102      * Gets the string for the given key from one of the doclet's
 103      * resource bundles, substituting additional arguments into
 104      * into the resulting string with {@link MessageFormat#format}.
 105      *
 106      * The more specific bundle is checked first;
 107      * if it is not there, the common bundle is then checked.
 108      *
 109      * @param key the key for the desired string
 110      * @param args values to be substituted into the resulting string
 111      * @return the string for the given key
 112      * @throws MissingResourceException if the key is not found in either
 113      *  bundle.
 114      */
 115     public String getText(String key, Object... args) throws MissingResourceException {
 116         return MessageFormat.format(getText(key), args);
 117     }
 118 
 119     /**
 120      * Lazily initializes the bundles. This is (currently) necessary because
 121      * this object may be created before the locale to be used is known.
 122      */
 123     protected void initBundles() {
 124         if (commonBundle == null) {
 125             Locale locale = configuration.getLocale();
 126             this.commonBundle = ResourceBundle.getBundle(commonBundleName, locale);
 127             this.docletBundle = ResourceBundle.getBundle(docletBundleName, locale);
 128         }
 129     }
 130 }