1 10 package com.ibm.icu.text; 11 import com.ibm.icu.impl.Utility; 12 13 36 class EscapeTransliterator extends Transliterator { 37 38 42 private String prefix; 43 44 47 private String suffix; 48 49 53 private int radix; 54 55 59 private int minDigits; 60 61 65 private boolean grokSupplementals; 66 67 74 private EscapeTransliterator supplementalHandler; 75 76 80 static void register() { 81 Transliterator.registerFactory("Any-Hex/Unicode", new Transliterator.Factory() { 83 public Transliterator getInstance(String ID) { 84 return new EscapeTransliterator("Any-Hex/Unicode", 85 "U+", "", 16, 4, true, null); 86 } 87 }); 88 89 Transliterator.registerFactory("Any-Hex/Java", new Transliterator.Factory() { 91 public Transliterator getInstance(String ID) { 92 return new EscapeTransliterator("Any-Hex/Java", 93 "\\u", "", 16, 4, false, null); 94 } 95 }); 96 97 Transliterator.registerFactory("Any-Hex/C", new Transliterator.Factory() { 99 public Transliterator getInstance(String ID) { 100 return new EscapeTransliterator("Any-Hex/C", 101 "\\u", "", 16, 4, true, 102 new EscapeTransliterator("", "\\U", "", 16, 8, true, null)); 103 } 104 }); 105 106 Transliterator.registerFactory("Any-Hex/XML", new Transliterator.Factory() { 108 public Transliterator getInstance(String ID) { 109 return new EscapeTransliterator("Any-Hex/XML", 110 "&#x", ";", 16, 1, true, null); 111 } 112 }); 113 114 Transliterator.registerFactory("Any-Hex/XML10", new Transliterator.Factory() { 116 public Transliterator getInstance(String ID) { 117 return new EscapeTransliterator("Any-Hex/XML10", 118 "&#", ";", 10, 1, true, null); 119 } 120 }); 121 122 Transliterator.registerFactory("Any-Hex/Perl", new Transliterator.Factory() { 124 public Transliterator getInstance(String ID) { 125 return new EscapeTransliterator("Any-Hex/Perl", 126 "\\x{", "}", 16, 1, true, null); 127 } 128 }); 129 130 Transliterator.registerFactory("Any-Hex", new Transliterator.Factory() { 132 public Transliterator getInstance(String ID) { 133 return new EscapeTransliterator("Any-Hex", 134 "\\u", "", 16, 4, false, null); 135 } 136 }); 137 } 138 139 143 EscapeTransliterator(String ID, String prefix, String suffix, 144 int radix, int minDigits, 145 boolean grokSupplementals, 146 EscapeTransliterator supplementalHandler) { 147 super(ID, null); 148 this.prefix = prefix; 149 this.suffix = suffix; 150 this.radix = radix; 151 this.minDigits = minDigits; 152 this.grokSupplementals = grokSupplementals; 153 this.supplementalHandler = supplementalHandler; 154 } 155 156 159 protected void handleTransliterate(Replaceable text, 160 Position pos, boolean incremental) { 161 int start = pos.start; 162 int limit = pos.limit; 163 164 StringBuffer buf = new StringBuffer (prefix); 165 int prefixLen = prefix.length(); 166 boolean redoPrefix = false; 167 168 while (start < limit) { 169 int c = grokSupplementals ? text.char32At(start) : text.charAt(start); 170 int charLen = grokSupplementals ? UTF16.getCharCount(c) : 1; 171 172 if ((c & 0xFFFF0000) != 0 && supplementalHandler != null) { 173 buf.setLength(0); 174 buf.append(supplementalHandler.prefix); 175 Utility.appendNumber(buf, c, supplementalHandler.radix, 176 supplementalHandler.minDigits); 177 buf.append(supplementalHandler.suffix); 178 redoPrefix = true; 179 } else { 180 if (redoPrefix) { 181 buf.setLength(0); 182 buf.append(prefix); 183 redoPrefix = false; 184 } else { 185 buf.setLength(prefixLen); 186 } 187 Utility.appendNumber(buf, c, radix, minDigits); 188 buf.append(suffix); 189 } 190 191 text.replace(start, start + charLen, buf.toString()); 192 start += buf.length(); 193 limit += buf.length() - charLen; 194 } 195 196 pos.contextLimit += limit - pos.limit; 197 pos.limit = limit; 198 pos.start = start; 199 } 200 } 201 | Popular Tags |