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