View Javadoc

1   /*
2    * $Id: RDate.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.Period;
42  import net.fortuna.ical4j.model.Property;
43  import net.fortuna.ical4j.model.ValidationException;
44  import net.fortuna.ical4j.model.parameter.Value;
45  import net.fortuna.ical4j.util.DateFormat;
46  import net.fortuna.ical4j.util.DateTimeFormat;
47  import net.fortuna.ical4j.util.ParameterValidator;
48  
49  /***
50   * Defines an RDATE iCalendar component property.
51   *
52   * @author benf
53   */
54  public class RDate extends Property {
55  
56      private Date time;
57  
58      private Period period;
59  
60      // default value determined through inspection
61      // of iCal-generated files..
62      private boolean utc = true;
63  
64      /***
65       * @param aList
66       *            a list of parameters for this component
67       * @param aValue
68       *            a value string for this component
69       * @throws ParseException
70       *             where the specified value string is not a valid
71       *             date-time/date representation
72       */
73      public RDate(final ParameterList aList, final String aValue)
74              throws ParseException {
75          super(RDATE, aList);
76  
77          // value can be either a date-time or a date..
78          Parameter valueParam = getParameters().getParameter(Parameter.VALUE);
79  
80          if (valueParam != null && Value.DATE.equals(valueParam.getValue())) {
81              time = DateFormat.getInstance().parse(aValue);
82          }
83          else if (valueParam != null && Value.PERIOD.equals(valueParam)) {
84              period = new Period(aValue);
85          }
86          else {
87              time = DateTimeFormat.getInstance().parse(aValue);
88          }
89      }
90  
91      /***
92       * Constructor. Date or Date-Time format is determined based on the presence
93       * of a VALUE parameter.
94       *
95       * @param aList
96       *            a list of parameters for this component
97       * @param aDate
98       *            a date representation of a date or date-time
99       */
100     public RDate(final ParameterList aList, final Date aDate) {
101         super(RDATE, aList);
102 
103         time = aDate;
104     }
105 
106     /***
107      * Constructor.
108      *
109      * @param aList
110      *            a list of parameters for this component
111      * @param aPeriod
112      *            a period
113      */
114     public RDate(final ParameterList aList, final Period aPeriod) {
115         super(RDATE, aList);
116 
117         period = aPeriod;
118     }
119 
120     /***
121      * @see net.fortuna.ical4j.model.Property#validate()
122      */
123     public final void validate() throws ValidationException {
124 
125         /*
126          * ; the following are optional, ; but MUST NOT occur more than once
127          *
128          * (";" "VALUE" "=" ("DATE-TIME" / "DATE" / "PERIOD")) / (";" tzidparam) /
129          */
130         ParameterValidator.getInstance().validateOneOrLess(Parameter.VALUE,
131                 getParameters());
132 
133         Parameter valueParam = getParameters().getParameter(Parameter.VALUE);
134 
135         if (valueParam != null
136                 && !Value.DATE_TIME.equals(valueParam.getValue())
137                 && !Value.DATE.equals(valueParam.getValue())) { throw new ValidationException(
138                 "Parameter [" + Parameter.VALUE + "] is invalid"); }
139 
140         ParameterValidator.getInstance().validateOneOrLess(Parameter.TZID,
141                 getParameters());
142 
143         /*
144          * ; the following is optional, ; and MAY occur more than once
145          *
146          * (";" xparam)
147          */
148     }
149 
150     /***
151      * @return Returns the period.
152      */
153     public final Period getPeriod() {
154         return period;
155     }
156 
157     /***
158      * @return Returns the time.
159      */
160     public final Date getTime() {
161         return time;
162     }
163 
164     /*
165      * (non-Javadoc)
166      *
167      * @see net.fortuna.ical4j.model.Property#getValue()
168      */
169     public String getValue() {
170         if (getTime() != null) {
171 
172             Parameter valueParam = getParameters()
173                     .getParameter(Parameter.VALUE);
174 
175             if (valueParam != null && Value.DATE.equals(valueParam)) {
176                 return DateFormat.getInstance().format(getTime());
177             }
178             else {
179                 return DateTimeFormat.getInstance().format(getTime(), isUtc());
180             }
181         }
182         else if (getPeriod() != null) { return getPeriod().toString(); }
183 
184         return null;
185     }
186 
187     /***
188      * @return Returns the utc.
189      */
190     public boolean isUtc() {
191         return utc;
192     }
193 
194     /***
195      * @param utc
196      *            The utc to set.
197      */
198     public void setUtc(boolean utc) {
199         this.utc = utc;
200     }
201 }