2 * Copyright 2007 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.result;
19 import com.google.zxing.Result;
22 * Implements the "MECARD" address book entry format.
24 * Supported keys: N, TEL, EMAIL, NOTE, ADR Unsupported keys: SOUND, TEL-AV, BDAY, URL, NICKNAME
26 * Except for TEL, multiple values for keys are also not supported;
27 * the first one found takes precedence.
29 * @author srowen@google.com (Sean Owen)
31 public final class AddressBookDoCoMoParsedResult extends AbstractDoCoMoParsedResult {
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;
39 private AddressBookDoCoMoParsedResult(String name, String[] phoneNumbers, String email, String note, String address) {
40 super(ParsedReaderResultType.ADDRESSBOOK);
42 this.phoneNumbers = phoneNumbers;
45 this.address = address;
48 public static AddressBookDoCoMoParsedResult parse(Result result) {
49 String rawText = result.getText();
50 if (rawText == null || !rawText.startsWith("MECARD:")) {
53 String[] rawName = matchPrefixedField("N:", rawText);
54 if (rawName == null) {
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);
65 public String getName() {
69 public String[] getPhoneNumbers() {
73 public String getEmail() {
77 public String getNote() {
81 public String getAddress() {
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();
94 private static String parseName(String name) {
95 int comma = name.indexOf((int) ',');
97 // Format may be last,first; switch it around
98 return name.substring(comma + 1) + ' ' + name.substring(0, comma);