Add support for Android Intent URIs encoded in a barcode
[zxing.git] / core / src / com / google / zxing / client / result / URLTOResult.java
1 /*
2  * Copyright 2007 Google Inc.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.client.result;
18
19 /**
20  * "URLTO" result format, which is of the form "URLTO:[title]:[url]".
21  * This seems to be used sometimes, but I am not able to find documentation
22  * on its origin or official format?
23  *
24  * @author srowen@google.com (Sean Owen)
25  */
26 public final class URLTOResult extends ParsedReaderResult {
27
28   private final String title;
29   private final String uri;
30
31   private URLTOResult(String title, String uri) {
32     super(ParsedReaderResultType.URLTO);
33     this.title = title;
34     this.uri = uri;
35   }
36
37   public static URLTOResult parse(String rawText) {
38     if (!rawText.startsWith("URLTO:")) {
39       return null;
40     }
41     int titleEnd = rawText.indexOf(':', 6);
42     if (titleEnd < 0) {
43       return null;
44     }
45     String title = rawText.substring(6, titleEnd);
46     String uri = rawText.substring(titleEnd + 1);
47     return new URLTOResult(title, uri);
48   }
49
50   public String getTitle() {
51     return title;
52   }
53
54   public String getURI() {
55     return uri;
56   }
57
58   public String getDisplayResult() {
59     if (title == null) {
60       return uri;
61     } else {
62       return title + '\n' + uri;
63     }
64   }
65
66 }