001    package biweekly.property.marshaller;
002    
003    import java.util.List;
004    
005    import biweekly.io.CannotParseException;
006    import biweekly.io.xml.XCalElement;
007    import biweekly.parameter.ICalParameters;
008    import biweekly.parameter.Value;
009    import biweekly.property.IntegerProperty;
010    
011    /*
012     Copyright (c) 2013, Michael Angstadt
013     All rights reserved.
014    
015     Redistribution and use in source and binary forms, with or without
016     modification, are permitted provided that the following conditions are met: 
017    
018     1. Redistributions of source code must retain the above copyright notice, this
019     list of conditions and the following disclaimer. 
020     2. Redistributions in binary form must reproduce the above copyright notice,
021     this list of conditions and the following disclaimer in the documentation
022     and/or other materials provided with the distribution. 
023    
024     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
025     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
026     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
027     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
028     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
029     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
030     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
031     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
033     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034     */
035    
036    /**
037     * Marshals properties that have integer values.
038     * @author Michael Angstadt
039     */
040    public abstract class IntegerPropertyMarshaller<T extends IntegerProperty> extends ICalPropertyMarshaller<T> {
041            public IntegerPropertyMarshaller(Class<T> clazz, String propertyName) {
042                    super(clazz, propertyName);
043            }
044    
045            @Override
046            protected String _writeText(T property) {
047                    Integer value = property.getValue();
048                    return (value == null) ? "" : value.toString();
049            }
050    
051            @Override
052            protected T _parseText(String value, ICalParameters parameters, List<String> warnings) {
053                    value = unescape(value);
054                    return parse(value);
055            }
056    
057            @Override
058            protected void _writeXml(T property, XCalElement element) {
059                    Integer value = property.getValue();
060                    if (value != null) {
061                            element.append(Value.INTEGER, value.toString());
062                    }
063            }
064    
065            @Override
066            protected T _parseXml(XCalElement element, ICalParameters parameters, List<String> warnings) {
067                    return parse(element.first(Value.INTEGER));
068            }
069    
070            protected T parse(String value) {
071                    if (value == null || value.length() == 0) {
072                            return newInstance(null);
073                    }
074    
075                    try {
076                            Integer intValue = Integer.valueOf(value);
077                            return newInstance(intValue);
078                    } catch (NumberFormatException e) {
079                            throw new CannotParseException("Could not parse integer value.");
080                    }
081            }
082    
083            protected abstract T newInstance(Integer value);
084    }