Big refactoring of ParsedResult: now split into ResultParser and ParsedResult classes...
[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     return new AddressBookParsedResult(new String[] {name},
48                                        phoneNumbers,
49                                        new String[] {email},
50                                        note,
51                                        address,
52                                        null,
53                                        null,
54                                        null);
55   }
56
57   private static String parseName(String name) {
58     int comma = name.indexOf((int) ',');
59     if (comma >= 0) {
60       // Format may be last,first; switch it around
61       return name.substring(comma + 1) + ' ' + name.substring(0, comma);
62     }
63     return name;
64   }
65
66 }