Tue, 09 Oct 2012 19:10:00 -0700
8000663: clean up langtools imports
Reviewed-by: darcy
duke@1 | 1 | /* |
jjg@1357 | 2 | * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | package com.sun.tools.doclets.internal.toolkit.util; |
duke@1 | 26 | |
jjg@1357 | 27 | import java.text.MessageFormat; |
jjg@1357 | 28 | import java.util.*; |
jjg@1357 | 29 | |
duke@1 | 30 | import com.sun.javadoc.*; |
duke@1 | 31 | import com.sun.tools.doclets.internal.toolkit.Configuration; |
duke@1 | 32 | |
duke@1 | 33 | |
duke@1 | 34 | /** |
duke@1 | 35 | * Retrieve and format messages stored in a resource. |
duke@1 | 36 | * |
duke@1 | 37 | * This code is not part of an API. |
duke@1 | 38 | * It is implementation that is subject to change. |
duke@1 | 39 | * Do not use it as an API |
duke@1 | 40 | * |
duke@1 | 41 | * @since 1.2 |
duke@1 | 42 | * @author Atul M Dambalkar |
duke@1 | 43 | * @author Robert Field |
duke@1 | 44 | */ |
duke@1 | 45 | public class MessageRetriever { |
duke@1 | 46 | /** |
duke@1 | 47 | * The global configuration information for this run. |
duke@1 | 48 | */ |
duke@1 | 49 | private final Configuration configuration; |
duke@1 | 50 | |
duke@1 | 51 | /** |
duke@1 | 52 | * The location from which to lazily fetch the resource.. |
duke@1 | 53 | */ |
duke@1 | 54 | private final String resourcelocation; |
duke@1 | 55 | |
duke@1 | 56 | /** |
duke@1 | 57 | * The lazily fetched resource.. |
duke@1 | 58 | */ |
duke@1 | 59 | private ResourceBundle messageRB; |
duke@1 | 60 | |
duke@1 | 61 | /** |
duke@1 | 62 | * Initilize the ResourceBundle with the given resource. |
duke@1 | 63 | * |
duke@1 | 64 | * @param rb the esource bundle to read. |
duke@1 | 65 | */ |
duke@1 | 66 | public MessageRetriever(ResourceBundle rb) { |
duke@1 | 67 | this.configuration = null; |
duke@1 | 68 | this.messageRB = rb; |
duke@1 | 69 | this.resourcelocation = null; |
duke@1 | 70 | } |
duke@1 | 71 | |
duke@1 | 72 | /** |
duke@1 | 73 | * Initilize the ResourceBundle with the given resource. |
duke@1 | 74 | * |
duke@1 | 75 | * @param configuration the configuration |
duke@1 | 76 | * @param resourcelocation Resource. |
duke@1 | 77 | */ |
duke@1 | 78 | public MessageRetriever(Configuration configuration, |
duke@1 | 79 | String resourcelocation) { |
duke@1 | 80 | this.configuration = configuration; |
duke@1 | 81 | this.resourcelocation = resourcelocation; |
duke@1 | 82 | } |
duke@1 | 83 | |
duke@1 | 84 | /** |
duke@1 | 85 | * Get and format message string from resource |
duke@1 | 86 | * |
duke@1 | 87 | * @param key selects message from resource |
jjg@192 | 88 | * @param args arguments to be replaced in the message. |
duke@1 | 89 | * @throws MissingResourceException when the key does not |
duke@1 | 90 | * exist in the properties file. |
duke@1 | 91 | */ |
jjg@192 | 92 | public String getText(String key, Object... args) throws MissingResourceException { |
duke@1 | 93 | if (messageRB == null) { |
duke@1 | 94 | try { |
duke@1 | 95 | messageRB = ResourceBundle.getBundle(resourcelocation); |
duke@1 | 96 | } catch (MissingResourceException e) { |
duke@1 | 97 | throw new Error("Fatal: Resource (" + resourcelocation + |
duke@1 | 98 | ") for javadoc doclets is missing."); |
duke@1 | 99 | } |
duke@1 | 100 | } |
duke@1 | 101 | String message = messageRB.getString(key); |
jjg@192 | 102 | return MessageFormat.format(message, args); |
duke@1 | 103 | } |
duke@1 | 104 | |
duke@1 | 105 | /** |
duke@1 | 106 | * Print error message, increment error count. |
duke@1 | 107 | * |
duke@1 | 108 | * @param pos the position of the source |
duke@1 | 109 | * @param msg message to print |
duke@1 | 110 | */ |
duke@1 | 111 | private void printError(SourcePosition pos, String msg) { |
duke@1 | 112 | configuration.root.printError(pos, msg); |
duke@1 | 113 | } |
duke@1 | 114 | |
duke@1 | 115 | /** |
duke@1 | 116 | * Print error message, increment error count. |
duke@1 | 117 | * |
duke@1 | 118 | * @param msg message to print |
duke@1 | 119 | */ |
duke@1 | 120 | private void printError(String msg) { |
duke@1 | 121 | configuration.root.printError(msg); |
duke@1 | 122 | } |
duke@1 | 123 | |
duke@1 | 124 | /** |
duke@1 | 125 | * Print warning message, increment warning count. |
duke@1 | 126 | * |
duke@1 | 127 | * @param pos the position of the source |
duke@1 | 128 | * @param msg message to print |
duke@1 | 129 | */ |
duke@1 | 130 | private void printWarning(SourcePosition pos, String msg) { |
duke@1 | 131 | configuration.root.printWarning(pos, msg); |
duke@1 | 132 | } |
duke@1 | 133 | |
duke@1 | 134 | /** |
duke@1 | 135 | * Print warning message, increment warning count. |
duke@1 | 136 | * |
duke@1 | 137 | * @param msg message to print |
duke@1 | 138 | */ |
duke@1 | 139 | private void printWarning(String msg) { |
duke@1 | 140 | configuration.root.printWarning(msg); |
duke@1 | 141 | } |
duke@1 | 142 | |
duke@1 | 143 | /** |
duke@1 | 144 | * Print a message. |
duke@1 | 145 | * |
duke@1 | 146 | * @param pos the position of the source |
duke@1 | 147 | * @param msg message to print |
duke@1 | 148 | */ |
duke@1 | 149 | private void printNotice(SourcePosition pos, String msg) { |
duke@1 | 150 | configuration.root.printNotice(pos, msg); |
duke@1 | 151 | } |
duke@1 | 152 | |
duke@1 | 153 | /** |
duke@1 | 154 | * Print a message. |
duke@1 | 155 | * |
duke@1 | 156 | * @param msg message to print |
duke@1 | 157 | */ |
duke@1 | 158 | private void printNotice(String msg) { |
duke@1 | 159 | configuration.root.printNotice(msg); |
duke@1 | 160 | } |
duke@1 | 161 | |
duke@1 | 162 | /** |
duke@1 | 163 | * Print error message, increment error count. |
duke@1 | 164 | * |
duke@1 | 165 | * @param pos the position of the source |
duke@1 | 166 | * @param key selects message from resource |
jjg@192 | 167 | * @param args arguments to be replaced in the message. |
duke@1 | 168 | */ |
jjg@192 | 169 | public void error(SourcePosition pos, String key, Object... args) { |
jjg@192 | 170 | printError(pos, getText(key, args)); |
duke@1 | 171 | } |
duke@1 | 172 | |
duke@1 | 173 | /** |
duke@1 | 174 | * Print error message, increment error count. |
duke@1 | 175 | * |
duke@1 | 176 | * @param key selects message from resource |
jjg@192 | 177 | * @param args arguments to be replaced in the message. |
duke@1 | 178 | */ |
jjg@192 | 179 | public void error(String key, Object... args) { |
jjg@192 | 180 | printError(getText(key, args)); |
duke@1 | 181 | } |
duke@1 | 182 | |
duke@1 | 183 | /** |
duke@1 | 184 | * Print warning message, increment warning count. |
duke@1 | 185 | * |
duke@1 | 186 | * @param pos the position of the source |
duke@1 | 187 | * @param key selects message from resource |
jjg@192 | 188 | * @param args arguments to be replaced in the message. |
duke@1 | 189 | */ |
jjg@192 | 190 | public void warning(SourcePosition pos, String key, Object... args) { |
jjg@192 | 191 | printWarning(pos, getText(key, args)); |
duke@1 | 192 | } |
duke@1 | 193 | |
duke@1 | 194 | /** |
duke@1 | 195 | * Print warning message, increment warning count. |
duke@1 | 196 | * |
duke@1 | 197 | * @param key selects message from resource |
jjg@192 | 198 | * @param args arguments to be replaced in the message. |
duke@1 | 199 | */ |
jjg@192 | 200 | public void warning(String key, Object... args) { |
jjg@192 | 201 | printWarning(getText(key, args)); |
duke@1 | 202 | } |
duke@1 | 203 | |
duke@1 | 204 | /** |
duke@1 | 205 | * Print a message. |
duke@1 | 206 | * |
duke@1 | 207 | * @param pos the position of the source |
duke@1 | 208 | * @param key selects message from resource |
jjg@192 | 209 | * @param args arguments to be replaced in the message. |
duke@1 | 210 | */ |
jjg@192 | 211 | public void notice(SourcePosition pos, String key, Object... args) { |
jjg@192 | 212 | printNotice(pos, getText(key, args)); |
duke@1 | 213 | } |
duke@1 | 214 | |
duke@1 | 215 | /** |
duke@1 | 216 | * Print a message. |
duke@1 | 217 | * |
duke@1 | 218 | * @param key selects message from resource |
jjg@192 | 219 | * @param args arguments to be replaced in the message. |
duke@1 | 220 | */ |
jjg@192 | 221 | public void notice(String key, Object... args) { |
jjg@192 | 222 | printNotice(getText(key, args)); |
duke@1 | 223 | } |
duke@1 | 224 | } |