001 package biweekly.property.marshaller; 002 003 import java.util.Date; 004 import java.util.List; 005 006 import biweekly.ICalDataType; 007 import biweekly.io.CannotParseException; 008 import biweekly.io.json.JCalValue; 009 import biweekly.io.xml.XCalElement; 010 import biweekly.parameter.ICalParameters; 011 import biweekly.property.DateOrDateTimeProperty; 012 import biweekly.util.DateTimeComponents; 013 import biweekly.util.ICalDateFormatter; 014 015 /* 016 Copyright (c) 2013, 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 either "date" or "date-time" values. 042 * @param <T> the property class 043 * @author Michael Angstadt 044 */ 045 public abstract class DateOrDateTimePropertyMarshaller<T extends DateOrDateTimeProperty> extends ICalPropertyMarshaller<T> { 046 public DateOrDateTimePropertyMarshaller(Class<T> clazz, String propertyName) { 047 super(clazz, propertyName, ICalDataType.DATE_TIME); 048 } 049 050 @Override 051 protected ICalDataType _dataType(T property) { 052 return (property.getRawComponents() != null || property.getValue() == null || property.hasTime()) ? ICalDataType.DATE_TIME : ICalDataType.DATE; 053 } 054 055 @Override 056 protected String _writeText(T property) { 057 DateTimeComponents components = property.getRawComponents(); 058 if (components != null) { 059 return components.toString(false); 060 } 061 062 Date value = property.getValue(); 063 if (value != null) { 064 return date(value).time(property.hasTime()).tz(property.isLocalTime(), property.getTimezoneId()).write(); 065 } 066 067 return ""; 068 } 069 070 @Override 071 protected T _parseText(String value, ICalDataType dataType, ICalParameters parameters, List<String> warnings) { 072 value = unescape(value); 073 return parse(value, parameters, warnings); 074 } 075 076 @Override 077 protected void _writeXml(T property, XCalElement element) { 078 String dateStr = null; 079 080 Date value = property.getValue(); 081 DateTimeComponents components = property.getRawComponents(); 082 if (components != null) { 083 dateStr = components.toString(true); 084 } else if (value != null) { 085 dateStr = date(value).time(property.hasTime()).tz(property.isLocalTime(), property.getTimezoneId()).extended(true).write(); 086 } 087 088 element.append(dataType(property), dateStr); 089 } 090 091 @Override 092 protected T _parseXml(XCalElement element, ICalParameters parameters, List<String> warnings) { 093 String value = element.first(ICalDataType.DATE_TIME); 094 if (value == null) { 095 value = element.first(ICalDataType.DATE); 096 } 097 098 if (value != null) { 099 return parse(value, parameters, warnings); 100 } 101 102 throw missingXmlElements(ICalDataType.DATE_TIME, ICalDataType.DATE); 103 } 104 105 @Override 106 protected JCalValue _writeJson(T property) { 107 DateTimeComponents components = property.getRawComponents(); 108 if (components != null) { 109 return JCalValue.single(components.toString(true)); 110 } 111 112 Date value = property.getValue(); 113 if (value != null) { 114 return JCalValue.single(date(value).time(property.hasTime()).tz(property.isLocalTime(), property.getTimezoneId()).extended(true).write()); 115 } 116 117 return JCalValue.single(""); 118 } 119 120 @Override 121 protected T _parseJson(JCalValue value, ICalDataType dataType, ICalParameters parameters, List<String> warnings) { 122 String valueStr = value.asSingle(); 123 return parse(valueStr, parameters, warnings); 124 } 125 126 protected abstract T newInstance(Date date, boolean hasTime); 127 128 private T parse(String value, ICalParameters parameters, List<String> warnings) { 129 if (value == null) { 130 return newInstance(null, true); 131 } 132 133 Date date; 134 try { 135 date = date(value).tzid(parameters.getTimezoneId(), warnings).parse(); 136 } catch (IllegalArgumentException e) { 137 throw new CannotParseException("Could not parse date-time value."); 138 } 139 140 DateTimeComponents components; 141 try { 142 components = DateTimeComponents.parse(value); 143 } catch (IllegalArgumentException e) { 144 warnings.add("Could not parse the raw date-time components: " + value); 145 components = null; 146 } 147 148 boolean hasTime = ICalDateFormatter.dateHasTime(value); 149 boolean localTz = !ICalDateFormatter.dateHasTimezone(value) && parameters.getTimezoneId() == null; 150 151 T prop = newInstance(date, hasTime); 152 prop.setRawComponents(components); 153 prop.setLocalTime(localTz); 154 return prop; 155 } 156 }