Decode an HTML encoded entity. @param source @return 0 if the value is not an encoded entity
(String source)
| 147 | * @return 0 if the value is not an encoded entity |
| 148 | */ |
| 149 | static char decodeEntity(String source) { |
| 150 | if (source.length() < 4 || source.charAt(0) != HTML_ENTITY_START |
| 151 | || source.charAt(source.length() - 1) != HTML_ENTITY_TERMINATE) { |
| 152 | return HTML_ENTITY_INVALID; |
| 153 | } |
| 154 | // Handle entities by value |
| 155 | if (source.charAt(1) == HTML_ENTITY_RAWVALUE) { |
| 156 | try { |
| 157 | if (Character.toLowerCase(source.charAt(2)) == HTML_ENTITY_RAWHEXVALUE) { |
| 158 | return (char) Integer.parseInt(source.substring(3, 4), 16); |
| 159 | } else { |
| 160 | return (char) Integer.parseInt(source.substring(2, 3), 10); |
| 161 | } |
| 162 | } catch (NumberFormatException nfe) { |
| 163 | return HTML_ENTITY_INVALID; |
| 164 | } |
| 165 | } |
| 166 | // Handle entities by alias |
| 167 | source = source.toLowerCase(); |
| 168 | for (int i = 0; i < HTML_ENCODED_ENTITIES.length; i++) { |
| 169 | if (source.equals(HTML_ENCODED_ENTITIES[i])) { |
| 170 | return HTML_ENTITIES[i]; |
| 171 | } |
| 172 | } |
| 173 | return HTML_ENTITY_INVALID; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Encode an HTML entity. |
no test coverage detected