OSDN Git Service

AdjustTime
[importpicture/importpicture.git] / src / main / java / osm / jp / gpx / GpxFolder.java
1 package osm.jp.gpx;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FilenameFilter;
6 import java.io.IOException;
7 import java.text.ParseException;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.Comparator;
11 import java.util.List;
12
13 import javax.xml.parsers.ParserConfigurationException;
14
15 import org.xml.sax.SAXException;
16
17 public class GpxFolder extends ArrayList<GpxFile> {
18         private static final long serialVersionUID = 6178901459948163548L;
19     AppParameters params;
20         
21         public GpxFolder(AppParameters params) throws ParserConfigurationException, SAXException, IOException, ParseException {
22                 this.params = params;
23
24                 File[] gpxfiles;
25                 File gpxDir = params.getGpxSourceFolder();
26         if (gpxDir == null) {
27                 // GPXファイルまたはディレクトリが存在しません。('%s')
28                 throw new FileNotFoundException(String.format(ImportPicture.i18n.getString("msg.100"), "null"));
29         }
30         if (!gpxDir.exists()) {
31                 // GPXファイルまたはディレクトリが存在しません。('%s')
32                 throw new FileNotFoundException(String.format(ImportPicture.i18n.getString("msg.100"), gpxDir.getAbsolutePath()));
33         }
34
35         if (gpxDir.isFile()) {
36                 if (accept(params, gpxDir.getName())) {
37                 List<File> fileList = new ArrayList<>();
38                 fileList.add(gpxDir);
39                 gpxfiles = fileList.toArray(new File[fileList.size()]);
40                 }
41                 else {
42                 // GPXファイルまたはディレクトリが存在しません。('%s')
43                         throw new FileNotFoundException(String.format(ImportPicture.i18n.getString("msg.100"), gpxDir.getAbsolutePath()));
44                 }
45         }
46         else if (gpxDir.isDirectory()) {
47                 // 指定されたディレクトリ内のGPXファイルすべてを対象とする
48             gpxfiles = gpxDir.listFiles(new GpxFileFilter());
49             if (gpxfiles == null) {
50                 // 対象となるGPXファイルがありませんでした。('%s')
51                 throw new FileNotFoundException(
52                     String.format(ImportPicture.i18n.getString("msg.110"), gpxDir.getAbsolutePath())
53                 );
54             }
55             if (params.isImgOutputAll() && (gpxfiles.length > 1)) {
56                 // "複数のGPXファイルがあるときには、'IMG.OUTPUT_ALL'オプションは指定できません。"
57                 throw new FileNotFoundException(
58                     String.format(ImportPicture.i18n.getString("msg.120"))
59                 );
60             }
61         }
62         else {
63                 // GPXファイルまたはディレクトリが存在しません。('%s')
64                 throw new FileNotFoundException(String.format(ImportPicture.i18n.getString("msg.100"), gpxDir.getAbsolutePath()));
65         }
66
67         Arrays.sort(gpxfiles, new FileSort());
68         for (File file : gpxfiles) {
69                 this.add(new GpxFile(params, file));
70         }
71         }
72         
73         /**
74          * 対象は '*.GPX' のみ対象とする
75          */
76         public static boolean accept(AppParameters params, String name) {
77                 String filename = name.toUpperCase();
78         if (filename.endsWith(".GPX")) {
79             if (!filename.endsWith("_.GPX") || params.isGpxReuse()) {
80                 return true;
81             }
82         }
83         return false;
84         }
85     
86     /**
87      * ファイル名の順序に並び替えるためのソートクラス
88      * 
89      */
90     static class FileSort implements Comparator<File> {
91         @Override
92         public int compare(File src, File target){
93             int diff = src.getName().compareTo(target.getName());
94             return diff;
95         }
96     }
97
98     /**
99      * GPXファイルフィルター
100      */
101     class GpxFileFilter implements FilenameFilter {
102         @Override
103         public boolean accept(File dir, String name) {
104                 return GpxFolder.accept(params, name);
105         }
106     }
107 }