First checkin of support for basic NDEF message types -- not enabled yet
[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 byte URI_WELL_KNOWN_TYPE = (byte) 0x55;
30
31   private static final String[] URI_PREFIXES = new String[] {
32       null,
33       "http://www.",
34       "https://www.",
35       "http://",
36       "https://",
37       "tel:",
38       "mailto:",
39       "ftp://anonymous:anonymous@",
40       "ftp://ftp.",
41       "ftps://",
42       "sftp://",
43       "smb://",
44       "nfs://",
45       "ftp://",
46       "dav://",
47       "news:",
48       "telnet://",
49       "imap:",
50       "rtsp://",
51       "urn:",
52       "pop:",
53       "sip:",
54       "sips:",
55       "tftp:",
56       "btspp://",
57       "btl2cap://",
58       "btgoep://",
59       "tcpobex://",
60       "irdaobex://",
61       "file://",
62       "urn:epc:id:",
63       "urn:epc:tag:",
64       "urn:epc:pat:",
65       "urn:epc:raw:",
66       "urn:epc:",
67       "urn:nfc:",
68   };
69
70   private final String uri;
71
72   private NDEFURIParsedResult(String uri) {
73     super(ParsedReaderResultType.NDEF_TEXT);
74     this.uri = uri;
75   }
76
77   public static NDEFURIParsedResult parse(Result result) {
78     byte[] bytes = result.getRawBytes();
79     if (!isMaybeNDEF(bytes)) {
80       return null;
81     }
82
83     int payloadLength = bytes[2] & 0xFF;
84
85     // Next 1 byte is type
86     if (bytes[3] != URI_WELL_KNOWN_TYPE) {
87       return null;
88     }
89
90     int identifierCode = bytes[4] & 0xFF;
91     String prefix = null;
92     if (identifierCode < URI_PREFIXES.length) {
93       prefix = URI_PREFIXES[identifierCode];
94     }
95
96     String restOfURI = bytesToString(bytes, 5, payloadLength - 1, "UTF-8");
97     String fullURI = prefix == null ? restOfURI : prefix + restOfURI;
98     return new NDEFURIParsedResult(fullURI);
99   }
100
101   public String getURI() {
102     return uri;
103   }
104
105   public String getDisplayResult() {
106     return uri;
107   }
108
109 }