2 * Copyright 2007 Google Inc.
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;
20 * Implements the "MECARD" address book entry format.
22 * Supported keys: N, TEL, EMAIL, NOTE, ADR Unsupported keys: SOUND, TEL-AV, BDAY, URL, NICKNAME
24 * Except for TEL, multiple values for keys are also not supported;
25 * the first one found takes precedence.
27 * @author srowen@google.com (Sean Owen)
29 public final class AddressBookDoCoMoResult extends AbstractDoCoMoResult {
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;
37 public AddressBookDoCoMoResult(String rawText) {
38 super(ParsedReaderResultType.ADDRESSBOOK);
39 if (!rawText.startsWith("MECARD:")) {
40 throw new IllegalArgumentException("Does not begin with MECARD");
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);
49 public String getName() {
53 public String[] getPhoneNumbers() {
57 public String getEmail() {
61 public String getNote() {
65 public String getAddress() {
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);
77 maybeAppend(note, result);
78 return result.toString();
81 private static String parseName(String name) {
82 int comma = name.indexOf((int) ',');
84 // Format may be last,first; switch it around
85 return name.substring(comma + 1) + ' ' + name.substring(0, comma);