2 * Copyright 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.result.optional;
19 import com.google.zxing.Result;
20 import com.google.zxing.client.result.ParsedReaderResultType;
23 * Recognizes an NDEF message that encodes a URI according to the
24 * "URI Record Type Definition" specification.
26 * @author srowen@google.com (Sean Owen)
28 public final class NDEFURIParsedResult extends AbstractNDEFParsedResult {
30 private static final String[] URI_PREFIXES = {
38 "ftp://anonymous:anonymous@",
69 private final String uri;
71 private NDEFURIParsedResult(String uri) {
72 super(ParsedReaderResultType.NDEF_URI);
76 public static NDEFURIParsedResult parse(Result result) {
77 byte[] bytes = result.getRawBytes();
81 NDEFRecord ndefRecord = NDEFRecord.readRecord(bytes, 0);
82 if (ndefRecord == null || !ndefRecord.isMessageBegin() || !ndefRecord.isMessageEnd()) {
85 if (!ndefRecord.getType().equals(NDEFRecord.URI_WELL_KNOWN_TYPE)) {
88 String fullURI = decodeURIPayload(ndefRecord.getPayload());
89 return new NDEFURIParsedResult(fullURI);
92 static String decodeURIPayload(byte[] payload) {
93 int identifierCode = payload[0] & 0xFF;
95 if (identifierCode < URI_PREFIXES.length) {
96 prefix = URI_PREFIXES[identifierCode];
98 String restOfURI = bytesToString(payload, 1, payload.length - 1, "UTF-8");
99 return prefix == null ? restOfURI : prefix + restOfURI;
102 public String getURI() {
106 public String getDisplayResult() {