diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/cgeo/geocaching/LogTemplateProvider.java | 43 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgBase.java | 73 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgCache.java | 8 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgGPXParser.java | 58 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgeodetail.java | 12 | ||||
| -rw-r--r-- | src/cgeo/geocaching/cgeovisit.java | 2 |
6 files changed, 122 insertions, 74 deletions
diff --git a/src/cgeo/geocaching/LogTemplateProvider.java b/src/cgeo/geocaching/LogTemplateProvider.java index 09acd0d..05d1341 100644 --- a/src/cgeo/geocaching/LogTemplateProvider.java +++ b/src/cgeo/geocaching/LogTemplateProvider.java @@ -1,6 +1,10 @@ package cgeo.geocaching;
import java.util.HashMap;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import android.util.Log;
/**
@@ -32,7 +36,10 @@ public class LogTemplateProvider { }
protected String apply(String input, cgBase base) {
- return input.replaceAll("\\[" + template + "\\]", getValue(base));
+ if (input.contains("[" + template + "]")) {
+ return input.replaceAll("\\[" + template + "\\]", getValue(base));
+ }
+ return input;
}
}
@@ -69,7 +76,7 @@ public class LogTemplateProvider { String findCount = "";
final HashMap<String, String> params = new HashMap<String, String>();
final String page = base.request(false, "www.geocaching.com", "/my/", "GET", params, false, false, false).getData();
- int current = cgBase.parseFindCount(page);
+ int current = parseFindCount(page);
if (current >= 0) {
findCount = String.valueOf(current + 1);
@@ -101,5 +108,37 @@ public class LogTemplateProvider { }
return result;
}
+
+ private static int parseFindCount(String page) {
+ if (page == null || page.length() == 0) {
+ return -1;
+ }
+
+ int findCount = -1;
+
+ try {
+ final Pattern findPattern = Pattern.compile("Finds\\s*</strong>\\s*<span class=\"statcount\">(\\d+)</span>", Pattern.CASE_INSENSITIVE);
+ final Matcher findMatcher = findPattern.matcher(page);
+ if (findMatcher.find()) {
+ if (findMatcher.groupCount() > 0) {
+ String count = findMatcher.group(1);
+
+ if (count != null) {
+ if (count.length() == 0) {
+ findCount = 0;
+ } else {
+ findCount = Integer.parseInt(count);
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ Log.w(cgSettings.tag, "cgBase.parseFindCount: " + e.toString());
+ }
+
+ return findCount;
+ }
+
+
}
diff --git a/src/cgeo/geocaching/cgBase.java b/src/cgeo/geocaching/cgBase.java index edf8abf..b12366f 100644 --- a/src/cgeo/geocaching/cgBase.java +++ b/src/cgeo/geocaching/cgBase.java @@ -92,7 +92,7 @@ public class cgBase { private final static Pattern patternDesc = Pattern.compile("<span id=\"ctl00_ContentBody_LongDescription\"[^>]*>" + "(.*)</span>[^<]*</div>[^<]*<p>[^<]*</p>[^<]*<p>[^<]*<strong>\\W*Additional Hints</strong>", Pattern.CASE_INSENSITIVE); private final static Pattern patternCountLogs = Pattern.compile("<span id=\"ctl00_ContentBody_lblFindCounts\"><p>(.*)<\\/p><\\/span>", Pattern.CASE_INSENSITIVE); private final static Pattern patternCountLog = Pattern.compile(" src=\"\\/images\\/icons\\/([^\\.]*).gif\" alt=\"[^\"]*\" title=\"[^\"]*\" />([0-9]*)[^0-9]+", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); - private final static Pattern patternLogs = Pattern.compile("<table class=\"LogsTable\">(.*?)</table>\\s*<p", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); + //private final static Pattern patternLogs = Pattern.compile("<table class=\"LogsTable\">(.*?)</table>\\s*<p", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); private final static Pattern patternLog = Pattern.compile("<tr><td class.+?<a href=\"/profile/\\?guid=.+?>(.+?)</a>.+?logOwnerStats.+?guid.+?>(\\d+)</a>.+?LogType.+?<img.+?/images/icons/([^\\.]+)\\..+?title=\"(.+?)\".+?LogDate.+?>(.+?)<.+?LogText.+?>(.*?)</p>(.*?)</div></div></div></td></tr>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); private final static Pattern patternLogImgs = Pattern.compile("href=\"(http://img.geocaching.com/cache/log/.+?)\".+?<span>([^<]*)", Pattern.CASE_INSENSITIVE); private final static Pattern patternAttributes = Pattern.compile("<h3 class=\"WidgetHeader\">[^<]*<img[^>]+>\\W*Attributes[^<]*</h3>[^<]*<div class=\"WidgetBody\">(([^<]*<img src=\"[^\"]+\" alt=\"[^\"]+\"[^>]*>)+)[^<]*<p", Pattern.CASE_INSENSITIVE); @@ -115,13 +115,15 @@ public class cgBase { public static HashMap<Integer, String> logTypesTrackable = new HashMap<Integer, String>(); public static HashMap<Integer, String> logTypesTrackableAction = new HashMap<Integer, String>(); public static HashMap<Integer, String> errorRetrieve = new HashMap<Integer, String>(); - public static SimpleDateFormat dateInBackslash = new SimpleDateFormat("MM/dd/yyyy"); - public static SimpleDateFormat dateInDash = new SimpleDateFormat("yyyy-MM-dd"); - public static SimpleDateFormat dateEvIn = new SimpleDateFormat("dd MMMMM yyyy", Locale.ENGLISH); // 28 March 2009 - public static SimpleDateFormat dateTbIn1 = new SimpleDateFormat("EEEEE, dd MMMMM yyyy", Locale.ENGLISH); // Saturday, 28 March 2009 - public static SimpleDateFormat dateTbIn2 = new SimpleDateFormat("EEEEE, MMMMM dd, yyyy", Locale.ENGLISH); // Saturday, March 28, 2009 - public static SimpleDateFormat dateSqlIn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 2010-07-25 14:44:01 - public static SimpleDateFormat dateGPXIn = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); // 2010-04-20T07:00:00Z + public final static SimpleDateFormat dateInBackslash = new SimpleDateFormat("MM/dd/yyyy"); + public final static SimpleDateFormat dateInDash = new SimpleDateFormat("yyyy-MM-dd"); + public final static SimpleDateFormat dateEvIn = new SimpleDateFormat("dd MMMMM yyyy", Locale.ENGLISH); // 28 March 2009 + public final static SimpleDateFormat dateTbIn1 = new SimpleDateFormat("EEEEE, dd MMMMM yyyy", Locale.ENGLISH); // Saturday, 28 March 2009 + public final static SimpleDateFormat dateTbIn2 = new SimpleDateFormat("EEEEE, MMMMM dd, yyyy", Locale.ENGLISH); // Saturday, March 28, 2009 + public final static SimpleDateFormat dateSqlIn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 2010-07-25 14:44:01 + public final static SimpleDateFormat dateGPXIn = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); // 2010-04-20T07:00:00Z + public final static SimpleDateFormat dateLogs1 = new SimpleDateFormat("MMMMM dd, yyyy", Locale.ENGLISH); // March 28, 2009 + public final static SimpleDateFormat dateLogs2 = new SimpleDateFormat("MMMMM dd", Locale.ENGLISH); // March 28 private Resources res = null; private HashMap<String, String> cookies = new HashMap<String, String>(); private static final String passMatch = "[/\\?&]*[Pp]ass(word)?=[^&^#^$]+"; @@ -1670,7 +1672,7 @@ public class cgBase { 2- Finds-count 3- Log type image name (e.g. "icon_smile") 4- Type string (e.g. "Found it") - 5- Date string (e.g. "about 4 days ago") + 5- Date string (e.g. "March 28, 2009") 6- Log text 7- The rest (e.g. log-images, maybe faster) */ @@ -1688,6 +1690,28 @@ public class cgBase { { logDone.type = logTypes.get("icon_note"); } + + try + { + logDone.date = dateLogs1.parse(matcherLog.group(5)).getTime(); // long format + } + catch (ParseException e) + { + try + { + // short format, with current year + final Date date = dateLogs2.parse(matcherLog.group(5)); + final Calendar cal = Calendar.getInstance(); + final int year = cal.get(Calendar.YEAR); + cal.setTime(date); + cal.set(Calendar.YEAR, year); + logDone.date = cal.getTimeInMillis(); + } + catch (ParseException ee) + { + Log.w(cgSettings.tag, "Failed to parse logs date: " + ee.toString()); + } + } logDone.author = Html.fromHtml(matcherLog.group(1)).toString(); @@ -2576,37 +2600,6 @@ public class cgBase { return trackables; } - public static int parseFindCount(String page) { - if (page == null || page.length() == 0) { - return -1; - } - - int findCount = -1; - - try { - final Pattern findPattern = Pattern.compile("Finds\\s*</strong>\\s*<span class=\"statcount\">(\\d+)</span>", Pattern.CASE_INSENSITIVE); - final Matcher findMatcher = findPattern.matcher(page); - if (findMatcher.find()) { - if (findMatcher.groupCount() > 0) { - String count = findMatcher.group(1); - - if (count != null) { - if (count.length() == 0) { - findCount = 0; - } else { - findCount = Integer.parseInt(count); - } - } - } - } - } catch (Exception e) { - Log.w(cgSettings.tag, "cgBase.parseFindCount: " + e.toString()); - } - - return findCount; - } - - public static String stripParagraphs(String text) { if (text == null) { return ""; diff --git a/src/cgeo/geocaching/cgCache.java b/src/cgeo/geocaching/cgCache.java index fe087e1..5abc057 100644 --- a/src/cgeo/geocaching/cgCache.java +++ b/src/cgeo/geocaching/cgCache.java @@ -272,7 +272,7 @@ public class cgCache { } public boolean isEventCache() { - return (type.equalsIgnoreCase("event") || type.equalsIgnoreCase("mega") || type.equalsIgnoreCase("cito")); + return ("event".equalsIgnoreCase(type) || "mega".equalsIgnoreCase(type) || "cito".equalsIgnoreCase(type)); } public boolean logVisit(IAbstractActivity fromActivity) { @@ -312,9 +312,9 @@ public class cgCache { } public ArrayList<Integer> getPossibleLogTypes(cgSettings settings) { - boolean isOwner = owner.equalsIgnoreCase(settings.getUsername()); + boolean isOwner = owner != null && owner.equalsIgnoreCase(settings.getUsername()); ArrayList<Integer> types = new ArrayList<Integer>(); - if (type.equals("event") || type.equals("mega") || type.equals("cito") || type.equals("lostfound")) { + if ("event".equals(type) || "mega".equals(type) || "cito".equals(type) || "lostfound".equals(type)) { types.add(cgBase.LOG_WILL_ATTEND); types.add(cgBase.LOG_NOTE); types.add(cgBase.LOG_ATTENDED); @@ -322,7 +322,7 @@ public class cgCache { if (isOwner) { types.add(cgBase.LOG_ANNOUNCEMENT); } - } else if (type.equals("webcam")) { + } else if ("webcam".equals(type)) { types.add(cgBase.LOG_WEBCAM_PHOTO_TAKEN); types.add(cgBase.LOG_DIDNT_FIND_IT); types.add(cgBase.LOG_NOTE); diff --git a/src/cgeo/geocaching/cgGPXParser.java b/src/cgeo/geocaching/cgGPXParser.java index c8c013f..2e9b671 100644 --- a/src/cgeo/geocaching/cgGPXParser.java +++ b/src/cgeo/geocaching/cgGPXParser.java @@ -254,20 +254,10 @@ public class cgGPXParser { cache.cacheid = attrs.getValue("id"); } if (attrs.getIndex("archived") > -1) { - final String at = attrs.getValue("archived").toLowerCase(); - if (at.equals("true")) { - cache.archived = true; - } else { - cache.archived = false; - } + cache.archived = attrs.getValue("archived").equalsIgnoreCase("true"); } if (attrs.getIndex("available") > -1) { - final String at = attrs.getValue("available").toLowerCase(); - if (at.equals("true")) { - cache.disabled = false; - } else { - cache.disabled = true; - } + cache.disabled = !attrs.getValue("available").equalsIgnoreCase("true"); } } catch (Exception e) { Log.w(cgSettings.tag, "Failed to parse cache attributes."); @@ -280,7 +270,7 @@ public class cgGPXParser { public void end(String body) { final String content = Html.fromHtml(body).toString().trim(); - cache.name = content; + cache.name = validate(content); } }); @@ -289,7 +279,7 @@ public class cgGPXParser { public void end(String body) { final String content = Html.fromHtml(body).toString().trim(); - cache.owner = content; + cache.owner = validate(content); } }); @@ -297,8 +287,8 @@ public class cgGPXParser { gcCache.getChild(nsGC, "type").setEndTextElementListener(new EndTextElementListener() { public void end(String body) { - final String content = cgBase.cacheTypes.get(body.toLowerCase()); - cache.type = content; + String parsedString = validate(body.toLowerCase()); + setType(parsedString); } }); @@ -307,7 +297,7 @@ public class cgGPXParser { public void end(String body) { final String content = body.toLowerCase(); - cache.size = content; + cache.size = validate(content); } }); @@ -340,7 +330,7 @@ public class cgGPXParser { public void end(String body) { if (cache.location == null || cache.location.length() == 0) { - cache.location = body.trim(); + cache.location = validate(body.trim()); } else { cache.location = cache.location + ", " + body.trim(); } @@ -352,7 +342,7 @@ public class cgGPXParser { public void end(String body) { if (cache.location == null || cache.location.length() == 0) { - cache.location = body.trim(); + cache.location = validate(body.trim()); } else { cache.location = body.trim() + ", " + cache.location; } @@ -363,7 +353,7 @@ public class cgGPXParser { gcCache.getChild(nsGC, "encoded_hints").setEndTextElementListener(new EndTextElementListener() { public void end(String body) { - cache.hint = body.trim(); + cache.hint = validate(body.trim()); } }); @@ -373,8 +363,8 @@ public class cgGPXParser { public void start(Attributes attrs) { try { if (attrs.getIndex("html") > -1) { - final String at = attrs.getValue("html").toLowerCase(); - if (at.equals("false")) { + final String at = attrs.getValue("html"); + if (at.equalsIgnoreCase("false")) { htmlShort = false; } } @@ -387,7 +377,7 @@ public class cgGPXParser { gcCache.getChild(nsGC, "short_description").setEndTextElementListener(new EndTextElementListener() { public void end(String body) { - if (htmlShort == false) { + if (!htmlShort) { cache.shortdesc = Html.fromHtml(body).toString(); } else { cache.shortdesc = body; @@ -401,8 +391,7 @@ public class cgGPXParser { public void start(Attributes attrs) { try { if (attrs.getIndex("html") > -1) { - final String at = attrs.getValue("html").toLowerCase(); - if (at.equals("false")) { + if (attrs.getValue("html").equalsIgnoreCase("false")) { htmlLong = false; } } @@ -562,4 +551,23 @@ public class cgGPXParser { } return parsed ? search.getCurrentId() : 0l; } + + protected String validate(String input) { + if ("nil".equalsIgnoreCase(input)) { + return ""; + } + return input; + } + + private void setType(String parsedString) { + final String knownType = cgBase.cacheTypes.get(parsedString); + if (knownType != null) { + cache.type = knownType; + } + else { + if (cache.type == null || cache.type.length() == 0) { + cache.type = "mystery"; // default for not recognized types + } + } + } } diff --git a/src/cgeo/geocaching/cgeodetail.java b/src/cgeo/geocaching/cgeodetail.java index c2b152e..7ee0d93 100644 --- a/src/cgeo/geocaching/cgeodetail.java +++ b/src/cgeo/geocaching/cgeodetail.java @@ -595,9 +595,17 @@ public class cgeodetail extends AbstractActivity { gcIcons.put("mystery", R.drawable.type_mystery); } - geocode = geocode.toUpperCase(); + if (null == geocode && cache.geocode.length() > 0) + { + geocode = cache.geocode; + } + + if (null == guid && cache.guid.length() > 0) + { + guid = cache.guid; + } - setTitle(geocode); + setTitle(cache.geocode.toUpperCase()); inflater = getLayoutInflater(); diff --git a/src/cgeo/geocaching/cgeovisit.java b/src/cgeo/geocaching/cgeovisit.java index 43a97aa..e13a5bc 100644 --- a/src/cgeo/geocaching/cgeovisit.java +++ b/src/cgeo/geocaching/cgeovisit.java @@ -778,7 +778,7 @@ public class cgeovisit extends cgLogForm { logNow.type = typeSelected; logNow.log = log; - if (cache != null) { + if (cache != null && null != cache.logs) { cache.logs.add(0, logNow); } app.addLog(geocode, logNow); |
