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 java.net.URISyntaxException; michael@0: michael@0: import net.fortuna.ical4j.model.parameter.Abbrev; michael@0: import net.fortuna.ical4j.model.parameter.AltRep; michael@0: import net.fortuna.ical4j.model.parameter.Cn; michael@0: import net.fortuna.ical4j.model.parameter.CuType; michael@0: import net.fortuna.ical4j.model.parameter.DelegatedFrom; michael@0: import net.fortuna.ical4j.model.parameter.DelegatedTo; michael@0: import net.fortuna.ical4j.model.parameter.Dir; michael@0: import net.fortuna.ical4j.model.parameter.Encoding; michael@0: import net.fortuna.ical4j.model.parameter.FbType; michael@0: import net.fortuna.ical4j.model.parameter.FmtType; michael@0: import net.fortuna.ical4j.model.parameter.Language; michael@0: import net.fortuna.ical4j.model.parameter.Member; michael@0: import net.fortuna.ical4j.model.parameter.PartStat; michael@0: import net.fortuna.ical4j.model.parameter.Range; michael@0: import net.fortuna.ical4j.model.parameter.RelType; michael@0: import net.fortuna.ical4j.model.parameter.Related; michael@0: import net.fortuna.ical4j.model.parameter.Role; michael@0: import net.fortuna.ical4j.model.parameter.Rsvp; michael@0: import net.fortuna.ical4j.model.parameter.ScheduleAgent; michael@0: import net.fortuna.ical4j.model.parameter.ScheduleStatus; michael@0: import net.fortuna.ical4j.model.parameter.SentBy; michael@0: import net.fortuna.ical4j.model.parameter.Type; michael@0: import net.fortuna.ical4j.model.parameter.TzId; michael@0: import net.fortuna.ical4j.model.parameter.Value; michael@0: import net.fortuna.ical4j.model.parameter.Vvenue; michael@0: import net.fortuna.ical4j.model.parameter.XParameter; michael@0: import net.fortuna.ical4j.util.Strings; michael@0: michael@0: /** michael@0: * A factory for creating iCalendar parameters. michael@0: * michael@0: * $Id $ michael@0: * michael@0: * [05-Apr-2004] michael@0: * michael@0: * @author Ben Fortuna michael@0: */ michael@0: public class ParameterFactoryImpl extends AbstractContentFactory implements ParameterFactory { michael@0: michael@0: private static final long serialVersionUID = -4034423507432249165L; michael@0: michael@0: private static ParameterFactoryImpl instance = new ParameterFactoryImpl(); michael@0: michael@0: protected ParameterFactoryImpl() { michael@0: registerDefaultFactory(Parameter.ABBREV, new AbbrevFactory()); michael@0: registerDefaultFactory(Parameter.ALTREP, new AltRepFactory()); michael@0: registerDefaultFactory(Parameter.CN, new CnFactory()); michael@0: registerDefaultFactory(Parameter.CUTYPE, new CuTypeFactory()); michael@0: registerDefaultFactory(Parameter.DELEGATED_FROM, new DelegatedFromFactory()); michael@0: registerDefaultFactory(Parameter.DELEGATED_TO, new DelegatedToFactory()); michael@0: registerDefaultFactory(Parameter.DIR, new DirFactory()); michael@0: registerDefaultFactory(Parameter.ENCODING, new EncodingFactory()); michael@0: registerDefaultFactory(Parameter.FMTTYPE, new FmtTypeFactory()); michael@0: registerDefaultFactory(Parameter.FBTYPE, new FbTypeFactory()); michael@0: registerDefaultFactory(Parameter.LANGUAGE, new LanguageFactory()); michael@0: registerDefaultFactory(Parameter.MEMBER, new MemberFactory()); michael@0: registerDefaultFactory(Parameter.PARTSTAT, new PartStatFactory()); michael@0: registerDefaultFactory(Parameter.RANGE, new RangeFactory()); michael@0: registerDefaultFactory(Parameter.RELATED, new RelatedFactory()); michael@0: registerDefaultFactory(Parameter.RELTYPE, new RelTypeFactory()); michael@0: registerDefaultFactory(Parameter.ROLE, new RoleFactory()); michael@0: registerDefaultFactory(Parameter.RSVP, new RsvpFactory()); michael@0: registerDefaultFactory(Parameter.SCHEDULE_AGENT, new ScheduleAgentFactory()); michael@0: registerDefaultFactory(Parameter.SCHEDULE_STATUS, new ScheduleStatusFactory()); michael@0: registerDefaultFactory(Parameter.SENT_BY, new SentByFactory()); michael@0: registerDefaultFactory(Parameter.TYPE, new TypeFactory()); michael@0: registerDefaultFactory(Parameter.TZID, new TzIdFactory()); michael@0: registerDefaultFactory(Parameter.VALUE, new ValueFactory()); michael@0: registerDefaultFactory(Parameter.VVENUE, new VvenueFactory()); michael@0: } michael@0: michael@0: /** michael@0: * @return Returns the instance. michael@0: */ michael@0: public static ParameterFactoryImpl getInstance() { michael@0: return instance; michael@0: } michael@0: michael@0: /** michael@0: * Creates a parameter. michael@0: * @param name name of the parameter michael@0: * @param value a parameter value michael@0: * @return a component michael@0: * @throws URISyntaxException thrown when the specified string is not a valid representation of a URI for selected michael@0: * parameters michael@0: */ michael@0: public Parameter createParameter(final String name, final String value) michael@0: throws URISyntaxException { michael@0: final ParameterFactory factory = (ParameterFactory) getFactory(name); michael@0: Parameter parameter = null; michael@0: if (factory != null) { michael@0: parameter = factory.createParameter(name, value); michael@0: } michael@0: else if (isExperimentalName(name)) { michael@0: parameter = new XParameter(name, value); michael@0: } michael@0: else if (allowIllegalNames()) { michael@0: parameter = new XParameter(name, value); michael@0: } michael@0: else { michael@0: throw new IllegalArgumentException("Invalid parameter name: " michael@0: + name); michael@0: } michael@0: return parameter; 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(Parameter.EXPERIMENTAL_PREFIX) michael@0: && name.length() > Parameter.EXPERIMENTAL_PREFIX.length(); michael@0: } michael@0: michael@0: private static class AbbrevFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, final String value) throws URISyntaxException { michael@0: return new Abbrev(value); michael@0: } michael@0: } michael@0: michael@0: private static class AltRepFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, final String value) throws URISyntaxException { michael@0: return new AltRep(value); michael@0: } michael@0: } michael@0: michael@0: private static class CnFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: return new Cn(value); michael@0: } michael@0: } michael@0: michael@0: private static class CuTypeFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, final String value) throws URISyntaxException { michael@0: CuType parameter = new CuType(value); michael@0: if (CuType.INDIVIDUAL.equals(parameter)) { michael@0: parameter = CuType.INDIVIDUAL; michael@0: } michael@0: else if (CuType.GROUP.equals(parameter)) { michael@0: parameter = CuType.GROUP; michael@0: } michael@0: else if (CuType.RESOURCE.equals(parameter)) { michael@0: parameter = CuType.RESOURCE; michael@0: } michael@0: else if (CuType.ROOM.equals(parameter)) { michael@0: parameter = CuType.ROOM; michael@0: } michael@0: else if (CuType.UNKNOWN.equals(parameter)) { michael@0: parameter = CuType.UNKNOWN; michael@0: } michael@0: return parameter; michael@0: } michael@0: } michael@0: michael@0: private static class DelegatedFromFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: return new DelegatedFrom(value); michael@0: } michael@0: } michael@0: michael@0: private static class DelegatedToFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: return new DelegatedTo(value); michael@0: } michael@0: } michael@0: michael@0: private static class DirFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: return new Dir(value); michael@0: } michael@0: } michael@0: michael@0: private static class EncodingFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: Encoding parameter = new Encoding(value); michael@0: if (Encoding.EIGHT_BIT.equals(parameter)) { michael@0: parameter = Encoding.EIGHT_BIT; michael@0: } michael@0: else if (Encoding.BASE64.equals(parameter)) { michael@0: parameter = Encoding.BASE64; michael@0: } michael@0: return parameter; michael@0: } michael@0: } michael@0: michael@0: private static class FmtTypeFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: return new FmtType(value); michael@0: } michael@0: } michael@0: michael@0: private static class FbTypeFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: FbType parameter = new FbType(value); michael@0: if (FbType.FREE.equals(parameter)) { michael@0: parameter = FbType.FREE; michael@0: } michael@0: else if (FbType.BUSY.equals(parameter)) { michael@0: parameter = FbType.BUSY; michael@0: } michael@0: else if (FbType.BUSY_TENTATIVE.equals(parameter)) { michael@0: parameter = FbType.BUSY_TENTATIVE; michael@0: } michael@0: else if (FbType.BUSY_UNAVAILABLE.equals(parameter)) { michael@0: parameter = FbType.BUSY_UNAVAILABLE; michael@0: } michael@0: return parameter; michael@0: } michael@0: } michael@0: michael@0: private static class LanguageFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: return new Language(value); michael@0: } michael@0: } michael@0: michael@0: private static class MemberFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: return new Member(value); michael@0: } michael@0: } michael@0: michael@0: private static class PartStatFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: PartStat parameter = new PartStat(value); michael@0: if (PartStat.NEEDS_ACTION.equals(parameter)) { michael@0: parameter = PartStat.NEEDS_ACTION; michael@0: } michael@0: else if (PartStat.ACCEPTED.equals(parameter)) { michael@0: parameter = PartStat.ACCEPTED; michael@0: } michael@0: else if (PartStat.DECLINED.equals(parameter)) { michael@0: parameter = PartStat.DECLINED; michael@0: } michael@0: else if (PartStat.TENTATIVE.equals(parameter)) { michael@0: parameter = PartStat.TENTATIVE; michael@0: } michael@0: else if (PartStat.DELEGATED.equals(parameter)) { michael@0: parameter = PartStat.DELEGATED; michael@0: } michael@0: else if (PartStat.COMPLETED.equals(parameter)) { michael@0: parameter = PartStat.COMPLETED; michael@0: } michael@0: else if (PartStat.IN_PROCESS.equals(parameter)) { michael@0: parameter = PartStat.IN_PROCESS; michael@0: } michael@0: return parameter; michael@0: } michael@0: } michael@0: michael@0: private static class RangeFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: Range parameter = new Range(value); michael@0: if (Range.THISANDFUTURE.equals(parameter)) { michael@0: parameter = Range.THISANDFUTURE; michael@0: } michael@0: else if (Range.THISANDPRIOR.equals(parameter)) { michael@0: parameter = Range.THISANDPRIOR; michael@0: } michael@0: return parameter; michael@0: } michael@0: } michael@0: michael@0: private static class RelatedFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: Related parameter = new Related(value); michael@0: if (Related.START.equals(parameter)) { michael@0: parameter = Related.START; michael@0: } michael@0: else if (Related.END.equals(parameter)) { michael@0: parameter = Related.END; michael@0: } michael@0: return parameter; michael@0: } michael@0: } michael@0: michael@0: private static class RelTypeFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: RelType parameter = new RelType(value); michael@0: if (RelType.PARENT.equals(parameter)) { michael@0: parameter = RelType.PARENT; michael@0: } michael@0: else if (RelType.CHILD.equals(parameter)) { michael@0: parameter = RelType.CHILD; michael@0: } michael@0: if (RelType.SIBLING.equals(parameter)) { michael@0: parameter = RelType.SIBLING; michael@0: } michael@0: return parameter; michael@0: } michael@0: } michael@0: michael@0: private static class RoleFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: Role parameter = new Role(value); michael@0: if (Role.CHAIR.equals(parameter)) { michael@0: parameter = Role.CHAIR; michael@0: } michael@0: else if (Role.REQ_PARTICIPANT.equals(parameter)) { michael@0: parameter = Role.REQ_PARTICIPANT; michael@0: } michael@0: else if (Role.OPT_PARTICIPANT.equals(parameter)) { michael@0: parameter = Role.OPT_PARTICIPANT; michael@0: } michael@0: else if (Role.NON_PARTICIPANT.equals(parameter)) { michael@0: parameter = Role.NON_PARTICIPANT; michael@0: } michael@0: return parameter; michael@0: } michael@0: } michael@0: michael@0: private static class RsvpFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: Rsvp parameter = new Rsvp(value); michael@0: if (Rsvp.TRUE.equals(parameter)) { michael@0: parameter = Rsvp.TRUE; michael@0: } michael@0: else if (Rsvp.FALSE.equals(parameter)) { michael@0: parameter = Rsvp.FALSE; michael@0: } michael@0: return parameter; michael@0: } michael@0: } michael@0: michael@0: private static class ScheduleAgentFactory implements ParameterFactory { michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: final ScheduleAgent parameter = new ScheduleAgent(value); michael@0: if (ScheduleAgent.SERVER.equals(parameter)) { michael@0: return ScheduleAgent.SERVER; michael@0: } michael@0: else if (ScheduleAgent.CLIENT.equals(parameter)) { michael@0: return ScheduleAgent.CLIENT; michael@0: } michael@0: else if (ScheduleAgent.NONE.equals(parameter)) { michael@0: return ScheduleAgent.NONE; michael@0: } michael@0: return parameter; michael@0: } michael@0: } michael@0: michael@0: private static class ScheduleStatusFactory implements ParameterFactory { michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: return new ScheduleStatus(value); michael@0: } michael@0: } michael@0: michael@0: private static class SentByFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: return new SentBy(value); michael@0: } michael@0: } michael@0: michael@0: private static class VvenueFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, final String value) michael@0: throws URISyntaxException { michael@0: return new Vvenue(value); michael@0: } michael@0: } michael@0: michael@0: private static class TypeFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: return new Type(value); michael@0: } michael@0: } michael@0: michael@0: private static class TzIdFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: return new TzId(Strings.unescape(value)); michael@0: } michael@0: } michael@0: michael@0: private static class ValueFactory implements ParameterFactory { michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public Parameter createParameter(final String name, michael@0: final String value) throws URISyntaxException { michael@0: Value parameter = new Value(value); michael@0: if (Value.BINARY.equals(parameter)) { michael@0: parameter = Value.BINARY; michael@0: } michael@0: else if (Value.BOOLEAN.equals(parameter)) { michael@0: parameter = Value.BOOLEAN; michael@0: } michael@0: else if (Value.CAL_ADDRESS.equals(parameter)) { michael@0: parameter = Value.CAL_ADDRESS; michael@0: } michael@0: else if (Value.DATE.equals(parameter)) { michael@0: parameter = Value.DATE; michael@0: } michael@0: else if (Value.DATE_TIME.equals(parameter)) { michael@0: parameter = Value.DATE_TIME; michael@0: } michael@0: else if (Value.DURATION.equals(parameter)) { michael@0: parameter = Value.DURATION; michael@0: } michael@0: else if (Value.FLOAT.equals(parameter)) { michael@0: parameter = Value.FLOAT; michael@0: } michael@0: else if (Value.INTEGER.equals(parameter)) { michael@0: parameter = Value.INTEGER; michael@0: } michael@0: else if (Value.PERIOD.equals(parameter)) { michael@0: parameter = Value.PERIOD; michael@0: } michael@0: else if (Value.RECUR.equals(parameter)) { michael@0: parameter = Value.RECUR; michael@0: } michael@0: else if (Value.TEXT.equals(parameter)) { michael@0: parameter = Value.TEXT; michael@0: } michael@0: else if (Value.TIME.equals(parameter)) { michael@0: parameter = Value.TIME; michael@0: } michael@0: else if (Value.URI.equals(parameter)) { michael@0: parameter = Value.URI; michael@0: } michael@0: else if (Value.UTC_OFFSET.equals(parameter)) { michael@0: parameter = Value.UTC_OFFSET; michael@0: } michael@0: return parameter; michael@0: } michael@0: } michael@0: michael@0: }