Prevented ISBN parsing from happening twice.
[zxing.git] / core / src / com / google / zxing / client / result / EmailAddressResultParser.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 final class EmailAddressResultParser extends ResultParser {
30
31   public static EmailAddressParsedResult parse(Result result) {
32     String rawText = result.getText();
33     if (rawText == null) {
34       return null;
35     }
36     String emailAddress;
37     if (rawText.startsWith("mailto:")) {
38       // If it starts with mailto:, assume it is definitely trying to be an email address
39       emailAddress = rawText.substring(7);
40       int queryStart = emailAddress.indexOf('?');
41       if (queryStart >= 0) {
42         emailAddress = emailAddress.substring(0, queryStart);
43       }
44       Hashtable nameValues = parseNameValuePairs(rawText);
45       String subject = null;
46       String body = null;
47       if (nameValues != null) {
48         if (emailAddress.length() == 0) {
49           emailAddress = (String) nameValues.get("to");
50         }
51         subject = (String) nameValues.get("subject");
52         body = (String) nameValues.get("body");
53       }
54       return new EmailAddressParsedResult(emailAddress, subject, body, rawText);
55     } else {
56       if (!EmailDoCoMoResultParser.isBasicallyValidEmailAddress(rawText)) {
57         return null;
58       }
59       emailAddress = rawText;
60       return new EmailAddressParsedResult(emailAddress, null, null, "mailto:" + emailAddress);
61     }
62   }
63
64 }