2 *******************************************************************************
4 * Copyright (C) 2000-2006, 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 uint32_t columnCount
;
234 char* buf
= (char*) malloc(sizeof(char)*length
);
237 *status
= U_MEMORY_ALLOCATION_ERROR
;
241 columnCount
= getColumnCount(srcLen
);
242 memset(buf
,0,length
);
244 bufLen
= uCharsToChars(buf
,length
,src
,srcLen
,status
);
249 if(U_FAILURE(*status
)){
254 if(bufLen
+(tabCount
*4) > columnCount
){
259 add
= columnCount
-(tabCount
*4)-5/* for ", +\n */;
261 if (add
< (bufLen
-len
)) {
262 uint32_t index
= strrch(current
,add
,'\\');
266 int32_t num
=index
-1;
269 if(current
[num
]=='\\'){
275 if ((index
-num
)%2
==0) {
278 seqLen
= (current
[index
+1]=='u') ? 6 : 2;
279 if ((add
-index
) < seqLen
) {
280 add
= index
+ seqLen
;
284 T_FileStream_write(out
,"\"",1);
286 T_FileStream_write(out
,current
,add
);
287 T_FileStream_write(out
,"\" +\n",4);
290 T_FileStream_write(out
,current
,bufLen
-len
);
295 T_FileStream_write(out
,"\"",1);
296 T_FileStream_write(out
, buf
,bufLen
);
299 T_FileStream_write(out
,"\",\n",3);
301 T_FileStream_write(out
,"\"",1);
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
);
314 uprv_strcat(fileName
,outDir
);
315 if(outDir
[uprv_strlen(outDir
)-1]!=U_FILE_SEP_CHAR
){
316 uprv_strcat(fileName
,U_FILE_SEP_STRING
);
319 uprv_strcat(fileName
,file
);/* UCARULES.utf8 UTF-8 file */
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");
330 *status
=U_MEMORY_ALLOCATION_ERROR
;
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
);
339 T_FileStream_write(datFile
,dest
,len
);
340 T_FileStream_close(datFile
);
343 #define MAX_SEQUENCE_LENGTH 30000
344 /* Writing Functions */
346 string_write_java(struct SResource
*res
,UErrorCode
*status
) {
347 if(res
->fKey
> 0 && uprv_strcmp(srBundle
->fKeys
+res
->fKey
,"%%UCARULES")==0 ){
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
);
361 str_write_java(res
->u
.fString
.fChars
,res
->u
.fString
.fLength
,TRUE
,status
);
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
);
373 alias_write_java(struct SResource
*res
,UErrorCode
*status
) {
374 static const char str
[] = "new ICUListResourceBundle.Alias(";
376 T_FileStream_write(out
,str
,(int32_t)uprv_strlen(str
));
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);
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);
392 str_write_java(res->u.fString.fChars,res->u.fString.fLength,FALSE,status);
395 str_write_java(res
->u
.fString
.fChars
,res
->u
.fString
.fLength
,FALSE
,status
);
397 T_FileStream_write(out
,"),\n",3);
401 array_write_java( struct SResource
*res
, UErrorCode
*status
) {
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
;
410 if (U_FAILURE(*status
)) {
414 if (res
->u
.fArray
.fCount
> 0) {
416 current
= res
->u
.fArray
.fFirst
;
418 while(current
!= NULL
){
419 if(current
->fType
!=URES_STRING
){
423 current
= current
->fNext
;
426 current
= res
->u
.fArray
.fFirst
;
427 if(allStrings
==FALSE
){
428 const char* object
= "new Object[]{\n";
430 T_FileStream_write(out
, object
, (int32_t)uprv_strlen(object
));
432 decrementTabs
= TRUE
;
435 T_FileStream_write(out
, arr
, (int32_t)uprv_strlen(arr
));
439 while (current
!= NULL
) {
440 /*if(current->fType==URES_STRING){
443 res_write_java(current
, status
);
444 if(U_FAILURE(*status
)){
448 current
= current
->fNext
;
450 T_FileStream_write(out
,"\n",1);
454 T_FileStream_write(out
,"},\n",3);
458 T_FileStream_write(out
,arr
,(int32_t)uprv_strlen(arr
));
460 T_FileStream_write(out
,"},\n",3);
465 intvector_write_java( struct SResource
*res
, UErrorCode
*status
) {
467 const char* intArr
= "new Integer[] {\n";
468 const char* intC
= "new Integer(";
469 const char* stringArr
= "new String[]{\n";
475 if(res
->fKey
> 0 && uprv_strcmp(srBundle
->fKeys
+res
->fKey
,"DateTimeElements")==0){
476 T_FileStream_write(out
, stringArr
, (int32_t)uprv_strlen(stringArr
));
478 for(i
= 0; i
<res
->u
.fIntVector
.fCount
; i
++) {
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);
487 T_FileStream_write(out
, intArr
, (int32_t)uprv_strlen(intArr
));
489 for(i
= 0; i
<res
->u
.fIntVector
.fCount
; i
++) {
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);
500 T_FileStream_write(out
,"},\n",3);
504 int_write_java(struct SResource
*res
,UErrorCode
*status
) {
505 const char* intC
= "new Integer(";
510 /* write the binary data */
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 );
520 bin_write_java( struct SResource
*res
, UErrorCode
*status
) {
521 const char* type
= "new ICUListResourceBundle.CompressedBinary(";
523 int32_t srcLen
=res
->u
.fBinaryValue
.fLength
;
526 uint16_t* target
=NULL
;
527 uint16_t* saveTarget
= NULL
;
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};
533 FileStream
* datFile
= NULL
;
534 if(uprv_strcmp(srBundle
->fKeys
+res
->fKey
,"BreakDictionaryData")==0){
535 uprv_strcat(fileName
,"BreakDictionaryData");
538 uprv_strcat(fileName
,"CollationElements");
541 if(uprv_strcmp(srBundle
->fLocale
,"root")!=0){
542 uprv_strcat(fileName
,"_");
543 uprv_strcat(fileName
,srBundle
->fLocale
);
545 uprv_strcat(fileName
, "_");
546 uprv_strcat(fileName
, &NUMBER
);
551 uprv_strcat(fileName
,ext
);
553 uprv_strcat(fn
,outDir
);
554 if(outDir
[uprv_strlen(outDir
)-1]!=U_FILE_SEP_CHAR
){
555 uprv_strcat(fn
,U_FILE_SEP_STRING
);
558 uprv_strcat(fn
,fileName
);
559 type
= "new ICUListResourceBundle.ResourceBinary(";
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);
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
);
572 srcLen
= res
->u
.fBinaryValue
.fLength
;
574 target
= (uint16_t*)malloc(sizeof(uint16_t) * tgtLen
);
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
));
584 /***************** Test Roundtripping *********************/
586 int32_t myTargetLen
= rleStringToByteArray(target
,tgtLen
,NULL
,0,status
);
587 uint8_t* myTarget
= (uint8_t*) malloc(sizeof(uint8_t) * myTargetLen
);
590 if(myTarget
== NULL
) {
591 *status
= U_MEMORY_ALLOCATION_ERROR
;
598 *status
= U_ZERO_ERROR
;
599 retVal
=rleStringToByteArray(target
,tgtLen
,myTarget
,myTargetLen
,status
);
600 if(U_SUCCESS(*status
)){
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
);
608 printf("Could not decode got error : %s \n", u_errorName(*status
));
616 *status
= U_MEMORY_ALLOCATION_ERROR
;
621 T_FileStream_write(out
, type
, (int32_t)uprv_strlen(type
));
622 T_FileStream_write(out
, "\n", 1);
625 str_write_java(target
, tgtLen
,FALSE
, status
);
627 T_FileStream_write(out
, "),\n", 3);
635 T_FileStream_write(out
,type
,(int32_t)uprv_strlen(type
));
636 T_FileStream_write(out
,"null),\n",7);
642 static UBool start
= TRUE
;
645 table_write_java(struct SResource
*res
, UErrorCode
*status
) {
647 UBool allStrings
=TRUE
;
648 struct SResource
*current
= NULL
;
649 struct SResource
*save
= NULL
;
650 const char* obj
= "new Object[][]{\n";
652 if (U_FAILURE(*status
)) {
656 if (res
->u
.fTable
.fCount
> 0) {
659 T_FileStream_write(out
, obj
, (int32_t)uprv_strlen(obj
));
663 save
= current
= res
->u
.fTable
.fFirst
;
667 while (current
!= NULL
) {
668 assert(i
< res
->u
.fTable
.fCount
);
671 T_FileStream_write(out
, openBrace
, 2);
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);
684 T_FileStream_write(out
, "\n", 1);
686 res_write_java(current
, status
);
687 if(U_FAILURE(*status
)){
691 current
= current
->fNext
;
694 T_FileStream_write(out
, "},\n", 3);
699 T_FileStream_write(out
, "},\n", 3);
704 T_FileStream_write(out
,obj
,(int32_t)uprv_strlen(obj
));
707 T_FileStream_write(out
,"},\n",3);
714 res_write_java(struct SResource
*res
,UErrorCode
*status
) {
716 if (U_FAILURE(*status
)) {
721 switch (res
->fType
) {
723 string_write_java (res
, status
);
726 alias_write_java (res
, status
);
728 case URES_INT_VECTOR
:
729 intvector_write_java (res
, status
);
732 bin_write_java (res
, status
);
735 int_write_java (res
, status
);
738 array_write_java (res
, status
);
742 table_write_java (res
, status
);
750 *status
= U_INTERNAL_PROGRAM_ERROR
;
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
) {
759 char fileName
[256] = {'\0'};
760 char className
[256]={'\0'};
761 char constructor
[1000] = { 0 };
765 bName
= (bundleName
==NULL
) ? "LocaleElements" : bundleName
;
766 pName
= (packageName
==NULL
)? "com.ibm.icu.impl.data" : packageName
;
768 uprv_strcpy(className
, bName
);
770 if(uprv_strcmp(srBundle
->fLocale
,"root")!=0){
771 uprv_strcat(className
,"_");
772 uprv_strcat(className
,srBundle
->fLocale
);
775 uprv_strcpy(fileName
, outputDir
);
776 if(outputDir
[uprv_strlen(outputDir
)-1] !=U_FILE_SEP_CHAR
){
777 uprv_strcat(fileName
,U_FILE_SEP_STRING
);
779 uprv_strcat(fileName
,className
);
780 uprv_strcat(fileName
,".java");
782 uprv_strcat(fileName
,className
);
783 uprv_strcat(fileName
,".java");
786 if (writtenFilename
) {
787 uprv_strncpy(writtenFilename
, fileName
, writtenFilenameLen
);
790 if (U_FAILURE(*status
)) {
794 out
= T_FileStream_open(fileName
,"w");
797 *status
= U_FILE_ACCESS_ERROR
;
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
));
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
));
810 T_FileStream_write(out
, javaClass1
, (int32_t)uprv_strlen(javaClass1
));
812 sprintf(constructor
,javaClassICU
,className
);
813 T_FileStream_write(out
, constructor
, (int32_t)uprv_strlen(constructor
));
816 if(outputEnc
&& *outputEnc
!='\0'){
817 /* store the output encoding */
819 conv
=ucnv_open(enc
,status
);
820 if(U_FAILURE(*status
)){
824 res_write_java(bundle
->fRoot
, status
);
826 T_FileStream_write(out
, closeClass
, (int32_t)uprv_strlen(closeClass
));
828 T_FileStream_close(out
);