Small fixes for 1.6 release
[zxing.git] / csharp / client / result / AddressBookAUResultParser.cs
1 /*\r
2 * Copyright 2008 ZXing authors\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 *      http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License.\r
15 */\r
16 using System;\r
17 using Result = com.google.zxing.Result;\r
18 namespace com.google.zxing.client.result\r
19 {\r
20         \r
21         /// <summary> Implements KDDI AU's address book format. See\r
22         /// <a href="http://www.au.kddi.com/ezfactory/tec/two_dimensions/index.html">\r
23         /// http://www.au.kddi.com/ezfactory/tec/two_dimensions/index.html</a>.\r
24         /// (Thanks to Yuzo for translating!)\r
25         /// \r
26         /// </summary>\r
27         /// <author>  Sean Owen\r
28         /// </author>\r
29         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
30         /// </author>\r
31         sealed class AddressBookAUResultParser:ResultParser\r
32         {\r
33                 \r
34                 public static AddressBookParsedResult parse(Result result)\r
35                 {\r
36                         System.String rawText = result.Text;\r
37                         // MEMORY is mandatory; seems like a decent indicator, as does end-of-record separator CR/LF\r
38                         if (rawText == null || rawText.IndexOf("MEMORY") < 0 || rawText.IndexOf("\r\n") < 0)\r
39                         {\r
40                                 return null;\r
41                         }\r
42                         \r
43                         // NAME1 and NAME2 have specific uses, namely written name and pronunciation, respectively.\r
44                         // Therefore we treat them specially instead of as an array of names.\r
45                         System.String name = matchSinglePrefixedField("NAME1:", rawText, '\r', true);\r
46                         System.String pronunciation = matchSinglePrefixedField("NAME2:", rawText, '\r', true);\r
47                         \r
48                         System.String[] phoneNumbers = matchMultipleValuePrefix("TEL", 3, rawText, true);\r
49                         System.String[] emails = matchMultipleValuePrefix("MAIL", 3, rawText, true);\r
50                         System.String note = matchSinglePrefixedField("MEMORY:", rawText, '\r', false);\r
51                         System.String address = matchSinglePrefixedField("ADD:", rawText, '\r', true);\r
52                         System.String[] addresses = address == null?null:new System.String[]{address};\r
53                         return new AddressBookParsedResult(maybeWrap(name), pronunciation, phoneNumbers, emails, note, addresses, null, null, null, null);\r
54                 }\r
55                 \r
56                 private static System.String[] matchMultipleValuePrefix(System.String prefix, int max, System.String rawText, bool trim)\r
57                 {\r
58                         System.Collections.ArrayList values = null;\r
59                         for (int i = 1; i <= max; i++)\r
60                         {\r
61                                 System.String value_Renamed = matchSinglePrefixedField(prefix + i + ':', rawText, '\r', trim);\r
62                                 if (value_Renamed == null)\r
63                                 {\r
64                                         break;\r
65                                 }\r
66                                 if (values == null)\r
67                                 {\r
68                                         values = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(max)); // lazy init\r
69                                 }\r
70                                 values.Add(value_Renamed);\r
71                         }\r
72                         if (values == null)\r
73                         {\r
74                                 return null;\r
75                         }\r
76                         return toStringArray(values);\r
77                 }\r
78         }\r
79 }