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.UtcOffsetProperty; 011import biweekly.util.UtcOffset; 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 properties that have UTC offset values. 040 * @param <T> the property class 041 * @author Michael Angstadt 042 */ 043public abstract class UtcOffsetPropertyScribe<T extends UtcOffsetProperty> extends ICalPropertyScribe<T> { 044 public UtcOffsetPropertyScribe(Class<T> clazz, String propertyName) { 045 super(clazz, propertyName, ICalDataType.UTC_OFFSET); 046 } 047 048 @Override 049 protected String _writeText(T property, WriteContext context) { 050 UtcOffset offset = property.getValue(); 051 if (offset != null) { 052 return offset.toString(false); 053 } 054 055 return ""; 056 } 057 058 @Override 059 protected T _parseText(String value, ICalDataType dataType, ICalParameters parameters, ParseContext context) { 060 value = unescape(value); 061 return parse(value); 062 } 063 064 @Override 065 protected void _writeXml(T property, XCalElement element, WriteContext context) { 066 String offsetStr = null; 067 068 UtcOffset offset = property.getValue(); 069 if (offset != null) { 070 offsetStr = offset.toString(true); 071 } 072 073 element.append(dataType(property, null), offsetStr); 074 } 075 076 @Override 077 protected T _parseXml(XCalElement element, ICalParameters parameters, ParseContext context) { 078 ICalDataType dataType = defaultDataType(context.getVersion()); 079 String value = element.first(dataType); 080 if (value != null) { 081 return parse(value); 082 } 083 084 throw missingXmlElements(dataType); 085 } 086 087 @Override 088 protected JCalValue _writeJson(T property, WriteContext context) { 089 UtcOffset offset = property.getValue(); 090 if (offset != null) { 091 return JCalValue.single(offset.toString(true)); 092 } 093 094 return JCalValue.single(""); 095 } 096 097 @Override 098 protected T _parseJson(JCalValue value, ICalDataType dataType, ICalParameters parameters, ParseContext context) { 099 return parse(value.asSingle()); 100 } 101 102 protected abstract T newInstance(UtcOffset offset); 103 104 private T parse(String value) { 105 if (value == null) { 106 return newInstance(null); 107 } 108 109 try { 110 return newInstance(UtcOffset.parse(value)); 111 } catch (IllegalArgumentException e) { 112 throw new CannotParseException(28); 113 } 114 } 115}