]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/save/xml/GpxCacher.java
Version 11, August 2010
[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 {
76                         _strings[_pointNum] = inTag;
77                         _pointNum++;
78                 }
79         }
80
81
82         /**
83          * @return the header string from the GPX tag
84          */
85         public String getHeaderString()
86         {
87                 return _headerString;
88         }
89
90         /**
91          * Get the source string for the given point
92          * @param inPoint point to retrieve
93          * @return string if found, otherwise null
94          */
95         public String getSourceString(DataPoint inPoint)
96         {
97                 int index = _sourceInfo.getIndex(inPoint);
98                 if (index >= 0) {
99                         return _strings[index];
100                 }
101                 return null;
102         }
103
104         /**
105          * Get an inputstream of a GPX file inside a zip
106          * @param inFile File object describing zip file
107          * @return input stream for Xml parser
108          */
109         private static InputStream getZipInputStream(File inFile)
110         {
111                 try
112                 {
113                         ZipInputStream zis = new ZipInputStream(new FileInputStream(inFile));
114                         while (zis.available() > 0)
115                         {
116                                 ZipEntry entry = zis.getNextEntry();
117                                 String entryName = entry.toString();
118                                 if (entryName != null && entryName.length() > 4)
119                                 {
120                                         String suffix = entryName.substring(entryName.length()-4).toLowerCase();
121                                         if (suffix.equals(".gpx") || suffix.equals(".xml")) {
122                                                 // First matching file so must be gpx
123                                                 return zis;
124                                         }
125                                 }
126                         }
127                 }
128                 catch (Exception e) {} // ignore errors
129                 // not found - error!
130                 return null;
131         }
132 }