-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginSettingUtil.java
61 lines (54 loc) · 1.78 KB
/
PluginSettingUtil.java
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.nicovideo.eka2513.commentviewerj.util;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import jp.co.nicovideo.eka2513.commentviewerj.constants.CommentViewerConstants;
import jp.co.nicovideo.eka2513.commentviewerj.plugin.form.sample.ChukeiPlugin;
public class PluginSettingUtil<T> {
public static void main(String[] args) {
ChukeiPlugin plugin = new ChukeiPlugin();
new PluginSettingUtil<ChukeiPlugin>().save(plugin);
plugin = new PluginSettingUtil<ChukeiPlugin>().load(
CommentViewerConstants.PLUGIN_XML_DIR + ChukeiPlugin.class.getName() + ".dat"
);
}
/**
* インスタンスをxmlにセーブします
* @param object
*/
public void save(T object) {
// new SerializerUtil<T>().save(createFilename(object), object);
try {
XMLEncoder xmlEncoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream(createFilename(object))));
xmlEncoder.writeObject(object);
xmlEncoder.close();
} catch (FileNotFoundException ignore) {
}
}
/**
* インスタンスをロードします
* @param filename ファイル名
* @return インスタンス
*/
public T load(String filename) {
// return new SerializerUtil<T>().load(filename);
try {
XMLDecoder xmlDecoder = new XMLDecoder(new BufferedInputStream(
new FileInputStream(filename)));
@SuppressWarnings("unchecked")
T object = (T) xmlDecoder.readObject();
xmlDecoder.close();
return object;
} catch (FileNotFoundException e) {
return null;
}
}
public String createFilename(T object) {
return CommentViewerConstants.PLUGIN_XML_DIR + object.getClass().getName() + ".dat";
}
}