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