Debian Codegraph @

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

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import java.time.temporal.Temporal;
4
5 import com.github.mangstadt.vinnie.io.VObjectPropertyValues;
6
7 import ezvcard.VCardDataType;
8 import ezvcard.VCardVersion;
9 import ezvcard.io.CannotParseException;
10 import ezvcard.io.ParseContext;
11 import ezvcard.io.html.HCardElement;
12 import ezvcard.io.json.JCardValue;
13 import ezvcard.io.text.WriteContext;
14 import ezvcard.io.xml.XCardElement;
15 import ezvcard.parameter.VCardParameters;
16 import ezvcard.property.DateOrTimeProperty;
17 import ezvcard.util.PartialDate;
18 import ezvcard.util.VCardDateFormat;
19
20 /*
21 Copyright (c) 2012-2023, Michael Angstadt
22 All rights reserved.
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions are met:
26
27 1. Redistributions of source code must retain the above copyright notice, this
28 list of conditions and the following disclaimer.
29 2. Redistributions in binary form must reproduce the above copyright notice,
30 this list of conditions and the following disclaimer in the documentation
31 and/or other materials provided with the distribution.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 */
44
45 /**
46 * Marshals properties with date-time values.
47 * @author Michael Angstadt
48 * @param <T> the property class
49 */
50 public abstract class DateOrTimePropertyScribe<T extends DateOrTimeProperty> extends VCardPropertyScribe<T> {
51 public DateOrTimePropertyScribe(Class<T> clazz, String propertyName) {
52 super(clazz, propertyName);
53 }
54
55 @Override
56 protected VCardDataType _defaultDataType(VCardVersion version) {
57 switch (version) {
58 case V2_1:
59 case V3_0:
60 return null;
61 case V4_0:
62 return VCardDataType.DATE_AND_OR_TIME;
63 }
64 return null;
65 }
66
67 @Override
68 protected VCardDataType _dataType(T property, VCardVersion version) {
69 switch (version) {
70 case V2_1:
71 case V3_0:
72 return null;
73 case V4_0:
74 if (property.getText() != null) {
75 return VCardDataType.TEXT;
76 }
77 if (property.getDate() != null) {
78 return VCardDateFormat.hasTime(property.getDate()) ? VCardDataType.DATE_TIME : VCardDataType.DATE;
79 }
80 if (property.getPartialDate() != null) {
81 return property.getPartialDate().hasTimeComponent() ? VCardDataType.DATE_TIME : VCardDataType.DATE;
82 }
83 return VCardDataType.DATE_AND_OR_TIME;
84 }
85 return null;
86 }
87
88 @Override
89 protected String _writeText(T property, WriteContext context) {
90 VCardVersion version = context.getVersion();
91 Temporal date = property.getDate();
92 if (date != null) {
93 boolean extended = (version == VCardVersion.V3_0);
94 return date(date).extended(extended).write();
95 }
96
97 if (version == VCardVersion.V4_0) {
98 String text = property.getText();
99 if (text != null) {
100 return VObjectPropertyValues.escape(text);
101 }
102
103 PartialDate partialDate = property.getPartialDate();
104 if (partialDate != null) {
105 return partialDate.toISO8601(false);
106 }
107 }
108
109 return "";
110 }
111
112 @Override
113 protected T _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
114 value = VObjectPropertyValues.unescape(value);
115 if (context.getVersion() == VCardVersion.V4_0 && dataType == VCardDataType.TEXT) {
116 return newInstance(value);
117 }
118
119 return parse(value, context);
120 }
121
122 @Override
123 protected void _writeXml(T property, XCardElement parent) {
124 Temporal date = property.getDate();
125 if (date != null) {
126 String value = date(date).extended(false).write();
127 VCardDataType dataType = VCardDateFormat.hasTime(date) ? VCardDataType.DATE_TIME : VCardDataType.DATE;
128 parent.append(dataType, value);
129 return;
130 }
131
132 PartialDate partialDate = property.getPartialDate();
133 if (partialDate != null) {
134 VCardDataType dataType;
135 if (partialDate.hasTimeComponent() && partialDate.hasDateComponent()) {
136 dataType = VCardDataType.DATE_TIME;
137 } else if (partialDate.hasTimeComponent()) {
138 dataType = VCardDataType.TIME;
139 } else if (partialDate.hasDateComponent()) {
140 dataType = VCardDataType.DATE;
141 } else {
142 dataType = VCardDataType.DATE_AND_OR_TIME;
143 }
144
145 parent.append(dataType, partialDate.toISO8601(false));
146 return;
147 }
148
149 String text = property.getText();
150 if (text != null) {
151 parent.append(VCardDataType.TEXT, text);
152 return;
153 }
154
155 parent.append(VCardDataType.DATE_AND_OR_TIME, "");
156 }
157
158 @Override
159 protected T _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) {
160 String value = element.first(VCardDataType.DATE, VCardDataType.DATE_TIME, VCardDataType.DATE_AND_OR_TIME);
161 if (value != null) {
162 return parse(value, context);
163 }
164
165 value = element.first(VCardDataType.TEXT);
166 if (value != null) {
167 return newInstance(value);
168 }
169
170 throw missingXmlElements(VCardDataType.DATE, VCardDataType.DATE_TIME, VCardDataType.DATE_AND_OR_TIME, VCardDataType.TEXT);
171 }
172
173 @Override
174 protected T _parseHtml(HCardElement element, ParseContext context) {
175 String value = null;
176 if ("time".equals(element.tagName())) {
177 String datetime = element.attr("datetime");
178 if (datetime.length() > 0) {
179 value = datetime;
180 }
181 }
182 if (value == null) {
183 value = element.value();
184 }
185 return parse(value, context);
186 }
187
188 @Override
189 protected JCardValue _writeJson(T property) {
190 Temporal date = property.getDate();
191 if (date != null) {
192 String value = date(date).extended(true).write();
193 return JCardValue.single(value);
194 }
195
196 PartialDate partialDate = property.getPartialDate();
197 if (partialDate != null) {
198 String value = partialDate.toISO8601(true);
199 return JCardValue.single(value);
200 }
201
202 String text = property.getText();
203 if (text != null) {
204 return JCardValue.single(text);
205 }
206
207 return JCardValue.single("");
208 }
209
210 @Override
211 protected T _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
212 String valueStr = value.asSingle();
213 if (dataType == VCardDataType.TEXT) {
214 return newInstance(valueStr);
215 }
216
217 return parse(valueStr, context);
218 }
219
220 private T parse(String value, ParseContext context) {
221 try {
222 return newInstance(date(value));
223 } catch (IllegalArgumentException e) {
224 if (context.getVersion() == VCardVersion.V2_1 || context.getVersion() == VCardVersion.V3_0) {
225 throw new CannotParseException(5);
226 }
227
228 try {
229 return newInstance(PartialDate.parse(value));
230 } catch (IllegalArgumentException e2) {
231 context.addWarning(6);
232 return newInstance(value);
233 }
234 }
235 }
236
237 protected abstract T newInstance(String text);
238
239 protected abstract T newInstance(Temporal date);
240
241 protected abstract T newInstance(PartialDate partialDate);
242 }