1/*
2**********************************************************************
3*   Copyright (c) 2002-2010, International Business Machines Corporation
4*   and others.  All Rights Reserved.
5**********************************************************************
6*   Date        Name        Description
7*   01/14/2002  aliu        Creation.
8**********************************************************************
9*/
10
11package 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 */
20class 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,
48                       int start,
49                       int limit,
50                       int[] cursor) {
51
52        // First delegate to subordinate replacer
53        int len = replacer.replace(text, start, limit, cursor);
54        limit = start + len;
55
56        // Now transliterate
57        limit = translit.transliterate(text, start, limit);
58
59        return limit - start;
60    }
61
62    /**
63     * UnicodeReplacer API
64     */
65    public String toReplacerPattern(boolean escapeUnprintable) {
66        StringBuilder rule = new StringBuilder("&");
67        rule.append(translit.getID());
68        rule.append("( ");
69        rule.append(replacer.toReplacerPattern(escapeUnprintable));
70        rule.append(" )");
71        return rule.toString();
72    }
73
74    /**
75     * Union the set of all characters that may output by this object
76     * into the given set.
77     * @param toUnionTo the set into which to union the output characters
78     */
79    public void addReplacementSetTo(UnicodeSet toUnionTo) {
80        toUnionTo.addAll(translit.getTargetSet());
81    }
82}
83
84//eof
85