1 package tim.prune.save.xml;
3 import java.io.BufferedInputStream;
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;
11 import tim.prune.data.DataPoint;
12 import tim.prune.data.SourceInfo;
15 * Class to read in a GPX file and cache all the point strings
17 public class GpxCacher implements TagReceiver
19 private SourceInfo _sourceInfo = null;
20 private String _headerString = null;
21 private String[] _strings = null;
22 private int _pointNum = 0;
27 * @param inSourceInfo source information
29 public GpxCacher(SourceInfo inInfo)
32 _strings = new String[inInfo.getNumPoints()];
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())
39 GpxSlicer slicer = new GpxSlicer(this);
40 InputStream istream = null;
41 BufferedInputStream bstream = null;
43 if (fileName.endsWith(".gpx") || fileName.endsWith(".xml")) {
44 istream = new FileInputStream(inInfo.getFile());
46 else if (fileName.endsWith(".zip")) {
47 istream = getZipInputStream(inInfo.getFile());
49 else if (fileName.endsWith(".gz")) {
50 istream = new GZIPInputStream(new FileInputStream(inInfo.getFile()));
53 System.out.println("GpxCacher unrecognised file type: " + inInfo.getFile().getName());
55 if (istream != null) {
56 bstream = new BufferedInputStream(istream);
57 slicer.slice(bstream);
60 } catch (Exception e) {
61 // TODO: Handle errors here with a list of warnings?
68 * Accept a tag from the slicer
70 public void reportTag(String inTag)
72 if (_headerString == null) {
73 _headerString = inTag;
76 _strings[_pointNum] = inTag;
83 * @return the header string from the GPX tag
85 public String getHeaderString()
91 * Get the source string for the given point
92 * @param inPoint point to retrieve
93 * @return string if found, otherwise null
95 public String getSourceString(DataPoint inPoint)
97 int index = _sourceInfo.getIndex(inPoint);
99 return _strings[index];
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
109 private static InputStream getZipInputStream(File inFile)
113 ZipInputStream zis = new ZipInputStream(new FileInputStream(inFile));
114 while (zis.available() > 0)
116 ZipEntry entry = zis.getNextEntry();
117 String entryName = entry.toString();
118 if (entryName != null && entryName.length() > 4)
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
128 catch (Exception e) {} // ignore errors
129 // not found - error!