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.UtcOffsetProperty;
011    import biweekly.util.UtcOffset;
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 properties that have UTC offset values.
040     * @param <T> the property class
041     * @author Michael Angstadt
042     */
043    public abstract class UtcOffsetPropertyMarshaller<T extends UtcOffsetProperty> extends ICalPropertyMarshaller<T> {
044            public UtcOffsetPropertyMarshaller(Class<T> clazz, String propertyName) {
045                    super(clazz, propertyName, ICalDataType.UTC_OFFSET);
046            }
047    
048            @Override
049            protected String _writeText(T property) {
050                    UtcOffset offset = property.getOffset();
051                    if (offset != null) {
052                            return offset.toString(false);
053                    }
054    
055                    return "";
056            }
057    
058            @Override
059            protected T _parseText(String value, ICalDataType dataType, ICalParameters parameters, List<String> warnings) {
060                    value = unescape(value);
061                    return parse(value);
062            }
063    
064            @Override
065            protected void _writeXml(T property, XCalElement element) {
066                    String offsetStr = null;
067    
068                    UtcOffset offset = property.getOffset();
069                    if (offset != null) {
070                            offsetStr = offset.toString(true);
071                    }
072    
073                    element.append(dataType(property), offsetStr);
074            }
075    
076            @Override
077            protected T _parseXml(XCalElement element, ICalParameters parameters, List<String> 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(T property) {
088                    UtcOffset offset = property.getOffset();
089                    if (offset != null) {
090                            return JCalValue.single(offset.toString(true));
091                    }
092    
093                    return JCalValue.single("");
094            }
095    
096            @Override
097            protected T _parseJson(JCalValue value, ICalDataType dataType, ICalParameters parameters, List<String> warnings) {
098                    return parse(value.asSingle());
099            }
100    
101            protected abstract T newInstance(UtcOffset offset);
102    
103            private T parse(String value) {
104                    if (value == null) {
105                            return newInstance(null);
106                    }
107    
108                    try {
109                            return newInstance(UtcOffset.parse(value));
110                    } catch (IllegalArgumentException e) {
111                            throw new CannotParseException("Could not parse offset string.");
112                    }
113            }
114    }