Refactorings to allow raw bytes to be passed back with reader result, where applicable
[zxing.git] / core / src / com / google / zxing / client / result / AddressBookDoCoMoResult.java
1 /*
2  * Copyright 2007 Google Inc.
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 /**
20  * Implements the "MECARD" address book entry format.
21  *
22  * Supported keys: N, TEL, EMAIL, NOTE, ADR Unsupported keys: SOUND, TEL-AV, BDAY, URL, NICKNAME
23  *
24  * Except for TEL, multiple values for keys are also not supported;
25  * the first one found takes precedence.
26  *
27  * @author srowen@google.com (Sean Owen)
28  */
29 public final class AddressBookDoCoMoResult extends AbstractDoCoMoResult {
30
31   private final String name;
32   private final String[] phoneNumbers;
33   private final String email;
34   private final String note;
35   private final String address;
36
37   private AddressBookDoCoMoResult(String name, String[] phoneNumbers, String email, String note, String address) {
38     super(ParsedReaderResultType.ADDRESSBOOK);
39     this.name = name;
40     this.phoneNumbers = phoneNumbers;
41     this.email = email;
42     this.note = note;
43     this.address = address;
44   }
45
46   public static AddressBookDoCoMoResult parse(String rawText) {
47     if (!rawText.startsWith("MECARD:")) {
48       return null;
49     }
50     String[] rawName = matchPrefixedField("N:", rawText);
51     if (rawName == null) {
52       return null;
53     }
54     String name = parseName(rawName[0]);
55     String[] phoneNumbers = matchPrefixedField("TEL:", rawText);
56     String email = matchSinglePrefixedField("EMAIL:", rawText);
57     String note = matchSinglePrefixedField("NOTE:", rawText);
58     String address = matchSinglePrefixedField("ADR:", rawText);
59     return new AddressBookDoCoMoResult(name, phoneNumbers, email, note, address);
60   }
61
62   public String getName() {
63     return name;
64   }
65
66   public String[] getPhoneNumbers() {
67     return phoneNumbers;
68   }
69
70   public String getEmail() {
71     return email;
72   }
73
74   public String getNote() {
75     return note;
76   }
77
78   public String getAddress() {
79     return address;
80   }
81
82   public String getDisplayResult() {
83     StringBuffer result = new StringBuffer(name);
84     maybeAppend(email, result);
85     maybeAppend(address, result);
86     maybeAppend(phoneNumbers, result);
87     maybeAppend(note, result);
88     return result.toString();
89   }
90
91   private static String parseName(String name) {
92     int comma = name.indexOf((int) ',');
93     if (comma >= 0) {
94       // Format may be last,first; switch it around
95       return name.substring(comma + 1) + ' ' + name.substring(0, comma);
96     }
97     return name;
98   }
99
100 }