Debian Codegraph @

src/main/java/ezvcard/io/scribe/SimplePropertyScribe.java

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import com.github.mangstadt.vinnie.io.VObjectPropertyValues;
4
5 import ezvcard.VCardDataType;
6 import ezvcard.VCardVersion;
7 import ezvcard.io.ParseContext;
8 import ezvcard.io.html.HCardElement;
9 import ezvcard.io.json.JCardValue;
10 import ezvcard.io.text.WriteContext;
11 import ezvcard.io.xml.XCardElement;
12 import ezvcard.parameter.VCardParameters;
13 import ezvcard.property.VCardProperty;
14
15 /*
16 Copyright (c) 2012-2023, Michael Angstadt
17 All rights reserved.
18
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions are met:
21
22 1. Redistributions of source code must retain the above copyright notice, this
23 list of conditions and the following disclaimer.
24 2. Redistributions in binary form must reproduce the above copyright notice,
25 this list of conditions and the following disclaimer in the documentation
26 and/or other materials provided with the distribution.
27
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
29 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
32 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /**
41 * Marshals properties that have just a single value thats need no parsing or
42 * writing logic and that always has the same data type.
43 * @param <T> the property class
44 * @author Michael Angstadt
45 */
46 public abstract class SimplePropertyScribe<T extends VCardProperty> extends VCardPropertyScribe<T> {
47 protected final VCardDataType dataType;
48
49 public SimplePropertyScribe(Class<T> clazz, String propertyName, VCardDataType dataType) {
50 super(clazz, propertyName);
51 this.dataType = dataType;
52 }
53
54 @Override
55 protected VCardDataType _defaultDataType(VCardVersion version) {
56 return dataType;
57 }
58
59 @Override
60 protected String _writeText(T property, WriteContext context) {
61 String value = _writeValue(property);
62 return (value == null) ? "" : escape(value, context);
63 }
64
65 @Override
66 protected T _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
67 value = VObjectPropertyValues.unescape(value);
68 return _parseValue(value);
69 }
70
71 @Override
72 protected void _writeXml(T property, XCardElement parent) {
73 parent.append(dataType, _writeValue(property));
74 }
75
76 @Override
77 protected T _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) {
78 String value = element.first(dataType);
79 if (value != null) {
80 return _parseValue(value);
81 }
82
83 throw super.missingXmlElements(dataType);
84 }
85
86 @Override
87 protected T _parseHtml(HCardElement element, ParseContext context) {
88 String value = element.value();
89 return _parseValue(value);
90 }
91
92 @Override
93 protected JCardValue _writeJson(T property) {
94 String value = _writeValue(property);
95 if (value == null) {
96 value = "";
97 }
98
99 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 }