1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/model/ComponentFactory.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,178 @@ 1.4 +/** 1.5 + * Copyright (c) 2012, Ben Fortuna 1.6 + * All rights reserved. 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1.12 + * o Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * o Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * o Neither the name of Ben Fortuna nor the names of any other contributors 1.20 + * may be used to endorse or promote products derived from this software 1.21 + * without specific prior written permission. 1.22 + * 1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + */ 1.35 +package net.fortuna.ical4j.model; 1.36 + 1.37 +import net.fortuna.ical4j.model.component.Available; 1.38 +import net.fortuna.ical4j.model.component.Daylight; 1.39 +import net.fortuna.ical4j.model.component.Observance; 1.40 +import net.fortuna.ical4j.model.component.Standard; 1.41 +import net.fortuna.ical4j.model.component.VAlarm; 1.42 +import net.fortuna.ical4j.model.component.VAvailability; 1.43 +import net.fortuna.ical4j.model.component.VEvent; 1.44 +import net.fortuna.ical4j.model.component.VFreeBusy; 1.45 +import net.fortuna.ical4j.model.component.VJournal; 1.46 +import net.fortuna.ical4j.model.component.VTimeZone; 1.47 +import net.fortuna.ical4j.model.component.VToDo; 1.48 +import net.fortuna.ical4j.model.component.VVenue; 1.49 +import net.fortuna.ical4j.model.component.XComponent; 1.50 +import net.fortuna.ical4j.util.CompatibilityHints; 1.51 + 1.52 +/** 1.53 + * $Id$ [05-Apr-2004] 1.54 + * 1.55 + * A factory for creating iCalendar components. Note that if relaxed parsing is enabled (via specifying the system 1.56 + * property: icalj.parsing.relaxed=true) illegal component names are allowed. 1.57 + * @author Ben Fortuna 1.58 + */ 1.59 +public final class ComponentFactory { 1.60 + 1.61 + private static ComponentFactory instance = new ComponentFactory(); 1.62 + 1.63 + /** 1.64 + * Constructor made private to prevent instantiation. 1.65 + */ 1.66 + private ComponentFactory() { 1.67 + } 1.68 + 1.69 + /** 1.70 + * @return Returns the instance. 1.71 + */ 1.72 + public static ComponentFactory getInstance() { 1.73 + return instance; 1.74 + } 1.75 + 1.76 + /** 1.77 + * @param name a component name 1.78 + * @return a new component instance of the specified type 1.79 + */ 1.80 + public Component createComponent(final String name) { 1.81 + return createComponent(name, new PropertyList()); 1.82 + } 1.83 + 1.84 + /** 1.85 + * Creates a component. 1.86 + * @param name name of the component 1.87 + * @param properties a list of component properties 1.88 + * @return a component 1.89 + */ 1.90 + public Component createComponent(final String name, final PropertyList properties) { 1.91 + Component component = null; 1.92 + if (Component.VALARM.equals(name)) { 1.93 + component = new VAlarm(properties); 1.94 + } 1.95 + else if (Component.VEVENT.equals(name)) { 1.96 + component = new VEvent(properties); 1.97 + } 1.98 + else if (Component.VFREEBUSY.equals(name)) { 1.99 + component = new VFreeBusy(properties); 1.100 + } 1.101 + else if (Component.VJOURNAL.equals(name)) { 1.102 + component = new VJournal(properties); 1.103 + } 1.104 + else if (Component.VTODO.equals(name)) { 1.105 + component = new VToDo(properties); 1.106 + } 1.107 + else if (Observance.STANDARD.equals(name)) { 1.108 + component = new Standard(properties); 1.109 + } 1.110 + else if (Observance.DAYLIGHT.equals(name)) { 1.111 + component = new Daylight(properties); 1.112 + } 1.113 + else if (Component.VTIMEZONE.equals(name)) { 1.114 + component = new VTimeZone(properties); 1.115 + } 1.116 + else if (Component.VVENUE.equals(name)) { 1.117 + component = new VVenue(properties); 1.118 + } 1.119 + else if (Component.VAVAILABILITY.equals(name)) { 1.120 + component = new VAvailability(properties); 1.121 + } 1.122 + else if (Component.AVAILABLE.equals(name)) { 1.123 + component = new Available(properties); 1.124 + } 1.125 + else if (isExperimentalName(name)) { 1.126 + component = new XComponent(name, properties); 1.127 + } 1.128 + else if (allowIllegalNames()) { 1.129 + component = new XComponent(name, properties); 1.130 + } 1.131 + else { 1.132 + throw new IllegalArgumentException("Illegal component [" + name 1.133 + + "]"); 1.134 + } 1.135 + return component; 1.136 + } 1.137 + 1.138 + /** 1.139 + * Creates a component which contains sub-components. Currently the only such component is VTIMEZONE. 1.140 + * @param name name of the component 1.141 + * @param properties a list of component properties 1.142 + * @param components a list of sub-components (namely standard/daylight timezones) 1.143 + * @return a component 1.144 + */ 1.145 + public Component createComponent(final String name, final PropertyList properties, 1.146 + final ComponentList components) { 1.147 + 1.148 + if (components != null) { 1.149 + Component component = null; 1.150 + if (Component.VTIMEZONE.equals(name)) { 1.151 + component = new VTimeZone(properties, components); 1.152 + } 1.153 + else if (Component.VEVENT.equals(name)) { 1.154 + component = new VEvent(properties, components); 1.155 + } 1.156 + else { 1.157 + throw new IllegalArgumentException("Illegal component [" + name 1.158 + + "]"); 1.159 + } 1.160 + return component; 1.161 + } 1.162 + return createComponent(name, properties); 1.163 + } 1.164 + 1.165 + /** 1.166 + * @param name 1.167 + * @return 1.168 + */ 1.169 + private boolean isExperimentalName(final String name) { 1.170 + return name.startsWith(Component.EXPERIMENTAL_PREFIX) 1.171 + && name.length() > Component.EXPERIMENTAL_PREFIX.length(); 1.172 + } 1.173 + 1.174 + /** 1.175 + * @return true if non-standard names are allowed, otherwise false 1.176 + */ 1.177 + protected boolean allowIllegalNames() { 1.178 + return CompatibilityHints 1.179 + .isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING); 1.180 + } 1.181 +}