Add geo: URL support (oh and removed an old moved file)
[zxing.git] / core / src / com / google / zxing / client / result / GeoParsedResult.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  * Represents a "geo:" URI result. See
23  * <a href="http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00">
24  * http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00</a>.
25  *
26  * @author srowen@google.com (Sean Owen)
27  */
28 public final class GeoParsedResult extends ParsedReaderResult {
29
30   private final float latitude;
31   private final float longitude;
32   private final float altitude;
33
34   private GeoParsedResult(float latitude, float longitude, float altitude) {
35     super(ParsedReaderResultType.GEO);
36     this.latitude = latitude;
37     this.longitude = longitude;
38     this.altitude = altitude;
39   }
40
41   public static GeoParsedResult parse(Result result) {
42     String rawText = result.getText();
43     if (!rawText.startsWith("geo:")) {
44       return null;
45     }
46     // Drop geo, query portion
47     int queryStart = rawText.indexOf('?', 4);
48     if (queryStart < 0) {
49       rawText = rawText.substring(4);
50     } else {
51       rawText = rawText.substring(4, queryStart);
52     }
53     int latitudeEnd = rawText.indexOf(',');
54     if (latitudeEnd < 0) {
55       return null;
56     }
57     float latitude = Float.parseFloat(rawText.substring(0, latitudeEnd));
58     int longitudeEnd = rawText.indexOf(',', latitudeEnd + 1);
59     float longitude;
60     float altitude;
61     if (longitudeEnd < 0) {
62       longitude = Float.parseFloat(rawText.substring(latitudeEnd + 1));
63       altitude = 0.0f;
64     } else {
65       longitude = Float.parseFloat(rawText.substring(latitudeEnd + 1, longitudeEnd));
66       altitude = Float.parseFloat(rawText.substring(longitudeEnd + 1));
67     }
68     return new GeoParsedResult(latitude, longitude, altitude);
69   }
70
71   /**
72    * @return latitude in degrees
73    */
74   public float getLatitude() {
75     return latitude;
76   }
77
78   /**
79    * @return longitude in degrees
80    */
81   public float getLongitude() {
82     return longitude;
83   }
84
85   /**
86    * @return altitude in meters. If not specified, in the geo URI, returns 0.0
87    */
88   public float getAltitude() {
89     return altitude;
90   }
91
92   public String getDisplayResult() {
93     StringBuffer result = new StringBuffer(50);
94     result.append(latitude);
95     result.append("deg N, ");
96     result.append(longitude);
97     result.append("deg E, ");
98     result.append(altitude);
99     result.append('m');
100     return result.toString();
101   }
102
103 }