001package biweekly.io.scribe.property;
002
003import java.util.EnumSet;
004import java.util.Set;
005import java.util.regex.Matcher;
006import java.util.regex.Pattern;
007
008import biweekly.ICalDataType;
009import biweekly.ICalVersion;
010import biweekly.io.ParseContext;
011import biweekly.io.WriteContext;
012import biweekly.parameter.ICalParameters;
013import biweekly.property.Organizer;
014
015/*
016 Copyright (c) 2013-2015, Michael Angstadt
017 All rights reserved.
018
019 Redistribution and use in source and binary forms, with or without
020 modification, are permitted provided that the following conditions are met: 
021
022 1. Redistributions of source code must retain the above copyright notice, this
023 list of conditions and the following disclaimer. 
024 2. Redistributions in binary form must reproduce the above copyright notice,
025 this list of conditions and the following disclaimer in the documentation
026 and/or other materials provided with the distribution. 
027
028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
029 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
030 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
032 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
034 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
037 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038 */
039
040/**
041 * Marshals {@link Organizer} properties.
042 * @author Michael Angstadt
043 */
044public class OrganizerScribe extends ICalPropertyScribe<Organizer> {
045        public OrganizerScribe() {
046                super(Organizer.class, "ORGANIZER", ICalDataType.CAL_ADDRESS);
047        }
048
049        @Override
050        protected ICalParameters _prepareParameters(Organizer property, WriteContext context) {
051                //CN parameter
052                String name = property.getCommonName();
053                if (name != null) {
054                        ICalParameters copy = new ICalParameters(property.getParameters());
055                        copy.put(ICalParameters.CN, name);
056                        return copy;
057                }
058
059                return super._prepareParameters(property, context);
060        }
061
062        @Override
063        protected Organizer _parseText(String value, ICalDataType dataType, ICalParameters parameters, ParseContext context) {
064                String name = parameters.first(ICalParameters.CN);
065                if (name != null) {
066                        parameters.remove(ICalParameters.CN, name);
067                }
068
069                String uri = null, email = null;
070                Pattern p = Pattern.compile("^mailto:(.*?)$");
071                Matcher m = p.matcher(value);
072                if (m.find()) {
073                        email = m.group(1);
074                } else {
075                        uri = value;
076                }
077
078                Organizer organizer = new Organizer(name, email);
079                organizer.setUri(uri);
080                return organizer;
081        }
082
083        @Override
084        protected String _writeText(Organizer property, WriteContext context) {
085                String uri = property.getUri();
086                if (uri != null) {
087                        return uri;
088                }
089
090                String email = property.getEmail();
091                if (email != null) {
092                        return "mailto:" + email;
093                }
094
095                return "";
096        }
097
098        @Override
099        public Set<ICalVersion> getSupportedVersions() {
100                return EnumSet.of(ICalVersion.V2_0_DEPRECATED, ICalVersion.V2_0);
101        }
102}