Loosen BDAY parsing for vCard results
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Sun, 22 Nov 2009 11:14:39 +0000 (11:14 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Sun, 22 Nov 2009 11:14:39 +0000 (11:14 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@1118 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/client/result/ResultParser.java
core/src/com/google/zxing/client/result/VCardResultParser.java

index c5fd9e0..18613ab 100644 (file)
@@ -208,6 +208,24 @@ public abstract class ResultParser {
     return true;
   }
 
+  protected static boolean isSubstringOfDigits(String value, int offset, int length) {
+    if (value == null) {
+      return false;
+    }
+    int stringLength = value.length();
+    int max = offset + length;
+    if (stringLength < max) {
+      return false;
+    }
+    for (int i = offset; i < max; i++) {
+      char c = value.charAt(i);
+      if (c < '0' || c > '9') {
+        return false;
+      }
+    }
+    return true;
+  }
+
   static Hashtable parseNameValuePairs(String uri) {
     int paramStart = uri.indexOf('?');
     if (paramStart < 0) {
index 98488b5..865fec4 100644 (file)
@@ -52,7 +52,7 @@ final class VCardResultParser extends ResultParser {
     address = formatAddress(address);
     String org = matchSingleVCardPrefixedField("ORG", rawText, true);
     String birthday = matchSingleVCardPrefixedField("BDAY", rawText, true);
-    if (birthday != null && !isStringOfDigits(birthday, 8)) {
+    if (!isLikeVCardDate(birthday)) {
       return null;
     }
     String title = matchSingleVCardPrefixedField("TITLE", rawText, true);
@@ -114,6 +114,22 @@ final class VCardResultParser extends ResultParser {
     return values == null ? null : values[0];
   }
 
+  private static boolean isLikeVCardDate(String value) {
+    // Not really sure this is true but matches practice
+    // Mach YYYYMMDD
+    if (isStringOfDigits(value, 8)) {
+      return true;
+    }
+    // or YYYY-MM-DD
+    return
+        value.length() == 10 &&
+        value.charAt(4) == '-' &&
+        value.charAt(7) == '-' &&
+        isSubstringOfDigits(value, 0, 4) &&
+        isSubstringOfDigits(value, 5, 2) &&
+        isSubstringOfDigits(value, 8, 2);
+  }
+
   private static String formatAddress(String address) {
     if (address == null) {
       return null;