01: /*
02: **********************************************************************
03: * Copyright (c) 2002-2003, International Business Machines Corporation
04: * and others. All Rights Reserved.
05: **********************************************************************
06: * Date Name Description
07: * 01/14/2002 aliu Creation.
08: **********************************************************************
09: */
10:
11: package com.ibm.icu.text;
12:
13: /**
14: * A replacer that calls a transliterator to generate its output text.
15: * The input text to the transliterator is the output of another
16: * UnicodeReplacer object. That is, this replacer wraps another
17: * replacer with a transliterator.
18: * @author Alan Liu
19: */
20: class FunctionReplacer implements UnicodeReplacer {
21:
22: /**
23: * The transliterator. Must not be null.
24: */
25: private Transliterator translit;
26:
27: /**
28: * The replacer object. This generates text that is then
29: * processed by 'translit'. Must not be null.
30: */
31: private UnicodeReplacer replacer;
32:
33: /**
34: * Construct a replacer that takes the output of the given
35: * replacer, passes it through the given transliterator, and emits
36: * the result as output.
37: */
38: public FunctionReplacer(Transliterator theTranslit,
39: UnicodeReplacer theReplacer) {
40: translit = theTranslit;
41: replacer = theReplacer;
42: }
43:
44: /**
45: * UnicodeReplacer API
46: */
47: public int replace(Replaceable text, int start, int limit,
48: int[] cursor) {
49:
50: // First delegate to subordinate replacer
51: int len = replacer.replace(text, start, limit, cursor);
52: limit = start + len;
53:
54: // Now transliterate
55: limit = translit.transliterate(text, start, limit);
56:
57: return limit - start;
58: }
59:
60: /**
61: * UnicodeReplacer API
62: */
63: public String toReplacerPattern(boolean escapeUnprintable) {
64: StringBuffer rule = new StringBuffer("&");
65: rule.append(translit.getID());
66: rule.append("( ");
67: rule.append(replacer.toReplacerPattern(escapeUnprintable));
68: rule.append(" )");
69: return rule.toString();
70: }
71:
72: /**
73: * Union the set of all characters that may output by this object
74: * into the given set.
75: * @param toUnionTo the set into which to union the output characters
76: */
77: public void addReplacementSetTo(UnicodeSet toUnionTo) {
78: toUnionTo.addAll(translit.getTargetSet());
79: }
80: }
81:
82: //eof
|