Ignore UTF byte order mark when parsing result

git-svn-id: https://zxing.googlecode.com/svn/trunk@2228 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2012-03-14 17:09:47 +00:00
parent 7399f24735
commit 9f45bab33c
19 changed files with 27 additions and 25 deletions

View file

@ -33,7 +33,7 @@ public final class AddressBookAUResultParser extends ResultParser {
@Override
public AddressBookParsedResult parse(Result result) {
String rawText = result.getText();
String rawText = getMassagedText(result);
// MEMORY is mandatory; seems like a decent indicator, as does end-of-record separator CR/LF
if (!rawText.contains("MEMORY") || !rawText.contains("\r\n")) {
return null;

View file

@ -37,7 +37,7 @@ public final class AddressBookDoCoMoResultParser extends AbstractDoCoMoResultPar
@Override
public AddressBookParsedResult parse(Result result) {
String rawText = result.getText();
String rawText = getMassagedText(result);
if (!rawText.startsWith("MECARD:")) {
return null;
}

View file

@ -36,7 +36,7 @@ public final class BizcardResultParser extends AbstractDoCoMoResultParser {
@Override
public AddressBookParsedResult parse(Result result) {
String rawText = result.getText();
String rawText = getMassagedText(result);
if (!rawText.startsWith("BIZCARD:")) {
return null;
}

View file

@ -30,7 +30,7 @@ public final class EmailAddressResultParser extends ResultParser {
@Override
public EmailAddressParsedResult parse(Result result) {
String rawText = result.getText();
String rawText = getMassagedText(result);
String emailAddress;
if (rawText.startsWith("mailto:") || rawText.startsWith("MAILTO:")) {
// If it starts with mailto:, assume it is definitely trying to be an email address

View file

@ -33,7 +33,7 @@ public final class EmailDoCoMoResultParser extends AbstractDoCoMoResultParser {
@Override
public EmailAddressParsedResult parse(Result result) {
String rawText = result.getText();
String rawText = getMassagedText(result);
if (!rawText.startsWith("MATMSG:")) {
return null;
}

View file

@ -48,7 +48,7 @@ public final class ExpandedProductResultParser extends ResultParser {
return null;
}
// Really neither of these should happen:
String rawText = result.getText();
String rawText = getMassagedText(result);
if (rawText == null) {
// ExtendedProductParsedResult NOT created. Input text is NULL
return null;

View file

@ -36,11 +36,7 @@ public final class GeoResultParser extends ResultParser {
@Override
public GeoParsedResult parse(Result result) {
String rawText = result.getText();
if (rawText == null) {
return null;
}
String rawText = getMassagedText(result);
Matcher matcher = GEO_URL_PATTERN.matcher(rawText);
if (!matcher.matches()) {
return null;

View file

@ -35,7 +35,7 @@ public final class ISBNResultParser extends ResultParser {
if (format != BarcodeFormat.EAN_13) {
return null;
}
String rawText = result.getText();
String rawText = getMassagedText(result);
int length = rawText.length();
if (length != 13) {
return null;

View file

@ -35,7 +35,7 @@ public final class ProductResultParser extends ResultParser {
format == BarcodeFormat.EAN_8 || format == BarcodeFormat.EAN_13)) {
return null;
}
String rawText = result.getText();
String rawText = getMassagedText(result);
int length = rawText.length();
for (int x = 0; x < length; x++) {
char c = rawText.charAt(x);

View file

@ -65,6 +65,7 @@ public abstract class ResultParser {
private static final Pattern ALPHANUM = Pattern.compile("[a-zA-Z0-9]*");
private static final Pattern AMPERSAND = Pattern.compile("&");
private static final Pattern EQUALS = Pattern.compile("=");
private static final String BYTE_ORDER_MARK = "\ufeff";
/**
* Attempts to parse the raw {@link Result}'s contents as a particular type
@ -73,6 +74,14 @@ public abstract class ResultParser {
*/
public abstract ParsedResult parse(Result theResult);
protected static String getMassagedText(Result result) {
String text = result.getText();
if (text.startsWith(BYTE_ORDER_MARK)) {
text = text.substring(1);
}
return text;
}
public static ParsedResult parseResult(Result theResult) {
for (ResultParser parser : PARSERS) {
ParsedResult result = parser.parse(theResult);

View file

@ -42,7 +42,7 @@ public final class SMSMMSResultParser extends ResultParser {
@Override
public SMSParsedResult parse(Result result) {
String rawText = result.getText();
String rawText = getMassagedText(result);
if (!(rawText.startsWith("sms:") || rawText.startsWith("SMS:") ||
rawText.startsWith("mms:") || rawText.startsWith("MMS:"))) {
return null;

View file

@ -32,7 +32,7 @@ public final class SMSTOMMSTOResultParser extends ResultParser {
@Override
public SMSParsedResult parse(Result result) {
String rawText = result.getText();
String rawText = getMassagedText(result);
if (!(rawText.startsWith("smsto:") || rawText.startsWith("SMSTO:") ||
rawText.startsWith("mmsto:") || rawText.startsWith("MMSTO:"))) {
return null;

View file

@ -30,7 +30,7 @@ public final class SMTPResultParser extends ResultParser {
@Override
public EmailAddressParsedResult parse(Result result) {
String rawText = result.getText();
String rawText = getMassagedText(result);
if (!(rawText.startsWith("smtp:") || rawText.startsWith("SMTP:"))) {
return null;
}

View file

@ -27,7 +27,7 @@ public final class TelResultParser extends ResultParser {
@Override
public TelParsedResult parse(Result result) {
String rawText = result.getText();
String rawText = getMassagedText(result);
if (!rawText.startsWith("tel:") && !rawText.startsWith("TEL:")) {
return null;
}

View file

@ -41,7 +41,7 @@ public final class URIResultParser extends ResultParser {
@Override
public URIParsedResult parse(Result result) {
String rawText = result.getText();
String rawText = getMassagedText(result);
// We specifically handle the odd "URL" scheme here for simplicity
if (rawText.startsWith("URL:")) {
rawText = rawText.substring(4);

View file

@ -29,7 +29,7 @@ public final class URLTOResultParser extends ResultParser {
@Override
public URIParsedResult parse(Result result) {
String rawText = result.getText();
String rawText = getMassagedText(result);
if (!rawText.startsWith("urlto:") && !rawText.startsWith("URLTO:")) {
return null;
}

View file

@ -48,7 +48,7 @@ public final class VCardResultParser extends ResultParser {
// Although we should insist on the raw text ending with "END:VCARD", there's no reason
// to throw out everything else we parsed just because this was omitted. In fact, Eclair
// is doing just that, and we can't parse its contacts without this leniency.
String rawText = result.getText();
String rawText = getMassagedText(result);
Matcher m = BEGIN_VCARD.matcher(rawText);
if (!m.find() || m.start() != 0) {
return null;

View file

@ -30,10 +30,7 @@ public final class VEventResultParser extends ResultParser {
@Override
public CalendarParsedResult parse(Result result) {
String rawText = result.getText();
if (rawText == null) {
return null;
}
String rawText = getMassagedText(result);
int vEventStart = rawText.indexOf("BEGIN:VEVENT");
if (vEventStart < 0) {
return null;

View file

@ -31,7 +31,7 @@ public final class WifiResultParser extends ResultParser {
@Override
public WifiParsedResult parse(Result result) {
String rawText = result.getText();
String rawText = getMassagedText(result);
if (!rawText.startsWith("WIFI:")) {
return null;
}