|
1 /** |
|
2 * Copyright (c) 2012, Ben Fortuna |
|
3 * All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * |
|
9 * o Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * o Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * o Neither the name of Ben Fortuna nor the names of any other contributors |
|
17 * may be used to endorse or promote products derived from this software |
|
18 * without specific prior written permission. |
|
19 * |
|
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 */ |
|
32 package net.fortuna.ical4j.model; |
|
33 |
|
34 import net.fortuna.ical4j.model.component.Available; |
|
35 import net.fortuna.ical4j.model.component.Daylight; |
|
36 import net.fortuna.ical4j.model.component.Observance; |
|
37 import net.fortuna.ical4j.model.component.Standard; |
|
38 import net.fortuna.ical4j.model.component.VAlarm; |
|
39 import net.fortuna.ical4j.model.component.VAvailability; |
|
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 import net.fortuna.ical4j.model.component.VVenue; |
|
46 import net.fortuna.ical4j.model.component.XComponent; |
|
47 import net.fortuna.ical4j.util.CompatibilityHints; |
|
48 |
|
49 /** |
|
50 * $Id$ [05-Apr-2004] |
|
51 * |
|
52 * A factory for creating iCalendar components. Note that if relaxed parsing is enabled (via specifying the system |
|
53 * property: icalj.parsing.relaxed=true) illegal component names are allowed. |
|
54 * @author Ben Fortuna |
|
55 */ |
|
56 public final class ComponentFactory { |
|
57 |
|
58 private static ComponentFactory instance = new ComponentFactory(); |
|
59 |
|
60 /** |
|
61 * Constructor made private to prevent instantiation. |
|
62 */ |
|
63 private ComponentFactory() { |
|
64 } |
|
65 |
|
66 /** |
|
67 * @return Returns the instance. |
|
68 */ |
|
69 public static ComponentFactory getInstance() { |
|
70 return instance; |
|
71 } |
|
72 |
|
73 /** |
|
74 * @param name a component name |
|
75 * @return a new component instance of the specified type |
|
76 */ |
|
77 public Component createComponent(final String name) { |
|
78 return createComponent(name, new PropertyList()); |
|
79 } |
|
80 |
|
81 /** |
|
82 * Creates a component. |
|
83 * @param name name of the component |
|
84 * @param properties a list of component properties |
|
85 * @return a component |
|
86 */ |
|
87 public Component createComponent(final String name, final PropertyList properties) { |
|
88 Component component = null; |
|
89 if (Component.VALARM.equals(name)) { |
|
90 component = new VAlarm(properties); |
|
91 } |
|
92 else if (Component.VEVENT.equals(name)) { |
|
93 component = new VEvent(properties); |
|
94 } |
|
95 else if (Component.VFREEBUSY.equals(name)) { |
|
96 component = new VFreeBusy(properties); |
|
97 } |
|
98 else if (Component.VJOURNAL.equals(name)) { |
|
99 component = new VJournal(properties); |
|
100 } |
|
101 else if (Component.VTODO.equals(name)) { |
|
102 component = new VToDo(properties); |
|
103 } |
|
104 else if (Observance.STANDARD.equals(name)) { |
|
105 component = new Standard(properties); |
|
106 } |
|
107 else if (Observance.DAYLIGHT.equals(name)) { |
|
108 component = new Daylight(properties); |
|
109 } |
|
110 else if (Component.VTIMEZONE.equals(name)) { |
|
111 component = new VTimeZone(properties); |
|
112 } |
|
113 else if (Component.VVENUE.equals(name)) { |
|
114 component = new VVenue(properties); |
|
115 } |
|
116 else if (Component.VAVAILABILITY.equals(name)) { |
|
117 component = new VAvailability(properties); |
|
118 } |
|
119 else if (Component.AVAILABLE.equals(name)) { |
|
120 component = new Available(properties); |
|
121 } |
|
122 else if (isExperimentalName(name)) { |
|
123 component = new XComponent(name, properties); |
|
124 } |
|
125 else if (allowIllegalNames()) { |
|
126 component = new XComponent(name, properties); |
|
127 } |
|
128 else { |
|
129 throw new IllegalArgumentException("Illegal component [" + name |
|
130 + "]"); |
|
131 } |
|
132 return component; |
|
133 } |
|
134 |
|
135 /** |
|
136 * Creates a component which contains sub-components. Currently the only such component is VTIMEZONE. |
|
137 * @param name name of the component |
|
138 * @param properties a list of component properties |
|
139 * @param components a list of sub-components (namely standard/daylight timezones) |
|
140 * @return a component |
|
141 */ |
|
142 public Component createComponent(final String name, final PropertyList properties, |
|
143 final ComponentList components) { |
|
144 |
|
145 if (components != null) { |
|
146 Component component = null; |
|
147 if (Component.VTIMEZONE.equals(name)) { |
|
148 component = new VTimeZone(properties, components); |
|
149 } |
|
150 else if (Component.VEVENT.equals(name)) { |
|
151 component = new VEvent(properties, components); |
|
152 } |
|
153 else { |
|
154 throw new IllegalArgumentException("Illegal component [" + name |
|
155 + "]"); |
|
156 } |
|
157 return component; |
|
158 } |
|
159 return createComponent(name, properties); |
|
160 } |
|
161 |
|
162 /** |
|
163 * @param name |
|
164 * @return |
|
165 */ |
|
166 private boolean isExperimentalName(final String name) { |
|
167 return name.startsWith(Component.EXPERIMENTAL_PREFIX) |
|
168 && name.length() > Component.EXPERIMENTAL_PREFIX.length(); |
|
169 } |
|
170 |
|
171 /** |
|
172 * @return true if non-standard names are allowed, otherwise false |
|
173 */ |
|
174 protected boolean allowIllegalNames() { |
|
175 return CompatibilityHints |
|
176 .isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING); |
|
177 } |
|
178 } |