001    package biweekly.property.marshaller;
002    
003    import java.util.List;
004    
005    import biweekly.ICalDataType;
006    import biweekly.io.json.JCalValue;
007    import biweekly.io.xml.XCalElement;
008    import biweekly.parameter.ICalParameters;
009    import biweekly.property.ListProperty;
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 contain a list of values.
038     * @param <T> the property class
039     * @param <V> the value class
040     * @author Michael Angstadt
041     */
042    public abstract class ListPropertyMarshaller<T extends ListProperty<V>, V> extends ICalPropertyMarshaller<T> {
043            public ListPropertyMarshaller(Class<T> clazz, String propertyName) {
044                    this(clazz, propertyName, ICalDataType.TEXT);
045            }
046    
047            public ListPropertyMarshaller(Class<T> clazz, String propertyName, ICalDataType dataType) {
048                    super(clazz, propertyName, dataType);
049            }
050    
051            @Override
052            protected String _writeText(final T property) {
053                    return list(property.getValues(), new ListCallback<V>() {
054                            public String asString(V value) {
055                                    return writeValue(property, value);
056                            }
057                    });
058            }
059    
060            @Override
061            protected T _parseText(String value, ICalDataType dataType, ICalParameters parameters, List<String> warnings) {
062                    return parse(list(value), dataType, parameters, warnings);
063            }
064    
065            @Override
066            protected void _writeXml(T property, XCalElement element) {
067                    for (V value : property.getValues()) {
068                            String valueStr = writeValue(property, value);
069                            element.append(dataType(property), valueStr);
070                    }
071            }
072    
073            @Override
074            protected T _parseXml(XCalElement element, ICalParameters parameters, List<String> warnings) {
075                    List<String> values = element.all(defaultDataType);
076                    if (!values.isEmpty()) {
077                            return parse(values, defaultDataType, parameters, warnings);
078                    }
079    
080                    throw missingXmlElements(defaultDataType);
081            }
082    
083            @Override
084            protected JCalValue _writeJson(T property) {
085                    List<V> values = property.getValues();
086                    if (!values.isEmpty()) {
087                            return JCalValue.multi(property.getValues());
088                    }
089    
090                    return JCalValue.single("");
091            }
092    
093            @Override
094            protected T _parseJson(JCalValue value, ICalDataType dataType, ICalParameters parameters, List<String> warnings) {
095                    return parse(value.asMulti(), dataType, parameters, warnings);
096            }
097    
098            private T parse(List<String> valueStrs, ICalDataType dataType, ICalParameters parameters, List<String> warnings) {
099                    T property = newInstance(dataType, parameters);
100    
101                    for (String valueStr : valueStrs) {
102                            V value = readValue(valueStr, dataType, parameters, warnings);
103                            property.addValue(value);
104                    }
105    
106                    return property;
107            }
108    
109            protected abstract T newInstance(ICalDataType dataType, ICalParameters parameters);
110    
111            protected abstract String writeValue(T property, V value);
112    
113            protected abstract V readValue(String value, ICalDataType dataType, ICalParameters parameters, List<String> warnings);
114    }