001/*
002 * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package org.jdrupes.mdoclet.internal.doclets.toolkit;
027
028import java.text.MessageFormat;
029import java.util.Locale;
030import java.util.MissingResourceException;
031import java.util.ResourceBundle;
032import java.util.function.Function;
033
034/**
035 * Access to the localizable resources used by a doclet.
036 * The resources are split across two resource bundles:
037 * one that contains format-neutral strings common to
038 * all supported formats, and one that contains strings
039 * specific to the selected doclet, such as the standard
040 * HTML doclet.
041 */
042public class Resources {
043
044    protected final ResourceBundle commonBundle;
045    protected final ResourceBundle docletBundle;
046    protected Function<String, String> mapper;
047
048    /**
049     * Creates a {@code Resources} object to provide access the resource
050     * bundles used by a doclet.
051     *
052     * @param locale           the locale to be used when accessing the
053     *                         resource bundles.
054     * @param commonBundleName the name of the bundle containing the strings
055     *                         common to all output formats
056     * @param docletBundleName the name of the bundle containing the strings
057     *                         specific to a particular format
058     */
059    public Resources(Locale locale, String commonBundleName, String docletBundleName) {
060        this.commonBundle = ResourceBundle.getBundle(commonBundleName, locale);
061        this.docletBundle = ResourceBundle.getBundle(docletBundleName, locale);
062    }
063
064    public void setKeyMapper(Function<String, String> mapper) {
065        this.mapper = mapper;
066    }
067
068    /**
069     * Returns the string for the given key from one of the doclet's
070     * resource bundles. If the current {@code mapper} is not {@code null},
071     * it will be applied to the {@code key} before looking up the resulting
072     * key in the resource bundle(s).
073     *
074     * The more specific bundle is checked first;
075     * if it is not there, the common bundle is then checked.
076     *
077     * @param key the key for the desired string
078     * @return the string for the given key
079     * @throws MissingResourceException if the key is not found in either
080     *                                  bundle.
081     */
082    public String getText(String key) throws MissingResourceException {
083        String mKey = mapper == null ? key : mapper.apply(key);
084
085        if (docletBundle.containsKey(mKey))
086            return docletBundle.getString(mKey);
087
088        return commonBundle.getString(mKey);
089    }
090
091    /**
092     * Returns the string for the given key (after applying the current
093     * {@code mapper} if it is not {@code null}) from one of the doclet's
094     * resource bundles, substituting additional arguments into
095     * into the resulting string with {@link MessageFormat#format}.
096     *
097     * The more specific bundle is checked first;
098     * if it is not there, the common bundle is then checked.
099     *
100     * @param key the key for the desired string
101     * @param args values to be substituted into the resulting string
102     * @return the string for the given key
103     * @throws MissingResourceException if the key is not found in either
104     *  bundle.
105     */
106    public String getText(String key, Object... args) throws MissingResourceException {
107        return MessageFormat.format(getText(key), args);
108    }
109}