-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMessagesBundle.java
More file actions
82 lines (71 loc) · 1.86 KB
/
Copy pathMessagesBundle.java
File metadata and controls
82 lines (71 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* © Copyright IBM Corporation 2016.
* © Copyright HCL Technologies Ltd. 2017.
* LICENSE: Apache License, Version 2.0 https://www.apache.org/licenses/LICENSE-2.0
*/
package com.hcl.appscan.sdk;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;
/**
* Wrapper class for using message resource bundles.
*/
public final class MessagesBundle {
private ResourceBundle m_bundle;
/**
* Constructor.
*
* @param name Name of the resource bundle.
* @param locale A specific locale.
* @param cl The class loader to use.
*/
public MessagesBundle(String name, Locale locale, ClassLoader cl) {
m_bundle = ResourceBundle.getBundle(name, locale, cl);
}
/**
* Constructor.
*
* @param name Name of the resource bundle.
* @param cl The class loader to use.
*/
public MessagesBundle(String name, ClassLoader cl) {
this(name, Locale.getDefault(), cl);
}
/**
* Bind a message with the given arguments.
*
* @param message The message.
* @param args Optional list of objects to be inserted into the message.
* @return The modified message.
*/
public static String bind(String message, Object... args) {
if (message.contains("{0}") && args.length > 0) { //$NON-NLS-1$
try {
return MessageFormat.format(message, args);
}
catch (IllegalArgumentException e) {
// just return original message
}
}
return message;
}
/**
* Get a message.
*
* @param key The key for the message.
* @param args Optional list of objects to be inserted into the message.
* @return The message.
*/
public String getMessage(String key, Object... args) {
return bind(m_bundle.getString(key), args);
}
/**
* Return the keys in this message bundle.
*
* @return The keys in this message bundle.
*/
public Set<String> keySet() {
return m_bundle.keySet();
}
}