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