Loosen BDAY parsing for vCard results

git-svn-id: https://zxing.googlecode.com/svn/trunk@1118 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2009-11-22 11:14:39 +00:00
parent c0c1290611
commit f75a6ec766
2 changed files with 35 additions and 1 deletions

View 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) {

View 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;