Support SMTP URLs
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Thu, 30 Sep 2010 15:44:39 +0000 (15:44 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Thu, 30 Sep 2010 15:44:39 +0000 (15:44 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@1609 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/client/result/ResultParser.java
core/src/com/google/zxing/client/result/SMTPResultParser.java [new file with mode: 0644]
core/test/src/com/google/zxing/client/result/EmailAddressParsedResultTestCase.java

index df30865..bdcc4d0 100644 (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) {
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 (file)
index 0000000..72576de
--- /dev/null
@@ -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);
+  }
+}
index c3fc5fd..5f5d820 100644 (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);