001package ezvcard.io.scribe; 002 003import com.github.mangstadt.vinnie.io.VObjectPropertyValues; 004 005import ezvcard.VCardDataType; 006import ezvcard.VCardVersion; 007import ezvcard.io.ParseContext; 008import ezvcard.io.html.HCardElement; 009import ezvcard.io.json.JCardValue; 010import ezvcard.io.text.WriteContext; 011import ezvcard.io.xml.XCardElement; 012import ezvcard.parameter.VCardParameters; 013import ezvcard.property.VCardProperty; 014 015/* 016 Copyright (c) 2012-2023, Michael Angstadt 017 All rights reserved. 018 019 Redistribution and use in source and binary forms, with or without 020 modification, are permitted provided that the following conditions are met: 021 022 1. Redistributions of source code must retain the above copyright notice, this 023 list of conditions and the following disclaimer. 024 2. Redistributions in binary form must reproduce the above copyright notice, 025 this list of conditions and the following disclaimer in the documentation 026 and/or other materials provided with the distribution. 027 028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 029 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 030 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 031 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 032 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 033 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 034 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 035 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 036 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 037 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 038 */ 039 040/** 041 * Marshals properties that have just a single value thats need no parsing or 042 * writing logic and that always has the same data type. 043 * @param <T> the property class 044 * @author Michael Angstadt 045 */ 046public abstract class SimplePropertyScribe<T extends VCardProperty> extends VCardPropertyScribe<T> { 047 protected final VCardDataType dataType; 048 049 public SimplePropertyScribe(Class<T> clazz, String propertyName, VCardDataType dataType) { 050 super(clazz, propertyName); 051 this.dataType = dataType; 052 } 053 054 @Override 055 protected VCardDataType _defaultDataType(VCardVersion version) { 056 return dataType; 057 } 058 059 @Override 060 protected String _writeText(T property, WriteContext context) { 061 String value = _writeValue(property); 062 return (value == null) ? "" : escape(value, context); 063 } 064 065 @Override 066 protected T _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { 067 value = VObjectPropertyValues.unescape(value); 068 return _parseValue(value); 069 } 070 071 @Override 072 protected void _writeXml(T property, XCardElement parent) { 073 parent.append(dataType, _writeValue(property)); 074 } 075 076 @Override 077 protected T _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) { 078 String value = element.first(dataType); 079 if (value != null) { 080 return _parseValue(value); 081 } 082 083 throw super.missingXmlElements(dataType); 084 } 085 086 @Override 087 protected T _parseHtml(HCardElement element, ParseContext context) { 088 String value = element.value(); 089 return _parseValue(value); 090 } 091 092 @Override 093 protected JCardValue _writeJson(T property) { 094 String value = _writeValue(property); 095 if (value == null) { 096 value = ""; 097 } 098 099 return JCardValue.single(value); 100 } 101 102 @Override 103 protected T _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { 104 String valueStr = value.asSingle(); 105 return _parseValue(valueStr); 106 } 107 108 /** 109 * Writes the property value to a string. 110 * @param property the property to write 111 * @return the property value 112 */ 113 protected abstract String _writeValue(T property); 114 115 /** 116 * Parses the property from a string. 117 * @param value the property value 118 * @return the parsed property object 119 */ 120 protected abstract T _parseValue(String value); 121}