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;
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;
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;
}
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;
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;
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");
/** ローカル画像設定ファイル。 */
private boolean useStoreFile;
private boolean isImplicitPath;
- private File configDir;
+ private Path configDir;
/**
*/
public ConfigStore(boolean useStoreFile,
boolean isImplicitPath,
- File configDirPath ){
+ Path configDirPath ){
super();
this.useStoreFile = useStoreFile;
*
* @return 設定ディレクトリ。設定ディレクトリを使わない場合はnull
*/
- public File getConfigDir(){
+ public Path getConfigDir(){
return this.configDir;
}
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;
}
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;
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();
* もしくは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;
* もしくは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;
}
* @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ファイル["
OutputStream ostream;
try{
- ostream = new FileOutputStream(absFile);
- }catch(FileNotFoundException e){
+ ostream = Files.newOutputStream(absFile);
+ }catch(IOException e){
assert false;
return false;
}
*/
public JsObject loadLocalImgConfig(){
Path path = LOCALIMG_DIR.resolve(LOCALIMGCONFIG_PATH);
- JsObject result = loadJsObject(path.toFile());
+ JsObject result = loadJsObject(path);
return result;
}
}
/**
- * 相対パスの絶対パス化を試みる。
- *
- * @param file 対象パス
- * @return 絶対パス。絶対化に失敗した場合は元の引数。
- */
- public static Path supplyFullPath(Path file){
- Path absFile;
-
- try{
- absFile = file.toAbsolutePath();
- }catch(SecurityException e){
- return file;
- }
-
- return absFile;
- }
-
- /**
* 任意のディレクトリがアクセス可能な状態にあるか判定する。
*
* <p>アクセス可能の条件を満たすためには、与えられたパスが
* @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;
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{
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;
}
*
* <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;
}
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);
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);