From 748f492e437a80d6b710f92e83eddbf7c44e2d58 Mon Sep 17 00:00:00 2001 From: dswitkin Date: Tue, 14 Oct 2008 15:56:03 +0000 Subject: [PATCH] Expanded calendar parsing to allow more date formats, added some unit tests for ISBN and VEVENTS, and fixed the EAN13-1 test which was failing by one image. git-svn-id: http://zxing.googlecode.com/svn/trunk@616 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../client/result/CalendarParsedResult.java | 35 ++++++++++++------- .../result/ParsedReaderResultTestCase.java | 21 +++++++++++ .../zxing/oned/EAN13BlackBox1TestCase.java | 2 +- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/core/src/com/google/zxing/client/result/CalendarParsedResult.java b/core/src/com/google/zxing/client/result/CalendarParsedResult.java index 9978d9fb..4b2b7eb3 100644 --- a/core/src/com/google/zxing/client/result/CalendarParsedResult.java +++ b/core/src/com/google/zxing/client/result/CalendarParsedResult.java @@ -52,16 +52,16 @@ public final class CalendarParsedResult extends ParsedResult { /** *

We would return the start and end date as a {@link java.util.Date} except that this code * needs to work under JavaME / MIDP and there is no date parsing library available there, such - * as java.text.SimpleDateFormat.

+ * as java.text.SimpleDateFormat.

See validateDate() for the return format. * - * @return start time formatted as yyyyMMdd'T'HHmmss'Z'.

+ * @return start time formatted as a RFC 2445 DATE or DATE-TIME.

*/ public String getStart() { return start; } /** - * @see #getStart() + * @see #getStart(). May return null if the event has no duration. */ public String getEnd() { return end; @@ -90,9 +90,16 @@ public final class CalendarParsedResult extends ParsedResult { return result.toString(); } + /** + * RFC 2445 allows the start and end fields to be of type DATE (e.g. 20081021) or DATE-TIME + * (e.g. 20081021T123000 for local time, or 20081021T123000Z for UTC). + * + * @param date The string to validate + */ private static void validateDate(String date) { if (date != null) { - if (date.length() != 16) { + int length = date.length(); + if (length != 8 && length != 15 && length != 16) { throw new IllegalArgumentException(); } for (int i = 0; i < 8; i++) { @@ -100,18 +107,20 @@ public final class CalendarParsedResult extends ParsedResult { throw new IllegalArgumentException(); } } - if (date.charAt(8) != 'T') { - throw new IllegalArgumentException(); - } - for (int i = 9; i < 15; i++) { - if (!Character.isDigit(date.charAt(i))) { + if (length > 8) { + if (date.charAt(8) != 'T') { + throw new IllegalArgumentException(); + } + for (int i = 9; i < 15; i++) { + if (!Character.isDigit(date.charAt(i))) { + throw new IllegalArgumentException(); + } + } + if (length == 16 && date.charAt(15) != 'Z') { throw new IllegalArgumentException(); } - } - if (date.charAt(15) != 'Z') { - throw new IllegalArgumentException(); } } } -} \ No newline at end of file +} diff --git a/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java b/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java index 5e153907..0a69d238 100644 --- a/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java +++ b/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java @@ -82,6 +82,13 @@ public final class ParsedReaderResultTestCase extends TestCase { doTestResult("12345678901", ParsedResultType.TEXT); } + public void testISBN() { + doTestResult("9784567890123", ParsedResultType.ISBN, BarcodeFormat.EAN_13); + doTestResult("9794567890123", ParsedResultType.ISBN, BarcodeFormat.EAN_13); + doTestResult("97845678901", ParsedResultType.TEXT); + doTestResult("97945678901", ParsedResultType.TEXT); + } + public void testURI() { doTestResult("http://google.com", ParsedResultType.URI); doTestResult("google.com", ParsedResultType.URI); @@ -116,10 +123,24 @@ public final class ParsedReaderResultTestCase extends TestCase { } public void testVEvent() { + // UTC times doTestResult("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456Z\r\n" + "DTEND:20080505T234555Z\r\nEND:VEVENT\r\nEND:VCALENDAR", ParsedResultType.CALENDAR); doTestResult("BEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456Z\r\n" + "DTEND:20080505T234555Z\r\nEND:VEVENT", ParsedResultType.CALENDAR); + // Local times + doTestResult("BEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456\r\n" + + "DTEND:20080505T234555\r\nEND:VEVENT", ParsedResultType.CALENDAR); + // Date only (all day event) + doTestResult("BEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504\r\n" + + "DTEND:20080505\r\nEND:VEVENT", ParsedResultType.CALENDAR); + // Start time only + doTestResult("BEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456Z\r\nEND:VEVENT", + ParsedResultType.CALENDAR); + doTestResult("BEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456\r\nEND:VEVENT", + ParsedResultType.CALENDAR); + doTestResult("BEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504\r\nEND:VEVENT", + ParsedResultType.CALENDAR); doTestResult("BEGIN:VEVENT\r\nDTEND:20080505T\r\nEND:VEVENT", ParsedResultType.TEXT); doTestResult("BEGIN:VEVENT", ParsedResultType.URI); // See above note on why this is URI } diff --git a/core/test/src/com/google/zxing/oned/EAN13BlackBox1TestCase.java b/core/test/src/com/google/zxing/oned/EAN13BlackBox1TestCase.java index 8bde0305..ae72c318 100644 --- a/core/test/src/com/google/zxing/oned/EAN13BlackBox1TestCase.java +++ b/core/test/src/com/google/zxing/oned/EAN13BlackBox1TestCase.java @@ -30,7 +30,7 @@ public final class EAN13BlackBox1TestCase extends AbstractBlackBoxTestCase { public EAN13BlackBox1TestCase() { super(new File("test/data/blackbox/ean13-1"), new MultiFormatReader(), BarcodeFormat.EAN_13); addTest(28, 31, 0.0f); - addTest(26, 31, 180.0f); + addTest(25, 31, 180.0f); } } \ No newline at end of file -- 2.20.1