1 // © 2016 and later: Unicode, Inc. and others. 
   2 // License & terms of use: http://www.unicode.org/copyright.html 
   4 ******************************************************************************* 
   6 *   Copyright (C) 2000-2015, International Business Machines 
   7 *   Corporation and others.  All Rights Reserved. 
   9 ******************************************************************************* 
  13 * Modification History: 
  15 *   Date        Name        Description 
  16 *   01/11/02    Ram         Creation. 
  17 *   02/12/08    Spieth      Fix errant 'new Object[][]{' insertion 
  18 *   02/19/08    Spieth      Removed ICUListResourceBundle dependancy 
  19 ******************************************************************************* 
  23 #include "unicode/unistr.h" 
  26 #include "unicode/ures.h" 
  30 #include "unicode/ucnv.h" 
  35 #include "unicode/ustring.h" 
  36 #include "unicode/utf8.h" 
  38 void res_write_java(struct SResource 
*res
,UErrorCode 
*status
); 
  41 static const char copyRight
[] = 
  43     " *******************************************************************************\n" 
  45     " *   Copyright (C) International Business Machines\n" 
  46     " *   Corporation and others.  All Rights Reserved.\n" 
  48     " *******************************************************************************\n" 
  51     " * $" "Revision:  $ \n" 
  52     " *******************************************************************************\n" 
  54 static const char warningMsg
[] = 
  55     "/*********************************************************************\n" 
  56     "######################################################################\n" 
  58     "   WARNING: This file is generated by genrb Version " GENRB_VERSION 
".\n" 
  59     "            If you edit this file, please make sure that, the source\n" 
  60     "            of this file (XXXX.txt in LocaleElements_XXXX.java)\n" 
  62     "######################################################################\n" 
  63     " *********************************************************************\n" 
  65 static const char* openBrace
="{\n"; 
  66 static const char* closeClass
="    };\n" 
  69 static const char* javaClass 
=  "import java.util.ListResourceBundle;\n\n" 
  72 static const char* javaClass1
=  " extends ListResourceBundle {\n\n" 
  74                                 "     * Overrides ListResourceBundle \n" 
  76                                 "    public final Object[][] getContents() { \n" 
  79                                 "    private static Object[][] contents = {\n"; 
  80 /*static const char* javaClassICU= " extends ListResourceBundle {\n\n" 
  82                                  "          super.contents = data;\n" 
  84                                  "    static final Object[][] data = new Object[][] { \n";*/ 
  85 static int tabCount 
= 3; 
  87 static FileStream
* out
=NULL
; 
  88 static struct SRBRoot
* srBundle 
; 
  89 /*static const char* outDir = NULL;*/ 
  91 static const char* bName
=NULL
; 
  92 static const char* pName
=NULL
; 
  94 static void write_tabs(FileStream
* os
){ 
  96     for(;i
<=tabCount
;i
++){ 
  97         T_FileStream_write(os
,"    ",4); 
 103 static const char* enc 
=""; 
 104 static UConverter
* conv 
= NULL
; 
 107 uCharsToChars(char *target
, int32_t targetLen
, const UChar 
*source
, int32_t sourceLen
, UErrorCode 
*status
) { 
 111         if (source
[i
] == '\n') { 
 112             if (j 
+ 2 < targetLen
) { 
 113                 uprv_strcat(target
, "\\n"); 
 116         }else if(source
[i
]==0x0D){ 
 118                 uprv_strcat(target
,"\\f"); 
 121         }else if(source
[i
] == '"'){ 
 122             if(source
[i
-1]=='\''){ 
 124                     uprv_strcat(target
,"\\"); 
 125                     target
[j
+1]= (char)source
[i
]; 
 128             }else if(source
[i
-1]!='\\'){ 
 131                     uprv_strcat(target
,"\\"); 
 132                     target
[j
+1]= (char)source
[i
]; 
 135             }else if(source
[i
-1]=='\\'){ 
 136                 target
[j
++]= (char)source
[i
]; 
 138         }else if(source
[i
]=='\\'){ 
 151                        uprv_strcat(target
,"\\\\"); 
 158                        uprv_strcat(target
,"\\\\\\"); 
 164                         target
[j
]=(char)source
[i
]; 
 171                     uprv_strcat(target
,"\\\\"); 
 175         }else if(source
[i
]>=0x20 && source
[i
]<0x7F/*ASCII*/){ 
 177                 target
[j
] = (char) source
[i
]; 
 181             if(*enc 
=='\0' || source
[i
]==0x0000){ 
 182                 uprv_strcpy(str
,"\\u"); 
 183                 itostr(str
+2,source
[i
],16,4); 
 185                     uprv_strcat(target
,str
); 
 190                 int retVal
=ucnv_fromUChars(conv
,dest
,30,source
+i
,1,status
); 
 191                 if(U_FAILURE(*status
)){ 
 194                 if(j
+retVal
<targetLen
){ 
 195                     uprv_strcat(target
,dest
); 
 207 strrch(const char* source
,uint32_t sourceLen
,char find
){ 
 208     const char* tSourceEnd 
=source 
+ (sourceLen
-1); 
 209     while(tSourceEnd
>= source
){ 
 210         if(*tSourceEnd
==find
){ 
 211             return (uint32_t)(tSourceEnd
-source
); 
 215     return (uint32_t)(tSourceEnd
-source
); 
 218 static int32_t getColumnCount(int32_t len
){ 
 219     int32_t columnCount 
= 80; 
 220     int32_t maxLines 
= 3000; 
 221     int32_t adjustedLen 
= len
*5; /* assume that every codepoint is represented in \uXXXX format*/ 
 223      * calculate the number of lines that 
 224      * may be required if column count is 80 
 226     if (maxLines  
< (adjustedLen 
/ columnCount
) ){ 
 227         columnCount 
= adjustedLen 
/ maxLines
; 
 232 str_write_java(const UChar 
*src
, int32_t srcLen
, UBool printEndLine
, UErrorCode 
*status
) { 
 234     uint32_t length 
= srcLen
*8; 
 236     uint32_t columnCount
; 
 237     char* buf 
= (char*) malloc(sizeof(char)*length
); 
 240         *status 
= U_MEMORY_ALLOCATION_ERROR
; 
 244     columnCount 
= getColumnCount(srcLen
); 
 245     memset(buf
,0,length
); 
 247     bufLen 
= uCharsToChars(buf
,length
,src
,srcLen
,status
); 
 248     // buflen accounts for extra bytes added due to multi byte encoding of 
 249     //        non ASCII characters 
 253     if(U_FAILURE(*status
)){ 
 258     if(bufLen
+(tabCount
*4) > columnCount  
){ 
 263             add 
= columnCount
-(tabCount
*4)-5/* for ", +\n */; 
 265             if (add 
< (bufLen
-len
)) { 
 266                 uint32_t idx 
= strrch(current
,add
,'\\'); 
 273                         if(current
[num
]=='\\'){ 
 279                     if ((idx
-num
)%2
==0) { 
 282                     seqLen 
= (current
[idx
+1]=='u') ? 6 : 2; 
 283                     if ((add
-idx
) < seqLen
) { 
 288             T_FileStream_write(out
,"\"",1); 
 289             uint32_t byteIndex 
= 0; 
 290             uint32_t trailBytes 
= 0; 
 292                 // check the trail bytes to be added to the output line 
 293                 while (byteIndex 
< add
) { 
 294                     if (U8_IS_LEAD(*(current 
+ byteIndex
))) { 
 295                         trailBytes 
= U8_COUNT_TRAIL_BYTES(*(current 
+ byteIndex
)); 
 300                 T_FileStream_write(out
,current
,add
); 
 301                 if (len 
+ add 
< bufLen
) { 
 302                     T_FileStream_write(out
,"\" +\n",4); 
 306                 T_FileStream_write(out
,current
,bufLen
-len
); 
 311         T_FileStream_write(out
,"\"",1); 
 312         T_FileStream_write(out
, buf
,bufLen
); 
 315         T_FileStream_write(out
,"\",\n",3); 
 317         T_FileStream_write(out
,"\"",1); 
 322 /* Writing Functions */ 
 324 string_write_java(const StringResource 
*res
,UErrorCode 
*status
) { 
 325     (void)res
->getKeyString(srBundle
); 
 327     str_write_java(res
->getBuffer(), res
->length(), TRUE
, status
); 
 331 array_write_java(const ArrayResource 
*res
, UErrorCode 
*status
) { 
 334     const char* arr 
="new String[] { \n"; 
 335     struct SResource 
*current 
= NULL
; 
 336     UBool allStrings    
= TRUE
; 
 338     if (U_FAILURE(*status
)) { 
 342     if (res
->fCount 
> 0) { 
 344         current 
= res
->fFirst
; 
 346         while(current 
!= NULL
){ 
 347             if(!current
->isString()){ 
 351             current
= current
->fNext
; 
 354         current 
= res
->fFirst
; 
 355         if(allStrings
==FALSE
){ 
 356             const char* object 
= "new Object[]{\n"; 
 358             T_FileStream_write(out
, object
, (int32_t)uprv_strlen(object
)); 
 362             T_FileStream_write(out
, arr
, (int32_t)uprv_strlen(arr
)); 
 365         while (current 
!= NULL
) { 
 366             /*if(current->isString()){ 
 369             res_write_java(current
, status
); 
 370             if(U_FAILURE(*status
)){ 
 374             current 
= current
->fNext
; 
 376         T_FileStream_write(out
,"\n",1); 
 380         T_FileStream_write(out
,"},\n",3); 
 384         T_FileStream_write(out
,arr
,(int32_t)uprv_strlen(arr
)); 
 386         T_FileStream_write(out
,"},\n",3); 
 391 intvector_write_java(const IntVectorResource 
*res
, UErrorCode 
* /*status*/) { 
 393     const char* intArr 
= "new int[] {\n"; 
 394     /* const char* intC   = "new Integer(";   */ 
 395     const char* stringArr 
= "new String[]{\n"; 
 396     const char *resname 
= res
->getKeyString(srBundle
); 
 402     if(resname 
!= NULL 
&& uprv_strcmp(resname
,"DateTimeElements")==0){ 
 403         T_FileStream_write(out
, stringArr
, (int32_t)uprv_strlen(stringArr
)); 
 405         for(i 
= 0; i
<res
->fCount
; i
++) { 
 407             len
=itostr(buf
,res
->fArray
[i
],10,0); 
 408             T_FileStream_write(out
,"\"",1); 
 409             T_FileStream_write(out
,buf
,len
); 
 410             T_FileStream_write(out
,"\",",2); 
 411             T_FileStream_write(out
,"\n",1); 
 414         T_FileStream_write(out
, intArr
, (int32_t)uprv_strlen(intArr
)); 
 416         for(i 
= 0; i
<res
->fCount
; i
++) { 
 418             /* T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC)); */ 
 419             len
=itostr(buf
,res
->fArray
[i
],10,0); 
 420             T_FileStream_write(out
,buf
,len
); 
 421             /* T_FileStream_write(out,"),",2);  */ 
 422             /* T_FileStream_write(out,"\n",1);  */ 
 423             T_FileStream_write(out
,",\n",2); 
 428     T_FileStream_write(out
,"},\n",3); 
 432 int_write_java(const IntResource 
*res
, UErrorCode 
* /*status*/) { 
 433     const char* intC   
=  "new Integer("; 
 438     /* write the binary data */ 
 440     T_FileStream_write(out
, intC
, (int32_t)uprv_strlen(intC
)); 
 441     len
=itostr(buf
, res
->fValue
, 10, 0); 
 442     T_FileStream_write(out
,buf
,len
); 
 443     T_FileStream_write(out
,"),\n",3 ); 
 448 bytes_write_java(const BinaryResource 
*res
, UErrorCode 
* /*status*/) { 
 449         const char* type  
= "new byte[] {"; 
 450         const char* byteDecl 
= "%i, "; 
 451     char byteBuffer
[100] = { 0 }; 
 452         uint8_t*  byteArray 
= NULL
; 
 453     int byteIterator 
= 0; 
 454     int32_t srcLen
=res
->fLength
; 
 457         byteArray 
= res
->fData
; 
 460         T_FileStream_write(out
, type
, (int32_t)uprv_strlen(type
)); 
 461         T_FileStream_write(out
, "\n", 1); 
 464                 for (;byteIterator
<srcLen
;byteIterator
++) 
 466             if (byteIterator%16 
== 0) 
 471                         if (byteArray
[byteIterator
] < 128) 
 473                 sprintf(byteBuffer
, byteDecl
, byteArray
[byteIterator
]); 
 477                 sprintf(byteBuffer
, byteDecl
, (byteArray
[byteIterator
]-256)); 
 480             T_FileStream_write(out
, byteBuffer
, (int32_t)uprv_strlen(byteBuffer
)); 
 482                         if (byteIterator%16 
== 15) 
 484                 T_FileStream_write(out
, "\n", 1); 
 489         if (((byteIterator
-1)%16
) != 15) 
 491             T_FileStream_write(out
, "\n", 1); 
 496                 T_FileStream_write(out
, "},\n", 3); 
 503         T_FileStream_write(out
,type
,(int32_t)uprv_strlen(type
)); 
 504                 T_FileStream_write(out
,"},\n",3); 
 509 static UBool start 
= TRUE
; 
 512 table_write_java(const TableResource 
*res
, UErrorCode 
*status
) { 
 514     struct SResource 
*current 
= NULL
; 
 515     const char* obj 
= "new Object[][]{\n"; 
 517     if (U_FAILURE(*status
)) { 
 521     if (res
->fCount 
> 0) { 
 524             T_FileStream_write(out
, obj
, (int32_t)uprv_strlen(obj
)); 
 528         current 
= res
->fFirst
; 
 532         while (current 
!= NULL
) { 
 533             const char *currentKeyString 
= current
->getKeyString(srBundle
); 
 535             assert(i 
< res
->fCount
); 
 538             T_FileStream_write(out
, openBrace
, 2); 
 544             if(currentKeyString 
!= NULL
) { 
 545                 T_FileStream_write(out
, "\"", 1); 
 546                 T_FileStream_write(out
, currentKeyString
, 
 547                                    (int32_t)uprv_strlen(currentKeyString
)); 
 548                 T_FileStream_write(out
, "\",\n", 2); 
 550                 T_FileStream_write(out
, "\n", 1); 
 552             res_write_java(current
, status
); 
 553             if(U_FAILURE(*status
)){ 
 557             current 
= current
->fNext
; 
 560             T_FileStream_write(out
, "},\n", 3); 
 565             T_FileStream_write(out
, "},\n", 3); 
 570         T_FileStream_write(out
,obj
,(int32_t)uprv_strlen(obj
)); 
 573         T_FileStream_write(out
,"},\n",3); 
 580 res_write_java(struct SResource 
*res
,UErrorCode 
*status
) { 
 582     if (U_FAILURE(*status
)) { 
 587         switch (res
->fType
) { 
 589              string_write_java    (static_cast<const StringResource 
*>(res
), status
); 
 592              printf("Encountered unsupported resource type %d of alias\n", res
->fType
); 
 593              *status 
= U_UNSUPPORTED_ERROR
; 
 595         case URES_INT_VECTOR
: 
 596              intvector_write_java (static_cast<const IntVectorResource 
*>(res
), status
); 
 599              bytes_write_java     (static_cast<const BinaryResource 
*>(res
), status
); 
 602              int_write_java       (static_cast<const IntResource 
*>(res
), status
); 
 605              array_write_java     (static_cast<const ArrayResource 
*>(res
), status
); 
 608              table_write_java     (static_cast<const TableResource 
*>(res
), status
); 
 615     *status 
= U_INTERNAL_PROGRAM_ERROR
; 
 619 bundle_write_java(struct SRBRoot 
*bundle
, const char *outputDir
,const char* outputEnc
, 
 620                   char *writtenFilename
, int writtenFilenameLen
, 
 621                   const char* packageName
, const char* bundleName
, 
 622                   UErrorCode 
*status
) { 
 624     char fileName
[256] = {'\0'}; 
 625     char className
[256]={'\0'}; 
 626     /*char constructor[1000] = { 0 };*/ 
 628     /*outDir = outputDir;*/ 
 630     start 
= TRUE
;                        /* Reset the start indictor*/ 
 632     bName 
= (bundleName
==NULL
) ? "LocaleElements" : bundleName
; 
 633     pName 
= (packageName
==NULL
)? "com.ibm.icu.impl.data" : packageName
; 
 635     uprv_strcpy(className
, bName
); 
 637     if(uprv_strcmp(srBundle
->fLocale
,"root")!=0){ 
 638         uprv_strcat(className
,"_"); 
 639         uprv_strcat(className
,srBundle
->fLocale
); 
 642         uprv_strcpy(fileName
, outputDir
); 
 643         if(outputDir
[uprv_strlen(outputDir
)-1] !=U_FILE_SEP_CHAR
){ 
 644             uprv_strcat(fileName
,U_FILE_SEP_STRING
); 
 646         uprv_strcat(fileName
,className
); 
 647         uprv_strcat(fileName
,".java"); 
 649         uprv_strcat(fileName
,className
); 
 650         uprv_strcat(fileName
,".java"); 
 653     if (writtenFilename
) { 
 654         uprv_strncpy(writtenFilename
, fileName
, writtenFilenameLen
); 
 657     if (U_FAILURE(*status
)) { 
 661     out
= T_FileStream_open(fileName
,"w"); 
 664         *status 
= U_FILE_ACCESS_ERROR
; 
 667     if(getIncludeCopyright()){ 
 668         T_FileStream_write(out
, copyRight
, (int32_t)uprv_strlen(copyRight
)); 
 669         T_FileStream_write(out
, warningMsg
, (int32_t)uprv_strlen(warningMsg
)); 
 671     T_FileStream_write(out
,"package ",(int32_t)uprv_strlen("package ")); 
 672     T_FileStream_write(out
,pName
,(int32_t)uprv_strlen(pName
)); 
 673     T_FileStream_write(out
,";\n\n",3); 
 674     T_FileStream_write(out
, javaClass
, (int32_t)uprv_strlen(javaClass
)); 
 675     T_FileStream_write(out
, className
, (int32_t)uprv_strlen(className
)); 
 676         T_FileStream_write(out
, javaClass1
, (int32_t)uprv_strlen(javaClass1
)); 
 679           T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1)); 
 681            sprintf(constructor,javaClassICU,className); 
 682            T_FileStream_write(out, constructor, (int32_t)uprv_strlen(constructor)); 
 686     if(outputEnc 
&& *outputEnc
!='\0'){ 
 687         /* store the output encoding */ 
 689         conv
=ucnv_open(enc
,status
); 
 690         if(U_FAILURE(*status
)){ 
 694     res_write_java(bundle
->fRoot
, status
); 
 696     T_FileStream_write(out
, closeClass
, (int32_t)uprv_strlen(closeClass
)); 
 698     T_FileStream_close(out
);