001 package biweekly.property.marshaller; 002 003 import java.util.List; 004 005 import biweekly.io.xml.XCalElement; 006 import biweekly.parameter.ICalParameters; 007 import biweekly.parameter.Value; 008 import biweekly.property.ListProperty; 009 import biweekly.util.StringUtils; 010 import biweekly.util.StringUtils.JoinCallback; 011 012 /* 013 Copyright (c) 2013, Michael Angstadt 014 All rights reserved. 015 016 Redistribution and use in source and binary forms, with or without 017 modification, are permitted provided that the following conditions are met: 018 019 1. Redistributions of source code must retain the above copyright notice, this 020 list of conditions and the following disclaimer. 021 2. Redistributions in binary form must reproduce the above copyright notice, 022 this list of conditions and the following disclaimer in the documentation 023 and/or other materials provided with the distribution. 024 025 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 026 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 027 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 028 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 029 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 030 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 031 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 032 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 033 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 034 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 035 */ 036 037 /** 038 * Marshals properties that contain a list of values. 039 * @author Michael Angstadt 040 */ 041 public abstract class ListPropertyMarshaller<T extends ListProperty<V>, V> extends ICalPropertyMarshaller<T> { 042 protected Value dataType; 043 044 public ListPropertyMarshaller(Class<T> clazz, String propertyName) { 045 this(clazz, propertyName, Value.TEXT); 046 } 047 048 public ListPropertyMarshaller(Class<T> clazz, String propertyName, Value dataType) { 049 super(clazz, propertyName); 050 this.dataType = dataType; 051 } 052 053 @Override 054 protected String _writeText(final T property) { 055 return StringUtils.join(property.getValues(), ",", new JoinCallback<V>() { 056 public void handle(StringBuilder sb, V value) { 057 String valueStr = writeValue(property, value); 058 sb.append(escape(valueStr)); 059 } 060 }); 061 } 062 063 @Override 064 protected T _parseText(String value, ICalParameters parameters, List<String> warnings) { 065 T property = newInstance(parameters); 066 067 String split[] = parseList(value); 068 for (String s : split) { 069 V v = readValue(s, parameters, warnings); 070 property.addValue(v); 071 } 072 073 return property; 074 } 075 076 @Override 077 protected void _writeXml(T property, XCalElement element) { 078 for (V value : property.getValues()) { 079 String valueStr = writeValue(property, value); 080 element.append(dataType, valueStr); 081 } 082 } 083 084 @Override 085 protected T _parseXml(XCalElement element, ICalParameters parameters, List<String> warnings) { 086 T property = newInstance(parameters); 087 088 for (String valueStr : element.all(dataType)) { 089 V value = readValue(valueStr, parameters, warnings); 090 property.addValue(value); 091 } 092 093 return property; 094 } 095 096 protected abstract T newInstance(ICalParameters parameters); 097 098 protected abstract String writeValue(T property, V value); 099 100 protected abstract V readValue(String value, ICalParameters parameters, List<String> warnings); 101 }