1 5 package com.ibm.icu.text; 6 import com.ibm.icu.lang.*; 7 import com.ibm.icu.impl.Utility; 8 import com.ibm.icu.impl.UCharacterProperty; 9 import com.ibm.icu.impl.UCharacterName; 10 11 15 class NameUnicodeTransliterator extends Transliterator { 16 17 char openDelimiter; 18 char closeDelimiter; 19 20 static final String _ID = "Name-Any"; 21 22 static final String OPEN_PAT = "\\N~{~"; 23 static final char OPEN_DELIM = '\\'; static final char CLOSE_DELIM = '}'; 25 static final char SPACE = ' '; 26 27 28 31 static void register() { 32 Transliterator.registerFactory(_ID, new Transliterator.Factory() { 33 public Transliterator getInstance(String ID) { 34 return new NameUnicodeTransliterator(null); 35 } 36 }); 37 } 38 39 42 public NameUnicodeTransliterator(UnicodeFilter filter) { 43 super(_ID, filter); 44 } 45 46 49 protected void handleTransliterate(Replaceable text, 50 Position offsets, boolean isIncremental) { 51 52 int maxLen = UCharacterName.getInstance().getMaxCharNameLength() + 1; 54 StringBuffer name = new StringBuffer (maxLen); 55 56 UnicodeSet legal = new UnicodeSet(); 58 UCharacterName.getInstance().getCharNameCharacters(legal); 59 60 int cursor = offsets.start; 61 int limit = offsets.limit; 62 63 int mode = 0; 67 int openPos = -1; 69 int c; 70 while (cursor < limit) { 71 c = text.char32At(cursor); 72 73 switch (mode) { 74 case 0: if (c == OPEN_DELIM) { openPos = cursor; 77 int i = Utility.parsePattern(OPEN_PAT, text, cursor, limit); 78 if (i >= 0 && i < limit) { 79 mode = 1; 80 name.setLength(0); 81 cursor = i; 82 continue; } 84 } 85 break; 86 87 case 1: 93 if (UCharacterProperty.isRuleWhiteSpace(c)) { 96 if (name.length() > 0 && 98 name.charAt(name.length()-1) != SPACE) { 99 name.append(SPACE); 100 if (name.length() > maxLen) { 103 mode = 0; 104 } 105 } 106 break; 107 } 108 109 if (c == CLOSE_DELIM) { 110 111 int len = name.length(); 112 113 if (len > 0 && 115 name.charAt(len-1) == SPACE) { 116 name.setLength(--len); 117 } 118 119 c = UCharacter.getCharFromExtendedName(name.toString()); 120 if (c != -1) { 121 123 cursor++; 126 String str = UTF16.valueOf(c); 127 text.replace(openPos, cursor, str); 128 129 int delta = cursor - openPos - str.length(); 133 cursor -= delta; 134 limit -= delta; 135 } 137 mode = 0; 140 openPos = -1; continue; } 143 144 if (legal.contains(c)) { 145 UTF16.append(name, c); 146 if (name.length() >= maxLen) { 149 mode = 0; 150 } 151 } 152 153 else { 155 --cursor; mode = 0; 157 } 158 159 break; 160 } 161 162 cursor += UTF16.getCharCount(c); 163 } 164 165 offsets.contextLimit += limit - offsets.limit; 166 offsets.limit = limit; 167 offsets.start = (isIncremental && openPos >= 0) ? openPos : cursor; 170 } 171 } 172 | Popular Tags |