…lines 1–33 unchanged (33 lines)
34 * {@inheritDoc}
35 */
36 @Override
37 public int translate(CharSequence input, int index, Writer out) throws IOException {
38 int seqEnd = input.length();
39 // Uses -2 to ensure there is something after the &#
40 if(input.charAt(index) == '&' && index < seqEnd - 2 && input.charAt(index + 1) == '#') {
41 int start = index + 2;
42 boolean isHex = false;
43
44 char firstChar = input.charAt(start);
45 if(firstChar == 'x' || firstChar == 'X') {
46 start++;
47 isHex = true;
48
49 // Check there's more than just an x after the &#
50 if(start == seqEnd) {
51 return 0;
52 }
53 }
54
55 int end = start;
56 // Note that this supports character codes without a ; on the end
57 while(end < seqEnd && ( (input.charAt(end) >= '0' && input.charAt(end) <= '9') ||
58 (input.charAt(end) >= 'a' && input.charAt(end) <= 'f') ||
59 (input.charAt(end) >= 'A' && input.charAt(end) <= 'F') ) )
60 {
61 end++;
62 }
63
64 int entityValue;
65 try {
…lines 66–77 unchanged (12 lines)
78 out.write(chrs[0]);
79 out.write(chrs[1]);
80 } else {
81 out.write(entityValue);
82 }
83
84 boolean semiNext = (end != seqEnd) && (input.charAt(end) == ';');
85
86 return 2 + (end - start) + (isHex ? 1 : 0) + (semiNext ? 1 : 0);
87 }
88 return 0;
89 }
90}
91