Path: blob/master/src/java.base/share/classes/sun/security/util/LocalizedMessage.java
41159 views
/*1* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.util;2627/**28* This class produces formatted and localized messages describing security29* issues. Some messages may be required when the VM is not fully booted. In30* this case, localization resources and classes used for message formatting31* may not be available. When the VM is not booted, the message will not be32* localized, and it will be formatted using simplified message formatting33* code that is contained in this class.34*/3536/*37* Some of this code is executed before the VM is fully booted. Some import38* statements have been omitted to help prevent accidental use of classes that39* may not be available during boot.40*/4142public class LocalizedMessage {4344private static final Resources RESOURCES = new Resources();4546private final String key;4748/**49* A LocalizedMessage can be instantiated with a key and formatted with50* arguments later in the style of MessageFormat. This organization51* allows the actual formatting (and associated permission checks) to be52* avoided unless the resulting string is needed.53* @param key54*/55public LocalizedMessage(String key) {56this.key = key;57}5859/**60* Return a localized string corresponding to the key stored in this61* object, formatted with the provided arguments. This method should only62* be called when the VM is booted and all resources needed to obtain63* and format the localized message are loaded (or can be loaded).64*65* @param arguments The arguments that should be placed in the message66* @return A formatted message string67*/68public String formatLocalized(Object... arguments) {69return getLocalized(key, arguments);70}7172/**73* Return a non-localized string corresponding to the key stored in this74* object, formatted with the provided arguments. All strings are obtained75* from sun.security.util.Resources, and the formatting only supports76* simple positional argument replacement (e.g. {1}).77*78* @param arguments The arguments that should be placed in the message79* @return A formatted message string80*/81public String formatNonlocalized(Object... arguments) {82return getNonlocalized(key, arguments);83}8485/**86* Return a non-localized string corresponding to the provided key, and87* formatted with the provided arguments. All strings are obtained from88* sun.security.util.Resources, and the formatting only supports89* simple positional argument replacement (e.g. {1}).90*91* @param key The key of the desired string in Resources92* @param arguments The arguments that should be placed in the message93* @return A formatted message string94*/95public static String getNonlocalized(String key,96Object... arguments) {9798String value = RESOURCES.getString(key);99if (arguments == null || arguments.length == 0) {100return value;101}102// Classes like StringTokenizer may not be loaded, so parsing103// is performed with String methods104StringBuilder sb = new StringBuilder();105int nextBraceIndex;106while ((nextBraceIndex = value.indexOf('{')) >= 0) {107108String firstPart = value.substring(0, nextBraceIndex);109sb.append(firstPart);110value = value.substring(nextBraceIndex + 1);111112// look for closing brace and argument index113nextBraceIndex = value.indexOf('}');114if (nextBraceIndex < 0) {115// no closing brace116// MessageFormat would throw IllegalArgumentException, but117// that exception class may not be loaded yet118throw new RuntimeException("Unmatched braces");119}120String indexStr = value.substring(0, nextBraceIndex);121try {122int index = Integer.parseInt(indexStr);123sb.append(arguments[index]);124} catch (NumberFormatException e) {125// argument index is not an integer126throw new RuntimeException("not an integer: " + indexStr);127}128value = value.substring(nextBraceIndex + 1);129}130sb.append(value);131return sb.toString();132}133134/**135* Return a localized string corresponding to the provided key, and136* formatted with the provided arguments. This method should only be137* called when the VM is booted and all resources needed to obtain138* and format the localized message are loaded (or can be loaded).139*140* @param key The key of the desired string in the security resource bundle141* @param arguments The arguments that should be placed in the message142* @return A formatted message string143*/144public static String getLocalized(String key, Object... arguments) {145146String value = ResourcesMgr.getString(key);147if (arguments == null) {148return value;149}150java.text.MessageFormat form = new java.text.MessageFormat(value);151return form.format(arguments);152}153154}155156157