Parsing wasn't correctly ignoring the ':' in prefix!
[zxing.git] / core / src / com / google / zxing / client / result / AddressBookAUParsedResult.java
1 /*
2  * Copyright 2008 Google Inc.
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 public final class AddressBookAUParsedResult extends ParsedReaderResult {
32
33   private final String[] names;
34   private final String[] phoneNumbers;
35   private final String[] emails;
36   private final String note;
37   private final String address;
38
39   private AddressBookAUParsedResult(String[] names, String[] phoneNumbers, String[] emails, String note, String address) {
40     super(ParsedReaderResultType.ADDRESSBOOK_AU);
41     this.names = names;
42     this.phoneNumbers = phoneNumbers;
43     this.emails = emails;
44     this.note = note;
45     this.address = address;
46   }
47
48   public static AddressBookAUParsedResult parse(Result result) {
49     String rawText = result.getText();
50     // MEMORY is mandatory; seems like a decent indicator, as does end-of-record separator CR/LF
51     if (rawText == null || rawText.indexOf("MEMORY") < 0 || rawText.indexOf("\r\n") < 0) {
52       return null;
53     }
54     String[] names = matchMultipleValuePrefix("NAME", 2, rawText);
55     String[] phoneNumbers = matchMultipleValuePrefix("TEL", 3, rawText);
56     String[] emails = matchMultipleValuePrefix("MAIL", 3, rawText);
57     String note = AbstractDoCoMoParsedResult.matchSinglePrefixedField("MEMORY:", rawText, '\r');
58     String address = AbstractDoCoMoParsedResult.matchSinglePrefixedField("ADD:", rawText, '\r');
59     return new AddressBookAUParsedResult(names, phoneNumbers, emails, note, address);
60   }
61
62   private static String[] matchMultipleValuePrefix(String prefix, int max, String rawText) {
63     Vector values = null;
64     for (int i = 1; i <= max; i++) {
65       String value = AbstractDoCoMoParsedResult.matchSinglePrefixedField(prefix + i + ':', rawText, '\r');
66       if (value == null) {
67         break;
68       }
69       if (values == null) {
70         values = new Vector(max); // lazy init
71       }
72       values.addElement(value);
73     }
74     if (values == null) {
75       return null;
76     }
77     String[] result = new String[values.size()];
78     for (int i = 0; i < result.length; i++) {
79       result[i] = (String) values.elementAt(i);
80     }
81     return result;
82   }
83
84   public String[] getNames() {
85     return names;
86   }
87
88   public String[] getPhoneNumbers() {
89     return phoneNumbers;
90   }
91
92   public String[] getEmails() {
93     return emails;
94   }
95
96   public String getNote() {
97     return note;
98   }
99
100   public String getAddress() {
101     return address;
102   }
103
104   public String getDisplayResult() {
105     StringBuffer result = new StringBuffer();
106     AbstractDoCoMoParsedResult.maybeAppend(names, result);
107     AbstractDoCoMoParsedResult.maybeAppend(emails, result);
108     AbstractDoCoMoParsedResult.maybeAppend(address, result);
109     AbstractDoCoMoParsedResult.maybeAppend(phoneNumbers, result);
110     AbstractDoCoMoParsedResult.maybeAppend(note, result);
111     return result.toString();
112   }
113
114 }