New C# port from Suraj Supekar
[zxing.git] / csharp / client / result / EmailAddressResultParser.cs
diff --git a/csharp/client/result/EmailAddressResultParser.cs b/csharp/client/result/EmailAddressResultParser.cs
new file mode 100755 (executable)
index 0000000..dff4546
--- /dev/null
@@ -0,0 +1,74 @@
+/*\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> Represents a result that encodes an e-mail address, either as a plain address\r
+       /// like "joe@example.org" or a mailto: URL like "mailto:joe@example.org".\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 EmailAddressResultParser:ResultParser\r
+       {\r
+               \r
+               public static EmailAddressParsedResult parse(Result result)\r
+               {\r
+                       System.String rawText = result.Text;\r
+                       if (rawText == null)\r
+                       {\r
+                               return null;\r
+                       }\r
+                       System.String emailAddress;\r
+                       if (rawText.StartsWith("mailto:") || rawText.StartsWith("MAILTO:"))\r
+                       {\r
+                               // If it starts with mailto:, assume it is definitely trying to be an email address\r
+                               emailAddress = rawText.Substring(7);\r
+                               int queryStart = emailAddress.IndexOf('?');\r
+                               if (queryStart >= 0)\r
+                               {\r
+                                       emailAddress = emailAddress.Substring(0, (queryStart) - (0));\r
+                               }\r
+                               System.Collections.Hashtable nameValues = parseNameValuePairs(rawText);\r
+                               System.String subject = null;\r
+                               System.String body = null;\r
+                               if (nameValues != null)\r
+                               {\r
+                                       if (emailAddress.Length == 0)\r
+                                       {\r
+                                               emailAddress = ((System.String) nameValues["to"]);\r
+                                       }\r
+                                       subject = ((System.String) nameValues["subject"]);\r
+                                       body = ((System.String) nameValues["body"]);\r
+                               }\r
+                               return new EmailAddressParsedResult(emailAddress, subject, body, rawText);\r
+                       }\r
+                       else\r
+                       {\r
+                               if (!EmailDoCoMoResultParser.isBasicallyValidEmailAddress(rawText))\r
+                               {\r
+                                       return null;\r
+                               }\r
+                               emailAddress = rawText;\r
+                               return new EmailAddressParsedResult(emailAddress, null, null, "mailto:" + emailAddress);\r
+                       }\r
+               }\r
+       }\r
+}
\ No newline at end of file