Issue 742 at least parse iCal GEO in core library

git-svn-id: https://zxing.googlecode.com/svn/trunk@1787 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2011-05-20 07:12:49 +00:00
parent ce5378194a
commit 2d7ef9faf4
3 changed files with 88 additions and 9 deletions

View file

@ -27,6 +27,8 @@ public final class CalendarParsedResult extends ParsedResult {
private final String location; private final String location;
private final String attendee; private final String attendee;
private final String description; private final String description;
private final double latitude;
private final double longitude;
public CalendarParsedResult(String summary, public CalendarParsedResult(String summary,
String start, String start,
@ -34,6 +36,17 @@ public final class CalendarParsedResult extends ParsedResult {
String location, String location,
String attendee, String attendee,
String description) { String description) {
this(summary, start, end, location, attendee, description, Double.NaN, Double.NaN);
}
public CalendarParsedResult(String summary,
String start,
String end,
String location,
String attendee,
String description,
double latitude,
double longitude) {
super(ParsedResultType.CALENDAR); super(ParsedResultType.CALENDAR);
// Start is required, end is not // Start is required, end is not
if (start == null) { if (start == null) {
@ -51,6 +64,8 @@ public final class CalendarParsedResult extends ParsedResult {
this.location = location; this.location = location;
this.attendee = attendee; this.attendee = attendee;
this.description = description; this.description = description;
this.latitude = latitude;
this.longitude = longitude;
} }
public String getSummary() { public String getSummary() {
@ -87,6 +102,14 @@ public final class CalendarParsedResult extends ParsedResult {
return description; return description;
} }
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
public String getDisplayResult() { public String getDisplayResult() {
StringBuffer result = new StringBuffer(100); StringBuffer result = new StringBuffer(100);
maybeAppend(summary, result); maybeAppend(summary, result);

View file

@ -20,7 +20,7 @@ import com.google.zxing.Result;
/** /**
* Partially implements the iCalendar format's "VEVENT" format for specifying a * Partially implements the iCalendar format's "VEVENT" format for specifying a
* calendar event. See RFC 2445. This supports SUMMARY, LOCATION, DTSTART and DTEND fields. * calendar event. See RFC 2445. This supports SUMMARY, LOCATION, GEO, DTSTART and DTEND fields.
* *
* @author Sean Owen * @author Sean Owen
*/ */
@ -44,8 +44,21 @@ final class VEventResultParser extends ResultParser {
String end = VCardResultParser.matchSingleVCardPrefixedField("DTEND", rawText, true); String end = VCardResultParser.matchSingleVCardPrefixedField("DTEND", rawText, true);
String location = VCardResultParser.matchSingleVCardPrefixedField("LOCATION", rawText, true); String location = VCardResultParser.matchSingleVCardPrefixedField("LOCATION", rawText, true);
String description = VCardResultParser.matchSingleVCardPrefixedField("DESCRIPTION", rawText, true); String description = VCardResultParser.matchSingleVCardPrefixedField("DESCRIPTION", rawText, true);
String geoString = VCardResultParser.matchSingleVCardPrefixedField("GEO", rawText, true);
double latitude;
double longitude;
if (geoString == null) {
latitude = Double.NaN;
longitude = Double.NaN;
} else {
int semicolon = geoString.indexOf(';');
latitude = Double.parseDouble(geoString.substring(0, semicolon));
longitude = Double.parseDouble(geoString.substring(semicolon + 1));
}
try { try {
return new CalendarParsedResult(summary, start, end, location, null, description); return new CalendarParsedResult(summary, start, end, location, null, description, latitude, longitude);
} catch (IllegalArgumentException iae) { } catch (IllegalArgumentException iae) {
return null; return null;
} }

View file

@ -28,6 +28,8 @@ import org.junit.Test;
*/ */
public final class CalendarParsedResultTestCase extends Assert { public final class CalendarParsedResultTestCase extends Assert {
private static final double EPSILON = 0.0000000001;
@Test @Test
public void testStartEnd() { public void testStartEnd() {
doTest( doTest(
@ -35,7 +37,17 @@ public final class CalendarParsedResultTestCase extends Assert {
"DTSTART:20080504T123456Z\r\n" + "DTSTART:20080504T123456Z\r\n" +
"DTEND:20080505T234555Z\r\n" + "DTEND:20080505T234555Z\r\n" +
"END:VEVENT\r\nEND:VCALENDAR", "END:VEVENT\r\nEND:VCALENDAR",
null, null, null, "20080504T123456Z", "20080505T234555Z", null); null, null, null, "20080504T123456Z", "20080505T234555Z");
}
@Test
public void testNoVCalendar() {
doTest(
"BEGIN:VEVENT\r\n" +
"DTSTART:20080504T123456Z\r\n" +
"DTEND:20080505T234555Z\r\n" +
"END:VEVENT",
null, null, null, "20080504T123456Z", "20080505T234555Z");
} }
@Test @Test
@ -44,7 +56,7 @@ public final class CalendarParsedResultTestCase extends Assert {
"BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" + "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
"DTSTART:20080504T123456Z\r\n" + "DTSTART:20080504T123456Z\r\n" +
"END:VEVENT\r\nEND:VCALENDAR", "END:VEVENT\r\nEND:VCALENDAR",
null, null, null, "20080504T123456Z", "20080504T123456Z", null); null, null, null, "20080504T123456Z", "20080504T123456Z");
} }
@Test @Test
@ -54,7 +66,7 @@ public final class CalendarParsedResultTestCase extends Assert {
"SUMMARY:foo\r\n" + "SUMMARY:foo\r\n" +
"DTSTART:20080504T123456Z\r\n" + "DTSTART:20080504T123456Z\r\n" +
"END:VEVENT\r\nEND:VCALENDAR", "END:VEVENT\r\nEND:VCALENDAR",
null, "foo", null, "20080504T123456Z", "20080504T123456Z", null); null, "foo", null, "20080504T123456Z", "20080504T123456Z");
} }
@Test @Test
@ -64,7 +76,7 @@ public final class CalendarParsedResultTestCase extends Assert {
"LOCATION:Miami\r\n" + "LOCATION:Miami\r\n" +
"DTSTART:20080504T123456Z\r\n" + "DTSTART:20080504T123456Z\r\n" +
"END:VEVENT\r\nEND:VCALENDAR", "END:VEVENT\r\nEND:VCALENDAR",
null, null, "Miami", "20080504T123456Z", "20080504T123456Z", null); null, null, "Miami", "20080504T123456Z", "20080504T123456Z");
} }
@Test @Test
@ -74,13 +86,32 @@ public final class CalendarParsedResultTestCase extends Assert {
"DTSTART:20080504T123456Z\r\n" + "DTSTART:20080504T123456Z\r\n" +
"DESCRIPTION:This is a test\r\n" + "DESCRIPTION:This is a test\r\n" +
"END:VEVENT\r\nEND:VCALENDAR", "END:VEVENT\r\nEND:VCALENDAR",
"This is a test", null, null, "20080504T123456Z", "20080504T123456Z", null); "This is a test", null, null, "20080504T123456Z", "20080504T123456Z");
doTest( doTest(
"BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" + "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
"DTSTART:20080504T123456Z\r\n" + "DTSTART:20080504T123456Z\r\n" +
"DESCRIPTION:This is a test\r\n\t with a continuation\r\n" + "DESCRIPTION:This is a test\r\n\t with a continuation\r\n" +
"END:VEVENT\r\nEND:VCALENDAR", "END:VEVENT\r\nEND:VCALENDAR",
"This is a test with a continuation", null, null, "20080504T123456Z", "20080504T123456Z", null); "This is a test with a continuation", null, null, "20080504T123456Z", "20080504T123456Z");
}
@Test
public void testGeo() {
doTest(
"BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
"DTSTART:20080504T123456Z\r\n" +
"GEO:-12.345;-45.678\r\n" +
"END:VEVENT\r\nEND:VCALENDAR",
null, null, null, "20080504T123456Z", "20080504T123456Z", null, -12.345, -45.678);
}
private static void doTest(String contents,
String description,
String summary,
String location,
String start,
String end) {
doTest(contents, description, summary, location, start, end, null, Double.NaN, Double.NaN);
} }
private static void doTest(String contents, private static void doTest(String contents,
@ -89,7 +120,9 @@ public final class CalendarParsedResultTestCase extends Assert {
String location, String location,
String start, String start,
String end, String end,
String attendee) { String attendee,
double latitude,
double longitude) {
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.CALENDAR, result.getType()); assertSame(ParsedResultType.CALENDAR, result.getType());
@ -100,6 +133,16 @@ public final class CalendarParsedResultTestCase extends Assert {
assertEquals(start, calResult.getStart()); assertEquals(start, calResult.getStart());
assertEquals(end, calResult.getEnd()); assertEquals(end, calResult.getEnd());
assertEquals(attendee, calResult.getAttendee()); assertEquals(attendee, calResult.getAttendee());
assertEqualOrNaN(latitude, calResult.getLatitude());
assertEqualOrNaN(longitude, calResult.getLongitude());
}
private static void assertEqualOrNaN(double expected, double actual) {
if (Double.isNaN(expected)) {
assertTrue(Double.isNaN(actual));
} else {
assertEquals(expected, actual, EPSILON);
}
} }
} }