Fixed issue 970 - favorites category exported as separate tag

This commit is contained in:
Pavol Zibrita 2012-04-20 06:33:59 +02:00
parent acf9a6fcee
commit 4c9ff0fbb4
3 changed files with 198 additions and 145 deletions

View file

@ -41,6 +41,7 @@ public class GPXUtilities {
public static class GPXExtensions {
Map<String, String> extensions = null;
protected boolean extensionReadMode;
public Map<String, String> getExtensionsToRead() {
if(extensions == null){
@ -55,6 +56,37 @@ public class GPXUtilities {
}
return extensions;
}
public void parse(XmlPullParser parser, Stack<GPXExtensions> parserState, SimpleDateFormat format) throws XmlPullParserException, IOException {
String tag = parser.getName();
if (extensionReadMode) {
String value = readText(parser, tag);
if (value != null) {
getExtensionsToWrite().put(tag, value);
}
}
if (tag.equals("extensions" )) {
extensionReadMode = true;
}
}
public final boolean parseEnd(String tag, Stack<GPXExtensions> parserState) {
if (tag.equals("extensions") && extensionReadMode) {
extensionReadMode = false;
return true;
} else {
if (isEndingTag(tag)) {
parserState.pop();
return true;
} else {
return false;
}
}
}
protected boolean isEndingTag(String tag) {
return false;
}
}
@ -62,6 +94,7 @@ public class GPXUtilities {
public double lat;
public double lon;
public String name = null;
public String category = null;
public String desc = null;
// by default
public long time = 0;
@ -69,7 +102,7 @@ public class GPXUtilities {
public double speed = 0;
public double hdop = Double.NaN;
public WptPt(){}
public WptPt() {};
public WptPt(double lat, double lon, long time, double ele, double speed, double hdop) {
this.lat = lat;
@ -80,23 +113,132 @@ public class GPXUtilities {
this.hdop = hdop;
}
@Override
public void parse(XmlPullParser parser, Stack<GPXExtensions> parserState, SimpleDateFormat format)
throws XmlPullParserException, IOException {
super.parse(parser, parserState, format);
String tag = parser.getName();
if (tag.equals("name")) {
name = readText(parser, "name");
} else if (tag.equals("category")) {
category = readText(parser, "category");
} else if (tag.equals("desc")) {
desc = readText(parser, "desc");
} else if (tag.equals("ele")) {
String text = readText(parser, "ele");
if (text != null) {
try {
ele = Float.parseFloat(text);
} catch (NumberFormatException e) {
}
}
} else if (tag.equals("hdop")) {
String text = readText(parser, "hdop");
if (text != null) {
try {
hdop = Float.parseFloat(text);
} catch (NumberFormatException e) {
}
}
} else if (tag.equals("time")) {
String text = readText(parser, "time");
if (text != null) {
try {
time = format.parse(text).getTime();
} catch (ParseException e) {
}
}
} else if (tag.equals("speed") && extensionReadMode) {
try {
speed = Float.parseFloat(getExtensionsToWrite().get(tag));
} catch (NumberFormatException e) {
}
}
}
@Override
public boolean isEndingTag(String tag) {
return tag.equals("trkpt") || tag.equals("wpt") || tag.equals("rtept");
}
}
public static class TrkSegment extends GPXExtensions {
public List<WptPt> points = new ArrayList<WptPt>();
@Override
public void parse(XmlPullParser parser, Stack<GPXExtensions> parserState, SimpleDateFormat format)
throws XmlPullParserException, IOException {
super.parse(parser, parserState, format);
if (parser.getName().equals("trkpt")) {
WptPt wptPt = parseWptAttributes(parser);
points.add(wptPt);
parserState.push(wptPt);
}
}
@Override
protected boolean isEndingTag(String tag) {
return tag.equals("trkseg");
}
}
public static class Track extends GPXExtensions {
public String name = null;
public String desc = null;
public List<TrkSegment> segments = new ArrayList<TrkSegment>();
@Override
public void parse(XmlPullParser parser, Stack<GPXExtensions> parserState, SimpleDateFormat format)
throws XmlPullParserException, IOException {
super.parse(parser, parserState, format);
String tag = parser.getName();
if (tag.equals("name")) {
name = readText(parser, "name");
} else if (tag.equals("desc")) {
desc = readText(parser, "desc");
} else if (tag.equals("trkseg")) {
TrkSegment trkSeg = new TrkSegment();
segments.add(trkSeg);
parserState.push(trkSeg);
}
}
@Override
public boolean isEndingTag(String tag) {
return tag.equals("trk");
}
}
public static class Route extends GPXExtensions {
public String name = null;
public String desc = null;
public List<WptPt> points = new ArrayList<WptPt>();
@Override
public void parse(XmlPullParser parser, Stack<GPXExtensions> parserState, SimpleDateFormat format)
throws XmlPullParserException, IOException
{
super.parse(parser, parserState, format);
String tag = parser.getName();
if (tag.equals("name")) {
name = readText(parser, "name");
} else if (tag.equals("desc")) {
desc = readText(parser, "desc");
} else if (tag.equals("rtept")) {
WptPt wptPt = parseWptAttributes(parser);
points.add(wptPt);
parserState.push(wptPt);
}
}
@Override
public boolean isEndingTag(String tag) {
return tag.equals("trk");
}
}
public static class Extensions extends GPXExtensions {
}
public static class GPXFile extends GPXExtensions {
@ -134,6 +276,34 @@ public class GPXUtilities {
return tracks.isEmpty() && points.isEmpty() && routes.isEmpty();
}
@Override
public void parse(XmlPullParser parser, Stack<GPXExtensions> parserState, SimpleDateFormat format) throws XmlPullParserException, IOException {
super.parse(parser, parserState, format);
String tag = parser.getName();
if (tag.equals("gpx")) {
this.author = parser.getAttributeValue("", "creator");
} else if (tag.equals("trk")) {
Track track = new Track();
tracks.add(track);
parserState.push(track);
} else if (tag.equals("rte")) {
Route route = new Route();
routes.add(route);
parserState.push(route);
} else if (tag.equals("wpt")) {
WptPt wptPt = parseWptAttributes(parser);
points.add(wptPt);
parserState.push(wptPt);
} else if (tag.equals("extensions")) {
Extensions ext = new Extensions();
parserState.push(ext);
}
}
@Override
protected boolean isEndingTag(String tag) {
return "gpx".equals(tag);
}
}
@ -234,6 +404,7 @@ public class GPXUtilities {
writeNotNullText(serializer, "ele", p.ele+"");
}
writeNotNullText(serializer, "name", p.name);
writeNotNullText(serializer, "category", p.category);
writeNotNullText(serializer, "desc", p.desc);
if(!Double.isNaN(p.hdop)){
writeNotNullText(serializer, "hdop", p.hdop+"");
@ -309,138 +480,19 @@ public class GPXUtilities {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(f, "UTF-8"); //$NON-NLS-1$
Stack<Object> parserState = new Stack<Object>();
boolean extensionReadMode = false;
Stack<GPXExtensions> parserState = new Stack<GPXExtensions>();
parserState.push(res);
int tok;
while ((tok = parser.next()) != XmlPullParser.END_DOCUMENT) {
if (tok == XmlPullParser.START_TAG) {
Object parse = parserState.peek();
String tag = parser.getName();
if (extensionReadMode && parse instanceof GPXExtensions) {
String value = readText(parser, tag);
if (value != null) {
((GPXExtensions) parse).getExtensionsToWrite().put(tag, value);
if (tag.equals("speed") && parse instanceof WptPt) {
try {
((WptPt) parse).speed = Float.parseFloat(value);
} catch (NumberFormatException e) {
}
}
}
} else if (parse instanceof GPXExtensions && tag.equals("extensions")) {
extensionReadMode = true;
} else {
if (parse instanceof GPXFile) {
if (parser.getName().equals("gpx")) {
((GPXFile) parse).author = parser.getAttributeValue("", "creator");
}
if (parser.getName().equals("trk")) {
Track track = new Track();
((GPXFile) parse).tracks.add(track);
parserState.push(track);
}
if (parser.getName().equals("rte")) {
Route route = new Route();
((GPXFile) parse).routes.add(route);
parserState.push(route);
}
if (parser.getName().equals("wpt")) {
WptPt wptPt = parseWptAttributes(parser);
((GPXFile) parse).points.add(wptPt);
parserState.push(wptPt);
}
} else if (parse instanceof Route) {
if (parser.getName().equals("name")) {
((Route) parse).name = readText(parser, "name");
}
if (parser.getName().equals("desc")) {
((Route) parse).desc = readText(parser, "desc");
}
if (parser.getName().equals("rtept")) {
WptPt wptPt = parseWptAttributes(parser);
((Route) parse).points.add(wptPt);
parserState.push(wptPt);
}
} else if (parse instanceof Track) {
if (parser.getName().equals("name")) {
((Track) parse).name = readText(parser, "name");
}
if (parser.getName().equals("desc")) {
((Track) parse).desc = readText(parser, "desc");
}
if (parser.getName().equals("trkseg")) {
TrkSegment trkSeg = new TrkSegment();
((Track) parse).segments.add(trkSeg);
parserState.push(trkSeg);
}
} else if (parse instanceof TrkSegment) {
if (parser.getName().equals("trkpt")) {
WptPt wptPt = parseWptAttributes(parser);
((TrkSegment) parse).points.add(wptPt);
parserState.push(wptPt);
}
// main object to parse
} else if (parse instanceof WptPt) {
if (parser.getName().equals("name")) {
((WptPt) parse).name = readText(parser, "name");
} else if (parser.getName().equals("desc")) {
((WptPt) parse).desc = readText(parser, "desc");
} else if (parser.getName().equals("ele")) {
String text = readText(parser, "ele");
if (text != null) {
try {
((WptPt) parse).ele = Float.parseFloat(text);
} catch (NumberFormatException e) {
}
}
} else if (parser.getName().equals("hdop")) {
String text = readText(parser, "hdop");
if (text != null) {
try {
((WptPt) parse).hdop = Float.parseFloat(text);
} catch (NumberFormatException e) {
}
}
} else if (parser.getName().equals("time")) {
String text = readText(parser, "time");
if (text != null) {
try {
((WptPt) parse).time = format.parse(text).getTime();
} catch (ParseException e) {
}
}
}
}
}
GPXExtensions element = parserState.peek();
element.parse(parser, parserState,format);
} else if (tok == XmlPullParser.END_TAG) {
Object parse = parserState.peek();
GPXExtensions parse = parserState.peek();
String tag = parser.getName();
if (parse instanceof GPXExtensions && tag.equals("extensions")) {
extensionReadMode = false;
if (!parse.parseEnd(tag,parserState)) {
log.error("Bad ending tag: " + tag + " for element " + parse.getClass());
}
if(tag.equals("trkpt")){
Object pop = parserState.pop();
assert pop instanceof WptPt;
} else if(tag.equals("wpt")){
Object pop = parserState.pop();
assert pop instanceof WptPt;
} else if(tag.equals("rtept")){
Object pop = parserState.pop();
assert pop instanceof WptPt;
} else if(tag.equals("trk")){
Object pop = parserState.pop();
assert pop instanceof Track;
} else if(tag.equals("rte")){
Object pop = parserState.pop();
assert pop instanceof Route;
} else if(tag.equals("trkseg")){
Object pop = parserState.pop();
assert pop instanceof TrkSegment;
}
}
}
if(convertCloudmadeSource && res.isCloudmadeRouteFile()){

View file

@ -81,10 +81,9 @@ public class FavouritesDbHelper extends SQLiteOpenHelper {
WptPt pt = new WptPt();
pt.lat = p.getLatitude();
pt.lon = p.getLongitude();
pt.name = p.getName();
if (p.getCategory().length() > 0)
pt.name = p.getName() + "_" + p.getCategory();
else
pt.name = p.getName();
pt.category = p.getCategory();
gpx.points.add(pt);
}
}

View file

@ -405,21 +405,23 @@ public class FavouritesActivity extends OsmandExpandableListActivity {
return res.warning;
}
for(WptPt p : res.points){
if(!existedPoints.contains(p.name)){
String categoryName = "";
int c;
String name = p.name;
if(name == null){
name = "";
}
if((c = p.name.lastIndexOf('_')) != -1){
categoryName = p.name.substring(c + 1);
name = p.name.substring(0, c);
}
FavouritePoint fp = new FavouritePoint(p.lat, p.lon, name, categoryName);
if (helper.addFavourite(fp)) {
publishProgress(fp);
}
if(existedPoints.contains(p.name) || existedPoints.contains(p.name + "_" + p.category)){
continue;
}
int c;
String name = p.name;
String categoryName = p.category != null ? p.category : "";
if(name == null){
name = "";
}
//old way to store the category, in name.
if("".equals(categoryName.trim()) && (c = p.name.lastIndexOf('_')) != -1) {
categoryName = p.name.substring(c + 1);
name = p.name.substring(0, c);
}
FavouritePoint fp = new FavouritePoint(p.lat, p.lon, name, categoryName);
if (helper.addFavourite(fp)) {
publishProgress(fp);
}
}
return null;