2 * Copyright 2007 ZXing authors
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 using Result = com.google.zxing.Result;
\r
18 namespace com.google.zxing.client.result
\r
21 /// <summary> Tries to parse results that are a URI of some kind.
\r
24 /// <author> Sean Owen
\r
26 /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
\r
28 sealed class URIResultParser:ResultParser
\r
31 private URIResultParser()
\r
35 public static URIParsedResult parse(Result result)
\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
41 rawText = rawText.Substring(4);
\r
43 if (!isBasicallyValidURI(rawText))
\r
47 return new URIParsedResult(rawText, null);
\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
54 internal static bool isBasicallyValidURI(System.String uri)
\r
56 if (uri == null || uri.IndexOf(' ') >= 0 || uri.IndexOf('\n') >= 0)
\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
67 int colon = uri.IndexOf(':');
\r
68 if (period < 0 && colon < 0)
\r
74 if (period < 0 || period > colon)
\r
76 // colon ends the protocol
\r
77 for (int i = 0; i < colon; i++)
\r
80 if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z'))
\r
88 // colon starts the port; crudely look for at least two numbers
\r
89 if (colon >= uri.Length - 2)
\r
93 for (int i = colon + 1; i < colon + 3; i++)
\r
96 if (c < '0' || c > '9')
\r