Add description support, as well as handle VEVENT line continuation. Get rid of unuse...
[zxing.git] / core / src / com / google / zxing / client / result / CalendarParsedResult.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.result;
18
19 /**
20  * @author Sean Owen
21  */
22 public final class CalendarParsedResult extends ParsedResult {
23
24   private final String summary;
25   private final String start;
26   private final String end;
27   private final String location;
28   private final String attendee;
29   private final String description;
30
31   public CalendarParsedResult(String summary,
32                               String start,
33                               String end,
34                               String location,
35                               String attendee,
36                               String description) {
37     super(ParsedResultType.CALENDAR);
38     // Start is required, end is not
39     if (start == null) {
40       throw new IllegalArgumentException();
41     }
42     validateDate(start);
43     if (end == null) {
44       end = start;
45     } else {
46       validateDate(end);
47     }
48     this.summary = summary;
49     this.start = start;
50     this.end = end;
51     this.location = location;
52     this.attendee = attendee;
53     this.description = description;
54   }
55
56   public String getSummary() {
57     return summary;
58   }
59
60   /**
61    * <p>We would return the start and end date as a {@link java.util.Date} except that this code
62    * needs to work under JavaME / MIDP and there is no date parsing library available there, such
63    * as <code>java.text.SimpleDateFormat</code>.</p> See validateDate() for the return format.
64    *
65    * @return start time formatted as a RFC 2445 DATE or DATE-TIME.</p>
66    */
67   public String getStart() {
68     return start;
69   }
70
71   /**
72    * @see #getStart(). May return null if the event has no duration.
73    */
74   public String getEnd() {
75     return end;
76   }
77
78   public String getLocation() {
79     return location;
80   }
81
82   public String getAttendee() {
83     return attendee;
84   }
85
86   public String getDescription() {
87     return description;
88   }
89
90   public String getDisplayResult() {
91     StringBuffer result = new StringBuffer(100);
92     maybeAppend(summary, result);
93     maybeAppend(start, result);
94     maybeAppend(end, result);
95     maybeAppend(location, result);
96     maybeAppend(attendee, result);
97     maybeAppend(description, result);
98     return result.toString();
99   }
100
101   /**
102    * RFC 2445 allows the start and end fields to be of type DATE (e.g. 20081021) or DATE-TIME
103    * (e.g. 20081021T123000 for local time, or 20081021T123000Z for UTC).
104    *
105    * @param date The string to validate
106    */
107   private static void validateDate(String date) {
108     if (date != null) {
109       int length = date.length();
110       if (length != 8 && length != 15 && length != 16) {
111         throw new IllegalArgumentException();
112       }
113       for (int i = 0; i < 8; i++) {
114         if (!Character.isDigit(date.charAt(i))) {
115           throw new IllegalArgumentException();
116         }
117       }
118       if (length > 8) {
119         if (date.charAt(8) != 'T') {
120           throw new IllegalArgumentException();
121         }
122         for (int i = 9; i < 15; i++) {
123           if (!Character.isDigit(date.charAt(i))) {
124             throw new IllegalArgumentException();
125           }
126         }
127         if (length == 16 && date.charAt(15) != 'Z') {
128           throw new IllegalArgumentException();
129         }
130       }
131     }
132   }
133
134 }