Issue 294, add support for several address lines in parsed results
[zxing.git] / core / src / com / google / zxing / client / result / AddressBookDoCoMoResultParser.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 "MECARD" address book entry format.
23  *
24  * Supported keys: N, SOUND, TEL, EMAIL, NOTE, ADR, BDAY, URL, plus ORG
25  * Unsupported keys: TEL-AV, NICKNAME
26  *
27  * Except for TEL, multiple values for keys are also not supported;
28  * the first one found takes precedence.
29  *
30  * Our understanding of the MECARD format is based on this document:
31  *
32  * http://www.mobicode.org.tw/files/OMIA%20Mobile%20Bar%20Code%20Standard%20v3.2.1.doc 
33  *
34  * @author Sean Owen
35  */
36 final class AddressBookDoCoMoResultParser extends AbstractDoCoMoResultParser {
37
38   public static AddressBookParsedResult parse(Result result) {
39     String rawText = result.getText();
40     if (rawText == null || !rawText.startsWith("MECARD:")) {
41       return null;
42     }
43     String[] rawName = matchDoCoMoPrefixedField("N:", rawText, true);
44     if (rawName == null) {
45       return null;
46     }
47     String name = parseName(rawName[0]);
48     String pronunciation = matchSingleDoCoMoPrefixedField("SOUND:", rawText, true);
49     String[] phoneNumbers = matchDoCoMoPrefixedField("TEL:", rawText, true);
50     String[] emails = matchDoCoMoPrefixedField("EMAIL:", rawText, true);
51     String note = matchSingleDoCoMoPrefixedField("NOTE:", rawText, false);
52     String[] addresses = matchDoCoMoPrefixedField("ADR:", rawText, true);
53     String birthday = matchSingleDoCoMoPrefixedField("BDAY:", rawText, true);
54     if (birthday != null && !isStringOfDigits(birthday, 8)) {
55       // No reason to throw out the whole card because the birthday is formatted wrong.
56       birthday = null;
57     }
58     String url = matchSingleDoCoMoPrefixedField("URL:", rawText, true);
59
60     // Although ORG may not be strictly legal in MECARD, it does exist in VCARD and we might as well
61     // honor it when found in the wild.
62     String org = matchSingleDoCoMoPrefixedField("ORG:", rawText, true);
63
64     return new AddressBookParsedResult(maybeWrap(name),
65                                        pronunciation,
66                                        phoneNumbers,
67                                        emails,
68                                        note,
69                                        addresses,
70                                        org,
71                                        birthday,
72                                        null,
73                                        url);
74   }
75
76   private static String parseName(String name) {
77     int comma = name.indexOf((int) ',');
78     if (comma >= 0) {
79       // Format may be last,first; switch it around
80       return name.substring(comma + 1) + ' ' + name.substring(0, comma);
81     }
82     return name;
83   }
84
85 }