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.DurationProperty; 012import biweekly.util.Duration; 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 {@link DurationProperty} properties. 041 * @author Michael Angstadt 042 */ 043public class DurationPropertyScribe extends ICalPropertyScribe<DurationProperty> { 044 public DurationPropertyScribe() { 045 super(DurationProperty.class, "DURATION"); 046 } 047 048 @Override 049 protected ICalDataType _defaultDataType(ICalVersion version) { 050 return ICalDataType.DURATION; 051 } 052 053 @Override 054 protected String _writeText(DurationProperty property, WriteContext context) { 055 Duration duration = property.getValue(); 056 if (duration != null) { 057 return duration.toString(); 058 } 059 060 return ""; 061 } 062 063 @Override 064 protected DurationProperty _parseText(String value, ICalDataType dataType, ICalParameters parameters, ParseContext context) { 065 value = unescape(value); 066 return parse(value); 067 } 068 069 @Override 070 protected void _writeXml(DurationProperty property, XCalElement element, WriteContext context) { 071 String durationStr = null; 072 073 Duration duration = property.getValue(); 074 if (duration != null) { 075 durationStr = duration.toString(); 076 } 077 078 element.append(dataType(property, null), durationStr); 079 } 080 081 @Override 082 protected DurationProperty _parseXml(XCalElement element, ICalParameters parameters, ParseContext context) { 083 ICalDataType dataType = defaultDataType(context.getVersion()); 084 String value = element.first(dataType); 085 if (value != null) { 086 return parse(value); 087 } 088 089 throw missingXmlElements(dataType); 090 } 091 092 @Override 093 protected JCalValue _writeJson(DurationProperty property, WriteContext context) { 094 Duration value = property.getValue(); 095 if (value != null) { 096 return JCalValue.single(value.toString()); 097 } 098 099 return JCalValue.single(""); 100 } 101 102 @Override 103 protected DurationProperty _parseJson(JCalValue value, ICalDataType dataType, ICalParameters parameters, ParseContext context) { 104 String valueStr = value.asSingle(); 105 return parse(valueStr); 106 } 107 108 private DurationProperty parse(String value) { 109 if (value == null) { 110 return new DurationProperty(null); 111 } 112 113 try { 114 Duration duration = Duration.parse(value); 115 return new DurationProperty(duration); 116 } catch (IllegalArgumentException e) { 117 throw new CannotParseException(18); 118 } 119 } 120}