mirror of
https://github.com/zxing/zxing.git
synced 2024-11-09 20:44:03 -08:00
Fixed crash and bad parsing of an SMS with a question mark in the subject or message.
git-svn-id: https://zxing.googlecode.com/svn/trunk@794 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
13432e7bb2
commit
48480eba95
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Reference in a new issue