Renamed UPC result type to Product, and introduced an idea of 'product ID' and 'norma...
[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, 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 address = matchSingleDoCoMoPrefixedField("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                                        buildPhoneNumbers(phoneNumber1, phoneNumber2, phoneNumber3),
54                                        maybeWrap(email),
55                                        null,
56                                        address,
57                                        org,
58                                        null,
59                                        title,
60                                        null);
61   }
62
63   private static String[] buildPhoneNumbers(String number1, String number2, String number3) {
64     Vector numbers = new Vector(3);
65     if (number1 != null) {
66       numbers.addElement(number1);
67     }
68     if (number2 != null) {
69       numbers.addElement(number2);
70     }
71     if (number3 != null) {
72       numbers.addElement(number3);
73     }
74     int size = numbers.size();
75     if (size == 0) {
76       return null;
77     }
78     String[] result = new String[size];
79     for (int i = 0; i < size; i++) {
80       result[i] = (String) numbers.elementAt(i);
81     }
82     return result;
83   }
84
85   private static String buildName(String firstName, String lastName) {
86     if (firstName == null) {
87       return lastName;
88     } else {
89       return lastName == null ? firstName : firstName + ' ' + lastName;
90     }
91   }
92
93 }