Issue 489 update the port
[zxing.git] / csharp / client / result / AddressBookDoCoMoResultParser.cs
1 /*\r
2 * Copyright 2007 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 the "MECARD" address book entry format.\r
22         /// \r
23         /// Supported keys: N, SOUND, TEL, EMAIL, NOTE, ADR, BDAY, URL, plus ORG\r
24         /// Unsupported keys: TEL-AV, NICKNAME\r
25         /// \r
26         /// Except for TEL, multiple values for keys are also not supported;\r
27         /// the first one found takes precedence.\r
28         /// \r
29         /// Our understanding of the MECARD format is based on this document:\r
30         /// \r
31         /// http://www.mobicode.org.tw/files/OMIA%20Mobile%20Bar%20Code%20Standard%20v3.2.1.doc \r
32         /// \r
33         /// </summary>\r
34         /// <author>  Sean Owen\r
35         /// </author>\r
36         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
37         /// </author>\r
38         sealed class AddressBookDoCoMoResultParser:AbstractDoCoMoResultParser\r
39         {\r
40                 \r
41                 public static AddressBookParsedResult parse(Result result)\r
42                 {\r
43                         System.String rawText = result.Text;\r
44                         if (rawText == null || !rawText.StartsWith("MECARD:"))\r
45                         {\r
46                                 return null;\r
47                         }\r
48                         System.String[] rawName = matchDoCoMoPrefixedField("N:", rawText, true);\r
49                         if (rawName == null)\r
50                         {\r
51                                 return null;\r
52                         }\r
53                         System.String name = parseName(rawName[0]);\r
54                         System.String pronunciation = matchSingleDoCoMoPrefixedField("SOUND:", rawText, true);\r
55                         System.String[] phoneNumbers = matchDoCoMoPrefixedField("TEL:", rawText, true);\r
56                         System.String[] emails = matchDoCoMoPrefixedField("EMAIL:", rawText, true);\r
57                         System.String note = matchSingleDoCoMoPrefixedField("NOTE:", rawText, false);\r
58                         System.String[] addresses = matchDoCoMoPrefixedField("ADR:", rawText, true);\r
59                         System.String birthday = matchSingleDoCoMoPrefixedField("BDAY:", rawText, true);\r
60                         if (birthday != null && !isStringOfDigits(birthday, 8))\r
61                         {\r
62                                 // No reason to throw out the whole card because the birthday is formatted wrong.\r
63                                 birthday = null;\r
64                         }\r
65                         System.String url = matchSingleDoCoMoPrefixedField("URL:", rawText, true);\r
66                         \r
67                         // Although ORG may not be strictly legal in MECARD, it does exist in VCARD and we might as well\r
68                         // honor it when found in the wild.\r
69                         System.String org = matchSingleDoCoMoPrefixedField("ORG:", rawText, true);\r
70                         \r
71                         return new AddressBookParsedResult(maybeWrap(name), pronunciation, phoneNumbers, emails, note, addresses, org, birthday, null, url);\r
72                 }\r
73                 \r
74                 private static System.String parseName(System.String name)\r
75                 {\r
76                         int comma = name.IndexOf(',');\r
77                         if (comma >= 0)\r
78                         {\r
79                                 // Format may be last,first; switch it around\r
80                                 return name.Substring(comma + 1) + ' ' + name.Substring(0, (comma) - (0));\r
81                         }\r
82                         return name;\r
83                 }\r
84         }\r
85 }