Implemented Paul's solution to the basic/regular build problem -- a sort of pseudo...
[zxing.git] / core / src / com / google / zxing / client / result / EmailAddressParsedResult.java
1 /*
2  * Copyright 2007 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 import java.util.Hashtable;
22
23 /**
24  * Represents a result that encodes an e-mail address, either as a plain address
25  * like "joe@example.org" or a mailto: URL like "mailto:joe@example.org".
26  *
27  * @author srowen@google.com (Sean Owen)
28  */
29 public final class EmailAddressParsedResult extends AbstractDoCoMoParsedResult {
30
31   private final String emailAddress;
32   private final String subject;
33   private final String body;
34   private final String mailtoURI;
35
36   private EmailAddressParsedResult(String emailAddress, String subject, String body, String mailtoURI) {
37     super(ParsedReaderResultType.EMAIL_ADDRESS);
38     this.emailAddress = emailAddress;
39     this.subject = subject;
40     this.body = body;
41     this.mailtoURI = mailtoURI;
42   }
43
44   public static EmailAddressParsedResult parse(Result result) {
45     String rawText = result.getText();
46     if (rawText == null) {
47       return null;
48     }
49     String emailAddress;
50     if (rawText.startsWith("mailto:")) {
51       // If it starts with mailto:, assume it is definitely trying to be an email address
52       emailAddress = rawText.substring(7);
53       int queryStart = emailAddress.indexOf('?');
54       if (queryStart >= 0) {
55         emailAddress = emailAddress.substring(0, queryStart);
56       }
57       Hashtable nameValues = parseNameValuePairs(rawText);
58       String subject = null;
59       String body = null;
60       if (nameValues != null) {
61         if (emailAddress.length() == 0) {
62           emailAddress = (String) nameValues.get("to");
63         }
64         subject = (String) nameValues.get("subject");
65         body = (String) nameValues.get("body");
66       }
67       return new EmailAddressParsedResult(emailAddress, subject, body, rawText);
68     } else {
69       if (!EmailDoCoMoParsedResult.isBasicallyValidEmailAddress(rawText)) {
70         return null;
71       }
72       emailAddress = rawText;
73       return new EmailAddressParsedResult(emailAddress, null, null, "mailto:" + emailAddress);
74     }
75   }
76
77   public String getEmailAddress() {
78     return emailAddress;
79   }
80
81   public String getSubject() {
82     return subject;
83   }
84
85   public String getBody() {
86     return body;
87   }
88
89   public String getMailtoURI() {
90     return mailtoURI;
91   }
92
93   public String getDisplayResult() {
94     return emailAddress;
95   }
96
97 }