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