/*
 * Copyright 2007 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.zxing.client.result;

import com.google.zxing.Result;

/**
 * Tries to parse results that are a URI of some kind.
 * 
 * @author Sean Owen
 */
final class URIResultParser extends ResultParser {

  private URIResultParser() {
  }

  public static URIParsedResult parse(Result result) {
    String rawText = result.getText();
    if (!isBasicallyValidURI(rawText)) {
      return null;
    }
    // We specifically handle the odd "URL" scheme here for simplicity
    if (rawText.startsWith("URL:")) {
      rawText = rawText.substring(4);
    }
    return new URIParsedResult(rawText, null);
  }

  /**
   * Determines whether a string is not obviously not a URI. This implements crude checks; this class does not
   * intend to strictly check URIs as its only function is to represent what is in a barcode, but, it does
   * need to know when a string is obviously not a URI.
   */
  static boolean isBasicallyValidURI(String uri) {
    return uri != null && uri.indexOf(' ') < 0 && uri.indexOf('\n') < 0 &&
           (uri.indexOf(':') >= 0 || uri.indexOf('.') >= 0);
  }

}
@@ -939,74 +939,8 @@
 ();%0A
-    if (!isBasicallyValidURI(rawText)) %7B%0A      return null;%0A    %7D%0A
     
@@ -1021,16 +1021,35 @@
 (rawText
+ != null && rawText
 .startsW
@@ -1107,16 +1107,82 @@
 ;%0A    %7D%0A
+    if (!isBasicallyValidURI(rawText)) %7B%0A      return null;%0A    %7D%0A
     retu
@@ -1566,33 +1566,31 @@
 ) %7B%0A
+%0A
     
-return 
+if (
 uri 
-!
+=
 = null 
-&&
+%7C%7C
  uri
@@ -1603,22 +1603,23 @@
 Of(' ') 
-%3C 0 &&
+%3E= 0 %7C%7C
  uri.ind
@@ -1633,27 +1633,59 @@
 n') 
-%3C 0 &&%0A           (
+%3E= 0) %7B%0A      return false;%0A    %7D%0A    int period = 
 uri.
@@ -1697,11 +1697,169 @@
 Of('
-:
+.
 ')
+;%0A    // Look for period in a domain but followed by at least a two-char TLD%0A    if (period %3E= uri.length() - 2) %7B%0A      return false;%0A    %7D%0A    return period
  %3E= 
@@ -1880,17 +1880,16 @@
 Of('
-.
+:
 ') %3E= 0
-)
 ;%0A  

Improve URL detection rule to exclude stuff like "Hi."