Add iCal support, plus many small changes suggested by code inspection -- mostly...
[zxing.git] / core / src / com / google / zxing / client / result / BizcardResultParser.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  * Implements the "BIZCARD" address book entry format, though this has been
25  * largely reverse-engineered from examples observed in the wild -- still
26  * looking for a definitive reference.
27  *
28  * @author srowen@google.com (Sean Owen)
29  */
30 final class BizcardResultParser extends AbstractDoCoMoResultParser {
31
32   // Yes, we extend AbstractDoCoMoResultParser since the format is very much
33   // like the DoCoMo MECARD format, but this is not technically one of 
34   // DoCoMo's proposed formats
35
36   public static AddressBookParsedResult parse(Result result) {
37     String rawText = result.getText();
38     if (rawText == null || !rawText.startsWith("BIZCARD:")) {
39       return null;
40     }
41     String firstName = matchSingleDoCoMoPrefixedField("N:", rawText);
42     String lastName = matchSingleDoCoMoPrefixedField("X:", rawText);
43     String fullName = buildName(firstName, lastName);
44     String title = matchSingleDoCoMoPrefixedField("T:", rawText);
45     String org = matchSingleDoCoMoPrefixedField("C:", rawText);
46     String address = matchSingleDoCoMoPrefixedField("A:", rawText);
47     String phoneNumber1 = matchSingleDoCoMoPrefixedField("B:", rawText);
48     String phoneNumber2 = matchSingleDoCoMoPrefixedField("M:", rawText);
49     String phoneNumber3 = matchSingleDoCoMoPrefixedField("F:", rawText);
50     String email = matchSingleDoCoMoPrefixedField("E:", rawText);
51
52     return new AddressBookParsedResult(maybeWrap(fullName),
53                                        buildPhoneNumbers(phoneNumber1, phoneNumber2, phoneNumber3),
54                                        maybeWrap(email),
55                                        null,
56                                        address,
57                                        org,
58                                        null,
59                                        title);
60   }
61
62   private static String[] buildPhoneNumbers(String number1, String number2, String number3) {
63     Vector numbers = new Vector(3);
64     if (number1 != null) {
65       numbers.addElement(number1);
66     }
67     if (number2 != null) {
68       numbers.addElement(number2);
69     }
70     if (number3 != null) {
71       numbers.addElement(number3);
72     }
73     int size = numbers.size();
74     if (size == 0) {
75       return null;
76     }
77     String[] result = new String[size];
78     for (int i = 0; i < size; i++) {
79       result[i] = (String) numbers.elementAt(i);
80     }
81     return result;
82   }
83
84   private static String buildName(String firstName, String lastName) {
85     if (firstName == null) {
86       return lastName;
87     } else {
88       return lastName == null ? firstName : firstName + ' ' + lastName;
89     }
90   }
91
92 }