Support SMTP URLs
[zxing.git] / core / src / com / google / zxing / client / result / SMTPResultParser.java
1 /*
2  * Copyright 2010 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.result;
18
19 import com.google.zxing.Result;
20
21 /**
22  * <p>Parses an "smtp:" URI result, whose format is not standardized but appears to be like:
23  * <code>smtp(:subject(:body))</code>.</p>
24  *
25  * <p>See http://code.google.com/p/zxing/issues/detail?id=536</p>
26  *
27  * @author Sean Owen
28  */
29 final class SMTPResultParser {
30
31   private SMTPResultParser() {
32   }
33
34   public static EmailAddressParsedResult parse(Result result) {
35     String rawText = result.getText();
36     if (rawText == null) {
37       return null;
38     }
39     if (!(rawText.startsWith("smtp:") || rawText.startsWith("SMTP:"))) {
40       return null;
41     }
42     String emailAddress = rawText.substring(5);
43     String subject = null;
44     String body = null;
45     int colon = emailAddress.indexOf(':');
46     if (colon >= 0) {
47       subject = emailAddress.substring(colon + 1);
48       emailAddress = emailAddress.substring(0, colon);
49       colon = subject.indexOf(':');
50       if (colon >= 0) {
51         body = subject.substring(colon + 1);
52         subject = subject.substring(0, colon);
53       }
54     }
55     String mailtoURI = "mailto:" + emailAddress;
56     return new EmailAddressParsedResult(emailAddress, subject, body, mailtoURI);
57   }
58 }