Debian Codegraph @

src/main/java/ezvcard/Messages.java

raw symbols SCIP
1 package ezvcard;
2
3 import java.text.MessageFormat;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
6
7 /*
8 Copyright (c) 2012-2023, Michael Angstadt
9 All rights reserved.
10
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13
14 1. Redistributions of source code must retain the above copyright notice, this
15 list of conditions and the following disclaimer.
16 2. Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /**
33 * Singleton for accessing the i18n resource bundle.
34 * @author Michael Angstadt
35 */
36 public enum Messages {
37 INSTANCE;
38
39 private final ResourceBundle messages;
40
41 Messages() {
42 messages = ResourceBundle.getBundle("ezvcard/messages");
43 }
44
45 /**
46 * Gets a validation warning message.
47 * @param code the message code
48 * @param args the message arguments
49 * @return the message or null if not found
50 */
51 public String getValidationWarning(int code, Object... args) {
52 return getMessage("validate." + code, args);
53 }
54
55 /**
56 * Gets a parser warning message.
57 * @param code the message code
58 * @param args the message arguments
59 * @return the message or null if not found
60 */
61 public String getParseMessage(int code, Object... args) {
62 return getMessage("parse." + code, args);
63 }
64
65 /**
66 * Gets an exception message.
67 * @param code the message code
68 * @param args the message arguments
69 * @return the message or null if not found
70 */
71 public String getExceptionMessage(int code, Object... args) {
72 String message = getMessage("exception." + code, args);
73 return (message == null) ? null : getMessage("exception.0", code, message);
74 }
75
76 /**
77 * Builds an {@link IllegalArgumentException} from an exception message.
78 * @param code the message code
79 * @param args the message arguments
80 * @return the exception or null if the message was not found
81 */
82 public IllegalArgumentException getIllegalArgumentException(int code, Object... args) {
83 String message = getExceptionMessage(code, args);
84 return (message == null) ? null : new IllegalArgumentException(message);
85 }
86
87 /**
88 * Gets a message.
89 * @param key the message key
90 * @param args the message arguments
91 * @return the message or null if not found
92 */
93 public String getMessage(String key, Object... args) {
94 try {
95 String message = messages.getString(key);
96 return MessageFormat.format(message, args);
97 } catch (MissingResourceException e) {
98 return null;
99 }
100 }
101 }