c805a9d26c999968d84135323e8cd448eb428b97
[zxing.git] / core / src / com / google / zxing / client / result / optional / MobileTagSimpleWebResultParser.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.URIParsedResult;
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 final class MobileTagSimpleWebResultParser extends AbstractMobileTagResultParser {
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   public static URIParsedResult parse(Result result) {
42     if (!result.getBarcodeFormat().equals(BarcodeFormat.DATAMATRIX)) {
43       return null;
44     }
45     String rawText = result.getText();
46     if (!rawText.startsWith(SERVICE_TYPE)) {
47       return null;
48     }
49
50     String[] matches = matchDelimitedFields(rawText.substring(2), 2);
51     if (matches == null) {
52       return null;
53     }
54     String uri = matches[0];
55     String title = matches[1];
56
57     char maybePrefixChar = uri.charAt(2);
58     if (maybePrefixChar >= '0' && maybePrefixChar <= '9') {
59       int prefixIndex = maybePrefixChar - '0';
60       // Note that '0' is reserved
61       if (prefixIndex >= 1 && prefixIndex < URI_PREFIXES.length) {
62         uri = URI_PREFIXES[prefixIndex] + uri.substring(1);
63       } else {
64         uri = uri.substring(1);
65       }
66     }
67
68     return new URIParsedResult(uri, title);
69   }
70
71 }