2 *******************************************************************************
4 * Copyright (C) 2000-2015, 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 *******************************************************************************
21 #include "unicode/unistr.h"
24 #include "unicode/ures.h"
28 #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
, const 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(const UChar
*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(const StringResource
*res
,UErrorCode
*status
) {
309 (void)res
->getKeyString(srBundle
);
311 str_write_java(res
->getBuffer(), res
->length(), TRUE
, status
);
315 array_write_java(const ArrayResource
*res
, UErrorCode
*status
) {
318 const char* arr
="new String[] { \n";
319 struct SResource
*current
= NULL
;
320 UBool allStrings
= TRUE
;
322 if (U_FAILURE(*status
)) {
326 if (res
->fCount
> 0) {
328 current
= res
->fFirst
;
330 while(current
!= NULL
){
331 if(!current
->isString()){
335 current
= current
->fNext
;
338 current
= res
->fFirst
;
339 if(allStrings
==FALSE
){
340 const char* object
= "new Object[]{\n";
342 T_FileStream_write(out
, object
, (int32_t)uprv_strlen(object
));
346 T_FileStream_write(out
, arr
, (int32_t)uprv_strlen(arr
));
349 while (current
!= NULL
) {
350 /*if(current->isString()){
353 res_write_java(current
, status
);
354 if(U_FAILURE(*status
)){
358 current
= current
->fNext
;
360 T_FileStream_write(out
,"\n",1);
364 T_FileStream_write(out
,"},\n",3);
368 T_FileStream_write(out
,arr
,(int32_t)uprv_strlen(arr
));
370 T_FileStream_write(out
,"},\n",3);
375 intvector_write_java(const IntVectorResource
*res
, UErrorCode
* /*status*/) {
377 const char* intArr
= "new int[] {\n";
378 /* const char* intC = "new Integer("; */
379 const char* stringArr
= "new String[]{\n";
380 const char *resname
= res
->getKeyString(srBundle
);
386 if(resname
!= NULL
&& uprv_strcmp(resname
,"DateTimeElements")==0){
387 T_FileStream_write(out
, stringArr
, (int32_t)uprv_strlen(stringArr
));
389 for(i
= 0; i
<res
->fCount
; i
++) {
391 len
=itostr(buf
,res
->fArray
[i
],10,0);
392 T_FileStream_write(out
,"\"",1);
393 T_FileStream_write(out
,buf
,len
);
394 T_FileStream_write(out
,"\",",2);
395 T_FileStream_write(out
,"\n",1);
398 T_FileStream_write(out
, intArr
, (int32_t)uprv_strlen(intArr
));
400 for(i
= 0; i
<res
->fCount
; i
++) {
402 /* T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC)); */
403 len
=itostr(buf
,res
->fArray
[i
],10,0);
404 T_FileStream_write(out
,buf
,len
);
405 /* T_FileStream_write(out,"),",2); */
406 /* T_FileStream_write(out,"\n",1); */
407 T_FileStream_write(out
,",\n",2);
412 T_FileStream_write(out
,"},\n",3);
416 int_write_java(const IntResource
*res
, UErrorCode
* /*status*/) {
417 const char* intC
= "new Integer(";
422 /* write the binary data */
424 T_FileStream_write(out
, intC
, (int32_t)uprv_strlen(intC
));
425 len
=itostr(buf
, res
->fValue
, 10, 0);
426 T_FileStream_write(out
,buf
,len
);
427 T_FileStream_write(out
,"),\n",3 );
432 bytes_write_java(const BinaryResource
*res
, UErrorCode
* /*status*/) {
433 const char* type
= "new byte[] {";
434 const char* byteDecl
= "%i, ";
435 char byteBuffer
[100] = { 0 };
436 uint8_t* byteArray
= NULL
;
437 int byteIterator
= 0;
439 int32_t srcLen
=res
->fLength
;
443 byteArray
= res
->fData
;
446 T_FileStream_write(out
, type
, (int32_t)uprv_strlen(type
));
447 T_FileStream_write(out
, "\n", 1);
450 for (;byteIterator
<srcLen
;byteIterator
++)
452 if (byteIterator%16
== 0)
457 if (byteArray
[byteIterator
] < 128)
459 sprintf(byteBuffer
, byteDecl
, byteArray
[byteIterator
]);
463 sprintf(byteBuffer
, byteDecl
, (byteArray
[byteIterator
]-256));
466 T_FileStream_write(out
, byteBuffer
, (int32_t)uprv_strlen(byteBuffer
));
468 if (byteIterator%16
== 15)
470 T_FileStream_write(out
, "\n", 1);
475 if (((byteIterator
-1)%16
) != 15)
477 T_FileStream_write(out
, "\n", 1);
482 T_FileStream_write(out
, "},\n", 3);
489 T_FileStream_write(out
,type
,(int32_t)uprv_strlen(type
));
490 T_FileStream_write(out
,"},\n",3);
495 static UBool start
= TRUE
;
498 table_write_java(const TableResource
*res
, UErrorCode
*status
) {
500 struct SResource
*current
= NULL
;
501 const char* obj
= "new Object[][]{\n";
503 if (U_FAILURE(*status
)) {
507 if (res
->fCount
> 0) {
510 T_FileStream_write(out
, obj
, (int32_t)uprv_strlen(obj
));
514 current
= res
->fFirst
;
518 while (current
!= NULL
) {
519 const char *currentKeyString
= current
->getKeyString(srBundle
);
521 assert(i
< res
->fCount
);
524 T_FileStream_write(out
, openBrace
, 2);
530 if(currentKeyString
!= NULL
) {
531 T_FileStream_write(out
, "\"", 1);
532 T_FileStream_write(out
, currentKeyString
,
533 (int32_t)uprv_strlen(currentKeyString
));
534 T_FileStream_write(out
, "\",\n", 2);
536 T_FileStream_write(out
, "\n", 1);
538 res_write_java(current
, status
);
539 if(U_FAILURE(*status
)){
543 current
= current
->fNext
;
546 T_FileStream_write(out
, "},\n", 3);
551 T_FileStream_write(out
, "},\n", 3);
556 T_FileStream_write(out
,obj
,(int32_t)uprv_strlen(obj
));
559 T_FileStream_write(out
,"},\n",3);
566 res_write_java(struct SResource
*res
,UErrorCode
*status
) {
568 if (U_FAILURE(*status
)) {
573 switch (res
->fType
) {
575 string_write_java (static_cast<const StringResource
*>(res
), status
);
578 printf("Encountered unsupported resource type %d of alias\n", res
->fType
);
579 *status
= U_UNSUPPORTED_ERROR
;
581 case URES_INT_VECTOR
:
582 intvector_write_java (static_cast<const IntVectorResource
*>(res
), status
);
585 bytes_write_java (static_cast<const BinaryResource
*>(res
), status
);
588 int_write_java (static_cast<const IntResource
*>(res
), status
);
591 array_write_java (static_cast<const ArrayResource
*>(res
), status
);
594 table_write_java (static_cast<const TableResource
*>(res
), status
);
601 *status
= U_INTERNAL_PROGRAM_ERROR
;
605 bundle_write_java(struct SRBRoot
*bundle
, const char *outputDir
,const char* outputEnc
,
606 char *writtenFilename
, int writtenFilenameLen
,
607 const char* packageName
, const char* bundleName
,
608 UErrorCode
*status
) {
610 char fileName
[256] = {'\0'};
611 char className
[256]={'\0'};
612 /*char constructor[1000] = { 0 };*/
614 /*outDir = outputDir;*/
616 start
= TRUE
; /* Reset the start indictor*/
618 bName
= (bundleName
==NULL
) ? "LocaleElements" : bundleName
;
619 pName
= (packageName
==NULL
)? "com.ibm.icu.impl.data" : packageName
;
621 uprv_strcpy(className
, bName
);
623 if(uprv_strcmp(srBundle
->fLocale
,"root")!=0){
624 uprv_strcat(className
,"_");
625 uprv_strcat(className
,srBundle
->fLocale
);
628 uprv_strcpy(fileName
, outputDir
);
629 if(outputDir
[uprv_strlen(outputDir
)-1] !=U_FILE_SEP_CHAR
){
630 uprv_strcat(fileName
,U_FILE_SEP_STRING
);
632 uprv_strcat(fileName
,className
);
633 uprv_strcat(fileName
,".java");
635 uprv_strcat(fileName
,className
);
636 uprv_strcat(fileName
,".java");
639 if (writtenFilename
) {
640 uprv_strncpy(writtenFilename
, fileName
, writtenFilenameLen
);
643 if (U_FAILURE(*status
)) {
647 out
= T_FileStream_open(fileName
,"w");
650 *status
= U_FILE_ACCESS_ERROR
;
653 if(getIncludeCopyright()){
654 T_FileStream_write(out
, copyRight
, (int32_t)uprv_strlen(copyRight
));
655 T_FileStream_write(out
, warningMsg
, (int32_t)uprv_strlen(warningMsg
));
657 T_FileStream_write(out
,"package ",(int32_t)uprv_strlen("package "));
658 T_FileStream_write(out
,pName
,(int32_t)uprv_strlen(pName
));
659 T_FileStream_write(out
,";\n\n",3);
660 T_FileStream_write(out
, javaClass
, (int32_t)uprv_strlen(javaClass
));
661 T_FileStream_write(out
, className
, (int32_t)uprv_strlen(className
));
662 T_FileStream_write(out
, javaClass1
, (int32_t)uprv_strlen(javaClass1
));
665 T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1));
667 sprintf(constructor,javaClassICU,className);
668 T_FileStream_write(out, constructor, (int32_t)uprv_strlen(constructor));
672 if(outputEnc
&& *outputEnc
!='\0'){
673 /* store the output encoding */
675 conv
=ucnv_open(enc
,status
);
676 if(U_FAILURE(*status
)){
680 res_write_java(bundle
->fRoot
, status
);
682 T_FileStream_write(out
, closeClass
, (int32_t)uprv_strlen(closeClass
));
684 T_FileStream_close(out
);