New C# port from Suraj Supekar
[zxing.git] / csharp / client / result / BizcardResultParser.cs
diff --git a/csharp/client/result/BizcardResultParser.cs b/csharp/client/result/BizcardResultParser.cs
new file mode 100755 (executable)
index 0000000..2e4f48b
--- /dev/null
@@ -0,0 +1,98 @@
+/*\r
+* Copyright 2008 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 "BIZCARD" address book entry format, though this has been\r
+       /// largely reverse-engineered from examples observed in the wild -- still\r
+       /// looking for a definitive reference.\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 BizcardResultParser:AbstractDoCoMoResultParser\r
+       {\r
+               \r
+               // Yes, we extend AbstractDoCoMoResultParser since the format is very much\r
+               // like the DoCoMo MECARD format, but this is not technically one of \r
+               // DoCoMo's proposed formats\r
+               \r
+               public static AddressBookParsedResult parse(Result result)\r
+               {\r
+                       System.String rawText = result.Text;\r
+                       if (rawText == null || !rawText.StartsWith("BIZCARD:"))\r
+                       {\r
+                               return null;\r
+                       }\r
+                       System.String firstName = matchSingleDoCoMoPrefixedField("N:", rawText, true);\r
+                       System.String lastName = matchSingleDoCoMoPrefixedField("X:", rawText, true);\r
+                       System.String fullName = buildName(firstName, lastName);\r
+                       System.String title = matchSingleDoCoMoPrefixedField("T:", rawText, true);\r
+                       System.String org = matchSingleDoCoMoPrefixedField("C:", rawText, true);\r
+                       System.String[] addresses = matchDoCoMoPrefixedField("A:", rawText, true);\r
+                       System.String phoneNumber1 = matchSingleDoCoMoPrefixedField("B:", rawText, true);\r
+                       System.String phoneNumber2 = matchSingleDoCoMoPrefixedField("M:", rawText, true);\r
+                       System.String phoneNumber3 = matchSingleDoCoMoPrefixedField("F:", rawText, true);\r
+                       System.String email = matchSingleDoCoMoPrefixedField("E:", rawText, true);\r
+                       \r
+                       return new AddressBookParsedResult(maybeWrap(fullName), null, buildPhoneNumbers(phoneNumber1, phoneNumber2, phoneNumber3), maybeWrap(email), null, addresses, org, null, title, null);\r
+               }\r
+               \r
+               private static System.String[] buildPhoneNumbers(System.String number1, System.String number2, System.String number3)\r
+               {\r
+                       System.Collections.ArrayList numbers = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(3));\r
+                       if (number1 != null)\r
+                       {\r
+                               numbers.Add(number1);\r
+                       }\r
+                       if (number2 != null)\r
+                       {\r
+                               numbers.Add(number2);\r
+                       }\r
+                       if (number3 != null)\r
+                       {\r
+                               numbers.Add(number3);\r
+                       }\r
+                       int size = numbers.Count;\r
+                       if (size == 0)\r
+                       {\r
+                               return null;\r
+                       }\r
+                       System.String[] result = new System.String[size];\r
+                       for (int i = 0; i < size; i++)\r
+                       {\r
+                               result[i] = ((System.String) numbers[i]);\r
+                       }\r
+                       return result;\r
+               }\r
+               \r
+               private static System.String buildName(System.String firstName, System.String lastName)\r
+               {\r
+                       if (firstName == null)\r
+                       {\r
+                               return lastName;\r
+                       }\r
+                       else\r
+                       {\r
+                               return lastName == null?firstName:firstName + ' ' + lastName;\r
+                       }\r
+               }\r
+       }\r
+}
\ No newline at end of file