001package biweekly.io.scribe.property; 002 003import biweekly.ICalDataType; 004import biweekly.io.CannotParseException; 005import biweekly.io.ParseContext; 006import biweekly.io.WriteContext; 007import biweekly.io.json.JCalValue; 008import biweekly.io.xml.XCalElement; 009import biweekly.parameter.ICalParameters; 010import biweekly.property.Version; 011import biweekly.util.VersionNumber; 012 013/* 014 Copyright (c) 2013-2015, Michael Angstadt 015 All rights reserved. 016 017 Redistribution and use in source and binary forms, with or without 018 modification, are permitted provided that the following conditions are met: 019 020 1. Redistributions of source code must retain the above copyright notice, this 021 list of conditions and the following disclaimer. 022 2. Redistributions in binary form must reproduce the above copyright notice, 023 this list of conditions and the following disclaimer in the documentation 024 and/or other materials provided with the distribution. 025 026 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 027 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 028 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 029 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 030 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 031 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 032 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 033 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 034 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 035 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 036 */ 037 038/** 039 * Marshals {@link Version} properties. 040 * @author Michael Angstadt 041 */ 042public class VersionScribe extends ICalPropertyScribe<Version> { 043 public VersionScribe() { 044 super(Version.class, "VERSION", ICalDataType.TEXT); 045 } 046 047 @Override 048 protected String _writeText(Version property, WriteContext context) { 049 StringBuilder sb = new StringBuilder(); 050 051 if (property.getMinVersion() != null) { 052 sb.append(property.getMinVersion()).append(';'); 053 } 054 if (property.getMaxVersion() != null) { 055 sb.append(property.getMaxVersion()); 056 } 057 058 return sb.toString(); 059 } 060 061 @Override 062 protected Version _parseText(String value, ICalDataType dataType, ICalParameters parameters, ParseContext context) { 063 SemiStructuredIterator it = semistructured(value, true); 064 String one = it.next(); 065 String two = it.next(); 066 067 String min = null, max = null; 068 if (two == null) { 069 max = one; 070 } else { 071 min = one; 072 max = two; 073 } 074 075 return parse(min, max); 076 } 077 078 @Override 079 protected void _writeXml(Version property, XCalElement element, WriteContext context) { 080 VersionNumber max = property.getMaxVersion(); 081 String value = (max == null) ? null : max.toString(); 082 element.append(dataType(property, null), value); 083 } 084 085 @Override 086 protected Version _parseXml(XCalElement element, ICalParameters parameters, ParseContext context) { 087 ICalDataType dataType = defaultDataType(context.getVersion()); 088 String value = element.first(dataType); 089 if (value != null) { 090 return parse(null, value); 091 } 092 093 throw missingXmlElements(dataType); 094 } 095 096 @Override 097 protected JCalValue _writeJson(Version property, WriteContext context) { 098 VersionNumber max = property.getMaxVersion(); 099 String value = (max == null) ? null : max.toString(); 100 return JCalValue.single(value); 101 } 102 103 @Override 104 protected Version _parseJson(JCalValue value, ICalDataType dataType, ICalParameters parameters, ParseContext context) { 105 return parse(null, value.asSingle()); 106 } 107 108 private Version parse(String min, String max) { 109 try { 110 return new Version(min, max); 111 } catch (IllegalArgumentException e) { 112 throw new CannotParseException(30); 113 } 114 } 115}