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