Add description support, as well as handle VEVENT line continuation. Get rid of unuse...
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 13 Apr 2010 10:46:31 +0000 (10:46 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 13 Apr 2010 10:46:31 +0000 (10:46 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@1304 59b500cc-1b3d-0410-9834-0bbf25fbcc57

android/src/com/google/zxing/client/android/result/CalendarResultHandler.java
android/src/com/google/zxing/client/android/result/ResultHandler.java
core/src/com/google/zxing/client/result/CalendarParsedResult.java
core/src/com/google/zxing/client/result/VCardResultParser.java
core/src/com/google/zxing/client/result/VEventResultParser.java
core/test/src/com/google/zxing/client/result/CalendarParsedResultTestCase.java

index 428a815..76c9cd3 100644 (file)
@@ -35,6 +35,7 @@ import java.util.GregorianCalendar;
  * @author dswitkin@google.com (Daniel Switkin)
  */
 public final class CalendarResultHandler extends ResultHandler {
+
   private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
   private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
 
@@ -63,7 +64,8 @@ public final class CalendarResultHandler extends ResultHandler {
       addCalendarEvent(calendarResult.getSummary(),
                        calendarResult.getStart(),
                        calendarResult.getEnd(),
-                       calendarResult.getLocation());
+                       calendarResult.getLocation(),
+                       calendarResult.getDescription());
     }
   }
 
@@ -83,7 +85,7 @@ public final class CalendarResultHandler extends ResultHandler {
 
     ParsedResult.maybeAppend(calResult.getLocation(), result);
     ParsedResult.maybeAppend(calResult.getAttendee(), result);
-    ParsedResult.maybeAppend(calResult.getTitle(), result);
+    ParsedResult.maybeAppend(calResult.getDescription(), result);
     return result.toString();
   }
 
index 2492f0c..97e57e1 100644 (file)
@@ -144,8 +144,13 @@ public abstract class ResultHandler {
    * @param start   The start time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
    * @param end     The end time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
    * @param location a text description of the event location
+   * @param description a text description of the event itself
    */
-  final void addCalendarEvent(String summary, String start, String end, String location) {
+  final void addCalendarEvent(String summary, 
+                              String start,
+                              String end,
+                              String location,
+                              String description) {
     Intent intent = new Intent(Intent.ACTION_EDIT);
     intent.setType("vnd.android.cursor.item/event");
     intent.putExtra("beginTime", calculateMilliseconds(start));
@@ -158,6 +163,7 @@ public abstract class ResultHandler {
     intent.putExtra("endTime", calculateMilliseconds(end));
     intent.putExtra("title", summary);
     intent.putExtra("eventLocation", location);
+    intent.putExtra("description", description);
     launchIntent(intent);
   }
 
index 00280d3..e2e4531 100644 (file)
@@ -26,27 +26,31 @@ public final class CalendarParsedResult extends ParsedResult {
   private final String end;
   private final String location;
   private final String attendee;
-  private final String title;
+  private final String description;
 
   public CalendarParsedResult(String summary,
                               String start,
                               String end,
                               String location,
                               String attendee,
-                              String title) {
+                              String description) {
     super(ParsedResultType.CALENDAR);
     // Start is required, end is not
     if (start == null) {
       throw new IllegalArgumentException();
     }
     validateDate(start);
-    validateDate(end);
+    if (end == null) {
+      end = start;
+    } else {
+      validateDate(end);
+    }
     this.summary = summary;
     this.start = start;
     this.end = end;
     this.location = location;
     this.attendee = attendee;
-    this.title = title;
+    this.description = description;
   }
 
   public String getSummary() {
@@ -79,8 +83,8 @@ public final class CalendarParsedResult extends ParsedResult {
     return attendee;
   }
 
-  public String getTitle() {
-    return title;
+  public String getDescription() {
+    return description;
   }
 
   public String getDisplayResult() {
@@ -90,7 +94,7 @@ public final class CalendarParsedResult extends ParsedResult {
     maybeAppend(end, result);
     maybeAppend(location, result);
     maybeAppend(attendee, result);
-    maybeAppend(title, result);
+    maybeAppend(description, result);
     return result.toString();
   }
 
index 0da0ebf..b43aa4f 100644 (file)
@@ -88,7 +88,12 @@ final class VCardResultParser extends ResultParser {
       }
       i++; // skip colon
       int start = i; // Found the start of a match here
-      i = rawText.indexOf((int) '\n', i); // Really, ends in \r\n
+      while ((i = rawText.indexOf((int) '\n', i)) >= 0 &&  // Really, ends in \r\n
+             i < rawText.length() - 1 &&           // But if followed by tab or space,
+             (rawText.charAt(i+1) == ' ' ||        // this is only a continuation
+              rawText.charAt(i+1) == '\t')) {
+        i += 2;
+      }
       if (i < 0) {
         // No terminating end character? uh, done. Set i such that loop terminates and break
         i = max;
@@ -97,10 +102,14 @@ final class VCardResultParser extends ResultParser {
         if (matches == null) {
           matches = new Vector(3); // lazy init
         }
+        if (rawText.charAt(i-1) == '\r') {
+          i--; // Back up over \r, which really should be there
+        }
         String element = rawText.substring(start, i);
         if (trim) {
           element = element.trim();
         }
+        element = stripContinuationCRLF(element);
         matches.addElement(element);
         i++;
       } else {
@@ -113,6 +122,30 @@ final class VCardResultParser extends ResultParser {
     return toStringArray(matches);
   }
 
+  private static String stripContinuationCRLF(String value) {
+    int length = value.length();
+    StringBuffer result = new StringBuffer(length);
+    boolean lastWasLF = false;
+    for (int i = 0; i < length; i++) {
+      if (lastWasLF) {
+        lastWasLF = false;
+        continue;
+      }
+      char c = value.charAt(i);
+      lastWasLF = false;
+      switch (c) {
+        case '\n':
+          lastWasLF = true;
+          break;
+        case '\r':
+          break;
+        default:
+          result.append(c);
+      }
+    }
+    return result.toString();
+  }
+
   static String matchSingleVCardPrefixedField(String prefix, String rawText, boolean trim) {
     String[] values = matchVCardPrefixedField(prefix, rawText, trim);
     return values == null ? null : values[0];
index 69f4d5d..96f263e 100644 (file)
@@ -47,8 +47,9 @@ final class VEventResultParser extends ResultParser {
     String start = VCardResultParser.matchSingleVCardPrefixedField("DTSTART", rawText, true);
     String end = VCardResultParser.matchSingleVCardPrefixedField("DTEND", rawText, true);
     String location = VCardResultParser.matchSingleVCardPrefixedField("LOCATION", rawText, true);
+    String description = VCardResultParser.matchSingleVCardPrefixedField("DESCRIPTION", rawText, true);
     try {
-      return new CalendarParsedResult(summary, start, end, location, null, null);
+      return new CalendarParsedResult(summary, start, end, location, null, description);
     } catch (IllegalArgumentException iae) {
       return null;
     }
index 11506c4..ec257e3 100644 (file)
@@ -27,16 +27,58 @@ import junit.framework.TestCase;
  */
 public final class CalendarParsedResultTestCase extends TestCase {
 
-  public void testVEvent() {
+  public void testStartEnd() {
     doTest(
-        "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456Z\r\n" +
-        "DTEND:20080505T234555Z\r\nLOCATION:Miami\r\n" +
+        "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
+        "DTSTART:20080504T123456Z\r\n" +
+        "DTEND:20080505T234555Z\r\n" +
         "END:VEVENT\r\nEND:VCALENDAR",
-        null, "foo", "Miami", "20080504T123456Z", "20080505T234555Z", null);
+        null, null, null, "20080504T123456Z", "20080505T234555Z", null);
+  }
+
+  public void testStart() {
+    doTest(
+        "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
+        "DTSTART:20080504T123456Z\r\n" +
+        "END:VEVENT\r\nEND:VCALENDAR",
+        null, null, null, "20080504T123456Z", "20080504T123456Z", null);
+  }
+
+  public void testSummary() {
+    doTest(
+        "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
+        "SUMMARY:foo\r\n" +
+        "DTSTART:20080504T123456Z\r\n" +
+        "END:VEVENT\r\nEND:VCALENDAR",
+        null, "foo", null, "20080504T123456Z", "20080504T123456Z", null);
+  }
+
+  public void testLocation() {
+    doTest(
+        "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
+        "LOCATION:Miami\r\n" +
+        "DTSTART:20080504T123456Z\r\n" +
+        "END:VEVENT\r\nEND:VCALENDAR",
+        null, null, "Miami", "20080504T123456Z", "20080504T123456Z", null);
+  }
+
+  public void testDescription() {
+    doTest(
+        "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
+        "DTSTART:20080504T123456Z\r\n" +
+        "DESCRIPTION:This is a test\r\n" +
+        "END:VEVENT\r\nEND:VCALENDAR",
+        "This is a test", null, null, "20080504T123456Z", "20080504T123456Z", null);
+    doTest(
+        "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
+        "DTSTART:20080504T123456Z\r\n" +
+        "DESCRIPTION:This is a test\r\n\t with a continuation\r\n" +        
+        "END:VEVENT\r\nEND:VCALENDAR",
+        "This is a test with a continuation", null, null, "20080504T123456Z", "20080504T123456Z", null);
   }
 
   private static void doTest(String contents,
-                             String title,
+                             String description,
                              String summary,
                              String location,
                              String start,
@@ -46,7 +88,7 @@ public final class CalendarParsedResultTestCase extends TestCase {
     ParsedResult result = ResultParser.parseResult(fakeResult);
     assertSame(ParsedResultType.CALENDAR, result.getType());
     CalendarParsedResult calResult = (CalendarParsedResult) result;
-    assertEquals(title, calResult.getTitle());
+    assertEquals(description, calResult.getDescription());
     assertEquals(summary, calResult.getSummary());
     assertEquals(location, calResult.getLocation());
     assertEquals(start, calResult.getStart());