michael@0: /** michael@0: * Copyright (c) 2012, Ben Fortuna michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * o Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * o Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * o Neither the name of Ben Fortuna nor the names of any other contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: package net.fortuna.ical4j.model; michael@0: michael@0: import net.fortuna.ical4j.model.component.Available; michael@0: import net.fortuna.ical4j.model.component.Daylight; michael@0: import net.fortuna.ical4j.model.component.Observance; michael@0: import net.fortuna.ical4j.model.component.Standard; michael@0: import net.fortuna.ical4j.model.component.VAlarm; michael@0: import net.fortuna.ical4j.model.component.VAvailability; michael@0: import net.fortuna.ical4j.model.component.VEvent; michael@0: import net.fortuna.ical4j.model.component.VFreeBusy; michael@0: import net.fortuna.ical4j.model.component.VJournal; michael@0: import net.fortuna.ical4j.model.component.VTimeZone; michael@0: import net.fortuna.ical4j.model.component.VToDo; michael@0: import net.fortuna.ical4j.model.component.VVenue; michael@0: import net.fortuna.ical4j.model.component.XComponent; michael@0: import net.fortuna.ical4j.util.CompatibilityHints; michael@0: michael@0: /** michael@0: * $Id$ [05-Apr-2004] michael@0: * michael@0: * A factory for creating iCalendar components. Note that if relaxed parsing is enabled (via specifying the system michael@0: * property: icalj.parsing.relaxed=true) illegal component names are allowed. michael@0: * @author Ben Fortuna michael@0: */ michael@0: public final class ComponentFactory { michael@0: michael@0: private static ComponentFactory instance = new ComponentFactory(); michael@0: michael@0: /** michael@0: * Constructor made private to prevent instantiation. michael@0: */ michael@0: private ComponentFactory() { michael@0: } michael@0: michael@0: /** michael@0: * @return Returns the instance. michael@0: */ michael@0: public static ComponentFactory getInstance() { michael@0: return instance; michael@0: } michael@0: michael@0: /** michael@0: * @param name a component name michael@0: * @return a new component instance of the specified type michael@0: */ michael@0: public Component createComponent(final String name) { michael@0: return createComponent(name, new PropertyList()); michael@0: } michael@0: michael@0: /** michael@0: * Creates a component. michael@0: * @param name name of the component michael@0: * @param properties a list of component properties michael@0: * @return a component michael@0: */ michael@0: public Component createComponent(final String name, final PropertyList properties) { michael@0: Component component = null; michael@0: if (Component.VALARM.equals(name)) { michael@0: component = new VAlarm(properties); michael@0: } michael@0: else if (Component.VEVENT.equals(name)) { michael@0: component = new VEvent(properties); michael@0: } michael@0: else if (Component.VFREEBUSY.equals(name)) { michael@0: component = new VFreeBusy(properties); michael@0: } michael@0: else if (Component.VJOURNAL.equals(name)) { michael@0: component = new VJournal(properties); michael@0: } michael@0: else if (Component.VTODO.equals(name)) { michael@0: component = new VToDo(properties); michael@0: } michael@0: else if (Observance.STANDARD.equals(name)) { michael@0: component = new Standard(properties); michael@0: } michael@0: else if (Observance.DAYLIGHT.equals(name)) { michael@0: component = new Daylight(properties); michael@0: } michael@0: else if (Component.VTIMEZONE.equals(name)) { michael@0: component = new VTimeZone(properties); michael@0: } michael@0: else if (Component.VVENUE.equals(name)) { michael@0: component = new VVenue(properties); michael@0: } michael@0: else if (Component.VAVAILABILITY.equals(name)) { michael@0: component = new VAvailability(properties); michael@0: } michael@0: else if (Component.AVAILABLE.equals(name)) { michael@0: component = new Available(properties); michael@0: } michael@0: else if (isExperimentalName(name)) { michael@0: component = new XComponent(name, properties); michael@0: } michael@0: else if (allowIllegalNames()) { michael@0: component = new XComponent(name, properties); michael@0: } michael@0: else { michael@0: throw new IllegalArgumentException("Illegal component [" + name michael@0: + "]"); michael@0: } michael@0: return component; michael@0: } michael@0: michael@0: /** michael@0: * Creates a component which contains sub-components. Currently the only such component is VTIMEZONE. michael@0: * @param name name of the component michael@0: * @param properties a list of component properties michael@0: * @param components a list of sub-components (namely standard/daylight timezones) michael@0: * @return a component michael@0: */ michael@0: public Component createComponent(final String name, final PropertyList properties, michael@0: final ComponentList components) { michael@0: michael@0: if (components != null) { michael@0: Component component = null; michael@0: if (Component.VTIMEZONE.equals(name)) { michael@0: component = new VTimeZone(properties, components); michael@0: } michael@0: else if (Component.VEVENT.equals(name)) { michael@0: component = new VEvent(properties, components); michael@0: } michael@0: else { michael@0: throw new IllegalArgumentException("Illegal component [" + name michael@0: + "]"); michael@0: } michael@0: return component; michael@0: } michael@0: return createComponent(name, properties); michael@0: } michael@0: michael@0: /** michael@0: * @param name michael@0: * @return michael@0: */ michael@0: private boolean isExperimentalName(final String name) { michael@0: return name.startsWith(Component.EXPERIMENTAL_PREFIX) michael@0: && name.length() > Component.EXPERIMENTAL_PREFIX.length(); michael@0: } michael@0: michael@0: /** michael@0: * @return true if non-standard names are allowed, otherwise false michael@0: */ michael@0: protected boolean allowIllegalNames() { michael@0: return CompatibilityHints michael@0: .isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING); michael@0: } michael@0: }