Expanded calendar parsing to allow more date formats, added some unit tests for ISBN...
authordswitkin <dswitkin@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 14 Oct 2008 15:56:03 +0000 (15:56 +0000)
committerdswitkin <dswitkin@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 14 Oct 2008 15:56:03 +0000 (15:56 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@616 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/client/result/CalendarParsedResult.java
core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java
core/test/src/com/google/zxing/oned/EAN13BlackBox1TestCase.java

index 9978d9f..4b2b7eb 100644 (file)
@@ -52,16 +52,16 @@ public final class CalendarParsedResult extends ParsedResult {
   /**
    * <p>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 <code>java.text.SimpleDateFormat</code>.</p>
+   * as <code>java.text.SimpleDateFormat</code>.</p> See validateDate() for the return format.
    *
-   * @return start time formatted as yyyyMMdd'T'HHmmss'Z'.</p>
+   * @return start time formatted as a RFC 2445 DATE or DATE-TIME.</p>
    */
   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
+}
index 5e15390..0a69d23 100644 (file)
@@ -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
   }
index 8bde030..ae72c31 100644 (file)
@@ -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