Debian Codegraph @

src/main/java/ezvcard/io/scribe/PlacePropertyScribe.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.json.JCardValue;
9 import ezvcard.io.text.WriteContext;
10 import ezvcard.io.xml.XCardElement;
11 import ezvcard.parameter.VCardParameters;
12 import ezvcard.property.PlaceProperty;
13 import ezvcard.util.GeoUri;
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 {@link PlaceProperty} properties.
42 * @param <T> the property class
43 * @author Michael Angstadt
44 */
45 public abstract class PlacePropertyScribe<T extends PlaceProperty> extends VCardPropertyScribe<T> {
46 public PlacePropertyScribe(Class<T> clazz, String propertyName) {
47 super(clazz, propertyName);
48 }
49
50 @Override
51 protected VCardDataType _defaultDataType(VCardVersion version) {
52 return VCardDataType.TEXT;
53 }
54
55 @Override
56 protected VCardDataType _dataType(T property, VCardVersion version) {
57 if (property.getText() != null) {
58 return VCardDataType.TEXT;
59 }
60
61 if (property.getUri() != null || property.getGeoUri() != null) {
62 return VCardDataType.URI;
63 }
64
65 return _defaultDataType(version);
66 }
67
68 @Override
69 protected String _writeText(T property, WriteContext context) {
70 String text = property.getText();
71 if (text != null) {
72 return VObjectPropertyValues.escape(text);
73 }
74
75 String uri = property.getUri();
76 if (uri != null) {
77 return uri;
78 }
79
80 GeoUri geoUri = property.getGeoUri();
81 if (geoUri != null) {
82 return geoUri.toString();
83 }
84
85 return "";
86 }
87
88 @Override
89 protected T _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
90 T property = newInstance();
91 value = VObjectPropertyValues.unescape(value);
92
93 if (dataType == VCardDataType.TEXT) {
94 property.setText(value);
95 return property;
96 }
97
98 if (dataType == VCardDataType.URI) {
99 try {
100 property.setGeoUri(GeoUri.parse(value));
101 } catch (IllegalArgumentException e) {
102 property.setUri(value);
103 }
104 return property;
105 }
106
107 property.setText(value);
108 return property;
109 }
110
111 @Override
112 protected void _writeXml(T property, XCardElement parent) {
113 String text = property.getText();
114 if (text != null) {
115 parent.append(VCardDataType.TEXT, text);
116 return;
117 }
118
119 String uri = property.getUri();
120 if (uri != null) {
121 parent.append(VCardDataType.URI, uri);
122 return;
123 }
124
125 GeoUri geoUri = property.getGeoUri();
126 if (geoUri != null) {
127 parent.append(VCardDataType.URI, geoUri.toString());
128 return;
129 }
130
131 parent.append(VCardDataType.TEXT, "");
132 }
133
134 @Override
135 protected T _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) {
136 T property = newInstance();
137
138 String text = element.first(VCardDataType.TEXT);
139 if (text != null) {
140 property.setText(text);
141 return property;
142 }
143
144 String uri = element.first(VCardDataType.URI);
145 if (uri != null) {
146 try {
147 property.setGeoUri(GeoUri.parse(uri));
148 } catch (IllegalArgumentException e) {
149 property.setUri(uri);
150 }
151 return property;
152 }
153
154 throw missingXmlElements(VCardDataType.TEXT, VCardDataType.URI);
155 }
156
157 @Override
158 protected JCardValue _writeJson(T property) {
159 String text = property.getText();
160 if (text != null) {
161 return JCardValue.single(text);
162 }
163
164 String uri = property.getUri();
165 if (uri != null) {
166 return JCardValue.single(uri);
167 }
168
169 GeoUri geoUri = property.getGeoUri();
170 if (geoUri != null) {
171 return JCardValue.single(geoUri.toString());
172 }
173
174 return JCardValue.single("");
175 }
176
177 @Override
178 protected T _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
179 T property = newInstance();
180 String valueStr = value.asSingle();
181
182 if (dataType == VCardDataType.TEXT) {
183 property.setText(valueStr);
184 return property;
185 }
186
187 if (dataType == VCardDataType.URI) {
188 try {
189 property.setGeoUri(GeoUri.parse(valueStr));
190 } catch (IllegalArgumentException e) {
191 property.setUri(valueStr);
192 }
193 return property;
194 }
195
196 property.setText(valueStr);
197 return property;
198 }
199
200 protected abstract T newInstance();
201 }