Preserve query in geo URI
[zxing.git] / core / src / com / google / zxing / client / result / EmailDoCoMoResultParser.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 Sean Owen
27  */
28 final class EmailDoCoMoResultParser extends AbstractDoCoMoResultParser {
29
30   private static final char[] ATEXT_SYMBOLS =
31       {'@','.','!','#','$','%','&','\'','*','+','-','/','=','?','^','_','`','{','|','}','~'};
32
33   public static EmailAddressParsedResult parse(Result result) {
34     String rawText = result.getText();
35     if (rawText == null || !rawText.startsWith("MATMSG:")) {
36       return null;
37     }
38     String[] rawTo = matchDoCoMoPrefixedField("TO:", rawText, true);
39     if (rawTo == null) {
40       return null;
41     }
42     String to = rawTo[0];
43     if (!isBasicallyValidEmailAddress(to)) {
44       return null;
45     }
46     String subject = matchSingleDoCoMoPrefixedField("SUB:", rawText, false);
47     String body = matchSingleDoCoMoPrefixedField("BODY:", rawText, false);
48     return new EmailAddressParsedResult(to, subject, body, "mailto:" + to);
49   }
50
51   /**
52    * This implements only the most basic checking for an email address's validity -- that it contains
53    * an '@' contains no characters disallowed by RFC 2822. This is an overly lenient definition of
54    * validity. We want to generally be lenient here since this class is only intended to encapsulate what's
55    * in a barcode, not "judge" it.
56    */
57   static boolean isBasicallyValidEmailAddress(String email) {
58     if (email == null) {
59       return false;
60     }
61     boolean atFound = false;
62     for (int i = 0; i < email.length(); i++) {
63       char c = email.charAt(i);
64       if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9') &&
65           !isAtextSymbol(c)) {
66         return false;
67       }
68       if (c == '@') {
69         if (atFound) {
70           return false;
71         }
72         atFound = true;
73       }
74     }
75     return atFound;
76   }
77
78   private static boolean isAtextSymbol(char c) {
79     for (int i = 0; i < ATEXT_SYMBOLS.length; i++) {
80       if (c == ATEXT_SYMBOLS[i]) {
81         return true;
82       }
83     }
84     return false;
85   }
86
87 }