View Javadoc

1   /*
2    * $Id: Calendar.java [Apr 5, 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;
35  
36  import java.util.Iterator;
37  
38  import net.fortuna.ical4j.model.property.XProperty;
39  import net.fortuna.ical4j.util.PropertyValidator;
40  
41  /***
42   * Defines an iCalendar calendar.
43   *
44   * @author benf
45   */
46  public class Calendar {
47  
48      public static final String BEGIN = "BEGIN";
49  
50      public static final String VCALENDAR = "VCALENDAR";
51  
52      public static final String END = "END";
53  
54      private PropertyList properties;
55  
56      private ComponentList components;
57  
58      /***
59       * Constructor.
60       *
61       * @param p
62       *            a list of properties
63       * @param c
64       *            a list of components
65       * @throws ValidationException
66       *             where the specified arguments do not meet the criteria for a
67       *             valid iCalendar
68       */
69      public Calendar(final PropertyList p, final ComponentList c)
70              throws ValidationException {
71  
72          this.properties = p;
73          this.components = c;
74      }
75  
76      /***
77       * @see java.lang.Object#toString()
78       */
79      public final String toString() {
80          return BEGIN + ":" + VCALENDAR + "\r\n" + properties + components + END
81                  + ":" + VCALENDAR + "\r\n";
82      }
83  
84      /***
85       * @return Returns the components.
86       */
87      public final ComponentList getComponents() {
88          return components;
89      }
90  
91      /***
92       * @return Returns the properties.
93       */
94      public final PropertyList getProperties() {
95          return properties;
96      }
97  
98      /***
99       * Perform validation on the calendar, its properties
100      * and its components in its current state.
101      * @throws ValidationException
102      *             where the calendar is not in a valid state
103      */
104     public final void validate() throws ValidationException {
105         validate(true);
106     }
107 
108     /***
109      * Perform validation on the calendar in its current state.
110      * @param recurse indicates whether to validate the calendar's
111      * properties and components
112      * @throws ValidationException
113      *             where the calendar is not in a valid state
114      */
115     public final void validate(boolean recurse) throws ValidationException {
116         // 'prodid' and 'version' are both REQUIRED,
117         // but MUST NOT occur more than once
118         PropertyValidator.getInstance().validateOne(Property.PRODID, properties);
119         PropertyValidator.getInstance().validateOne(Property.VERSION, properties);
120 
121         // 'calscale' and 'method' are optional,
122         // but MUST NOT occur more than once
123         PropertyValidator.getInstance()
124                 .validateOneOrLess(Property.CALSCALE, properties);
125         PropertyValidator.getInstance().validateOneOrLess(Property.METHOD, properties);
126 
127         // must contain at least one component
128         if (getComponents().isEmpty()) { throw new ValidationException(
129                 "Calendar must contain at least one component"); }
130 
131         // validate properties..
132         for (Iterator i = getProperties().iterator(); i.hasNext();) {
133             Property property = (Property) i.next();
134 
135             if (!(property instanceof XProperty)
136                  && !property.isCalendarProperty()) {
137                 throw new IllegalArgumentException(
138                     "Invalid property: " + property.getName());
139             }
140         }
141 
142         // validate components..
143         for (Iterator i = getComponents().iterator(); i.hasNext();) {
144             Component component = (Component) i.next();
145 
146             if (!component.isCalendarComponent()) {
147                 throw new IllegalArgumentException(
148                     "Invalid component: " + component.getName());
149             }
150         }
151         
152         if (recurse) {
153             validateProperties();
154             validateComponents();
155         }
156     }
157     
158     /***
159      * Invoke validation on the calendar properties in its current state.
160      * @throws ValidationException
161      *             where any of the calendar properties is not in a valid state
162      */
163     private void validateProperties() throws ValidationException {
164         for (Iterator i = getProperties().iterator(); i.hasNext();) {
165             Property property = (Property) i.next();
166             property.validate();
167         }
168     }
169     
170     /***
171      * Invoke validation on the calendar components in its current state.
172      * @throws ValidationException
173      *             where any of the calendar components is not in a valid state
174      */
175     private void validateComponents() throws ValidationException {
176         for (Iterator i = getComponents().iterator(); i.hasNext();) {
177             Component component = (Component) i.next();
178             component.validate();
179         }
180     }
181 }