2 * Copyright 2008 Google Inc.
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;
19 import com.google.zxing.Result;
22 * Recognizes an NDEF message that encodes a URI according to the
23 * "URI Record Type Definition" specification.
25 * @author srowen@google.com (Sean Owen)
27 public final class NDEFURIParsedResult extends AbstractNDEFParsedResult {
29 private static final String[] URI_PREFIXES = new String[] {
37 "ftp://anonymous:anonymous@",
68 private final String uri;
70 private NDEFURIParsedResult(String uri) {
71 super(ParsedReaderResultType.NDEF_URI);
75 public static NDEFURIParsedResult parse(Result result) {
76 byte[] bytes = result.getRawBytes();
80 NDEFRecord ndefRecord = NDEFRecord.readRecord(bytes, 0);
81 if (ndefRecord == null || !ndefRecord.isMessageBegin() || !ndefRecord.isMessageEnd()) {
84 if (!ndefRecord.getType().equals(NDEFRecord.URI_WELL_KNOWN_TYPE)) {
87 String fullURI = decodeURIPayload(ndefRecord.getPayload());
88 return new NDEFURIParsedResult(fullURI);
91 static String decodeURIPayload(byte[] payload) {
92 int identifierCode = payload[0] & 0xFF;
94 if (identifierCode < URI_PREFIXES.length) {
95 prefix = URI_PREFIXES[identifierCode];
97 String restOfURI = bytesToString(payload, 1, payload.length - 1, "UTF-8");
98 return prefix == null ? restOfURI : prefix + restOfURI;
101 public String getURI() {
105 public String getDisplayResult() {