Add iCal support, plus many small changes suggested by code inspection -- mostly...
[zxing.git] / core / src / com / google / zxing / client / result / AddressBookAUResultParser.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 KDDI AU's address book format. See
25  * <a href="http://www.au.kddi.com/ezfactory/tec/two_dimensions/index.html">
26  * http://www.au.kddi.com/ezfactory/tec/two_dimensions/index.html</a>.
27  * (Thanks to Yuzo for translating!)
28  *
29  * @author srowen@google.com (Sean Owen)
30  */
31 final class AddressBookAUResultParser extends ResultParser {
32
33   public static AddressBookParsedResult parse(Result result) {
34     String rawText = result.getText();
35     // MEMORY is mandatory; seems like a decent indicator, as does end-of-record separator CR/LF
36     if (rawText == null || rawText.indexOf("MEMORY") < 0 || rawText.indexOf("\r\n") < 0) {
37       return null;
38     }
39     String[] names = matchMultipleValuePrefix("NAME", 2, rawText);
40     String[] phoneNumbers = matchMultipleValuePrefix("TEL", 3, rawText);
41     String[] emails = matchMultipleValuePrefix("MAIL", 3, rawText);
42     String note = matchSinglePrefixedField("MEMORY:", rawText, '\r');
43     String address = matchSinglePrefixedField("ADD:", rawText, '\r');
44     return new AddressBookParsedResult(names, phoneNumbers, emails, note, address, null, null, null);
45   }
46
47   private static String[] matchMultipleValuePrefix(String prefix, int max, String rawText) {
48     Vector values = null;
49     for (int i = 1; i <= max; i++) {
50       String value = matchSinglePrefixedField(prefix + i + ':', rawText, '\r');
51       if (value == null) {
52         break;
53       }
54       if (values == null) {
55         values = new Vector(max); // lazy init
56       }
57       values.addElement(value);
58     }
59     if (values == null) {
60       return null;
61     }
62     return toStringArray(values);
63   }
64
65
66 }