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