| 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.CannotParseException; |
| 8 |
import ezvcard.io.ParseContext; |
| 9 |
import ezvcard.io.html.HCardElement; |
| 10 |
import ezvcard.io.json.JCardValue; |
| 11 |
import ezvcard.io.text.WriteContext; |
| 12 |
import ezvcard.io.xml.XCardElement; |
| 13 |
import ezvcard.parameter.VCardParameters; |
| 14 |
import ezvcard.property.Geo; |
| 15 |
import ezvcard.util.GeoUri; |
| 16 |
import ezvcard.util.VCardFloatFormatter; |
| 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 |
|
| 46 |
|
| 47 |
public class GeoScribe extends VCardPropertyScribe<Geo> { |
| 48 |
public GeoScribe() { |
| 49 |
super(Geo.class, "GEO"); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
protected VCardDataType _defaultDataType(VCardVersion version) { |
| 54 |
switch (version) { |
| 55 |
case V2_1: |
| 56 |
case V3_0: |
| 57 |
return null; |
| 58 |
case V4_0: |
| 59 |
return VCardDataType.URI; |
| 60 |
} |
| 61 |
return null; |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
protected String _writeText(Geo property, WriteContext context) { |
| 66 |
return write(property, context.getVersion()); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
protected Geo _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 71 |
if (value.isEmpty()) { |
| 72 |
return new Geo((GeoUri) null); |
| 73 |
} |
| 74 |
|
| 75 |
switch (context.getVersion()) { |
| 76 |
case V2_1: |
| 77 |
case V3_0: |
| 78 |
int pos = value.indexOf(';'); |
| 79 |
if (pos < 0) { |
| 80 |
throw new CannotParseException(11); |
| 81 |
} |
| 82 |
|
| 83 |
String latitudeStr = value.substring(0, pos); |
| 84 |
String longitudeStr = value.substring(pos + 1); |
| 85 |
|
| 86 |
Double latitude; |
| 87 |
try { |
| 88 |
latitude = Double.valueOf(latitudeStr); |
| 89 |
} catch (NumberFormatException e) { |
| 90 |
throw new CannotParseException(8, latitudeStr); |
| 91 |
} |
| 92 |
|
| 93 |
Double longitude; |
| 94 |
try { |
| 95 |
longitude = Double.valueOf(longitudeStr); |
| 96 |
} catch (NumberFormatException e) { |
| 97 |
throw new CannotParseException(10, longitudeStr); |
| 98 |
} |
| 99 |
|
| 100 |
return new Geo(latitude, longitude); |
| 101 |
case V4_0: |
| 102 |
value = VObjectPropertyValues.unescape(value); |
| 103 |
return parseGeoUri(value); |
| 104 |
} |
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
protected void _writeXml(Geo property, XCardElement parent) { |
| 110 |
parent.append(VCardDataType.URI, write(property, parent.version())); |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
protected Geo _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) { |
| 115 |
String value = element.first(VCardDataType.URI); |
| 116 |
if (value != null) { |
| 117 |
if (value.isEmpty()) { |
| 118 |
return new Geo((GeoUri) null); |
| 119 |
} |
| 120 |
return parseGeoUri(value); |
| 121 |
} |
| 122 |
|
| 123 |
throw missingXmlElements(VCardDataType.URI); |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
protected Geo _parseHtml(HCardElement element, ParseContext context) { |
| 128 |
String latitudeStr = element.firstValue("latitude"); |
| 129 |
if (latitudeStr == null) { |
| 130 |
throw new CannotParseException(7); |
| 131 |
} |
| 132 |
|
| 133 |
Double latitude; |
| 134 |
try { |
| 135 |
latitude = Double.parseDouble(latitudeStr); |
| 136 |
} catch (NumberFormatException e) { |
| 137 |
throw new CannotParseException(8, latitudeStr); |
| 138 |
} |
| 139 |
|
| 140 |
String longitudeStr = element.firstValue("longitude"); |
| 141 |
if (longitudeStr == null) { |
| 142 |
throw new CannotParseException(9); |
| 143 |
} |
| 144 |
|
| 145 |
Double longitude; |
| 146 |
try { |
| 147 |
longitude = Double.parseDouble(longitudeStr); |
| 148 |
} catch (NumberFormatException e) { |
| 149 |
throw new CannotParseException(10, longitudeStr); |
| 150 |
} |
| 151 |
|
| 152 |
return new Geo(latitude, longitude); |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
protected JCardValue _writeJson(Geo property) { |
| 157 |
return JCardValue.single(write(property, VCardVersion.V4_0)); |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
protected Geo _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 162 |
String valueStr = value.asSingle(); |
| 163 |
if (valueStr.isEmpty()) { |
| 164 |
return new Geo((GeoUri) null); |
| 165 |
} |
| 166 |
return parseGeoUri(valueStr); |
| 167 |
} |
| 168 |
|
| 169 |
private Geo parseGeoUri(String value) { |
| 170 |
try { |
| 171 |
return new Geo(GeoUri.parse(value)); |
| 172 |
} catch (IllegalArgumentException e) { |
| 173 |
throw new CannotParseException(12); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
private String write(Geo property, VCardVersion version) { |
| 178 |
if (property.getGeoUri() == null) { |
| 179 |
return ""; |
| 180 |
} |
| 181 |
|
| 182 |
switch (version) { |
| 183 |
case V2_1: |
| 184 |
case V3_0: |
| 185 |
VCardFloatFormatter formatter = new VCardFloatFormatter(6); |
| 186 |
String latitudeStr = formatter.format(property.getLatitude()); |
| 187 |
String longitudeStr = formatter.format(property.getLongitude()); |
| 188 |
return latitudeStr + ';' + longitudeStr; |
| 189 |
case V4_0: |
| 190 |
return property.getGeoUri().toString(6); |
| 191 |
} |
| 192 |
return null; |
| 193 |
} |
| 194 |
} |