New C# port from Suraj Supekar
[zxing.git] / csharp / client / result / URIResultParser.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> Tries to parse results that are a URI of some kind.\r
22         /// \r
23         /// </summary>\r
24         /// <author>  Sean Owen\r
25         /// </author>\r
26         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
27         /// </author>\r
28         sealed class URIResultParser:ResultParser\r
29         {\r
30                 \r
31                 private URIResultParser()\r
32                 {\r
33                 }\r
34                 \r
35                 public static URIParsedResult parse(Result result)\r
36                 {\r
37                         System.String rawText = result.Text;\r
38                         // We specifically handle the odd "URL" scheme here for simplicity\r
39                         if (rawText != null && rawText.StartsWith("URL:"))\r
40                         {\r
41                                 rawText = rawText.Substring(4);\r
42                         }\r
43                         if (!isBasicallyValidURI(rawText))\r
44                         {\r
45                                 return null;\r
46                         }\r
47                         return new URIParsedResult(rawText, null);\r
48                 }\r
49                 \r
50                 /// <summary> Determines whether a string is not obviously not a URI. This implements crude checks; this class does not\r
51                 /// intend to strictly check URIs as its only function is to represent what is in a barcode, but, it does\r
52                 /// need to know when a string is obviously not a URI.\r
53                 /// </summary>\r
54                 internal static bool isBasicallyValidURI(System.String uri)\r
55                 {\r
56                         if (uri == null || uri.IndexOf(' ') >= 0 || uri.IndexOf('\n') >= 0)\r
57                         {\r
58                                 return false;\r
59                         }\r
60                         // Look for period in a domain but followed by at least a two-char TLD\r
61                         // Forget strings that don't have a valid-looking protocol\r
62                         int period = uri.IndexOf('.');\r
63                         if (period >= uri.Length - 2)\r
64                         {\r
65                                 return false;\r
66                         }\r
67                         int colon = uri.IndexOf(':');\r
68                         if (period < 0 && colon < 0)\r
69                         {\r
70                                 return false;\r
71                         }\r
72                         if (colon >= 0)\r
73                         {\r
74                                 if (period < 0 || period > colon)\r
75                                 {\r
76                                         // colon ends the protocol\r
77                                         for (int i = 0; i < colon; i++)\r
78                                         {\r
79                                                 char c = uri[i];\r
80                                                 if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z'))\r
81                                                 {\r
82                                                         return false;\r
83                                                 }\r
84                                         }\r
85                                 }\r
86                                 else\r
87                                 {\r
88                                         // colon starts the port; crudely look for at least two numbers\r
89                                         if (colon >= uri.Length - 2)\r
90                                         {\r
91                                                 return false;\r
92                                         }\r
93                                         for (int i = colon + 1; i < colon + 3; i++)\r
94                                         {\r
95                                                 char c = uri[i];\r
96                                                 if (c < '0' || c > '9')\r
97                                                 {\r
98                                                         return false;\r
99                                                 }\r
100                                         }\r
101                                 }\r
102                         }\r
103                         return true;\r
104                 }\r
105         }\r
106 }