New C# port from Suraj Supekar
[zxing.git] / csharp / client / result / AddressBookDoCoMoResultParser.cs
diff --git a/csharp/client/result/AddressBookDoCoMoResultParser.cs b/csharp/client/result/AddressBookDoCoMoResultParser.cs
new file mode 100755 (executable)
index 0000000..68b5f23
--- /dev/null
@@ -0,0 +1,85 @@
+/*\r
+* Copyright 2007 ZXing authors\r
+*\r
+* Licensed under the Apache License, Version 2.0 (the "License");\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+*      http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*/\r
+using System;\r
+using Result = com.google.zxing.Result;\r
+namespace com.google.zxing.client.result\r
+{\r
+       \r
+       /// <summary> Implements the "MECARD" address book entry format.\r
+       /// \r
+       /// Supported keys: N, SOUND, TEL, EMAIL, NOTE, ADR, BDAY, URL, plus ORG\r
+       /// Unsupported keys: TEL-AV, NICKNAME\r
+       /// \r
+       /// Except for TEL, multiple values for keys are also not supported;\r
+       /// the first one found takes precedence.\r
+       /// \r
+       /// Our understanding of the MECARD format is based on this document:\r
+       /// \r
+       /// http://www.mobicode.org.tw/files/OMIA%20Mobile%20Bar%20Code%20Standard%20v3.2.1.doc \r
+       /// \r
+       /// </summary>\r
+       /// <author>  Sean Owen\r
+       /// </author>\r
+       /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+       /// </author>\r
+       sealed class AddressBookDoCoMoResultParser:AbstractDoCoMoResultParser\r
+       {\r
+               \r
+               public static AddressBookParsedResult parse(Result result)\r
+               {\r
+                       System.String rawText = result.Text;\r
+                       if (rawText == null || !rawText.StartsWith("MECARD:"))\r
+                       {\r
+                               return null;\r
+                       }\r
+                       System.String[] rawName = matchDoCoMoPrefixedField("N:", rawText, true);\r
+                       if (rawName == null)\r
+                       {\r
+                               return null;\r
+                       }\r
+                       System.String name = parseName(rawName[0]);\r
+                       System.String pronunciation = matchSingleDoCoMoPrefixedField("SOUND:", rawText, true);\r
+                       System.String[] phoneNumbers = matchDoCoMoPrefixedField("TEL:", rawText, true);\r
+                       System.String[] emails = matchDoCoMoPrefixedField("EMAIL:", rawText, true);\r
+                       System.String note = matchSingleDoCoMoPrefixedField("NOTE:", rawText, false);\r
+                       System.String[] addresses = matchDoCoMoPrefixedField("ADR:", rawText, true);\r
+                       System.String birthday = matchSingleDoCoMoPrefixedField("BDAY:", rawText, true);\r
+                       if (birthday != null && !isStringOfDigits(birthday, 8))\r
+                       {\r
+                               // No reason to throw out the whole card because the birthday is formatted wrong.\r
+                               birthday = null;\r
+                       }\r
+                       System.String url = matchSingleDoCoMoPrefixedField("URL:", rawText, true);\r
+                       \r
+                       // Although ORG may not be strictly legal in MECARD, it does exist in VCARD and we might as well\r
+                       // honor it when found in the wild.\r
+                       System.String org = matchSingleDoCoMoPrefixedField("ORG:", rawText, true);\r
+                       \r
+                       return new AddressBookParsedResult(maybeWrap(name), pronunciation, phoneNumbers, emails, note, addresses, org, birthday, null, url);\r
+               }\r
+               \r
+               private static System.String parseName(System.String name)\r
+               {\r
+                       int comma = name.IndexOf(',');\r
+                       if (comma >= 0)\r
+                       {\r
+                               // Format may be last,first; switch it around\r
+                               return name.Substring(comma + 1) + ' ' + name.Substring(0, (comma) - (0));\r
+                       }\r
+                       return name;\r
+               }\r
+       }\r
+}
\ No newline at end of file