diff --git a/src/main/java/osm/jp/gpx/AppParameters.java b/src/main/java/osm/jp/gpx/AppParameters.java index 8e7c610..62b06a9 100644 --- a/src/main/java/osm/jp/gpx/AppParameters.java +++ b/src/main/java/osm/jp/gpx/AppParameters.java @@ -1,12 +1,17 @@ package osm.jp.gpx; -import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Properties; +import osm.jp.hayashi.tools.files.Directory; + @SuppressWarnings("serial") public class AppParameters extends Properties { static final String FILE_PATH = "AdjustTime.ini"; @@ -58,32 +63,30 @@ //public boolean param_GpxSplit = false; //public boolean param_GpxReuse = false; - File file; + Path file; - public AppParameters() throws FileNotFoundException, IOException { - super(); - this.file = new File(FILE_PATH); - syncFile(); + public AppParameters() throws FileNotFoundException, IOException, URISyntaxException { + this(FILE_PATH); } - public AppParameters(Properties defaults) throws FileNotFoundException, IOException { + public AppParameters(Properties defaults) throws FileNotFoundException, IOException, URISyntaxException { super(defaults); - this.file = new File(FILE_PATH); + this.file = Paths.get(FILE_PATH); syncFile(); } - public AppParameters(String iniFileName) throws FileNotFoundException, IOException { + public AppParameters(String iniFileName) throws FileNotFoundException, IOException, URISyntaxException { super(); - this.file = new File(iniFileName); + this.file = Paths.get(iniFileName); syncFile(); } - private void syncFile() throws FileNotFoundException, IOException { + private void syncFile() throws FileNotFoundException, IOException, URISyntaxException { boolean update = false; - if (this.file.exists()) { + if (Files.exists(this.file)) { // ファイルが存在すれば、その内容をロードする。 - this.load(new FileInputStream(file)); + this.load(new FileInputStream(file.toFile())); } else { update = true; @@ -94,7 +97,7 @@ String valueStr = this.getProperty(IMG_SOURCE_FOLDER); if (valueStr == null) { update = true; - this.setProperty(IMG_SOURCE_FOLDER, (new File(".")).getAbsolutePath()); + this.setProperty(IMG_SOURCE_FOLDER, Directory.getCurrentDirectory().toString()); } //------------------------------------------------ @@ -102,7 +105,7 @@ valueStr = this.getProperty(GPX_SOURCE_FOLDER); if (valueStr == null) { update = true; - this.setProperty(GPX_SOURCE_FOLDER, (new File(".")).getAbsolutePath()); + this.setProperty(GPX_SOURCE_FOLDER, Directory.getCurrentDirectory().toString()); } //------------------------------------------------ @@ -118,7 +121,7 @@ valueStr = this.getProperty(IMG_OUTPUT_FOLDER); if (valueStr == null) { update = true; - this.setProperty(IMG_OUTPUT_FOLDER, (new File(".")).getAbsolutePath()); + this.setProperty(IMG_OUTPUT_FOLDER, Directory.getCurrentDirectory().toString()); } //------------------------------------------------ @@ -225,17 +228,16 @@ update = true; setProperty(AppParameters.GPX_REUSE, Boolean.toString(false)); } - if (update) { // ・ファイルがなければ新たに作る // ・項目が足りない時は書き足す。 - this.store(new FileOutputStream(this.file), "defuilt settings"); + this.store(new FileOutputStream(this.file.toFile()), "defuilt settings"); } } public void store() throws FileNotFoundException, IOException { - this.store(new FileOutputStream(this.file), "by AdjustTime"); + this.store(new FileOutputStream(this.file.toFile()), "by AdjustTime"); } public void printout() { @@ -285,13 +287,13 @@ /** * AppParameters.IMG_SOURCE_FOLDER - * @return new File(getProperty(AppParameters.IMG_SOURCE_FOLDER)); + * @return new Path(getProperty(AppParameters.IMG_SOURCE_FOLDER)); */ - public File getImgSourceFolder() { - return new File(getProperty(AppParameters.IMG_SOURCE_FOLDER)); + public Path getImgSourceFolder() { + return Paths.get(getProperty(AppParameters.IMG_SOURCE_FOLDER)); } - public File getGpxSourceFolder() { + public Path getGpxSourceFolder() { String str = getProperty(AppParameters.GPX_SOURCE_FOLDER); if (str == null) { return null; @@ -299,7 +301,7 @@ if (str.isEmpty()) { return null; } - return new File(str); + return Paths.get(str); } /** diff --git a/src/main/java/osm/jp/gpx/ElementMapTRKPT.java b/src/main/java/osm/jp/gpx/ElementMapTRKPT.java index 2599ccd..82b538f 100644 --- a/src/main/java/osm/jp/gpx/ElementMapTRKPT.java +++ b/src/main/java/osm/jp/gpx/ElementMapTRKPT.java @@ -77,28 +77,33 @@ * @throws ParseException */ private TagTrkpt getTrkpt(Date jptime) throws ParseException { - Date keyTime = null; - for (Date key : this.keySet()) { - int flag = jptime.compareTo(key); - if (flag < 0) { - if (keyTime != null) { - return this.get(keyTime); + try { + Date keyTime = null; + for (Date key : this.keySet()) { + int flag = jptime.compareTo(key); + if (flag < 0) { + if (keyTime != null) { + return this.get(keyTime); + } + return null; } - return null; - } - else if (flag == 0) { - return this.get(key); - } - else if (flag > 0) { - keyTime = new Date(key.getTime()); - } - } - if (keyTime != null) { - if (Math.abs(keyTime.getTime() - jptime.getTime()) <= OVER_TIME_LIMIT) { - return this.get(keyTime); - } - } - return null; + else if (flag == 0) { + return this.get(key); + } + else if (flag > 0) { + keyTime = new Date(key.getTime()); + } + } + if (keyTime != null) { + if (Math.abs(keyTime.getTime() - jptime.getTime()) <= OVER_TIME_LIMIT) { + return this.get(keyTime); + } + } + return null; + } + catch (Exception e) { + throw new ParseException(e.toString(), 0); + } } /** diff --git a/src/main/java/osm/jp/gpx/GpxFolder.java b/src/main/java/osm/jp/gpx/GpxFolder.java index 9b6bcaa..4a731ea 100644 --- a/src/main/java/osm/jp/gpx/GpxFolder.java +++ b/src/main/java/osm/jp/gpx/GpxFolder.java @@ -22,7 +22,7 @@ this.params = params; File[] gpxfiles; - File gpxDir = params.getGpxSourceFolder(); + File gpxDir = params.getGpxSourceFolder().toFile(); if (gpxDir == null) { // GPXファイルまたはディレクトリが存在しません。('%s') throw new FileNotFoundException(String.format(ImportPicture.i18n.getString("msg.100"), "null")); diff --git a/src/main/java/osm/jp/gpx/ImgFile.java b/src/main/java/osm/jp/gpx/ImgFile.java index 1e21d54..bd67b6b 100644 --- a/src/main/java/osm/jp/gpx/ImgFile.java +++ b/src/main/java/osm/jp/gpx/ImgFile.java @@ -60,6 +60,20 @@ return ((name != null) && name.toUpperCase().endsWith(".JPG")); } + /** + * + * @param params :AppParameters + * @param delta :long + * @param gpxFile :GpxFile + * @param outDir :File + * @return + * @throws ParseException + * @throws ImageReadException + * @throws IOException + * @throws ImageWriteException + * @throws ParserConfigurationExceptionGpxFile + * @throws SAXException + */ public boolean procImageFile(AppParameters params, long delta, GpxFile gpxFile, File outDir) throws ParseException, ImageReadException, IOException, ImageWriteException, ParserConfigurationException, SAXException { //ElementMapTRKSEG mapTRKSEG = gpxFile.parse(); boolean exifWrite = params.isImgOutputExif(); diff --git a/src/main/java/osm/jp/gpx/ImgFolder.java b/src/main/java/osm/jp/gpx/ImgFolder.java index 66879d9..e94fea3 100644 --- a/src/main/java/osm/jp/gpx/ImgFolder.java +++ b/src/main/java/osm/jp/gpx/ImgFolder.java @@ -2,6 +2,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; @@ -17,28 +18,28 @@ public class ImgFolder extends ArrayList { private static final long serialVersionUID = -1137199371724546343L; AppParameters params; - File imgDir; - File outDir; + Path imgDir; + Path outDir; public ImgFolder(AppParameters params) { this.params = params; imgDir = params.getImgSourceFolder(); - File[] files = imgDir.listFiles(new ImgFileFilter()); + File[] files = imgDir.toFile().listFiles(new ImgFileFilter()); Arrays.sort(files, new FileSort()); for (File file : files) { this.add(new ImgFile(file)); } } - public void setOutDir(File outDir) { + public void setOutDir(Path outDir) { this.outDir = outDir; } - public File getOutDir() { + public Path getOutDir() { return this.outDir; } - public File getImgDir() { + public Path getImgDir() { return this.imgDir; } @@ -63,7 +64,7 @@ for (ImgFile image : this) { try { if (!image.isDone()) { - if(image.procImageFile(params, delta, gpxFile, outDir)) { + if(image.procImageFile(params, delta, gpxFile, outDir.toFile())) { image.setDone(true); } } diff --git a/src/main/java/osm/jp/gpx/ImportPicture.java b/src/main/java/osm/jp/gpx/ImportPicture.java index 2c6724d..2904946 100644 --- a/src/main/java/osm/jp/gpx/ImportPicture.java +++ b/src/main/java/osm/jp/gpx/ImportPicture.java @@ -1,6 +1,14 @@ package osm.jp.gpx; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -103,16 +111,18 @@ // 出力ファイル // AppParameters.IMG_OUTPUT に置き換え if (params.isImgOutput()) { - File outDir = new File(params.getProperty(AppParameters.IMG_OUTPUT_FOLDER)); - if (!outDir.exists()) { + Path outDir = Paths.get(params.getProperty(AppParameters.IMG_OUTPUT_FOLDER)); + if (!Files.exists(outDir)) { // 出力ディレクトリが存在しないときは作成する - outDir.mkdir(); + Files.createDirectories(outDir); } - else if (outDir.isFile()) { + else if (!Files.isDirectory(outDir)) { // 出力ディレクトリがFILEのときはERROR - throw new FileNotFoundException(String.format("OUTPUT directory '%s' is a File!", outDir.getAbsolutePath())); + throw new FileNotFoundException(String.format("OUTPUT directory '%s' is a File!", outDir.toAbsolutePath().toString())); } - outDir = new File(outDir, imgFolder.getImgDir().getName()); + outDir = Paths.get(outDir.toAbsolutePath().toString(), imgFolder.getImgDir().getFileName().toString()); + //File f = new File(outDir.toFile(), imgFolder.getImgDir().toFile().getName()); + //outDir = f.toPath(); imgFolder.setOutDir(outDir); } @@ -170,7 +180,7 @@ System.out.println("time difference: "+ (delta / 1000) +"(sec)"); System.out.println(" Target GPX: ["+ gpxFile.getAbsolutePath() +"]"); - System.out.println(" EXIF: "+ (params.isImgOutputExif() ? ("convert to '" + imgFolder.getOutDir().getAbsolutePath() +"'") : "off")); + System.out.println(" EXIF: "+ (params.isImgOutputExif() ? ("convert to '" + imgFolder.getOutDir().toAbsolutePath() +"'") : "off")); System.out.println(); gpxFile.printinfo(); @@ -234,7 +244,7 @@ } public File getImgBaseFile() { - return new File(imgFolder.getImgDir(), params.getProperty(AppParameters.IMG_BASE_FILE)); + return new File(imgFolder.getImgDir().toFile(), params.getProperty(AppParameters.IMG_BASE_FILE)); } static String getShortPathName(File dir, File iFile) { diff --git a/src/main/java/osm/jp/hayashi/tools/files/Directory.java b/src/main/java/osm/jp/hayashi/tools/files/Directory.java new file mode 100644 index 0000000..cbf2c8f --- /dev/null +++ b/src/main/java/osm/jp/hayashi/tools/files/Directory.java @@ -0,0 +1,30 @@ +package osm.jp.hayashi.tools.files; + +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.CodeSource; +import java.security.ProtectionDomain; + +public abstract class Directory { + + public static Path getCurrentDirectory(Class cls) throws URISyntaxException { + ProtectionDomain pd = cls.getProtectionDomain(); + CodeSource cs = pd.getCodeSource(); + URL location = cs.getLocation(); + URI uri = location.toURI(); + Path path = Paths.get(uri); + if (!Files.isDirectory(path)) { + return path.getParent(); + } + return path; + } + + public static Path getCurrentDirectory() throws URISyntaxException { + return getCurrentDirectory(Directory.class); + } + +} diff --git a/src/test/java/osm/jp/gpx/AppParametersTest.java b/src/test/java/osm/jp/gpx/AppParametersTest.java index 686a249..c624988 100644 --- a/src/test/java/osm/jp/gpx/AppParametersTest.java +++ b/src/test/java/osm/jp/gpx/AppParametersTest.java @@ -6,6 +6,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.net.URISyntaxException; import java.nio.channels.FileChannel; import org.junit.After; @@ -36,9 +37,9 @@ String valueStr = params.getProperty(AppParameters.IMG_OUTPUT_ALL); assertFalse(Boolean.getBoolean(valueStr)); } - catch (IOException e) { + catch (Exception e) { fail("Exceptionが発生した。"); - } + } } } @@ -62,7 +63,7 @@ String valueStr = params.getProperty(AppParameters.IMG_OUTPUT_ALL); assertTrue(Boolean.valueOf(valueStr)); } - catch (IOException e) { + catch (Exception e) { fail("Exceptionが発生した。"); } } @@ -77,7 +78,7 @@ String valueStr = newParams.getProperty(AppParameters.IMG_OUTPUT_ALL); assertFalse(Boolean.valueOf(valueStr)); } - catch (IOException e) { + catch (Exception e) { fail("Exceptionが発生した。"); } } @@ -103,7 +104,7 @@ String valueStr = params.getProperty(AppParameters.IMG_OUTPUT_ALL); assertFalse(Boolean.valueOf(valueStr)); } - catch (IOException e) { + catch (Exception e) { fail("Exceptionが発生した。"); } } @@ -118,7 +119,7 @@ String valueStr = newParams.getProperty(AppParameters.IMG_OUTPUT_ALL); assertTrue(Boolean.valueOf(valueStr)); } - catch (IOException e) { + catch (Exception e) { fail("Exceptionが発生した。"); } } diff --git a/src/test/java/osm/jp/gpx/ElementMapTRKSEGTest.java b/src/test/java/osm/jp/gpx/ElementMapTRKSEGTest.java index f00436d..eadc092 100644 --- a/src/test/java/osm/jp/gpx/ElementMapTRKSEGTest.java +++ b/src/test/java/osm/jp/gpx/ElementMapTRKSEGTest.java @@ -2,6 +2,7 @@ import java.io.File; import java.io.IOException; +import java.net.URISyntaxException; import java.text.ParseException; import java.util.Date; @@ -71,7 +72,9 @@ } catch (IOException | ParseException | ParserConfigurationException | SAXException e) { fail(); - } + } catch (URISyntaxException e) { + fail(); + } } @Test @@ -98,7 +101,9 @@ } catch (SAXException e) { // 整形されていないXML - } + } catch (URISyntaxException e) { + fail(); + } } } } diff --git a/src/test/java/osm/jp/hayashi/tools/files/DirectoryTest.java b/src/test/java/osm/jp/hayashi/tools/files/DirectoryTest.java new file mode 100644 index 0000000..0bb0774 --- /dev/null +++ b/src/test/java/osm/jp/hayashi/tools/files/DirectoryTest.java @@ -0,0 +1,47 @@ +package osm.jp.hayashi.tools.files; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.*; + +import java.nio.file.Files; +import java.nio.file.Path; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class DirectoryTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + try { + Path path = Directory.getCurrentDirectory(); + System.out.print(path.toAbsolutePath().toString()); + assertThat(path.isAbsolute(), is(true)); + assertThat(Files.isDirectory(path), is(true)); + assertThat(Files.exists(path), is(true)); + } + catch (Exception e) { + fail(); + } + } + +}