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