1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
/**
*******************************************************************************
* Copyright (C) 1996-2005, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
*******************************************************************************
*/
#include "JNIHelp.h"
#include "AndroidSystemNatives.h"
#include "ErrorCode.h"
#include "unicode/ucol.h"
#include "unicode/ucoleitr.h"
#include "ucol_imp.h"
/**
* Closing a C UCollator with the argument locale rules.
* Note determining if a collator currently exist for the caller is to be handled
* by the caller. Hence if the caller has a existing collator, it is his
* responsibility to delete first before calling this method.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address of the C UCollator
*/
static void closeCollator(JNIEnv *env, jclass obj,
jint address) {
UCollator *collator = (UCollator *)(int)address;
ucol_close(collator);
}
/**
* Close a C collation element iterator.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address of C collation element iterator to close.
*/
static void closeElements(JNIEnv *env, jclass obj,
jint address) {
UCollationElements *iterator = (UCollationElements *)(int)address;
ucol_closeElements(iterator);
}
/**
* Compare two strings.
* The strings will be compared using the normalization mode and options
* specified in openCollator or openCollatorFromRules
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address address of the c collator
* @param source The source string.
* @param target The target string.
* @return result of the comparison, UCOL_EQUAL, UCOL_GREATER or UCOL_LESS
*/
static jint compare(JNIEnv *env, jclass obj, jint address,
jstring source, jstring target) {
const UCollator *collator = (const UCollator *)(int)address;
jint result = -2;
if(collator){
jsize srcLength = env->GetStringLength(source);
const UChar *chars = (const UChar *) env->GetStringCritical(source,0);
if(chars){
jsize tgtlength = env->GetStringLength(target);
const UChar *tgtstr = (const UChar *) env->GetStringCritical(target,0);
if(tgtstr){
result = ucol_strcoll(collator, chars, srcLength, tgtstr, tgtlength);
env->ReleaseStringCritical(source, chars);
env->ReleaseStringCritical(target, tgtstr);
return result;
}else{
icu4jni_error(env,U_ILLEGAL_ARGUMENT_ERROR);
}
}else{
icu4jni_error(env,U_ILLEGAL_ARGUMENT_ERROR);
}
}else{
icu4jni_error(env,U_ILLEGAL_ARGUMENT_ERROR);
}
return result;
}
static jint getAttribute(JNIEnv *env, jclass, jint address, jint type) {
const UCollator *collator = (const UCollator *)(int)address;
if (!collator) {
icu4jni_error(env, U_ILLEGAL_ARGUMENT_ERROR);
return 0;
}
UErrorCode status = U_ZERO_ERROR;
jint result = (jint)ucol_getAttribute(collator, (UColAttribute)type, &status);
icu4jni_error(env, status);
return result;
}
/**
* Create a CollationElementIterator object that will iterator over the elements
* in a string, using the collation rules defined in this RuleBasedCollatorJNI
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address address of C collator
* @param source string to iterate over
* @return address of C collationelement
*/
static jint getCollationElementIterator(JNIEnv *env,
jclass obj, jint address, jstring source) {
UErrorCode status = U_ZERO_ERROR;
UCollator *collator = (UCollator *)(int)address;
jint result=0;
if(collator){
jsize srcLength = env->GetStringLength(source);
const UChar *chars = (const UChar *) env->GetStringCritical(source,0);
if(chars){
result = (jint)(ucol_openElements(collator, chars, srcLength, &status));
env->ReleaseStringCritical(source, chars);
icu4jni_error(env, status);
}else{
icu4jni_error(env, U_ILLEGAL_ARGUMENT_ERROR);
}
}else{
icu4jni_error(env, U_ILLEGAL_ARGUMENT_ERROR);
}
return result;
}
/**
* Get the maximum length of any expansion sequences that end with the specified
* comparison order.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address of the C collation element iterator containing the text.
* @param order collation order returned by previous or next.
* @return maximum length of any expansion sequences ending with the specified
* order or 1 if collation order does not occur at the end of any
* expansion sequence.
*/
static jint getMaxExpansion(JNIEnv *env, jclass obj,
jint address, jint order) {
UCollationElements *iterator = (UCollationElements *)(int)address;
return ucol_getMaxExpansion(iterator, order);
}
/**
* Get the normalization mode for this object.
* The normalization mode influences how strings are compared.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address of C collator
* @return normalization mode; one of the values from NormalizerEnum
*/
static jint getNormalization(JNIEnv *env, jclass obj,
jint address) {
const UCollator* collator = (const UCollator*) address;
UErrorCode status = U_ZERO_ERROR;
jint result = ucol_getAttribute(collator, UCOL_NORMALIZATION_MODE, &status);
icu4jni_error(env, status);
return result;
}
/**
* Set the normalization mode for this object.
* The normalization mode influences how strings are compared.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address of C collator
* @param mode the normalization mode
*/
static void setNormalization(JNIEnv *env, jclass, jint address, jint mode) {
UCollator* collator = reinterpret_cast<UCollator*>(static_cast<uintptr_t>(address));
UErrorCode status = U_ZERO_ERROR;
ucol_setAttribute(collator, UCOL_NORMALIZATION_MODE, UColAttributeValue(mode), &status);
icu4jni_error(env, status);
}
/**
* Get the offset of the current source character.
* This is an offset into the text of the character containing the current
* collation elements.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param addresss of the C collation elements iterator to query.
* @return offset of the current source character.
*/
static jint getOffset(JNIEnv *env, jclass obj, jint address) {
UCollationElements *iterator = (UCollationElements *)(int)address;
return ucol_getOffset(iterator);
}
/**
* Get the collation rules from a UCollator.
* The rules will follow the rule syntax.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address the address of the C collator
* @return collation rules.
*/
static jstring getRules(JNIEnv *env, jclass obj,
jint address) {
const UCollator *collator = (const UCollator *)(int)address;
int32_t length=0;
const UChar *rules = ucol_getRules(collator, &length);
return env->NewString(rules, length);
}
/**
* Get a sort key for the argument string
* Sort keys may be compared using java.util.Arrays.equals
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address address of the C collator
* @param source string for key to be generated
* @return sort key
*/
static jbyteArray getSortKey(JNIEnv *env, jclass obj,
jint address, jstring source) {
const UCollator *collator = (const UCollator *)(int)address;
jbyteArray result = NULL;
if(collator && source){
// BEGIN android-added
if(!source) {
return NULL;
}
// END android-added
jsize srcLength = env->GetStringLength(source);
const UChar *chars = (const UChar *) env->GetStringCritical(source, 0);
if (chars){
// BEGIN android-changed
uint8_t byteArray[UCOL_MAX_BUFFER * 2];
uint8_t *largerByteArray = NULL;
uint8_t *usedByteArray = byteArray;
size_t byteArraySize = ucol_getSortKey(collator, chars, srcLength, byteArray,
sizeof(byteArray) - 1);
if (byteArraySize > sizeof(byteArray) - 1) {
// didn't fit, try again with a larger buffer.
largerByteArray = new uint8_t[byteArraySize + 1];
usedByteArray = largerByteArray;
byteArraySize = ucol_getSortKey(collator, chars, srcLength, largerByteArray,
byteArraySize);
}
env->ReleaseStringCritical(source, chars);
if (byteArraySize == 0) {
delete[] largerByteArray;
return NULL;
}
/* no problem converting uint8_t to int8_t, gives back the correct value
* tried and tested
*/
result = env->NewByteArray(byteArraySize);
env->SetByteArrayRegion(result, 0, byteArraySize, reinterpret_cast<jbyte*>(usedByteArray));
delete[] largerByteArray;
// END android-changed
}else{
icu4jni_error(env,U_ILLEGAL_ARGUMENT_ERROR);
}
}else{
icu4jni_error(env,U_ILLEGAL_ARGUMENT_ERROR);
}
return result;
}
/**
* Returns a hash of this collation object
* Note this method is not complete, it only returns 0 at the moment.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address address of C collator
* @return hash of this collation object
*/
static jint hashCode(JNIEnv *env, jclass obj, jint address) {
UCollator *collator = (UCollator *)(int)address;
int32_t length=0;
const UChar *rules = ucol_getRules(collator, &length);
/* temporary commented out
* return uhash_hashUCharsN(rules, length);
*/
return 0;
}
/**
* Get the ordering priority of the next collation element in the text.
* A single character may contain more than one collation element.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address if C collation elements containing the text.
* @return next collation elements ordering, otherwise returns NULLORDER if an
* error has occured or if the end of string has been reached
*/
static jint next(JNIEnv *env, jclass obj, jint address) {
UCollationElements *iterator = (UCollationElements *) address;
UErrorCode status = U_ZERO_ERROR;
jint result = ucol_next(iterator, &status);
icu4jni_error(env, status);
return result;
}
/**
* Opening a new C UCollator with the default locale.
* Note determining if a collator currently exist for the caller is to be handled
* by the caller. Hence if the caller has a existing collator, it is his
* responsibility to delete first before calling this method.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @return address of the new C UCollator
* @exception thrown if creation of the UCollator fails
*/
static jint openCollator__(JNIEnv *env, jclass obj) {
UErrorCode status = U_ZERO_ERROR;
UCollator* result = ucol_open(NULL, &status);
icu4jni_error(env, status);
return reinterpret_cast<uintptr_t>(result);
}
/**
* Opening a new C UCollator with the argument locale rules.
* Note determining if a collator currently exist for the caller is to be handled
* by the caller. Hence if the caller has a existing collator, it is his
* responsibility to delete first before calling this method.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param locale name
* @return address of the new C UCollator
* @exception thrown if creation of the UCollator fails
*/
static jint openCollator__Ljava_lang_String_2(JNIEnv *env,
jclass obj, jstring locale) {
/* this will be null terminated */
const char* localeStr = env->GetStringUTFChars(locale, NULL);
if (localeStr == NULL) {
icu4jni_error(env, U_ILLEGAL_ARGUMENT_ERROR);
return 0;
}
UErrorCode status = U_ZERO_ERROR;
UCollator* result = ucol_open(localeStr, &status);
env->ReleaseStringUTFChars(locale, localeStr);
icu4jni_error(env, status);
return reinterpret_cast<uintptr_t>(result);
}
/**
* Opening a new C UCollator with the argument locale rules.
* Note determining if a collator currently exist for the caller is to be
* handled by the caller. Hence if the caller has a existing collator, it is his
* responsibility to delete first before calling this method.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param rules set of collation rules
* @param normalizationmode normalization mode
* @param strength collation strength
* @return address of the new C UCollator
* @exception thrown if creation of the UCollator fails
*/
static jint openCollatorFromRules(JNIEnv *env, jclass obj,
jstring rules, jint normalizationmode, jint strength) {
jsize ruleslength = env->GetStringLength(rules);
const UChar *rulestr = (const UChar *) env->GetStringCritical(rules, 0);
UErrorCode status = U_ZERO_ERROR;
jint result = 0;
if(rulestr){
result = (jint)ucol_openRules(rulestr, ruleslength,
(UColAttributeValue)normalizationmode,
(UCollationStrength)strength, NULL, &status);
env->ReleaseStringCritical(rules, rulestr);
icu4jni_error(env, status);
}else{
icu4jni_error(env,U_ILLEGAL_ARGUMENT_ERROR);
}
return result;
}
/**
* Get the ordering priority of the previous collation element in the text.
* A single character may contain more than one collation element.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address of the C collation element iterator containing the text.
* @return previous collation element ordering, otherwise returns NULLORDER if
* an error has occured or if the start of string has been reached
* @exception thrown when retrieval of previous collation element fails.
*/
static jint previous(JNIEnv *env, jclass obj, jint address) {
UCollationElements *iterator = (UCollationElements *)(int)address;
UErrorCode status = U_ZERO_ERROR;
jint result = ucol_previous(iterator, &status);
icu4jni_error(env, status);
return result;
}
/**
* Reset the collation elements to their initial state.
* This will move the 'cursor' to the beginning of the text.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address of C collation element iterator to reset.
*/
static void reset(JNIEnv *env, jclass obj, jint address) {
UCollationElements *iterator = (UCollationElements *)(int)address;
ucol_reset(iterator);
}
/**
* Thread safe cloning operation
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address address of C collator to be cloned
* @return address of the new clone
* @exception thrown when error occurs while cloning
*/
static jint safeClone(JNIEnv *env, jclass obj, jint address) {
const UCollator *collator = (const UCollator *)(int)address;
UErrorCode status = U_ZERO_ERROR;
jint result;
jint buffersize = U_COL_SAFECLONE_BUFFERSIZE;
result = (jint)ucol_safeClone(collator, NULL, &buffersize, &status);
if ( icu4jni_error(env, status) != FALSE) {
return 0;
}
return result;
}
static void setAttribute(JNIEnv *env, jclass, jint address, jint type, jint value) {
UCollator *collator = (UCollator *)(int)address;
UErrorCode status = U_ZERO_ERROR;
ucol_setAttribute(collator, (UColAttribute)type, (UColAttributeValue)value, &status);
icu4jni_error(env, status);
}
/**
* Set the offset of the current source character.
* This is an offset into the text of the character to be processed.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address of the C collation element iterator to set.
* @param offset The desired character offset.
* @exception thrown when offset setting fails
*/
static void setOffset(JNIEnv *env, jclass obj, jint address,
jint offset) {
UCollationElements *iterator = (UCollationElements *)(int)address;
UErrorCode status = U_ZERO_ERROR;
ucol_setOffset(iterator, offset, &status);
icu4jni_error(env, status);
}
/**
* Set the text containing the collation elements.
* @param env JNI environment
* @param obj RuleBasedCollatorJNI object
* @param address of the C collation element iterator to be set
* @param source text containing the collation elements.
* @exception thrown when error occurs while setting offset
*/
static void setText(JNIEnv *env, jclass obj, jint address,
jstring source) {
UCollationElements *iterator = (UCollationElements *)(int)address;
UErrorCode status = U_ZERO_ERROR;
int strlength = env->GetStringLength(source);
const UChar *str = (const UChar *) env->GetStringCritical(source, 0);
ucol_setText(iterator, str, strlength, &status);
env->ReleaseStringCritical(source, str);
icu4jni_error(env, status);
}
static JNINativeMethod gMethods[] = {
/* name, signature, funcPtr */
{ "openCollator", "()I", (void*) openCollator__ },
{ "openCollator", "(Ljava/lang/String;)I", (void*) openCollator__Ljava_lang_String_2 },
{ "openCollatorFromRules", "(Ljava/lang/String;II)I", (void*) openCollatorFromRules },
{ "closeCollator", "(I)V", (void*) closeCollator },
{ "compare", "(ILjava/lang/String;Ljava/lang/String;)I", (void*) compare },
{ "getNormalization", "(I)I", (void*) getNormalization },
{ "setNormalization", "(II)V", (void*) setNormalization },
{ "getRules", "(I)Ljava/lang/String;", (void*) getRules },
{ "getSortKey", "(ILjava/lang/String;)[B", (void*) getSortKey },
{ "setAttribute", "(III)V", (void*) setAttribute },
{ "getAttribute", "(II)I", (void*) getAttribute },
{ "safeClone", "(I)I", (void*) safeClone },
{ "getCollationElementIterator", "(ILjava/lang/String;)I", (void*) getCollationElementIterator },
{ "hashCode", "(I)I", (void*) hashCode },
{ "closeElements", "(I)V", (void*) closeElements },
{ "reset", "(I)V", (void*) reset },
{ "next", "(I)I", (void*) next },
{ "previous", "(I)I", (void*) previous },
{ "getMaxExpansion", "(II)I", (void*) getMaxExpansion },
{ "setText", "(ILjava/lang/String;)V", (void*) setText },
{ "getOffset", "(I)I", (void*) getOffset },
{ "setOffset", "(II)V", (void*) setOffset }
};
int register_com_ibm_icu4jni_text_NativeCollator(JNIEnv *_env) {
return jniRegisterNativeMethods(_env, "com/ibm/icu4jni/text/NativeCollation",
gMethods, NELEM(gMethods));
}
|