Support SMTP URLs
[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 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, true);
42     String lastName = matchSingleDoCoMoPrefixedField("X:", rawText, true);
43     String fullName = buildName(firstName, lastName);
44     String title = matchSingleDoCoMoPrefixedField("T:", rawText, true);
45     String org = matchSingleDoCoMoPrefixedField("C:", rawText, true);
46     String[] addresses = matchDoCoMoPrefixedField("A:", rawText, true);
47     String phoneNumber1 = matchSingleDoCoMoPrefixedField("B:", rawText, true);
48     String phoneNumber2 = matchSingleDoCoMoPrefixedField("M:", rawText, true);
49     String phoneNumber3 = matchSingleDoCoMoPrefixedField("F:", rawText, true);
50     String email = matchSingleDoCoMoPrefixedField("E:", rawText, true);
51
52     return new AddressBookParsedResult(maybeWrap(fullName),
53                                        null,
54                                        buildPhoneNumbers(phoneNumber1, phoneNumber2, phoneNumber3),
55                                        maybeWrap(email),
56                                        null,
57                                        addresses,
58                                        org,
59                                        null,
60                                        title,
61                                        null);
62   }
63
64   private static String[] buildPhoneNumbers(String number1, String number2, String number3) {
65     Vector numbers = new Vector(3);
66     if (number1 != null) {
67       numbers.addElement(number1);
68     }
69     if (number2 != null) {
70       numbers.addElement(number2);
71     }
72     if (number3 != null) {
73       numbers.addElement(number3);
74     }
75     int size = numbers.size();
76     if (size == 0) {
77       return null;
78     }
79     String[] result = new String[size];
80     for (int i = 0; i < size; i++) {
81       result[i] = (String) numbers.elementAt(i);
82     }
83     return result;
84   }
85
86   private static String buildName(String firstName, String lastName) {
87     if (firstName == null) {
88       return lastName;
89     } else {
90       return lastName == null ? firstName : firstName + ' ' + lastName;
91     }
92   }
93
94 }