| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package org.apache.commons.lang3.text.translate; |
| 18 |
|
| 19 |
import java.io.IOException; |
| 20 |
import java.io.Writer; |
| 21 |
import java.util.Arrays; |
| 22 |
import java.util.Collections; |
| 23 |
import java.util.EnumSet; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
@Deprecated |
| 37 |
public class NumericEntityUnescaper extends CharSequenceTranslator { |
| 38 |
|
| 39 |
|
| 40 |
public enum OPTION { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
semiColonRequired, |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
semiColonOptional, |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
errorIfNoSemiColon |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
private final EnumSet<OPTION> options; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public NumericEntityUnescaper(final OPTION... options) { |
| 78 |
if (options.length > 0) { |
| 79 |
this.options = EnumSet.copyOf(Arrays.asList(options)); |
| 80 |
} else { |
| 81 |
this.options = EnumSet.copyOf(Collections.singletonList(OPTION.semiColonRequired)); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public boolean isSet(final OPTION option) { |
| 92 |
return options != null && options.contains(option); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
@Override |
| 99 |
public int translate(final CharSequence input, final int index, final Writer out) throws IOException { |
| 100 |
final int seqEnd = input.length(); |
| 101 |
|
| 102 |
if (input.charAt(index) == '&' && index < seqEnd - 2 && input.charAt(index + 1) == '#') { |
| 103 |
int start = index + 2; |
| 104 |
boolean isHex = false; |
| 105 |
|
| 106 |
final char firstChar = input.charAt(start); |
| 107 |
if (firstChar == 'x' || firstChar == 'X') { |
| 108 |
start++; |
| 109 |
isHex = true; |
| 110 |
|
| 111 |
|
| 112 |
if (start == seqEnd) { |
| 113 |
return 0; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
int end = start; |
| 118 |
|
| 119 |
while (end < seqEnd && ( input.charAt(end) >= '0' && input.charAt(end) <= '9' || |
| 120 |
input.charAt(end) >= 'a' && input.charAt(end) <= 'f' || |
| 121 |
input.charAt(end) >= 'A' && input.charAt(end) <= 'F' ) ) { |
| 122 |
end++; |
| 123 |
} |
| 124 |
|
| 125 |
final boolean semiNext = end != seqEnd && input.charAt(end) == ';'; |
| 126 |
|
| 127 |
if (!semiNext) { |
| 128 |
if (isSet(OPTION.semiColonRequired)) { |
| 129 |
return 0; |
| 130 |
} |
| 131 |
if (isSet(OPTION.errorIfNoSemiColon)) { |
| 132 |
throw new IllegalArgumentException("Semi-colon required at end of numeric entity"); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
final int entityValue; |
| 137 |
try { |
| 138 |
if (isHex) { |
| 139 |
entityValue = Integer.parseInt(input.subSequence(start, end).toString(), 16); |
| 140 |
} else { |
| 141 |
entityValue = Integer.parseInt(input.subSequence(start, end).toString(), 10); |
| 142 |
} |
| 143 |
} catch (final NumberFormatException nfe) { |
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
if (entityValue > 0xFFFF) { |
| 148 |
final char[] chars = Character.toChars(entityValue); |
| 149 |
out.write(chars[0]); |
| 150 |
out.write(chars[1]); |
| 151 |
} else { |
| 152 |
out.write(entityValue); |
| 153 |
} |
| 154 |
|
| 155 |
return 2 + end - start + (isHex ? 1 : 0) + (semiNext ? 1 : 0); |
| 156 |
} |
| 157 |
return 0; |
| 158 |
} |
| 159 |
} |