import java.io.File;
import java.io.FileInputStream;
-import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.InputStream;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Properties;
+import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public final class Config {
+ /**
+ * feedurl.txtから取得した情報を格納する.
+ */
+ public static class NicoFeed {
+
+ private final String url;
+ private final int number;
+
+ public NicoFeed(String url, int number) {
+ this.url = url;
+ this.number = number;
+ }
+
+ /** @return 上位いくつまでダウンロード対象とするか. */
+ public int getNumber() {
+ return number;
+ }
+
+ /** @return フィードのURL. */
+ public String getUrl() {
+ return url;
+ }
+ }
private static Log log = LogFactory.getLog(Config.class);
private static Config instance;
private final Properties properties;
- private final static String APPLICATION_NAME = "nicobrowser";
- private final static String CONFIG_NAME = APPLICATION_NAME + ".properties";
- public final static File APP_HOME = new File(System.getProperty("user.home", "."), "." + APPLICATION_NAME);
- public final static File CONFIG_FILE = new File(APP_HOME, CONFIG_NAME);
- public final static File DEF_CONFIG_FILE = new File("resources/nicobrowser.xml");
-
- private Config() throws IOException {
-
- if (!APP_HOME.isDirectory()) {
- boolean result = APP_HOME.mkdir();
- if (!result) {
- throw new IOException("アプリケーションディレクトリ作成失敗");
+ private static final String APPLICATION_NAME = "nicobrowser";
+ private static final String CONFIG_NAME = APPLICATION_NAME + ".properties";
+ private static final String FEEDURL_NAME = "feedurl.txt";
+ private static final File APP_HOME = new File(System.getProperty("user.home", "."), "." + APPLICATION_NAME);
+ private static final File CONFIG_FILE = new File(APP_HOME, CONFIG_NAME);
+ private static final File FEEDURL_FILE = new File(APP_HOME, FEEDURL_NAME);
+ private static final String P_PATH_DB = "path.db";
+ private static final String P_PATH_SAVEFILE = "path.savefile";
+ private static final String P_FILE_ENCODING = "encoding";
+ private static final String P_NICOVIDEO_MAIL = "nicovideo.mail";
+ private static final String P_NICOVIDEO_PASSWORD = "nicovideo.password";
+ private static final String P_DOWNLOAD_RETRY = "download.retry";
+ private static final String P_DOWNLOAD_MYLIST = "download.mylist";
+
+ /**
+ * プログラム実行に必要なコンフィグファイルを作成する.
+ * @return 今回コンフィグを作成したのであればtrue. 既に存在していたため, ファイル作成を行わなかった場合にはfalse.
+ * @throws java.io.IOException ファイル作成に失敗した.
+ */
+ public static boolean createNewConfigFiles() throws IOException {
+ boolean result = false;
+ try {
+ if (!CONFIG_FILE.exists()) {
+ createNewConfigFile(CONFIG_FILE);
+ result = true;
+ log.info("コンフィグファイルを作成しました: " + CONFIG_FILE.getCanonicalPath());
}
- new File(APP_HOME, "flv").mkdir();
+ if (!FEEDURL_FILE.exists()) {
+ URL resource = ClassLoader.getSystemResource("resources/" + FEEDURL_NAME);
+ createNewFeedFile(new File(resource.toURI()), FEEDURL_FILE);
+ result = true;
+ log.info("FEED URLファイルを作成しました: " + FEEDURL_FILE.getCanonicalPath());
+ }
+ } catch (URISyntaxException ex) {
+ throw new IOException(ex);
}
+ return result;
+ }
- Properties currentProperties = new Properties();
- if (CONFIG_FILE.isFile()) {
- InputStream is = new FileInputStream(CONFIG_FILE);
- currentProperties.load(is);
- is.close();
- } else {
- log.info("設定ファイルを新規作成: " + CONFIG_FILE.getCanonicalPath());
- }
+ private static void createNewConfigFile(File file) throws IOException {
+ ArrayList<CharSequence> props = new ArrayList<CharSequence>();
+
+ StringBuilder dbpath = new StringBuilder(P_PATH_DB + "=");
+ File dbDir = new File(APP_HOME, "db");
+ dbDir.mkdirs();
+ //Windowsのパス区切りバックスペースをエスケープするための処理も入れている.
+ dbpath.append(dbDir.getAbsolutePath().replaceAll("\\\\", "\\\\\\\\"));
+ props.add(dbpath);
+
+ StringBuilder savepath = new StringBuilder(P_PATH_SAVEFILE + "=");
+ File saveDir = new File(APP_HOME, "flv");
+ saveDir.mkdirs();
+ savepath.append(saveDir.getAbsolutePath().replaceAll("\\\\", "\\\\\\\\"));
+ props.add(savepath);
+
+ props.add(P_FILE_ENCODING + "=" + System.getProperty("file.encoding"));
- properties = createNewProperties(currentProperties, APP_HOME.toString());
+ props.add(P_NICOVIDEO_MAIL + "=set.your@mail.address");
+ props.add(P_NICOVIDEO_PASSWORD + "=set_your_password");
+ props.add(P_DOWNLOAD_RETRY + "=3");
+ props.add(P_DOWNLOAD_MYLIST + "=");
- FileOutputStream fos = new FileOutputStream(CONFIG_FILE);
- properties.store(fos, APPLICATION_NAME + " (concept ver.) config file");
- fos.close();
+ FileUtils.writeLines(file, props);
}
- private Properties createNewProperties(Properties current, String home) {
- Properties next = new Properties();
- setNewProperty(next, current, "nicovideo.mail", "set.your@mail.address");
- setNewProperty(next, current, "nicovideo.password", "set your password");
- setNewProperty(next, current, "path.db", home + File.separator + "db" + File.separator + "nicodb");
- setNewProperty(next, current, "dir.save.src", home + File.separator + "flv");
- setNewProperty(next, current, "download.ext.mp4", "true");
- setNewProperty(next, current, "download.retry", "3");
- setNewProperty(next, current, "download.number", "20");
- setNewProperty(next, current, "download.mylist", "");
-
- return next;
+ private static void createNewFeedFile(File resource, File dest) throws IOException {
+ List text = FileUtils.readLines(resource, "UTF-8");
+ FileUtils.writeLines(dest, text);
}
- private void setNewProperty(Properties next, Properties current, String key, String defaultValue) {
- next.setProperty(key, current.getProperty(key, defaultValue));
+ private Config() {
+ properties = new Properties();
+ FileInputStream fis = null;
+ try {
+ fis = new FileInputStream(CONFIG_FILE);
+ properties.load(fis);
+ } catch (IOException ex) {
+ log.fatal("コンフィグの読み込みに失敗: " + CONFIG_FILE);
+ throw new RuntimeException(ex);
+ } finally {
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException ex) {
+ log.warn("", ex);
+ }
+ }
+ }
+
+ try {
+ List urls = FileUtils.readLines(FEEDURL_FILE);
+ } catch (IOException ex) {
+ log.fatal("コンフィグの読み込みに失敗: " + CONFIG_FILE);
+ throw new RuntimeException(ex);
+ }
}
public static Config getInstance() {
if (instance == null) {
- try {
- instance = new Config();
- } catch (IOException e) {
- throw new Error(e);
- }
+ instance = new Config();
}
return instance;
}
* @return ニコニコ動画ログインID.
*/
public String getNicoMail() {
- return properties.getProperty("nicovideo.mail");
+ return properties.getProperty(P_NICOVIDEO_MAIL);
}
/**
* @return ニコニコ動画ログインパスワード.
*/
public String getNicoPassword() {
- return properties.getProperty("nicovideo.password");
- }
-
- public String getFfmpegPath() {
- return properties.getProperty("path.ffmpeg");
- }
-
- public String getFfmpegOption() {
- return properties.getProperty("ffpmeg.option");
+ return properties.getProperty(P_NICOVIDEO_PASSWORD);
}
- public String getDbPath() {
- return properties.getProperty("path.db");
+ /** @return DBパス */
+ public String getDbFile() {
+ return new File(properties.getProperty(P_PATH_DB), "nicodb").getAbsolutePath();
}
+ /** @return 保存先の指定. */
public String getSrcSaveDir() {
- return properties.getProperty("dir.save.src");
+ return properties.getProperty(P_PATH_SAVEFILE);
}
- /**
- * video/mp4のストリーム保存時、ファイルの拡張子に
- * .mp4を使用する(true)か、使用しない(.flv)か.
- * @return mp4拡張子を使用する場合true.
- */
- public boolean isExtMp4Use() {
- String res = properties.getProperty("download.ext.mp4");
- if ("false".equalsIgnoreCase(res)) {
- return false;
- }
- return true;
+ /** @return feedurl.txtの文字エンコーディング. */
+ public String getEncoding() {
+ String res = properties.getProperty(P_FILE_ENCODING, System.getProperty("file.encoding"));
+ return res;
}
+// /**
+// * video/mp4のストリーム保存時、ファイルの拡張子に
+// * .mp4を使用する(true)か、使用しない(.flv)か.
+// * @return mp4拡張子を使用する場合true.
+// */
+// public boolean isExtMp4Use() {
+// String res = properties.getProperty("download.ext.mp4");
+// if ("false".equalsIgnoreCase(res)) {
+// return false;
+// }
+// return true;
+// }
+//
/**
* 失敗したダウンロードファイルの最大リトライ回数を取得する.
* @return リトライ回数.
*/
public int getMaxRetry() {
- String res = properties.getProperty("download.retry");
+ String res = properties.getProperty(P_DOWNLOAD_RETRY);
return Integer.parseInt(res);
}
+//
+// /**
+// * 上位何位までの動画をダウンロードするか.
+// * @return ダウンロードするファイルの最下位.
+// */
+// public int getMaxDownloadNumber() {
+// String res = properties.getProperty("download.number");
+// return Integer.parseInt(res);
+// }
+//
- /**
- * 上位何位までの動画をダウンロードするか.
- * @return ダウンロードするファイルの最下位.
- */
- public int getMaxDownloadNumber() {
- String res = properties.getProperty("download.number");
- return Integer.parseInt(res);
+ public String[] getDownLoadMyList() {
+ String res = properties.getProperty(P_DOWNLOAD_MYLIST);
+ String[] nums = res.split(",");
+ for (int i = 0; i < nums.length; i++) {
+ nums[i] = nums[i].trim();
+ }
+ return nums;
}
- public String[] getDownLoadMyList() {
- String res = properties.getProperty("download.mylist");
- return res.split(",");
+ public List<NicoFeed> getNicoFeeds() {
+ throw new UnsupportedOperationException("Not yet implemented");
}
}
/*$Id$*/
package nicobrowser;
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.util.List;
+import org.apache.commons.io.FileUtils;
import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
public class ConfigTest {
+ private static File APP_HOME;
+ private static File CONFIG_FILE;
+ private static File FEEDURL_FILE;
+ private final File TEST_PROPERTY_FILE;
+// private final File TEST_FEED_FILE;
+
public ConfigTest() {
+ TEST_PROPERTY_FILE = new File("test/testdata/nicobrowser.properties");
+// TEST_FEED_FILE = new File("test/testdata/feedurl.txt");
}
@BeforeClass
- public static void setUpClass() throws Exception {
- }
+ public static void setUpClass() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
+ try {
+ Field f;
+ f = Config.class.getDeclaredField("APP_HOME");
+ f.setAccessible(true);
+ APP_HOME = (File) f.get(null);
- @AfterClass
- public static void tearDownClass() throws Exception {
- }
+ f = Config.class.getDeclaredField("CONFIG_FILE");
+ f.setAccessible(true);
+ CONFIG_FILE = (File) f.get(null);
- @Before
- public void setUp() {
+ f = Config.class.getDeclaredField("FEEDURL_FILE");
+ f.setAccessible(true);
+ FEEDURL_FILE = (File) f.get(null);
+
+ if (APP_HOME.exists()) {
+ String message = "ディレクトリを削除/移動してから再実行してください: " + APP_HOME;
+ System.err.println(message);
+ throw new Error(message);
+ }
+ } catch (Throwable th) {
+ throw new Error(th);
+ }
}
@After
- public void tearDown() {
+ public void tearDown() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
+ // コンフィグディレクトリを削除
+ FileUtils.deleteQuietly(APP_HOME);
+
+ // コンフィグインスタンスを削除.
+ Field f = Config.class.getDeclaredField("instance");
+ f.setAccessible(true);
+ f.set(null, null);
}
/**
* Test of getInstance method, of class Config.
*/
@Test
- public void getInstance() {
+ public void testGetInstance() throws IOException {
System.out.println("getInstance");
-
- Config result = Config.getInstance();
- assertNotNull(result);
-
- System.out.println(result.getDbPath());
- System.out.println(result.getFfmpegPath());
- System.out.println(result.getFfmpegOption());
-
+
+ try {
+ Config.getInstance();
+ fail("コンフィグファイル未作成の場合は例外発生");
+ } catch (Exception ex) {
+ }
+
+ Config.createNewConfigFiles();
+ assertNotNull(Config.getInstance());
+
+ }
+
+ /**
+ * Test of createNewConfigFiles method, of class Config.
+ */
+ @Test
+ public void testCreateNewConfigFiles() throws Exception {
+ System.out.println("createNewConfigFiles");
+ boolean result;
+
+ result = Config.createNewConfigFiles();
+ assertTrue("コンフィグが無いので新規作成される", result);
+ assertTrue(CONFIG_FILE.isFile());
+ assertTrue(FEEDURL_FILE.isFile());
+
+ result = Config.createNewConfigFiles();
+ assertFalse("作成済みなので作成されない", result);
+
+ CONFIG_FILE.delete();
+ assertFalse(CONFIG_FILE.exists());
+ result = Config.createNewConfigFiles();
+ assertTrue("コンフィグファイルだけ無くてもtrue", result);
+ assertTrue(CONFIG_FILE.isFile());
+
+ FEEDURL_FILE.delete();
+ assertFalse(FEEDURL_FILE.exists());
+ result = Config.createNewConfigFiles();
+ assertTrue("フィードファイルだけ無くてもtrue", result);
+ assertTrue(FEEDURL_FILE.isFile());
+ }
+
+ /**
+ * 初期作成コンフィグの妥当性をテストする.
+ */
+ @Test
+ public void testConfigInitialMake() throws IOException {
+ System.out.println("testConfigInitialMake");
+ Config.createNewConfigFiles();
+
+ Config conf = Config.getInstance();
+ assertEquals(new File(APP_HOME, "db/nicodb").getAbsolutePath(), conf.getDbFile());
+ assertEquals(new File(APP_HOME, "flv").getAbsolutePath(), conf.getSrcSaveDir());
+ }
+
+ /**
+ * コンフィグ初期生成を行うためのヘルパーメソッド.
+ */
+ private void initConfig() {
+ try {
+ Config.createNewConfigFiles();
+ FileUtils.copyFile(TEST_PROPERTY_FILE, CONFIG_FILE);
+ } catch (Exception ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+
+ /**
+ * Test of getNicoMail method, of class Config.
+ */
+ @Test
+ public void testGetNicoMail() {
+ System.out.println("getNicoMail");
+ initConfig();
+
+ String result = Config.getInstance().getNicoMail();
+ assertEquals("my@mail.address", result);
+ }
+
+ /**
+ * Test of getNicoPassword method, of class Config.
+ */
+ @Test
+ public void testGetNicoPassword() {
+ System.out.println("getNicoPassword");
+ initConfig();
+
+ String result = Config.getInstance().getNicoPassword();
+ assertEquals("my_password", result);
+ }
+
+ /**
+ * Test of getDbFile method, of class Config.
+ */
+ @Test
+ public void testGetDbFile() {
+ System.out.println("getDbFile");
+ initConfig();
+
+ String result = Config.getInstance().getDbFile();
+ assertEquals("C:\\Documents and Settings\\test\\.nicobrowser\\db\\nicodb", result);
+ }
+
+ /**
+ * Test of getSrcSaveDir method, of class Config.
+ */
+ @Test
+ public void testGetSrcSaveDir() {
+ System.out.println("getSrcSaveDir");
+ initConfig();
+
+ String result = Config.getInstance().getSrcSaveDir();
+ assertEquals("d:\\test", result);
+ }
+
+ /**
+ * Test of getEncoding method, of class Config.
+ */
+ @Test
+ public void testGetEncoding() {
+ System.out.println("getEncoding");
+ initConfig();
+
+ String result = Config.getInstance().getEncoding();
+ assertEquals("UTF-8", result);
+ }
+
+ /**
+ * Test of getMaxRetry method, of class Config.
+ */
+ @Test
+ public void testGetMaxRetry() {
+ System.out.println("getMaxRetry");
+ initConfig();
+
+ int result = Config.getInstance().getMaxRetry();
+ assertEquals(3, result);
+ }
+
+ /**
+ * Test of getDownLoadMyList method, of class Config.
+ */
+ @Test
+ public void testGetDownLoadMyList() {
+ System.out.println("getDownLoadMyList");
+ initConfig();
+
+ String[] result = Config.getInstance().getDownLoadMyList();
+ assertArrayEquals(new String[]{"100", "200", "300"}, result);
+ }
+
+ @Test
+ public void testGetFeeds() {
+ System.out.println("testGetFeeds");
+ initConfig();
+
+ List<Config.NicoFeed> feeds = Config.getInstance().getNicoFeeds();
}
}