Add BIZCARD support and a little refactoring
[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, TEL, EMAIL, NOTE, ADR Unsupported keys: SOUND, TEL-AV, BDAY, URL, NICKNAME
25  *
26  * Except for TEL, multiple values for keys are also not supported;
27  * the first one found takes precedence.
28  *
29  * @author srowen@google.com (Sean Owen)
30  */
31 public final class AddressBookDoCoMoResultParser extends AbstractDoCoMoResultParser {
32
33   public static AddressBookParsedResult parse(Result result) {
34     String rawText = result.getText();
35     if (rawText == null || !rawText.startsWith("MECARD:")) {
36       return null;
37     }
38     String[] rawName = matchPrefixedField("N:", rawText);
39     if (rawName == null) {
40       return null;
41     }
42     String name = parseName(rawName[0]);
43     String[] phoneNumbers = matchPrefixedField("TEL:", rawText);
44     String email = matchSinglePrefixedField("EMAIL:", rawText);
45     String note = matchSinglePrefixedField("NOTE:", rawText);
46     String address = matchSinglePrefixedField("ADR:", rawText);
47     String birthday = matchSinglePrefixedField("BDAY:", rawText);
48     if (birthday != null && !isStringOfDigits(birthday, 8)) {
49       return null;
50     }
51     return new AddressBookParsedResult(maybeWrap(name),
52                                        phoneNumbers,
53                                        maybeWrap(email),
54                                        note,
55                                        address,
56                                        null,
57                                        birthday,
58                                        null);
59   }
60
61   private static String parseName(String name) {
62     int comma = name.indexOf((int) ',');
63     if (comma >= 0) {
64       // Format may be last,first; switch it around
65       return name.substring(comma + 1) + ' ' + name.substring(0, comma);
66     }
67     return name;
68   }
69
70 }