Backed out last 'fix' to email address parsing -- isn't going to work. Needs a better...
[zxing.git] / core / src / com / google / zxing / client / result / GeoResultParser.java
1 /*
2  * Copyright 2008 ZXing authors
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  * Parses a "geo:" URI result, which specifices 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>.
26  *
27  * @author Sean Owen
28  */
29 final class GeoResultParser extends ResultParser {
30
31   private GeoResultParser() {
32   }
33
34   public static GeoParsedResult parse(Result result) {
35     String rawText = result.getText();
36     if (rawText == null || (!rawText.startsWith("geo:") && !rawText.startsWith("GEO:"))) {
37       return null;
38     }
39     // Drop geo, query portion
40     int queryStart = rawText.indexOf('?', 4);
41     String geoURIWithoutQuery;
42     if (queryStart < 0) {
43       geoURIWithoutQuery = rawText.substring(4);
44     } else {
45       geoURIWithoutQuery = rawText.substring(4, queryStart);
46     }
47     int latitudeEnd = geoURIWithoutQuery.indexOf(',');
48     if (latitudeEnd < 0) {
49       return null;
50     }
51     int longitudeEnd = geoURIWithoutQuery.indexOf(',', latitudeEnd + 1);    
52     double latitude, longitude, altitude;
53     try {
54       latitude = Double.parseDouble(geoURIWithoutQuery.substring(0, latitudeEnd));
55       if (longitudeEnd < 0) {
56         longitude = Double.parseDouble(geoURIWithoutQuery.substring(latitudeEnd + 1));
57         altitude = 0.0;
58       } else {
59         longitude = Double.parseDouble(geoURIWithoutQuery.substring(latitudeEnd + 1, longitudeEnd));
60         altitude = Double.parseDouble(geoURIWithoutQuery.substring(longitudeEnd + 1));
61       }
62     } catch (NumberFormatException nfe) {
63       return null;
64     }
65     return new GeoParsedResult(rawText, latitude, longitude, altitude);
66   }
67
68 }