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