Issue 489 update the port
[zxing.git] / csharp / client / result / EmailDoCoMoResultParser.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 "MATMSG" email message entry format.\r
22         /// \r
23         /// Supported keys: TO, SUB, BODY\r
24         /// \r
25         /// </summary>\r
26         /// <author>  Sean Owen\r
27         /// </author>\r
28         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
29         /// </author>\r
30         sealed class EmailDoCoMoResultParser:AbstractDoCoMoResultParser\r
31         {\r
32                 \r
33                 //UPGRADE_NOTE: Final was removed from the declaration of 'ATEXT_SYMBOLS'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
34                 private static readonly char[] ATEXT_SYMBOLS = new char[]{'@', '.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^', '_', '`', '{', '|', '}', '~'};\r
35                 \r
36                 public static EmailAddressParsedResult parse(Result result)\r
37                 {\r
38                         System.String rawText = result.Text;\r
39                         if (rawText == null || !rawText.StartsWith("MATMSG:"))\r
40                         {\r
41                                 return null;\r
42                         }\r
43                         System.String[] rawTo = matchDoCoMoPrefixedField("TO:", rawText, true);\r
44                         if (rawTo == null)\r
45                         {\r
46                                 return null;\r
47                         }\r
48                         System.String to = rawTo[0];\r
49                         if (!isBasicallyValidEmailAddress(to))\r
50                         {\r
51                                 return null;\r
52                         }\r
53                         System.String subject = matchSingleDoCoMoPrefixedField("SUB:", rawText, false);\r
54                         System.String body = matchSingleDoCoMoPrefixedField("BODY:", rawText, false);\r
55                         return new EmailAddressParsedResult(to, subject, body, "mailto:" + to);\r
56                 }\r
57                 \r
58                 /// <summary> This implements only the most basic checking for an email address's validity -- that it contains\r
59                 /// an '@' contains no characters disallowed by RFC 2822. This is an overly lenient definition of\r
60                 /// validity. We want to generally be lenient here since this class is only intended to encapsulate what's\r
61                 /// in a barcode, not "judge" it.\r
62                 /// </summary>\r
63                 internal static bool isBasicallyValidEmailAddress(System.String email)\r
64                 {\r
65                         if (email == null)\r
66                         {\r
67                                 return false;\r
68                         }\r
69                         bool atFound = false;\r
70                         for (int i = 0; i < email.Length; i++)\r
71                         {\r
72                                 char c = email[i];\r
73                                 if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9') && !isAtextSymbol(c))\r
74                                 {\r
75                                         return false;\r
76                                 }\r
77                                 if (c == '@')\r
78                                 {\r
79                                         if (atFound)\r
80                                         {\r
81                                                 return false;\r
82                                         }\r
83                                         atFound = true;\r
84                                 }\r
85                         }\r
86                         return atFound;\r
87                 }\r
88                 \r
89                 private static bool isAtextSymbol(char c)\r
90                 {\r
91                         for (int i = 0; i < ATEXT_SYMBOLS.Length; i++)\r
92                         {\r
93                                 if (c == ATEXT_SYMBOLS[i])\r
94                                 {\r
95                                         return true;\r
96                                 }\r
97                         }\r
98                         return false;\r
99                 }\r
100         }\r
101 }