From 64369b3878e0c5f64ea65c961ff9de1298e7b3ce Mon Sep 17 00:00:00 2001 From: srowen Date: Tue, 1 Jul 2008 19:02:03 +0000 Subject: [PATCH] Add BIZCARD support and a little refactoring git-svn-id: https://zxing.googlecode.com/svn/trunk@494 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../result/AbstractDoCoMoResultParser.java | 9 ++- .../result/AddressBookDoCoMoResultParser.java | 6 +- .../client/result/BizcardResultParser.java | 75 +++++++++++++++++++ .../zxing/client/result/ResultParser.java | 4 +- 4 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 core/src/com/google/zxing/client/result/BizcardResultParser.java diff --git a/core/src/com/google/zxing/client/result/AbstractDoCoMoResultParser.java b/core/src/com/google/zxing/client/result/AbstractDoCoMoResultParser.java index b50cfdcbf..30b260cf6 100644 --- a/core/src/com/google/zxing/client/result/AbstractDoCoMoResultParser.java +++ b/core/src/com/google/zxing/client/result/AbstractDoCoMoResultParser.java @@ -28,9 +28,6 @@ package com.google.zxing.client.result; */ abstract class AbstractDoCoMoResultParser extends ResultParser { - // This could as well be implemented with java.util.regex. It was already implemented partially - // to run in a J2ME enviroment, where this unavailable. - static String[] matchPrefixedField(String prefix, String rawText) { return matchPrefixedField(prefix, rawText, ';'); } @@ -39,4 +36,8 @@ abstract class AbstractDoCoMoResultParser extends ResultParser { return matchSinglePrefixedField(prefix, rawText, ';'); } -} \ No newline at end of file + static String[] maybeWrap(String value) { + return value == null ? null : new String[] { value }; + } + +} diff --git a/core/src/com/google/zxing/client/result/AddressBookDoCoMoResultParser.java b/core/src/com/google/zxing/client/result/AddressBookDoCoMoResultParser.java index eaedc31ea..65ffc3eaa 100644 --- a/core/src/com/google/zxing/client/result/AddressBookDoCoMoResultParser.java +++ b/core/src/com/google/zxing/client/result/AddressBookDoCoMoResultParser.java @@ -48,9 +48,9 @@ public final class AddressBookDoCoMoResultParser extends AbstractDoCoMoResultPar if (birthday != null && !isStringOfDigits(birthday, 8)) { return null; } - return new AddressBookParsedResult(new String[] {name}, + return new AddressBookParsedResult(maybeWrap(name), phoneNumbers, - new String[] {email}, + maybeWrap(email), note, address, null, @@ -67,4 +67,4 @@ public final class AddressBookDoCoMoResultParser extends AbstractDoCoMoResultPar return name; } -} \ No newline at end of file +} diff --git a/core/src/com/google/zxing/client/result/BizcardResultParser.java b/core/src/com/google/zxing/client/result/BizcardResultParser.java new file mode 100644 index 000000000..f756e8f93 --- /dev/null +++ b/core/src/com/google/zxing/client/result/BizcardResultParser.java @@ -0,0 +1,75 @@ +/* + * Copyright 2008 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.zxing.client.result; + +import com.google.zxing.Result; + +/** + * Implements the "BIZCARD" address book entry format, though this has been + * largely reverse-engineered from examples observed in the wild -- still + * looking for a definitive reference. + * + * @author srowen@google.com (Sean Owen) + */ +public final class BizcardResultParser extends AbstractDoCoMoResultParser { + + // Yes, we extend AbstractDoCoMoResultParser since the format is very much + // like the DoCoMo MECARD format, but this is not technically one of + // DoCoMo's proposed formats + + public static AddressBookParsedResult parse(Result result) { + String rawText = result.getText(); + if (rawText == null || !rawText.startsWith("BIZCARD:")) { + return null; + } + String firstName = matchSinglePrefixedField("N:", rawText); + String lastName = matchSinglePrefixedField("X:", rawText); + String fullName = buildName(firstName, lastName); + String title = matchSinglePrefixedField("T:", rawText); + String org = matchSinglePrefixedField("C:", rawText); + String address = matchSinglePrefixedField("A:", rawText); + String phoneNumber1 = matchSinglePrefixedField("B:", rawText); + String phoneNumber2 = matchSinglePrefixedField("F:", rawText); + String email = matchSinglePrefixedField("E:", rawText); + + return new AddressBookParsedResult(maybeWrap(fullName), + buildPhoneNumbers(phoneNumber1, phoneNumber2), + maybeWrap(email), + null, + address, + org, + null, + title); + } + + private static String[] buildPhoneNumbers(String number1, String number2) { + if (number1 == null) { + return maybeWrap(number2); + } else { + return number2 == null ? new String[] { number1 } : new String[] { number1, number2 }; + } + } + + private static String buildName(String firstName, String lastName) { + if (firstName == null) { + return lastName; + } else { + return lastName == null ? firstName : firstName + ' ' + lastName; + } + } + +} diff --git a/core/src/com/google/zxing/client/result/ResultParser.java b/core/src/com/google/zxing/client/result/ResultParser.java index c7d029582..0f928c7d7 100644 --- a/core/src/com/google/zxing/client/result/ResultParser.java +++ b/core/src/com/google/zxing/client/result/ResultParser.java @@ -51,6 +51,8 @@ public abstract class ResultParser { return result; } else if ((result = VCardResultParser.parse(theResult)) != null) { return result; + } else if ((result = BizcardResultParser.parse(theResult)) != null) { + return result; } else if ((result = TelResultParser.parse(theResult)) != null) { return result; } else if ((result = SMSMMSResultParser.parse(theResult)) != null) { @@ -280,4 +282,4 @@ public abstract class ResultParser { return result; } -} \ No newline at end of file +}