2 *******************************************************************************
4 * Copyright (C) 2000-2003, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
11 * Modification History:
13 * Date Name Description
14 * 01/11/02 Ram Creation.
15 *******************************************************************************
21 #include "unicode/ures.h"
25 #include "unicode/ucnv.h"
31 #include "unicode/ustring.h"
33 void res_write_java(struct SResource
*res
,UErrorCode
*status
);
36 static const char copyRight
[] =
38 " *******************************************************************************\n"
40 " * Copyright (C) International Business Machines\n"
41 " * Corporation and others. All Rights Reserved.\n"
43 " *******************************************************************************\n"
46 " * $" "Revision: $ \n"
47 " *******************************************************************************\n"
49 static const char warningMsg
[] =
50 "/*********************************************************************\n"
51 "######################################################################\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"
57 "######################################################################\n"
58 " *********************************************************************\n"
60 static const char* openBrace
="{\n";
61 static const char* closeClass
=" };\n"
64 static const char* javaClass
= "import java.util.ListResourceBundle;\n"
65 "import com.ibm.icu.impl.ICUListResourceBundle;\n\n"
68 static const char* javaClass1
= " extends ICUListResourceBundle {\n\n"
70 " * Overrides ListResourceBundle \n"
72 " public final Object[][] getContents() { \n"
75 " private static Object[][] contents = {\n";
76 static const char* javaClassICU
= " extends ICUListResourceBundle {\n\n"
78 " super.contents = data;\n"
80 " static final Object[][] data = new Object[][] { \n";
81 static int tabCount
= 3;
83 static FileStream
* out
=NULL
;
84 static struct SRBRoot
* srBundle
;
85 static const char* outDir
= NULL
;
87 static const char* bName
=NULL
;
88 static const char* pName
=NULL
;
90 static void write_tabs(FileStream
* os
){
92 for(;i
<=tabCount
;i
++){
93 T_FileStream_write(os
," ",4);
99 static const char* enc
="";
100 static UConverter
* conv
= NULL
;
101 static char NUMBER
= ZERO
;
104 uCharsToChars( char* target
,int32_t targetLen
, UChar
* source
, int32_t sourceLen
,UErrorCode
* status
){
108 if (source
[i
] == '\n') {
109 if (j
+ 2 < targetLen
) {
110 uprv_strcat(target
, "\\n");
113 }else if(source
[i
]==0x0D){
115 uprv_strcat(target
,"\\f");
118 }else if(source
[i
] == '"'){
119 if(source
[i
-1]=='\''){
121 uprv_strcat(target
,"\\");
122 target
[j
+1]= (char)source
[i
];
125 }else if(source
[i
-1]!='\\'){
128 uprv_strcat(target
,"\\");
129 target
[j
+1]= (char)source
[i
];
132 }else if(source
[i
-1]=='\\'){
133 target
[j
++]= (char)source
[i
];
135 }else if(source
[i
]=='\\'){
148 uprv_strcat(target
,"\\\\");
155 uprv_strcat(target
,"\\\\\\");
161 target
[j
]=(char)source
[i
];
168 uprv_strcat(target
,"\\\\");
172 }else if(source
[i
]>=0x20 && source
[i
]<0x7F/*ASCII*/){
174 target
[j
] = (char) source
[i
];
178 if(*enc
=='\0' || source
[i
]==0x0000){
179 uprv_strcpy(str
,"\\u");
180 itostr(str
+2,source
[i
],16,4);
182 uprv_strcat(target
,str
);
187 int retVal
=ucnv_fromUChars(conv
,dest
,30,source
+i
,1,status
);
188 if(U_FAILURE(*status
)){
191 if(j
+retVal
<targetLen
){
192 uprv_strcat(target
,dest
);
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
);
212 return (uint32_t)(tSourceEnd
-source
);
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*/
220 * calculate the number of lines that
221 * may be required if column count is 80
223 if (maxLines
< (adjustedLen
/ columnCount
) ){
224 columnCount
= adjustedLen
/ maxLines
;
229 str_write_java( uint16_t* src
, int32_t srcLen
, UBool printEndLine
, UErrorCode
*status
){
231 uint32_t length
= srcLen
*8;
233 char* buf
= (char*) malloc(sizeof(char)*length
);
235 uint32_t columnCount
= getColumnCount(srcLen
);
239 *status
= U_MEMORY_ALLOCATION_ERROR
;
243 memset(buf
,0,length
);
245 bufLen
= uCharsToChars(buf
,length
,src
,srcLen
,status
);
247 if(printEndLine
) write_tabs(out
);
249 if(U_FAILURE(*status
)){
253 if(bufLen
+(tabCount
*4) > columnCount
){
258 add
= columnCount
-(tabCount
*4)-5/* for ", +\n */;
260 if (add
< (bufLen
-len
)) {
261 uint32_t index
= strrch(current
,add
,'\\');
265 int32_t num
=index
-1;
268 if(current
[num
]=='\\'){
274 if ((index
-num
)%2
==0) {
277 seqLen
= (current
[index
+1]=='u') ? 6 : 2;
278 if ((add
-index
) < seqLen
) {
279 add
= index
+ seqLen
;
283 T_FileStream_write(out
,"\"",1);
285 T_FileStream_write(out
,current
,add
);
286 T_FileStream_write(out
,"\" +\n",4);
289 T_FileStream_write(out
,current
,bufLen
-len
);
294 T_FileStream_write(out
,"\"",1);
295 T_FileStream_write(out
, buf
,bufLen
);
298 T_FileStream_write(out
,"\",\n",3);
300 T_FileStream_write(out
,"\"",1);
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
);
312 uprv_strcat(fileName
,outDir
);
313 if(outDir
[uprv_strlen(outDir
)-1]!=U_FILE_SEP_CHAR
){
314 uprv_strcat(fileName
,U_FILE_SEP_STRING
);
317 uprv_strcat(fileName
,file
);/* UCARULES.utf8 UTF-8 file */
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");
328 *status
=U_MEMORY_ALLOCATION_ERROR
;
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
);
337 T_FileStream_write(datFile
,dest
,len
);
338 T_FileStream_close(datFile
);
341 #define MAX_SEQUENCE_LENGTH 30000
342 /* Writing Functions */
344 string_write_java(struct SResource
*res
,UErrorCode
*status
) {
345 if(res
->fKey
> 0 && uprv_strcmp(srBundle
->fKeys
+res
->fKey
,"%%UCARULES")==0 ){
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
);
359 str_write_java(res
->u
.fString
.fChars
,res
->u
.fString
.fLength
,TRUE
,status
);
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
);
371 alias_write_java(struct SResource
*res
,UErrorCode
*status
) {
372 static const char str
[] = "new ICUListResourceBundle.Alias(";
374 T_FileStream_write(out
,str
,(int32_t)uprv_strlen(str
));
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);
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);
390 str_write_java(res->u.fString.fChars,res->u.fString.fLength,FALSE,status);
393 str_write_java(res
->u
.fString
.fChars
,res
->u
.fString
.fLength
,FALSE
,status
);
395 T_FileStream_write(out
,"),\n",3);
399 array_write_java( struct SResource
*res
, UErrorCode
*status
) {
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
;
408 if (U_FAILURE(*status
)) {
412 if (res
->u
.fArray
.fCount
> 0) {
414 current
= res
->u
.fArray
.fFirst
;
416 while(current
!= NULL
){
417 if(current
->fType
!=URES_STRING
){
421 current
= current
->fNext
;
424 current
= res
->u
.fArray
.fFirst
;
425 if(allStrings
==FALSE
){
426 const char* object
= "new Object[]{\n";
428 T_FileStream_write(out
, object
, (int32_t)uprv_strlen(object
));
430 decrementTabs
= TRUE
;
433 T_FileStream_write(out
, arr
, (int32_t)uprv_strlen(arr
));
437 while (current
!= NULL
) {
438 /*if(current->fType==URES_STRING){
441 res_write_java(current
, status
);
442 if(U_FAILURE(*status
)){
446 current
= current
->fNext
;
448 T_FileStream_write(out
,"\n",1);
452 T_FileStream_write(out
,"},\n",3);
456 T_FileStream_write(out
,arr
,(int32_t)uprv_strlen(arr
));
458 T_FileStream_write(out
,"},\n",3);
463 intvector_write_java( struct SResource
*res
, UErrorCode
*status
) {
465 const char* intArr
= "new Integer[] {\n";
466 const char* intC
= "new Integer(";
467 const char* stringArr
= "new String[]{\n";
473 if(res
->fKey
> 0 && uprv_strcmp(srBundle
->fKeys
+res
->fKey
,"DateTimeElements")==0){
474 T_FileStream_write(out
, stringArr
, (int32_t)uprv_strlen(stringArr
));
476 for(i
= 0; i
<res
->u
.fIntVector
.fCount
; i
++) {
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);
485 T_FileStream_write(out
, intArr
, (int32_t)uprv_strlen(intArr
));
487 for(i
= 0; i
<res
->u
.fIntVector
.fCount
; i
++) {
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);
498 T_FileStream_write(out
,"},\n",3);
502 int_write_java(struct SResource
*res
,UErrorCode
*status
) {
503 const char* intC
= "new Integer(";
508 /* write the binary data */
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 );
518 bin_write_java( struct SResource
*res
, UErrorCode
*status
) {
519 const char* type
= "new ICUListResourceBundle.CompressedBinary(";
521 int32_t srcLen
=res
->u
.fBinaryValue
.fLength
;
524 uint16_t* target
=NULL
;
525 uint16_t* saveTarget
= NULL
;
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};
531 FileStream
* datFile
= NULL
;
532 if(uprv_strcmp(srBundle
->fKeys
+res
->fKey
,"BreakDictionaryData")==0){
533 uprv_strcat(fileName
,"BreakDictionaryData");
536 uprv_strcat(fileName
,"CollationElements");
539 if(uprv_strcmp(srBundle
->fLocale
,"root")!=0){
540 uprv_strcat(fileName
,"_");
541 uprv_strcat(fileName
,srBundle
->fLocale
);
543 uprv_strcat(fileName
, "_");
544 uprv_strcat(fileName
, &NUMBER
);
549 uprv_strcat(fileName
,ext
);
551 uprv_strcat(fn
,outDir
);
552 if(outDir
[uprv_strlen(outDir
)-1]!=U_FILE_SEP_CHAR
){
553 uprv_strcat(fn
,U_FILE_SEP_STRING
);
556 uprv_strcat(fn
,fileName
);
557 type
= "new ICUListResourceBundle.ResourceBinary(";
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);
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
);
570 srcLen
= res
->u
.fBinaryValue
.fLength
;
572 target
= (uint16_t*)malloc(sizeof(uint16_t) * tgtLen
);
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
));
582 /***************** Test Roundtripping *********************/
584 int32_t myTargetLen
= rleStringToByteArray(target
,tgtLen
,NULL
,0,status
);
585 uint8_t* myTarget
= (uint8_t*) malloc(sizeof(uint8_t) * myTargetLen
);
588 if(myTarget
== NULL
) {
589 *status
= U_MEMORY_ALLOCATION_ERROR
;
596 *status
= U_ZERO_ERROR
;
597 retVal
=rleStringToByteArray(target
,tgtLen
,myTarget
,myTargetLen
,status
);
598 if(U_SUCCESS(*status
)){
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
);
606 printf("Could not decode got error : %s \n", u_errorName(*status
));
614 *status
= U_MEMORY_ALLOCATION_ERROR
;
619 T_FileStream_write(out
, type
, (int32_t)uprv_strlen(type
));
620 T_FileStream_write(out
, "\n", 1);
623 str_write_java(target
, tgtLen
,FALSE
, status
);
625 T_FileStream_write(out
, "),\n", 3);
633 T_FileStream_write(out
,type
,(int32_t)uprv_strlen(type
));
634 T_FileStream_write(out
,"null),\n",7);
640 static UBool start
= TRUE
;
643 table_write_java(struct SResource
*res
, UErrorCode
*status
) {
645 UBool allStrings
=TRUE
;
646 struct SResource
*current
= NULL
;
647 struct SResource
*save
= NULL
;
648 const char* obj
= "new Object[][]{\n";
650 if (U_FAILURE(*status
)) {
654 if (res
->u
.fTable
.fCount
> 0) {
657 T_FileStream_write(out
, obj
, (int32_t)uprv_strlen(obj
));
661 save
= current
= res
->u
.fTable
.fFirst
;
665 while (current
!= NULL
) {
666 assert(i
< res
->u
.fTable
.fCount
);
669 T_FileStream_write(out
, openBrace
, 2);
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);
682 T_FileStream_write(out
, "\n", 1);
684 res_write_java(current
, status
);
685 if(U_FAILURE(*status
)){
689 current
= current
->fNext
;
692 T_FileStream_write(out
, "},\n", 3);
697 T_FileStream_write(out
, "},\n", 3);
702 T_FileStream_write(out
,obj
,(int32_t)uprv_strlen(obj
));
705 T_FileStream_write(out
,"},\n",3);
712 res_write_java(struct SResource
*res
,UErrorCode
*status
) {
714 if (U_FAILURE(*status
)) {
719 switch (res
->fType
) {
721 string_write_java (res
, status
);
724 alias_write_java (res
, status
);
726 case URES_INT_VECTOR
:
727 intvector_write_java (res
, status
);
730 bin_write_java (res
, status
);
733 int_write_java (res
, status
);
736 array_write_java (res
, status
);
740 table_write_java (res
, status
);
748 *status
= U_INTERNAL_PROGRAM_ERROR
;
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
) {
757 char fileName
[256] = {'\0'};
758 char className
[256]={'\0'};
759 char constructor
[1000] = { 0 };
763 bName
= (bundleName
==NULL
) ? "LocaleElements" : bundleName
;
764 pName
= (packageName
==NULL
)? "com.ibm.icu.impl.data" : packageName
;
766 uprv_strcpy(className
, bName
);
768 if(uprv_strcmp(srBundle
->fLocale
,"root")!=0){
769 uprv_strcat(className
,"_");
770 uprv_strcat(className
,srBundle
->fLocale
);
773 uprv_strcpy(fileName
, outputDir
);
774 if(outputDir
[uprv_strlen(outputDir
)-1] !=U_FILE_SEP_CHAR
){
775 uprv_strcat(fileName
,U_FILE_SEP_STRING
);
777 uprv_strcat(fileName
,className
);
778 uprv_strcat(fileName
,".java");
780 uprv_strcat(fileName
,className
);
781 uprv_strcat(fileName
,".java");
784 if (writtenFilename
) {
785 uprv_strncpy(writtenFilename
, fileName
, writtenFilenameLen
);
788 if (U_FAILURE(*status
)) {
792 out
= T_FileStream_open(fileName
,"w");
795 *status
= U_FILE_ACCESS_ERROR
;
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
));
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
));
808 T_FileStream_write(out
, javaClass1
, (int32_t)uprv_strlen(javaClass1
));
810 sprintf(constructor
,javaClassICU
,className
);
811 T_FileStream_write(out
, constructor
, (int32_t)uprv_strlen(constructor
));
814 if(outputEnc
&& *outputEnc
!='\0'){
815 /* store the output encoding */
817 conv
=ucnv_open(enc
,status
);
818 if(U_FAILURE(*status
)){
822 res_write_java(bundle
->fRoot
, status
);
824 T_FileStream_write(out
, closeClass
, (int32_t)uprv_strlen(closeClass
));
826 T_FileStream_close(out
);