1 7 package com.ibm.icu.text; 8 9 import java.util.Hashtable ; 10 11 12 276 public class RuleBasedTransliterator extends Transliterator { 277 278 private Data data; 279 280 private static final String COPYRIGHT = 281 "\u00A9 IBM Corporation 1999. All rights reserved."; 282 283 292 public RuleBasedTransliterator(String ID, String rules, int direction, 293 UnicodeFilter filter) { 294 super(ID, filter); 295 if (direction != FORWARD && direction != REVERSE) { 296 throw new IllegalArgumentException ("Invalid direction"); 297 } 298 299 TransliteratorParser parser = new TransliteratorParser(); 300 parser.parse(rules, direction); 301 if (parser.idBlockVector.size() != 0 || 302 parser.compoundFilter != null) { 303 throw new IllegalArgumentException ("::ID blocks illegal in RuleBasedTransliterator constructor"); 304 } 305 306 data = (Data)parser.dataVector.get(0); 307 setMaximumContextLength(data.ruleSet.getMaximumContextLength()); 308 } 309 310 319 public RuleBasedTransliterator(String ID, String rules) { 320 this(ID, rules, FORWARD, null); 321 } 322 323 RuleBasedTransliterator(String ID, Data data, UnicodeFilter filter) { 324 super(ID, filter); 325 this.data = data; 326 setMaximumContextLength(data.ruleSet.getMaximumContextLength()); 327 } 328 329 334 protected synchronized void handleTransliterate(Replaceable text, 335 Position index, boolean incremental) { 336 350 351 360 int loopCount = 0; 361 int loopLimit = (index.limit - index.start) << 4; 362 if (loopLimit < 0) { 363 loopLimit = 0x7FFFFFFF; 364 } 365 366 while (index.start < index.limit && 367 loopCount <= loopLimit && 368 data.ruleSet.transliterate(text, index, incremental)) { 369 ++loopCount; 370 } 371 } 372 373 374 static class Data { 375 public Data() { 376 variableNames = new Hashtable (); 377 ruleSet = new TransliterationRuleSet(); 378 } 379 380 383 public TransliterationRuleSet ruleSet; 384 385 394 Hashtable variableNames; 395 396 405 Object [] variables; 406 407 412 char variablesBase; 413 414 418 public UnicodeMatcher lookupMatcher(int standIn) { 419 int i = standIn - variablesBase; 420 return (i >= 0 && i < variables.length) 421 ? (UnicodeMatcher) variables[i] : null; 422 } 423 424 428 public UnicodeReplacer lookupReplacer(int standIn) { 429 int i = standIn - variablesBase; 430 return (i >= 0 && i < variables.length) 431 ? (UnicodeReplacer) variables[i] : null; 432 } 433 } 434 435 436 448 public String toRules(boolean escapeUnprintable) { 449 return data.ruleSet.toRules(escapeUnprintable); 450 } 451 452 458 protected UnicodeSet handleGetSourceSet() { 459 return data.ruleSet.getSourceTargetSet(false); 460 } 461 462 468 public UnicodeSet getTargetSet() { 469 return data.ruleSet.getSourceTargetSet(true); 470 } 471 } 472 473 648 | Popular Tags |