From 6271b8631501e5e9f9931c21d76f0383b7908bb5 Mon Sep 17 00:00:00 2001 From: srowen Date: Thu, 10 Dec 2009 00:34:54 +0000 Subject: [PATCH] Issue 294, add support for several address lines in parsed results git-svn-id: https://zxing.googlecode.com/svn/trunk@1148 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../client/android/encode/QRCodeEncoder.java | 12 ++++++---- .../result/AddressBookResultHandler.java | 4 ++-- .../result/AddressBookAUResultParser.java | 3 ++- .../result/AddressBookDoCoMoResultParser.java | 4 ++-- .../result/AddressBookParsedResult.java | 24 +++++++++++++++---- .../client/result/BizcardResultParser.java | 4 ++-- .../client/result/VCardResultParser.java | 10 +++++--- .../AddressBookParsedResultTestCase.java | 16 +++++++++---- 8 files changed, 54 insertions(+), 23 deletions(-) diff --git a/android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java b/android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java index e4b04a135..599e40f5b 100755 --- a/android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java +++ b/android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java @@ -257,10 +257,14 @@ final class QRCodeEncoder { newContents.append("N:").append(names[0]).append(';'); newDisplayContents.append(names[0]); } - String address = contact.getAddress(); - if (address != null && address.length() > 0) { - newContents.append("ADR:").append(address).append(';'); - newDisplayContents.append('\n').append(address); + String[] addresses = contact.getAddresses(); + if (addresses != null) { + for (int x = 0; x < addresses.length; x++) { + if (addresses[x] != null && addresses[x].length() > 0) { + newContents.append("ADR:").append(addresses[x]).append(';'); + newDisplayContents.append('\n').append(addresses[x]); + } + } } String[] phoneNumbers = contact.getPhoneNumbers(); if (phoneNumbers != null) { diff --git a/android/src/com/google/zxing/client/android/result/AddressBookResultHandler.java b/android/src/com/google/zxing/client/android/result/AddressBookResultHandler.java index 573adb4cf..16df105b8 100644 --- a/android/src/com/google/zxing/client/android/result/AddressBookResultHandler.java +++ b/android/src/com/google/zxing/client/android/result/AddressBookResultHandler.java @@ -62,8 +62,8 @@ public final class AddressBookResultHandler extends ResultHandler { public AddressBookResultHandler(Activity activity, ParsedResult result) { super(activity, result); AddressBookParsedResult addressResult = (AddressBookParsedResult) result; - String address = addressResult.getAddress(); - boolean hasAddress = address != null && address.length() > 0; + String[] addresses = addressResult.getAddresses(); + boolean hasAddress = addresses != null && addresses.length > 0 && addresses[0].length() > 0; String[] phoneNumbers = addressResult.getPhoneNumbers(); boolean hasPhoneNumber = phoneNumbers != null && phoneNumbers.length > 0; String[] emails = addressResult.getEmails(); diff --git a/core/src/com/google/zxing/client/result/AddressBookAUResultParser.java b/core/src/com/google/zxing/client/result/AddressBookAUResultParser.java index 84585797f..49ab174bd 100644 --- a/core/src/com/google/zxing/client/result/AddressBookAUResultParser.java +++ b/core/src/com/google/zxing/client/result/AddressBookAUResultParser.java @@ -46,8 +46,9 @@ final class AddressBookAUResultParser extends ResultParser { String[] emails = matchMultipleValuePrefix("MAIL", 3, rawText, true); String note = matchSinglePrefixedField("MEMORY:", rawText, '\r', false); String address = matchSinglePrefixedField("ADD:", rawText, '\r', true); + String[] addresses = address == null ? null : new String[] {address}; return new AddressBookParsedResult(maybeWrap(name), pronunciation, phoneNumbers, emails, note, - address, null, null, null, null); + addresses, null, null, null, null); } private static String[] matchMultipleValuePrefix(String prefix, int max, String rawText, diff --git a/core/src/com/google/zxing/client/result/AddressBookDoCoMoResultParser.java b/core/src/com/google/zxing/client/result/AddressBookDoCoMoResultParser.java index ab4395f1a..86fe8b90c 100644 --- a/core/src/com/google/zxing/client/result/AddressBookDoCoMoResultParser.java +++ b/core/src/com/google/zxing/client/result/AddressBookDoCoMoResultParser.java @@ -49,7 +49,7 @@ final class AddressBookDoCoMoResultParser extends AbstractDoCoMoResultParser { String[] phoneNumbers = matchDoCoMoPrefixedField("TEL:", rawText, true); String[] emails = matchDoCoMoPrefixedField("EMAIL:", rawText, true); String note = matchSingleDoCoMoPrefixedField("NOTE:", rawText, false); - String address = matchSingleDoCoMoPrefixedField("ADR:", rawText, true); + String[] addresses = matchDoCoMoPrefixedField("ADR:", rawText, true); String birthday = matchSingleDoCoMoPrefixedField("BDAY:", rawText, true); if (birthday != null && !isStringOfDigits(birthday, 8)) { // No reason to throw out the whole card because the birthday is formatted wrong. @@ -66,7 +66,7 @@ final class AddressBookDoCoMoResultParser extends AbstractDoCoMoResultParser { phoneNumbers, emails, note, - address, + addresses, org, birthday, null, diff --git a/core/src/com/google/zxing/client/result/AddressBookParsedResult.java b/core/src/com/google/zxing/client/result/AddressBookParsedResult.java index 7ef889c5d..8f39e626b 100644 --- a/core/src/com/google/zxing/client/result/AddressBookParsedResult.java +++ b/core/src/com/google/zxing/client/result/AddressBookParsedResult.java @@ -26,7 +26,7 @@ public final class AddressBookParsedResult extends ParsedResult { private final String[] phoneNumbers; private final String[] emails; private final String note; - private final String address; + private final String[] addresses; private final String org; private final String birthday; private final String title; @@ -37,7 +37,7 @@ public final class AddressBookParsedResult extends ParsedResult { String[] phoneNumbers, String[] emails, String note, - String address, + String[] addresses, String org, String birthday, String title, @@ -48,7 +48,7 @@ public final class AddressBookParsedResult extends ParsedResult { this.phoneNumbers = phoneNumbers; this.emails = emails; this.note = note; - this.address = address; + this.addresses = addresses; this.org = org; this.birthday = birthday; this.title = title; @@ -81,8 +81,22 @@ public final class AddressBookParsedResult extends ParsedResult { return note; } + public String[] getAddresses() { + return addresses; + } + public String getAddress() { - return address; + if (addresses == null || addresses.length == 0) { + return null; + } + StringBuffer result = new StringBuffer(); + for (int i = 0; i < addresses.length; i++) { + if (result.length() > 0) { + result.append(' '); + } + result.append(addresses[i]); + } + return result.toString(); } public String getTitle() { @@ -110,7 +124,7 @@ public final class AddressBookParsedResult extends ParsedResult { maybeAppend(pronunciation, result); maybeAppend(title, result); maybeAppend(org, result); - maybeAppend(address, result); + maybeAppend(addresses, result); maybeAppend(phoneNumbers, result); maybeAppend(emails, result); maybeAppend(url, result); diff --git a/core/src/com/google/zxing/client/result/BizcardResultParser.java b/core/src/com/google/zxing/client/result/BizcardResultParser.java index d6a4e5ceb..0de970a67 100644 --- a/core/src/com/google/zxing/client/result/BizcardResultParser.java +++ b/core/src/com/google/zxing/client/result/BizcardResultParser.java @@ -43,7 +43,7 @@ final class BizcardResultParser extends AbstractDoCoMoResultParser { String fullName = buildName(firstName, lastName); String title = matchSingleDoCoMoPrefixedField("T:", rawText, true); String org = matchSingleDoCoMoPrefixedField("C:", rawText, true); - String address = matchSingleDoCoMoPrefixedField("A:", rawText, true); + String[] addresses = matchDoCoMoPrefixedField("A:", rawText, true); String phoneNumber1 = matchSingleDoCoMoPrefixedField("B:", rawText, true); String phoneNumber2 = matchSingleDoCoMoPrefixedField("M:", rawText, true); String phoneNumber3 = matchSingleDoCoMoPrefixedField("F:", rawText, true); @@ -54,7 +54,7 @@ final class BizcardResultParser extends AbstractDoCoMoResultParser { buildPhoneNumbers(phoneNumber1, phoneNumber2, phoneNumber3), maybeWrap(email), null, - address, + addresses, org, null, title, diff --git a/core/src/com/google/zxing/client/result/VCardResultParser.java b/core/src/com/google/zxing/client/result/VCardResultParser.java index 047c3420b..6b3c28229 100644 --- a/core/src/com/google/zxing/client/result/VCardResultParser.java +++ b/core/src/com/google/zxing/client/result/VCardResultParser.java @@ -48,8 +48,12 @@ final class VCardResultParser extends ResultParser { String[] phoneNumbers = matchVCardPrefixedField("TEL", rawText, true); String[] emails = matchVCardPrefixedField("EMAIL", rawText, true); String note = matchSingleVCardPrefixedField("NOTE", rawText, false); - String address = matchSingleVCardPrefixedField("ADR", rawText, true); - address = formatAddress(address); + String[] addresses = matchVCardPrefixedField("ADR", rawText, true); + if (addresses != null) { + for (int i = 0; i < addresses.length; i++) { + addresses[i] = formatAddress(addresses[i]); + } + } String org = matchSingleVCardPrefixedField("ORG", rawText, true); String birthday = matchSingleVCardPrefixedField("BDAY", rawText, true); if (!isLikeVCardDate(birthday)) { @@ -57,7 +61,7 @@ final class VCardResultParser extends ResultParser { } String title = matchSingleVCardPrefixedField("TITLE", rawText, true); String url = matchSingleVCardPrefixedField("URL", rawText, true); - return new AddressBookParsedResult(names, null, phoneNumbers, emails, note, address, org, + return new AddressBookParsedResult(names, null, phoneNumbers, emails, note, addresses, org, birthday, title, url); } diff --git a/core/test/src/com/google/zxing/client/result/AddressBookParsedResultTestCase.java b/core/test/src/com/google/zxing/client/result/AddressBookParsedResultTestCase.java index 870654b79..bd23ebc43 100644 --- a/core/test/src/com/google/zxing/client/result/AddressBookParsedResultTestCase.java +++ b/core/test/src/com/google/zxing/client/result/AddressBookParsedResultTestCase.java @@ -43,20 +43,28 @@ public final class AddressBookParsedResultTestCase extends TestCase { public void testVCard() { doTest("BEGIN:VCARD\r\nADR;HOME:123 Main St\r\nVERSION:2.1\r\nN:Owen;Sean\r\nEND:VCARD", - null, new String[] {"Sean Owen"}, null, "123 Main St", null, null, null, null, null, null); + null, new String[] {"Sean Owen"}, null, new String[] {"123 Main St"}, null, null, null, null, null, null); } public void testBizcard() { doTest("BIZCARD:N:Sean;X:Owen;C:Google;A:123 Main St;M:+12125551212;E:srowen@example.org;", - null, new String[] {"Sean Owen"}, null, "123 Main St", new String[] {"srowen@example.org"}, + null, new String[] {"Sean Owen"}, null, new String[] {"123 Main St"}, new String[] {"srowen@example.org"}, new String[] {"+12125551212"}, "Google", null, null, null); } + public void testSeveralAddresses() { + doTest("MECARD:N:Foo Bar;ORG:Company;TEL:5555555555;EMAIL:foo.bar@xyz.com;ADR:City, 10001;" + + "ADR:City, 10001;NOTE:This is the memo.;;", + null, new String[] {"Foo Bar"}, null, new String[] {"City, 10001", "City, 10001"}, + new String[] {"foo.bar@xyz.com"}, + new String[] {"5555555555" }, "Company", null, null, "This is the memo."); + } + private static void doTest(String contents, String title, String[] names, String pronunciation, - String address, + String[] addresses, String[] emails, String[] phoneNumbers, String org, @@ -70,7 +78,7 @@ public final class AddressBookParsedResultTestCase extends TestCase { assertEquals(title, addressResult.getTitle()); assertTrue(Arrays.equals(names, addressResult.getNames())); assertEquals(pronunciation, addressResult.getPronunciation()); - assertEquals(address, addressResult.getAddress()); + assertTrue(Arrays.equals(addresses, addressResult.getAddresses())); assertTrue(Arrays.equals(emails, addressResult.getEmails())); assertTrue(Arrays.equals(phoneNumbers, addressResult.getPhoneNumbers())); assertEquals(org, addressResult.getOrg());