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