Fix Issue 143, failure on invalid geo: URIs
[zxing.git] / core / src / com / google / zxing / client / result / GeoResultParser.java
index 0335cd0..b4a0325 100644 (file)
@@ -24,7 +24,7 @@ import com.google.zxing.Result;
  * <a href="http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00">
  * http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00</a>.
  *
- * @author srowen@google.com (Sean Owen)
+ * @author Sean Owen
  */
 final class GeoResultParser extends ResultParser {
 
@@ -33,7 +33,7 @@ final class GeoResultParser extends ResultParser {
 
   public static GeoParsedResult parse(Result result) {
     String rawText = result.getText();
-    if (rawText == null || !rawText.startsWith("geo:")) {
+    if (rawText == null || (!rawText.startsWith("geo:") && !rawText.startsWith("GEO:"))) {
       return null;
     }
     // Drop geo, query portion
@@ -48,16 +48,19 @@ final class GeoResultParser extends ResultParser {
     if (latitudeEnd < 0) {
       return null;
     }
-    float latitude = Float.parseFloat(geoURIWithoutQuery.substring(0, latitudeEnd));
-    int longitudeEnd = geoURIWithoutQuery.indexOf(',', latitudeEnd + 1);
-    float longitude;
-    float altitude; // in meters
-    if (longitudeEnd < 0) {
-      longitude = Float.parseFloat(geoURIWithoutQuery.substring(latitudeEnd + 1));
-      altitude = 0.0f;
-    } else {
-      longitude = Float.parseFloat(geoURIWithoutQuery.substring(latitudeEnd + 1, longitudeEnd));
-      altitude = Float.parseFloat(geoURIWithoutQuery.substring(longitudeEnd + 1));
+    int longitudeEnd = geoURIWithoutQuery.indexOf(',', latitudeEnd + 1);    
+    double latitude, longitude, altitude;
+    try {
+      latitude = Double.parseDouble(geoURIWithoutQuery.substring(0, latitudeEnd));
+      if (longitudeEnd < 0) {
+        longitude = Double.parseDouble(geoURIWithoutQuery.substring(latitudeEnd + 1));
+        altitude = 0.0;
+      } else {
+        longitude = Double.parseDouble(geoURIWithoutQuery.substring(latitudeEnd + 1, longitudeEnd));
+        altitude = Double.parseDouble(geoURIWithoutQuery.substring(longitudeEnd + 1));
+      }
+    } catch (NumberFormatException nfe) {
+      return null;
     }
     return new GeoParsedResult(rawText, latitude, longitude, altitude);
   }