Implemented Paul's solution to the basic/regular build problem -- a sort of pseudo...
[zxing.git] / core / src / com / google / zxing / client / result / EmailDoCoMoParsedResult.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 /**
22  * Implements the "MATMSG" email message entry format.
23  *
24  * Supported keys: TO, SUB, BODY
25  *
26  * @author srowen@google.com (Sean Owen)
27  */
28 public final class EmailDoCoMoParsedResult extends AbstractDoCoMoParsedResult {
29
30   private final String to;
31   private final String subject;
32   private final String body;
33
34   private EmailDoCoMoParsedResult(String to, String subject, String body) {
35     super(ParsedReaderResultType.EMAIL);
36     this.to = to;
37     this.subject = subject;
38     this.body = body;
39   }
40
41   public static EmailDoCoMoParsedResult parse(Result result) {
42     String rawText = result.getText();
43     if (rawText == null || !rawText.startsWith("MATMSG:")) {
44       return null;
45     }
46     String[] rawTo = matchPrefixedField("TO:", rawText);
47     if (rawTo == null) {
48       return null;
49     }
50     String to = rawTo[0];
51     if (!isBasicallyValidEmailAddress(to)) {
52       return null;
53     }
54     String subject = matchSinglePrefixedField("SUB:", rawText);
55     String body = matchSinglePrefixedField("BODY:", rawText);
56     return new EmailDoCoMoParsedResult(to, subject, body);
57   }
58
59   public String getTo() {
60     return to;
61   }
62
63   public String getSubject() {
64     return subject;
65   }
66
67   public String getBody() {
68     return body;
69   }
70
71   public String getMailtoURI() {
72     StringBuffer result = new StringBuffer(to);
73     boolean hasParams = false;
74     if (subject != null) {
75       result.append(hasParams ? '&' : '?');
76       hasParams = true;
77       result.append("subject=");
78       result.append(subject);
79       // TODO we need to escape this?
80     }
81     if (body != null) {
82       result.append(hasParams ? '&' : '?');
83       hasParams = true;
84       result.append("body=");
85       result.append(body);
86       // TODO we need to escape this?
87     }
88     return result.toString();
89   }
90
91   public String getDisplayResult() {
92     StringBuffer result = new StringBuffer(to);
93     maybeAppend(subject, result);
94     maybeAppend(body, result);
95     return result.toString();
96   }
97
98   /**
99    * This implements only the most basic checking for an email address's validity -- that it contains
100    * an '@' and a '.' somewhere after that, and that it contains no space.
101    * We want to generally be lenient here since this class is only intended to encapsulate what's
102    * in a barcode, not "judge" it.
103    */
104   static boolean isBasicallyValidEmailAddress(String email) {
105     if (email == null) {
106       return false;
107     }
108     int atIndex = email.indexOf('@');
109     return atIndex >= 0 && email.indexOf('.') > atIndex && email.indexOf(' ') < 0;
110   }
111
112 }