001package biweekly.io.scribe.property; 002 003import java.util.List; 004 005import biweekly.ICalDataType; 006import biweekly.Warning; 007import biweekly.io.CannotParseException; 008import biweekly.io.json.JCalValue; 009import biweekly.io.xml.XCalElement; 010import biweekly.parameter.ICalParameters; 011import biweekly.property.UtcOffsetProperty; 012import biweekly.util.UtcOffset; 013 014/* 015 Copyright (c) 2013, Michael Angstadt 016 All rights reserved. 017 018 Redistribution and use in source and binary forms, with or without 019 modification, are permitted provided that the following conditions are met: 020 021 1. Redistributions of source code must retain the above copyright notice, this 022 list of conditions and the following disclaimer. 023 2. Redistributions in binary form must reproduce the above copyright notice, 024 this list of conditions and the following disclaimer in the documentation 025 and/or other materials provided with the distribution. 026 027 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 028 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 029 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 030 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 031 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 032 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 033 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 034 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 035 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 036 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 037 */ 038 039/** 040 * Marshals properties that have UTC offset values. 041 * @param <T> the property class 042 * @author Michael Angstadt 043 */ 044public abstract class UtcOffsetPropertyScribe<T extends UtcOffsetProperty> extends ICalPropertyScribe<T> { 045 public UtcOffsetPropertyScribe(Class<T> clazz, String propertyName) { 046 super(clazz, propertyName, ICalDataType.UTC_OFFSET); 047 } 048 049 @Override 050 protected String _writeText(T property) { 051 UtcOffset offset = property.getValue(); 052 if (offset != null) { 053 return offset.toString(false); 054 } 055 056 return ""; 057 } 058 059 @Override 060 protected T _parseText(String value, ICalDataType dataType, ICalParameters parameters, List<Warning> warnings) { 061 value = unescape(value); 062 return parse(value); 063 } 064 065 @Override 066 protected void _writeXml(T property, XCalElement element) { 067 String offsetStr = null; 068 069 UtcOffset offset = property.getValue(); 070 if (offset != null) { 071 offsetStr = offset.toString(true); 072 } 073 074 element.append(dataType(property), offsetStr); 075 } 076 077 @Override 078 protected T _parseXml(XCalElement element, ICalParameters parameters, List<Warning> warnings) { 079 String value = element.first(defaultDataType); 080 if (value != null) { 081 return parse(value); 082 } 083 084 throw missingXmlElements(defaultDataType); 085 } 086 087 @Override 088 protected JCalValue _writeJson(T property) { 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, List<Warning> warnings) { 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}