Support SMTP URLs

git-svn-id: https://zxing.googlecode.com/svn/trunk@1609 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2010-09-30 15:44:39 +00:00
parent 5abef2f3aa
commit c01ee70804
3 changed files with 67 additions and 0 deletions

View file

@ -55,6 +55,8 @@ public abstract class ResultParser {
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) {

View file

@ -0,0 +1,58 @@
/*
* Copyright 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.client.result;
import com.google.zxing.Result;
/**
* <p>Parses an "smtp:" URI result, whose format is not standardized but appears to be like:
* <code>smtp(:subject(:body))</code>.</p>
*
* <p>See http://code.google.com/p/zxing/issues/detail?id=536</p>
*
* @author Sean Owen
*/
final class SMTPResultParser {
private SMTPResultParser() {
}
public static EmailAddressParsedResult parse(Result result) {
String rawText = result.getText();
if (rawText == null) {
return null;
}
if (!(rawText.startsWith("smtp:") || rawText.startsWith("SMTP:"))) {
return null;
}
String emailAddress = rawText.substring(5);
String subject = null;
String body = null;
int colon = emailAddress.indexOf(':');
if (colon >= 0) {
subject = emailAddress.substring(colon + 1);
emailAddress = emailAddress.substring(0, colon);
colon = subject.indexOf(':');
if (colon >= 0) {
body = subject.substring(colon + 1);
subject = subject.substring(0, colon);
}
}
String mailtoURI = "mailto:" + emailAddress;
return new EmailAddressParsedResult(emailAddress, subject, body, mailtoURI);
}
}

View file

@ -39,6 +39,13 @@ public final class EmailAddressParsedResultTestCase extends TestCase {
"Stuff", "This is some text");
}
public void testSMTP() {
doTest("smtp:srowen@example.org", "srowen@example.org", null, null);
doTest("SMTP:srowen@example.org", "srowen@example.org", null, null);
doTest("smtp:srowen@example.org:foo", "srowen@example.org", "foo", null);
doTest("smtp:srowen@example.org:foo:bar", "srowen@example.org", "foo", "bar");
}
private static void doTest(String contents, String email, String subject, String body) {
Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
ParsedResult result = ResultParser.parseResult(fakeResult);