From 10276c1fd6c1871565ad6d6c87f0249f1c0b7e5c Mon Sep 17 00:00:00 2001 From: srowen Date: Thu, 30 Sep 2010 15:44:39 +0000 Subject: [PATCH] Support SMTP URLs git-svn-id: http://zxing.googlecode.com/svn/trunk@1609 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../zxing/client/result/ResultParser.java | 2 + .../zxing/client/result/SMTPResultParser.java | 58 +++++++++++++++++++ .../EmailAddressParsedResultTestCase.java | 7 +++ 3 files changed, 67 insertions(+) create mode 100644 core/src/com/google/zxing/client/result/SMTPResultParser.java diff --git a/core/src/com/google/zxing/client/result/ResultParser.java b/core/src/com/google/zxing/client/result/ResultParser.java index df308652..bdcc4d09 100644 --- a/core/src/com/google/zxing/client/result/ResultParser.java +++ b/core/src/com/google/zxing/client/result/ResultParser.java @@ -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) { diff --git a/core/src/com/google/zxing/client/result/SMTPResultParser.java b/core/src/com/google/zxing/client/result/SMTPResultParser.java new file mode 100644 index 00000000..72576de6 --- /dev/null +++ b/core/src/com/google/zxing/client/result/SMTPResultParser.java @@ -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; + +/** + *

Parses an "smtp:" URI result, whose format is not standardized but appears to be like: + * smtp(:subject(:body)).

+ * + *

See http://code.google.com/p/zxing/issues/detail?id=536

+ * + * @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); + } +} diff --git a/core/test/src/com/google/zxing/client/result/EmailAddressParsedResultTestCase.java b/core/test/src/com/google/zxing/client/result/EmailAddressParsedResultTestCase.java index c3fc5fdf..5f5d8202 100644 --- a/core/test/src/com/google/zxing/client/result/EmailAddressParsedResultTestCase.java +++ b/core/test/src/com/google/zxing/client/result/EmailAddressParsedResultTestCase.java @@ -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); -- 2.20.1