001 package biweekly.property.marshaller;
002
003 import java.util.List;
004
005 import biweekly.ICalDataType;
006 import biweekly.io.CannotParseException;
007 import biweekly.io.json.JCalValue;
008 import biweekly.io.xml.XCalElement;
009 import biweekly.parameter.ICalParameters;
010 import biweekly.property.DurationProperty;
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 DurationProperty} properties.
040 * @author Michael Angstadt
041 */
042 public class DurationPropertyMarshaller extends ICalPropertyMarshaller<DurationProperty> {
043 public DurationPropertyMarshaller() {
044 super(DurationProperty.class, "DURATION", ICalDataType.DURATION);
045 }
046
047 @Override
048 protected String _writeText(DurationProperty property) {
049 Duration duration = property.getValue();
050 if (duration != null) {
051 return duration.toString();
052 }
053
054 return "";
055 }
056
057 @Override
058 protected DurationProperty _parseText(String value, ICalDataType dataType, ICalParameters parameters, List<String> warnings) {
059 value = unescape(value);
060 return parse(value);
061 }
062
063 @Override
064 protected void _writeXml(DurationProperty property, XCalElement element) {
065 String durationStr = null;
066
067 Duration duration = property.getValue();
068 if (duration != null) {
069 durationStr = duration.toString();
070 }
071
072 element.append(dataType(property), durationStr);
073 }
074
075 @Override
076 protected DurationProperty _parseXml(XCalElement element, ICalParameters parameters, List<String> warnings) {
077 String value = element.first(defaultDataType);
078 if (value != null) {
079 return parse(value);
080 }
081
082 throw missingXmlElements(defaultDataType);
083 }
084
085 @Override
086 protected JCalValue _writeJson(DurationProperty property) {
087 Duration value = property.getValue();
088 if (value != null) {
089 return JCalValue.single(value.toString());
090 }
091
092 return JCalValue.single("");
093 }
094
095 @Override
096 protected DurationProperty _parseJson(JCalValue value, ICalDataType dataType, ICalParameters parameters, List<String> warnings) {
097 String valueStr = value.asSingle();
098 return parse(valueStr);
099 }
100
101 private DurationProperty parse(String value) {
102 if (value == null) {
103 return new DurationProperty(null);
104 }
105
106 try {
107 Duration duration = Duration.parse(value);
108 return new DurationProperty(duration);
109 } catch (IllegalArgumentException e) {
110 throw new CannotParseException("Could not parse duration value.");
111 }
112 }
113 }