Preserve query in geo URI

git-svn-id: https://zxing.googlecode.com/svn/trunk@1514 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2010-08-07 13:21:35 +00:00
parent ffb33d4c74
commit 0a5e8e9298
3 changed files with 39 additions and 8 deletions

View file

@ -24,12 +24,14 @@ public final class GeoParsedResult extends ParsedResult {
private final double latitude; private final double latitude;
private final double longitude; private final double longitude;
private final double altitude; private final double altitude;
private final String query;
GeoParsedResult(double latitude, double longitude, double altitude) { GeoParsedResult(double latitude, double longitude, double altitude, String query) {
super(ParsedResultType.GEO); super(ParsedResultType.GEO);
this.latitude = latitude; this.latitude = latitude;
this.longitude = longitude; this.longitude = longitude;
this.altitude = altitude; this.altitude = altitude;
this.query = query;
} }
public String getGeoURI() { public String getGeoURI() {
@ -42,6 +44,10 @@ public final class GeoParsedResult extends ParsedResult {
result.append(','); result.append(',');
result.append(altitude); result.append(altitude);
} }
if (query != null) {
result.append('?');
result.append(query);
}
return result.toString(); return result.toString();
} }
@ -66,8 +72,15 @@ public final class GeoParsedResult extends ParsedResult {
return altitude; return altitude;
} }
/**
* @return query string associated with geo URI or null if none exists
*/
public String getQuery() {
return query;
}
public String getDisplayResult() { public String getDisplayResult() {
StringBuffer result = new StringBuffer(); StringBuffer result = new StringBuffer(20);
result.append(latitude); result.append(latitude);
result.append(", "); result.append(", ");
result.append(longitude); result.append(longitude);
@ -76,6 +89,11 @@ public final class GeoParsedResult extends ParsedResult {
result.append(altitude); result.append(altitude);
result.append('m'); result.append('m');
} }
if (query != null) {
result.append(" (");
result.append(query);
result.append(')');
}
return result.toString(); return result.toString();
} }

View file

@ -38,7 +38,15 @@ final class GeoResultParser extends ResultParser {
} }
// Drop geo, query portion // Drop geo, query portion
int queryStart = rawText.indexOf('?', 4); int queryStart = rawText.indexOf('?', 4);
String geoURIWithoutQuery = queryStart < 0 ? rawText.substring(4) : rawText.substring(4, queryStart); String query;
String geoURIWithoutQuery;
if (queryStart < 0) {
query = null;
geoURIWithoutQuery = rawText.substring(4);
} else {
query = rawText.substring(queryStart + 1);
geoURIWithoutQuery = rawText.substring(4, queryStart);
}
int latitudeEnd = geoURIWithoutQuery.indexOf(','); int latitudeEnd = geoURIWithoutQuery.indexOf(',');
if (latitudeEnd < 0) { if (latitudeEnd < 0) {
return null; return null;
@ -63,7 +71,7 @@ final class GeoResultParser extends ResultParser {
} catch (NumberFormatException nfe) { } catch (NumberFormatException nfe) {
return null; return null;
} }
return new GeoParsedResult(latitude, longitude, altitude); return new GeoParsedResult(latitude, longitude, altitude, query);
} }
} }

View file

@ -28,12 +28,17 @@ import junit.framework.TestCase;
public final class GeoParsedResultTestCase extends TestCase { public final class GeoParsedResultTestCase extends TestCase {
public void testGeo() { public void testGeo() {
doTest("geo:1,2", 1.0, 2.0, 0.0); doTest("geo:1,2", 1.0, 2.0, 0.0, null);
doTest("geo:80.33,-32.3344,3.35", 80.33, -32.3344, 3.35); doTest("geo:80.33,-32.3344,3.35", 80.33, -32.3344, 3.35, null);
doTest("geo:-20.33,132.3344,0.01", -20.33, 132.3344, 0.01); doTest("geo:-20.33,132.3344,0.01", -20.33, 132.3344, 0.01, null);
doTest("geo:-20.33,132.3344,0.01?q=foobar", -20.33, 132.3344, 0.01, "q=foobar");
} }
private static void doTest(String contents, double latitude, double longitude, double altitude) { private static void doTest(String contents,
double latitude,
double longitude,
double altitude,
String query) {
Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE); Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
ParsedResult result = ResultParser.parseResult(fakeResult); ParsedResult result = ResultParser.parseResult(fakeResult);
assertSame(ParsedResultType.GEO, result.getType()); assertSame(ParsedResultType.GEO, result.getType());