View Javadoc

1   /*
2    * $Id: Component.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  /***
39   * Defines an iCalendar component. Subclasses of this class provide additional
40   * validation and typed values for specific iCalendar components.
41   *
42   * @author benf
43   */
44  public abstract class Component {
45  
46      public static final String BEGIN = "BEGIN";
47  
48      public static final String END = "END";
49  
50      public static final String VEVENT = "VEVENT";
51  
52      public static final String VTODO = "VTODO";
53  
54      public static final String VJOURNAL = "VJOURNAL";
55  
56      public static final String VFREEBUSY = "VFREEBUSY";
57  
58      public static final String VTIMEZONE = "VTIMEZONE";
59  
60      public static final String VALARM = "VALARM";
61  
62      private String name;
63  
64      private PropertyList properties;
65  
66      /***
67       * Constructor made protected to enforce the use of
68       * <code>ComponentFactory</code> for component instantiation.
69       * @param s component name
70       * @param p a list of properties
71       */
72      protected Component(final String s, final PropertyList p) {
73  
74          this.name = s;
75          this.properties = p;
76      }
77  
78      /***
79       * @see java.lang.Object#toString()
80       */
81      public String toString() {
82  
83          return BEGIN + ":" + name + "\r\n" + properties + END + ":" + name
84                  + "\r\n";
85      }
86  
87      /***
88       * @return Returns the name.
89       */
90      public final String getName() {
91          return name;
92      }
93  
94      /***
95       * Indicates whether this component is a top-level
96       * calendar component.
97       * @return a boolean value
98       */
99      public final boolean isCalendarComponent() {
100         return VALARM.equals(getName())
101             || VEVENT.equals(getName())
102             || VFREEBUSY.equals(getName())
103             || VJOURNAL.equals(getName())
104             || VTIMEZONE.equals(getName())
105             || VTODO.equals(getName());
106     }
107 
108     /***
109      * @return Returns the properties.
110      */
111     public final PropertyList getProperties() {
112         return properties;
113     }
114 
115     /***
116      * Perform validation on a component and its properties.
117      * @throws ValidationException
118      *             where the component is not in a valid state
119      */
120     public final void validate() throws ValidationException {
121         validate(true);
122     }
123 
124     /***
125      * Perform validation on a component.
126      * @param recurse indicates whether to validate the component's
127      * properties
128      * @throws ValidationException
129      *             where the component is not in a valid state
130      */
131     public abstract void validate(final boolean recurse) throws ValidationException;
132     
133     /***
134      * Invoke validation on the component properties in its current state.
135      * @throws ValidationException
136      *             where any of the component properties is not in a valid state
137      */
138     protected void validateProperties() throws ValidationException {
139         for (Iterator i = getProperties().iterator(); i.hasNext();) {
140             Property property = (Property) i.next();
141             property.validate();
142         }
143     }
144 }