-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.java
More file actions
61 lines (48 loc) · 1.96 KB
/
Copy pathConfig.java
File metadata and controls
61 lines (48 loc) · 1.96 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
package jp.co.training;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import jp.co.training.book.ItemCode;
public class Config {
// 外部ファイルによる設定が可能な定数
public String delimiter;
public String saveFile;
public String userName;
private static final String DELIMITER_KEY = "delimiter";
private static final String SAVE_FILE_KEY = "save.file";
private static final String USER_NAME_KEY = "user.name";
ResourceBundle rbMessages;
//メッセージ定義ファイルのパス
private static final String MESSAGES_RESOURCE = "messages";
public Config load(String path) {
ResourceBundle rbProperties = null;
try {
rbProperties = ResourceBundle.getBundle(path);
} catch (MissingResourceException e) {
System.out.println("ERROR: cannnot find configfile.");
System.exit(1);
}
if (!rbProperties.containsKey(USER_NAME_KEY)) {
System.out.println("ERROR: you must define registrant in configfile.");
System.exit(1);
}
try {
rbMessages = ResourceBundle.getBundle(MESSAGES_RESOURCE);
} catch (MissingResourceException e) {
System.out.println("ERROR: cannnot find message file.");
System.exit(1);
}
userName = rbProperties.getString(USER_NAME_KEY);
delimiter = (rbProperties.containsKey(DELIMITER_KEY)) ? rbProperties.getString(DELIMITER_KEY) : ",";
saveFile = (rbProperties.containsKey(SAVE_FILE_KEY)) ? rbProperties.getString(DELIMITER_KEY) : "savefile";
return this;
}
public String getMessage(ItemCode code, String... params) {
// メッセージ取得
String msg = rbMessages.getString(code.getCode());
// 可変項目の置換え
for (int i = 0; i < params.length; i++) {
msg.replaceFirst("{" + i + "}", params[i]);
}
return msg;
}
}