1 6 package com.ibm.icu.text; 7 8 import java.io.IOException ; 9 10 import com.ibm.icu.impl.UCaseProps; 11 12 import com.ibm.icu.util.ULocale; 13 14 import com.ibm.icu.text.ReplaceableContextIterator; 15 16 23 class TitlecaseTransliterator extends Transliterator { 24 25 static final String _ID = "Any-Title"; 26 27 30 static void register() { 31 Transliterator.registerFactory(_ID, new Transliterator.Factory() { 32 public Transliterator getInstance(String ID) { 33 return new TitlecaseTransliterator(ULocale.US); 34 } 35 }); 36 37 registerSpecialInverse("Title", "Lower", false); 38 } 39 40 private ULocale locale; 41 42 private UCaseProps csp; 43 private ReplaceableContextIterator iter; 44 private StringBuffer result; 45 private int[] locCache; 46 47 50 public TitlecaseTransliterator(ULocale loc) { 51 super(_ID, null); 52 locale = loc; 53 setMaximumContextLength(2); 55 try { 56 csp=UCaseProps.getSingleton(); 57 } catch (IOException e) { 58 csp=null; 59 } 60 iter=new ReplaceableContextIterator(); 61 result = new StringBuffer (); 62 int[] locCache = new int[1]; 63 locCache[0]=0; 64 } 65 66 69 protected void handleTransliterate(Replaceable text, 70 Position offsets, boolean isIncremental) { 71 78 if (offsets.start >= offsets.limit) { 79 return; 80 } 81 82 int type; 84 85 boolean doTitle = true; 88 89 int c, start; 94 for (start = offsets.start - 1; start >= offsets.contextStart; start -= UTF16.getCharCount(c)) { 95 c = text.char32At(start); 96 type=csp.getTypeOrIgnorable(c); 97 if(type>0) { doTitle=false; 99 break; 100 } else if(type==0) { break; 102 } 103 } 105 106 110 iter.setText(text); 111 iter.setIndex(offsets.start); 112 iter.setLimit(offsets.limit); 113 iter.setContextLimits(offsets.contextStart, offsets.contextLimit); 114 115 result.setLength(0); 116 117 int delta; 120 121 while((c=iter.nextCaseMapCP())>=0) { 122 type=csp.getTypeOrIgnorable(c); 123 if(type>=0) { if(doTitle) { 125 c=csp.toFullTitle(c, iter, result, locale, locCache); 126 } else { 127 c=csp.toFullLower(c, iter, result, locale, locCache); 128 } 129 doTitle = type==0; 131 if(iter.didReachLimit() && isIncremental) { 132 offsets.start=iter.getCaseMapCPStart(); 135 return; 136 } 137 138 139 if(c<0) { 140 141 continue; 142 } else if(c<=UCaseProps.MAX_STRING_LENGTH) { 143 144 delta=iter.replace(result.toString()); 145 result.setLength(0); 146 } else { 147 148 delta=iter.replace(UTF16.valueOf(c)); 149 } 150 151 if(delta!=0) { 152 offsets.limit += delta; 153 offsets.contextLimit += delta; 154 } 155 } 156 } 157 offsets.start = offsets.limit; 158 } 159 } 160 | Popular Tags |