2 * Copyright 2008 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;
21 import java.util.Vector;
24 * Parses contact information formatted according to the VCard (2.1) format. This is not a complete
25 * implementation but should parse information as commonly encoded in 2D barcodes.
27 * @author srowen@google.com (Sean Owen)
29 final class VCardResultParser extends ResultParser {
31 private VCardResultParser() {
34 public static AddressBookParsedResult parse(Result result) {
35 String rawText = result.getText();
36 if (rawText == null || !rawText.startsWith("BEGIN:VCARD") || !rawText.endsWith("END:VCARD")) {
39 String[] names = matchVCardPrefixedField("FN", rawText, true);
41 // If no display names found, look for regular name fields and format them
42 names = matchVCardPrefixedField("N", rawText, true);
45 String[] phoneNumbers = matchVCardPrefixedField("TEL", rawText, true);
46 String[] emails = matchVCardPrefixedField("EMAIL", rawText, true);
47 String note = matchSingleVCardPrefixedField("NOTE", rawText, false);
48 String address = matchSingleVCardPrefixedField("ADR", rawText, true);
49 address = formatAddress(address);
50 String org = matchSingleVCardPrefixedField("ORG", rawText, true);
51 String birthday = matchSingleVCardPrefixedField("BDAY", rawText, true);
52 if (birthday != null && !isStringOfDigits(birthday, 8)) {
55 String title = matchSingleVCardPrefixedField("TITLE", rawText, true);
56 return new AddressBookParsedResult(names, phoneNumbers, emails, note, address, org, birthday, title);
59 private static String[] matchVCardPrefixedField(String prefix, String rawText, boolean trim) {
60 Vector matches = null;
62 final int max = rawText.length();
64 i = rawText.indexOf(prefix, i);
68 if (i > 0 && rawText.charAt(i - 1) != '\n') {
69 // then this didn't start a new token, we matched in the middle of something
73 i += prefix.length(); // Skip past this prefix we found to start
74 if (rawText.charAt(i) != ':' && rawText.charAt(i) != ';') {
77 while (rawText.charAt(i) != ':') { // Skip until a colon
81 int start = i; // Found the start of a match here
82 i = rawText.indexOf((int) '\n', i); // Really, ends in \r\n
84 // No terminating end character? uh, done. Set i such that loop terminates and break
86 } else if (i > start) {
88 if (matches == null) {
89 matches = new Vector(3); // lazy init
91 String element = rawText.substring(start, i);
93 element = element.trim();
95 matches.addElement(element);
101 if (matches == null || matches.isEmpty()) {
104 return toStringArray(matches);
107 static String matchSingleVCardPrefixedField(String prefix, String rawText, boolean trim) {
108 String[] values = matchVCardPrefixedField(prefix, rawText, trim);
109 return values == null ? null : values[0];
112 private static String formatAddress(String address) {
113 if (address == null) {
116 int length = address.length();
117 StringBuffer newAddress = new StringBuffer(length);
118 for (int j = 0; j < length; j++) {
119 char c = address.charAt(j);
121 newAddress.append(' ');
123 newAddress.append(c);
126 return newAddress.toString().trim();
130 * Formats name fields of the form "Public;John;Q.;Reverend;III" into a form like
131 * "Reverend John Q. Public III".
133 * @param names name values to format, in place
135 private static void formatNames(String[] names) {
137 for (int i = 0; i < names.length; i++) {
138 String name = names[i];
139 String[] components = new String[5];
142 int componentIndex = 0;
143 while ((end = name.indexOf(';', start)) > 0) {
144 components[componentIndex] = name.substring(start, end);
148 components[componentIndex] = name.substring(start);
149 StringBuffer newName = new StringBuffer();
150 maybeAppendComponent(components, 3, newName);
151 maybeAppendComponent(components, 1, newName);
152 maybeAppendComponent(components, 2, newName);
153 maybeAppendComponent(components, 0, newName);
154 maybeAppendComponent(components, 4, newName);
155 names[i] = newName.toString().trim();
160 private static void maybeAppendComponent(String[] components, int i, StringBuffer newName) {
161 if (components[i] != null) {
163 newName.append(components[i]);