| 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 |
|
| 17 |
|
| 18 |
|
| 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 |
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 |
} |