Remove some redundant 'throws'; allocate more reasonably sized StringBuffers for...
[zxing.git] / core / src / com / google / zxing / client / result / VCardResultParser.java
index 834db4c..98488b5 100644 (file)
@@ -24,39 +24,44 @@ import java.util.Vector;
  * Parses contact information formatted according to the VCard (2.1) format. This is not a complete
  * implementation but should parse information as commonly encoded in 2D barcodes.
  *
- * @author srowen@google.com (Sean Owen)
+ * @author Sean Owen
  */
-public final class VCardResultParser extends ResultParser {
+final class VCardResultParser extends ResultParser {
 
   private VCardResultParser() {
   }
 
   public static AddressBookParsedResult parse(Result result) {
+    // Although we should insist on the raw text ending with "END:VCARD", there's no reason
+    // to throw out everything else we parsed just because this was omitted. In fact, Eclair
+    // is doing just that, and we can't parse its contacts without this leniency.
     String rawText = result.getText();
-    if (rawText == null || !rawText.startsWith("BEGIN:VCARD") || !rawText.endsWith("END:VCARD")) {
+    if (rawText == null || !rawText.startsWith("BEGIN:VCARD")) {
       return null;
     }
-    String[] names = matchVCardPrefixedField("FN", rawText);
+    String[] names = matchVCardPrefixedField("FN", rawText, true);
     if (names == null) {
       // If no display names found, look for regular name fields and format them
-      names = matchVCardPrefixedField("N", rawText);
+      names = matchVCardPrefixedField("N", rawText, true);
       formatNames(names);
     }
-    String[] phoneNumbers = matchVCardPrefixedField("TEL", rawText);
-    String[] emails = matchVCardPrefixedField("EMAIL", rawText);
-    String note = matchSingleVCardPrefixedField("NOTE", rawText);
-    String address = matchSingleVCardPrefixedField("ADR", rawText);
+    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 org = matchSingleVCardPrefixedField("ORG", rawText);
-    String birthday = matchSingleVCardPrefixedField("BDAY", rawText);
+    String org = matchSingleVCardPrefixedField("ORG", rawText, true);
+    String birthday = matchSingleVCardPrefixedField("BDAY", rawText, true);
     if (birthday != null && !isStringOfDigits(birthday, 8)) {
       return null;
     }
-    String title = matchSingleVCardPrefixedField("TITLE", rawText);
-    return new AddressBookParsedResult(names, phoneNumbers, emails, note, address, org, birthday, title); 
+    String title = matchSingleVCardPrefixedField("TITLE", rawText, true);
+    String url = matchSingleVCardPrefixedField("URL", rawText, true);
+    return new AddressBookParsedResult(names, null, phoneNumbers, emails, note, address, org,
+        birthday, title, url);
   }
 
-  private static String[] matchVCardPrefixedField(String prefix, String rawText) {
+  private static String[] matchVCardPrefixedField(String prefix, String rawText, boolean trim) {
     Vector matches = null;
     int i = 0;
     int max = rawText.length();
@@ -65,7 +70,7 @@ public final class VCardResultParser extends ResultParser {
       if (i < 0) {
         break;
       }
-      if (rawText.charAt(i - 1) != '\n') {
+      if (i > 0 && rawText.charAt(i - 1) != '\n') {
         // then this didn't start a new token, we matched in the middle of something
         i++;
         continue;
@@ -79,22 +84,23 @@ public final class VCardResultParser extends ResultParser {
       }
       i++; // skip colon
       int start = i; // Found the start of a match here
-      boolean done = false;
-      while (!done) {
-        i = rawText.indexOf((int) '\n', i); // Really, ends in \r\n
-        if (i < 0) {
-          // No terminating end character? uh, done. Set i such that loop terminates and break
-          i = rawText.length();
-          done = true;
-        } else {
-          // found a match
-          if (matches == null) {
-            matches = new Vector(3); // lazy init
-          }
-          matches.addElement(rawText.substring(start, i - 1)); // i - 1 to strip off the \r too
-          i++;
-          done = true;
+      i = rawText.indexOf((int) '\n', i); // Really, ends in \r\n
+      if (i < 0) {
+        // No terminating end character? uh, done. Set i such that loop terminates and break
+        i = max;
+      } else if (i > start) {
+        // found a match
+        if (matches == null) {
+          matches = new Vector(3); // lazy init
+        }
+        String element = rawText.substring(start, i);
+        if (trim) {
+          element = element.trim();
         }
+        matches.addElement(element);
+        i++;
+      } else {
+        i++;
       }
     }
     if (matches == null || matches.isEmpty()) {
@@ -103,8 +109,8 @@ public final class VCardResultParser extends ResultParser {
     return toStringArray(matches);
   }
 
-  private static String matchSingleVCardPrefixedField(String prefix, String rawText) {
-    String[] values = matchVCardPrefixedField(prefix, rawText);
+  static String matchSingleVCardPrefixedField(String prefix, String rawText, boolean trim) {
+    String[] values = matchVCardPrefixedField(prefix, rawText, trim);
     return values == null ? null : values[0];
   }
 
@@ -145,7 +151,7 @@ public final class VCardResultParser extends ResultParser {
           start = end + 1;
         }
         components[componentIndex] = name.substring(start);
-        StringBuffer newName = new StringBuffer();
+        StringBuffer newName = new StringBuffer(100);
         maybeAppendComponent(components, 3, newName);
         maybeAppendComponent(components, 1, newName);
         maybeAppendComponent(components, 2, newName);
@@ -163,4 +169,4 @@ public final class VCardResultParser extends ResultParser {
     }
   }
 
-}
\ No newline at end of file
+}