Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/util/LocalizedMessage.java
41159 views
1
/*
2
* Copyright (c) 2017, 2018, 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 sun.security.util;
27
28
/**
29
* This class produces formatted and localized messages describing security
30
* issues. Some messages may be required when the VM is not fully booted. In
31
* this case, localization resources and classes used for message formatting
32
* may not be available. When the VM is not booted, the message will not be
33
* localized, and it will be formatted using simplified message formatting
34
* code that is contained in this class.
35
*/
36
37
/*
38
* Some of this code is executed before the VM is fully booted. Some import
39
* statements have been omitted to help prevent accidental use of classes that
40
* may not be available during boot.
41
*/
42
43
public class LocalizedMessage {
44
45
private static final Resources RESOURCES = new Resources();
46
47
private final String key;
48
49
/**
50
* A LocalizedMessage can be instantiated with a key and formatted with
51
* arguments later in the style of MessageFormat. This organization
52
* allows the actual formatting (and associated permission checks) to be
53
* avoided unless the resulting string is needed.
54
* @param key
55
*/
56
public LocalizedMessage(String key) {
57
this.key = key;
58
}
59
60
/**
61
* Return a localized string corresponding to the key stored in this
62
* object, formatted with the provided arguments. This method should only
63
* be called when the VM is booted and all resources needed to obtain
64
* and format the localized message are loaded (or can be loaded).
65
*
66
* @param arguments The arguments that should be placed in the message
67
* @return A formatted message string
68
*/
69
public String formatLocalized(Object... arguments) {
70
return getLocalized(key, arguments);
71
}
72
73
/**
74
* Return a non-localized string corresponding to the key stored in this
75
* object, formatted with the provided arguments. All strings are obtained
76
* from sun.security.util.Resources, and the formatting only supports
77
* simple positional argument replacement (e.g. {1}).
78
*
79
* @param arguments The arguments that should be placed in the message
80
* @return A formatted message string
81
*/
82
public String formatNonlocalized(Object... arguments) {
83
return getNonlocalized(key, arguments);
84
}
85
86
/**
87
* Return a non-localized string corresponding to the provided key, and
88
* formatted with the provided arguments. All strings are obtained from
89
* sun.security.util.Resources, and the formatting only supports
90
* simple positional argument replacement (e.g. {1}).
91
*
92
* @param key The key of the desired string in Resources
93
* @param arguments The arguments that should be placed in the message
94
* @return A formatted message string
95
*/
96
public static String getNonlocalized(String key,
97
Object... arguments) {
98
99
String value = RESOURCES.getString(key);
100
if (arguments == null || arguments.length == 0) {
101
return value;
102
}
103
// Classes like StringTokenizer may not be loaded, so parsing
104
// is performed with String methods
105
StringBuilder sb = new StringBuilder();
106
int nextBraceIndex;
107
while ((nextBraceIndex = value.indexOf('{')) >= 0) {
108
109
String firstPart = value.substring(0, nextBraceIndex);
110
sb.append(firstPart);
111
value = value.substring(nextBraceIndex + 1);
112
113
// look for closing brace and argument index
114
nextBraceIndex = value.indexOf('}');
115
if (nextBraceIndex < 0) {
116
// no closing brace
117
// MessageFormat would throw IllegalArgumentException, but
118
// that exception class may not be loaded yet
119
throw new RuntimeException("Unmatched braces");
120
}
121
String indexStr = value.substring(0, nextBraceIndex);
122
try {
123
int index = Integer.parseInt(indexStr);
124
sb.append(arguments[index]);
125
} catch (NumberFormatException e) {
126
// argument index is not an integer
127
throw new RuntimeException("not an integer: " + indexStr);
128
}
129
value = value.substring(nextBraceIndex + 1);
130
}
131
sb.append(value);
132
return sb.toString();
133
}
134
135
/**
136
* Return a localized string corresponding to the provided key, and
137
* formatted with the provided arguments. This method should only be
138
* called when the VM is booted and all resources needed to obtain
139
* and format the localized message are loaded (or can be loaded).
140
*
141
* @param key The key of the desired string in the security resource bundle
142
* @param arguments The arguments that should be placed in the message
143
* @return A formatted message string
144
*/
145
public static String getLocalized(String key, Object... arguments) {
146
147
String value = ResourcesMgr.getString(key);
148
if (arguments == null) {
149
return value;
150
}
151
java.text.MessageFormat form = new java.text.MessageFormat(value);
152
return form.format(arguments);
153
}
154
155
}
156
157