Tue, 10 Feb 2015 18:12:00 +0100
Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.
michael@0 | 1 | /** |
michael@0 | 2 | * Copyright (c) 2012, Ben Fortuna |
michael@0 | 3 | * All rights reserved. |
michael@0 | 4 | * |
michael@0 | 5 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 6 | * modification, are permitted provided that the following conditions |
michael@0 | 7 | * are met: |
michael@0 | 8 | * |
michael@0 | 9 | * o Redistributions of source code must retain the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 11 | * |
michael@0 | 12 | * o Redistributions in binary form must reproduce the above copyright |
michael@0 | 13 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 14 | * documentation and/or other materials provided with the distribution. |
michael@0 | 15 | * |
michael@0 | 16 | * o Neither the name of Ben Fortuna nor the names of any other contributors |
michael@0 | 17 | * may be used to endorse or promote products derived from this software |
michael@0 | 18 | * without specific prior written permission. |
michael@0 | 19 | * |
michael@0 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
michael@0 | 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
michael@0 | 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
michael@0 | 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@0 | 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 31 | */ |
michael@0 | 32 | package net.fortuna.ical4j.model.component; |
michael@0 | 33 | |
michael@0 | 34 | import java.io.IOException; |
michael@0 | 35 | import java.net.URISyntaxException; |
michael@0 | 36 | import java.text.ParseException; |
michael@0 | 37 | import java.util.Iterator; |
michael@0 | 38 | |
michael@0 | 39 | import net.fortuna.ical4j.model.Component; |
michael@0 | 40 | import net.fortuna.ical4j.model.ComponentList; |
michael@0 | 41 | import net.fortuna.ical4j.model.Date; |
michael@0 | 42 | import net.fortuna.ical4j.model.Property; |
michael@0 | 43 | import net.fortuna.ical4j.model.PropertyList; |
michael@0 | 44 | import net.fortuna.ical4j.model.ValidationException; |
michael@0 | 45 | import net.fortuna.ical4j.model.Validator; |
michael@0 | 46 | import net.fortuna.ical4j.model.property.LastModified; |
michael@0 | 47 | import net.fortuna.ical4j.model.property.Method; |
michael@0 | 48 | import net.fortuna.ical4j.model.property.TzId; |
michael@0 | 49 | import net.fortuna.ical4j.model.property.TzUrl; |
michael@0 | 50 | import net.fortuna.ical4j.util.PropertyValidator; |
michael@0 | 51 | import net.fortuna.ical4j.util.Strings; |
michael@0 | 52 | |
michael@0 | 53 | import org.apache.commons.lang.ObjectUtils; |
michael@0 | 54 | import org.apache.commons.lang.builder.HashCodeBuilder; |
michael@0 | 55 | |
michael@0 | 56 | /** |
michael@0 | 57 | * $Id$ [Apr 5, 2004] |
michael@0 | 58 | * |
michael@0 | 59 | * Defines an iCalendar VTIMEZONE component. |
michael@0 | 60 | * |
michael@0 | 61 | * <pre> |
michael@0 | 62 | * 4.6.5 Time Zone Component |
michael@0 | 63 | * |
michael@0 | 64 | * Component Name: VTIMEZONE |
michael@0 | 65 | * |
michael@0 | 66 | * Purpose: Provide a grouping of component properties that defines a |
michael@0 | 67 | * time zone. |
michael@0 | 68 | * |
michael@0 | 69 | * Formal Definition: A "VTIMEZONE" calendar component is defined by the |
michael@0 | 70 | * following notation: |
michael@0 | 71 | * |
michael@0 | 72 | * timezonec = "BEGIN" ":" "VTIMEZONE" CRLF |
michael@0 | 73 | * |
michael@0 | 74 | * 2*( |
michael@0 | 75 | * |
michael@0 | 76 | * ; 'tzid' is required, but MUST NOT occur more |
michael@0 | 77 | * ; than once |
michael@0 | 78 | * |
michael@0 | 79 | * tzid / |
michael@0 | 80 | * |
michael@0 | 81 | * ; 'last-mod' and 'tzurl' are optional, |
michael@0 | 82 | * but MUST NOT occur more than once |
michael@0 | 83 | * |
michael@0 | 84 | * last-mod / tzurl / |
michael@0 | 85 | * |
michael@0 | 86 | * ; one of 'standardc' or 'daylightc' MUST occur |
michael@0 | 87 | * ..; and each MAY occur more than once. |
michael@0 | 88 | * |
michael@0 | 89 | * standardc / daylightc / |
michael@0 | 90 | * |
michael@0 | 91 | * ; the following is optional, |
michael@0 | 92 | * ; and MAY occur more than once |
michael@0 | 93 | * |
michael@0 | 94 | * x-prop |
michael@0 | 95 | * |
michael@0 | 96 | * ) |
michael@0 | 97 | * |
michael@0 | 98 | * "END" ":" "VTIMEZONE" CRLF |
michael@0 | 99 | * |
michael@0 | 100 | * standardc = "BEGIN" ":" "STANDARD" CRLF |
michael@0 | 101 | * |
michael@0 | 102 | * tzprop |
michael@0 | 103 | * |
michael@0 | 104 | * "END" ":" "STANDARD" CRLF |
michael@0 | 105 | * |
michael@0 | 106 | * daylightc = "BEGIN" ":" "DAYLIGHT" CRLF |
michael@0 | 107 | * |
michael@0 | 108 | * tzprop |
michael@0 | 109 | * |
michael@0 | 110 | * "END" ":" "DAYLIGHT" CRLF |
michael@0 | 111 | * |
michael@0 | 112 | * tzprop = 3*( |
michael@0 | 113 | * |
michael@0 | 114 | * ; the following are each REQUIRED, |
michael@0 | 115 | * ; but MUST NOT occur more than once |
michael@0 | 116 | * |
michael@0 | 117 | * dtstart / tzoffsetto / tzoffsetfrom / |
michael@0 | 118 | * |
michael@0 | 119 | * ; the following are optional, |
michael@0 | 120 | * ; and MAY occur more than once |
michael@0 | 121 | * |
michael@0 | 122 | * comment / rdate / rrule / tzname / x-prop |
michael@0 | 123 | * |
michael@0 | 124 | * ) |
michael@0 | 125 | * </pre> |
michael@0 | 126 | * |
michael@0 | 127 | * @author Ben Fortuna |
michael@0 | 128 | */ |
michael@0 | 129 | public class VTimeZone extends CalendarComponent { |
michael@0 | 130 | |
michael@0 | 131 | private static final long serialVersionUID = 5629679741050917815L; |
michael@0 | 132 | |
michael@0 | 133 | private final Validator itipValidator = new ITIPValidator(); |
michael@0 | 134 | |
michael@0 | 135 | private ComponentList observances; |
michael@0 | 136 | |
michael@0 | 137 | /** |
michael@0 | 138 | * Default constructor. |
michael@0 | 139 | */ |
michael@0 | 140 | public VTimeZone() { |
michael@0 | 141 | super(VTIMEZONE); |
michael@0 | 142 | this.observances = new ComponentList(); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | /** |
michael@0 | 146 | * Constructs a new instance containing the specified properties. |
michael@0 | 147 | * @param properties a list of properties |
michael@0 | 148 | */ |
michael@0 | 149 | public VTimeZone(final PropertyList properties) { |
michael@0 | 150 | super(VTIMEZONE, properties); |
michael@0 | 151 | this.observances = new ComponentList(); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | /** |
michael@0 | 155 | * Constructs a new vtimezone component with no properties and the specified list of type components. |
michael@0 | 156 | * @param observances a list of type components |
michael@0 | 157 | */ |
michael@0 | 158 | public VTimeZone(final ComponentList observances) { |
michael@0 | 159 | super(VTIMEZONE); |
michael@0 | 160 | this.observances = observances; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | /** |
michael@0 | 164 | * Constructor. |
michael@0 | 165 | * @param properties a list of properties |
michael@0 | 166 | * @param observances a list of timezone types |
michael@0 | 167 | */ |
michael@0 | 168 | public VTimeZone(final PropertyList properties, |
michael@0 | 169 | final ComponentList observances) { |
michael@0 | 170 | super(VTIMEZONE, properties); |
michael@0 | 171 | this.observances = observances; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | /** |
michael@0 | 175 | * {@inheritDoc} |
michael@0 | 176 | */ |
michael@0 | 177 | public final String toString() { |
michael@0 | 178 | final StringBuffer b = new StringBuffer(); |
michael@0 | 179 | b.append(BEGIN); |
michael@0 | 180 | b.append(':'); |
michael@0 | 181 | b.append(getName()); |
michael@0 | 182 | b.append(Strings.LINE_SEPARATOR); |
michael@0 | 183 | b.append(getProperties()); |
michael@0 | 184 | b.append(observances); |
michael@0 | 185 | b.append(END); |
michael@0 | 186 | b.append(':'); |
michael@0 | 187 | b.append(getName()); |
michael@0 | 188 | b.append(Strings.LINE_SEPARATOR); |
michael@0 | 189 | return b.toString(); |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | /** |
michael@0 | 193 | * {@inheritDoc} |
michael@0 | 194 | */ |
michael@0 | 195 | public final void validate(final boolean recurse) |
michael@0 | 196 | throws ValidationException { |
michael@0 | 197 | |
michael@0 | 198 | /* |
michael@0 | 199 | * ; 'tzid' is required, but MUST NOT occur more ; than once tzid / |
michael@0 | 200 | */ |
michael@0 | 201 | PropertyValidator.getInstance().assertOne(Property.TZID, |
michael@0 | 202 | getProperties()); |
michael@0 | 203 | |
michael@0 | 204 | /* |
michael@0 | 205 | * ; 'last-mod' and 'tzurl' are optional, but MUST NOT occur more than once last-mod / tzurl / |
michael@0 | 206 | */ |
michael@0 | 207 | PropertyValidator.getInstance().assertOneOrLess(Property.LAST_MODIFIED, |
michael@0 | 208 | getProperties()); |
michael@0 | 209 | PropertyValidator.getInstance().assertOneOrLess(Property.TZURL, |
michael@0 | 210 | getProperties()); |
michael@0 | 211 | |
michael@0 | 212 | /* |
michael@0 | 213 | * ; one of 'standardc' or 'daylightc' MUST occur ..; and each MAY occur more than once. standardc / daylightc / |
michael@0 | 214 | */ |
michael@0 | 215 | if (getObservances().getComponent(Observance.STANDARD) == null |
michael@0 | 216 | && getObservances().getComponent(Observance.DAYLIGHT) == null) { |
michael@0 | 217 | throw new ValidationException("Sub-components [" |
michael@0 | 218 | + Observance.STANDARD + "," + Observance.DAYLIGHT |
michael@0 | 219 | + "] must be specified at least once"); |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | for (final Iterator i = getObservances().iterator(); i.hasNext();) { |
michael@0 | 223 | ((Component) i.next()).validate(recurse); |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | /* |
michael@0 | 227 | * ; the following is optional, ; and MAY occur more than once x-prop |
michael@0 | 228 | */ |
michael@0 | 229 | |
michael@0 | 230 | if (recurse) { |
michael@0 | 231 | validateProperties(); |
michael@0 | 232 | } |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | /** |
michael@0 | 236 | * {@inheritDoc} |
michael@0 | 237 | */ |
michael@0 | 238 | protected Validator getValidator(Method method) { |
michael@0 | 239 | return itipValidator; |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | /** |
michael@0 | 243 | * Common validation for all iTIP methods. |
michael@0 | 244 | * |
michael@0 | 245 | * <pre> |
michael@0 | 246 | * Component/Property Presence |
michael@0 | 247 | * ------------------- ---------------------------------------------- |
michael@0 | 248 | * VTIMEZONE 0+ MUST be present if any date/time refers |
michael@0 | 249 | * to timezone |
michael@0 | 250 | * DAYLIGHT 0+ MUST be one or more of either STANDARD or |
michael@0 | 251 | * DAYLIGHT |
michael@0 | 252 | * COMMENT 0 or 1 |
michael@0 | 253 | * DTSTART 1 MUST be local time format |
michael@0 | 254 | * RDATE 0+ if present RRULE MUST NOT be present |
michael@0 | 255 | * RRULE 0+ if present RDATE MUST NOT be present |
michael@0 | 256 | * TZNAME 0 or 1 |
michael@0 | 257 | * TZOFFSET 1 |
michael@0 | 258 | * TZOFFSETFROM 1 |
michael@0 | 259 | * TZOFFSETTO 1 |
michael@0 | 260 | * X-PROPERTY 0+ |
michael@0 | 261 | * LAST-MODIFIED 0 or 1 |
michael@0 | 262 | * STANDARD 0+ MUST be one or more of either STANDARD or |
michael@0 | 263 | * DAYLIGHT |
michael@0 | 264 | * COMMENT 0 or 1 |
michael@0 | 265 | * DTSTART 1 MUST be local time format |
michael@0 | 266 | * RDATE 0+ if present RRULE MUST NOT be present |
michael@0 | 267 | * RRULE 0+ if present RDATE MUST NOT be present |
michael@0 | 268 | * TZNAME 0 or 1 |
michael@0 | 269 | * TZOFFSETFROM 1 |
michael@0 | 270 | * TZOFFSETTO 1 |
michael@0 | 271 | * X-PROPERTY 0+ |
michael@0 | 272 | * TZID 1 |
michael@0 | 273 | * TZURL 0 or 1 |
michael@0 | 274 | * X-PROPERTY 0+ |
michael@0 | 275 | * </pre> |
michael@0 | 276 | */ |
michael@0 | 277 | private class ITIPValidator implements Validator { |
michael@0 | 278 | |
michael@0 | 279 | private static final long serialVersionUID = 1L; |
michael@0 | 280 | |
michael@0 | 281 | /** |
michael@0 | 282 | * {@inheritDoc} |
michael@0 | 283 | */ |
michael@0 | 284 | public void validate() throws ValidationException { |
michael@0 | 285 | for (final Iterator i = getObservances().iterator(); i.hasNext();) { |
michael@0 | 286 | final Observance observance = (Observance) i.next(); |
michael@0 | 287 | PropertyValidator.getInstance().assertOne(Property.DTSTART, observance.getProperties()); |
michael@0 | 288 | PropertyValidator.getInstance().assertOne(Property.TZOFFSETFROM, observance.getProperties()); |
michael@0 | 289 | PropertyValidator.getInstance().assertOne(Property.TZOFFSETTO, observance.getProperties()); |
michael@0 | 290 | |
michael@0 | 291 | PropertyValidator.getInstance().assertOneOrLess(Property.COMMENT, observance.getProperties()); |
michael@0 | 292 | PropertyValidator.getInstance().assertOneOrLess(Property.TZNAME, observance.getProperties()); |
michael@0 | 293 | } |
michael@0 | 294 | } |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | /** |
michael@0 | 298 | * @return Returns the types. |
michael@0 | 299 | */ |
michael@0 | 300 | public final ComponentList getObservances() { |
michael@0 | 301 | return observances; |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | /** |
michael@0 | 305 | * Returns the latest applicable timezone observance for the specified date. |
michael@0 | 306 | * @param date the latest possible date for a timezone observance onset |
michael@0 | 307 | * @return the latest applicable timezone observance for the specified date or null if there are no applicable |
michael@0 | 308 | * observances |
michael@0 | 309 | */ |
michael@0 | 310 | public final Observance getApplicableObservance(final Date date) { |
michael@0 | 311 | Observance latestObservance = null; |
michael@0 | 312 | Date latestOnset = null; |
michael@0 | 313 | for (final Iterator i = getObservances().iterator(); i.hasNext();) { |
michael@0 | 314 | final Observance observance = (Observance) i.next(); |
michael@0 | 315 | final Date onset = observance.getLatestOnset(date); |
michael@0 | 316 | if (latestOnset == null |
michael@0 | 317 | || (onset != null && onset.after(latestOnset))) { |
michael@0 | 318 | latestOnset = onset; |
michael@0 | 319 | latestObservance = observance; |
michael@0 | 320 | } |
michael@0 | 321 | } |
michael@0 | 322 | return latestObservance; |
michael@0 | 323 | } |
michael@0 | 324 | |
michael@0 | 325 | /** |
michael@0 | 326 | * @return the mandatory timezone identifier property |
michael@0 | 327 | */ |
michael@0 | 328 | public final TzId getTimeZoneId() { |
michael@0 | 329 | return (TzId) getProperty(Property.TZID); |
michael@0 | 330 | } |
michael@0 | 331 | |
michael@0 | 332 | /** |
michael@0 | 333 | * @return the optional last-modified property |
michael@0 | 334 | */ |
michael@0 | 335 | public final LastModified getLastModified() { |
michael@0 | 336 | return (LastModified) getProperty(Property.LAST_MODIFIED); |
michael@0 | 337 | } |
michael@0 | 338 | |
michael@0 | 339 | /** |
michael@0 | 340 | * @return the optional timezone url property |
michael@0 | 341 | */ |
michael@0 | 342 | public final TzUrl getTimeZoneUrl() { |
michael@0 | 343 | return (TzUrl) getProperty(Property.TZURL); |
michael@0 | 344 | } |
michael@0 | 345 | |
michael@0 | 346 | /** |
michael@0 | 347 | * {@inheritDoc} |
michael@0 | 348 | */ |
michael@0 | 349 | public boolean equals(final Object arg0) { |
michael@0 | 350 | if (arg0 instanceof VTimeZone) { |
michael@0 | 351 | return super.equals(arg0) |
michael@0 | 352 | && ObjectUtils.equals(observances, ((VTimeZone) arg0) |
michael@0 | 353 | .getObservances()); |
michael@0 | 354 | } |
michael@0 | 355 | return super.equals(arg0); |
michael@0 | 356 | } |
michael@0 | 357 | |
michael@0 | 358 | /** |
michael@0 | 359 | * {@inheritDoc} |
michael@0 | 360 | */ |
michael@0 | 361 | public int hashCode() { |
michael@0 | 362 | return new HashCodeBuilder().append(getName()).append(getProperties()) |
michael@0 | 363 | .append(getObservances()).toHashCode(); |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | /** |
michael@0 | 367 | * Overrides default copy method to add support for copying observance sub-components. |
michael@0 | 368 | * @return a copy of the instance |
michael@0 | 369 | * @throws ParseException where an error occurs parsing data |
michael@0 | 370 | * @throws IOException where an error occurs reading data |
michael@0 | 371 | * @throws URISyntaxException where an invalid URI is encountered |
michael@0 | 372 | * @see net.fortuna.ical4j.model.Component#copy() |
michael@0 | 373 | */ |
michael@0 | 374 | public Component copy() throws ParseException, IOException, URISyntaxException { |
michael@0 | 375 | final VTimeZone copy = (VTimeZone) super.copy(); |
michael@0 | 376 | copy.observances = new ComponentList(observances); |
michael@0 | 377 | return copy; |
michael@0 | 378 | } |
michael@0 | 379 | } |