001package biweekly.io.scribe.property;
002
003import biweekly.ICalDataType;
004import biweekly.io.WriteContext;
005import biweekly.io.json.JCalValue;
006import biweekly.io.xml.XCalElement;
007import biweekly.parameter.ICalParameters;
008import biweekly.property.DateStart;
009import biweekly.util.ICalDate;
010
011/*
012 Copyright (c) 2013-2015, Michael Angstadt
013 All rights reserved.
014
015 Redistribution and use in source and binary forms, with or without
016 modification, are permitted provided that the following conditions are met: 
017
018 1. Redistributions of source code must retain the above copyright notice, this
019 list of conditions and the following disclaimer. 
020 2. Redistributions in binary form must reproduce the above copyright notice,
021 this list of conditions and the following disclaimer in the documentation
022 and/or other materials provided with the distribution. 
023
024 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
025 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
026 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
027 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
028 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
029 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
030 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
031 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034 */
035
036/**
037 * Marshals {@link DateStart} properties.
038 * @author Michael Angstadt
039 */
040public class DateStartScribe extends DateOrDateTimePropertyScribe<DateStart> {
041        public DateStartScribe() {
042                super(DateStart.class, "DTSTART");
043        }
044
045        @Override
046        protected ICalParameters _prepareParameters(DateStart property, WriteContext context) {
047                if (isInObservance(context)) {
048                        return property.getParameters();
049                }
050                return super._prepareParameters(property, context);
051        }
052
053        @Override
054        protected String _writeText(DateStart property, WriteContext context) {
055                if (isInObservance(context)) {
056                        return write(property, false);
057                }
058                return super._writeText(property, context);
059        }
060
061        @Override
062        protected void _writeXml(DateStart property, XCalElement element, WriteContext context) {
063                if (isInObservance(context)) {
064                        String dateStr = write(property, true);
065                        ICalDataType dataType = dataType(property, null);
066                        element.append(dataType, dateStr);
067                        return;
068                }
069
070                super._writeXml(property, element, context);
071        }
072
073        @Override
074        protected JCalValue _writeJson(DateStart property, WriteContext context) {
075                if (isInObservance(context)) {
076                        return JCalValue.single(write(property, true));
077                }
078                return super._writeJson(property, context);
079        }
080
081        private String write(DateStart property, boolean extended) {
082                ICalDate value = property.getValue();
083                return date(value).observance(true).extended(extended).write();
084        }
085
086        @Override
087        protected DateStart newInstance(ICalDate date) {
088                return new DateStart(date);
089        }
090}