Add BIZCARD support and a little refactoring
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 1 Jul 2008 19:02:03 +0000 (19:02 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 1 Jul 2008 19:02:03 +0000 (19:02 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@494 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/client/result/AbstractDoCoMoResultParser.java
core/src/com/google/zxing/client/result/AddressBookDoCoMoResultParser.java
core/src/com/google/zxing/client/result/BizcardResultParser.java [new file with mode: 0644]
core/src/com/google/zxing/client/result/ResultParser.java

index b50cfdc..30b260c 100644 (file)
@@ -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 };
+  }
+
+}
index eaedc31..65ffc3e 100644 (file)
@@ -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 (file)
index 0000000..f756e8f
--- /dev/null
@@ -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;
+    }
+  }
+
+}
index c7d0295..0f928c7 100644 (file)
@@ -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
+}