Now supports KDDI/AU / Softbank address book format
[zxing.git] / core / src / com / google / zxing / client / result / AbstractDoCoMoResult.java
index fdf3503..88a4024 100644 (file)
@@ -19,9 +19,12 @@ package com.google.zxing.client.result;
 import java.util.Vector;
 
 /**
- * See
+ * <p>See
  * <a href="http://www.nttdocomo.co.jp/english/service/imode/make/content/barcode/about/s2.html">
- * DoCoMo's documentation</a> about the result types represented by subclasses of this class.
+ * DoCoMo's documentation</a> about the result types represented by subclasses of this class.</p>
+ *
+ * <p>Thanks to Jeff Griffin for proposing rewrite of these classes that relies less
+ * on exception-based mechanisms during parsing.</p>
  *
  * @author srowen@google.com (Sean Owen)
  */
@@ -35,6 +38,10 @@ abstract class AbstractDoCoMoResult extends ParsedReaderResult {
   // to run in a J2ME enviroment, where this unavailable.
 
   static String[] matchPrefixedField(String prefix, String rawText) {
+    return matchPrefixedField(prefix, rawText, ';');
+  }
+
+  static String[] matchPrefixedField(String prefix, String rawText, char endChar) {
     Vector matches = null;
     int i = 0;
     int max = rawText.length();
@@ -47,12 +54,12 @@ abstract class AbstractDoCoMoResult extends ParsedReaderResult {
       int start = i; // Found the start of a match here
       boolean done = false;
       while (!done) {
-        i = rawText.indexOf((int) ';', i);
+        i = rawText.indexOf((int) endChar, i);
         if (i < 0) {
-          // No terminating semicolon? uh, done. Set i such that loop terminates and break
+          // No terminating end character? uh, done. Set i such that loop terminates and break
           i = rawText.length();
           done = true;
-        } else if (rawText.charAt(i-1) == '\\') {
+        } else if (rawText.charAt(i - 1) == '\\') {
           // semicolon was escaped so continue
           i++;
         } else {
@@ -66,7 +73,7 @@ abstract class AbstractDoCoMoResult extends ParsedReaderResult {
         }
       }
     }
-    if (matches == null) {
+    if (matches == null || matches.isEmpty()) {
       return null;
     }
     int size = matches.size();
@@ -78,16 +85,12 @@ abstract class AbstractDoCoMoResult extends ParsedReaderResult {
   }
 
   static String matchSinglePrefixedField(String prefix, String rawText) {
-    String[] matches = matchPrefixedField(prefix, rawText);
-    return matches == null ? null : matches[0];
+    return matchSinglePrefixedField(prefix, rawText, ';');
   }
 
-  static String[] matchRequiredPrefixedField(String prefix, String rawText) {
-    String[] result = matchPrefixedField(prefix, rawText);
-    if (result == null) {
-      throw new IllegalArgumentException("Did not match prefix " + prefix);
-    }
-    return result;
+  static String matchSinglePrefixedField(String prefix, String rawText, char endChar) {
+    String[] matches = matchPrefixedField(prefix, rawText, endChar);
+    return matches == null ? null : matches[0];
   }
 
   private static String unescape(String escaped) {
@@ -120,4 +123,13 @@ abstract class AbstractDoCoMoResult extends ParsedReaderResult {
     }
   }
 
+  static void maybeAppend(String[] value, StringBuffer result) {
+    if (value != null) {
+      for (int i = 0; i < value.length; i++) {
+        result.append('\n');
+        result.append(value[i]);
+      }
+    }
+  }
+
 }
\ No newline at end of file