OSDN Git Service

Eclipse用の設定
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / config / Properties.java
1 package yukihane.inqubus.config;
2
3 import java.io.File;
4 import java.lang.reflect.Field;
5 import java.nio.file.Path;
6 import java.util.ArrayList;
7 import java.util.List;
8 import org.apache.commons.configuration.ConfigurationException;
9 import org.apache.commons.configuration.XMLConfiguration;
10
11 public enum Properties {
12
13     INSTANCE;
14     private final XMLConfiguration config;
15     /** ニコニコ動画ログインID */
16     private String p_nicovideo_id;
17     /** ニコニコ動画ログインパスワード */
18     private String p_nicovideo_password;
19     /** プロキシを使用する */
20     private String p_proxy_use;
21     /** プロキシホスト */
22     private String p_proxy_host;
23     /** プロキシポート */
24     private String p_proxy_port;
25     /** NicoBrowser情報を参照する */
26     private String p_nicobrowser_use;
27     /** NicoBrowser DB ディレクトリ */
28     private String p_nicobrowser_dblocation;
29     /** 動画をダウンロードする */
30     private String p_movie_download;
31     /** 動画ダウンロードディレクトリ */
32     private String p_movie_directory;
33     /** コメントをダウンロードする */
34     private String p_comment_download;
35     /** コメントダウンロードディレクトリ */
36     private String p_comment_directory;
37     /** 投稿者コメントをダウンロードする */
38     private String p_ownercomment_download;
39     /** 投稿者コメントダウンロードディレクトリ */
40     private String p_ownercomment_directory;
41
42     public boolean isOwnerCommentDownload() {
43         return Boolean.valueOf(p_ownercomment_download);
44     }
45
46     public void setOwnerCommentDownload(boolean download) {
47         p_ownercomment_download = Boolean.toString(download);
48     }
49
50     public boolean isCommentDownload() {
51         return Boolean.valueOf(p_comment_download);
52     }
53
54     public void setCommentDownload(boolean download) {
55         p_comment_download = Boolean.toString(download);
56     }
57
58     public File getMovieDirectory() {
59         // TODO return new File(p_movie_directory);
60         return new File("out/movie");
61     }
62
63     public File getCommentDirectory() {
64         // TODO
65         return new File("out/comment");
66     }
67
68     public void setMovieDirectory(String dir) {
69         this.p_movie_directory = dir;
70     }
71
72     public boolean isMovieDownload() {
73         return Boolean.valueOf(p_movie_download);
74     }
75
76     public void setMovieDownload(boolean download) {
77         this.p_movie_download = Boolean.toString(download);
78     }
79
80     public String getDbLocation() {
81         return p_nicobrowser_dblocation;
82     }
83
84     public void setDbLocation(String p_nicobrowser_dblocation) {
85         this.p_nicobrowser_dblocation = p_nicobrowser_dblocation;
86     }
87
88     public boolean isDbUse() {
89         return Boolean.valueOf(p_nicobrowser_use);
90     }
91
92     public void setDbUse(boolean use) {
93         this.p_nicobrowser_use = Boolean.toString(use);
94     }
95
96     public String getNicoVideoId() {
97         return p_nicovideo_id;
98     }
99
100     public void setNicoVideoId(String id) {
101         this.p_nicovideo_id = id;
102     }
103
104     public String getNicoVideoPassword() {
105         return p_nicovideo_password;
106     }
107
108     public void setNicoVideoPassword(String password) {
109         this.p_nicovideo_password = password;
110     }
111
112     public String getProxyHost() {
113         return p_proxy_host;
114     }
115
116     public void setProxyHost(String host) {
117         this.p_proxy_host = host;
118     }
119
120     public int getP_proxy_port() {
121         return Integer.parseInt(p_proxy_port);
122     }
123
124     public void setP_proxy_port(int port) {
125         this.p_proxy_port = Integer.toString(port);
126     }
127
128     public boolean isProxyUse() {
129         return Boolean.valueOf(p_proxy_use);
130     }
131
132     public void setProxyUse(boolean use) {
133         this.p_proxy_use = Boolean.toString(use);
134     }
135
136     private String getPrefix() {
137         return "p_";
138     }
139
140     private Properties() {
141         config = new XMLConfiguration();
142     }
143
144     public void load(String fileName) throws ConfigurationException {
145         config.setFileName(fileName);
146         config.load();
147     }
148
149     public void save() throws ConfigurationException {
150         try {
151             final Class<? extends Properties> clazz = this.getClass();
152             final Field[] fields = clazz.getDeclaredFields();
153             final Field[] propertyFields = getPropertyFields(fields);
154             for (Field f : propertyFields) {
155                 config.setProperty(getPropertyName(f), getPropertyValue(f));
156             }
157             config.save();
158         } catch (Exception ex) {
159             throw new ConfigurationException(ex);
160         }
161     }
162
163     private Field[] getPropertyFields(Field[] fields) {
164         List<Field> res = new ArrayList<Field>();
165         for (Field f : fields) {
166             if (f.getName().startsWith(getPrefix())) {
167                 res.add(f);
168             }
169         }
170         return res.toArray(new Field[0]);
171     }
172
173     private String getPropertyName(Field field) {
174         return field.getName().replace(getPrefix(), "").replace("_", ".");
175     }
176
177     private String getPropertyValue(Field field) throws IllegalArgumentException, IllegalAccessException {
178         field.setAccessible(true);
179         return (String) field.get(this);
180     }
181
182     public boolean getUseMovieFileLocal() {
183         // TODO
184         return false;
185     }
186
187     public boolean getCommentFileLocal() {
188         // TODO
189         return false;
190     }
191
192     public boolean getOutputConvert() {
193         // TODO
194         return true;
195     }
196
197     public String getFileNamePattern() {
198         // TODO
199         return "[{id}]{title}";
200     }
201 }