Debian Codegraph @

src/main/java/ezvcard/io/scribe/GeoScribe.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.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 Copyright (c) 2012-2023, Michael Angstadt
20 All rights reserved.
21
22 Redistribution and use in source and binary forms, with or without
23 modification, are permitted provided that the following conditions are met:
24
25 1. Redistributions of source code must retain the above copyright notice, this
26 list of conditions and the following disclaimer.
27 2. Redistributions in binary form must reproduce the above copyright notice,
28 this list of conditions and the following disclaimer in the documentation
29 and/or other materials provided with the distribution.
30
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
32 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
35 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42
43 /**
44 * Marshals {@link Geo} properties.
45 * @author Michael Angstadt
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 }