- Created Barcode Scanner 3.0 beta 1, for Donut and above only.
[zxing.git] / core / src / com / google / zxing / client / result / VCardResultParser.java
1 /*
2  * Copyright 2008 ZXing authors
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 import com.google.zxing.Result;
20
21 import java.util.Vector;
22
23 /**
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.
26  *
27  * @author Sean Owen
28  */
29 final class VCardResultParser extends ResultParser {
30
31   private VCardResultParser() {
32   }
33
34   public static AddressBookParsedResult parse(Result result) {
35     // Although we should insist on the raw text ending with "END:VCARD", there's no reason
36     // to throw out everything else we parsed just because this was omitted. In fact, Eclair
37     // is doing just that, and we can't parse its contacts without this leniency.
38     String rawText = result.getText();
39     if (rawText == null || !rawText.startsWith("BEGIN:VCARD")) {
40       return null;
41     }
42     String[] names = matchVCardPrefixedField("FN", rawText, true);
43     if (names == null) {
44       // If no display names found, look for regular name fields and format them
45       names = matchVCardPrefixedField("N", rawText, true);
46       formatNames(names);
47     }
48     String[] phoneNumbers = matchVCardPrefixedField("TEL", rawText, true);
49     String[] emails = matchVCardPrefixedField("EMAIL", rawText, true);
50     String note = matchSingleVCardPrefixedField("NOTE", rawText, false);
51     String address = matchSingleVCardPrefixedField("ADR", rawText, true);
52     address = formatAddress(address);
53     String org = matchSingleVCardPrefixedField("ORG", rawText, true);
54     String birthday = matchSingleVCardPrefixedField("BDAY", rawText, true);
55     if (birthday != null && !isStringOfDigits(birthday, 8)) {
56       return null;
57     }
58     String title = matchSingleVCardPrefixedField("TITLE", rawText, true);
59     String url = matchSingleVCardPrefixedField("URL", rawText, true);
60     return new AddressBookParsedResult(names, null, phoneNumbers, emails, note, address, org,
61         birthday, title, url);
62   }
63
64   private static String[] matchVCardPrefixedField(String prefix, String rawText, boolean trim) {
65     Vector matches = null;
66     int i = 0;
67     int max = rawText.length();
68     while (i < max) {
69       i = rawText.indexOf(prefix, i);
70       if (i < 0) {
71         break;
72       }
73       if (i > 0 && rawText.charAt(i - 1) != '\n') {
74         // then this didn't start a new token, we matched in the middle of something
75         i++;
76         continue;
77       }
78       i += prefix.length(); // Skip past this prefix we found to start
79       if (rawText.charAt(i) != ':' && rawText.charAt(i) != ';') {
80         continue;
81       }
82       while (rawText.charAt(i) != ':') { // Skip until a colon
83         i++;
84       }
85       i++; // skip colon
86       int start = i; // Found the start of a match here
87       i = rawText.indexOf((int) '\n', i); // Really, ends in \r\n
88       if (i < 0) {
89         // No terminating end character? uh, done. Set i such that loop terminates and break
90         i = max;
91       } else if (i > start) {
92         // found a match
93         if (matches == null) {
94           matches = new Vector(3); // lazy init
95         }
96         String element = rawText.substring(start, i);
97         if (trim) {
98           element = element.trim();
99         }
100         matches.addElement(element);
101         i++;
102       } else {
103         i++;
104       }
105     }
106     if (matches == null || matches.isEmpty()) {
107       return null;
108     }
109     return toStringArray(matches);
110   }
111
112   static String matchSingleVCardPrefixedField(String prefix, String rawText, boolean trim) {
113     String[] values = matchVCardPrefixedField(prefix, rawText, trim);
114     return values == null ? null : values[0];
115   }
116
117   private static String formatAddress(String address) {
118     if (address == null) {
119       return null;
120     }
121     int length = address.length();
122     StringBuffer newAddress = new StringBuffer(length);
123     for (int j = 0; j < length; j++) {
124       char c = address.charAt(j);
125       if (c == ';') {
126         newAddress.append(' ');
127       } else {
128         newAddress.append(c);
129       }
130     }
131     return newAddress.toString().trim();
132   }
133
134   /**
135    * Formats name fields of the form "Public;John;Q.;Reverend;III" into a form like
136    * "Reverend John Q. Public III".
137    *
138    * @param names name values to format, in place
139    */
140   private static void formatNames(String[] names) {
141     if (names != null) {
142       for (int i = 0; i < names.length; i++) {
143         String name = names[i];
144         String[] components = new String[5];
145         int start = 0;
146         int end;
147         int componentIndex = 0;
148         while ((end = name.indexOf(';', start)) > 0) {
149           components[componentIndex] = name.substring(start, end);
150           componentIndex++;
151           start = end + 1;
152         }
153         components[componentIndex] = name.substring(start);
154         StringBuffer newName = new StringBuffer();
155         maybeAppendComponent(components, 3, newName);
156         maybeAppendComponent(components, 1, newName);
157         maybeAppendComponent(components, 2, newName);
158         maybeAppendComponent(components, 0, newName);
159         maybeAppendComponent(components, 4, newName);
160         names[i] = newName.toString().trim();
161       }
162     }
163   }
164
165   private static void maybeAppendComponent(String[] components, int i, StringBuffer newName) {
166     if (components[i] != null) {
167       newName.append(' ');
168       newName.append(components[i]);
169     }
170   }
171
172 }