| 1 |
package ezvcard.util; |
| 2 |
|
| 3 |
import java.net.URI; |
| 4 |
import java.util.Collections; |
| 5 |
import java.util.LinkedHashMap; |
| 6 |
import java.util.Map; |
| 7 |
import java.util.regex.Matcher; |
| 8 |
import java.util.regex.Pattern; |
| 9 |
|
| 10 |
import ezvcard.Messages; |
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public final class GeoUri { |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static final String CRS_WGS84 = "wgs84"; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private static final boolean validParameterValueCharacters[] = new boolean[128]; |
| 75 |
static { |
| 76 |
for (int i = '0'; i <= '9'; i++) { |
| 77 |
validParameterValueCharacters[i] = true; |
| 78 |
} |
| 79 |
for (int i = 'A'; i <= 'Z'; i++) { |
| 80 |
validParameterValueCharacters[i] = true; |
| 81 |
} |
| 82 |
for (int i = 'a'; i <= 'z'; i++) { |
| 83 |
validParameterValueCharacters[i] = true; |
| 84 |
} |
| 85 |
String s = "!$&'()*+-.:[]_~"; |
| 86 |
for (int i = 0; i < s.length(); i++) { |
| 87 |
char c = s.charAt(i); |
| 88 |
validParameterValueCharacters[c] = true; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private static final Pattern hexPattern = Pattern.compile("(?i)%([0-9a-f]{2})"); |
| 96 |
|
| 97 |
private static final String PARAM_CRS = "crs"; |
| 98 |
private static final String PARAM_UNCERTAINTY = "u"; |
| 99 |
|
| 100 |
private final Double coordA; |
| 101 |
private final Double coordB; |
| 102 |
private final Double coordC; |
| 103 |
private final String crs; |
| 104 |
private final Double uncertainty; |
| 105 |
private final Map<String, String> parameters; |
| 106 |
|
| 107 |
private GeoUri(Builder builder) { |
| 108 |
this.coordA = (builder.coordA == null) ? Double.valueOf(0.0) : builder.coordA; |
| 109 |
this.coordB = (builder.coordB == null) ? Double.valueOf(0.0) : builder.coordB; |
| 110 |
this.coordC = builder.coordC; |
| 111 |
this.crs = builder.crs; |
| 112 |
this.uncertainty = builder.uncertainty; |
| 113 |
this.parameters = Collections.unmodifiableMap(builder.parameters); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static GeoUri parse(String uri) { |
| 123 |
|
| 124 |
|
| 125 |
String scheme = "geo:"; |
| 126 |
if (uri.length() < scheme.length() || !uri.substring(0, scheme.length()).equalsIgnoreCase(scheme)) { |
| 127 |
|
| 128 |
throw Messages.INSTANCE.getIllegalArgumentException(18, scheme); |
| 129 |
} |
| 130 |
|
| 131 |
Builder builder = new Builder(null, null); |
| 132 |
ClearableStringBuilder buffer = new ClearableStringBuilder(); |
| 133 |
String paramName = null; |
| 134 |
boolean coordinatesDone = false; |
| 135 |
for (int i = scheme.length(); i < uri.length(); i++) { |
| 136 |
char c = uri.charAt(i); |
| 137 |
|
| 138 |
if (c == ',' && !coordinatesDone) { |
| 139 |
handleEndOfCoordinate(buffer, builder); |
| 140 |
continue; |
| 141 |
} |
| 142 |
|
| 143 |
if (c == ';') { |
| 144 |
if (coordinatesDone) { |
| 145 |
handleEndOfParameter(buffer, paramName, builder); |
| 146 |
paramName = null; |
| 147 |
} else { |
| 148 |
handleEndOfCoordinate(buffer, builder); |
| 149 |
if (builder.coordB == null) { |
| 150 |
throw Messages.INSTANCE.getIllegalArgumentException(21); |
| 151 |
} |
| 152 |
coordinatesDone = true; |
| 153 |
} |
| 154 |
continue; |
| 155 |
} |
| 156 |
|
| 157 |
if (c == '=' && coordinatesDone && paramName == null) { |
| 158 |
paramName = buffer.getAndClear(); |
| 159 |
continue; |
| 160 |
} |
| 161 |
|
| 162 |
buffer.append(c); |
| 163 |
} |
| 164 |
|
| 165 |
if (coordinatesDone) { |
| 166 |
handleEndOfParameter(buffer, paramName, builder); |
| 167 |
} else { |
| 168 |
handleEndOfCoordinate(buffer, builder); |
| 169 |
if (builder.coordB == null) { |
| 170 |
throw Messages.INSTANCE.getIllegalArgumentException(21); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return builder.build(); |
| 175 |
} |
| 176 |
|
| 177 |
private static void handleEndOfCoordinate(ClearableStringBuilder buffer, Builder builder) { |
| 178 |
String s = buffer.getAndClear(); |
| 179 |
|
| 180 |
if (builder.coordA == null) { |
| 181 |
try { |
| 182 |
builder.coordA = Double.parseDouble(s); |
| 183 |
} catch (NumberFormatException e) { |
| 184 |
throw new IllegalArgumentException(Messages.INSTANCE.getExceptionMessage(22, "A"), e); |
| 185 |
} |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
if (builder.coordB == null) { |
| 190 |
try { |
| 191 |
builder.coordB = Double.parseDouble(s); |
| 192 |
} catch (NumberFormatException e) { |
| 193 |
throw new IllegalArgumentException(Messages.INSTANCE.getExceptionMessage(22, "B"), e); |
| 194 |
} |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
if (builder.coordC == null) { |
| 199 |
try { |
| 200 |
builder.coordC = Double.parseDouble(s); |
| 201 |
} catch (NumberFormatException e) { |
| 202 |
throw new IllegalArgumentException(Messages.INSTANCE.getExceptionMessage(22, "C"), e); |
| 203 |
} |
| 204 |
return; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
private static void addParameter(String name, String value, Builder builder) { |
| 209 |
value = decodeParameterValue(value); |
| 210 |
|
| 211 |
if (PARAM_CRS.equalsIgnoreCase(name)) { |
| 212 |
builder.crs = value; |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
if (PARAM_UNCERTAINTY.equalsIgnoreCase(name)) { |
| 217 |
try { |
| 218 |
builder.uncertainty = Double.valueOf(value); |
| 219 |
return; |
| 220 |
} catch (NumberFormatException e) { |
| 221 |
|
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
builder.parameters.put(name, value); |
| 226 |
} |
| 227 |
|
| 228 |
private static void handleEndOfParameter(ClearableStringBuilder buffer, String paramName, Builder builder) { |
| 229 |
String s = buffer.getAndClear(); |
| 230 |
|
| 231 |
if (paramName == null) { |
| 232 |
if (s.length() > 0) { |
| 233 |
addParameter(s, "", builder); |
| 234 |
} |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
addParameter(paramName, s, builder); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public Double getCoordA() { |
| 246 |
return coordA; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public Double getCoordB() { |
| 254 |
return coordB; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public Double getCoordC() { |
| 262 |
return coordC; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public String getCrs() { |
| 271 |
return crs; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
public Double getUncertainty() { |
| 279 |
return uncertainty; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
public String getParameter(String name) { |
| 288 |
return parameters.get(name); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public Map<String, String> getParameters() { |
| 296 |
return parameters; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public URI toUri() { |
| 304 |
return URI.create(toString()); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
@Override |
| 312 |
public String toString() { |
| 313 |
return toString(6); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public String toString(int decimals) { |
| 323 |
VCardFloatFormatter formatter = new VCardFloatFormatter(decimals); |
| 324 |
StringBuilder sb = new StringBuilder("geo:"); |
| 325 |
|
| 326 |
sb.append(formatter.format(coordA)); |
| 327 |
sb.append(','); |
| 328 |
sb.append(formatter.format(coordB)); |
| 329 |
|
| 330 |
if (coordC != null) { |
| 331 |
sb.append(','); |
| 332 |
sb.append(coordC); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
if (crs != null && !crs.equalsIgnoreCase(CRS_WGS84)) { |
| 337 |
writeParameter(PARAM_CRS, crs, sb); |
| 338 |
} |
| 339 |
|
| 340 |
if (uncertainty != null) { |
| 341 |
writeParameter(PARAM_UNCERTAINTY, formatter.format(uncertainty), sb); |
| 342 |
} |
| 343 |
|
| 344 |
for (Map.Entry<String, String> entry : parameters.entrySet()) { |
| 345 |
String name = entry.getKey(); |
| 346 |
String value = entry.getValue(); |
| 347 |
writeParameter(name, value, sb); |
| 348 |
} |
| 349 |
|
| 350 |
return sb.toString(); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
private void writeParameter(String name, String value, StringBuilder sb) { |
| 360 |
sb.append(';').append(name).append('=').append(encodeParameterValue(value)); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
private static String encodeParameterValue(String value) { |
| 369 |
StringBuilder sb = null; |
| 370 |
for (int i = 0; i < value.length(); i++) { |
| 371 |
char c = value.charAt(i); |
| 372 |
if (c < validParameterValueCharacters.length && validParameterValueCharacters[c]) { |
| 373 |
if (sb != null) { |
| 374 |
sb.append(c); |
| 375 |
} |
| 376 |
} else { |
| 377 |
if (sb == null) { |
| 378 |
sb = new StringBuilder(value.length() * 2); |
| 379 |
sb.append(value, 0, i); |
| 380 |
} |
| 381 |
String hex = Integer.toString(c, 16); |
| 382 |
sb.append('%').append(hex); |
| 383 |
} |
| 384 |
} |
| 385 |
return (sb == null) ? value : sb.toString(); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
private static String decodeParameterValue(String value) { |
| 394 |
Matcher m = hexPattern.matcher(value); |
| 395 |
StringBuffer sb = null; |
| 396 |
|
| 397 |
while (m.find()) { |
| 398 |
if (sb == null) { |
| 399 |
sb = new StringBuffer(value.length()); |
| 400 |
} |
| 401 |
|
| 402 |
int hex = Integer.parseInt(m.group(1), 16); |
| 403 |
m.appendReplacement(sb, Character.toString((char) hex)); |
| 404 |
} |
| 405 |
|
| 406 |
if (sb == null) { |
| 407 |
return value; |
| 408 |
} |
| 409 |
|
| 410 |
m.appendTail(sb); |
| 411 |
return sb.toString(); |
| 412 |
} |
| 413 |
|
| 414 |
@Override |
| 415 |
public int hashCode() { |
| 416 |
final int prime = 31; |
| 417 |
int result = 1; |
| 418 |
result = prime * result + ((coordA == null) ? 0 : coordA.hashCode()); |
| 419 |
result = prime * result + ((coordB == null) ? 0 : coordB.hashCode()); |
| 420 |
result = prime * result + ((coordC == null) ? 0 : coordC.hashCode()); |
| 421 |
result = prime * result + ((crs == null) ? 0 : crs.toLowerCase().hashCode()); |
| 422 |
result = prime * result + ((parameters == null) ? 0 : StringUtils.toLowerCase(parameters).hashCode()); |
| 423 |
result = prime * result + ((uncertainty == null) ? 0 : uncertainty.hashCode()); |
| 424 |
return result; |
| 425 |
} |
| 426 |
|
| 427 |
@Override |
| 428 |
public boolean equals(Object obj) { |
| 429 |
if (this == obj) return true; |
| 430 |
if (obj == null) return false; |
| 431 |
if (getClass() != obj.getClass()) return false; |
| 432 |
GeoUri other = (GeoUri) obj; |
| 433 |
if (coordA == null) { |
| 434 |
if (other.coordA != null) return false; |
| 435 |
} else if (!coordA.equals(other.coordA)) return false; |
| 436 |
if (coordB == null) { |
| 437 |
if (other.coordB != null) return false; |
| 438 |
} else if (!coordB.equals(other.coordB)) return false; |
| 439 |
if (coordC == null) { |
| 440 |
if (other.coordC != null) return false; |
| 441 |
} else if (!coordC.equals(other.coordC)) return false; |
| 442 |
if (crs == null) { |
| 443 |
if (other.crs != null) return false; |
| 444 |
} else if (!crs.equalsIgnoreCase(other.crs)) return false; |
| 445 |
if (uncertainty == null) { |
| 446 |
if (other.uncertainty != null) return false; |
| 447 |
} else if (!uncertainty.equals(other.uncertainty)) return false; |
| 448 |
if (parameters == null) { |
| 449 |
if (other.parameters != null) return false; |
| 450 |
} else { |
| 451 |
if (other.parameters == null) return false; |
| 452 |
if (parameters.size() != other.parameters.size()) return false; |
| 453 |
|
| 454 |
Map<String, String> parametersLower = StringUtils.toLowerCase(parameters); |
| 455 |
Map<String, String> otherParametersLower = StringUtils.toLowerCase(other.parameters); |
| 456 |
if (!parametersLower.equals(otherParametersLower)) return false; |
| 457 |
} |
| 458 |
return true; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public static class Builder { |
| 466 |
private Double coordA; |
| 467 |
private Double coordB; |
| 468 |
private Double coordC; |
| 469 |
private String crs; |
| 470 |
private Double uncertainty; |
| 471 |
private Map<String, String> parameters; |
| 472 |
private CharacterBitSet validParamChars = new CharacterBitSet("a-zA-Z0-9-"); |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
public Builder(Double coordA, Double coordB) { |
| 480 |
parameters = new LinkedHashMap<>(0); |
| 481 |
coordA(coordA); |
| 482 |
coordB(coordB); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
public Builder(GeoUri original) { |
| 490 |
coordA(original.coordA); |
| 491 |
coordB(original.coordB); |
| 492 |
this.coordC = original.coordC; |
| 493 |
this.crs = original.crs; |
| 494 |
this.uncertainty = original.uncertainty; |
| 495 |
this.parameters = new LinkedHashMap<>(original.parameters); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
public Builder coordA(Double coordA) { |
| 504 |
this.coordA = coordA; |
| 505 |
return this; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
public Builder coordB(Double coordB) { |
| 514 |
this.coordB = coordB; |
| 515 |
return this; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
public Builder coordC(Double coordC) { |
| 524 |
this.coordC = coordC; |
| 525 |
return this; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
public Builder crs(String crs) { |
| 537 |
if (crs != null && !validParamChars.containsOnly(crs)) { |
| 538 |
throw Messages.INSTANCE.getIllegalArgumentException(24); |
| 539 |
} |
| 540 |
this.crs = crs; |
| 541 |
return this; |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
public Builder uncertainty(Double uncertainty) { |
| 550 |
this.uncertainty = uncertainty; |
| 551 |
return this; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
public Builder parameter(String name, String value) { |
| 564 |
if (!validParamChars.containsOnly(name)) { |
| 565 |
throw Messages.INSTANCE.getIllegalArgumentException(23); |
| 566 |
} |
| 567 |
|
| 568 |
if (value == null) { |
| 569 |
parameters.remove(name); |
| 570 |
} else { |
| 571 |
parameters.put(name, value); |
| 572 |
} |
| 573 |
return this; |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
public GeoUri build() { |
| 581 |
return new GeoUri(this); |
| 582 |
} |
| 583 |
} |
| 584 |
} |