001package biweekly.io.scribe.property;
002
003import java.util.Date;
004
005import biweekly.ICalDataType;
006import biweekly.io.CannotParseException;
007import biweekly.io.ParseContext;
008import biweekly.io.WriteContext;
009import biweekly.io.json.JCalValue;
010import biweekly.io.xml.XCalElement;
011import biweekly.parameter.ICalParameters;
012import biweekly.property.DateTimeProperty;
013import biweekly.util.ICalDate;
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 properties that have "date-time" values. These values will always be
042 * formatted in the UTC timezone.
043 * @param <T> the property class
044 * @author Michael Angstadt
045 */
046public abstract class DateTimePropertyScribe<T extends DateTimeProperty> extends ICalPropertyScribe<T> {
047        public DateTimePropertyScribe(Class<T> clazz, String propertyName) {
048                super(clazz, propertyName, ICalDataType.DATE_TIME);
049        }
050
051        @Override
052        protected String _writeText(T property, WriteContext context) {
053                Date value = property.getValue();
054                return date(value).utc(true).extended(false).write();
055        }
056
057        @Override
058        protected T _parseText(String value, ICalDataType dataType, ICalParameters parameters, ParseContext context) {
059                value = unescape(value);
060                return parse(value, parameters, context);
061        }
062
063        @Override
064        protected void _writeXml(T property, XCalElement element, WriteContext context) {
065                ICalDataType dataType = dataType(property, null);
066                Date value = property.getValue();
067                String dateStr = date(value).utc(true).extended(true).write();
068
069                element.append(dataType, dateStr);
070        }
071
072        @Override
073        protected T _parseXml(XCalElement element, ICalParameters parameters, ParseContext context) {
074                ICalDataType dataType = defaultDataType(context.getVersion());
075                String value = element.first(dataType);
076                if (value != null) {
077                        return parse(value, parameters, context);
078                }
079
080                throw missingXmlElements(dataType);
081        }
082
083        @Override
084        protected JCalValue _writeJson(T property, WriteContext context) {
085                Date value = property.getValue();
086                String dateStr = date(value).utc(true).extended(true).write();
087                return JCalValue.single(dateStr);
088        }
089
090        @Override
091        protected T _parseJson(JCalValue value, ICalDataType dataType, ICalParameters parameters, ParseContext context) {
092                String valueStr = value.asSingle();
093                return parse(valueStr, parameters, context);
094        }
095
096        private T parse(String value, ICalParameters parameters, ParseContext context) {
097                ICalDate date;
098                try {
099                        date = date(value).parse();
100                } catch (IllegalArgumentException e) {
101                        throw new CannotParseException(17);
102                }
103
104                T property = newInstance(date);
105                context.addDate(date, property, parameters);
106                return property;
107        }
108
109        protected abstract T newInstance(Date date);
110}