1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/model/Component.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,415 @@ 1.4 +/** 1.5 + * Copyright (c) 2012, Ben Fortuna 1.6 + * All rights reserved. 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1.12 + * o Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * o Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * o Neither the name of Ben Fortuna nor the names of any other contributors 1.20 + * may be used to endorse or promote products derived from this software 1.21 + * without specific prior written permission. 1.22 + * 1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + */ 1.35 +package net.fortuna.ical4j.model; 1.36 + 1.37 +import java.io.IOException; 1.38 +import java.io.Serializable; 1.39 +import java.net.URISyntaxException; 1.40 +import java.text.ParseException; 1.41 +import java.util.Iterator; 1.42 + 1.43 +import net.fortuna.ical4j.model.parameter.Value; 1.44 +import net.fortuna.ical4j.model.property.DateProperty; 1.45 +import net.fortuna.ical4j.model.property.DtStart; 1.46 +import net.fortuna.ical4j.model.property.Duration; 1.47 +import net.fortuna.ical4j.model.property.ExDate; 1.48 +import net.fortuna.ical4j.model.property.ExRule; 1.49 +import net.fortuna.ical4j.model.property.RDate; 1.50 +import net.fortuna.ical4j.model.property.RRule; 1.51 +import net.fortuna.ical4j.util.Strings; 1.52 + 1.53 +import org.apache.commons.lang.builder.EqualsBuilder; 1.54 +import org.apache.commons.lang.builder.HashCodeBuilder; 1.55 + 1.56 +/** 1.57 + * $Id$ [Apr 5, 2004] 1.58 + * 1.59 + * Defines an iCalendar component. Subclasses of this class provide additional validation and typed values for specific 1.60 + * iCalendar components. 1.61 + * @author Ben Fortuna 1.62 + */ 1.63 +public abstract class Component implements Serializable { 1.64 + 1.65 + private static final long serialVersionUID = 4943193483665822201L; 1.66 + 1.67 + /** 1.68 + * Component start token. 1.69 + */ 1.70 + public static final String BEGIN = "BEGIN"; 1.71 + 1.72 + /** 1.73 + * Component end token. 1.74 + */ 1.75 + public static final String END = "END"; 1.76 + 1.77 + /** 1.78 + * Component token. 1.79 + */ 1.80 + public static final String VEVENT = "VEVENT"; 1.81 + 1.82 + /** 1.83 + * Component token. 1.84 + */ 1.85 + public static final String VTODO = "VTODO"; 1.86 + 1.87 + /** 1.88 + * Component token. 1.89 + */ 1.90 + public static final String VJOURNAL = "VJOURNAL"; 1.91 + 1.92 + /** 1.93 + * Component token. 1.94 + */ 1.95 + public static final String VFREEBUSY = "VFREEBUSY"; 1.96 + 1.97 + /** 1.98 + * Component token. 1.99 + */ 1.100 + public static final String VTIMEZONE = "VTIMEZONE"; 1.101 + 1.102 + /** 1.103 + * Component token. 1.104 + */ 1.105 + public static final String VALARM = "VALARM"; 1.106 + 1.107 + /** 1.108 + * Component token. 1.109 + */ 1.110 + public static final String VAVAILABILITY = "VAVAILABILITY"; 1.111 + 1.112 + /** 1.113 + * Component token. 1.114 + */ 1.115 + public static final String VVENUE = "VVENUE"; 1.116 + 1.117 + /** 1.118 + * Component token. 1.119 + */ 1.120 + public static final String AVAILABLE = "AVAILABLE"; 1.121 + 1.122 + /** 1.123 + * Prefix for non-standard components. 1.124 + */ 1.125 + public static final String EXPERIMENTAL_PREFIX = "X-"; 1.126 + 1.127 + private String name; 1.128 + 1.129 + private PropertyList properties; 1.130 + 1.131 + /** 1.132 + * Constructs a new component containing no properties. 1.133 + * @param s a component name 1.134 + */ 1.135 + protected Component(final String s) { 1.136 + this(s, new PropertyList()); 1.137 + } 1.138 + 1.139 + /** 1.140 + * Constructor made protected to enforce the use of <code>ComponentFactory</code> for component instantiation. 1.141 + * @param s component name 1.142 + * @param p a list of properties 1.143 + */ 1.144 + protected Component(final String s, final PropertyList p) { 1.145 + this.name = s; 1.146 + this.properties = p; 1.147 + } 1.148 + 1.149 + /** 1.150 + * {@inheritDoc} 1.151 + */ 1.152 + public String toString() { 1.153 + final StringBuffer buffer = new StringBuffer(); 1.154 + buffer.append(BEGIN); 1.155 + buffer.append(':'); 1.156 + buffer.append(getName()); 1.157 + buffer.append(Strings.LINE_SEPARATOR); 1.158 + buffer.append(getProperties()); 1.159 + buffer.append(END); 1.160 + buffer.append(':'); 1.161 + buffer.append(getName()); 1.162 + buffer.append(Strings.LINE_SEPARATOR); 1.163 + 1.164 + return buffer.toString(); 1.165 + } 1.166 + 1.167 + /** 1.168 + * @return Returns the name. 1.169 + */ 1.170 + public final String getName() { 1.171 + return name; 1.172 + } 1.173 + 1.174 + /** 1.175 + * @return Returns the properties. 1.176 + */ 1.177 + public final PropertyList getProperties() { 1.178 + return properties; 1.179 + } 1.180 + 1.181 + /** 1.182 + * Convenience method for retrieving a list of named properties. 1.183 + * @param name name of properties to retrieve 1.184 + * @return a property list containing only properties with the specified name 1.185 + */ 1.186 + public final PropertyList getProperties(final String name) { 1.187 + return getProperties().getProperties(name); 1.188 + } 1.189 + 1.190 + /** 1.191 + * Convenience method for retrieving a named property. 1.192 + * @param name name of the property to retrieve 1.193 + * @return the first matching property in the property list with the specified name 1.194 + */ 1.195 + public final Property getProperty(final String name) { 1.196 + return getProperties().getProperty(name); 1.197 + } 1.198 + 1.199 + /** 1.200 + * Perform validation on a component and its properties. 1.201 + * @throws ValidationException where the component is not in a valid state 1.202 + */ 1.203 + public final void validate() throws ValidationException { 1.204 + validate(true); 1.205 + } 1.206 + 1.207 + /** 1.208 + * Perform validation on a component. 1.209 + * @param recurse indicates whether to validate the component's properties 1.210 + * @throws ValidationException where the component is not in a valid state 1.211 + */ 1.212 + public abstract void validate(final boolean recurse) 1.213 + throws ValidationException; 1.214 + 1.215 + /** 1.216 + * Invoke validation on the component properties in its current state. 1.217 + * @throws ValidationException where any of the component properties is not in a valid state 1.218 + */ 1.219 + protected final void validateProperties() throws ValidationException { 1.220 + for (final Iterator i = getProperties().iterator(); i.hasNext();) { 1.221 + final Property property = (Property) i.next(); 1.222 + property.validate(); 1.223 + } 1.224 + } 1.225 + 1.226 + /** 1.227 + * {@inheritDoc} 1.228 + */ 1.229 + public boolean equals(final Object arg0) { 1.230 + if (arg0 instanceof Component) { 1.231 + final Component c = (Component) arg0; 1.232 + return new EqualsBuilder().append(getName(), c.getName()) 1.233 + .append(getProperties(), c.getProperties()).isEquals(); 1.234 + } 1.235 + return super.equals(arg0); 1.236 + } 1.237 + 1.238 + /** 1.239 + * {@inheritDoc} 1.240 + */ 1.241 + public int hashCode() { 1.242 + return new HashCodeBuilder().append(getName()).append(getProperties()) 1.243 + .toHashCode(); 1.244 + } 1.245 + 1.246 + /** 1.247 + * Create a (deep) copy of this component. 1.248 + * @return the component copy 1.249 + * @throws IOException where an error occurs reading the component data 1.250 + * @throws ParseException where parsing component data fails 1.251 + * @throws URISyntaxException where component data contains an invalid URI 1.252 + */ 1.253 + public Component copy() throws ParseException, IOException, 1.254 + URISyntaxException { 1.255 + 1.256 + // Deep copy properties.. 1.257 + final PropertyList newprops = new PropertyList(getProperties()); 1.258 + 1.259 + return ComponentFactory.getInstance().createComponent(getName(), 1.260 + newprops); 1.261 + } 1.262 + 1.263 + /** 1.264 + * Calculates the recurrence set for this component using the specified period. 1.265 + * The recurrence set is derived from a combination of the component start date, 1.266 + * recurrence rules and dates, and exception rules and dates. Note that component 1.267 + * transparency and anniversary-style dates do not affect the resulting 1.268 + * intersection. 1.269 + * <p>If an explicit DURATION is not specified, the effective duration of each 1.270 + * returned period is derived from the DTSTART and DTEND or DUE properties. 1.271 + * If the component has no DURATION, DTEND or DUE, the effective duration is set 1.272 + * to PT0S</p> 1.273 + * @param period a range to calculate recurrences for 1.274 + * @return a list of periods 1.275 + */ 1.276 + public final PeriodList calculateRecurrenceSet(final Period period) { 1.277 + 1.278 +// validate(); 1.279 + 1.280 + final PeriodList recurrenceSet = new PeriodList(); 1.281 + 1.282 + final DtStart start = (DtStart) getProperty(Property.DTSTART); 1.283 + DateProperty end = (DateProperty) getProperty(Property.DTEND); 1.284 + if (end == null) { 1.285 + end = (DateProperty) getProperty(Property.DUE); 1.286 + } 1.287 + Duration duration = (Duration) getProperty(Property.DURATION); 1.288 + 1.289 + // if no start date specified return empty list.. 1.290 + if (start == null) { 1.291 + return recurrenceSet; 1.292 + } 1.293 + 1.294 + final Value startValue = (Value) start.getParameter(Parameter.VALUE); 1.295 + 1.296 + // initialise timezone.. 1.297 +// if (startValue == null || Value.DATE_TIME.equals(startValue)) { 1.298 + if (start.isUtc()) { 1.299 + recurrenceSet.setUtc(true); 1.300 + } 1.301 + else if (start.getDate() instanceof DateTime) { 1.302 + recurrenceSet.setTimeZone(((DateTime) start.getDate()).getTimeZone()); 1.303 + } 1.304 + 1.305 + // if an explicit event duration is not specified, derive a value for recurring 1.306 + // periods from the end date.. 1.307 + Dur rDuration; 1.308 + // if no end or duration specified, end date equals start date.. 1.309 + if (end == null && duration == null) { 1.310 + rDuration = new Dur(start.getDate(), start.getDate()); 1.311 + } 1.312 + else if (duration == null) { 1.313 + rDuration = new Dur(start.getDate(), end.getDate()); 1.314 + } 1.315 + else { 1.316 + rDuration = duration.getDuration(); 1.317 + } 1.318 + 1.319 + // add recurrence dates.. 1.320 + for (final Iterator i = getProperties(Property.RDATE).iterator(); i.hasNext();) { 1.321 + final RDate rdate = (RDate) i.next(); 1.322 + final Value rdateValue = (Value) rdate.getParameter(Parameter.VALUE); 1.323 + if (Value.PERIOD.equals(rdateValue)) { 1.324 + for (final Iterator j = rdate.getPeriods().iterator(); j.hasNext();) { 1.325 + final Period rdatePeriod = (Period) j.next(); 1.326 + if (period.intersects(rdatePeriod)) { 1.327 + recurrenceSet.add(rdatePeriod); 1.328 + } 1.329 + } 1.330 + } 1.331 + else if (Value.DATE_TIME.equals(rdateValue)) { 1.332 + for (final Iterator j = rdate.getDates().iterator(); j.hasNext();) { 1.333 + final DateTime rdateTime = (DateTime) j.next(); 1.334 + if (period.includes(rdateTime)) { 1.335 + recurrenceSet.add(new Period(rdateTime, rDuration)); 1.336 + } 1.337 + } 1.338 + } 1.339 + else { 1.340 + for (final Iterator j = rdate.getDates().iterator(); j.hasNext();) { 1.341 + final Date rdateDate = (Date) j.next(); 1.342 + if (period.includes(rdateDate)) { 1.343 + recurrenceSet.add(new Period(new DateTime(rdateDate), rDuration)); 1.344 + } 1.345 + } 1.346 + } 1.347 + } 1.348 + 1.349 + // allow for recurrence rules that start prior to the specified period 1.350 + // but still intersect with it.. 1.351 + final DateTime startMinusDuration = new DateTime(period.getStart()); 1.352 + startMinusDuration.setTime(rDuration.negate().getTime( 1.353 + period.getStart()).getTime()); 1.354 + 1.355 + // add recurrence rules.. 1.356 + for (final Iterator i = getProperties(Property.RRULE).iterator(); i.hasNext();) { 1.357 + final RRule rrule = (RRule) i.next(); 1.358 + final DateList rruleDates = rrule.getRecur().getDates(start.getDate(), 1.359 + new Period(startMinusDuration, period.getEnd()), startValue); 1.360 + for (final Iterator j = rruleDates.iterator(); j.hasNext();) { 1.361 + final Date rruleDate = (Date) j.next(); 1.362 + recurrenceSet.add(new Period(new DateTime(rruleDate), rDuration)); 1.363 + } 1.364 + } 1.365 + 1.366 + // add initial instance if intersection with the specified period.. 1.367 + Period startPeriod = null; 1.368 + if (end != null) { 1.369 + startPeriod = new Period(new DateTime(start.getDate()), 1.370 + new DateTime(end.getDate())); 1.371 + } 1.372 + else { 1.373 + /* 1.374 + * PeS: Anniversary type has no DTEND nor DUR, define DUR 1.375 + * locally, otherwise we get NPE 1.376 + */ 1.377 + if (duration == null) { 1.378 + duration = new Duration(rDuration); 1.379 + } 1.380 + 1.381 + startPeriod = new Period(new DateTime(start.getDate()), 1.382 + duration.getDuration()); 1.383 + } 1.384 + if (period.intersects(startPeriod)) { 1.385 + recurrenceSet.add(startPeriod); 1.386 + } 1.387 + 1.388 + // subtract exception dates.. 1.389 + for (final Iterator i = getProperties(Property.EXDATE).iterator(); i.hasNext();) { 1.390 + final ExDate exdate = (ExDate) i.next(); 1.391 + for (final Iterator j = recurrenceSet.iterator(); j.hasNext();) { 1.392 + final Period recurrence = (Period) j.next(); 1.393 + // for DATE-TIME instances check for DATE-based exclusions also.. 1.394 + if (exdate.getDates().contains(recurrence.getStart()) 1.395 + || exdate.getDates().contains(new Date(recurrence.getStart()))) { 1.396 + j.remove(); 1.397 + } 1.398 + } 1.399 + } 1.400 + 1.401 + // subtract exception rules.. 1.402 + for (final Iterator i = getProperties(Property.EXRULE).iterator(); i.hasNext();) { 1.403 + final ExRule exrule = (ExRule) i.next(); 1.404 + final DateList exruleDates = exrule.getRecur().getDates(start.getDate(), 1.405 + period, startValue); 1.406 + for (final Iterator j = recurrenceSet.iterator(); j.hasNext();) { 1.407 + final Period recurrence = (Period) j.next(); 1.408 + // for DATE-TIME instances check for DATE-based exclusions also.. 1.409 + if (exruleDates.contains(recurrence.getStart()) 1.410 + || exruleDates.contains(new Date(recurrence.getStart()))) { 1.411 + j.remove(); 1.412 + } 1.413 + } 1.414 + } 1.415 + 1.416 + return recurrenceSet; 1.417 + } 1.418 +}