001package biweekly.property;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.List;
006
007import biweekly.ICalVersion;
008import biweekly.ICalendar;
009import biweekly.Warning;
010import biweekly.component.ICalComponent;
011import biweekly.parameter.ICalParameters;
012
013/*
014 Copyright (c) 2013-2015, Michael Angstadt
015 All rights reserved.
016
017 Redistribution and use in source and binary forms, with or without
018 modification, are permitted provided that the following conditions are met: 
019
020 1. Redistributions of source code must retain the above copyright notice, this
021 list of conditions and the following disclaimer. 
022 2. Redistributions in binary form must reproduce the above copyright notice,
023 this list of conditions and the following disclaimer in the documentation
024 and/or other materials provided with the distribution. 
025
026 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
027 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
028 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
029 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
030 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
031 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
032 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
033 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
034 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036 */
037
038/**
039 * Base class for all iCalendar properties.
040 * @author Michael Angstadt
041 */
042public abstract class ICalProperty {
043        /**
044         * The property parameters.
045         */
046        protected ICalParameters parameters = new ICalParameters();
047
048        /**
049         * Gets the property's parameters.
050         * @return the parameters
051         */
052        public ICalParameters getParameters() {
053                return parameters;
054        }
055
056        /**
057         * Sets the property's parameters
058         * @param parameters the parameters
059         */
060        public void setParameters(ICalParameters parameters) {
061                this.parameters = parameters;
062        }
063
064        /**
065         * Gets the first value of a parameter with the given name.
066         * @param name the parameter name (case insensitive, e.g. "LANGUAGE")
067         * @return the parameter value or null if not found
068         */
069        public String getParameter(String name) {
070                return parameters.first(name);
071        }
072
073        /**
074         * Gets all values of a parameter with the given name.
075         * @param name the parameter name (case insensitive, e.g. "LANGUAGE")
076         * @return the parameter values
077         */
078        public List<String> getParameters(String name) {
079                return parameters.get(name);
080        }
081
082        /**
083         * Adds a value to a parameter.
084         * @param name the parameter name (case insensitive, e.g. "LANGUAGE")
085         * @param value the parameter value
086         */
087        public void addParameter(String name, String value) {
088                parameters.put(name, value);
089        }
090
091        /**
092         * Replaces all existing values of a parameter with the given value.
093         * @param name the parameter name (case insensitive, e.g. "LANGUAGE")
094         * @param value the parameter value
095         */
096        public void setParameter(String name, String value) {
097                parameters.replace(name, value);
098        }
099
100        /**
101         * Replaces all existing values of a parameter with the given values.
102         * @param name the parameter name (case insensitive, e.g. "LANGUAGE")
103         * @param values the parameter values
104         */
105        public void setParameter(String name, Collection<String> values) {
106                parameters.replace(name, values);
107        }
108
109        /**
110         * Removes a parameter from the property.
111         * @param name the parameter name (case insensitive, e.g. "LANGUAGE")
112         */
113        public void removeParameter(String name) {
114                parameters.removeAll(name);
115        }
116
117        //Note: The following parameter helper methods are package-scoped to prevent them from cluttering up the Javadocs
118
119        /**
120         * Gets a URI pointing to additional information about the entity
121         * represented by the property.
122         * @return the URI or null if not set
123         * @see <a href="http://tools.ietf.org/html/rfc5545#page-14">RFC 5545
124         * p.14-5</a>
125         */
126        String getAltRepresentation() {
127                return parameters.getAltRepresentation();
128        }
129
130        /**
131         * Sets a URI pointing to additional information about the entity
132         * represented by the property.
133         * @param uri the URI or null to remove
134         * @see <a href="http://tools.ietf.org/html/rfc5545#page-14">RFC 5545
135         * p.14-5</a>
136         */
137        void setAltRepresentation(String uri) {
138                parameters.setAltRepresentation(uri);
139        }
140
141        /**
142         * Gets the content-type of the property's value.
143         * @return the content type (e.g. "image/png") or null if not set
144         * @see <a href="http://tools.ietf.org/html/rfc5545#page-19">RFC 5545
145         * p.19-20</a>
146         */
147        String getFormatType() {
148                return parameters.getFormatType();
149        }
150
151        /**
152         * Sets the content-type of the property's value.
153         * @param formatType the content type (e.g. "image/png") or null to remove
154         * @see <a href="http://tools.ietf.org/html/rfc5545#page-19">RFC 5545
155         * p.19-20</a>
156         */
157        void setFormatType(String formatType) {
158                parameters.setFormatType(formatType);
159        }
160
161        /**
162         * Gets the language that the property value is written in.
163         * @return the language (e.g. "en" for English) or null if not set
164         * @see <a href="http://tools.ietf.org/html/rfc5545#page-21">RFC 5545
165         * p.21</a>
166         */
167        String getLanguage() {
168                return parameters.getLanguage();
169        }
170
171        /**
172         * Sets the language that the property value is written in.
173         * @param language the language (e.g. "en" for English) or null to remove
174         * @see <a href="http://tools.ietf.org/html/rfc5545#page-21">RFC 5545
175         * p.21</a>
176         */
177        void setLanguage(String language) {
178                parameters.setLanguage(language);
179        }
180
181        /**
182         * <p>
183         * Gets a person that is acting on behalf of the person defined in the
184         * property.
185         * </p>
186         * <p>
187         * <b>Supported versions:</b> {@code 2.0}
188         * </p>
189         * @return a URI representing the person (typically, an email URI, e.g.
190         * "mailto:janedoe@example.com") or null if not set
191         * @see <a href="http://tools.ietf.org/html/rfc5545#page-27">RFC 5545
192         * p.27</a>
193         */
194        String getSentBy() {
195                return parameters.getSentBy();
196        }
197
198        /**
199         * <p>
200         * Sets a person that is acting on behalf of the person defined in the
201         * property.
202         * </p>
203         * <p>
204         * <b>Supported versions:</b> {@code 2.0}
205         * </p>
206         * @param uri a URI representing the person (typically, an email URI, e.g.
207         * "mailto:janedoe@example.com") or null to remove
208         * @see <a href="http://tools.ietf.org/html/rfc5545#page-27">RFC 5545
209         * p.27</a>
210         */
211        void setSentBy(String uri) {
212                parameters.setSentBy(uri);
213        }
214
215        /**
216         * <p>
217         * Gets the display name of the person.
218         * </p>
219         * <p>
220         * <b>Supported versions:</b> {@code 2.0}
221         * </p>
222         * @return the display name (e.g. "John Doe") or null if not set
223         * @see <a href="http://tools.ietf.org/html/rfc5545#page-15">RFC 5545
224         * p.15-6</a>
225         */
226        String getCommonName() {
227                return parameters.getCommonName();
228        }
229
230        /**
231         * <p>
232         * Sets the display name of the person.
233         * </p>
234         * <p>
235         * <b>Supported versions:</b> {@code 2.0}
236         * </p>
237         * @param commonName the display name (e.g. "John Doe") or null to remove
238         * @see <a href="http://tools.ietf.org/html/rfc5545#page-15">RFC 5545
239         * p.15-6</a>
240         */
241        void setCommonName(String commonName) {
242                parameters.setCommonName(commonName);
243        }
244
245        /**
246         * <p>
247         * Gets a URI that contains additional information about the person.
248         * </p>
249         * <p>
250         * <b>Supported versions:</b> {@code 2.0}
251         * </p>
252         * @return the URI (e.g. an LDAP URI) or null if not set
253         * @see <a href="http://tools.ietf.org/html/rfc5545#page-18">RFC 5545
254         * p.18</a>
255         */
256        String getDirectoryEntry() {
257                return parameters.getDirectoryEntry();
258        }
259
260        /**
261         * <p>
262         * Sets a URI that contains additional information about the person.
263         * </p>
264         * <p>
265         * <b>Supported versions:</b> {@code 2.0}
266         * </p>
267         * @param uri the URI (e.g. an LDAP URI) or null to remove
268         * @see <a href="http://tools.ietf.org/html/rfc5545#page-18">RFC 5545
269         * p.18</a>
270         */
271        void setDirectoryEntry(String uri) {
272                parameters.setDirectoryEntry(uri);
273        }
274
275        /**
276         * Checks the property for data consistency problems or deviations from the
277         * spec. These problems will not prevent the property from being written to
278         * a data stream, but may prevent it from being parsed correctly by the
279         * consuming application. These problems can largely be avoided by reading
280         * the Javadocs of the property class, or by being familiar with the
281         * iCalendar standard.
282         * @param components the hierarchy of components that the property belongs
283         * to
284         * @param version the version to validate against
285         * @see ICalendar#validate
286         * @return a list of warnings or an empty list if no problems were found
287         */
288        public final List<Warning> validate(List<ICalComponent> components, ICalVersion version) {
289                //validate property value
290                List<Warning> warnings = new ArrayList<Warning>(0);
291                validate(components, version, warnings);
292
293                //validate parameters
294                warnings.addAll(parameters.validate(version));
295
296                return warnings;
297        }
298
299        /**
300         * <p>
301         * Checks the property for data consistency problems or deviations from the
302         * spec.
303         * </p>
304         * <p>
305         * This method should be overridden by child classes that wish to provide
306         * validation logic. The default implementation of this method does nothing.
307         * </p>
308         * @param components the hierarchy of components that the property belongs
309         * to
310         * @param version the version to validate against
311         * @param warnings the list to add the warnings to
312         */
313        protected void validate(List<ICalComponent> components, ICalVersion version, List<Warning> warnings) {
314                //do nothing
315        }
316}