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;
75 else if (_strings != null)
77 if (_pointNum < _strings.length)
79 _strings[_pointNum] = inTag;
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
93 * @return the header string from the GPX tag
95 public String getHeaderString()
101 * Get the source string for the given point
102 * @param inPoint point to retrieve
103 * @return string if found, otherwise null
105 public String getSourceString(DataPoint inPoint)
107 int index = _sourceInfo.getIndex(inPoint);
108 if (_strings != null && index >= 0 && index < _strings.length) {
109 return _strings[index];
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
119 private static InputStream getZipInputStream(File inFile)
123 ZipInputStream zis = new ZipInputStream(new FileInputStream(inFile));
124 while (zis.available() > 0)
126 ZipEntry entry = zis.getNextEntry();
127 String entryName = entry.toString();
128 if (entryName != null && entryName.length() > 4)
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
138 catch (Exception e) {} // ignore errors
139 // not found - error!