Various improvements to handling and detection of URLs in codes
[zxing.git] / core-ext / 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   public AddressBookDoCoMoResult(String rawText) {
38     super(ParsedReaderResultType.ADDRESSBOOK);
39     if (!rawText.startsWith("MECARD:")) {
40       throw new IllegalArgumentException("Does not begin with MECARD");
41     }
42     name = parseName(matchRequiredPrefixedField("N:", rawText)[0]);
43     phoneNumbers = matchPrefixedField("TEL:", rawText);
44     email = matchSinglePrefixedField("EMAIL:", rawText);
45     note = matchSinglePrefixedField("NOTE:", rawText);
46     address = matchSinglePrefixedField("ADR:", rawText);
47   }
48
49   public String getName() {
50     return name;
51   }
52
53   public String[] getPhoneNumbers() {
54     return phoneNumbers;
55   }
56
57   public String getEmail() {
58     return email;
59   }
60
61   public String getNote() {
62     return note;
63   }
64
65   public String getAddress() {
66     return address;
67   }
68
69   @Override
70   public String getDisplayResult() {
71     StringBuilder result = new StringBuilder(name);
72     maybeAppend(email, result);
73     maybeAppend(address, result);
74     for (int i = 0; i < phoneNumbers.length; i++) {
75       maybeAppend(phoneNumbers[i], result);
76     }
77     maybeAppend(note, result);
78     return result.toString();
79   }
80
81   private static String parseName(String name) {
82     int comma = name.indexOf((int) ',');
83     if (comma >= 0) {
84       // Format may be last,first; switch it around
85       return name.substring(comma + 1) + ' ' + name.substring(0, comma);
86     }
87     return name;
88   }
89
90 }