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