001package biweekly.io.scribe.property; 002 003import biweekly.ICalDataType; 004import biweekly.ICalVersion; 005import biweekly.io.CannotParseException; 006import biweekly.io.ParseContext; 007import biweekly.io.WriteContext; 008import biweekly.io.json.JCalValue; 009import biweekly.io.xml.XCalElement; 010import biweekly.parameter.ICalParameters; 011import biweekly.property.DateOrDateTimeProperty; 012import biweekly.util.ICalDate; 013 014/* 015 Copyright (c) 2013-2015, Michael Angstadt 016 All rights reserved. 017 018 Redistribution and use in source and binary forms, with or without 019 modification, are permitted provided that the following conditions are met: 020 021 1. Redistributions of source code must retain the above copyright notice, this 022 list of conditions and the following disclaimer. 023 2. Redistributions in binary form must reproduce the above copyright notice, 024 this list of conditions and the following disclaimer in the documentation 025 and/or other materials provided with the distribution. 026 027 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 028 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 029 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 030 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 031 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 032 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 033 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 034 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 035 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 036 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 037 */ 038 039/** 040 * Marshals properties that have either "date" or "date-time" values. 041 * @param <T> the property class 042 * @author Michael Angstadt 043 */ 044public abstract class DateOrDateTimePropertyScribe<T extends DateOrDateTimeProperty> extends ICalPropertyScribe<T> { 045 public DateOrDateTimePropertyScribe(Class<T> clazz, String propertyName) { 046 super(clazz, propertyName, ICalDataType.DATE_TIME); 047 } 048 049 @Override 050 protected ICalParameters _prepareParameters(T property, WriteContext context) { 051 ICalDate value = property.getValue(); 052 if (value == null) { 053 return property.getParameters(); 054 } 055 056 return handleTzidParameter(property, value.hasTime(), context); 057 } 058 059 @Override 060 protected ICalDataType _dataType(T property, ICalVersion version) { 061 ICalDate value = property.getValue(); 062 if (value == null) { 063 return ICalDataType.DATE_TIME; 064 } 065 066 return value.hasTime() ? ICalDataType.DATE_TIME : ICalDataType.DATE; 067 } 068 069 @Override 070 protected String _writeText(T property, WriteContext context) { 071 ICalDate value = property.getValue(); 072 return date(value, property, context).extended(false).write(); 073 } 074 075 @Override 076 protected T _parseText(String value, ICalDataType dataType, ICalParameters parameters, ParseContext context) { 077 value = unescape(value); 078 return parse(value, parameters, context); 079 } 080 081 @Override 082 protected void _writeXml(T property, XCalElement element, WriteContext context) { 083 ICalDataType dataType = dataType(property, null); 084 ICalDate value = property.getValue(); 085 086 String dateStr = date(value, property, context).extended(true).write(); 087 element.append(dataType, dateStr); 088 } 089 090 @Override 091 protected T _parseXml(XCalElement element, ICalParameters parameters, ParseContext context) { 092 String value = element.first(ICalDataType.DATE_TIME); 093 if (value == null) { 094 value = element.first(ICalDataType.DATE); 095 } 096 097 if (value != null) { 098 return parse(value, parameters, context); 099 } 100 101 throw missingXmlElements(ICalDataType.DATE_TIME, ICalDataType.DATE); 102 } 103 104 @Override 105 protected JCalValue _writeJson(T property, WriteContext context) { 106 ICalDate value = property.getValue(); 107 String dateStr = date(value, property, context).extended(true).write(); 108 return JCalValue.single(dateStr); 109 } 110 111 @Override 112 protected T _parseJson(JCalValue value, ICalDataType dataType, ICalParameters parameters, ParseContext context) { 113 String valueStr = value.asSingle(); 114 return parse(valueStr, parameters, context); 115 } 116 117 protected abstract T newInstance(ICalDate date); 118 119 private T parse(String value, ICalParameters parameters, ParseContext context) { 120 if (value == null) { 121 return newInstance(null); 122 } 123 124 ICalDate date; 125 try { 126 date = date(value).parse(); 127 } catch (IllegalArgumentException e) { 128 throw new CannotParseException(17); 129 } 130 131 T property = newInstance(date); 132 context.addDate(date, property, parameters); 133 return property; 134 } 135}