| 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 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
} |