2 * Copyright 2007 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.client.result;
19 import com.google.zxing.Result;
22 * @author srowen@google.com (Sean Owen)
24 public final class URIParsedResult extends ParsedReaderResult {
26 private final String uri;
28 private URIParsedResult(String uri) {
29 super(ParsedReaderResultType.URI);
33 public static URIParsedResult parse(Result result) {
34 String rawText = result.getText();
35 if (!isBasicallyValidURI(rawText)) {
38 String uri = massagePossibleURI(rawText);
39 return new URIParsedResult(uri);
42 public String getURI() {
46 public String getDisplayResult() {
51 * Transforms a string that possibly represents a URI into something more proper, by adding or canonicalizing
54 private static String massagePossibleURI(String uri) {
55 // Take off leading "URL:" if present
56 if (uri.startsWith("URL:")) {
57 uri = uri.substring(4);
59 int protocolEnd = uri.indexOf(':');
60 if (protocolEnd < 0) {
61 // No protocol, assume http
62 uri = "http://" + uri;
64 // Lowercase protocol to avoid problems
65 uri = uri.substring(0, protocolEnd).toLowerCase() + uri.substring(protocolEnd);
66 // TODO this logic isn't quite right for URIs like "example.org:443/foo"
72 * Determines whether a string is not obviously not a URI. This implements crude checks; this class does not
73 * intend to strictly check URIs as its only function is to represent what is in a barcode, but, it does
74 * need to know when a string is obviously not a URI.
76 static boolean isBasicallyValidURI(String uri) {
77 return uri != null && uri.indexOf(' ') < 0 && (uri.indexOf(':') >= 0 || uri.indexOf('.') >= 0);