0cd392510c3138a607d1faaf8ef7e25c7430daa5
[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 title;
30
31   public CalendarParsedResult(String summary,
32                               String start,
33                               String end,
34                               String location,
35                               String attendee,
36                               String title) {
37     super(ParsedResultType.CALENDAR);
38     // Start is required, end is not
39     if (start == null) {
40       throw new IllegalArgumentException();
41     }
42     validateDate(start);
43     validateDate(end);
44     this.summary = summary;
45     this.start = start;
46     this.end = end;
47     this.location = location;
48     this.attendee = attendee;
49     this.title = title;
50   }
51
52   public String getSummary() {
53     return summary;
54   }
55
56   /**
57    * <p>We would return the start and end date as a {@link java.util.Date} except that this code
58    * needs to work under JavaME / MIDP and there is no date parsing library available there, such
59    * as <code>java.text.SimpleDateFormat</code>.</p> See validateDate() for the return format.
60    *
61    * @return start time formatted as a RFC 2445 DATE or DATE-TIME.</p>
62    */
63   public String getStart() {
64     return start;
65   }
66
67   /**
68    * @see #getStart(). May return null if the event has no duration.
69    */
70   public String getEnd() {
71     return end;
72   }
73
74   public String getLocation() {
75     return location;
76   }
77
78   public String getAttendee() {
79     return attendee;
80   }
81
82   public String getTitle() {
83     return title;
84   }
85
86   public String getDisplayResult() {
87     StringBuffer result = new StringBuffer();
88     maybeAppend(summary, result);
89     maybeAppend(start, result);
90     maybeAppend(end, result);
91     maybeAppend(location, result);
92     maybeAppend(attendee, result);
93     maybeAppend(title, result);
94     return result.toString();
95   }
96
97   /**
98    * RFC 2445 allows the start and end fields to be of type DATE (e.g. 20081021) or DATE-TIME
99    * (e.g. 20081021T123000 for local time, or 20081021T123000Z for UTC).
100    *
101    * @param date The string to validate
102    */
103   private static void validateDate(String date) {
104     if (date != null) {
105       int length = date.length();
106       if (length != 8 && length != 15 && length != 16) {
107         throw new IllegalArgumentException();
108       }
109       for (int i = 0; i < 8; i++) {
110         if (!Character.isDigit(date.charAt(i))) {
111           throw new IllegalArgumentException();
112         }
113       }
114       if (length > 8) {
115         if (date.charAt(8) != 'T') {
116           throw new IllegalArgumentException();
117         }
118         for (int i = 9; i < 15; i++) {
119           if (!Character.isDigit(date.charAt(i))) {
120             throw new IllegalArgumentException();
121           }
122         }
123         if (length == 16 && date.charAt(15) != 'Z') {
124           throw new IllegalArgumentException();
125         }
126       }
127     }
128   }
129
130 }