• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 *
4 *   Copyright (C) 2000-2006, 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 
write_tabs(FileStream * os)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
uCharsToChars(char * target,int32_t targetLen,UChar * source,int32_t sourceLen,UErrorCode * status)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
strrch(const char * source,uint32_t sourceLen,char find)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 
getColumnCount(int32_t len)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
str_write_java(uint16_t * src,int32_t srcLen,UBool printEndLine,UErrorCode * status)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     uint32_t columnCount;
234     char* buf = (char*) malloc(sizeof(char)*length);
235 
236     if(buf == NULL) {
237         *status = U_MEMORY_ALLOCATION_ERROR;
238         return;
239     }
240 
241     columnCount = getColumnCount(srcLen);
242     memset(buf,0,length);
243 
244     bufLen = uCharsToChars(buf,length,src,srcLen,status);
245 
246     if(printEndLine)
247         write_tabs(out);
248 
249     if(U_FAILURE(*status)){
250         uprv_free(buf);
251         return;
252     }
253 
254     if(bufLen+(tabCount*4) > columnCount  ){
255         uint32_t len = 0;
256         char* current = buf;
257         uint32_t add;
258         while(len < bufLen){
259             add = columnCount-(tabCount*4)-5/* for ", +\n */;
260             current = buf +len;
261             if (add < (bufLen-len)) {
262                 uint32_t index = strrch(current,add,'\\');
263                 if (index > add) {
264                     index = add;
265                 } else {
266                     int32_t num =index-1;
267                     uint32_t seqLen;
268                     while(num>0){
269                         if(current[num]=='\\'){
270                             num--;
271                         }else{
272                             break;
273                         }
274                     }
275                     if ((index-num)%2==0) {
276                         index--;
277                     }
278                     seqLen = (current[index+1]=='u') ? 6 : 2;
279                     if ((add-index) < seqLen) {
280                         add = index + seqLen;
281                     }
282                 }
283             }
284             T_FileStream_write(out,"\"",1);
285             if(len+add<bufLen){
286                 T_FileStream_write(out,current,add);
287                 T_FileStream_write(out,"\" +\n",4);
288                 write_tabs(out);
289             }else{
290                 T_FileStream_write(out,current,bufLen-len);
291             }
292             len+=add;
293         }
294     }else{
295         T_FileStream_write(out,"\"",1);
296         T_FileStream_write(out, buf,bufLen);
297     }
298     if(printEndLine){
299         T_FileStream_write(out,"\",\n",3);
300     }else{
301         T_FileStream_write(out,"\"",1);
302     }
303     uprv_free(buf);
304 }
305 
306 static void
write_utf8_file(struct SResource * res,const char * file,UErrorCode * status)307 write_utf8_file(struct SResource *res, const char *file, UErrorCode *status){
308         char fileName[1024] ={0};
309         FileStream* datFile = NULL;
310         const char* type = "new ICUListResourceBundle.ResourceString(";
311         char* dest  = (char*) uprv_malloc( 8 * res->u.fString.fLength);
312         int32_t len = 0;
313         if(outDir){
314             uprv_strcat(fileName,outDir);
315             if(outDir[uprv_strlen(outDir)-1]!=U_FILE_SEP_CHAR){
316                 uprv_strcat(fileName,U_FILE_SEP_STRING);
317             }
318         }
319         uprv_strcat(fileName,file);/* UCARULES.utf8 UTF-8 file */
320 
321         write_tabs(out);
322 
323         T_FileStream_write(out, type, (int32_t)uprv_strlen(type));
324         T_FileStream_write(out, "\"", 1);
325         T_FileStream_write(out, file, (int32_t)uprv_strlen(file));
326         T_FileStream_write(out, "\")\n", 3);
327         datFile=T_FileStream_open(fileName,"w");
328 
329         if(!dest){
330             *status=U_MEMORY_ALLOCATION_ERROR;
331         }
332 
333         u_strToUTF8(dest,8*res->u.fString.fLength,&len,res->u.fString.fChars,res->u.fString.fLength,status);
334         if(U_FAILURE(*status)){
335             T_FileStream_close(datFile);
336             uprv_free(dest);
337             return;
338         }
339         T_FileStream_write(datFile,dest,len);
340         T_FileStream_close(datFile);
341         uprv_free(dest);
342 }
343 #define MAX_SEQUENCE_LENGTH 30000
344 /* Writing Functions */
345 static void
string_write_java(struct SResource * res,UErrorCode * status)346 string_write_java(struct SResource *res,UErrorCode *status) {
347     if(res->fKey > 0 && uprv_strcmp(srBundle->fKeys+res->fKey,"%%UCARULES")==0 ){
348 
349         const char* file = "UCARules.utf8";
350         write_utf8_file(res, file, status);
351     }else if(res->fKey > 0 && uprv_strcmp(srBundle->fKeys+res->fKey,"Sequence")==0
352              && res->fType == RES_STRING
353              && res->u.fString.fLength > MAX_SEQUENCE_LENGTH){
354         char file[1024] = {0};
355         uprv_strcpy(file, "CollationSequence_");
356         uprv_strcat(file, srBundle->fLocale);
357         uprv_strcat(file, ".utf8");
358         write_utf8_file(res, file, status);
359 
360     }else{
361         str_write_java(res->u.fString.fChars,res->u.fString.fLength,TRUE,status);
362 
363         if(res->fKey > 0 && uprv_strcmp(srBundle->fKeys+res->fKey,"Rule")==0){
364             UChar* buf = (UChar*) uprv_malloc(sizeof(UChar)*res->u.fString.fLength);
365             uprv_memcpy(buf,res->u.fString.fChars,res->u.fString.fLength);
366             uprv_free(buf);
367         }
368     }
369 
370 }
371 
372 static void
alias_write_java(struct SResource * res,UErrorCode * status)373 alias_write_java(struct SResource *res,UErrorCode *status) {
374     static const char str[] = "new ICUListResourceBundle.Alias(";
375     write_tabs(out);
376     T_FileStream_write(out,str,(int32_t)uprv_strlen(str));
377 
378     /*str_write_java(res->u.fString.fChars,res->u.fString.fLength,FALSE,status);*/
379     /*if(*res->u.fString.fChars == RES_PATH_SEPARATOR) {*/
380         /* there is a path included
381         locale = u_strchr(res->u.fString.fChars +1, RES_PATH_SEPARATOR);
382         *locale = 0;
383         locale++;
384 
385         T_FileStream_write(out,"\"/",2);
386         T_FileStream_write(out,apName,(int32_t)uprv_strlen(apName));
387         T_FileStream_write(out,"/",1);
388         T_FileStream_write(out,abName,(int32_t)uprv_strlen(abName));
389         T_FileStream_write(out,"/\"+",3);
390         str_write_java(locale,res->u.fString.fLength-(locale-res->u.fString.fChars),FALSE,status);
391     } else {
392         str_write_java(res->u.fString.fChars,res->u.fString.fLength,FALSE,status);
393     }*/
394 
395     str_write_java(res->u.fString.fChars,res->u.fString.fLength,FALSE,status);
396 
397     T_FileStream_write(out,"),\n",3);
398 }
399 
400 static void
array_write_java(struct SResource * res,UErrorCode * status)401 array_write_java( struct SResource *res, UErrorCode *status) {
402 
403     uint32_t  i         = 0;
404     const char* arr ="new String[] { \n";
405     struct SResource *current = NULL;
406     struct SResource *first =NULL;
407     UBool decrementTabs = FALSE;
408     UBool allStrings    = TRUE;
409 
410     if (U_FAILURE(*status)) {
411         return;
412     }
413 
414     if (res->u.fArray.fCount > 0) {
415 
416         current = res->u.fArray.fFirst;
417         i = 0;
418         while(current != NULL){
419             if(current->fType!=URES_STRING){
420                 allStrings = FALSE;
421                 break;
422             }
423             current= current->fNext;
424         }
425 
426         current = res->u.fArray.fFirst;
427         if(allStrings==FALSE){
428             const char* object = "new Object[]{\n";
429             write_tabs(out);
430             T_FileStream_write(out, object, (int32_t)uprv_strlen(object));
431             tabCount++;
432             decrementTabs = TRUE;
433         }else{
434             write_tabs(out);
435             T_FileStream_write(out, arr, (int32_t)uprv_strlen(arr));
436             tabCount++;
437         }
438         first=current;
439         while (current != NULL) {
440             /*if(current->fType==URES_STRING){
441                 write_tabs(out);
442             }*/
443             res_write_java(current, status);
444             if(U_FAILURE(*status)){
445                 return;
446             }
447             i++;
448             current = current->fNext;
449         }
450         T_FileStream_write(out,"\n",1);
451 
452         tabCount--;
453         write_tabs(out);
454         T_FileStream_write(out,"},\n",3);
455 
456     } else {
457         write_tabs(out);
458         T_FileStream_write(out,arr,(int32_t)uprv_strlen(arr));
459         write_tabs(out);
460         T_FileStream_write(out,"},\n",3);
461     }
462 }
463 
464 static void
intvector_write_java(struct SResource * res,UErrorCode * status)465 intvector_write_java( struct SResource *res, UErrorCode *status) {
466     uint32_t i = 0;
467     const char* intArr = "new Integer[] {\n";
468     const char* intC   = "new Integer(";
469     const char* stringArr = "new String[]{\n";
470     char buf[100];
471     int len =0;
472     buf[0]=0;
473     write_tabs(out);
474 
475     if(res->fKey > 0 && uprv_strcmp(srBundle->fKeys+res->fKey,"DateTimeElements")==0){
476         T_FileStream_write(out, stringArr, (int32_t)uprv_strlen(stringArr));
477         tabCount++;
478         for(i = 0; i<res->u.fIntVector.fCount; i++) {
479             write_tabs(out);
480             len=itostr(buf,res->u.fIntVector.fArray[i],10,0);
481             T_FileStream_write(out,"\"",1);
482             T_FileStream_write(out,buf,len);
483             T_FileStream_write(out,"\",",2);
484             T_FileStream_write(out,"\n",1);
485         }
486     }else{
487         T_FileStream_write(out, intArr, (int32_t)uprv_strlen(intArr));
488         tabCount++;
489         for(i = 0; i<res->u.fIntVector.fCount; i++) {
490             write_tabs(out);
491             T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC));
492             len=itostr(buf,res->u.fIntVector.fArray[i],10,0);
493             T_FileStream_write(out,buf,len);
494             T_FileStream_write(out,"),",2);
495             T_FileStream_write(out,"\n",1);
496         }
497     }
498     tabCount--;
499     write_tabs(out);
500     T_FileStream_write(out,"},\n",3);
501 }
502 
503 static void
int_write_java(struct SResource * res,UErrorCode * status)504 int_write_java(struct SResource *res,UErrorCode *status) {
505     const char* intC   =  "new Integer(";
506     char buf[100];
507     int len =0;
508     buf[0]=0;
509 
510     /* write the binary data */
511     write_tabs(out);
512     T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC));
513     len=itostr(buf, res->u.fIntValue.fValue, 10, 0);
514     T_FileStream_write(out,buf,len);
515     T_FileStream_write(out,"),\n",3 );
516 
517 }
518 
519 static void
bin_write_java(struct SResource * res,UErrorCode * status)520 bin_write_java( struct SResource *res, UErrorCode *status) {
521     const char* type = "new ICUListResourceBundle.CompressedBinary(";
522     const char* ext;
523     int32_t srcLen=res->u.fBinaryValue.fLength;
524 
525     if(srcLen>0 ){
526         uint16_t* target=NULL;
527         uint16_t* saveTarget = NULL;
528         int32_t tgtLen = 0;
529 
530         if(res->fKey > 0 && (uprv_strcmp(srBundle->fKeys+res->fKey,"%%CollationBin")==0 || uprv_strcmp(srBundle->fKeys+res->fKey,"BreakDictionaryData")==0)){
531             char fileName[1024] ={0};
532             char fn[1024] =  {0};
533             FileStream* datFile = NULL;
534             if(uprv_strcmp(srBundle->fKeys+res->fKey,"BreakDictionaryData")==0){
535                 uprv_strcat(fileName,"BreakDictionaryData");
536                 ext = ".brk";
537             }else{
538                 uprv_strcat(fileName,"CollationElements");
539                 ext=".col";
540             }
541             if(uprv_strcmp(srBundle->fLocale,"root")!=0){
542                 uprv_strcat(fileName,"_");
543                 uprv_strcat(fileName,srBundle->fLocale);
544                 if(NUMBER > ZERO){
545                     uprv_strcat(fileName, "_");
546                     uprv_strcat(fileName, &NUMBER);
547                 }
548                 NUMBER++;
549             }
550 
551             uprv_strcat(fileName,ext);
552             if(outDir ){
553                 uprv_strcat(fn,outDir);
554                 if(outDir[uprv_strlen(outDir)-1]!=U_FILE_SEP_CHAR){
555                     uprv_strcat(fn,U_FILE_SEP_STRING);
556                 }
557             }
558             uprv_strcat(fn,fileName);
559             type = "new ICUListResourceBundle.ResourceBinary(";
560             write_tabs(out);
561             T_FileStream_write(out, type, (int32_t)uprv_strlen(type));
562             T_FileStream_write(out, "\"", 1);
563             T_FileStream_write(out, fileName, (int32_t)uprv_strlen(fileName));
564             T_FileStream_write(out, "\"),\n", 4);
565 
566             datFile=T_FileStream_open(fn,"w");
567             T_FileStream_write(datFile, res->u.fBinaryValue.fData, res->u.fBinaryValue.fLength);
568             T_FileStream_close(datFile);
569 
570         }else{
571 
572             srcLen = res->u.fBinaryValue.fLength;
573             tgtLen = srcLen * 2;
574             target = (uint16_t*)malloc(sizeof(uint16_t) * tgtLen);
575             saveTarget  = target;
576             if(target){
577                 tgtLen = byteArrayToRLEString(res->u.fBinaryValue.fData,
578                                               srcLen,target, tgtLen,status);
579                 if(U_FAILURE(*status)){
580                      printf("Could not encode got error : %s \n", u_errorName(*status));
581                      return;
582                 }
583 #if DEBUG
584                 /***************** Test Roundtripping *********************/
585                 {
586                     int32_t myTargetLen = rleStringToByteArray(target,tgtLen,NULL,0,status);
587                     uint8_t* myTarget = (uint8_t*) malloc(sizeof(uint8_t) * myTargetLen);
588 
589                     /* test for NULL */
590                     if(myTarget == NULL) {
591                         *status = U_MEMORY_ALLOCATION_ERROR;
592                         return;
593                     }
594 
595                     int i=0;
596                     int32_t retVal=0;
597 
598                     *status = U_ZERO_ERROR;
599                     retVal=rleStringToByteArray(target,tgtLen,myTarget,myTargetLen,status);
600                     if(U_SUCCESS(*status)){
601 
602                         for(i=0; i< srcLen;i++){
603                             if(res->u.fBinaryValue.fData[i]!= myTarget[i]){
604                                 printf("the encoded string cannot be decoded Expected : 0x%02X Got : %: 0x%02X at %i\n",res->u.fBinaryValue.fData[i],myTarget[i], i);
605                             }
606                         }
607                     }else{
608                         printf("Could not decode got error : %s \n", u_errorName(*status));
609                     }
610                     free(myTarget);
611 
612                 }
613 #endif
614 
615             }else{
616                 *status = U_MEMORY_ALLOCATION_ERROR;
617                 return;
618             }
619 
620             write_tabs(out);
621             T_FileStream_write(out, type, (int32_t)uprv_strlen(type));
622             T_FileStream_write(out, "\n", 1);
623             tabCount++;
624             write_tabs(out);
625             str_write_java(target, tgtLen,FALSE, status);
626             tabCount--;
627             T_FileStream_write(out, "),\n", 3);
628 
629             free(target);
630 
631         }
632 
633    }else{
634         write_tabs(out);
635         T_FileStream_write(out,type,(int32_t)uprv_strlen(type));
636         T_FileStream_write(out,"null),\n",7);
637    }
638 
639 }
640 
641 
642 static UBool start = TRUE;
643 
644 static void
table_write_java(struct SResource * res,UErrorCode * status)645 table_write_java(struct SResource *res, UErrorCode *status) {
646     uint32_t  i         = 0;
647     UBool allStrings =TRUE;
648     struct SResource *current = NULL;
649     struct SResource *save = NULL;
650     const char* obj = "new Object[][]{\n";
651 
652     if (U_FAILURE(*status)) {
653         return ;
654     }
655 
656     if (res->u.fTable.fCount > 0) {
657         if(start==FALSE){
658             write_tabs(out);
659             T_FileStream_write(out, obj, (int32_t)uprv_strlen(obj));
660             tabCount++;
661         }
662         start = FALSE;
663         save = current = res->u.fTable.fFirst;
664         i       = 0;
665 
666 
667         while (current != NULL) {
668             assert(i < res->u.fTable.fCount);
669             write_tabs(out);
670 
671             T_FileStream_write(out, openBrace, 2);
672 
673 
674             tabCount++;
675             allStrings=FALSE;
676 
677             write_tabs(out);
678             if(current->fKey > 0){
679                 T_FileStream_write(out, "\"", 1);
680                 T_FileStream_write(out, srBundle->fKeys+current->fKey,
681                                    (int32_t)uprv_strlen(srBundle->fKeys+current->fKey));
682                 T_FileStream_write(out, "\",\n", 2);
683 
684                 T_FileStream_write(out, "\n", 1);
685             }
686             res_write_java(current, status);
687             if(U_FAILURE(*status)){
688                 return;
689             }
690             i++;
691             current = current->fNext;
692             tabCount--;
693             write_tabs(out);
694             T_FileStream_write(out, "},\n", 3);
695         }
696         if(tabCount>4){
697             tabCount--;
698             write_tabs(out);
699             T_FileStream_write(out, "},\n", 3);
700         }
701 
702     } else {
703         write_tabs(out);
704         T_FileStream_write(out,obj,(int32_t)uprv_strlen(obj));
705 
706         write_tabs(out);
707         T_FileStream_write(out,"},\n",3);
708 
709     }
710 
711 }
712 
713 void
res_write_java(struct SResource * res,UErrorCode * status)714 res_write_java(struct SResource *res,UErrorCode *status) {
715 
716     if (U_FAILURE(*status)) {
717         return ;
718     }
719 
720     if (res != NULL) {
721         switch (res->fType) {
722         case URES_STRING:
723              string_write_java    (res, status);
724              return;
725         case URES_ALIAS:
726              alias_write_java     (res, status);
727              return;
728         case URES_INT_VECTOR:
729              intvector_write_java (res, status);
730              return;
731         case URES_BINARY:
732              bin_write_java       (res, status);
733              return;
734         case URES_INT:
735              int_write_java       (res, status);
736              return;
737         case URES_ARRAY:
738              array_write_java     (res, status);
739              return;
740         case URES_TABLE:
741         case URES_TABLE32:
742              table_write_java     (res, status);
743              return;
744 
745         default:
746             break;
747         }
748     }
749 
750     *status = U_INTERNAL_PROGRAM_ERROR;
751 }
752 
753 void
bundle_write_java(struct SRBRoot * bundle,const char * outputDir,const char * outputEnc,char * writtenFilename,int writtenFilenameLen,const char * packageName,const char * bundleName,UErrorCode * status)754 bundle_write_java(struct SRBRoot *bundle, const char *outputDir,const char* outputEnc,
755                   char *writtenFilename, int writtenFilenameLen,
756                   const char* packageName, const char* bundleName,
757                   UErrorCode *status) {
758 
759     char fileName[256] = {'\0'};
760     char className[256]={'\0'};
761     char constructor[1000] = { 0 };
762     UBool j1 =FALSE;
763     outDir = outputDir;
764 
765     bName = (bundleName==NULL) ? "LocaleElements" : bundleName;
766     pName = (packageName==NULL)? "com.ibm.icu.impl.data" : packageName;
767 
768     uprv_strcpy(className, bName);
769     srBundle = bundle;
770     if(uprv_strcmp(srBundle->fLocale,"root")!=0){
771         uprv_strcat(className,"_");
772         uprv_strcat(className,srBundle->fLocale);
773     }
774     if(outputDir){
775         uprv_strcpy(fileName, outputDir);
776         if(outputDir[uprv_strlen(outputDir)-1] !=U_FILE_SEP_CHAR){
777             uprv_strcat(fileName,U_FILE_SEP_STRING);
778         }
779         uprv_strcat(fileName,className);
780         uprv_strcat(fileName,".java");
781     }else{
782         uprv_strcat(fileName,className);
783         uprv_strcat(fileName,".java");
784     }
785 
786     if (writtenFilename) {
787         uprv_strncpy(writtenFilename, fileName, writtenFilenameLen);
788     }
789 
790     if (U_FAILURE(*status)) {
791         return;
792     }
793 
794     out= T_FileStream_open(fileName,"w");
795 
796     if(out==NULL){
797         *status = U_FILE_ACCESS_ERROR;
798         return;
799     }
800     if(getIncludeCopyright()){
801         T_FileStream_write(out, copyRight, (int32_t)uprv_strlen(copyRight));
802         T_FileStream_write(out, warningMsg, (int32_t)uprv_strlen(warningMsg));
803     }
804     T_FileStream_write(out,"package ",(int32_t)uprv_strlen("package "));
805     T_FileStream_write(out,pName,(int32_t)uprv_strlen(pName));
806     T_FileStream_write(out,";\n\n",3);
807     T_FileStream_write(out, javaClass, (int32_t)uprv_strlen(javaClass));
808     T_FileStream_write(out, className, (int32_t)uprv_strlen(className));
809     if(j1){
810         T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1));
811     }else{
812         sprintf(constructor,javaClassICU,className);
813         T_FileStream_write(out, constructor, (int32_t)uprv_strlen(constructor));
814     }
815 
816     if(outputEnc && *outputEnc!='\0'){
817         /* store the output encoding */
818         enc = outputEnc;
819         conv=ucnv_open(enc,status);
820         if(U_FAILURE(*status)){
821             return;
822         }
823     }
824     res_write_java(bundle->fRoot, status);
825 
826     T_FileStream_write(out, closeClass, (int32_t)uprv_strlen(closeClass));
827 
828     T_FileStream_close(out);
829 
830     ucnv_close(conv);
831 }
832