2 *******************************************************************************
4 * Copyright (C) 2000-2009, 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 * 02/12/08 Spieth Fix errant 'new Object[][]{' insertion
16 * 02/19/08 Spieth Removed ICUListResourceBundle dependancy
17 *******************************************************************************
23 #include "unicode/ures.h"
27 #include "unicode/ucnv.h"
33 #include "unicode/ustring.h"
35 void res_write_java(struct SResource
*res
,UErrorCode
*status
);
38 static const char copyRight
[] =
40 " *******************************************************************************\n"
42 " * Copyright (C) International Business Machines\n"
43 " * Corporation and others. All Rights Reserved.\n"
45 " *******************************************************************************\n"
48 " * $" "Revision: $ \n"
49 " *******************************************************************************\n"
51 static const char warningMsg
[] =
52 "/*********************************************************************\n"
53 "######################################################################\n"
55 " WARNING: This file is generated by genrb Version " GENRB_VERSION
".\n"
56 " If you edit this file, please make sure that, the source\n"
57 " of this file (XXXX.txt in LocaleElements_XXXX.java)\n"
59 "######################################################################\n"
60 " *********************************************************************\n"
62 static const char* openBrace
="{\n";
63 static const char* closeClass
=" };\n"
66 static const char* javaClass
= "import java.util.ListResourceBundle;\n\n"
69 static const char* javaClass1
= " extends ListResourceBundle {\n\n"
71 " * Overrides ListResourceBundle \n"
73 " public final Object[][] getContents() { \n"
76 " private static Object[][] contents = {\n";
77 /*static const char* javaClassICU= " extends ListResourceBundle {\n\n"
79 " super.contents = data;\n"
81 " static final Object[][] data = new Object[][] { \n";*/
82 static int tabCount
= 3;
84 static FileStream
* out
=NULL
;
85 static struct SRBRoot
* srBundle
;
86 static const char* outDir
= NULL
;
88 static const char* bName
=NULL
;
89 static const char* pName
=NULL
;
91 static void write_tabs(FileStream
* os
){
93 for(;i
<=tabCount
;i
++){
94 T_FileStream_write(os
," ",4);
100 static const char* enc
="";
101 static UConverter
* conv
= NULL
;
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 idx
= strrch(current
,add
,'\\');
269 if(current
[num
]=='\\'){
275 if ((idx
-num
)%2
==0) {
278 seqLen
= (current
[idx
+1]=='u') ? 6 : 2;
279 if ((add
-idx
) < 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);
306 /* Writing Functions */
308 string_write_java(struct SResource
*res
,UErrorCode
*status
) {
309 char resKeyBuffer
[8];
310 const char *resname
= res_getKeyString(srBundle
, res
, resKeyBuffer
);
312 str_write_java(res
->u
.fString
.fChars
,res
->u
.fString
.fLength
,TRUE
,status
);
314 if(resname
!= NULL
&& uprv_strcmp(resname
,"Rule")==0)
316 UChar
* buf
= (UChar
*) uprv_malloc(sizeof(UChar
)*res
->u
.fString
.fLength
);
317 uprv_memcpy(buf
,res
->u
.fString
.fChars
,res
->u
.fString
.fLength
);
324 array_write_java( struct SResource
*res
, UErrorCode
*status
) {
327 const char* arr
="new String[] { \n";
328 struct SResource
*current
= NULL
;
329 struct SResource
*first
=NULL
;
330 UBool decrementTabs
= FALSE
;
331 UBool allStrings
= TRUE
;
333 if (U_FAILURE(*status
)) {
337 if (res
->u
.fArray
.fCount
> 0) {
339 current
= res
->u
.fArray
.fFirst
;
341 while(current
!= NULL
){
342 if(current
->fType
!=URES_STRING
){
346 current
= current
->fNext
;
349 current
= res
->u
.fArray
.fFirst
;
350 if(allStrings
==FALSE
){
351 const char* object
= "new Object[]{\n";
353 T_FileStream_write(out
, object
, (int32_t)uprv_strlen(object
));
355 decrementTabs
= TRUE
;
358 T_FileStream_write(out
, arr
, (int32_t)uprv_strlen(arr
));
362 while (current
!= NULL
) {
363 /*if(current->fType==URES_STRING){
366 res_write_java(current
, status
);
367 if(U_FAILURE(*status
)){
371 current
= current
->fNext
;
373 T_FileStream_write(out
,"\n",1);
377 T_FileStream_write(out
,"},\n",3);
381 T_FileStream_write(out
,arr
,(int32_t)uprv_strlen(arr
));
383 T_FileStream_write(out
,"},\n",3);
388 intvector_write_java( struct SResource
*res
, UErrorCode
*status
) {
390 const char* intArr
= "new int[] {\n";
391 /* const char* intC = "new Integer("; */
392 const char* stringArr
= "new String[]{\n";
393 char resKeyBuffer
[8];
394 const char *resname
= res_getKeyString(srBundle
, res
, resKeyBuffer
);
400 if(resname
!= NULL
&& uprv_strcmp(resname
,"DateTimeElements")==0){
401 T_FileStream_write(out
, stringArr
, (int32_t)uprv_strlen(stringArr
));
403 for(i
= 0; i
<res
->u
.fIntVector
.fCount
; i
++) {
405 len
=itostr(buf
,res
->u
.fIntVector
.fArray
[i
],10,0);
406 T_FileStream_write(out
,"\"",1);
407 T_FileStream_write(out
,buf
,len
);
408 T_FileStream_write(out
,"\",",2);
409 T_FileStream_write(out
,"\n",1);
412 T_FileStream_write(out
, intArr
, (int32_t)uprv_strlen(intArr
));
414 for(i
= 0; i
<res
->u
.fIntVector
.fCount
; i
++) {
416 /* T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC)); */
417 len
=itostr(buf
,res
->u
.fIntVector
.fArray
[i
],10,0);
418 T_FileStream_write(out
,buf
,len
);
419 /* T_FileStream_write(out,"),",2); */
420 /* T_FileStream_write(out,"\n",1); */
421 T_FileStream_write(out
,",\n",2);
426 T_FileStream_write(out
,"},\n",3);
430 int_write_java(struct SResource
*res
,UErrorCode
*status
) {
431 const char* intC
= "new Integer(";
436 /* write the binary data */
438 T_FileStream_write(out
, intC
, (int32_t)uprv_strlen(intC
));
439 len
=itostr(buf
, res
->u
.fIntValue
.fValue
, 10, 0);
440 T_FileStream_write(out
,buf
,len
);
441 T_FileStream_write(out
,"),\n",3 );
446 bytes_write_java( struct SResource
*res
, UErrorCode
*status
) {
447 const char* type
= "new byte[] {";
448 const char* byteDecl
= "%i, ";
449 char byteBuffer
[100] = { 0 };
450 uint8_t* byteArray
= NULL
;
451 int byteIterator
= 0;
453 int32_t srcLen
=res
->u
.fBinaryValue
.fLength
;
457 byteArray
= res
->u
.fBinaryValue
.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(struct SResource
*res
, UErrorCode
*status
) {
514 UBool allStrings
=TRUE
;
515 struct SResource
*current
= NULL
;
516 struct SResource
*save
= NULL
;
517 const char* obj
= "new Object[][]{\n";
519 if (U_FAILURE(*status
)) {
523 if (res
->u
.fTable
.fCount
> 0) {
526 T_FileStream_write(out
, obj
, (int32_t)uprv_strlen(obj
));
530 save
= current
= res
->u
.fTable
.fFirst
;
534 while (current
!= NULL
) {
535 char currentKeyBuffer
[8];
536 const char *currentKeyString
= res_getKeyString(srBundle
, current
, currentKeyBuffer
);
538 assert(i
< res
->u
.fTable
.fCount
);
541 T_FileStream_write(out
, openBrace
, 2);
548 if(currentKeyString
!= NULL
) {
549 T_FileStream_write(out
, "\"", 1);
550 T_FileStream_write(out
, currentKeyString
,
551 (int32_t)uprv_strlen(currentKeyString
));
552 T_FileStream_write(out
, "\",\n", 2);
554 T_FileStream_write(out
, "\n", 1);
556 res_write_java(current
, status
);
557 if(U_FAILURE(*status
)){
561 current
= current
->fNext
;
564 T_FileStream_write(out
, "},\n", 3);
569 T_FileStream_write(out
, "},\n", 3);
574 T_FileStream_write(out
,obj
,(int32_t)uprv_strlen(obj
));
577 T_FileStream_write(out
,"},\n",3);
584 res_write_java(struct SResource
*res
,UErrorCode
*status
) {
586 if (U_FAILURE(*status
)) {
591 switch (res
->fType
) {
593 string_write_java (res
, status
);
596 printf("Encountered unsupported resource type %d of alias\n", res
->fType
);
597 *status
= U_UNSUPPORTED_ERROR
;
599 case URES_INT_VECTOR
:
600 intvector_write_java (res
, status
);
603 bytes_write_java (res
, status
);
606 int_write_java (res
, status
);
609 array_write_java (res
, status
);
612 table_write_java (res
, status
);
619 *status
= U_INTERNAL_PROGRAM_ERROR
;
623 bundle_write_java(struct SRBRoot
*bundle
, const char *outputDir
,const char* outputEnc
,
624 char *writtenFilename
, int writtenFilenameLen
,
625 const char* packageName
, const char* bundleName
,
626 UErrorCode
*status
) {
628 char fileName
[256] = {'\0'};
629 char className
[256]={'\0'};
630 /*char constructor[1000] = { 0 };*/
634 start
= TRUE
; /* Reset the start indictor*/
636 bName
= (bundleName
==NULL
) ? "LocaleElements" : bundleName
;
637 pName
= (packageName
==NULL
)? "com.ibm.icu.impl.data" : packageName
;
639 uprv_strcpy(className
, bName
);
641 if(uprv_strcmp(srBundle
->fLocale
,"root")!=0){
642 uprv_strcat(className
,"_");
643 uprv_strcat(className
,srBundle
->fLocale
);
646 uprv_strcpy(fileName
, outputDir
);
647 if(outputDir
[uprv_strlen(outputDir
)-1] !=U_FILE_SEP_CHAR
){
648 uprv_strcat(fileName
,U_FILE_SEP_STRING
);
650 uprv_strcat(fileName
,className
);
651 uprv_strcat(fileName
,".java");
653 uprv_strcat(fileName
,className
);
654 uprv_strcat(fileName
,".java");
657 if (writtenFilename
) {
658 uprv_strncpy(writtenFilename
, fileName
, writtenFilenameLen
);
661 if (U_FAILURE(*status
)) {
665 out
= T_FileStream_open(fileName
,"w");
668 *status
= U_FILE_ACCESS_ERROR
;
671 if(getIncludeCopyright()){
672 T_FileStream_write(out
, copyRight
, (int32_t)uprv_strlen(copyRight
));
673 T_FileStream_write(out
, warningMsg
, (int32_t)uprv_strlen(warningMsg
));
675 T_FileStream_write(out
,"package ",(int32_t)uprv_strlen("package "));
676 T_FileStream_write(out
,pName
,(int32_t)uprv_strlen(pName
));
677 T_FileStream_write(out
,";\n\n",3);
678 T_FileStream_write(out
, javaClass
, (int32_t)uprv_strlen(javaClass
));
679 T_FileStream_write(out
, className
, (int32_t)uprv_strlen(className
));
680 T_FileStream_write(out
, javaClass1
, (int32_t)uprv_strlen(javaClass1
));
683 T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1));
685 sprintf(constructor,javaClassICU,className);
686 T_FileStream_write(out, constructor, (int32_t)uprv_strlen(constructor));
690 if(outputEnc
&& *outputEnc
!='\0'){
691 /* store the output encoding */
693 conv
=ucnv_open(enc
,status
);
694 if(U_FAILURE(*status
)){
698 res_write_java(bundle
->fRoot
, status
);
700 T_FileStream_write(out
, closeClass
, (int32_t)uprv_strlen(closeClass
));
702 T_FileStream_close(out
);