1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
117
118 PropertyValidator.getInstance().validateOne(Property.PRODID, properties);
119 PropertyValidator.getInstance().validateOne(Property.VERSION, properties);
120
121
122
123 PropertyValidator.getInstance()
124 .validateOneOrLess(Property.CALSCALE, properties);
125 PropertyValidator.getInstance().validateOneOrLess(Property.METHOD, properties);
126
127
128 if (getComponents().isEmpty()) { throw new ValidationException(
129 "Calendar must contain at least one component"); }
130
131
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
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 }