diff options
author | Samuel Tardieu <sam@rfc1149.net> | 2014-02-28 21:36:54 +0100 |
---|---|---|
committer | Samuel Tardieu <sam@rfc1149.net> | 2014-02-28 22:14:48 +0100 |
commit | ce4c2d36b88e126aa245f7549a9a873e761e5c57 (patch) | |
tree | 0516c267e0b3fb710c1e1d121e0aef30492629ae /main | |
parent | 423da50116afcf1a6720f2a91fefece41562ef20 (diff) | |
download | cgeo-ce4c2d36b88e126aa245f7549a9a873e761e5c57.zip cgeo-ce4c2d36b88e126aa245f7549a9a873e761e5c57.tar.gz cgeo-ce4c2d36b88e126aa245f7549a9a873e761e5c57.tar.bz2 |
refactoring: move waypoint parsing code in Waypoint
Diffstat (limited to 'main')
-rw-r--r-- | main/src/cgeo/geocaching/Geocache.java | 54 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/Waypoint.java | 63 |
2 files changed, 66 insertions, 51 deletions
diff --git a/main/src/cgeo/geocaching/Geocache.java b/main/src/cgeo/geocaching/Geocache.java index 156c4b6..5d299d4 100644 --- a/main/src/cgeo/geocaching/Geocache.java +++ b/main/src/cgeo/geocaching/Geocache.java @@ -57,7 +57,6 @@ import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; @@ -1403,58 +1402,11 @@ public class Geocache implements ICache, IWaypoint { * Detect coordinates in the personal note and convert them to user defined waypoints. Works by rule of thumb. */ public void parseWaypointsFromNote() { - try { - if (StringUtils.isBlank(getPersonalNote())) { - return; - } - final Pattern coordPattern = Pattern.compile("\\b[nNsS]{1}\\s*\\d"); // begin of coordinates - int count = 1; - String note = getPersonalNote(); - MatcherWrapper matcher = new MatcherWrapper(coordPattern, note); - while (matcher.find()) { - try { - final Geopoint point = new Geopoint(note.substring(matcher.start())); - // Coords must have non zero latitude and longitude, at least one part shall have fractional degrees, - // and there must exist no waypoint with the same coordinates already. - if (point.getLatitudeE6() != 0 && point.getLongitudeE6() != 0 && - ((point.getLatitudeE6() % 1000) != 0 || (point.getLongitudeE6() % 1000) != 0) && - !hasIdenticalWaypoint(point)) { - final String name = CgeoApplication.getInstance().getString(R.string.cache_personal_note) + " " + count; - final String potentialWaypointType = note.substring(Math.max(0, matcher.start() - 15)); - final Waypoint waypoint = new Waypoint(name, parseWaypointType(potentialWaypointType), false); - waypoint.setCoords(point); - addOrChangeWaypoint(waypoint, false); - count++; - } - } catch (final Geopoint.ParseException e) { - // ignore - } - - note = note.substring(matcher.start() + 1); - matcher = new MatcherWrapper(coordPattern, note); - } - } catch (final Exception e) { - Log.e("Geocache.parseWaypointsFromNote", e); - } - } - - /** - * Detect waypoint types in the personal note text. It works by rule of thumb only. - */ - private static WaypointType parseWaypointType(final String input) { - final String lowerInput = StringUtils.substring(input, 0, 20).toLowerCase(Locale.getDefault()); - for (final WaypointType wpType : WaypointType.values()) { - if (lowerInput.contains(wpType.getL10n().toLowerCase(Locale.getDefault()))) { - return wpType; - } - if (lowerInput.contains(wpType.id)) { - return wpType; - } - if (lowerInput.contains(wpType.name().toLowerCase(Locale.US))) { - return wpType; + for (final Waypoint waypoint : Waypoint.parseWaypointsFromNote(StringUtils.defaultString(getPersonalNote()))) { + if (!hasIdenticalWaypoint(waypoint.getCoords())) { + addOrChangeWaypoint(waypoint, false); } } - return WaypointType.WAYPOINT; } private boolean hasIdenticalWaypoint(final Geopoint point) { diff --git a/main/src/cgeo/geocaching/Waypoint.java b/main/src/cgeo/geocaching/Waypoint.java index 47977bc..6626c4d 100644 --- a/main/src/cgeo/geocaching/Waypoint.java +++ b/main/src/cgeo/geocaching/Waypoint.java @@ -3,14 +3,20 @@ package cgeo.geocaching; import cgeo.geocaching.enumerations.LoadFlags; import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.utils.Log; +import cgeo.geocaching.utils.MatcherWrapper; import org.apache.commons.lang3.StringUtils; import org.eclipse.jdt.annotation.NonNull; +import java.util.Collection; import java.util.Comparator; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.regex.Pattern; public class Waypoint implements IWaypoint { @@ -285,4 +291,61 @@ public class Waypoint implements IWaypoint { return gpxId; } + + /** + * Detect coordinates in the personal note and convert them to user defined waypoints. Works by rule of thumb. + * + * @param initialNote Note content + * @return a collection of found waypoints + */ + public static Collection<Waypoint> parseWaypointsFromNote(@NonNull final String initialNote) { + final List<Waypoint> waypoints = new LinkedList<Waypoint>(); + final Pattern COORDPATTERN = Pattern.compile("\\b[nNsS]{1}\\s*\\d"); // begin of coordinates + + String note = initialNote; + MatcherWrapper matcher = new MatcherWrapper(COORDPATTERN, note); + int count = 1; + while (matcher.find()) { + try { + final Geopoint point = new Geopoint(note.substring(matcher.start())); + // Coords must have non zero latitude and longitude and at least one part shall have fractional degrees. + if (point.getLatitudeE6() != 0 && point.getLongitudeE6() != 0 && + ((point.getLatitudeE6() % 1000) != 0 || (point.getLongitudeE6() % 1000) != 0)) { + final String name = CgeoApplication.getInstance().getString(R.string.cache_personal_note) + " " + count; + final String potentialWaypointType = note.substring(Math.max(0, matcher.start() - 15)); + final Waypoint waypoint = new Waypoint(name, parseWaypointType(potentialWaypointType), false); + waypoint.setCoords(point); + waypoints.add(waypoint); + count++; + } + } catch (final Geopoint.ParseException e) { + // ignore + } + + note = note.substring(matcher.start() + 1); + matcher = new MatcherWrapper(COORDPATTERN, note); + } + return waypoints; + } + + /** + * Detect waypoint types in the personal note text. It works by rule of thumb only. + */ + private static WaypointType parseWaypointType(final String input) { + final String lowerInput = StringUtils.substring(input, 0, 20).toLowerCase(Locale.getDefault()); + for (final WaypointType wpType : WaypointType.values()) { + if (lowerInput.contains(wpType.getL10n().toLowerCase(Locale.getDefault()))) { + return wpType; + } + if (lowerInput.contains(wpType.id)) { + return wpType; + } + if (lowerInput.contains(wpType.name().toLowerCase(Locale.US))) { + return wpType; + } + } + return WaypointType.WAYPOINT; + } + + } |