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, SOUND, TEL, EMAIL, NOTE, ADR, BDAY, URL, plus ORG
25 * Unsupported keys: TEL-AV, NICKNAME
27 * Except for TEL, multiple values for keys are also not supported;
28 * the first one found takes precedence.
30 * Our understanding of the MECARD format is based on this document:
32 * http://www.mobicode.org.tw/files/OMIA%20Mobile%20Bar%20Code%20Standard%20v3.2.1.doc
36 final class AddressBookDoCoMoResultParser extends AbstractDoCoMoResultParser {
38 public static AddressBookParsedResult parse(Result result) {
39 String rawText = result.getText();
40 if (rawText == null || !rawText.startsWith("MECARD:")) {
43 String[] rawName = matchDoCoMoPrefixedField("N:", rawText, true);
44 if (rawName == null) {
47 String name = parseName(rawName[0]);
48 String pronunciation = matchSingleDoCoMoPrefixedField("SOUND:", rawText, true);
49 String[] phoneNumbers = matchDoCoMoPrefixedField("TEL:", rawText, true);
50 String[] emails = matchDoCoMoPrefixedField("EMAIL:", rawText, true);
51 String note = matchSingleDoCoMoPrefixedField("NOTE:", rawText, false);
52 String address = matchSingleDoCoMoPrefixedField("ADR:", rawText, true);
53 String birthday = matchSingleDoCoMoPrefixedField("BDAY:", rawText, true);
54 if (birthday != null && !isStringOfDigits(birthday, 8)) {
55 // No reason to throw out the whole card because the birthday is formatted wrong.
58 String url = matchSingleDoCoMoPrefixedField("URL:", rawText, true);
60 // Although ORG may not be strictly legal in MECARD, it does exist in VCARD and we might as well
61 // honor it when found in the wild.
62 String org = matchSingleDoCoMoPrefixedField("ORG:", rawText, true);
64 return new AddressBookParsedResult(maybeWrap(name),
76 private static String parseName(String name) {
77 int comma = name.indexOf((int) ',');
79 // Format may be last,first; switch it around
80 return name.substring(comma + 1) + ' ' + name.substring(0, comma);