1 7 package com.ibm.icu.text; 8 9 import java.util.*; 10 import com.ibm.icu.impl.UtilityExtensions; 11 12 26 class TransliterationRuleSet { 27 30 private Vector ruleVector; 31 32 35 private int maxContextLength; 36 37 43 private TransliterationRule[] rules; 44 45 50 private int[] index; 51 52 private static final String COPYRIGHT = 53 "\u00A9 IBM Corporation 1999-2001. All rights reserved."; 54 55 58 public TransliterationRuleSet() { 59 ruleVector = new Vector(); 60 maxContextLength = 0; 61 } 62 63 67 public int getMaximumContextLength() { 68 return maxContextLength; 69 } 70 71 76 public void addRule(TransliterationRule rule) { 77 ruleVector.addElement(rule); 78 int len; 79 if ((len = rule.getAnteContextLength()) > maxContextLength) { 80 maxContextLength = len; 81 } 82 83 rules = null; 84 } 85 86 91 public void freeze() { 92 108 int n = ruleVector.size(); 109 index = new int[257]; Vector v = new Vector(2*n); 112 114 int[] indexValue = new int[n]; 115 for (int j=0; j<n; ++j) { 116 TransliterationRule r = (TransliterationRule) ruleVector.elementAt(j); 117 indexValue[j] = r.getIndexValue(); 118 } 119 for (int x=0; x<256; ++x) { 120 index[x] = v.size(); 121 for (int j=0; j<n; ++j) { 122 if (indexValue[j] >= 0) { 123 if (indexValue[j] == x) { 124 v.addElement(ruleVector.elementAt(j)); 125 } 126 } else { 127 TransliterationRule r = (TransliterationRule) ruleVector.elementAt(j); 132 if (r.matchesIndexValue(x)) { 133 v.addElement(r); 134 } 135 } 136 } 137 } 138 index[256] = v.size(); 139 140 142 rules = new TransliterationRule[v.size()]; 143 v.copyInto(rules); 144 145 StringBuffer errors = null; 146 147 154 for (int x=0; x<256; ++x) { 155 for (int j=index[x]; j<index[x+1]-1; ++j) { 156 TransliterationRule r1 = rules[j]; 157 for (int k=j+1; k<index[x+1]; ++k) { 158 TransliterationRule r2 = rules[k]; 159 if (r1.masks(r2)) { 160 if (errors == null) { 161 errors = new StringBuffer (); 162 } else { 163 errors.append("\n"); 164 } 165 errors.append("Rule " + r1 + " masks " + r2); 166 } 167 } 168 } 169 } 170 171 if (errors != null) { 172 throw new IllegalArgumentException (errors.toString()); 173 } 174 } 175 176 189 public boolean transliterate(Replaceable text, 190 Transliterator.Position pos, 191 boolean incremental) { 192 int indexByte = text.char32At(pos.start) & 0xFF; 193 for (int i=index[indexByte]; i<index[indexByte+1]; ++i) { 194 int m = rules[i].matchAndReplace(text, pos, incremental); 195 switch (m) { 196 case UnicodeMatcher.U_MATCH: 197 if (Transliterator.DEBUG) { 198 System.out.println((incremental ? "Rule.i: match ":"Rule: match ") + 199 rules[i].toRule(true) + " => " + 200 UtilityExtensions.formatInput(text, pos)); 201 } 202 return true; 203 case UnicodeMatcher.U_PARTIAL_MATCH: 204 if (Transliterator.DEBUG) { 205 System.out.println((incremental ? "Rule.i: partial match ":"Rule: partial match ") + 206 rules[i].toRule(true) + " => " + 207 UtilityExtensions.formatInput(text, pos)); 208 } 209 return false; 210 } 211 } 212 pos.start += UTF16.getCharCount(text.char32At(pos.start)); 214 if (Transliterator.DEBUG) { 215 System.out.println((incremental ? "Rule.i: no match => ":"Rule: no match => ") + 216 UtilityExtensions.formatInput(text, pos)); 217 } 218 return true; 219 } 220 221 224 String toRules(boolean escapeUnprintable) { 225 int i; 226 int count = ruleVector.size(); 227 StringBuffer ruleSource = new StringBuffer (); 228 for (i=0; i<count; ++i) { 229 if (i != 0) { 230 ruleSource.append('\n'); 231 } 232 TransliterationRule r = 233 (TransliterationRule) ruleVector.elementAt(i); 234 ruleSource.append(r.toRule(escapeUnprintable)); 235 } 236 return ruleSource.toString(); 237 } 238 239 243 UnicodeSet getSourceTargetSet(boolean getTarget) { 244 UnicodeSet set = new UnicodeSet(); 245 int count = ruleVector.size(); 246 for (int i=0; i<count; ++i) { 247 TransliterationRule r = 248 (TransliterationRule) ruleVector.elementAt(i); 249 if (getTarget) { 250 r.addTargetSetTo(set); 251 } else { 252 r.addSourceSetTo(set); 253 } 254 } 255 return set; 256 } 257 } 258 | Popular Tags |