| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.android.util; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
|
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.HashMap; |
| 23 |
import java.util.LinkedHashMap; |
| 24 |
import java.util.List; |
| 25 |
import java.util.Set; |
| 26 |
import java.util.regex.Matcher; |
| 27 |
import java.util.regex.Pattern; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public abstract class AbstractMessageParser { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public static interface Resources { |
| 46 |
|
| 47 |
|
| 48 |
public Set<String> getSchemes(); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public TrieNode getDomainSuffixes(); |
| 54 |
|
| 55 |
|
| 56 |
public TrieNode getSmileys(); |
| 57 |
|
| 58 |
|
| 59 |
public TrieNode getAcronyms(); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
protected abstract Resources getResources(); |
| 67 |
|
| 68 |
|
| 69 |
public static final String musicNote = "\u266B "; |
| 70 |
|
| 71 |
private String text; |
| 72 |
private int nextChar; |
| 73 |
private int nextClass; |
| 74 |
private ArrayList<Part> parts; |
| 75 |
private ArrayList<Token> tokens; |
| 76 |
private HashMap<Character,Format> formatStart; |
| 77 |
private boolean parseSmilies; |
| 78 |
private boolean parseAcronyms; |
| 79 |
private boolean parseFormatting; |
| 80 |
private boolean parseUrls; |
| 81 |
private boolean parseMeText; |
| 82 |
private boolean parseMusic; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public AbstractMessageParser(String text) { |
| 91 |
this(text, true, true, true, true, true, true); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public AbstractMessageParser(String text, boolean parseSmilies, |
| 101 |
boolean parseAcronyms, boolean parseFormatting, boolean parseUrls, |
| 102 |
boolean parseMusic, boolean parseMeText) { |
| 103 |
this.text = text; |
| 104 |
this.nextChar = 0; |
| 105 |
this.nextClass = 10; |
| 106 |
this.parts = new ArrayList<Part>(); |
| 107 |
this.tokens = new ArrayList<Token>(); |
| 108 |
this.formatStart = new HashMap<Character,Format>(); |
| 109 |
this.parseSmilies = parseSmilies; |
| 110 |
this.parseAcronyms = parseAcronyms; |
| 111 |
this.parseFormatting = parseFormatting; |
| 112 |
this.parseUrls = parseUrls; |
| 113 |
this.parseMusic = parseMusic; |
| 114 |
this.parseMeText = parseMeText; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
public final String getRawText() { return text; } |
| 119 |
|
| 120 |
|
| 121 |
public final int getPartCount() { return parts.size(); } |
| 122 |
|
| 123 |
|
| 124 |
public final Part getPart(int index) { return parts.get(index); } |
| 125 |
|
| 126 |
|
| 127 |
public final List<Part> getParts() { return parts; } |
| 128 |
|
| 129 |
|
| 130 |
public void parse() { |
| 131 |
|
| 132 |
|
| 133 |
if (parseMusicTrack()) { |
| 134 |
buildParts(null); |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
String meText = null; |
| 140 |
if (parseMeText && text.startsWith("/me") && (text.length() > 3) && |
| 141 |
Character.isWhitespace(text.charAt(3))) { |
| 142 |
meText = text.substring(0, 4); |
| 143 |
text = text.substring(4); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
boolean wasSmiley = false; |
| 148 |
while (nextChar < text.length()) { |
| 149 |
if (!isWordBreak(nextChar)) { |
| 150 |
if (!wasSmiley || !isSmileyBreak(nextChar)) { |
| 151 |
throw new AssertionError("last chunk did not end at word break"); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
if (parseSmiley()) { |
| 156 |
wasSmiley = true; |
| 157 |
} else { |
| 158 |
wasSmiley = false; |
| 159 |
|
| 160 |
if (!parseAcronym() && !parseURL() && !parseFormatting()) { |
| 161 |
parseText(); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
for (int i = 0; i < tokens.size(); ++i) { |
| 168 |
if (tokens.get(i).isMedia()) { |
| 169 |
if ((i > 0) && (tokens.get(i - 1) instanceof Html)) { |
| 170 |
((Html)tokens.get(i - 1)).trimLeadingWhitespace(); |
| 171 |
} |
| 172 |
if ((i + 1 < tokens.size()) && (tokens.get(i + 1) instanceof Html)) { |
| 173 |
((Html)tokens.get(i + 1)).trimTrailingWhitespace(); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
for (int i = 0; i < tokens.size(); ++i) { |
| 180 |
if (tokens.get(i).isHtml() && |
| 181 |
(tokens.get(i).toHtml(true).length() == 0)) { |
| 182 |
tokens.remove(i); |
| 183 |
--i; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
buildParts(meText); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public static Token tokenForUrl(String url, String text) { |
| 198 |
if(url == null) { |
| 199 |
return null; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
Video video = Video.matchURL(url, text); |
| 204 |
if (video != null) { |
| 205 |
return video; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
YouTubeVideo ytVideo = YouTubeVideo.matchURL(url, text); |
| 210 |
if (ytVideo != null) { |
| 211 |
return ytVideo; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
Photo photo = Photo.matchURL(url, text); |
| 216 |
if (photo != null) { |
| 217 |
return photo; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
FlickrPhoto flickrPhoto = FlickrPhoto.matchURL(url, text); |
| 222 |
if (flickrPhoto != null) { |
| 223 |
return flickrPhoto; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
return new Link(url, text); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
private void buildParts(String meText) { |
| 236 |
for (int i = 0; i < tokens.size(); ++i) { |
| 237 |
Token token = tokens.get(i); |
| 238 |
if (token.isMedia() || (parts.size() == 0) || lastPart().isMedia()) { |
| 239 |
parts.add(new Part()); |
| 240 |
} |
| 241 |
lastPart().add(token); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
if (parts.size() > 0) { |
| 246 |
parts.get(0).setMeText(meText); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
private Part lastPart() { return parts.get(parts.size() - 1); } |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
private boolean parseMusicTrack() { |
| 258 |
|
| 259 |
if (parseMusic && text.startsWith(musicNote)) { |
| 260 |
addToken(new MusicTrack(text.substring(musicNote.length()))); |
| 261 |
nextChar = text.length(); |
| 262 |
return true; |
| 263 |
} |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
private void parseText() { |
| 269 |
StringBuilder buf = new StringBuilder(); |
| 270 |
int start = nextChar; |
| 271 |
do { |
| 272 |
char ch = text.charAt(nextChar++); |
| 273 |
switch (ch) { |
| 274 |
case '<': buf.append("<"); break; |
| 275 |
case '>': buf.append(">"); break; |
| 276 |
case '&': buf.append("&"); break; |
| 277 |
case '"': buf.append("""); break; |
| 278 |
case '\'': buf.append("'"); break; |
| 279 |
case '\n': buf.append("<br>"); break; |
| 280 |
default: buf.append(ch); break; |
| 281 |
} |
| 282 |
} while (!isWordBreak(nextChar)); |
| 283 |
|
| 284 |
addToken(new Html(text.substring(start, nextChar), buf.toString())); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
private boolean parseSmiley() { |
| 292 |
if(!parseSmilies) { |
| 293 |
return false; |
| 294 |
} |
| 295 |
TrieNode match = longestMatch(getResources().getSmileys(), this, nextChar, |
| 296 |
true); |
| 297 |
if (match == null) { |
| 298 |
return false; |
| 299 |
} else { |
| 300 |
int previousCharClass = getCharClass(nextChar - 1); |
| 301 |
int nextCharClass = getCharClass(nextChar + match.getText().length()); |
| 302 |
if ((previousCharClass == 2 || previousCharClass == 3) |
| 303 |
&& (nextCharClass == 2 || nextCharClass == 3)) { |
| 304 |
return false; |
| 305 |
} |
| 306 |
addToken(new Smiley(match.getText())); |
| 307 |
nextChar += match.getText().length(); |
| 308 |
return true; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
private boolean parseAcronym() { |
| 315 |
if(!parseAcronyms) { |
| 316 |
return false; |
| 317 |
} |
| 318 |
TrieNode match = longestMatch(getResources().getAcronyms(), this, nextChar); |
| 319 |
if (match == null) { |
| 320 |
return false; |
| 321 |
} else { |
| 322 |
addToken(new Acronym(match.getText(), match.getValue())); |
| 323 |
nextChar += match.getText().length(); |
| 324 |
return true; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
private boolean isDomainChar(char c) { |
| 330 |
return c == '-' || Character.isLetter(c) || Character.isDigit(c); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
private boolean isValidDomain(String domain) { |
| 335 |
|
| 336 |
if (matches(getResources().getDomainSuffixes(), reverse(domain))) { |
| 337 |
return true; |
| 338 |
} |
| 339 |
return false; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
private boolean parseURL() { |
| 347 |
|
| 348 |
if (!parseUrls || !isURLBreak(nextChar)) { |
| 349 |
return false; |
| 350 |
} |
| 351 |
|
| 352 |
int start = nextChar; |
| 353 |
|
| 354 |
|
| 355 |
int index = start; |
| 356 |
while ((index < text.length()) && isDomainChar(text.charAt(index))) { |
| 357 |
index += 1; |
| 358 |
} |
| 359 |
|
| 360 |
String url = ""; |
| 361 |
boolean done = false; |
| 362 |
|
| 363 |
if (index == text.length()) { |
| 364 |
return false; |
| 365 |
} else if (text.charAt(index) == ':') { |
| 366 |
|
| 367 |
String scheme = text.substring(nextChar, index); |
| 368 |
if (!getResources().getSchemes().contains(scheme)) { |
| 369 |
return false; |
| 370 |
} |
| 371 |
} else if (text.charAt(index) == '.') { |
| 372 |
|
| 373 |
while (index < text.length()) { |
| 374 |
char ch = text.charAt(index); |
| 375 |
if ((ch != '.') && !isDomainChar(ch)) { |
| 376 |
break; |
| 377 |
} else { |
| 378 |
index += 1; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
String domain = text.substring(nextChar, index); |
| 385 |
if (!isValidDomain(domain)) { |
| 386 |
return false; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
if ((index + 1 < text.length()) && (text.charAt(index) == ':')) { |
| 392 |
char ch = text.charAt(index + 1); |
| 393 |
if (Character.isDigit(ch)) { |
| 394 |
index += 1; |
| 395 |
while ((index < text.length()) && |
| 396 |
Character.isDigit(text.charAt(index))) { |
| 397 |
index += 1; |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
if (index == text.length()) { |
| 409 |
done = true; |
| 410 |
} else { |
| 411 |
char ch = text.charAt(index); |
| 412 |
if (ch == '?') { |
| 413 |
|
| 414 |
|
| 415 |
if (index + 1 == text.length()) { |
| 416 |
done = true; |
| 417 |
} else { |
| 418 |
char ch2 = text.charAt(index + 1); |
| 419 |
if (Character.isWhitespace(ch2) || isPunctuation(ch2)) { |
| 420 |
done = true; |
| 421 |
} |
| 422 |
} |
| 423 |
} else if (isPunctuation(ch)) { |
| 424 |
done = true; |
| 425 |
} else if (Character.isWhitespace(ch)) { |
| 426 |
done = true; |
| 427 |
} else if ((ch == '/') || (ch == '#')) { |
| 428 |
|
| 429 |
|
| 430 |
} else { |
| 431 |
return false; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
url = "http://"; |
| 439 |
} else { |
| 440 |
return false; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
if (!done) { |
| 446 |
while ((index < text.length()) && |
| 447 |
!Character.isWhitespace(text.charAt(index))) { |
| 448 |
index += 1; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
String urlText = text.substring(start, index); |
| 453 |
url += urlText; |
| 454 |
|
| 455 |
|
| 456 |
addURLToken(url, urlText); |
| 457 |
|
| 458 |
nextChar = index; |
| 459 |
return true; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
private void addURLToken(String url, String text) { |
| 467 |
addToken(tokenForUrl(url, text)); |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
private boolean parseFormatting() { |
| 487 |
if(!parseFormatting) { |
| 488 |
return false; |
| 489 |
} |
| 490 |
int endChar = nextChar; |
| 491 |
while ((endChar < text.length()) && isFormatChar(text.charAt(endChar))) { |
| 492 |
endChar += 1; |
| 493 |
} |
| 494 |
|
| 495 |
if ((endChar == nextChar) || !isWordBreak(endChar)) { |
| 496 |
return false; |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
LinkedHashMap<Character, Boolean> seenCharacters = |
| 503 |
new LinkedHashMap<Character, Boolean>(); |
| 504 |
|
| 505 |
for (int index = nextChar; index < endChar; ++index) { |
| 506 |
char ch = text.charAt(index); |
| 507 |
Character key = Character.valueOf(ch); |
| 508 |
if (seenCharacters.containsKey(key)) { |
| 509 |
|
| 510 |
|
| 511 |
addToken(new Format(ch, false)); |
| 512 |
} else { |
| 513 |
Format start = formatStart.get(key); |
| 514 |
if (start != null) { |
| 515 |
|
| 516 |
start.setMatched(true); |
| 517 |
formatStart.remove(key); |
| 518 |
seenCharacters.put(key, Boolean.TRUE); |
| 519 |
} else { |
| 520 |
|
| 521 |
start = new Format(ch, true); |
| 522 |
formatStart.put(key, start); |
| 523 |
addToken(start); |
| 524 |
seenCharacters.put(key, Boolean.FALSE); |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
for (Character key : seenCharacters.keySet()) { |
| 531 |
if (seenCharacters.get(key) == Boolean.TRUE) { |
| 532 |
Format end = new Format(key.charValue(), false); |
| 533 |
end.setMatched(true); |
| 534 |
addToken(end); |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
nextChar = endChar; |
| 539 |
return true; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
private boolean isWordBreak(int index) { |
| 544 |
return getCharClass(index - 1) != getCharClass(index); |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
private boolean isSmileyBreak(int index) { |
| 549 |
if (index > 0 && index < text.length()) { |
| 550 |
if (isSmileyBreak(text.charAt(index - 1), text.charAt(index))) { |
| 551 |
return true; |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
return false; |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
private boolean isURLBreak(int index) { |
| 563 |
switch (getCharClass(index - 1)) { |
| 564 |
case 2: |
| 565 |
case 3: |
| 566 |
case 4: |
| 567 |
return false; |
| 568 |
|
| 569 |
case 0: |
| 570 |
case 1: |
| 571 |
default: |
| 572 |
return true; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
private int getCharClass(int index) { |
| 578 |
if ((index < 0) || (text.length() <= index)) { |
| 579 |
return 0; |
| 580 |
} |
| 581 |
|
| 582 |
char ch = text.charAt(index); |
| 583 |
if (Character.isWhitespace(ch)) { |
| 584 |
return 1; |
| 585 |
} else if (Character.isLetter(ch)) { |
| 586 |
return 2; |
| 587 |
} else if (Character.isDigit(ch)) { |
| 588 |
return 3; |
| 589 |
} else if (isPunctuation(ch)) { |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
return ++nextClass; |
| 594 |
} else { |
| 595 |
return 4; |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
private static boolean isSmileyBreak(char c1, char c2) { |
| 606 |
switch (c1) { |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
case '$': case '&': case '*': case '+': case '-': |
| 611 |
case '/': case '<': case '=': case '>': case '@': |
| 612 |
case '[': case '\\': case ']': case '^': case '|': |
| 613 |
case '}': case '~': |
| 614 |
switch (c2) { |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
case '#': case '$': case '%': case '*': case '/': |
| 620 |
case '<': case '=': case '>': case '@': case '[': |
| 621 |
case '\\': case '^': case '~': |
| 622 |
return true; |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
return false; |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
private static boolean isPunctuation(char ch) { |
| 631 |
switch (ch) { |
| 632 |
case '.': case ',': case '"': case ':': case ';': |
| 633 |
case '?': case '!': case '(': case ')': |
| 634 |
return true; |
| 635 |
|
| 636 |
default: |
| 637 |
return false; |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
private static boolean isFormatChar(char ch) { |
| 646 |
switch (ch) { |
| 647 |
case '*': case '_': case '^': |
| 648 |
return true; |
| 649 |
|
| 650 |
default: |
| 651 |
return false; |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
public static abstract class Token { |
| 657 |
@UnsupportedAppUsage(implicitMember = |
| 658 |
"values()[Lcom/google/android/util/AbstractMessageParser$Token$Type;") |
| 659 |
public enum Type { |
| 660 |
|
| 661 |
@UnsupportedAppUsage |
| 662 |
HTML ("html"), |
| 663 |
@UnsupportedAppUsage |
| 664 |
FORMAT ("format"), |
| 665 |
@UnsupportedAppUsage |
| 666 |
LINK ("l"), |
| 667 |
@UnsupportedAppUsage |
| 668 |
SMILEY ("e"), |
| 669 |
@UnsupportedAppUsage |
| 670 |
ACRONYM ("a"), |
| 671 |
@UnsupportedAppUsage |
| 672 |
MUSIC ("m"), |
| 673 |
@UnsupportedAppUsage |
| 674 |
GOOGLE_VIDEO ("v"), |
| 675 |
@UnsupportedAppUsage |
| 676 |
YOUTUBE_VIDEO ("yt"), |
| 677 |
@UnsupportedAppUsage |
| 678 |
PHOTO ("p"), |
| 679 |
@UnsupportedAppUsage |
| 680 |
FLICKR ("f"); |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
private String stringRep; |
| 686 |
|
| 687 |
Type(String stringRep) { |
| 688 |
this.stringRep = stringRep; |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
public String toString() { |
| 693 |
return this.stringRep; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
protected Type type; |
| 698 |
protected String text; |
| 699 |
|
| 700 |
protected Token(Type type, String text) { |
| 701 |
this.type = type; |
| 702 |
this.text = text; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
public Type getType() { return type; } |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
public List<String> getInfo() { |
| 715 |
List<String> info = new ArrayList<String>(); |
| 716 |
info.add(getType().toString()); |
| 717 |
return info; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
public String getRawText() { return text; } |
| 722 |
|
| 723 |
public boolean isMedia() { return false; } |
| 724 |
public abstract boolean isHtml(); |
| 725 |
public boolean isArray() { return !isHtml(); } |
| 726 |
|
| 727 |
public String toHtml(boolean caps) { throw new AssertionError("not html"); } |
| 728 |
|
| 729 |
|
| 730 |
public boolean controlCaps() { return false; } |
| 731 |
public boolean setCaps() { return false; } |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
public static class Html extends Token { |
| 736 |
private String html; |
| 737 |
|
| 738 |
public Html(String text, String html) { |
| 739 |
super(Type.HTML, text); |
| 740 |
this.html = html; |
| 741 |
} |
| 742 |
|
| 743 |
public boolean isHtml() { return true; } |
| 744 |
public String toHtml(boolean caps) { |
| 745 |
return caps ? html.toUpperCase() : html; |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
public List<String> getInfo() { |
| 751 |
throw new UnsupportedOperationException(); |
| 752 |
} |
| 753 |
|
| 754 |
public void trimLeadingWhitespace() { |
| 755 |
text = trimLeadingWhitespace(text); |
| 756 |
html = trimLeadingWhitespace(html); |
| 757 |
} |
| 758 |
|
| 759 |
public void trimTrailingWhitespace() { |
| 760 |
text = trimTrailingWhitespace(text); |
| 761 |
html = trimTrailingWhitespace(html); |
| 762 |
} |
| 763 |
|
| 764 |
private static String trimLeadingWhitespace(String text) { |
| 765 |
int index = 0; |
| 766 |
while ((index < text.length()) && |
| 767 |
Character.isWhitespace(text.charAt(index))) { |
| 768 |
++index; |
| 769 |
} |
| 770 |
return text.substring(index); |
| 771 |
} |
| 772 |
|
| 773 |
public static String trimTrailingWhitespace(String text) { |
| 774 |
int index = text.length(); |
| 775 |
while ((index > 0) && Character.isWhitespace(text.charAt(index - 1))) { |
| 776 |
--index; |
| 777 |
} |
| 778 |
return text.substring(0, index); |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
public static class MusicTrack extends Token { |
| 784 |
private String track; |
| 785 |
|
| 786 |
public MusicTrack(String track) { |
| 787 |
super(Type.MUSIC, track); |
| 788 |
this.track = track; |
| 789 |
} |
| 790 |
|
| 791 |
public String getTrack() { return track; } |
| 792 |
|
| 793 |
public boolean isHtml() { return false; } |
| 794 |
|
| 795 |
public List<String> getInfo() { |
| 796 |
List<String> info = super.getInfo(); |
| 797 |
info.add(getTrack()); |
| 798 |
return info; |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
public static class Link extends Token { |
| 804 |
private String url; |
| 805 |
|
| 806 |
public Link(String url, String text) { |
| 807 |
super(Type.LINK, text); |
| 808 |
this.url = url; |
| 809 |
} |
| 810 |
|
| 811 |
public String getURL() { return url; } |
| 812 |
|
| 813 |
public boolean isHtml() { return false; } |
| 814 |
|
| 815 |
public List<String> getInfo() { |
| 816 |
List<String> info = super.getInfo(); |
| 817 |
info.add(getURL()); |
| 818 |
info.add(getRawText()); |
| 819 |
return info; |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
public static class Video extends Token { |
| 825 |
|
| 826 |
private static final Pattern URL_PATTERN = Pattern.compile( |
| 827 |
"(?i)http://video\\.google\\.[a-z0-9]+(?:\\.[a-z0-9]+)?/videoplay\\?" |
| 828 |
+ ".*?\\bdocid=(-?\\d+).*"); |
| 829 |
|
| 830 |
private String docid; |
| 831 |
|
| 832 |
public Video(String docid, String text) { |
| 833 |
super(Type.GOOGLE_VIDEO, text); |
| 834 |
this.docid = docid; |
| 835 |
} |
| 836 |
|
| 837 |
public String getDocID() { return docid; } |
| 838 |
|
| 839 |
public boolean isHtml() { return false; } |
| 840 |
public boolean isMedia() { return true; } |
| 841 |
|
| 842 |
|
| 843 |
public static Video matchURL(String url, String text) { |
| 844 |
Matcher m = URL_PATTERN.matcher(url); |
| 845 |
if (m.matches()) { |
| 846 |
return new Video(m.group(1), text); |
| 847 |
} else { |
| 848 |
return null; |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
public List<String> getInfo() { |
| 853 |
List<String> info = super.getInfo(); |
| 854 |
info.add(getRssUrl(docid)); |
| 855 |
info.add(getURL(docid)); |
| 856 |
return info; |
| 857 |
} |
| 858 |
|
| 859 |
|
| 860 |
public static String getRssUrl(String docid) { |
| 861 |
return "http://video.google.com/videofeed" |
| 862 |
+ "?type=docid&output=rss&sourceid=gtalk&docid=" + docid; |
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
public static String getURL(String docid) { |
| 867 |
return getURL(docid, null); |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
public static String getURL(String docid, String extraParams) { |
| 872 |
if (extraParams == null) { |
| 873 |
extraParams = ""; |
| 874 |
} else if (extraParams.length() > 0) { |
| 875 |
extraParams += "&"; |
| 876 |
} |
| 877 |
return "http://video.google.com/videoplay?" + extraParams |
| 878 |
+ "docid=" + docid; |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
public static class YouTubeVideo extends Token { |
| 884 |
|
| 885 |
private static final Pattern URL_PATTERN = Pattern.compile( |
| 886 |
"(?i)http://(?:[a-z0-9]+\\.)?youtube\\.[a-z0-9]+(?:\\.[a-z0-9]+)?/watch\\?" |
| 887 |
+ ".*\\bv=([-_a-zA-Z0-9=]+).*"); |
| 888 |
|
| 889 |
private String docid; |
| 890 |
|
| 891 |
public YouTubeVideo(String docid, String text) { |
| 892 |
super(Type.YOUTUBE_VIDEO, text); |
| 893 |
this.docid = docid; |
| 894 |
} |
| 895 |
|
| 896 |
public String getDocID() { return docid; } |
| 897 |
|
| 898 |
public boolean isHtml() { return false; } |
| 899 |
public boolean isMedia() { return true; } |
| 900 |
|
| 901 |
|
| 902 |
public static YouTubeVideo matchURL(String url, String text) { |
| 903 |
Matcher m = URL_PATTERN.matcher(url); |
| 904 |
if (m.matches()) { |
| 905 |
return new YouTubeVideo(m.group(1), text); |
| 906 |
} else { |
| 907 |
return null; |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
public List<String> getInfo() { |
| 912 |
List<String> info = super.getInfo(); |
| 913 |
info.add(getRssUrl(docid)); |
| 914 |
info.add(getURL(docid)); |
| 915 |
return info; |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
public static String getRssUrl(String docid) { |
| 920 |
return "http://youtube.com/watch?v=" + docid; |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
public static String getURL(String docid) { |
| 925 |
return getURL(docid, null); |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
public static String getURL(String docid, String extraParams) { |
| 930 |
if (extraParams == null) { |
| 931 |
extraParams = ""; |
| 932 |
} else if (extraParams.length() > 0) { |
| 933 |
extraParams += "&"; |
| 934 |
} |
| 935 |
return "http://youtube.com/watch?" + extraParams + "v=" + docid; |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
public static String getPrefixedURL(boolean http, String prefix, |
| 944 |
String docid, String extraParams) { |
| 945 |
String protocol = ""; |
| 946 |
|
| 947 |
if (http) { |
| 948 |
protocol = "http://"; |
| 949 |
} |
| 950 |
|
| 951 |
if (prefix == null) { |
| 952 |
prefix = ""; |
| 953 |
} |
| 954 |
|
| 955 |
if (extraParams == null) { |
| 956 |
extraParams = ""; |
| 957 |
} else if (extraParams.length() > 0) { |
| 958 |
extraParams += "&"; |
| 959 |
} |
| 960 |
|
| 961 |
return protocol + prefix + "youtube.com/watch?" + extraParams + "v=" + |
| 962 |
docid; |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
public static class Photo extends Token { |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
private static final Pattern URL_PATTERN = Pattern.compile( |
| 972 |
"http://picasaweb.google.com/([^/?#&]+)/+((?!searchbrowse)[^/?#&]+)(?:/|/photo)?(?:\\?[^#]*)?(?:#(.*))?"); |
| 973 |
|
| 974 |
private String user; |
| 975 |
private String album; |
| 976 |
private String photo; |
| 977 |
|
| 978 |
public Photo(String user, String album, String photo, String text) { |
| 979 |
super(Type.PHOTO, text); |
| 980 |
this.user = user; |
| 981 |
this.album = album; |
| 982 |
this.photo = photo; |
| 983 |
} |
| 984 |
|
| 985 |
public String getUser() { return user; } |
| 986 |
public String getAlbum() { return album; } |
| 987 |
public String getPhoto() { return photo; } |
| 988 |
|
| 989 |
public boolean isHtml() { return false; } |
| 990 |
public boolean isMedia() { return true; } |
| 991 |
|
| 992 |
|
| 993 |
public static Photo matchURL(String url, String text) { |
| 994 |
Matcher m = URL_PATTERN.matcher(url); |
| 995 |
if (m.matches()) { |
| 996 |
return new Photo(m.group(1), m.group(2), m.group(3), text); |
| 997 |
} else { |
| 998 |
return null; |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
public List<String> getInfo() { |
| 1003 |
List<String> info = super.getInfo(); |
| 1004 |
info.add(getRssUrl(getUser())); |
| 1005 |
info.add(getAlbumURL(getUser(), getAlbum())); |
| 1006 |
if (getPhoto() != null) { |
| 1007 |
info.add(getPhotoURL(getUser(), getAlbum(), getPhoto())); |
| 1008 |
} else { |
| 1009 |
info.add((String)null); |
| 1010 |
} |
| 1011 |
return info; |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
public static String getRssUrl(String user) { |
| 1016 |
return "http://picasaweb.google.com/data/feed/api/user/" + user + |
| 1017 |
"?category=album&alt=rss"; |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
public static String getAlbumURL(String user, String album) { |
| 1022 |
return "http://picasaweb.google.com/" + user + "/" + album; |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
public static String getPhotoURL(String user, String album, String photo) { |
| 1027 |
return "http://picasaweb.google.com/" + user + "/" + album + "/photo#" |
| 1028 |
+ photo; |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
|
| 1033 |
public static class FlickrPhoto extends Token { |
| 1034 |
|
| 1035 |
private static final Pattern URL_PATTERN = Pattern.compile( |
| 1036 |
"http://(?:www.)?flickr.com/photos/([^/?#&]+)/?([^/?#&]+)?/?.*"); |
| 1037 |
private static final Pattern GROUPING_PATTERN = Pattern.compile( |
| 1038 |
"http://(?:www.)?flickr.com/photos/([^/?#&]+)/(tags|sets)/" + |
| 1039 |
"([^/?#&]+)/?"); |
| 1040 |
|
| 1041 |
private static final String SETS = "sets"; |
| 1042 |
private static final String TAGS = "tags"; |
| 1043 |
|
| 1044 |
private String user; |
| 1045 |
private String photo; |
| 1046 |
private String grouping; |
| 1047 |
private String groupingId; |
| 1048 |
|
| 1049 |
public FlickrPhoto(String user, String photo, String grouping, |
| 1050 |
String groupingId, String text) { |
| 1051 |
super(Type.FLICKR, text); |
| 1052 |
|
| 1053 |
|
| 1054 |
if (!TAGS.equals(user)) { |
| 1055 |
this.user = user; |
| 1056 |
|
| 1057 |
this.photo = (!"show".equals(photo) ? photo : null); |
| 1058 |
this.grouping = grouping; |
| 1059 |
this.groupingId = groupingId; |
| 1060 |
} else { |
| 1061 |
this.user = null; |
| 1062 |
this.photo = null; |
| 1063 |
this.grouping = TAGS; |
| 1064 |
this.groupingId = photo; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
public String getUser() { return user; } |
| 1069 |
public String getPhoto() { return photo; } |
| 1070 |
public String getGrouping() { return grouping; } |
| 1071 |
public String getGroupingId() { return groupingId; } |
| 1072 |
|
| 1073 |
public boolean isHtml() { return false; } |
| 1074 |
public boolean isMedia() { return true; } |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
public static FlickrPhoto matchURL(String url, String text) { |
| 1081 |
Matcher m = GROUPING_PATTERN.matcher(url); |
| 1082 |
if (m.matches()) { |
| 1083 |
return new FlickrPhoto(m.group(1), null, m.group(2), m.group(3), text); |
| 1084 |
} |
| 1085 |
|
| 1086 |
m = URL_PATTERN.matcher(url); |
| 1087 |
if (m.matches()) { |
| 1088 |
return new FlickrPhoto(m.group(1), m.group(2), null, null, text); |
| 1089 |
} else { |
| 1090 |
return null; |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
public List<String> getInfo() { |
| 1095 |
List<String> info = super.getInfo(); |
| 1096 |
info.add(getUrl()); |
| 1097 |
info.add(getUser() != null ? getUser() : ""); |
| 1098 |
info.add(getPhoto() != null ? getPhoto() : ""); |
| 1099 |
info.add(getGrouping() != null ? getGrouping() : ""); |
| 1100 |
info.add(getGroupingId() != null ? getGroupingId() : ""); |
| 1101 |
return info; |
| 1102 |
} |
| 1103 |
|
| 1104 |
public String getUrl() { |
| 1105 |
if (SETS.equals(grouping)) { |
| 1106 |
return getUserSetsURL(user, groupingId); |
| 1107 |
} else if (TAGS.equals(grouping)) { |
| 1108 |
if (user != null) { |
| 1109 |
return getUserTagsURL(user, groupingId); |
| 1110 |
} else { |
| 1111 |
return getTagsURL(groupingId); |
| 1112 |
} |
| 1113 |
} else if (photo != null) { |
| 1114 |
return getPhotoURL(user, photo); |
| 1115 |
} else { |
| 1116 |
return getUserURL(user); |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
public static String getRssUrl(String user) { |
| 1122 |
return null; |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
public static String getTagsURL(String tag) { |
| 1127 |
return "http://flickr.com/photos/tags/" + tag; |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
public static String getUserURL(String user) { |
| 1132 |
return "http://flickr.com/photos/" + user; |
| 1133 |
} |
| 1134 |
|
| 1135 |
|
| 1136 |
public static String getPhotoURL(String user, String photo) { |
| 1137 |
return "http://flickr.com/photos/" + user + "/" + photo; |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
public static String getUserTagsURL(String user, String tagId) { |
| 1142 |
return "http://flickr.com/photos/" + user + "/tags/" + tagId; |
| 1143 |
} |
| 1144 |
|
| 1145 |
|
| 1146 |
public static String getUserSetsURL(String user, String setId) { |
| 1147 |
return "http://flickr.com/photos/" + user + "/sets/" + setId; |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
|
| 1152 |
public static class Smiley extends Token { |
| 1153 |
|
| 1154 |
|
| 1155 |
public Smiley(String text) { |
| 1156 |
super(Type.SMILEY, text); |
| 1157 |
} |
| 1158 |
|
| 1159 |
public boolean isHtml() { return false; } |
| 1160 |
|
| 1161 |
public List<String> getInfo() { |
| 1162 |
List<String> info = super.getInfo(); |
| 1163 |
info.add(getRawText()); |
| 1164 |
return info; |
| 1165 |
} |
| 1166 |
} |
| 1167 |
|
| 1168 |
|
| 1169 |
public static class Acronym extends Token { |
| 1170 |
private String value; |
| 1171 |
|
| 1172 |
|
| 1173 |
public Acronym(String text, String value) { |
| 1174 |
super(Type.ACRONYM, text); |
| 1175 |
this.value = value; |
| 1176 |
} |
| 1177 |
|
| 1178 |
public String getValue() { return value; } |
| 1179 |
|
| 1180 |
public boolean isHtml() { return false; } |
| 1181 |
|
| 1182 |
public List<String> getInfo() { |
| 1183 |
List<String> info = super.getInfo(); |
| 1184 |
info.add(getRawText()); |
| 1185 |
info.add(getValue()); |
| 1186 |
return info; |
| 1187 |
} |
| 1188 |
} |
| 1189 |
|
| 1190 |
|
| 1191 |
public static class Format extends Token { |
| 1192 |
private char ch; |
| 1193 |
private boolean start; |
| 1194 |
private boolean matched; |
| 1195 |
|
| 1196 |
public Format(char ch, boolean start) { |
| 1197 |
super(Type.FORMAT, String.valueOf(ch)); |
| 1198 |
this.ch = ch; |
| 1199 |
this.start = start; |
| 1200 |
} |
| 1201 |
|
| 1202 |
public void setMatched(boolean matched) { this.matched = matched; } |
| 1203 |
|
| 1204 |
public boolean isHtml() { return true; } |
| 1205 |
|
| 1206 |
public String toHtml(boolean caps) { |
| 1207 |
|
| 1208 |
|
| 1209 |
if (matched) { |
| 1210 |
return start ? getFormatStart(ch) : getFormatEnd(ch); |
| 1211 |
} else { |
| 1212 |
|
| 1213 |
return (ch == '"') ? """ : String.valueOf(ch); |
| 1214 |
} |
| 1215 |
} |
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
public List<String> getInfo() { |
| 1221 |
throw new UnsupportedOperationException(); |
| 1222 |
} |
| 1223 |
|
| 1224 |
public boolean controlCaps() { return (ch == '^'); } |
| 1225 |
public boolean setCaps() { return start; } |
| 1226 |
|
| 1227 |
private String getFormatStart(char ch) { |
| 1228 |
switch (ch) { |
| 1229 |
case '*': return "<b>"; |
| 1230 |
case '_': return "<i>"; |
| 1231 |
case '^': return "<b><font color=\"#005FFF\">"; |
| 1232 |
case '"': return "<font color=\"#999999\">\u201c"; |
| 1233 |
default: throw new AssertionError("unknown format '" + ch + "'"); |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|
| 1237 |
private String getFormatEnd(char ch) { |
| 1238 |
switch (ch) { |
| 1239 |
case '*': return "</b>"; |
| 1240 |
case '_': return "</i>"; |
| 1241 |
case '^': return "</font></b>"; |
| 1242 |
case '"': return "\u201d</font>"; |
| 1243 |
default: throw new AssertionError("unknown format '" + ch + "'"); |
| 1244 |
} |
| 1245 |
} |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
private void addToken(Token token) { |
| 1250 |
tokens.add(token); |
| 1251 |
} |
| 1252 |
|
| 1253 |
|
| 1254 |
public String toHtml() { |
| 1255 |
StringBuilder html = new StringBuilder(); |
| 1256 |
|
| 1257 |
for (Part part : parts) { |
| 1258 |
boolean caps = false; |
| 1259 |
|
| 1260 |
html.append("<p>"); |
| 1261 |
for (Token token : part.getTokens()) { |
| 1262 |
if (token.isHtml()) { |
| 1263 |
html.append(token.toHtml(caps)); |
| 1264 |
} else { |
| 1265 |
switch (token.getType()) { |
| 1266 |
case LINK: |
| 1267 |
html.append("<a href=\""); |
| 1268 |
html.append(((Link)token).getURL()); |
| 1269 |
html.append("\">"); |
| 1270 |
html.append(token.getRawText()); |
| 1271 |
html.append("</a>"); |
| 1272 |
break; |
| 1273 |
|
| 1274 |
case SMILEY: |
| 1275 |
|
| 1276 |
html.append(token.getRawText()); |
| 1277 |
break; |
| 1278 |
|
| 1279 |
case ACRONYM: |
| 1280 |
html.append(token.getRawText()); |
| 1281 |
break; |
| 1282 |
|
| 1283 |
case MUSIC: |
| 1284 |
|
| 1285 |
html.append(((MusicTrack)token).getTrack()); |
| 1286 |
break; |
| 1287 |
|
| 1288 |
case GOOGLE_VIDEO: |
| 1289 |
|
| 1290 |
html.append("<a href=\""); |
| 1291 |
html.append(((Video)token).getURL(((Video)token).getDocID())); |
| 1292 |
html.append("\">"); |
| 1293 |
html.append(token.getRawText()); |
| 1294 |
html.append("</a>"); |
| 1295 |
break; |
| 1296 |
|
| 1297 |
case YOUTUBE_VIDEO: |
| 1298 |
|
| 1299 |
html.append("<a href=\""); |
| 1300 |
html.append(((YouTubeVideo)token).getURL( |
| 1301 |
((YouTubeVideo)token).getDocID())); |
| 1302 |
html.append("\">"); |
| 1303 |
html.append(token.getRawText()); |
| 1304 |
html.append("</a>"); |
| 1305 |
break; |
| 1306 |
|
| 1307 |
case PHOTO: { |
| 1308 |
|
| 1309 |
html.append("<a href=\""); |
| 1310 |
html.append(Photo.getAlbumURL( |
| 1311 |
((Photo)token).getUser(), ((Photo)token).getAlbum())); |
| 1312 |
html.append("\">"); |
| 1313 |
html.append(token.getRawText()); |
| 1314 |
html.append("</a>"); |
| 1315 |
break; |
| 1316 |
} |
| 1317 |
|
| 1318 |
case FLICKR: |
| 1319 |
|
| 1320 |
Photo p = (Photo) token; |
| 1321 |
html.append("<a href=\""); |
| 1322 |
html.append(((FlickrPhoto)token).getUrl()); |
| 1323 |
html.append("\">"); |
| 1324 |
html.append(token.getRawText()); |
| 1325 |
html.append("</a>"); |
| 1326 |
break; |
| 1327 |
|
| 1328 |
default: |
| 1329 |
throw new AssertionError("unknown token type: " + token.getType()); |
| 1330 |
} |
| 1331 |
} |
| 1332 |
|
| 1333 |
if (token.controlCaps()) { |
| 1334 |
caps = token.setCaps(); |
| 1335 |
} |
| 1336 |
} |
| 1337 |
html.append("</p>\n"); |
| 1338 |
} |
| 1339 |
|
| 1340 |
return html.toString(); |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
protected static String reverse(String str) { |
| 1345 |
StringBuilder buf = new StringBuilder(); |
| 1346 |
for (int i = str.length() - 1; i >= 0; --i) { |
| 1347 |
buf.append(str.charAt(i)); |
| 1348 |
} |
| 1349 |
return buf.toString(); |
| 1350 |
} |
| 1351 |
|
| 1352 |
public static class TrieNode { |
| 1353 |
private final HashMap<Character,TrieNode> children = |
| 1354 |
new HashMap<Character,TrieNode>(); |
| 1355 |
private String text; |
| 1356 |
private String value; |
| 1357 |
|
| 1358 |
public TrieNode() { this(""); } |
| 1359 |
public TrieNode(String text) { |
| 1360 |
this.text = text; |
| 1361 |
} |
| 1362 |
|
| 1363 |
public final boolean exists() { return value != null; } |
| 1364 |
public final String getText() { return text; } |
| 1365 |
public final String getValue() { return value; } |
| 1366 |
public void setValue(String value) { this.value = value; } |
| 1367 |
|
| 1368 |
public TrieNode getChild(char ch) { |
| 1369 |
return children.get(Character.valueOf(ch)); |
| 1370 |
} |
| 1371 |
|
| 1372 |
public TrieNode getOrCreateChild(char ch) { |
| 1373 |
Character key = Character.valueOf(ch); |
| 1374 |
TrieNode node = children.get(key); |
| 1375 |
if (node == null) { |
| 1376 |
node = new TrieNode(text + String.valueOf(ch)); |
| 1377 |
children.put(key, node); |
| 1378 |
} |
| 1379 |
return node; |
| 1380 |
} |
| 1381 |
|
| 1382 |
|
| 1383 |
public static void addToTrie(TrieNode root, String str, String value) { |
| 1384 |
int index = 0; |
| 1385 |
while (index < str.length()) { |
| 1386 |
root = root.getOrCreateChild(str.charAt(index++)); |
| 1387 |
} |
| 1388 |
root.setValue(value); |
| 1389 |
} |
| 1390 |
} |
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
private static boolean matches(TrieNode root, String str) { |
| 1396 |
int index = 0; |
| 1397 |
while (index < str.length()) { |
| 1398 |
root = root.getChild(str.charAt(index++)); |
| 1399 |
if (root == null) { |
| 1400 |
break; |
| 1401 |
} else if (root.exists()) { |
| 1402 |
return true; |
| 1403 |
} |
| 1404 |
} |
| 1405 |
return false; |
| 1406 |
} |
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
private static TrieNode longestMatch( |
| 1413 |
TrieNode root, AbstractMessageParser p, int start) { |
| 1414 |
return longestMatch(root, p, start, false); |
| 1415 |
} |
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
private static TrieNode longestMatch( |
| 1423 |
TrieNode root, AbstractMessageParser p, int start, boolean smiley) { |
| 1424 |
int index = start; |
| 1425 |
TrieNode bestMatch = null; |
| 1426 |
while (index < p.getRawText().length()) { |
| 1427 |
root = root.getChild(p.getRawText().charAt(index++)); |
| 1428 |
if (root == null) { |
| 1429 |
break; |
| 1430 |
} else if (root.exists()) { |
| 1431 |
if (p.isWordBreak(index)) { |
| 1432 |
bestMatch = root; |
| 1433 |
} else if (smiley && p.isSmileyBreak(index)) { |
| 1434 |
bestMatch = root; |
| 1435 |
} |
| 1436 |
} |
| 1437 |
} |
| 1438 |
return bestMatch; |
| 1439 |
} |
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
public static class Part { |
| 1444 |
private String meText; |
| 1445 |
private ArrayList<Token> tokens; |
| 1446 |
|
| 1447 |
public Part() { |
| 1448 |
this.tokens = new ArrayList<Token>(); |
| 1449 |
} |
| 1450 |
|
| 1451 |
public String getType(boolean isSend) { |
| 1452 |
return (isSend ? "s" : "r") + getPartType(); |
| 1453 |
} |
| 1454 |
|
| 1455 |
private String getPartType() { |
| 1456 |
if (isMedia()) { |
| 1457 |
return "d"; |
| 1458 |
} else if (meText != null) { |
| 1459 |
return "m"; |
| 1460 |
} else { |
| 1461 |
return ""; |
| 1462 |
} |
| 1463 |
} |
| 1464 |
|
| 1465 |
public boolean isMedia() { |
| 1466 |
return (tokens.size() == 1) && tokens.get(0).isMedia(); |
| 1467 |
} |
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
public Token getMediaToken() { |
| 1477 |
if(isMedia()) { |
| 1478 |
return tokens.get(0); |
| 1479 |
} |
| 1480 |
return null; |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
public void add(Token token) { |
| 1485 |
if (isMedia()) { |
| 1486 |
throw new AssertionError("media "); |
| 1487 |
} |
| 1488 |
tokens.add(token); |
| 1489 |
} |
| 1490 |
|
| 1491 |
public void setMeText(String meText) { |
| 1492 |
this.meText = meText; |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
public String getRawText() { |
| 1497 |
StringBuilder buf = new StringBuilder(); |
| 1498 |
if (meText != null) { |
| 1499 |
buf.append(meText); |
| 1500 |
} |
| 1501 |
for (int i = 0; i < tokens.size(); ++i) { |
| 1502 |
buf.append(tokens.get(i).getRawText()); |
| 1503 |
} |
| 1504 |
return buf.toString(); |
| 1505 |
} |
| 1506 |
|
| 1507 |
|
| 1508 |
public ArrayList<Token> getTokens() { return tokens; } |
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
} |
| 1523 |
} |