]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | ******************************************************************************* | |
3 | * | |
4 | * Copyright (C) 2000-2003, International Business Machines | |
5 | * Corporation and others. All Rights Reserved. | |
6 | * | |
7 | ******************************************************************************* | |
8 | * | |
9 | * File wrtjava.c | |
10 | * | |
11 | * Modification History: | |
12 | * | |
13 | * Date Name Description | |
14 | * 01/11/02 Ram Creation. | |
15 | ******************************************************************************* | |
16 | */ | |
17 | ||
18 | #include <assert.h> | |
19 | #include "reslist.h" | |
20 | #include "unewdata.h" | |
21 | #include "unicode/ures.h" | |
22 | #include "errmsg.h" | |
23 | #include "filestrm.h" | |
24 | #include "cstring.h" | |
25 | #include "unicode/ucnv.h" | |
26 | #include "genrb.h" | |
27 | #include "rle.h" | |
28 | #include "ucol_tok.h" | |
29 | #include "uhash.h" | |
30 | #include "uresimp.h" | |
31 | #include "unicode/ustring.h" | |
32 | ||
33 | void res_write_java(struct SResource *res,UErrorCode *status); | |
34 | ||
35 | ||
36 | static const char copyRight[] = | |
37 | "/* \n" | |
38 | " *******************************************************************************\n" | |
39 | " *\n" | |
40 | " * Copyright (C) International Business Machines\n" | |
41 | " * Corporation and others. All Rights Reserved.\n" | |
42 | " *\n" | |
43 | " *******************************************************************************\n" | |
44 | " * $" "Source: $ \n" | |
45 | " * $" "Date: $ \n" | |
46 | " * $" "Revision: $ \n" | |
47 | " *******************************************************************************\n" | |
48 | " */\n\n"; | |
49 | static const char warningMsg[] = | |
50 | "/*********************************************************************\n" | |
51 | "######################################################################\n" | |
52 | "\n" | |
53 | " WARNING: This file is generated by genrb Version " GENRB_VERSION ".\n" | |
54 | " If you edit this file, please make sure that, the source\n" | |
55 | " of this file (XXXX.txt in LocaleElements_XXXX.java)\n" | |
56 | " is also edited.\n" | |
57 | "######################################################################\n" | |
58 | " *********************************************************************\n" | |
59 | " */\n\n"; | |
60 | static const char* openBrace="{\n"; | |
61 | static const char* closeClass=" };\n" | |
62 | "}\n"; | |
63 | ||
64 | static const char* javaClass = "import java.util.ListResourceBundle;\n" | |
65 | "import com.ibm.icu.impl.ICUListResourceBundle;\n\n" | |
66 | "public class "; | |
67 | ||
68 | static const char* javaClass1= " extends ICUListResourceBundle {\n\n" | |
69 | " /**\n" | |
70 | " * Overrides ListResourceBundle \n" | |
71 | " */\n" | |
72 | " public final Object[][] getContents() { \n" | |
73 | " return contents;\n" | |
74 | " }\n" | |
75 | " private static Object[][] contents = {\n"; | |
76 | static const char* javaClassICU= " extends ICUListResourceBundle {\n\n" | |
77 | " public %s () {\n" | |
78 | " super.contents = data;\n" | |
79 | " }\n" | |
80 | " static final Object[][] data = new Object[][] { \n"; | |
81 | static int tabCount = 3; | |
82 | ||
83 | static FileStream* out=NULL; | |
84 | static struct SRBRoot* srBundle ; | |
85 | static const char* outDir = NULL; | |
86 | ||
87 | static const char* bName=NULL; | |
88 | static const char* pName=NULL; | |
89 | ||
90 | static void write_tabs(FileStream* os){ | |
91 | int i=0; | |
92 | for(;i<=tabCount;i++){ | |
93 | T_FileStream_write(os," ",4); | |
94 | } | |
95 | } | |
96 | ||
97 | #define ZERO 0x30 | |
98 | ||
99 | static const char* enc =""; | |
100 | static UConverter* conv = NULL; | |
101 | static char NUMBER = ZERO; | |
102 | ||
103 | static int32_t | |
104 | uCharsToChars( char* target,int32_t targetLen, UChar* source, int32_t sourceLen,UErrorCode* status){ | |
105 | int i=0, j=0; | |
106 | char str[30]={'\0'}; | |
107 | while(i<sourceLen){ | |
108 | if (source[i] == '\n') { | |
109 | if (j + 2 < targetLen) { | |
110 | uprv_strcat(target, "\\n"); | |
111 | } | |
112 | j += 2; | |
113 | }else if(source[i]==0x0D){ | |
114 | if(j+2<targetLen){ | |
115 | uprv_strcat(target,"\\f"); | |
116 | } | |
117 | j+=2; | |
118 | }else if(source[i] == '"'){ | |
119 | if(source[i-1]=='\''){ | |
120 | if(j+2<targetLen){ | |
121 | uprv_strcat(target,"\\"); | |
122 | target[j+1]= (char)source[i]; | |
123 | } | |
124 | j+=2; | |
125 | }else if(source[i-1]!='\\'){ | |
126 | ||
127 | if(j+2<targetLen){ | |
128 | uprv_strcat(target,"\\"); | |
129 | target[j+1]= (char)source[i]; | |
130 | } | |
131 | j+=2; | |
132 | }else if(source[i-1]=='\\'){ | |
133 | target[j++]= (char)source[i]; | |
134 | } | |
135 | }else if(source[i]=='\\'){ | |
136 | if(i+1<sourceLen){ | |
137 | switch(source[i+1]){ | |
138 | case ',': | |
139 | case '!': | |
140 | case '?': | |
141 | case '#': | |
142 | case '.': | |
143 | case '%': | |
144 | case '&': | |
145 | case ':': | |
146 | case ';': | |
147 | if(j+2<targetLen){ | |
148 | uprv_strcat(target,"\\\\"); | |
149 | } | |
150 | j+=2; | |
151 | break; | |
152 | case '"': | |
153 | case '\'': | |
154 | if(j+3<targetLen){ | |
155 | uprv_strcat(target,"\\\\\\"); | |
156 | } | |
157 | j+=3; | |
158 | break; | |
159 | default : | |
160 | if(j<targetLen){ | |
161 | target[j]=(char)source[i]; | |
162 | } | |
163 | j++; | |
164 | break; | |
165 | } | |
166 | }else{ | |
167 | if(j<targetLen){ | |
168 | uprv_strcat(target,"\\\\"); | |
169 | } | |
170 | j+=2; | |
171 | } | |
172 | }else if(source[i]>=0x20 && source[i]<0x7F/*ASCII*/){ | |
173 | if(j<targetLen){ | |
174 | target[j] = (char) source[i]; | |
175 | } | |
176 | j++; | |
177 | }else{ | |
178 | if(*enc =='\0' || source[i]==0x0000){ | |
179 | uprv_strcpy(str,"\\u"); | |
180 | itostr(str+2,source[i],16,4); | |
181 | if(j+6<targetLen){ | |
182 | uprv_strcat(target,str); | |
183 | } | |
184 | j+=6; | |
185 | }else{ | |
186 | char dest[30] = {0}; | |
187 | int retVal=ucnv_fromUChars(conv,dest,30,source+i,1,status); | |
188 | if(U_FAILURE(*status)){ | |
189 | return 0; | |
190 | } | |
191 | if(j+retVal<targetLen){ | |
192 | uprv_strcat(target,dest); | |
193 | } | |
194 | j+=retVal; | |
195 | } | |
196 | } | |
197 | i++; | |
198 | } | |
199 | return j; | |
200 | } | |
201 | ||
202 | ||
203 | static uint32_t | |
204 | strrch(const char* source,uint32_t sourceLen,char find){ | |
205 | const char* tSourceEnd =source + (sourceLen-1); | |
206 | while(tSourceEnd>= source){ | |
207 | if(*tSourceEnd==find){ | |
208 | return (uint32_t)(tSourceEnd-source); | |
209 | } | |
210 | tSourceEnd--; | |
211 | } | |
212 | return (uint32_t)(tSourceEnd-source); | |
213 | } | |
214 | ||
215 | static int32_t getColumnCount(int32_t len){ | |
216 | int32_t columnCount = 80; | |
217 | int32_t maxLines = 3000; | |
218 | int32_t adjustedLen = len*5; /* assume that every codepoint is represented in \uXXXX format*/ | |
219 | /* | |
220 | * calculate the number of lines that | |
221 | * may be required if column count is 80 | |
222 | */ | |
223 | if (maxLines < (adjustedLen / columnCount) ){ | |
224 | columnCount = adjustedLen / maxLines; | |
225 | } | |
226 | return columnCount; | |
227 | } | |
228 | static void | |
229 | str_write_java( uint16_t* src, int32_t srcLen, UBool printEndLine, UErrorCode *status){ | |
230 | ||
231 | uint32_t length = srcLen*8; | |
232 | uint32_t bufLen = 0; | |
233 | char* buf = (char*) malloc(sizeof(char)*length); | |
234 | ||
235 | uint32_t columnCount = getColumnCount(srcLen); | |
236 | ||
237 | /* test for NULL */ | |
238 | if(buf == NULL) { | |
239 | *status = U_MEMORY_ALLOCATION_ERROR; | |
240 | return; | |
241 | } | |
242 | ||
243 | memset(buf,0,length); | |
244 | ||
245 | bufLen = uCharsToChars(buf,length,src,srcLen,status); | |
246 | ||
247 | if(printEndLine) write_tabs(out); | |
248 | ||
249 | if(U_FAILURE(*status)){ | |
250 | return; | |
251 | } | |
252 | ||
253 | if(bufLen+(tabCount*4) > columnCount ){ | |
254 | uint32_t len = 0; | |
255 | char* current = buf; | |
256 | uint32_t add; | |
257 | while(len < bufLen){ | |
258 | add = columnCount-(tabCount*4)-5/* for ", +\n */; | |
259 | current = buf +len; | |
260 | if (add < (bufLen-len)) { | |
261 | uint32_t index = strrch(current,add,'\\'); | |
262 | if (index > add) { | |
263 | index = add; | |
264 | } else { | |
265 | int32_t num =index-1; | |
266 | uint32_t seqLen; | |
267 | while(num>0){ | |
268 | if(current[num]=='\\'){ | |
269 | num--; | |
270 | }else{ | |
271 | break; | |
272 | } | |
273 | } | |
274 | if ((index-num)%2==0) { | |
275 | index--; | |
276 | } | |
277 | seqLen = (current[index+1]=='u') ? 6 : 2; | |
278 | if ((add-index) < seqLen) { | |
279 | add = index + seqLen; | |
280 | } | |
281 | } | |
282 | } | |
283 | T_FileStream_write(out,"\"",1); | |
284 | if(len+add<bufLen){ | |
285 | T_FileStream_write(out,current,add); | |
286 | T_FileStream_write(out,"\" +\n",4); | |
287 | write_tabs(out); | |
288 | }else{ | |
289 | T_FileStream_write(out,current,bufLen-len); | |
290 | } | |
291 | len+=add; | |
292 | } | |
293 | }else{ | |
294 | T_FileStream_write(out,"\"",1); | |
295 | T_FileStream_write(out, buf,bufLen); | |
296 | } | |
297 | if(printEndLine){ | |
298 | T_FileStream_write(out,"\",\n",3); | |
299 | }else{ | |
300 | T_FileStream_write(out,"\"",1); | |
301 | } | |
302 | } | |
303 | ||
304 | static void | |
305 | write_utf8_file(struct SResource *res, const char *file, UErrorCode *status){ | |
306 | char fileName[1024] ={0}; | |
307 | FileStream* datFile = NULL; | |
308 | const char* type = "new ICUListResourceBundle.ResourceString("; | |
309 | char* dest = (char*) uprv_malloc( 8 * res->u.fString.fLength); | |
310 | int32_t len = 0; | |
311 | if(outDir){ | |
312 | uprv_strcat(fileName,outDir); | |
313 | if(outDir[uprv_strlen(outDir)-1]!=U_FILE_SEP_CHAR){ | |
314 | uprv_strcat(fileName,U_FILE_SEP_STRING); | |
315 | } | |
316 | } | |
317 | uprv_strcat(fileName,file);/* UCARULES.utf8 UTF-8 file */ | |
318 | ||
319 | write_tabs(out); | |
320 | ||
321 | T_FileStream_write(out, type, (int32_t)uprv_strlen(type)); | |
322 | T_FileStream_write(out, "\"", 1); | |
323 | T_FileStream_write(out, file, (int32_t)uprv_strlen(file)); | |
324 | T_FileStream_write(out, "\")\n", 3); | |
325 | datFile=T_FileStream_open(fileName,"w"); | |
326 | ||
327 | if(!dest){ | |
328 | *status=U_MEMORY_ALLOCATION_ERROR; | |
329 | } | |
330 | ||
331 | u_strToUTF8(dest,8*res->u.fString.fLength,&len,res->u.fString.fChars,res->u.fString.fLength,status); | |
332 | if(U_FAILURE(*status)){ | |
333 | T_FileStream_close(datFile); | |
334 | uprv_free(dest); | |
335 | return; | |
336 | } | |
337 | T_FileStream_write(datFile,dest,len); | |
338 | T_FileStream_close(datFile); | |
339 | uprv_free(dest); | |
340 | } | |
341 | #define MAX_SEQUENCE_LENGTH 30000 | |
342 | /* Writing Functions */ | |
343 | static void | |
344 | string_write_java(struct SResource *res,UErrorCode *status) { | |
345 | if(res->fKey > 0 && uprv_strcmp(srBundle->fKeys+res->fKey,"%%UCARULES")==0 ){ | |
346 | ||
347 | const char* file = "UCARules.utf8"; | |
348 | write_utf8_file(res, file, status); | |
349 | }else if(res->fKey > 0 && uprv_strcmp(srBundle->fKeys+res->fKey,"Sequence")==0 | |
350 | && res->fType == RES_STRING | |
351 | && res->u.fString.fLength > MAX_SEQUENCE_LENGTH){ | |
352 | char file[1024] = {0}; | |
353 | uprv_strcpy(file, "CollationSequence_"); | |
354 | uprv_strcat(file, srBundle->fLocale); | |
355 | uprv_strcat(file, ".utf8"); | |
356 | write_utf8_file(res, file, status); | |
357 | ||
358 | }else{ | |
359 | str_write_java(res->u.fString.fChars,res->u.fString.fLength,TRUE,status); | |
360 | ||
361 | if(res->fKey > 0 && uprv_strcmp(srBundle->fKeys+res->fKey,"Rule")==0){ | |
362 | UChar* buf = (UChar*) uprv_malloc(sizeof(UChar)*res->u.fString.fLength); | |
363 | uprv_memcpy(buf,res->u.fString.fChars,res->u.fString.fLength); | |
364 | uprv_free(buf); | |
365 | } | |
366 | } | |
367 | ||
368 | } | |
369 | ||
370 | static void | |
371 | alias_write_java(struct SResource *res,UErrorCode *status) { | |
372 | static const char str[] = "new ICUListResourceBundle.Alias("; | |
373 | write_tabs(out); | |
374 | T_FileStream_write(out,str,(int32_t)uprv_strlen(str)); | |
375 | ||
376 | /*str_write_java(res->u.fString.fChars,res->u.fString.fLength,FALSE,status);*/ | |
377 | /*if(*res->u.fString.fChars == RES_PATH_SEPARATOR) {*/ | |
378 | /* there is a path included | |
379 | locale = u_strchr(res->u.fString.fChars +1, RES_PATH_SEPARATOR); | |
380 | *locale = 0; | |
381 | locale++; | |
382 | ||
383 | T_FileStream_write(out,"\"/",2); | |
384 | T_FileStream_write(out,apName,(int32_t)uprv_strlen(apName)); | |
385 | T_FileStream_write(out,"/",1); | |
386 | T_FileStream_write(out,abName,(int32_t)uprv_strlen(abName)); | |
387 | T_FileStream_write(out,"/\"+",3); | |
388 | str_write_java(locale,res->u.fString.fLength-(locale-res->u.fString.fChars),FALSE,status); | |
389 | } else { | |
390 | str_write_java(res->u.fString.fChars,res->u.fString.fLength,FALSE,status); | |
391 | }*/ | |
392 | ||
393 | str_write_java(res->u.fString.fChars,res->u.fString.fLength,FALSE,status); | |
394 | ||
395 | T_FileStream_write(out,"),\n",3); | |
396 | } | |
397 | ||
398 | static void | |
399 | array_write_java( struct SResource *res, UErrorCode *status) { | |
400 | ||
401 | uint32_t i = 0; | |
402 | const char* arr ="new String[] { \n"; | |
403 | struct SResource *current = NULL; | |
404 | struct SResource *first =NULL; | |
405 | UBool decrementTabs = FALSE; | |
406 | UBool allStrings = TRUE; | |
407 | ||
408 | if (U_FAILURE(*status)) { | |
409 | return; | |
410 | } | |
411 | ||
412 | if (res->u.fArray.fCount > 0) { | |
413 | ||
414 | current = res->u.fArray.fFirst; | |
415 | i = 0; | |
416 | while(current != NULL){ | |
417 | if(current->fType!=URES_STRING){ | |
418 | allStrings = FALSE; | |
419 | break; | |
420 | } | |
421 | current= current->fNext; | |
422 | } | |
423 | ||
424 | current = res->u.fArray.fFirst; | |
425 | if(allStrings==FALSE){ | |
426 | const char* object = "new Object[]{\n"; | |
427 | write_tabs(out); | |
428 | T_FileStream_write(out, object, (int32_t)uprv_strlen(object)); | |
429 | tabCount++; | |
430 | decrementTabs = TRUE; | |
431 | }else{ | |
432 | write_tabs(out); | |
433 | T_FileStream_write(out, arr, (int32_t)uprv_strlen(arr)); | |
434 | tabCount++; | |
435 | } | |
436 | first=current; | |
437 | while (current != NULL) { | |
438 | /*if(current->fType==URES_STRING){ | |
439 | write_tabs(out); | |
440 | }*/ | |
441 | res_write_java(current, status); | |
442 | if(U_FAILURE(*status)){ | |
443 | return; | |
444 | } | |
445 | i++; | |
446 | current = current->fNext; | |
447 | } | |
448 | T_FileStream_write(out,"\n",1); | |
449 | ||
450 | tabCount--; | |
451 | write_tabs(out); | |
452 | T_FileStream_write(out,"},\n",3); | |
453 | ||
454 | } else { | |
455 | write_tabs(out); | |
456 | T_FileStream_write(out,arr,(int32_t)uprv_strlen(arr)); | |
457 | write_tabs(out); | |
458 | T_FileStream_write(out,"},\n",3); | |
459 | } | |
460 | } | |
461 | ||
462 | static void | |
463 | intvector_write_java( struct SResource *res, UErrorCode *status) { | |
464 | uint32_t i = 0; | |
465 | const char* intArr = "new Integer[] {\n"; | |
466 | const char* intC = "new Integer("; | |
467 | const char* stringArr = "new String[]{\n"; | |
468 | char buf[100]; | |
469 | int len =0; | |
470 | buf[0]=0; | |
471 | write_tabs(out); | |
472 | ||
473 | if(res->fKey > 0 && uprv_strcmp(srBundle->fKeys+res->fKey,"DateTimeElements")==0){ | |
474 | T_FileStream_write(out, stringArr, (int32_t)uprv_strlen(stringArr)); | |
475 | tabCount++; | |
476 | for(i = 0; i<res->u.fIntVector.fCount; i++) { | |
477 | write_tabs(out); | |
478 | len=itostr(buf,res->u.fIntVector.fArray[i],10,0); | |
479 | T_FileStream_write(out,"\"",1); | |
480 | T_FileStream_write(out,buf,len); | |
481 | T_FileStream_write(out,"\",",2); | |
482 | T_FileStream_write(out,"\n",1); | |
483 | } | |
484 | }else{ | |
485 | T_FileStream_write(out, intArr, (int32_t)uprv_strlen(intArr)); | |
486 | tabCount++; | |
487 | for(i = 0; i<res->u.fIntVector.fCount; i++) { | |
488 | write_tabs(out); | |
489 | T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC)); | |
490 | len=itostr(buf,res->u.fIntVector.fArray[i],10,0); | |
491 | T_FileStream_write(out,buf,len); | |
492 | T_FileStream_write(out,"),",2); | |
493 | T_FileStream_write(out,"\n",1); | |
494 | } | |
495 | } | |
496 | tabCount--; | |
497 | write_tabs(out); | |
498 | T_FileStream_write(out,"},\n",3); | |
499 | } | |
500 | ||
501 | static void | |
502 | int_write_java(struct SResource *res,UErrorCode *status) { | |
503 | const char* intC = "new Integer("; | |
504 | char buf[100]; | |
505 | int len =0; | |
506 | buf[0]=0; | |
507 | ||
508 | /* write the binary data */ | |
509 | write_tabs(out); | |
510 | T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC)); | |
511 | len=itostr(buf, res->u.fIntValue.fValue, 10, 0); | |
512 | T_FileStream_write(out,buf,len); | |
513 | T_FileStream_write(out,"),\n",3 ); | |
514 | ||
515 | } | |
516 | ||
517 | static void | |
518 | bin_write_java( struct SResource *res, UErrorCode *status) { | |
519 | const char* type = "new ICUListResourceBundle.CompressedBinary("; | |
520 | const char* ext; | |
521 | int32_t srcLen=res->u.fBinaryValue.fLength; | |
522 | ||
523 | if(srcLen>0 ){ | |
524 | uint16_t* target=NULL; | |
525 | uint16_t* saveTarget = NULL; | |
526 | int32_t tgtLen = 0; | |
527 | ||
528 | if(res->fKey > 0 && (uprv_strcmp(srBundle->fKeys+res->fKey,"%%CollationBin")==0 || uprv_strcmp(srBundle->fKeys+res->fKey,"BreakDictionaryData")==0)){ | |
529 | char fileName[1024] ={0}; | |
530 | char fn[1024] = {0}; | |
531 | FileStream* datFile = NULL; | |
532 | if(uprv_strcmp(srBundle->fKeys+res->fKey,"BreakDictionaryData")==0){ | |
533 | uprv_strcat(fileName,"BreakDictionaryData"); | |
534 | ext = ".brk"; | |
535 | }else{ | |
536 | uprv_strcat(fileName,"CollationElements"); | |
537 | ext=".col"; | |
538 | } | |
539 | if(uprv_strcmp(srBundle->fLocale,"root")!=0){ | |
540 | uprv_strcat(fileName,"_"); | |
541 | uprv_strcat(fileName,srBundle->fLocale); | |
542 | if(NUMBER > ZERO){ | |
543 | uprv_strcat(fileName, "_"); | |
544 | uprv_strcat(fileName, &NUMBER); | |
545 | } | |
546 | NUMBER++; | |
547 | } | |
548 | ||
549 | uprv_strcat(fileName,ext); | |
550 | if(outDir ){ | |
551 | uprv_strcat(fn,outDir); | |
552 | if(outDir[uprv_strlen(outDir)-1]!=U_FILE_SEP_CHAR){ | |
553 | uprv_strcat(fn,U_FILE_SEP_STRING); | |
554 | } | |
555 | } | |
556 | uprv_strcat(fn,fileName); | |
557 | type = "new ICUListResourceBundle.ResourceBinary("; | |
558 | write_tabs(out); | |
559 | T_FileStream_write(out, type, (int32_t)uprv_strlen(type)); | |
560 | T_FileStream_write(out, "\"", 1); | |
561 | T_FileStream_write(out, fileName, (int32_t)uprv_strlen(fileName)); | |
562 | T_FileStream_write(out, "\"),\n", 4); | |
563 | ||
564 | datFile=T_FileStream_open(fn,"w"); | |
565 | T_FileStream_write(datFile, res->u.fBinaryValue.fData, res->u.fBinaryValue.fLength); | |
566 | T_FileStream_close(datFile); | |
567 | ||
568 | }else{ | |
569 | ||
570 | srcLen = res->u.fBinaryValue.fLength; | |
571 | tgtLen = srcLen * 2; | |
572 | target = (uint16_t*)malloc(sizeof(uint16_t) * tgtLen); | |
573 | saveTarget = target; | |
574 | if(target){ | |
575 | tgtLen = byteArrayToRLEString(res->u.fBinaryValue.fData, | |
576 | srcLen,target, tgtLen,status); | |
577 | if(U_FAILURE(*status)){ | |
578 | printf("Could not encode got error : %s \n", u_errorName(*status)); | |
579 | return; | |
580 | } | |
581 | #if DEBUG | |
582 | /***************** Test Roundtripping *********************/ | |
583 | { | |
584 | int32_t myTargetLen = rleStringToByteArray(target,tgtLen,NULL,0,status); | |
585 | uint8_t* myTarget = (uint8_t*) malloc(sizeof(uint8_t) * myTargetLen); | |
586 | ||
587 | /* test for NULL */ | |
588 | if(myTarget == NULL) { | |
589 | *status = U_MEMORY_ALLOCATION_ERROR; | |
590 | return; | |
591 | } | |
592 | ||
593 | int i=0; | |
594 | int32_t retVal=0; | |
595 | ||
596 | *status = U_ZERO_ERROR; | |
597 | retVal=rleStringToByteArray(target,tgtLen,myTarget,myTargetLen,status); | |
598 | if(U_SUCCESS(*status)){ | |
599 | ||
600 | for(i=0; i< srcLen;i++){ | |
601 | if(res->u.fBinaryValue.fData[i]!= myTarget[i]){ | |
602 | printf("the encoded string cannot be decoded Expected : 0x%02X Got : %: 0x%02X at %i\n",res->u.fBinaryValue.fData[i],myTarget[i], i); | |
603 | } | |
604 | } | |
605 | }else{ | |
606 | printf("Could not decode got error : %s \n", u_errorName(*status)); | |
607 | } | |
608 | free(myTarget); | |
609 | ||
610 | } | |
611 | #endif | |
612 | ||
613 | }else{ | |
614 | *status = U_MEMORY_ALLOCATION_ERROR; | |
615 | return; | |
616 | } | |
617 | ||
618 | write_tabs(out); | |
619 | T_FileStream_write(out, type, (int32_t)uprv_strlen(type)); | |
620 | T_FileStream_write(out, "\n", 1); | |
621 | tabCount++; | |
622 | write_tabs(out); | |
623 | str_write_java(target, tgtLen,FALSE, status); | |
624 | tabCount--; | |
625 | T_FileStream_write(out, "),\n", 3); | |
626 | ||
627 | free(target); | |
628 | ||
629 | } | |
630 | ||
631 | }else{ | |
632 | write_tabs(out); | |
633 | T_FileStream_write(out,type,(int32_t)uprv_strlen(type)); | |
634 | T_FileStream_write(out,"null),\n",7); | |
635 | } | |
636 | ||
637 | } | |
638 | ||
639 | ||
640 | static UBool start = TRUE; | |
641 | ||
642 | static void | |
643 | table_write_java(struct SResource *res, UErrorCode *status) { | |
644 | uint32_t i = 0; | |
645 | UBool allStrings =TRUE; | |
646 | struct SResource *current = NULL; | |
647 | struct SResource *save = NULL; | |
648 | const char* obj = "new Object[][]{\n"; | |
649 | ||
650 | if (U_FAILURE(*status)) { | |
651 | return ; | |
652 | } | |
653 | ||
654 | if (res->u.fTable.fCount > 0) { | |
655 | if(start==FALSE){ | |
656 | write_tabs(out); | |
657 | T_FileStream_write(out, obj, (int32_t)uprv_strlen(obj)); | |
658 | tabCount++; | |
659 | } | |
660 | start = FALSE; | |
661 | save = current = res->u.fTable.fFirst; | |
662 | i = 0; | |
663 | ||
664 | ||
665 | while (current != NULL) { | |
666 | assert(i < res->u.fTable.fCount); | |
667 | write_tabs(out); | |
668 | ||
669 | T_FileStream_write(out, openBrace, 2); | |
670 | ||
671 | ||
672 | tabCount++; | |
673 | allStrings=FALSE; | |
674 | ||
675 | write_tabs(out); | |
676 | if(current->fKey > 0){ | |
677 | T_FileStream_write(out, "\"", 1); | |
678 | T_FileStream_write(out, srBundle->fKeys+current->fKey, | |
679 | (int32_t)uprv_strlen(srBundle->fKeys+current->fKey)); | |
680 | T_FileStream_write(out, "\",\n", 2); | |
681 | ||
682 | T_FileStream_write(out, "\n", 1); | |
683 | } | |
684 | res_write_java(current, status); | |
685 | if(U_FAILURE(*status)){ | |
686 | return; | |
687 | } | |
688 | i++; | |
689 | current = current->fNext; | |
690 | tabCount--; | |
691 | write_tabs(out); | |
692 | T_FileStream_write(out, "},\n", 3); | |
693 | } | |
694 | if(tabCount>4){ | |
695 | tabCount--; | |
696 | write_tabs(out); | |
697 | T_FileStream_write(out, "},\n", 3); | |
698 | } | |
699 | ||
700 | } else { | |
701 | write_tabs(out); | |
702 | T_FileStream_write(out,obj,(int32_t)uprv_strlen(obj)); | |
703 | ||
704 | write_tabs(out); | |
705 | T_FileStream_write(out,"},\n",3); | |
706 | ||
707 | } | |
708 | ||
709 | } | |
710 | ||
711 | void | |
712 | res_write_java(struct SResource *res,UErrorCode *status) { | |
713 | ||
714 | if (U_FAILURE(*status)) { | |
715 | return ; | |
716 | } | |
717 | ||
718 | if (res != NULL) { | |
719 | switch (res->fType) { | |
720 | case URES_STRING: | |
721 | string_write_java (res, status); | |
722 | return; | |
723 | case URES_ALIAS: | |
724 | alias_write_java (res, status); | |
725 | return; | |
726 | case URES_INT_VECTOR: | |
727 | intvector_write_java (res, status); | |
728 | return; | |
729 | case URES_BINARY: | |
730 | bin_write_java (res, status); | |
731 | return; | |
732 | case URES_INT: | |
733 | int_write_java (res, status); | |
734 | return; | |
735 | case URES_ARRAY: | |
736 | array_write_java (res, status); | |
737 | return; | |
738 | case URES_TABLE: | |
739 | case URES_TABLE32: | |
740 | table_write_java (res, status); | |
741 | return; | |
742 | ||
743 | default: | |
744 | break; | |
745 | } | |
746 | } | |
747 | ||
748 | *status = U_INTERNAL_PROGRAM_ERROR; | |
749 | } | |
750 | ||
751 | void | |
752 | bundle_write_java(struct SRBRoot *bundle, const char *outputDir,const char* outputEnc, | |
753 | char *writtenFilename, int writtenFilenameLen, | |
754 | const char* packageName, const char* bundleName, | |
755 | UErrorCode *status) { | |
756 | ||
757 | char fileName[256] = {'\0'}; | |
758 | char className[256]={'\0'}; | |
759 | char constructor[1000] = { 0 }; | |
760 | UBool j1 =FALSE; | |
761 | outDir = outputDir; | |
762 | ||
763 | bName = (bundleName==NULL) ? "LocaleElements" : bundleName; | |
764 | pName = (packageName==NULL)? "com.ibm.icu.impl.data" : packageName; | |
765 | ||
766 | uprv_strcpy(className, bName); | |
767 | srBundle = bundle; | |
768 | if(uprv_strcmp(srBundle->fLocale,"root")!=0){ | |
769 | uprv_strcat(className,"_"); | |
770 | uprv_strcat(className,srBundle->fLocale); | |
771 | } | |
772 | if(outputDir){ | |
773 | uprv_strcpy(fileName, outputDir); | |
774 | if(outputDir[uprv_strlen(outputDir)-1] !=U_FILE_SEP_CHAR){ | |
775 | uprv_strcat(fileName,U_FILE_SEP_STRING); | |
776 | } | |
777 | uprv_strcat(fileName,className); | |
778 | uprv_strcat(fileName,".java"); | |
779 | }else{ | |
780 | uprv_strcat(fileName,className); | |
781 | uprv_strcat(fileName,".java"); | |
782 | } | |
783 | ||
784 | if (writtenFilename) { | |
785 | uprv_strncpy(writtenFilename, fileName, writtenFilenameLen); | |
786 | } | |
787 | ||
788 | if (U_FAILURE(*status)) { | |
789 | return; | |
790 | } | |
791 | ||
792 | out= T_FileStream_open(fileName,"w"); | |
793 | ||
794 | if(out==NULL){ | |
795 | *status = U_FILE_ACCESS_ERROR; | |
796 | return; | |
797 | } | |
798 | if(getIncludeCopyright()){ | |
799 | T_FileStream_write(out, copyRight, (int32_t)uprv_strlen(copyRight)); | |
800 | T_FileStream_write(out, warningMsg, (int32_t)uprv_strlen(warningMsg)); | |
801 | } | |
802 | T_FileStream_write(out,"package ",(int32_t)uprv_strlen("package ")); | |
803 | T_FileStream_write(out,pName,(int32_t)uprv_strlen(pName)); | |
804 | T_FileStream_write(out,";\n\n",3); | |
805 | T_FileStream_write(out, javaClass, (int32_t)uprv_strlen(javaClass)); | |
806 | T_FileStream_write(out, className, (int32_t)uprv_strlen(className)); | |
807 | if(j1){ | |
808 | T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1)); | |
809 | }else{ | |
810 | sprintf(constructor,javaClassICU,className); | |
811 | T_FileStream_write(out, constructor, (int32_t)uprv_strlen(constructor)); | |
812 | } | |
813 | ||
814 | if(outputEnc && *outputEnc!='\0'){ | |
815 | /* store the output encoding */ | |
816 | enc = outputEnc; | |
817 | conv=ucnv_open(enc,status); | |
818 | if(U_FAILURE(*status)){ | |
819 | return; | |
820 | } | |
821 | } | |
822 | res_write_java(bundle->fRoot, status); | |
823 | ||
824 | T_FileStream_write(out, closeClass, (int32_t)uprv_strlen(closeClass)); | |
825 | ||
826 | T_FileStream_close(out); | |
827 | ||
828 | ucnv_close(conv); | |
829 | } |