001 package biweekly.property.marshaller;
002
003 import java.util.List;
004
005 import biweekly.ICalDataType;
006 import biweekly.Warning;
007 import biweekly.io.json.JCalValue;
008 import biweekly.io.xml.XCalElement;
009 import biweekly.parameter.ICalParameters;
010 import biweekly.property.Version;
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 {@link Version} properties.
039 * @author Michael Angstadt
040 */
041 public class VersionMarshaller extends ICalPropertyMarshaller<Version> {
042 public VersionMarshaller() {
043 super(Version.class, "VERSION", ICalDataType.TEXT);
044 }
045
046 @Override
047 protected String _writeText(Version property) {
048 StringBuilder sb = new StringBuilder();
049
050 if (property.getMinVersion() != null) {
051 sb.append(property.getMinVersion()).append(';');
052 }
053 if (property.getMaxVersion() != null) {
054 sb.append(property.getMaxVersion());
055 }
056
057 return sb.toString();
058 }
059
060 @Override
061 protected Version _parseText(String value, ICalDataType dataType, ICalParameters parameters, List<Warning> warnings) {
062 List<String> split = split(value, ";").unescape(true).split();
063
064 String min = null, max = null;
065 if (split.size() == 1) {
066 max = split.get(0);
067 } else {
068 min = split.get(0);
069 max = split.get(1);
070 }
071 return new Version(min, max);
072 }
073
074 @Override
075 protected void _writeXml(Version property, XCalElement element) {
076 element.append(dataType(property), property.getMaxVersion());
077 }
078
079 @Override
080 protected Version _parseXml(XCalElement element, ICalParameters parameters, List<Warning> warnings) {
081 String value = element.first(defaultDataType);
082 if (value != null) {
083 return new Version(value);
084 }
085
086 throw missingXmlElements(defaultDataType);
087 }
088
089 @Override
090 protected JCalValue _writeJson(Version property) {
091 return JCalValue.single(property.getMaxVersion());
092 }
093
094 @Override
095 protected Version _parseJson(JCalValue value, ICalDataType dataType, ICalParameters parameters, List<Warning> warnings) {
096 return new Version(value.asSingle());
097 }
098 }