From 1c93d30a4df918837f81360d43d145dd255dd63f Mon Sep 17 00:00:00 2001 From: SammysHP Date: Fri, 26 Aug 2011 13:31:38 +0200 Subject: Improve date-parsing. Maybe not the best solution (performance, memory), but it supports 6 out of 7 formats you can choose in gc.com-settings. Closes #294 --- src/cgeo/geocaching/cgBase.java | 80 +++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 35 deletions(-) (limited to 'src/cgeo/geocaching/cgBase.java') diff --git a/src/cgeo/geocaching/cgBase.java b/src/cgeo/geocaching/cgBase.java index e143ab7..92082a3 100644 --- a/src/cgeo/geocaching/cgBase.java +++ b/src/cgeo/geocaching/cgBase.java @@ -23,6 +23,7 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; +import java.util.Collections; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; @@ -80,7 +81,7 @@ public class cgBase { private final static Pattern patternTerrain = Pattern.compile("]*>[^<]*\"[^\"]+\"[^]*>[^<]*", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); private final static Pattern patternOwner = Pattern.compile("\\W*An?(\\W*Event)?\\W*cache\\W*by[^<]*([^<]+)[^<]*", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); private final static Pattern patternOwnerReal = Pattern.compile("[^<]+", Pattern.CASE_INSENSITIVE); - private final static Pattern patternHidden = Pattern.compile("]*>\\W*Hidden[^\\d]*([^<]+)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); + private final static Pattern patternHidden = Pattern.compile("]*>\\W*Hidden[\\s:]*([^<]+)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); private final static Pattern patternHiddenEvent = Pattern.compile("]*>\\W*Event\\W*Date[^:]*:([^<]*)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); private final static Pattern patternFavourite = Pattern.compile("]*>[^<]*[^\\d]*([0-9]+)[^\\d^<]*", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); @@ -116,9 +117,26 @@ public class cgBase { public static HashMap logTypesTrackable = new HashMap(); public static HashMap logTypesTrackableAction = new HashMap(); public static HashMap errorRetrieve = new HashMap(); - public final static SimpleDateFormat dateInSlash = 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 static final Map gcCustomDateFormats; + static { + final String[] formats = new String[] { + "MM/dd/yyyy", + "yyyy-MM-dd", + "yyyy/MM/dd", + "dd/MMM/yyyy", + "MMM/dd/yyyy", + "dd MMM yy" + }; + + HashMap map = new HashMap(); + + for (String format : formats) + { + map.put(format, new SimpleDateFormat(format, Locale.ENGLISH)); + } + + gcCustomDateFormats = Collections.unmodifiableMap(map); + } 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 @@ -1244,10 +1262,10 @@ public class cgBase { final Matcher matcherHidden = patternHidden.matcher(tableInside); if (matcherHidden.find()) { if (matcherHidden.groupCount() > 0) { - cache.hidden = parseDate(matcherHidden.group(1)); + cache.hidden = parseGcCustomDate(matcherHidden.group(1)); } } - } catch (Exception e) { + } catch (ParseException e) { // failed to parse cache hidden date Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache hidden date"); } @@ -1258,10 +1276,10 @@ public class cgBase { final Matcher matcherHiddenEvent = patternHiddenEvent.matcher(tableInside); if (matcherHiddenEvent.find()) { if (matcherHiddenEvent.groupCount() > 0) { - cache.hidden = parseDate(matcherHiddenEvent.group(1)); + cache.hidden = parseGcCustomDate(matcherHiddenEvent.group(1)); } } - } catch (Exception e) { + } catch (ParseException e) { // failed to parse cache event date Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache event date"); } @@ -1613,7 +1631,7 @@ public class cgBase { try { - logDone.date = dateInSlash.parse(matcherLog.group(5)).getTime(); + logDone.date = parseGcCustomDate(matcherLog.group(5)).getTime(); } catch (ParseException e) { @@ -1878,34 +1896,26 @@ public class cgBase { // And BTW: You cannot even see that effect in the debugger, but must use a separate memory profiler! } - private static Date parseDate(String input) { - if (input == null) { - return null; + private static Date parseGcCustomDate(String input) + throws ParseException + { + if (input == null) + { + throw new ParseException("Input is null", 0); } + input = input.trim(); - try { - Date result; - if (input.indexOf('/') > 0) { - result = dateInSlash.parse(input); - if (result != null) { - return result; - } - } - if (input.indexOf('-') > 0) { - result = dateInDash.parse(input); - if (result != null) { - return result; - } - } - result = dateEvIn.parse(input); - if (result != null) { - return result; - } - } catch (ParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return null; + + for (SimpleDateFormat format : gcCustomDateFormats.values()) + { + try + { + return format.parse(input); + } + catch (ParseException e) {} + } + + throw new ParseException("No matching pattern", 0); } public cgRating getRating(String guid, String geocode) { -- cgit v1.1