Implemented Paul's solution to the basic/regular build problem -- a sort of pseudo...
[zxing.git] / core / src / com / google / zxing / client / result / optional / MobileTagSimpleWebParsedResult.java
1 /*
2  * Copyright 2008 ZXing authors
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.optional;
18
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.Result;
21 import com.google.zxing.client.result.ParsedReaderResultType;
22
23 /**
24  * <p>Represents a "simple web" result encoded according to section 4.11 of the
25  * MobileTag Reader International Specification.</p>
26  *
27  * @author srowen@google.com (Sean Owen)
28  */
29 public final class MobileTagSimpleWebParsedResult extends AbstractMobileTagParsedResult {
30
31   public static final String SERVICE_TYPE = "04";
32   private static final String[] URI_PREFIXES = {
33       null,
34       "http://",
35       "http://www.",
36       "https://",
37       "https://www.",
38       "rtsp://",
39   };
40
41   private final String title;
42   private final String uri;
43
44   private MobileTagSimpleWebParsedResult(String title, String uri) {
45     super(ParsedReaderResultType.MOBILETAG_SIMPLE_WEB);
46     this.title = title;
47     this.uri = uri;
48   }
49
50   public static MobileTagSimpleWebParsedResult parse(Result result) {
51     if (!result.getBarcodeFormat().equals(BarcodeFormat.DATAMATRIX)) {
52       return null;
53     }
54     String rawText = result.getText();
55     if (!rawText.startsWith(SERVICE_TYPE)) {
56       return null;
57     }
58
59     String[] matches = matchDelimitedFields(rawText.substring(2), 2);
60     if (matches == null) {
61       return null;
62     }
63     String uri = matches[0];
64     String title = matches[1];
65
66     char maybePrefixChar = uri.charAt(2);
67     if (maybePrefixChar >= '0' && maybePrefixChar <= '9') {
68       int prefixIndex = maybePrefixChar - '0';
69       // Note that '0' is reserved
70       if (prefixIndex >= 1 && prefixIndex < URI_PREFIXES.length) {
71         uri = URI_PREFIXES[prefixIndex] + uri.substring(1);
72       } else {
73         uri = uri.substring(1);
74       }
75     }
76
77     return new MobileTagSimpleWebParsedResult(title, uri);
78   }
79
80   public String getTitle() {
81     return title;
82   }
83
84   public String getURI() {
85     return uri;
86   }
87
88   public String getDisplayResult() {
89     if (title == null) {
90       return uri;
91     } else {
92       return title + '\n' + uri;
93     }
94   }
95
96 }