]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/save/xml/GpxCacher.java
Version 19.2, December 2018
[GpsPrune.git] / tim / prune / save / xml / GpxCacher.java
1 package tim.prune.save.xml;
2
3 import java.io.BufferedInputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.InputStream;
7 import java.util.zip.GZIPInputStream;
8 import java.util.zip.ZipEntry;
9 import java.util.zip.ZipInputStream;
10
11 import tim.prune.data.DataPoint;
12 import tim.prune.data.SourceInfo;
13
14 /**
15  * Class to read in a GPX file and cache all the point strings
16  */
17 public class GpxCacher implements TagReceiver
18 {
19         private SourceInfo _sourceInfo = null;
20         private String _headerString = null;
21         private String[] _strings = null;
22         private int _pointNum = 0;
23
24
25         /**
26          * Constructor
27          * @param inSourceInfo source information
28          */
29         public GpxCacher(SourceInfo inInfo)
30         {
31                 _sourceInfo = inInfo;
32                 _strings = new String[inInfo.getNumPoints()];
33                 _pointNum = 0;
34                 // Should be a gpx file, but might be raw, zipped or gzipped
35                 File gpxFile = inInfo.getFile();
36                 String fileName = gpxFile.getName().toLowerCase();
37                 if (gpxFile.exists() && gpxFile.canRead())
38                 {
39                         GpxSlicer slicer = new GpxSlicer(this);
40                         InputStream istream = null;
41                         BufferedInputStream bstream = null;
42                         try {
43                                 if (fileName.endsWith(".gpx") || fileName.endsWith(".xml")) {
44                                         istream = new FileInputStream(inInfo.getFile());
45                                 }
46                                 else if (fileName.endsWith(".zip")) {
47                                         istream = getZipInputStream(inInfo.getFile());
48                                 }
49                                 else if (fileName.endsWith(".gz")) {
50                                         istream = new GZIPInputStream(new FileInputStream(inInfo.getFile()));
51                                 }
52                                 else {
53                                         System.out.println("GpxCacher unrecognised file type: " + inInfo.getFile().getName());
54                                 }
55                                 if (istream != null) {
56                                         bstream = new BufferedInputStream(istream);
57                                         slicer.slice(bstream);
58                                         bstream.close();
59                                 }
60                         } catch (Exception e) {
61                                 // TODO: Handle errors here with a list of warnings?
62                                 e.printStackTrace();
63                         }
64                 }
65         }
66
67         /**
68          * Accept a tag from the slicer
69          */
70         public void reportTag(String inTag)
71         {
72                 if (_headerString == null) {
73                         _headerString = inTag;
74                 }
75                 else if (_strings != null)
76                 {
77                         if (_pointNum < _strings.length)
78                         {
79                                 _strings[_pointNum] = inTag;
80                                 _pointNum++;
81                         }
82                         else
83                         {
84                                 // _pointNum has got too high for the strings array
85                                 // This means the cacher has failed, probably by invalid points - need to give up caching here
86                                 _strings = null;
87                         }
88                 }
89         }
90
91
92         /**
93          * @return the header string from the GPX tag
94          */
95         public String getHeaderString()
96         {
97                 return _headerString;
98         }
99
100         /**
101          * Get the source string for the given point
102          * @param inPoint point to retrieve
103          * @return string if found, otherwise null
104          */
105         public String getSourceString(DataPoint inPoint)
106         {
107                 int index = _sourceInfo.getIndex(inPoint);
108                 if (_strings != null && index >= 0 && index < _strings.length) {
109                         return _strings[index];
110                 }
111                 return null;
112         }
113
114         /**
115          * Get an inputstream of a GPX file inside a zip
116          * @param inFile File object describing zip file
117          * @return input stream for Xml parser
118          */
119         private static InputStream getZipInputStream(File inFile)
120         {
121                 try
122                 {
123                         ZipInputStream zis = new ZipInputStream(new FileInputStream(inFile));
124                         while (zis.available() > 0)
125                         {
126                                 ZipEntry entry = zis.getNextEntry();
127                                 String entryName = entry.toString();
128                                 if (entryName != null && entryName.length() > 4)
129                                 {
130                                         String suffix = entryName.substring(entryName.length()-4).toLowerCase();
131                                         if (suffix.equals(".gpx") || suffix.equals(".xml")) {
132                                                 // First matching file so must be gpx
133                                                 return zis;
134                                         }
135                                 }
136                         }
137                 }
138                 catch (Exception e) {} // ignore errors
139                 // not found - error!
140                 return null;
141         }
142 }