OSDN Git Service

change File to Path again.
authorOlyutorskii <olyutorskii@users.osdn.me>
Sat, 2 May 2020 13:22:33 +0000 (22:22 +0900)
committerOlyutorskii <olyutorskii@users.osdn.me>
Sat, 2 May 2020 13:22:33 +0000 (22:22 +0900)
src/main/java/jp/sfjp/jindolf/JindolfMain.java
src/main/java/jp/sfjp/jindolf/config/AppSetting.java
src/main/java/jp/sfjp/jindolf/config/ConfigDirUtils.java
src/main/java/jp/sfjp/jindolf/config/ConfigStore.java
src/main/java/jp/sfjp/jindolf/config/FileUtils.java
src/main/java/jp/sfjp/jindolf/view/HelpFrame.java

index c586bc5..cae3552 100644 (file)
@@ -12,6 +12,7 @@ import java.awt.EventQueue;
 import java.io.PrintStream;
 import java.lang.reflect.InvocationTargetException;
 import java.text.MessageFormat;
+import java.util.Locale;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.swing.JFrame;
@@ -20,6 +21,7 @@ import jp.sfjp.jindolf.config.AppSetting;
 import jp.sfjp.jindolf.config.CmdOption;
 import jp.sfjp.jindolf.config.ConfigStore;
 import jp.sfjp.jindolf.config.EnvInfo;
+import jp.sfjp.jindolf.config.FileUtils;
 import jp.sfjp.jindolf.config.OptionInfo;
 import jp.sfjp.jindolf.data.LandsTreeModel;
 import jp.sfjp.jindolf.log.LogUtils;
@@ -142,6 +144,17 @@ public final class JindolfMain {
             LOGGER.info(LOG_NOCONF);
         }
 
+        if(FileUtils.isWindowsOSFs()){
+            LOGGER.info("Windows環境と認識されました。");
+        }
+
+        if(FileUtils.isMacOSXFs()){
+            LOGGER.info("macOS環境と認識されました。");
+        }
+
+        Locale locale = Locale.getDefault();
+        LOGGER.log(Level.INFO, "ロケールに{0}が用いられます。", locale.toString());
+
         return;
     }
 
index 32748fb..8e49d9e 100644 (file)
@@ -100,7 +100,7 @@ public class AppSetting{
 
         boolean useConfig;
         boolean isImplicitPath;
-        File configPath;
+        Path configPath;
 
         if(opt == CmdOption.OPT_NOCONF){
             useConfig = false;
@@ -109,12 +109,13 @@ public class AppSetting{
         }else if(opt == CmdOption.OPT_CONFDIR){
             useConfig = true;
             isImplicitPath = false;
-            String optPath = option.getStringArg(opt);
-            configPath = FileUtils.supplyFullPath(new File(optPath).toPath()).toFile();
+            String optArg = option.getStringArg(opt);
+            configPath = Paths.get(optArg);
+            configPath = configPath.toAbsolutePath();
         }else{
             useConfig = true;
             isImplicitPath = true;
-            configPath = ConfigDirUtils.getImplicitConfigDirectory().toFile();
+            configPath = ConfigDirUtils.getImplicitConfigDirectory();
         }
 
         ConfigStore result =
index 2d2c6f5..38e37d3 100644 (file)
@@ -119,7 +119,7 @@ public final class ConfigDirUtils{
             throws IllegalArgumentException{
         if(Files.exists(confPath)) throw new IllegalArgumentException();
 
-        Path absPath = FileUtils.supplyFullPath(confPath);
+        Path absPath = confPath.toAbsolutePath();
 
         String preErrMessage =
                 "設定格納ディレクトリ<br/>"
index 8a35fd9..1f57685 100644 (file)
@@ -12,9 +12,6 @@ import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -24,6 +21,7 @@ import java.io.Reader;
 import java.io.Writer;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.logging.Level;
@@ -41,11 +39,11 @@ import jp.sourceforge.jovsonz.Json;
 public class ConfigStore {
 
     /** 検索履歴ファイル。 */
-    public static final File HIST_FILE = new File("searchHistory.json");
+    public static final Path HIST_FILE = Paths.get("searchHistory.json");
     /** ネットワーク設定ファイル。 */
-    public static final File NETCONFIG_FILE = new File("netconfig.json");
+    public static final Path NETCONFIG_FILE = Paths.get("netconfig.json");
     /** 台詞表示設定ファイル。 */
-    public static final File TALKCONFIG_FILE = new File("talkconfig.json");
+    public static final Path TALKCONFIG_FILE = Paths.get("talkconfig.json");
     /** ローカル画像格納ディレクトリ。 */
     public static final Path LOCALIMG_DIR = Paths.get("img");
     /** ローカル画像設定ファイル。 */
@@ -61,7 +59,7 @@ public class ConfigStore {
 
     private boolean useStoreFile;
     private boolean isImplicitPath;
-    private File configDir;
+    private Path configDir;
 
 
     /**
@@ -76,7 +74,7 @@ public class ConfigStore {
      */
     public ConfigStore(boolean useStoreFile,
                        boolean isImplicitPath,
-                       File configDirPath ){
+                       Path configDirPath ){
         super();
 
         this.useStoreFile = useStoreFile;
@@ -107,7 +105,7 @@ public class ConfigStore {
      *
      * @return 設定ディレクトリ。設定ディレクトリを使わない場合はnull
      */
-    public File getConfigDir(){
+    public Path getConfigDir(){
         return this.configDir;
     }
 
@@ -119,8 +117,7 @@ public class ConfigStore {
     public Path getLocalImgDir(){
         if(this.configDir == null) return null;
 
-        Path configPath = this.configDir.toPath();
-        Path result = configPath.resolve(LOCALIMG_DIR);
+        Path result = this.configDir.resolve(LOCALIMG_DIR);
 
         return result;
     }
@@ -133,18 +130,18 @@ public class ConfigStore {
     public void prepareConfigDir(){
         if( ! this.useStoreFile ) return;
 
-        if( ! this.configDir.exists() ){
+        if( ! Files.exists(this.configDir) ){
             Path created =
-                ConfigDirUtils.buildConfigDirectory(this.configDir.toPath(),
+                ConfigDirUtils.buildConfigDirectory(this.configDir,
                                                     this.isImplicitPath );
             ConfigDirUtils.checkAccessibility(created);
         }else{
-            ConfigDirUtils.checkAccessibility(this.configDir.toPath());
+            ConfigDirUtils.checkAccessibility(this.configDir);
         }
 
-        File imgDir = new File(this.configDir, "img");
-        if( ! imgDir.exists()){
-            ConfigDirUtils.buildImageCacheDir(imgDir.toPath());
+        Path imgDir = this.configDir.resolve("img");
+        if( ! Files.exists(imgDir) ){
+            ConfigDirUtils.buildImageCacheDir(imgDir);
         }
 
         return;
@@ -159,7 +156,7 @@ public class ConfigStore {
     public void tryLock(){
         if( ! this.useStoreFile ) return;
 
-        File lockFile = new File(this.configDir, LOCKFILE);
+        File lockFile = new File(this.configDir.toFile(), LOCKFILE);
         InterVMLock lock = new InterVMLock(lockFile);
 
         lock.tryLock();
@@ -186,7 +183,7 @@ public class ConfigStore {
      *     もしくはOBJECT型でなかった、
      *     もしくは入力エラーがあればnull
      */
-    public JsObject loadJsObject(File file){
+    public JsObject loadJsObject(Path file){
         JsComposition<?> root = loadJson(file);
         if(root == null || root.getJsTypes() != JsTypes.OBJECT) return null;
         JsObject result = (JsObject) root;
@@ -202,24 +199,24 @@ public class ConfigStore {
      *     もしくはJSONファイルが存在しない、
      *     もしくは入力エラーがあればnull
      */
-    public JsComposition<?> loadJson(File file){
+    public JsComposition<?> loadJson(Path file){
         if( ! this.useStoreFile ) return null;
 
-        File absFile;
+        Path absFile;
         if(file.isAbsolute()){
             absFile = file;
         }else{
             if(this.configDir == null) return null;
-            absFile = new File(this.configDir, file.getPath());
-            if( ! absFile.exists() ) return null;
+            absFile = this.configDir.resolve(file);
+            if( ! Files.exists(absFile) ) return null;
             if( ! absFile.isAbsolute() ) return null;
         }
-        String absPath = absFile.getPath();
+        String absPath = absFile.toString();
 
         InputStream istream;
         try{
-            istream = new FileInputStream(absFile);
-        }catch(FileNotFoundException e){
+            istream = Files.newInputStream(absFile);
+        }catch(IOException e){
             assert false;
             return null;
         }
@@ -295,16 +292,16 @@ public class ConfigStore {
      * @return 正しくセーブが行われればtrue。
      *     何らかの理由でセーブが完了できなければfalse
      */
-    public boolean saveJson(File file, JsComposition<?> root){
+    public boolean saveJson(Path file, JsComposition<?> root){
         if( ! this.useStoreFile ) return false;
 
         // TODO テンポラリファイルを用いたより安全なファイル更新
-        File absFile = new File(this.configDir, file.getPath());
-        String absPath = absFile.getPath();
+        Path absFile = this.configDir.resolve(file);
+        String absPath = absFile.toString();
 
-        absFile.delete();
         try{
-            if(absFile.createNewFile() != true) return false;
+            Files.delete(absFile);
+            Files.createFile(absFile);
         }catch(IOException e){
             LOGGER.log(Level.SEVERE,
                     "JSONファイル["
@@ -315,8 +312,8 @@ public class ConfigStore {
 
         OutputStream ostream;
         try{
-            ostream = new FileOutputStream(absFile);
-        }catch(FileNotFoundException e){
+            ostream = Files.newOutputStream(absFile);
+        }catch(IOException e){
             assert false;
             return false;
         }
@@ -423,7 +420,7 @@ public class ConfigStore {
      */
     public JsObject loadLocalImgConfig(){
         Path path = LOCALIMG_DIR.resolve(LOCALIMGCONFIG_PATH);
-        JsObject result = loadJsObject(path.toFile());
+        JsObject result = loadJsObject(path);
         return result;
     }
 
index 82e1971..bf77b8a 100644 (file)
@@ -63,24 +63,6 @@ public final class FileUtils{
     }
 
     /**
-     * 相対パスの絶対パス化を試みる。
-     *
-     * @param file 対象パス
-     * @return 絶対パス。絶対化に失敗した場合は元の引数。
-     */
-    public static Path supplyFullPath(Path file){
-        Path absFile;
-
-        try{
-            absFile = file.toAbsolutePath();
-        }catch(SecurityException e){
-            return file;
-        }
-
-        return absFile;
-    }
-
-    /**
      * 任意のディレクトリがアクセス可能な状態にあるか判定する。
      *
      * <p>アクセス可能の条件を満たすためには、与えられたパスが
@@ -115,12 +97,8 @@ public final class FileUtils{
      * @return ロード元ファイル。見つからなければnull。
      */
     public static Path getClassSourceFile(Class<?> klass){
-        ProtectionDomain domain;
-        try{
-            domain = klass.getProtectionDomain();
-        }catch(SecurityException e){
-            return null;
-        }
+        ProtectionDomain domain = klass.getProtectionDomain();
+        if(domain == null) return null;
 
         CodeSource src = domain.getCodeSource();
         if(src == null) return null;
@@ -129,7 +107,7 @@ public final class FileUtils{
         if(location == null) return null;
 
         String scheme = location.getProtocol();
-        if( ! SCHEME_FILE.equals(scheme) ) return null;
+        if( ! SCHEME_FILE.equalsIgnoreCase(scheme) ) return null;
 
         URI uri;
         try{
@@ -155,8 +133,8 @@ public final class FileUtils{
         if( ! Files.exists(path) ) return false;
         if( ! Files.isRegularFile(path) ) return false;
 
-        String name = path.getFileName().toString();
-        boolean result = name.matches("^.+\\.[jJ][aA][rR]$");
+        String leafName = path.getFileName().toString();
+        boolean result = leafName.matches("^.+\\.[jJ][aA][rR]$");
 
         return result;
     }
@@ -196,19 +174,11 @@ public final class FileUtils{
      *
      * <p>システムプロパティuser.homeで示されたホームディレクトリを返す。
      *
-     * @return ホームディレクトリ。何らかの事情でnullを返す場合もあり。
+     * @return ホームディレクトリ。
      */
     public static Path getHomeDirectory(){
-        String homeProp;
-        try{
-            homeProp = System.getProperty("user.home");
-        }catch(SecurityException e){
-            return null;
-        }
-
-        File homeFile = new File(homeProp);
-        Path result = homeFile.toPath();
-
+        String homeProp = System.getProperty("user.home");
+        Path result = Paths.get(homeProp);
         return result;
     }
 
@@ -220,13 +190,7 @@ public final class FileUtils{
     public static boolean isMacOSXFs(){
         if(File.separatorChar != '/') return false;
 
-        String osName;
-        try{
-            osName = System.getProperty(SYSPROP_OSNAME);
-        }catch(SecurityException e){
-            return false;
-        }
-
+        String osName = System.getProperty(SYSPROP_OSNAME);
         if(osName == null) return false;
 
         osName = osName.toLowerCase(Locale.ROOT);
@@ -246,13 +210,7 @@ public final class FileUtils{
     public static boolean isWindowsOSFs(){
         if(File.separatorChar != '\\') return false;
 
-        String osName;
-        try{
-            osName = System.getProperty(SYSPROP_OSNAME);
-        }catch(SecurityException e){
-            return false;
-        }
-
+        String osName = System.getProperty(SYSPROP_OSNAME);
         if(osName == null) return false;
 
         osName = osName.toLowerCase(Locale.ROOT);
index 2100db2..7cc8e40 100644 (file)
@@ -41,7 +41,7 @@ import jp.sfjp.jindolf.util.GUIUtils;
  * ヘルプ画面。
  */
 @SuppressWarnings("serial")
-public class HelpFrame extends JFrame
+public final class HelpFrame extends JFrame
         implements ActionListener, HyperlinkListener{
 
     private static final String HELP_HTML = "resources/html/help.html";
@@ -172,7 +172,7 @@ public class HelpFrame extends JFrame
 
         if(configStore.useStoreFile()){
             info.append("設定格納ディレクトリ : ")
-                .append(configStore.getConfigDir().getPath());
+                .append(configStore.getConfigDir().toString());
         }else{
             info.append("※ 設定格納ディレクトリは使っていません。");
         }