1 /*
   2  * Copyright (c) 1998, 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.util;
  27 
  28 import java.text.MessageFormat;
  29 import java.util.*;
  30 
  31 import javax.lang.model.element.Element;
  32 
  33 import com.sun.source.util.DocTreePath;
  34 import jdk.javadoc.internal.doclets.toolkit.Configuration;
  35 
  36 import static javax.tools.Diagnostic.Kind.*;
  37 
  38 
  39 /**
  40  * Retrieve and format messages stored in a resource.
  41  *
  42  *  <p><b>This is NOT part of any supported API.
  43  *  If you write code that depends on this, you do so at your own risk.
  44  *  This code and its internal interfaces are subject to change or
  45  *  deletion without notice.</b>
  46  *
  47  * @author Atul M Dambalkar
  48  * @author Robert Field
  49  */
  50 public class MessageRetriever {
  51     /**
  52      * The global configuration information for this run.
  53      */
  54     private final Configuration configuration;
  55 
  56     /**
  57      * The location from which to lazily fetch the resource..
  58      */
  59     private final String resourcelocation;
  60 
  61     /**
  62      * The lazily fetched resource..
  63      */
  64     private ResourceBundle messageRB;
  65 
  66     /**
  67      * Initialize the ResourceBundle with the given resource.
  68      *
  69      * @param rb the resource bundle to read.
  70      */
  71     public MessageRetriever(ResourceBundle rb) {
  72         this.configuration = null;
  73         this.messageRB = rb;
  74         this.resourcelocation = null;
  75     }
  76 
  77     /**
  78      * Initialize the ResourceBundle with the given resource.
  79      *
  80      * @param configuration the configuration
  81      * @param resourcelocation Resource.
  82      */
  83     public MessageRetriever(Configuration configuration,
  84                             String resourcelocation) {
  85         this.configuration = configuration;
  86         this.resourcelocation = resourcelocation;
  87     }
  88 
  89     private void initRB() {
  90         if (messageRB == null) {
  91             try {
  92                 messageRB = ResourceBundle.getBundle(resourcelocation, configuration.getLocale());
  93             } catch (MissingResourceException e) {
  94                 throw new Error("Fatal: Resource (" + resourcelocation
  95                         + ") for javadoc doclets is missing.");
  96             }
  97         }
  98     }
  99 
 100     /**
 101      * Get and format message string from resource
 102      *
 103      * @param key selects message from resource
 104      * @param args arguments to be replaced in the message.
 105      * @return the composed text
 106      * @throws MissingResourceException when the key does not
 107      * exist in the properties file.
 108      */
 109     public String getText(String key, Object... args) throws MissingResourceException {
 110         initRB();
 111         String message = messageRB.getString(key);
 112         return MessageFormat.format(message, args);
 113     }
 114 
 115     /**
 116      * Print error message, increment error count.
 117      *
 118      * @param pos the position of the source
 119      * @param msg message to print
 120      */
 121     private void printError(DocTreePath path, String msg) {
 122         configuration.reporter.print(ERROR, path, msg);
 123     }
 124 
 125     /**
 126      * Print error message, increment error count.
 127      *
 128      * @param msg message to print
 129      */
 130     private void printError(String msg) {
 131         configuration.reporter.print(ERROR, msg);
 132     }
 133 
 134     /**
 135      * Print warning message, increment warning count.
 136      *
 137      * @param pos the position of the source
 138      * @param msg message to print
 139      */
 140     private void printWarning(DocTreePath path, String msg) {
 141         configuration.reporter.print(WARNING, path, msg);
 142     }
 143 
 144     private void printWarning(Element e, String msg) {
 145         configuration.reporter.print(WARNING, e, msg);
 146     }
 147 
 148     /**
 149      * Print warning message, increment warning count.
 150      *
 151      * @param msg message to print
 152      */
 153     private void printWarning(String msg) {
 154         configuration.reporter.print(WARNING, msg);
 155     }
 156 
 157 //    Note: the following do not appear to be needed any more, delete me.
 158 //    /**
 159 //     * Print a message.
 160 //     *
 161 //     * @param pos the position of the source
 162 //     * @param msg message to print
 163 //     */
 164 //    private void printNotice(DocTreePath path, String msg) {
 165 //        DocEnv env = ((RootDocImpl)configuration.root).env;
 166 //        if (env.isQuiet() || env.isSilent()) {
 167 //            return;
 168 //        }
 169 //        configuration.reporter.print(NOTE, path, msg);
 170 //    }
 171 
 172 //    Note: does not appear to be needed any more.
 173 //    /**
 174 //     * Print a message.
 175 //     *
 176 //     * @param pos the position of the source
 177 //     * @param key selects message from resource
 178 //     * @param args arguments to be replaced in the message.
 179 //     */
 180 //    public void notice(DocTreePath path, String key, Object... args) {
 181 //        printNotice(path, getText(key, args));
 182 //    }
 183 
 184     // ERRORS
 185     /**
 186      * Print error message, increment error count.
 187      *
 188      * @param path the path to the source
 189      * @param key selects message from resource
 190      * @param args arguments to be replaced in the message.
 191      */
 192     public void error(DocTreePath path, String key, Object... args) {
 193         printError(path, getText(key, args));
 194     }
 195 
 196     /**
 197      * Print error message, increment error count.
 198      *
 199      * @param key selects message from resource
 200      * @param args arguments to be replaced in the message.
 201      */
 202     public void error(String key, Object... args) {
 203         printError(getText(key, args));
 204     }
 205 
 206     // WARNINGS
 207     /**
 208      * Print warning message, increment warning count.
 209 
 210      * @param path the path to the source
 211      * @param key selects message from resource
 212      * @param args arguments to be replaced in the message.
 213      */
 214     public void warning(DocTreePath path, String key, Object... args) {
 215         if (configuration.showMessage(path, key))
 216             printWarning(path, getText(key, args));
 217     }
 218 
 219     /**
 220      * Print warning message, increment warning count.
 221      *
 222      * @param e element target of the message
 223      * @param key selects message from resource
 224      * @param args arguments to be replaced in the message.
 225      */
 226     public void warning(Element e, String key, Object... args) {
 227         if (configuration.showMessage(e, key))
 228             printWarning(e, getText(key, args));
 229     }
 230 
 231     /**
 232      * Print warning message, increment warning count.
 233      *
 234      * @param key selects message from resource
 235      * @param args arguments to be replaced in the message.
 236      */
 237     public void warning(String key, Object... args) {
 238         printWarning(getText(key, args));
 239     }
 240 
 241     // NOTICES
 242     /**
 243      * Print a message.
 244      *
 245      * @param msg message to print
 246      */
 247     private void printNotice(String msg) {
 248         if (configuration.quiet) {
 249             return;
 250         }
 251         configuration.reporter.print(NOTE, msg);
 252     }
 253 
 254     /**
 255      * Print a message.
 256      *
 257      * @param key selects message from resource
 258      * @param args arguments to be replaced in the message.
 259      */
 260     public void notice(String key, Object... args) {
 261         printNotice(getText(key, args));
 262     }
 263 }