Implemented Paul's solution to the basic/regular build problem -- a sort of pseudo...
[zxing.git] / core / src / com / google / zxing / client / result / AddressBookDoCoMoParsedResult.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 AddressBookDoCoMoParsedResult extends AbstractDoCoMoParsedResult {
32
33   private final String name;
34   private final String[] phoneNumbers;
35   private final String email;
36   private final String note;
37   private final String address;
38
39   private AddressBookDoCoMoParsedResult(String name, String[] phoneNumbers, String email, String note, String address) {
40     super(ParsedReaderResultType.ADDRESSBOOK);
41     this.name = name;
42     this.phoneNumbers = phoneNumbers;
43     this.email = email;
44     this.note = note;
45     this.address = address;
46   }
47
48   public static AddressBookDoCoMoParsedResult parse(Result result) {
49     String rawText = result.getText();
50     if (rawText == null || !rawText.startsWith("MECARD:")) {
51       return null;
52     }
53     String[] rawName = matchPrefixedField("N:", rawText);
54     if (rawName == null) {
55       return null;
56     }
57     String name = parseName(rawName[0]);
58     String[] phoneNumbers = matchPrefixedField("TEL:", rawText);
59     String email = matchSinglePrefixedField("EMAIL:", rawText);
60     String note = matchSinglePrefixedField("NOTE:", rawText);
61     String address = matchSinglePrefixedField("ADR:", rawText);
62     return new AddressBookDoCoMoParsedResult(name, phoneNumbers, email, note, address);
63   }
64
65   public String getName() {
66     return name;
67   }
68
69   public String[] getPhoneNumbers() {
70     return phoneNumbers;
71   }
72
73   public String getEmail() {
74     return email;
75   }
76
77   public String getNote() {
78     return note;
79   }
80
81   public String getAddress() {
82     return address;
83   }
84
85   public String getDisplayResult() {
86     StringBuffer result = new StringBuffer(name);
87     maybeAppend(email, result);
88     maybeAppend(address, result);
89     maybeAppend(phoneNumbers, result);
90     maybeAppend(note, result);
91     return result.toString();
92   }
93
94   private static String parseName(String name) {
95     int comma = name.indexOf((int) ',');
96     if (comma >= 0) {
97       // Format may be last,first; switch it around
98       return name.substring(comma + 1) + ' ' + name.substring(0, comma);
99     }
100     return name;
101   }
102
103 }