]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/shaping/ArabicShapingTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / shaping / ArabicShapingTest.java
1 /*\r
2 *******************************************************************************\r
3 *   Copyright (C) 2001-2009, International Business Machines\r
4 *   Corporation and others.  All Rights Reserved.\r
5 *******************************************************************************\r
6 */\r
7 \r
8 package com.ibm.icu.dev.test.shaping;\r
9 \r
10 import com.ibm.icu.text.ArabicShaping;\r
11 import com.ibm.icu.text.ArabicShapingException;\r
12 \r
13 /**\r
14  * Interactive test for Arabic shaping.\r
15  * Invoke from a command line passing args and strings.  Use '-help' to see description of arguments.\r
16  */\r
17 public class ArabicShapingTest{\r
18     private static final int COPY = 0;\r
19     private static final int INPLACE = 1;\r
20     private static final int STRING = 2;\r
21 \r
22     public static final void main(String[] args) {\r
23         int testtype = COPY;\r
24         int options = 0;\r
25         int ss = 0;\r
26         int sl = -1;\r
27         int ds = 0;\r
28         int dl = -1;\r
29         String text = "$22.4 test 123 \ufef6\u0644\u0622 456 \u0664\u0665\u0666!";\r
30 \r
31         for (int i = 0; i < args.length; ++i) {\r
32             String arg = args[i];\r
33             if (arg.charAt(0) == '-') {\r
34                 String opt = arg.substring(1);\r
35                 String val = opt;\r
36                 int index = arg.indexOf(':');\r
37                 if (index != -1) {\r
38                     opt = opt.substring(0, Math.min(index, 3));\r
39                     val = arg.substring(index + 1);\r
40                 }\r
41                 \r
42                 if (opt.equalsIgnoreCase("len")) {\r
43                     options &= ~ArabicShaping.LENGTH_MASK;\r
44                     if (val.equalsIgnoreCase("gs")) {\r
45                         options |= ArabicShaping.LENGTH_GROW_SHRINK;\r
46                     } else if (val.equalsIgnoreCase("sn")) {\r
47                         options |= ArabicShaping.LENGTH_FIXED_SPACES_NEAR;\r
48                     } else if (val.equalsIgnoreCase("se")) {\r
49                         options |= ArabicShaping.LENGTH_FIXED_SPACES_AT_END;\r
50                     } else if (val.equalsIgnoreCase("sb")) {\r
51                         options |= ArabicShaping.LENGTH_FIXED_SPACES_AT_BEGINNING;\r
52                     } else {\r
53                         throwValError(opt, val);\r
54                     }\r
55                 } else if (opt.equalsIgnoreCase("dir")) {\r
56                     options &= ~ArabicShaping.TEXT_DIRECTION_MASK;\r
57                     if (val.equalsIgnoreCase("log")) {\r
58                         options |= ArabicShaping.TEXT_DIRECTION_LOGICAL;\r
59                     } else if (val.equalsIgnoreCase("vis")) {\r
60                         options |= ArabicShaping.TEXT_DIRECTION_VISUAL_LTR;\r
61                     } else {\r
62                         throwValError(opt, val);\r
63                     }\r
64                 } else if (opt.equalsIgnoreCase("let")) {\r
65                     options &= ~ArabicShaping.LETTERS_MASK;\r
66                     if (val.equalsIgnoreCase("no")) {\r
67                         options |= ArabicShaping.LETTERS_NOOP;\r
68                     } else if (val.equalsIgnoreCase("sh")) {\r
69                         options |= ArabicShaping.LETTERS_SHAPE;\r
70                     } else if (val.equalsIgnoreCase("un")) {\r
71                         options |= ArabicShaping.LETTERS_UNSHAPE;\r
72                     } else if (val.equalsIgnoreCase("ta")) {\r
73                         options |= ArabicShaping.LETTERS_SHAPE_TASHKEEL_ISOLATED;\r
74                     } else {\r
75                         throwValError(opt, val);\r
76                     }\r
77                 } else if (opt.equalsIgnoreCase("dig")) {\r
78                     options &= ~ArabicShaping.DIGITS_MASK;\r
79                     if (val.equalsIgnoreCase("no")) {\r
80                         options |= ArabicShaping.DIGITS_NOOP;\r
81                     } else if (val.equalsIgnoreCase("ea")) {\r
82                         options |= ArabicShaping.DIGITS_EN2AN;\r
83                     } else if (val.equalsIgnoreCase("ae")) {\r
84                         options |= ArabicShaping.DIGITS_AN2EN;\r
85                     } else if (val.equalsIgnoreCase("lr")) {\r
86                         options |= ArabicShaping.DIGITS_EN2AN_INIT_LR;\r
87                     } else if (val.equalsIgnoreCase("al")) {\r
88                         options |= ArabicShaping.DIGITS_EN2AN_INIT_AL;\r
89                     } else {\r
90                         throwValError(opt, val);\r
91                     }\r
92                 } else if (opt.equalsIgnoreCase("typ")) {\r
93                     options &= ~ArabicShaping.DIGIT_TYPE_MASK;\r
94                     if (val.equalsIgnoreCase("an")) {\r
95                         options |= ArabicShaping.DIGIT_TYPE_AN;\r
96                     } else if (val.equalsIgnoreCase("ex")) {\r
97                         options |= ArabicShaping.DIGIT_TYPE_AN_EXTENDED;\r
98                     } else {\r
99                         throwValError(opt, val);\r
100                     }\r
101                 } else if (opt.equalsIgnoreCase("dst")) {\r
102                     try {\r
103                         ds = Integer.parseInt(val);\r
104                     }\r
105                     catch (Exception e) {\r
106                         throwValError(opt, val);\r
107                     }\r
108                 } else if (opt.equalsIgnoreCase("dln")) {\r
109                     try {\r
110                         dl = Integer.parseInt(val);\r
111                     }\r
112                     catch (Exception e) {\r
113                         throwValError(opt, val);\r
114                     }\r
115                 } else if (opt.equalsIgnoreCase("sst")) {\r
116                     try {\r
117                         ss = Integer.parseInt(val);\r
118                     }\r
119                     catch (Exception e) {\r
120                         throwValError(opt, val);\r
121                     }\r
122                 } else if (opt.equalsIgnoreCase("sln")) {\r
123                     try {\r
124                         sl = Integer.parseInt(val);\r
125                     }\r
126                     catch (Exception e) {\r
127                         throwValError(opt, val);\r
128                     }\r
129                 } else if (opt.equalsIgnoreCase("tes")) {\r
130                     if (val.equalsIgnoreCase("cp")) {\r
131                         testtype = COPY;\r
132                     } else if (val.equalsIgnoreCase("ip")) {\r
133                         testtype = INPLACE;\r
134                     } else if (val.equalsIgnoreCase("st")) {\r
135                         testtype = STRING;\r
136                     } else {\r
137                         throwValError(opt, val);\r
138                     }\r
139                 } else if (opt.equalsIgnoreCase("help")) {\r
140                     System.out.println(usage);\r
141                 } else {\r
142                     throwOptError(opt);\r
143                 }\r
144             } else {\r
145                 // assume text\r
146                 text = parseText(arg);\r
147             }\r
148         }\r
149 \r
150         if (sl < 0) {\r
151             sl = text.length() - ss;\r
152             System.out.println("sl defaulting to " + sl);\r
153         }\r
154         if (dl < 0) {\r
155             dl = 2 * sl;\r
156             System.out.println("dl defaulting to " + dl);\r
157         }\r
158 \r
159         ArabicShaping shaper = new ArabicShaping(options);\r
160         System.out.println("shaper: " + shaper);\r
161 \r
162         char[] src = text.toCharArray();\r
163         System.out.println(" input: '" + escapedText(src, ss, sl) + "'");\r
164         if (testtype != STRING) {\r
165             System.out.println("start: " + ss + " length: " + sl + " total length: " + src.length);\r
166         }\r
167 \r
168         int result = -1;\r
169         char[] dest = null;\r
170 \r
171         try {\r
172             switch (testtype) {\r
173             case COPY:\r
174                 dest = new char[ds + dl];\r
175                 result = shaper.shape(src, ss, sl, dest, ds, dl);\r
176                 break;\r
177 \r
178             case INPLACE:\r
179                 shaper.shape(src, ss, sl);\r
180                 ds = ss;\r
181                 result = sl;\r
182                 dest = src;\r
183                 break;\r
184 \r
185             case STRING:\r
186                 dest = shaper.shape(text).toCharArray();\r
187                 ds = 0;\r
188                 result = dest.length;\r
189                 break;\r
190             }\r
191 \r
192             System.out.println("output: '" + escapedText(dest, ds, result) + "'");\r
193             System.out.println("length: " + result);\r
194             if (ds != 0 || result != dest.length) {\r
195                 System.out.println("full output: '" + escapedText(dest, 0, dest.length) + "'");\r
196             }\r
197         }\r
198         catch (ArabicShapingException e) {\r
199             System.out.println("Caught ArabicShapingException");\r
200             System.out.println(e);\r
201         }\r
202         catch (Exception e) {\r
203             System.out.println("Caught Exception");\r
204             System.out.println(e);\r
205         }\r
206     }\r
207 \r
208     private static void throwOptError(String opt) {\r
209         throwUsageError("unknown option: " + opt);\r
210     }\r
211 \r
212     private static void throwValError(String opt, String val) {\r
213         throwUsageError("unknown value: " + val + " for option: " + opt);\r
214     }\r
215 \r
216     private static void throwUsageError(String message) {\r
217         StringBuffer buf = new StringBuffer("*** usage error ***\n");\r
218         buf.append(message);\r
219         buf.append("\n");\r
220         buf.append(usage);\r
221         throw new Error(buf.toString());\r
222     }\r
223 \r
224     private static final String usage = \r
225         "Usage: [option]* [text]\n" +\r
226         "  where option is in the format '-opt[:val]'\n" +\r
227         "  options are:\n" +\r
228         "    -len:[gs|sn|se|sb]    (length: grow/shrink, spaces near, spaces end, spaces beginning)\n" +\r
229         "    -dir:[log|vis]        (direction: logical, visual)\n" +\r
230         "    -let:[no|sh|un|ta]    (letters: noop, shape, unshape, tashkeel)\n" +\r
231         // "    -let:[no|sh|un]       (letters: noop, shape, unshape)\n" +\r
232         "    -dig:[no|ea|ae|lr|al] (digits: noop, en2an, an2en, en2an_lr, en2an_al)\n" +\r
233         "    -typ:[an|ex]          (digit type: arabic, arabic extended)\n" +\r
234         "    -dst:#                (dest start: [integer])\n" +\r
235         "    -dln:#                (dest length (max size): [integer])\n" +\r
236         "    -sst:#                (source start: [integer])\n" +\r
237         "    -sln:#                (source length: [integer])\n" +\r
238         "    -tes:[cp|ip|st]       (test type: copy, in place, string)\n" +\r
239         "    -help                 (print this help message)\n" +\r
240         "  text can contain unicode escape values in the format '\\uXXXX' only\n";\r
241         \r
242     private static String escapedText(char[] text, int start, int length) {\r
243         StringBuffer buf = new StringBuffer();\r
244         for (int i = start, e = start + length; i < e; ++i) {\r
245             char ch = text[i];\r
246             if (ch < 0x20 || ch > 0x7e) {\r
247                 buf.append("\\u");\r
248                 if (ch < 0x1000) {\r
249                     buf.append('0');\r
250                 }\r
251                 if (ch < 0x100) {\r
252                     buf.append('0');\r
253                 }\r
254                 if (ch < 0x10) {\r
255                     buf.append('0');\r
256                 }\r
257                 buf.append(Integer.toHexString(ch));\r
258             } else {\r
259                 buf.append(ch);\r
260             }\r
261         }\r
262         return buf.toString();\r
263     }\r
264 \r
265     private static String parseText(String text) {\r
266         // process unicode escapes (only)\r
267         StringBuffer buf = new StringBuffer();\r
268         char[] chars = text.toCharArray();\r
269         for (int i = 0; i < chars.length; ++i) {\r
270             char ch = chars[i];\r
271             if (ch == '\\') {\r
272                 if ((i < chars.length - 1) &&\r
273                     (chars[i+1] == 'u')) {\r
274                     int val = Integer.parseInt(text.substring(i+2, i+6), 16);\r
275                     buf.append((char)val);\r
276                     i += 5;\r
277                 } else {\r
278                     buf.append('\\');\r
279                 }\r
280             } else {\r
281                 buf.append(ch);\r
282             }\r
283         }\r
284         return buf.toString();\r
285     }\r
286 }\r