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