summaryrefslogtreecommitdiffstats
path: root/dcaedt_validator/kwalify/src/main/java/kwalify/Messages.java
blob: e0bafb1ca91912659a22fd7340361b665f916585 (plain)
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
/*
 * copyright(c) 2005 kuwata-lab all rights reserved.
 */

package kwalify;

import java.util.ResourceBundle;

/**
 * set of utility methods around messages.
 *
 */
public class Messages {

    private static final String KWALIFY_MESSAGES = "kwalify.messages";
    private static ResourceBundle __messages = ResourceBundle.getBundle(KWALIFY_MESSAGES);

    // So that no one instantiate Messages and make sonar happy
    private Messages(){}

    public static String message(String key) {
        return __messages.getString(key);
    }

    public static String buildMessage(String key, Object[] args) {
        return buildMessage(key, null, args);
    }

    public static String buildMessage(String key, Object value, Object[] args) {
        String msg = message(key);
        if (args != null) {
            for (Object arg : args) {  // don't use MessageFormat
                msg = msg.replaceFirst("%[sd]", escape(arg));
            }
        }
        if (value != null && !Types.isCollection(value)) {
            msg = "'" + escape(value) + "': " + msg;
        }
        return msg;
    }

    private static String escape(Object obj) {
        return obj.toString().replaceAll("\\\\", "\\\\\\\\").replaceAll("\\n", "\\\\n");
    }
}