|
1 /** |
|
2 * Copyright (c) 2012, Ben Fortuna |
|
3 * All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * |
|
9 * o Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * o Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * o Neither the name of Ben Fortuna nor the names of any other contributors |
|
17 * may be used to endorse or promote products derived from this software |
|
18 * without specific prior written permission. |
|
19 * |
|
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 */ |
|
32 package net.fortuna.ical4j.model.property; |
|
33 |
|
34 import java.text.ParseException; |
|
35 |
|
36 import net.fortuna.ical4j.model.DateList; |
|
37 import net.fortuna.ical4j.model.Parameter; |
|
38 import net.fortuna.ical4j.model.ParameterList; |
|
39 import net.fortuna.ical4j.model.PeriodList; |
|
40 import net.fortuna.ical4j.model.PropertyFactoryImpl; |
|
41 import net.fortuna.ical4j.model.TimeZone; |
|
42 import net.fortuna.ical4j.model.ValidationException; |
|
43 import net.fortuna.ical4j.model.parameter.Value; |
|
44 import net.fortuna.ical4j.util.ParameterValidator; |
|
45 import net.fortuna.ical4j.util.Strings; |
|
46 |
|
47 /** |
|
48 * $Id$ |
|
49 * |
|
50 * Created: [Apr 6, 2004] |
|
51 * |
|
52 * Defines an RDATE iCalendar component property. |
|
53 * |
|
54 * <pre> |
|
55 * 4.8.5.3 Recurrence Date/Times |
|
56 * |
|
57 * Property Name: RDATE |
|
58 * |
|
59 * Purpose: This property defines the list of date/times for a |
|
60 * recurrence set. |
|
61 * |
|
62 * Value Type: The default value type for this property is DATE-TIME. |
|
63 * The value type can be set to DATE or PERIOD. |
|
64 * |
|
65 * Property Parameters: Non-standard, value data type and time zone |
|
66 * identifier property parameters can be specified on this property. |
|
67 * |
|
68 * Conformance: The property can be specified in "VEVENT", "VTODO", |
|
69 * "VJOURNAL" or "VTIMEZONE" calendar components. |
|
70 * |
|
71 * Description: This property can appear along with the "RRULE" property |
|
72 * to define an aggregate set of repeating occurrences. When they both |
|
73 * appear in an iCalendar object, the recurring events are defined by |
|
74 * the union of occurrences defined by both the "RDATE" and "RRULE". |
|
75 * |
|
76 * The recurrence dates, if specified, are used in computing the |
|
77 * recurrence set. The recurrence set is the complete set of recurrence |
|
78 * instances for a calendar component. The recurrence set is generated |
|
79 * by considering the initial "DTSTART" property along with the "RRULE", |
|
80 * "RDATE", "EXDATE" and "EXRULE" properties contained within the |
|
81 * iCalendar object. The "DTSTART" property defines the first instance |
|
82 * in the recurrence set. Multiple instances of the "RRULE" and "EXRULE" |
|
83 * properties can also be specified to define more sophisticated |
|
84 * recurrence sets. The final recurrence set is generated by gathering |
|
85 * all of the start date/times generated by any of the specified "RRULE" |
|
86 * and "RDATE" properties, and excluding any start date/times which fall |
|
87 * within the union of start date/times generated by any specified |
|
88 * "EXRULE" and "EXDATE" properties. This implies that start date/times |
|
89 * within exclusion related properties (i.e., "EXDATE" and "EXRULE") |
|
90 * take precedence over those specified by inclusion properties (i.e., |
|
91 * "RDATE" and "RRULE"). Where duplicate instances are generated by the |
|
92 * "RRULE" and "RDATE" properties, only one recurrence is considered. |
|
93 * Duplicate instances are ignored. |
|
94 * |
|
95 * Format Definition: The property is defined by the following notation: |
|
96 * |
|
97 * rdate = "RDATE" rdtparam ":" rdtval *("," rdtval) CRLF |
|
98 * |
|
99 * rdtparam = *( |
|
100 * |
|
101 * ; the following are optional, |
|
102 * ; but MUST NOT occur more than once |
|
103 * |
|
104 * (";" "VALUE" "=" ("DATE-TIME" |
|
105 * / "DATE" / "PERIOD")) / |
|
106 * (";" tzidparam) / |
|
107 * |
|
108 * ; the following is optional, |
|
109 * ; and MAY occur more than once |
|
110 * |
|
111 * (";" xparam) |
|
112 * |
|
113 * ) |
|
114 * |
|
115 * rdtval = date-time / date / period |
|
116 * ;Value MUST match value type |
|
117 * |
|
118 * Example: The following are examples of this property: |
|
119 * |
|
120 * RDATE:19970714T123000Z |
|
121 * |
|
122 * RDATE;TZID=US-EASTERN:19970714T083000 |
|
123 * |
|
124 * RDATE;VALUE=PERIOD:19960403T020000Z/19960403T040000Z, |
|
125 * 19960404T010000Z/PT3H |
|
126 * |
|
127 * RDATE;VALUE=DATE:19970101,19970120,19970217,19970421 |
|
128 * 19970526,19970704,19970901,19971014,19971128,19971129,19971225 |
|
129 * </pre> |
|
130 * |
|
131 * @author Ben Fortuna |
|
132 */ |
|
133 public class RDate extends DateListProperty { |
|
134 |
|
135 private static final long serialVersionUID = -3320381650013860193L; |
|
136 |
|
137 private PeriodList periods; |
|
138 |
|
139 /** |
|
140 * Default constructor. |
|
141 */ |
|
142 public RDate() { |
|
143 super(RDATE, PropertyFactoryImpl.getInstance()); |
|
144 periods = new PeriodList(false, true); |
|
145 } |
|
146 |
|
147 /** |
|
148 * @param aList a list of parameters for this component |
|
149 * @param aValue a value string for this component |
|
150 * @throws ParseException where the specified value string is not a valid date-time/date representation |
|
151 */ |
|
152 public RDate(final ParameterList aList, final String aValue) |
|
153 throws ParseException { |
|
154 super(RDATE, aList, PropertyFactoryImpl.getInstance()); |
|
155 periods = new PeriodList(false, true); |
|
156 setValue(aValue); |
|
157 } |
|
158 |
|
159 /** |
|
160 * Constructor. Date or Date-Time format is determined based on the presence of a VALUE parameter. |
|
161 * @param dates a list of dates |
|
162 */ |
|
163 public RDate(final DateList dates) { |
|
164 super(RDATE, dates, PropertyFactoryImpl.getInstance()); |
|
165 periods = new PeriodList(false, true); |
|
166 } |
|
167 |
|
168 /** |
|
169 * Constructor. Date or Date-Time format is determined based on the presence of a VALUE parameter. |
|
170 * @param aList a list of parameters for this component |
|
171 * @param dates a list of dates |
|
172 */ |
|
173 public RDate(final ParameterList aList, final DateList dates) { |
|
174 super(RDATE, aList, dates, PropertyFactoryImpl.getInstance()); |
|
175 periods = new PeriodList(false, true); |
|
176 } |
|
177 |
|
178 /** |
|
179 * Constructor. |
|
180 * @param periods a list of periods |
|
181 */ |
|
182 public RDate(final PeriodList periods) { |
|
183 super(RDATE, new DateList(true), PropertyFactoryImpl.getInstance()); |
|
184 this.periods = periods; |
|
185 } |
|
186 |
|
187 /** |
|
188 * Constructor. |
|
189 * @param aList a list of parameters for this component |
|
190 * @param periods a list of periods |
|
191 */ |
|
192 public RDate(final ParameterList aList, final PeriodList periods) { |
|
193 super(RDATE, aList, new DateList(true), PropertyFactoryImpl.getInstance()); |
|
194 this.periods = periods; |
|
195 } |
|
196 |
|
197 /** |
|
198 * {@inheritDoc} |
|
199 */ |
|
200 public final void validate() throws ValidationException { |
|
201 |
|
202 /* |
|
203 * ; the following are optional, ; but MUST NOT occur more than once (";" "VALUE" "=" ("DATE-TIME" / "DATE" / |
|
204 * "PERIOD")) / (";" tzidparam) / |
|
205 */ |
|
206 ParameterValidator.getInstance().assertOneOrLess(Parameter.VALUE, |
|
207 getParameters()); |
|
208 |
|
209 final Parameter valueParam = getParameter(Parameter.VALUE); |
|
210 |
|
211 if (valueParam != null && !Value.DATE_TIME.equals(valueParam) |
|
212 && !Value.DATE.equals(valueParam) |
|
213 && !Value.PERIOD.equals(valueParam)) { |
|
214 throw new ValidationException("Parameter [" + Parameter.VALUE |
|
215 + "] is invalid"); |
|
216 } |
|
217 |
|
218 ParameterValidator.getInstance().assertOneOrLess(Parameter.TZID, |
|
219 getParameters()); |
|
220 |
|
221 /* |
|
222 * ; the following is optional, ; and MAY occur more than once (";" xparam) |
|
223 */ |
|
224 } |
|
225 |
|
226 /** |
|
227 * @return Returns the period list. |
|
228 */ |
|
229 public final PeriodList getPeriods() { |
|
230 return periods; |
|
231 } |
|
232 |
|
233 /** |
|
234 * {@inheritDoc} |
|
235 */ |
|
236 public final void setValue(final String aValue) throws ParseException { |
|
237 if (Value.PERIOD.equals(getParameter(Parameter.VALUE))) { |
|
238 periods = new PeriodList(aValue); |
|
239 } |
|
240 else { |
|
241 super.setValue(aValue); |
|
242 } |
|
243 } |
|
244 |
|
245 /** |
|
246 * {@inheritDoc} |
|
247 */ |
|
248 public final String getValue() { |
|
249 if (periods != null && !(periods.isEmpty() && periods.isUnmodifiable())) { |
|
250 return Strings.valueOf(getPeriods()); |
|
251 } |
|
252 return super.getValue(); |
|
253 } |
|
254 |
|
255 /** |
|
256 * {@inheritDoc} |
|
257 */ |
|
258 public final void setTimeZone(TimeZone timezone) { |
|
259 if (periods != null && !(periods.isEmpty() && periods.isUnmodifiable())) { |
|
260 periods.setTimeZone(timezone); |
|
261 } |
|
262 else { |
|
263 super.setTimeZone(timezone); |
|
264 } |
|
265 } |
|
266 } |