Remove my old email address from files. Might as well save spammers the trouble.
[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     String rawText = result.getText();
36     if (rawText == null || !rawText.startsWith("BEGIN:VCARD") || !rawText.endsWith("END:VCARD")) {
37       return null;
38     }
39     String[] names = matchVCardPrefixedField("FN", rawText, true);
40     if (names == null) {
41       // If no display names found, look for regular name fields and format them
42       names = matchVCardPrefixedField("N", rawText, true);
43       formatNames(names);
44     }
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)) {
53       return null;
54     }
55     String title = matchSingleVCardPrefixedField("TITLE", rawText, true);
56     String url = matchSingleVCardPrefixedField("URL", rawText, true);
57     return new AddressBookParsedResult(names, null, phoneNumbers, emails, note, address, org,
58         birthday, title, url);
59   }
60
61   private static String[] matchVCardPrefixedField(String prefix, String rawText, boolean trim) {
62     Vector matches = null;
63     int i = 0;
64     final int max = rawText.length();
65     while (i < max) {
66       i = rawText.indexOf(prefix, i);
67       if (i < 0) {
68         break;
69       }
70       if (i > 0 && rawText.charAt(i - 1) != '\n') {
71         // then this didn't start a new token, we matched in the middle of something
72         i++;
73         continue;
74       }
75       i += prefix.length(); // Skip past this prefix we found to start
76       if (rawText.charAt(i) != ':' && rawText.charAt(i) != ';') {
77         continue;
78       }
79       while (rawText.charAt(i) != ':') { // Skip until a colon
80         i++;
81       }
82       i++; // skip colon
83       int start = i; // Found the start of a match here
84       i = rawText.indexOf((int) '\n', i); // Really, ends in \r\n
85       if (i < 0) {
86         // No terminating end character? uh, done. Set i such that loop terminates and break
87         i = max;
88       } else if (i > start) {
89         // found a match
90         if (matches == null) {
91           matches = new Vector(3); // lazy init
92         }
93         String element = rawText.substring(start, i);
94         if (trim) {
95           element = element.trim();
96         }
97         matches.addElement(element);
98         i++;
99       } else {
100         i++;
101       }
102     }
103     if (matches == null || matches.isEmpty()) {
104       return null;
105     }
106     return toStringArray(matches);
107   }
108
109   static String matchSingleVCardPrefixedField(String prefix, String rawText, boolean trim) {
110     String[] values = matchVCardPrefixedField(prefix, rawText, trim);
111     return values == null ? null : values[0];
112   }
113
114   private static String formatAddress(String address) {
115     if (address == null) {
116       return null;
117     }
118     int length = address.length();
119     StringBuffer newAddress = new StringBuffer(length);
120     for (int j = 0; j < length; j++) {
121       char c = address.charAt(j);
122       if (c == ';') {
123         newAddress.append(' ');
124       } else {
125         newAddress.append(c);
126       }
127     }
128     return newAddress.toString().trim();
129   }
130
131   /**
132    * Formats name fields of the form "Public;John;Q.;Reverend;III" into a form like
133    * "Reverend John Q. Public III".
134    *
135    * @param names name values to format, in place
136    */
137   private static void formatNames(String[] names) {
138     if (names != null) {
139       for (int i = 0; i < names.length; i++) {
140         String name = names[i];
141         String[] components = new String[5];
142         int start = 0;
143         int end;
144         int componentIndex = 0;
145         while ((end = name.indexOf(';', start)) > 0) {
146           components[componentIndex] = name.substring(start, end);
147           componentIndex++;
148           start = end + 1;
149         }
150         components[componentIndex] = name.substring(start);
151         StringBuffer newName = new StringBuffer();
152         maybeAppendComponent(components, 3, newName);
153         maybeAppendComponent(components, 1, newName);
154         maybeAppendComponent(components, 2, newName);
155         maybeAppendComponent(components, 0, newName);
156         maybeAppendComponent(components, 4, newName);
157         names[i] = newName.toString().trim();
158       }
159     }
160   }
161
162   private static void maybeAppendComponent(String[] components, int i, StringBuffer newName) {
163     if (components[i] != null) {
164       newName.append(' ');
165       newName.append(components[i]);
166     }
167   }
168
169 }