Support SMTP URLs
[zxing.git] / core / src / com / google / zxing / client / result / ResultParser.java
index 440c6e5..bdcc4d0 100644 (file)
@@ -19,21 +19,22 @@ package com.google.zxing.client.result;
 import com.google.zxing.Result;
 
 import java.util.Hashtable;
+import java.util.Vector;
 
 /**
  * <p>Abstract class representing the result of decoding a barcode, as more than
  * a String -- as some type of structured data. This might be a subclass which represents
- * a URL, or an e-mail address. {@link #parseReaderResult(com.google.zxing.Result)} will turn a raw
+ * a URL, or an e-mail address. {@link #parseResult(com.google.zxing.Result)} will turn a raw
  * decoded string into the most appropriate type of structured representation.</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)
+ * @author Sean Owen
  */
 public abstract class ResultParser {
 
-  public static ParsedResult parseReaderResult(Result theResult) {
+  public static ParsedResult parseResult(Result theResult) {
     // This is a bit messy, but given limited options in MIDP / CLDC, this may well be the simplest
     // way to go about this. For example, we have no reflection available, really.
     // Order is important here.
@@ -44,23 +45,38 @@ public abstract class ResultParser {
       return result;
     } else if ((result = EmailDoCoMoResultParser.parse(theResult)) != null) {
       return result;
+    } else if ((result = AddressBookAUResultParser.parse(theResult)) != null) {
+      return result;
+    } else if ((result = VCardResultParser.parse(theResult)) != null) {
+      return result;
+    } else if ((result = BizcardResultParser.parse(theResult)) != null) {
+      return result;
+    } else if ((result = VEventResultParser.parse(theResult)) != null) {
+      return result;
     } else if ((result = EmailAddressResultParser.parse(theResult)) != null) {
       return result;
-    } else if ((result = AddressBookAUResultParser.parse(theResult)) != null) {
+    } else if ((result = SMTPResultParser.parse(theResult)) != null) {
       return result;
     } else if ((result = TelResultParser.parse(theResult)) != null) {
       return result;
-    } else if ((result = SMSResultParser.parse(theResult)) != null) {
+    } else if ((result = SMSMMSResultParser.parse(theResult)) != null) {
       return result;
-    } else if ((result = SMSTOResultParser.parse(theResult)) != null) {
+    } else if ((result = SMSTOMMSTOResultParser.parse(theResult)) != null) {
       return result;
     } else if ((result = GeoResultParser.parse(theResult)) != null) {
       return result;
+    } else if ((result = WifiResultParser.parse(theResult)) != null) {
+      return result;
     } else if ((result = URLTOResultParser.parse(theResult)) != null) {
       return result;
     } else if ((result = URIResultParser.parse(theResult)) != null) {
       return result;
-    } else if ((result = UPCResultParser.parse(theResult)) != null) {
+    } else if ((result = ISBNResultParser.parse(theResult)) != null) {
+      // We depend on ISBN parsing coming before UPC, as it is a subset.
+      return result;
+    } else if ((result = ProductResultParser.parse(theResult)) != null) {
+      return result;
+    } else if ((result = ExpandedProductResultParser.parse(theResult)) != null) {
       return result;
     }
     return new TextParsedResult(theResult.getText(), null);
@@ -82,6 +98,10 @@ public abstract class ResultParser {
     }
   }
 
+  protected static String[] maybeWrap(String value) {
+    return value == null ? null : new String[] { value };
+  }
+
   protected static String unescapeBackslash(String escaped) {
     if (escaped != null) {
       int backslash = escaped.indexOf((int) '\\');
@@ -105,7 +125,7 @@ public abstract class ResultParser {
     return escaped;
   }
 
-  protected static String urlDecode(String escaped) {
+  private static String urlDecode(String escaped) {
 
     // No we can't use java.net.URLDecoder here. JavaME doesn't have it.
     if (escaped == null) {
@@ -179,7 +199,42 @@ public abstract class ResultParser {
     return -1;
   }
 
-  protected static Hashtable parseNameValuePairs(String uri) {
+  protected static boolean isStringOfDigits(String value, int length) {
+    if (value == null) {
+      return false;
+    }
+    int stringLength = value.length();
+    if (length != stringLength) {
+      return false;
+    }
+    for (int i = 0; i < length; i++) {
+      char c = value.charAt(i);
+      if (c < '0' || c > '9') {
+        return false;
+      }
+    }
+    return true;
+  }
+
+  protected static boolean isSubstringOfDigits(String value, int offset, int length) {
+    if (value == null) {
+      return false;
+    }
+    int stringLength = value.length();
+    int max = offset + length;
+    if (stringLength < max) {
+      return false;
+    }
+    for (int i = offset; i < max; i++) {
+      char c = value.charAt(i);
+      if (c < '0' || c > '9') {
+        return false;
+      }
+    }
+    return true;
+  }
+
+  static Hashtable parseNameValuePairs(String uri) {
     int paramStart = uri.indexOf('?');
     if (paramStart < 0) {
       return null;
@@ -203,11 +258,64 @@ public abstract class ResultParser {
       String value = uri.substring(separator + 1, paramEnd);
       value = urlDecode(value);
       result.put(key, value);
-    } else {
-      // key, no value
-      String key = uri.substring(paramStart, paramEnd);
-      result.put(key, null);
     }
+    // Can't put key, null into a hashtable
+  }
+
+  static String[] matchPrefixedField(String prefix, String rawText, char endChar, boolean trim) {
+    Vector matches = null;
+    int i = 0;
+    int max = rawText.length();
+    while (i < max) {
+      i = rawText.indexOf(prefix, i);
+      if (i < 0) {
+        break;
+      }
+      i += prefix.length(); // Skip past this prefix we found to start
+      int start = i; // Found the start of a match here
+      boolean done = false;
+      while (!done) {
+        i = rawText.indexOf((int) endChar, i);
+        if (i < 0) {
+          // 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) == '\\') {
+          // semicolon was escaped so continue
+          i++;
+        } else {
+          // found a match
+          if (matches == null) {
+            matches = new Vector(3); // lazy init
+          }
+          String element = unescapeBackslash(rawText.substring(start, i));
+          if (trim) {
+            element = element.trim();
+          }
+          matches.addElement(element);
+          i++;
+          done = true;
+        }
+      }
+    }
+    if (matches == null || matches.isEmpty()) {
+      return null;
+    }
+    return toStringArray(matches);
+  }
+
+  static String matchSinglePrefixedField(String prefix, String rawText, char endChar, boolean trim) {
+    String[] matches = matchPrefixedField(prefix, rawText, endChar, trim);
+    return matches == null ? null : matches[0];
+  }
+
+  static String[] toStringArray(Vector strings) {
+    int size = strings.size();
+    String[] result = new String[size];
+    for (int j = 0; j < size; j++) {
+      result[j] = (String) strings.elementAt(j);
+    }
+    return result;
   }
 
-}
\ No newline at end of file
+}