Big refactoring of ParsedResult: now split into ResultParser and ParsedResult classes...
[zxing.git] / core / src / com / google / zxing / client / result / optional / NDEFURIResultParser.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.Result;
20 import com.google.zxing.client.result.URIParsedResult;
21
22 /**
23  * Recognizes an NDEF message that encodes a URI according to the
24  * "URI Record Type Definition" specification.
25  *
26  * @author srowen@google.com (Sean Owen)
27  */
28 public final class NDEFURIResultParser extends AbstractNDEFResultParser {
29
30   private static final String[] URI_PREFIXES = {
31       null,
32       "http://www.",
33       "https://www.",
34       "http://",
35       "https://",
36       "tel:",
37       "mailto:",
38       "ftp://anonymous:anonymous@",
39       "ftp://ftp.",
40       "ftps://",
41       "sftp://",
42       "smb://",
43       "nfs://",
44       "ftp://",
45       "dav://",
46       "news:",
47       "telnet://",
48       "imap:",
49       "rtsp://",
50       "urn:",
51       "pop:",
52       "sip:",
53       "sips:",
54       "tftp:",
55       "btspp://",
56       "btl2cap://",
57       "btgoep://",
58       "tcpobex://",
59       "irdaobex://",
60       "file://",
61       "urn:epc:id:",
62       "urn:epc:tag:",
63       "urn:epc:pat:",
64       "urn:epc:raw:",
65       "urn:epc:",
66       "urn:nfc:",
67   };
68
69   public static URIParsedResult parse(Result result) {
70     byte[] bytes = result.getRawBytes();
71     if (bytes == null) {
72       return null;
73     }
74     NDEFRecord ndefRecord = NDEFRecord.readRecord(bytes, 0);
75     if (ndefRecord == null || !ndefRecord.isMessageBegin() || !ndefRecord.isMessageEnd()) {
76       return null;
77     }
78     if (!ndefRecord.getType().equals(NDEFRecord.URI_WELL_KNOWN_TYPE)) {
79       return null;
80     }
81     String fullURI = decodeURIPayload(ndefRecord.getPayload());
82     return new URIParsedResult(fullURI, null);
83   }
84
85   static String decodeURIPayload(byte[] payload) {
86     int identifierCode = payload[0] & 0xFF;
87     String prefix = null;
88     if (identifierCode < URI_PREFIXES.length) {
89       prefix = URI_PREFIXES[identifierCode];
90     }
91     String restOfURI = bytesToString(payload, 1, payload.length - 1, "UTF8");
92     return prefix == null ? restOfURI : prefix + restOfURI;
93   }
94
95 }