1 /*
2 * $Id: VAlarm.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.component;
35
36 import net.fortuna.ical4j.model.Component;
37 import net.fortuna.ical4j.model.Property;
38 import net.fortuna.ical4j.model.PropertyList;
39 import net.fortuna.ical4j.model.ValidationException;
40 import net.fortuna.ical4j.model.property.Action;
41 import net.fortuna.ical4j.util.PropertyValidator;
42
43 /***
44 * Defines an iCalendar VALARM component.
45 *
46 * @author benf
47 */
48 public class VAlarm extends Component {
49
50 /***
51 * Constructor.
52 *
53 * @param aList
54 * a list of properties
55 */
56 public VAlarm(final PropertyList aList) {
57
58 super(VALARM, aList);
59 }
60
61 /*
62 * (non-Javadoc)
63 *
64 * @see net.fortuna.ical4j.model.Component#validate(boolean)
65 */
66 public final void validate(boolean recurse) throws ValidationException {
67
68 /*
69 * ; 'action' and 'trigger' are both REQUIRED, ; but MUST NOT occur more
70 * than once
71 *
72 * action / trigger /
73 */
74 Property action = getProperties().getProperty(Property.ACTION);
75
76 if (action == null) { throw new ValidationException("Property ["
77 + Property.ACTION + "] must be specified once"); }
78
79 if (getProperties().getProperty(Property.TRIGGER) == null) { throw new ValidationException(
80 "Property [" + Property.TRIGGER + "] must be specified once"); }
81
82 /*
83 * ; 'duration' and 'repeat' are both optional, ; and MUST NOT occur
84 * more than once each, ; but if one occurs, so MUST the other
85 *
86 * duration / repeat /
87 */
88 PropertyValidator.getInstance().validateOneOrLess(Property.DURATION,
89 getProperties());
90 PropertyValidator.getInstance().validateOneOrLess(Property.REPEAT,
91 getProperties());
92
93 if ((getProperties().getProperty(Property.DURATION) == null && getProperties()
94 .getProperty(Property.REPEAT) != null)
95 || (getProperties().getProperty(Property.REPEAT) == null && getProperties()
96 .getProperty(Property.DURATION) != null)) { throw new ValidationException(
97 "Properties [" + Property.DURATION + "," + Property.REPEAT
98 + "] must both be specified or not at all"); }
99
100 /*
101 * ; the following is optional, ; and MAY occur more than once
102 *
103 * x-prop
104 */
105
106 if (Action.AUDIO.equals(action.getValue())) {
107 validateAudio();
108 }
109 else if (Action.DISPLAY.equals(action.getValue())) {
110 validateDisplay();
111 }
112 else if (Action.EMAIL.equals(action.getValue())) {
113 validateEmail();
114 }
115 else if (Action.PROCEDURE.equals(action.getValue())) {
116 validateProcedure();
117 }
118
119 if (recurse) {
120 validateProperties();
121 }
122 }
123
124 private void validateAudio() throws ValidationException {
125 /*
126 * ; the following is optional, ; but MUST NOT occur more than once
127 *
128 * attach /
129 */
130 PropertyValidator.getInstance().validateOneOrLess(Property.ATTACH,
131 getProperties());
132 }
133
134 private void validateDisplay() throws ValidationException {
135 /*
136 * ; the following are all REQUIRED, ; but MUST NOT occur more than once
137 *
138 * action / description / trigger /
139 */
140 PropertyValidator.getInstance().validateOne(Property.DESCRIPTION,
141 getProperties());
142 }
143
144 private void validateEmail() throws ValidationException {
145 /*
146 * ; the following are all REQUIRED, ; but MUST NOT occur more than once
147 *
148 * action / description / trigger / summary
149 * ; the following is REQUIRED, ; and MAY occur more than once
150 *
151 * attendee /
152 * ; 'duration' and 'repeat' are both optional, ; and MUST NOT occur
153 * more than once each, ; but if one occurs, so MUST the other
154 *
155 * duration / repeat /
156 * ; the following are optional, ; and MAY occur more than once
157 *
158 * attach / x-prop
159 */
160 PropertyValidator.getInstance().validateOne(Property.DESCRIPTION,
161 getProperties());
162 PropertyValidator.getInstance().validateOne(Property.SUMMARY,
163 getProperties());
164
165 PropertyValidator.getInstance().validateOneOrMore(Property.ATTENDEE,
166 getProperties());
167 }
168
169 private void validateProcedure() throws ValidationException {
170 /*
171 * ; the following are all REQUIRED, ; but MUST NOT occur more than once
172 *
173 * action / attach / trigger /
174 * ; 'duration' and 'repeat' are both optional, ; and MUST NOT occur
175 * more than once each, ; but if one occurs, so MUST the other
176 *
177 * duration / repeat /
178 * ; 'description' is optional, ; and MUST NOT occur more than once
179 *
180 * description /
181 * ; the following is optional, ; and MAY occur more than once
182 *
183 * x-prop
184 */
185 PropertyValidator.getInstance().validateOne(Property.ATTACH,
186 getProperties());
187
188 PropertyValidator.getInstance().validateOneOrLess(Property.DESCRIPTION,
189 getProperties());
190 }
191 }