View Javadoc

1   /*
2    * $Id: ComponentFactory.java [05-Apr-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 net.fortuna.ical4j.model.component.Daylight;
37  import net.fortuna.ical4j.model.component.Standard;
38  import net.fortuna.ical4j.model.component.Time;
39  import net.fortuna.ical4j.model.component.VAlarm;
40  import net.fortuna.ical4j.model.component.VEvent;
41  import net.fortuna.ical4j.model.component.VFreeBusy;
42  import net.fortuna.ical4j.model.component.VJournal;
43  import net.fortuna.ical4j.model.component.VTimeZone;
44  import net.fortuna.ical4j.model.component.VToDo;
45  
46  /***
47   * A factory for creating iCalendar components.
48   *
49   * @author benfortuna
50   */
51  public final class ComponentFactory {
52  
53      private static ComponentFactory instance = new ComponentFactory();
54  
55      /***
56       * Constructor made private to prevent instantiation.
57       */
58      private ComponentFactory() {
59      }
60  
61      /***
62       * @return Returns the instance.
63       */
64      public static ComponentFactory getInstance() {
65          return instance;
66      }
67  
68      /***
69       * Creates a component.
70       *
71       * @param name
72       *            name of the component
73       * @param properties
74       *            a list of component properties
75       * @return a component
76       */
77      public Component createComponent(final String name,
78              final PropertyList properties) {
79  
80          if (Component.VALARM.equals(name)) {
81  
82              return new VAlarm(properties);
83          }
84          else if (Component.VEVENT.equals(name)) {
85  
86              return new VEvent(properties);
87          }
88          else if (Component.VFREEBUSY.equals(name)) {
89  
90              return new VFreeBusy(properties);
91          }
92          else if (Component.VJOURNAL.equals(name)) {
93  
94              return new VJournal(properties);
95          }
96          else if (Component.VTODO.equals(name)) {
97  
98              return new VToDo(properties);
99          }
100         else if (Time.STANDARD.equals(name)) {
101 
102             return new Standard(properties);
103         }
104         else if (Time.DAYLIGHT.equals(name)) {
105 
106             return new Daylight(properties);
107         }
108         else {
109 
110             throw new IllegalArgumentException("Unkown component [" + name
111                     + "]");
112         }
113     }
114 
115     /***
116      * Creates a component which contains sub-components. Currently the only
117      * such component is VTIMEZONE.
118      *
119      * @param name
120      *            name of the component
121      * @param properties
122      *            a list of component properties
123      * @param components
124      *            a list of sub-components (namely standard/daylight timezones)
125      * @return a component
126      */
127     public Component createComponent(final String name,
128             final PropertyList properties, final ComponentList components) {
129 
130         if (components != null) {
131 
132             if (Component.VTIMEZONE.equals(name)) {
133 
134                 return new VTimeZone(properties, components);
135             }
136             else if (Component.VEVENT.equals(name)) {
137 
138                 return new VEvent(properties, components);
139             }
140             else {
141 
142                 throw new IllegalArgumentException("Unkown component [" + name
143                         + "]");
144             }
145         }
146 
147         return createComponent(name, properties);
148     }
149 }