]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/translit/src/com/ibm/icu/impl/UtilityExtensions.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / translit / src / com / ibm / icu / impl / UtilityExtensions.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2002-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.impl;\r
8 \r
9 import com.ibm.icu.text.Replaceable;\r
10 import com.ibm.icu.text.ReplaceableString;\r
11 import com.ibm.icu.text.Transliterator;\r
12 import com.ibm.icu.text.UnicodeMatcher;\r
13 /**\r
14  * @author Ram\r
15  */\r
16 //This class contains utility functions so testing not needed\r
17 ///CLOVER:OFF\r
18 public class UtilityExtensions {\r
19     /**\r
20      * Append the given string to the rule.  Calls the single-character\r
21      * version of appendToRule for each character.\r
22      */\r
23     public static void appendToRule(StringBuffer rule,\r
24                                     String text,\r
25                                     boolean isLiteral,\r
26                                     boolean escapeUnprintable,\r
27                                     StringBuffer quoteBuf) {\r
28         for (int i=0; i<text.length(); ++i) {\r
29             // Okay to process in 16-bit code units here\r
30             Utility.appendToRule(rule, text.charAt(i), isLiteral, escapeUnprintable, quoteBuf);\r
31         }\r
32     }\r
33 \r
34 \r
35     /**\r
36      * Given a matcher reference, which may be null, append its\r
37      * pattern as a literal to the given rule.\r
38      */\r
39     public static void appendToRule(StringBuffer rule,\r
40                                     UnicodeMatcher matcher,\r
41                                     boolean escapeUnprintable,\r
42                                     StringBuffer quoteBuf) {\r
43         if (matcher != null) {\r
44             appendToRule(rule, matcher.toPattern(escapeUnprintable),\r
45                          true, escapeUnprintable, quoteBuf);\r
46         }\r
47     }\r
48     /**\r
49      * For debugging purposes; format the given text in the form\r
50      * aaa{bbb|ccc|ddd}eee, where the {} indicate the context start\r
51      * and limit, and the || indicate the start and limit.\r
52      */\r
53     public static String formatInput(ReplaceableString input,\r
54                                      Transliterator.Position pos) {\r
55         StringBuffer appendTo = new StringBuffer();\r
56         formatInput(appendTo, input, pos);\r
57         return com.ibm.icu.impl.Utility.escape(appendTo.toString());\r
58     }\r
59 \r
60     /**\r
61      * For debugging purposes; format the given text in the form\r
62      * aaa{bbb|ccc|ddd}eee, where the {} indicate the context start\r
63      * and limit, and the || indicate the start and limit.\r
64      */\r
65     public static StringBuffer formatInput(StringBuffer appendTo,\r
66                                            ReplaceableString input,\r
67                                            Transliterator.Position pos) {\r
68         if (0 <= pos.contextStart &&\r
69             pos.contextStart <= pos.start &&\r
70             pos.start <= pos.limit &&\r
71             pos.limit <= pos.contextLimit &&\r
72             pos.contextLimit <= input.length()) {\r
73 \r
74             String  b, c, d;\r
75             //a = input.substring(0, pos.contextStart);\r
76             b = input.substring(pos.contextStart, pos.start);\r
77             c = input.substring(pos.start, pos.limit);\r
78             d = input.substring(pos.limit, pos.contextLimit);\r
79             //e = input.substring(pos.contextLimit, input.length());\r
80             appendTo.//append(a).\r
81                 append('{').append(b).\r
82                 append('|').append(c).append('|').append(d).\r
83                 append('}')\r
84                 //.append(e)\r
85                 ;\r
86         } else {\r
87             appendTo.append("INVALID Position {cs=" +\r
88                             pos.contextStart + ", s=" + pos.start + ", l=" +\r
89                             pos.limit + ", cl=" + pos.contextLimit + "} on " +\r
90                             input);\r
91         }\r
92         return appendTo;\r
93     }\r
94 \r
95     /**\r
96      * Convenience method.\r
97      */\r
98     public static String formatInput(Replaceable input,\r
99                                      Transliterator.Position pos) {\r
100         return formatInput((ReplaceableString) input, pos);\r
101     }\r
102 \r
103     /**\r
104      * Convenience method.\r
105      */\r
106     public static StringBuffer formatInput(StringBuffer appendTo,\r
107                                            Replaceable input,\r
108                                            Transliterator.Position pos) {\r
109         return formatInput(appendTo, (ReplaceableString) input, pos);\r
110     }\r
111 \r
112 }\r
113 //CLOVER:ON