Fixed crash and bad parsing of an SMS with a question mark in the subject or message.
authordswitkin <dswitkin@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Fri, 19 Dec 2008 15:00:20 +0000 (15:00 +0000)
committerdswitkin <dswitkin@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Fri, 19 Dec 2008 15:00:20 +0000 (15:00 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@794 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/client/result/ResultParser.java
core/src/com/google/zxing/client/result/SMSMMSResultParser.java
core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java
javase/src/com/google/zxing/client/j2se/CommandLineRunner.java

index a00c0ef..d741de6 100644 (file)
@@ -232,11 +232,8 @@ 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) {
index 8e38835..dfa5da0 100644 (file)
@@ -51,10 +51,23 @@ final class SMSMMSResultParser extends ResultParser {
     } else {
       return null;
     }
+
+    // Check up front if this is a URI syntax string with query arguments
+    Hashtable nameValuePairs = parseNameValuePairs(rawText);
+    String subject = null;
+    String body = null;
+    boolean querySyntax = false;
+    if (nameValuePairs != null && nameValuePairs.size() > 0) {
+      subject = (String) nameValuePairs.get("subject");
+      body = (String) nameValuePairs.get("body");
+      querySyntax = true;
+    }
+
     // Drop sms, query portion
     int queryStart = rawText.indexOf('?', prefixLength);
     String smsURIWithoutQuery;
-    if (queryStart < 0) {
+    // If it's not query syntax, the question mark is part of the subject or message
+    if (queryStart < 0 || !querySyntax) {
       smsURIWithoutQuery = rawText.substring(prefixLength);
     } else {
       smsURIWithoutQuery = rawText.substring(prefixLength, queryStart);
@@ -74,13 +87,7 @@ final class SMSMMSResultParser extends ResultParser {
         via = null;
       }
     }
-    Hashtable nameValuePairs = parseNameValuePairs(rawText);
-    String subject = null;
-    String body = null;
-    if (nameValuePairs != null) {
-      subject = (String) nameValuePairs.get("subject");
-      body = (String) nameValuePairs.get("body");
-    }
+
     // Thanks to dominik.wild for suggesting this enhancement to support
     // smsto:number:body URIs
     if (body == null) {
index 9474f98..b2419ab 100644 (file)
@@ -121,6 +121,7 @@ public final class ParsedReaderResultTestCase extends TestCase {
     doTestResult("TEL:+15551212", ParsedResultType.TEL);
     doTestResult("tel:212 555 1212", ParsedResultType.TEL);
     doTestResult("tel:2125551212", ParsedResultType.TEL);
+    doTestResult("tel:212-555-1212", ParsedResultType.TEL);
     doTestResult("telephone", ParsedResultType.TEXT);
   }
 
@@ -167,6 +168,9 @@ public final class ParsedReaderResultTestCase extends TestCase {
     doTestResult("sms:+15551212;via=999333", ParsedResultType.SMS);
     doTestResult("sms:+15551212?subject=foo&body=bar", ParsedResultType.SMS);
     doTestResult("sms:+15551212:subject", ParsedResultType.SMS);
+    // Need to handle question mark in the subject
+    doTestResult("sms:+15551212:What's up?", ParsedResultType.SMS);
+    doTestResult("sms:212-555-1212:Here's a longer message. Should be fine.", ParsedResultType.SMS);
   }
 
   public void testMMS() {
@@ -177,6 +181,8 @@ public final class ParsedReaderResultTestCase extends TestCase {
     doTestResult("mms:+15551212;via=999333", ParsedResultType.SMS);
     doTestResult("mms:+15551212?subject=foo&body=bar", ParsedResultType.SMS);
     doTestResult("mms:+15551212:subject", ParsedResultType.SMS);
+    doTestResult("mms:+15551212:What's up?", ParsedResultType.SMS);
+    doTestResult("mms:212-555-1212:Here's a longer message. Should be fine.", ParsedResultType.SMS);
   }
 
   /*
index 08e17f4..78f7626 100644 (file)
@@ -21,6 +21,8 @@ import com.google.zxing.MonochromeBitmapSource;
 import com.google.zxing.MultiFormatReader;
 import com.google.zxing.ReaderException;
 import com.google.zxing.Result;
+import com.google.zxing.client.result.ParsedResult;
+import com.google.zxing.client.result.ResultParser;
 
 import javax.imageio.ImageIO;
 import java.awt.image.BufferedImage;
@@ -31,8 +33,8 @@ import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.net.URI;
-import java.util.Hashtable;
 import java.nio.charset.Charset;
+import java.util.Hashtable;
 
 /**
  * <p>This simple command line utility decodes files, directories of files, or URIs which are passed
@@ -138,8 +140,10 @@ public final class CommandLineRunner {
     try {
       MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
       Result result = new MultiFormatReader().decode(source, hints);
-      System.out.println(uri.toString() + " (format: " + result.getBarcodeFormat() + "):\n" +
-          result.getText());
+      ParsedResult parsedResult = ResultParser.parseResult(result);
+      System.out.println(uri.toString() + " (format: " + result.getBarcodeFormat() +
+          ", type: " + parsedResult.getType() + "):\nRaw result:\n" + result.getText() +
+          "\nParsed result:\n" + parsedResult.getDisplayResult());
       return result;
     } catch (ReaderException e) {
       System.out.println(uri.toString() + ": No barcode found");