Support SMTP URLs
[zxing.git] / core / src / com / google / zxing / client / result / ResultParser.java
index 0f928c7..bdcc4d0 100644 (file)
@@ -30,7 +30,7 @@ import java.util.Vector;
  * <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 {
 
@@ -45,25 +45,38 @@ public abstract class ResultParser {
       return result;
     } else if ((result = EmailDoCoMoResultParser.parse(theResult)) != null) {
       return result;
-    } else if ((result = EmailAddressResultParser.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 = SMTPResultParser.parse(theResult)) != null) {
+      return result;
     } else if ((result = TelResultParser.parse(theResult)) != null) {
       return result;
     } else if ((result = SMSMMSResultParser.parse(theResult)) != null) {
       return result;
+    } 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);
@@ -85,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) '\\');
@@ -108,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) {
@@ -199,7 +216,25 @@ public abstract class ResultParser {
     return true;
   }
 
-  protected static Hashtable parseNameValuePairs(String uri) {
+  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;
@@ -223,14 +258,11 @@ 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) {
+  static String[] matchPrefixedField(String prefix, String rawText, char endChar, boolean trim) {
     Vector matches = null;
     int i = 0;
     int max = rawText.length();
@@ -256,7 +288,11 @@ public abstract class ResultParser {
           if (matches == null) {
             matches = new Vector(3); // lazy init
           }
-          matches.addElement(unescapeBackslash(rawText.substring(start, i)));
+          String element = unescapeBackslash(rawText.substring(start, i));
+          if (trim) {
+            element = element.trim();
+          }
+          matches.addElement(element);
           i++;
           done = true;
         }
@@ -268,8 +304,8 @@ public abstract class ResultParser {
     return toStringArray(matches);
   }
 
-  static String matchSinglePrefixedField(String prefix, String rawText, char endChar) {
-    String[] matches = matchPrefixedField(prefix, rawText, endChar);
+  static String matchSinglePrefixedField(String prefix, String rawText, char endChar, boolean trim) {
+    String[] matches = matchPrefixedField(prefix, rawText, endChar, trim);
     return matches == null ? null : matches[0];
   }