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;
19 import com.google.zxing.Result;
22 * Parses a "geo:" URI result, which specifies a location on the surface of
23 * the Earth as well as an optional altitude above the surface. See
24 * <a href="http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00">
25 * http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00</a>.
29 final class GeoResultParser extends ResultParser {
31 private GeoResultParser() {
34 public static GeoParsedResult parse(Result result) {
35 String rawText = result.getText();
36 if (rawText == null || (!rawText.startsWith("geo:") && !rawText.startsWith("GEO:"))) {
39 // Drop geo, query portion
40 int queryStart = rawText.indexOf('?', 4);
42 String geoURIWithoutQuery;
45 geoURIWithoutQuery = rawText.substring(4);
47 query = rawText.substring(queryStart + 1);
48 geoURIWithoutQuery = rawText.substring(4, queryStart);
50 int latitudeEnd = geoURIWithoutQuery.indexOf(',');
51 if (latitudeEnd < 0) {
54 int longitudeEnd = geoURIWithoutQuery.indexOf(',', latitudeEnd + 1);
55 double latitude, longitude, altitude;
57 latitude = Double.parseDouble(geoURIWithoutQuery.substring(0, latitudeEnd));
58 if (latitude > 90.0 || latitude < -90.0) {
61 if (longitudeEnd < 0) {
62 longitude = Double.parseDouble(geoURIWithoutQuery.substring(latitudeEnd + 1));
65 longitude = Double.parseDouble(geoURIWithoutQuery.substring(latitudeEnd + 1, longitudeEnd));
66 altitude = Double.parseDouble(geoURIWithoutQuery.substring(longitudeEnd + 1));
68 if (longitude > 180.0 || longitude < -180.0 || altitude < 0) {
71 } catch (NumberFormatException nfe) {
74 return new GeoParsedResult(latitude, longitude, altitude, query);