public static GPXFile loadGPXFile(Context ctx, File f, boolean convertCloudmadeSource) { try { GPXFile file = loadGPXFile(ctx, new FileInputStream(f), convertCloudmadeSource); file.path = f.getAbsolutePath(); return file; } catch (FileNotFoundException e) { GPXFile res = new GPXFile(); res.path = f.getAbsolutePath(); log.error("Error reading gpx", e); // $NON-NLS-1$ res.warning = ctx.getString(R.string.error_reading_gpx); return res; } }
public static GPXFile loadGPXFile(Context ctx, InputStream f, boolean convertCloudmadeSource) { GPXFile res = new GPXFile(); SimpleDateFormat format = new SimpleDateFormat(GPX_TIME_FORMAT); format.setTimeZone(TimeZone.getTimeZone("UTC")); try { XmlPullParser parser = Xml.newPullParser(); parser.setInput(new InputStreamReader(f)); // $NON-NLS-1$ Stack<GPXExtensions> parserState = new Stack<GPXExtensions>(); parserState.push(res); int tok; while ((tok = parser.next()) != XmlPullParser.END_DOCUMENT) { if (tok == XmlPullParser.START_TAG) { GPXExtensions element = parserState.peek(); element.parse(parser, parserState, format); } else if (tok == XmlPullParser.END_TAG) { GPXExtensions parse = parserState.peek(); String tag = parser.getName(); if (!parse.parseEnd(tag, parserState)) { log.debug("Bad ending tag: " + tag + " for element " + parse.getClass()); } } } if (convertCloudmadeSource && res.isCloudmadeRouteFile()) { Track tk = new Track(); res.tracks.add(tk); TrkSegment segment = new TrkSegment(); tk.segments.add(segment); for (WptPt wp : res.points) { segment.points.add(wp); } res.points.clear(); } } catch (XmlPullParserException e) { log.error("Error reading gpx", e); // $NON-NLS-1$ res.warning = ctx.getString(R.string.error_reading_gpx); } catch (IOException e) { log.error("Error reading gpx", e); // $NON-NLS-1$ res.warning = ctx.getString(R.string.error_reading_gpx); } return res; }