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