decode characters. {@literal "\n" <- "&nl;" "\r" <- "&cr;" "\f" <- "&lf;" ":" <- ":" "|" <- "&pipe;" "[" <- "&lbracket;" "]" <- "&rbracket;" "&" <- "&" } author: Thomas Behr 09-09-02 @param s the String to decode @return the decoded String
(String s)
| 81 | * @return the decoded String |
| 82 | */ |
| 83 | public static String decode(String s) |
| 84 | { |
| 85 | final StringBuilder buffer = new StringBuilder(); |
| 86 | final StringTokenizer tokens = new StringTokenizer(s, "&;", true); |
| 87 | |
| 88 | while (tokens.hasMoreTokens()) |
| 89 | { |
| 90 | String cToken = tokens.nextToken(); |
| 91 | |
| 92 | if ("&".equals(cToken)) |
| 93 | { |
| 94 | String tok1 = null; |
| 95 | String tok2 = null; |
| 96 | |
| 97 | try |
| 98 | { |
| 99 | tok1 = tokens.nextToken(); |
| 100 | tok2 = tokens.nextToken(); |
| 101 | buffer.append(ENTITIES.get(cToken + tok1 + tok2)); |
| 102 | } |
| 103 | catch (NoSuchElementException exc) |
| 104 | { |
| 105 | buffer.append(cToken); |
| 106 | |
| 107 | if (tok1 != null) |
| 108 | { |
| 109 | buffer.append(tok1); |
| 110 | |
| 111 | if (tok2 != null) |
| 112 | { |
| 113 | buffer.append(tok2); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | buffer.append(cToken); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return buffer.toString(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * encode characters. |