Issue 1184 handle escaped semicolons and fix test

git-svn-id: https://zxing.googlecode.com/svn/trunk@2221 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2012-03-06 22:47:05 +00:00
parent c4b7431a44
commit 882fef230c
3 changed files with 25 additions and 25 deletions

View file

@ -41,7 +41,7 @@ public final class VCardResultParser extends ResultParser {
private static final Pattern VCARD_ESCAPES = Pattern.compile("\\\\([,;\\\\])"); private static final Pattern VCARD_ESCAPES = Pattern.compile("\\\\([,;\\\\])");
private static final Pattern EQUALS = Pattern.compile("="); private static final Pattern EQUALS = Pattern.compile("=");
private static final Pattern SEMICOLON = Pattern.compile(";"); private static final Pattern SEMICOLON = Pattern.compile(";");
private static final Pattern SEMICOLONS = Pattern.compile(";+"); private static final Pattern UNESCAPED_SEMICOLONS = Pattern.compile("(?<!\\\\);+");
@Override @Override
public AddressBookParsedResult parse(Result result) { public AddressBookParsedResult parse(Result result) {
@ -53,32 +53,24 @@ public final class VCardResultParser extends ResultParser {
if (!m.find() || m.start() != 0) { if (!m.find() || m.start() != 0) {
return null; return null;
} }
List<List<String>> names = matchVCardPrefixedField("FN", rawText, true); List<List<String>> names = matchVCardPrefixedField("FN", rawText, true, false);
if (names == null) { if (names == null) {
// If no display names found, look for regular name fields and format them // If no display names found, look for regular name fields and format them
names = matchVCardPrefixedField("N", rawText, true); names = matchVCardPrefixedField("N", rawText, true, false);
formatNames(names); formatNames(names);
} }
List<List<String>> phoneNumbers = matchVCardPrefixedField("TEL", rawText, true); List<List<String>> phoneNumbers = matchVCardPrefixedField("TEL", rawText, true, false);
List<List<String>> emails = matchVCardPrefixedField("EMAIL", rawText, true); List<List<String>> emails = matchVCardPrefixedField("EMAIL", rawText, true, false);
List<String> note = matchSingleVCardPrefixedField("NOTE", rawText, false); List<String> note = matchSingleVCardPrefixedField("NOTE", rawText, false, false);
List<List<String>> addresses = matchVCardPrefixedField("ADR", rawText, true); List<List<String>> addresses = matchVCardPrefixedField("ADR", rawText, true, true);
if (addresses != null) { List<String> org = matchSingleVCardPrefixedField("ORG", rawText, true, false);
for (List<String> list : addresses) { List<String> birthday = matchSingleVCardPrefixedField("BDAY", rawText, true, false);
String adr = list.get(0);
// Semicolon separators -- just make them a newline
adr = SEMICOLONS.matcher(adr).replaceAll("\n").trim();
list.set(0, adr);
}
}
List<String> org = matchSingleVCardPrefixedField("ORG", rawText, true);
List<String> birthday = matchSingleVCardPrefixedField("BDAY", rawText, true);
if (birthday != null && !isLikeVCardDate(birthday.get(0))) { if (birthday != null && !isLikeVCardDate(birthday.get(0))) {
birthday = null; birthday = null;
} }
List<String> title = matchSingleVCardPrefixedField("TITLE", rawText, true); List<String> title = matchSingleVCardPrefixedField("TITLE", rawText, true, false);
List<String> url = matchSingleVCardPrefixedField("URL", rawText, true); List<String> url = matchSingleVCardPrefixedField("URL", rawText, true, false);
List<String> instantMessenger = matchSingleVCardPrefixedField("IMPP", rawText, true); List<String> instantMessenger = matchSingleVCardPrefixedField("IMPP", rawText, true, false);
return new AddressBookParsedResult(toPrimaryValues(names), return new AddressBookParsedResult(toPrimaryValues(names),
null, null,
toPrimaryValues(phoneNumbers), toPrimaryValues(phoneNumbers),
@ -97,7 +89,8 @@ public final class VCardResultParser extends ResultParser {
private static List<List<String>> matchVCardPrefixedField(CharSequence prefix, private static List<List<String>> matchVCardPrefixedField(CharSequence prefix,
String rawText, String rawText,
boolean trim) { boolean trim,
boolean parseFieldDivider) {
List<List<String>> matches = null; List<List<String>> matches = null;
int i = 0; int i = 0;
int max = rawText.length(); int max = rawText.length();
@ -172,7 +165,13 @@ public final class VCardResultParser extends ResultParser {
} }
if (quotedPrintable) { if (quotedPrintable) {
element = decodeQuotedPrintable(element, quotedPrintableCharset); element = decodeQuotedPrintable(element, quotedPrintableCharset);
if (parseFieldDivider) {
element = UNESCAPED_SEMICOLONS.matcher(element).replaceAll("\n").trim();
}
} else { } else {
if (parseFieldDivider) {
element = UNESCAPED_SEMICOLONS.matcher(element).replaceAll("\n").trim();
}
element = CR_LF_SPACE_TAB.matcher(element).replaceAll(""); element = CR_LF_SPACE_TAB.matcher(element).replaceAll("");
element = NEWLINE_ESCAPE.matcher(element).replaceAll("\n"); element = NEWLINE_ESCAPE.matcher(element).replaceAll("\n");
element = VCARD_ESCAPES.matcher(element).replaceAll("$1"); element = VCARD_ESCAPES.matcher(element).replaceAll("$1");
@ -253,8 +252,9 @@ public final class VCardResultParser extends ResultParser {
static List<String> matchSingleVCardPrefixedField(CharSequence prefix, static List<String> matchSingleVCardPrefixedField(CharSequence prefix,
String rawText, String rawText,
boolean trim) { boolean trim,
List<List<String>> values = matchVCardPrefixedField(prefix, rawText, trim); boolean parseFieldDivider) {
List<List<String>> values = matchVCardPrefixedField(prefix, rawText, trim, parseFieldDivider);
return values == null || values.isEmpty() ? null : values.get(0); return values == null || values.isEmpty() ? null : values.get(0);
} }

View file

@ -74,7 +74,7 @@ public final class VEventResultParser extends ResultParser {
private static String matchSingleVCardPrefixedField(CharSequence prefix, private static String matchSingleVCardPrefixedField(CharSequence prefix,
String rawText, String rawText,
boolean trim) { boolean trim) {
List<String> values = VCardResultParser.matchSingleVCardPrefixedField(prefix, rawText, trim); List<String> values = VCardResultParser.matchSingleVCardPrefixedField(prefix, rawText, trim, false);
return values == null || values.isEmpty() ? null : values.get(0); return values == null || values.isEmpty() ? null : values.get(0);
} }

View file

@ -84,7 +84,7 @@ public final class AddressBookParsedResultTestCase extends Assert {
"=38=38=20=4C=79=6E=62=72=6F=6F=6B=0D=0A=43=\r\n" + "=38=38=20=4C=79=6E=62=72=6F=6F=6B=0D=0A=43=\r\n" +
"=4F=20=36=39=39=\r\n" + "=4F=20=36=39=39=\r\n" +
"=39=39;;;\r\nEND:VCARD", "=39=39;;;\r\nEND:VCARD",
null, null, null, new String[] {";;88 Lynbrook\r\nCO 69999;;;"}, null, null, null, new String[] {"88 Lynbrook\r\nCO 69999"},
null, null, null, null, null, null); null, null, null, null, null, null);
} }