View Javadoc

1   /*
2    * $Id: RecurrenceId.java [Apr 6, 2004]
3    *
4    * Copyright (c) 2004, Ben Fortuna
5    * All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 	o Redistributions of source code must retain the above copyright
12   * notice, this list of conditions and the following disclaimer.
13   *
14   * 	o Redistributions in binary form must reproduce the above copyright
15   * notice, this list of conditions and the following disclaimer in the
16   * documentation and/or other materials provided with the distribution.
17   *
18   * 	o Neither the name of Ben Fortuna nor the names of any other contributors
19   * may be used to endorse or promote products derived from this software
20   * without specific prior written permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   */
34  package net.fortuna.ical4j.model.property;
35  
36  import java.text.ParseException;
37  import java.util.Date;
38  
39  import net.fortuna.ical4j.model.Parameter;
40  import net.fortuna.ical4j.model.ParameterList;
41  import net.fortuna.ical4j.model.Property;
42  import net.fortuna.ical4j.model.ValidationException;
43  import net.fortuna.ical4j.model.parameter.Value;
44  import net.fortuna.ical4j.util.DateFormat;
45  import net.fortuna.ical4j.util.DateTimeFormat;
46  import net.fortuna.ical4j.util.ParameterValidator;
47  
48  /***
49   * Defines a RECURRENCE-ID iCalendar component property.
50   *
51   * @author benf
52   */
53  public class RecurrenceId extends Property {
54  
55      private Date time;
56  
57      // default value determined through inspection
58      // of iCal-generated files..
59      private boolean utc = false;
60  
61      /***
62       * @param aList
63       *            a list of parameters for this component
64       * @param aValue
65       *            a value string for this component
66       * @throws ParseException
67       *             where the specified value string is not a valid
68       *             date-time/date representation
69       */
70      public RecurrenceId(final ParameterList aList, final String aValue)
71              throws ParseException {
72          super(RECURRENCE_ID, aList);
73  
74          // value can be either a date-time or a date..
75          Parameter valueParam = getParameters().getParameter(Parameter.VALUE);
76  
77          if (valueParam != null && Value.DATE.equals(valueParam.getValue())) {
78              time = DateFormat.getInstance().parse(aValue);
79          }
80          else {
81              time = DateTimeFormat.getInstance().parse(aValue);
82          }
83      }
84  
85      /***
86       * Constructor. Date or Date-Time format is determined based
87       * on the presence of a VALUE parameter.
88       * @param aList
89       *            a list of parameters for this component
90       * @param aDate
91       *            a date representation of a date or date-time
92       */
93      public RecurrenceId(final ParameterList aList, final Date aDate) {
94          super(RECURRENCE_ID, aList);
95  
96          time = aDate;
97      }
98  
99      /***
100      * @return Returns the time.
101      */
102     public final Date getTime() {
103         return time;
104     }
105 
106     /***
107      * @see net.fortuna.ical4j.model.Property#validate()
108      */
109     public final void validate() throws ValidationException {
110 
111         /*
112          * ; the following are optional, ; but MUST NOT occur more than once
113          *
114          * (";" "VALUE" "=" ("DATE-TIME" / "DATE)) / (";" tzidparam) / (";"
115          * rangeparam) /
116          */
117         ParameterValidator.getInstance().validateOneOrLess(Parameter.VALUE,
118                 getParameters());
119 
120         Parameter valueParam = getParameters().getParameter(Parameter.VALUE);
121 
122         if (valueParam != null
123                 && !Value.DATE_TIME.equals(valueParam.getValue())
124                 && !Value.DATE.equals(valueParam.getValue())) {
125             throw new ValidationException(
126                 "Parameter [" + Parameter.VALUE + "] is invalid");
127         }
128 
129         ParameterValidator.getInstance().validateOneOrLess(Parameter.TZID,
130                 getParameters());
131         ParameterValidator.getInstance().validateOneOrLess(Parameter.RANGE,
132                 getParameters());
133 
134         /*
135          * ; the following is optional, ; and MAY occur more than once
136          *
137          * (";" xparam)
138          */
139     }
140 
141 
142     /* (non-Javadoc)
143      * @see net.fortuna.ical4j.model.Property#getValue()
144      */
145     public String getValue() {
146         Parameter valueParam = getParameters().getParameter(Parameter.VALUE);
147 
148         if (valueParam != null && Value.DATE.equals(valueParam)) {
149             return DateFormat.getInstance().format(getTime());
150         }
151 
152         // return local time..
153         return DateTimeFormat.getInstance().format(getTime(), isUtc());
154     }
155 
156     /***
157      * @return Returns the utc.
158      */
159     public boolean isUtc() {
160         return utc;
161     }
162 
163     /***
164      * @param utc The utc to set.
165      */
166     public void setUtc(boolean utc) {
167         this.utc = utc;
168     }
169 }