2 *******************************************************************************
4 * Copyright (C) 2000-2012, 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"
32 #include "unicode/ustring.h"
34 void res_write_java(struct SResource *res,UErrorCode *status);
37 static const char copyRight[] =
39 " *******************************************************************************\n"
41 " * Copyright (C) International Business Machines\n"
42 " * Corporation and others. All Rights Reserved.\n"
44 " *******************************************************************************\n"
47 " * $" "Revision: $ \n"
48 " *******************************************************************************\n"
50 static const char warningMsg[] =
51 "/*********************************************************************\n"
52 "######################################################################\n"
54 " WARNING: This file is generated by genrb Version " GENRB_VERSION ".\n"
55 " If you edit this file, please make sure that, the source\n"
56 " of this file (XXXX.txt in LocaleElements_XXXX.java)\n"
58 "######################################################################\n"
59 " *********************************************************************\n"
61 static const char* openBrace="{\n";
62 static const char* closeClass=" };\n"
65 static const char* javaClass = "import java.util.ListResourceBundle;\n\n"
68 static const char* javaClass1= " extends ListResourceBundle {\n\n"
70 " * Overrides ListResourceBundle \n"
72 " public final Object[][] getContents() { \n"
75 " private static Object[][] contents = {\n";
76 /*static const char* javaClassICU= " extends ListResourceBundle {\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;
103 uCharsToChars( char* target,int32_t targetLen, UChar* source, int32_t sourceLen,UErrorCode* status){
107 if (source[i] == '\n') {
108 if (j + 2 < targetLen) {
109 uprv_strcat(target, "\\n");
112 }else if(source[i]==0x0D){
114 uprv_strcat(target,"\\f");
117 }else if(source[i] == '"'){
118 if(source[i-1]=='\''){
120 uprv_strcat(target,"\\");
121 target[j+1]= (char)source[i];
124 }else if(source[i-1]!='\\'){
127 uprv_strcat(target,"\\");
128 target[j+1]= (char)source[i];
131 }else if(source[i-1]=='\\'){
132 target[j++]= (char)source[i];
134 }else if(source[i]=='\\'){
147 uprv_strcat(target,"\\\\");
154 uprv_strcat(target,"\\\\\\");
160 target[j]=(char)source[i];
167 uprv_strcat(target,"\\\\");
171 }else if(source[i]>=0x20 && source[i]<0x7F/*ASCII*/){
173 target[j] = (char) source[i];
177 if(*enc =='\0' || source[i]==0x0000){
178 uprv_strcpy(str,"\\u");
179 itostr(str+2,source[i],16,4);
181 uprv_strcat(target,str);
186 int retVal=ucnv_fromUChars(conv,dest,30,source+i,1,status);
187 if(U_FAILURE(*status)){
190 if(j+retVal<targetLen){
191 uprv_strcat(target,dest);
203 strrch(const char* source,uint32_t sourceLen,char find){
204 const char* tSourceEnd =source + (sourceLen-1);
205 while(tSourceEnd>= source){
206 if(*tSourceEnd==find){
207 return (uint32_t)(tSourceEnd-source);
211 return (uint32_t)(tSourceEnd-source);
214 static int32_t getColumnCount(int32_t len){
215 int32_t columnCount = 80;
216 int32_t maxLines = 3000;
217 int32_t adjustedLen = len*5; /* assume that every codepoint is represented in \uXXXX format*/
219 * calculate the number of lines that
220 * may be required if column count is 80
222 if (maxLines < (adjustedLen / columnCount) ){
223 columnCount = adjustedLen / maxLines;
228 str_write_java( uint16_t* src, int32_t srcLen, UBool printEndLine, UErrorCode *status){
230 uint32_t length = srcLen*8;
232 uint32_t columnCount;
233 char* buf = (char*) malloc(sizeof(char)*length);
236 *status = U_MEMORY_ALLOCATION_ERROR;
240 columnCount = getColumnCount(srcLen);
241 memset(buf,0,length);
243 bufLen = uCharsToChars(buf,length,src,srcLen,status);
248 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 idx = strrch(current,add,'\\');
268 if(current[num]=='\\'){
274 if ((idx-num)%2==0) {
277 seqLen = (current[idx+1]=='u') ? 6 : 2;
278 if ((add-idx) < 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 /* Writing Functions */
307 string_write_java(struct SResource *res,UErrorCode *status) {
308 char resKeyBuffer[8];
309 const char *resname = res_getKeyString(srBundle, res, resKeyBuffer);
311 str_write_java(res->u.fString.fChars,res->u.fString.fLength,TRUE,status);
313 if(resname != NULL && uprv_strcmp(resname,"Rule")==0)
315 UChar* buf = (UChar*) uprv_malloc(sizeof(UChar)*res->u.fString.fLength);
316 uprv_memcpy(buf,res->u.fString.fChars,res->u.fString.fLength);
323 array_write_java( struct SResource *res, UErrorCode *status) {
326 const char* arr ="new String[] { \n";
327 struct SResource *current = NULL;
328 UBool allStrings = TRUE;
330 if (U_FAILURE(*status)) {
334 if (res->u.fArray.fCount > 0) {
336 current = res->u.fArray.fFirst;
338 while(current != NULL){
339 if(current->fType!=URES_STRING){
343 current= current->fNext;
346 current = res->u.fArray.fFirst;
347 if(allStrings==FALSE){
348 const char* object = "new Object[]{\n";
350 T_FileStream_write(out, object, (int32_t)uprv_strlen(object));
354 T_FileStream_write(out, arr, (int32_t)uprv_strlen(arr));
357 while (current != NULL) {
358 /*if(current->fType==URES_STRING){
361 res_write_java(current, status);
362 if(U_FAILURE(*status)){
366 current = current->fNext;
368 T_FileStream_write(out,"\n",1);
372 T_FileStream_write(out,"},\n",3);
376 T_FileStream_write(out,arr,(int32_t)uprv_strlen(arr));
378 T_FileStream_write(out,"},\n",3);
383 intvector_write_java( struct SResource *res, UErrorCode *status) {
385 const char* intArr = "new int[] {\n";
386 /* const char* intC = "new Integer("; */
387 const char* stringArr = "new String[]{\n";
388 char resKeyBuffer[8];
389 const char *resname = res_getKeyString(srBundle, res, resKeyBuffer);
395 if(resname != NULL && uprv_strcmp(resname,"DateTimeElements")==0){
396 T_FileStream_write(out, stringArr, (int32_t)uprv_strlen(stringArr));
398 for(i = 0; i<res->u.fIntVector.fCount; i++) {
400 len=itostr(buf,res->u.fIntVector.fArray[i],10,0);
401 T_FileStream_write(out,"\"",1);
402 T_FileStream_write(out,buf,len);
403 T_FileStream_write(out,"\",",2);
404 T_FileStream_write(out,"\n",1);
407 T_FileStream_write(out, intArr, (int32_t)uprv_strlen(intArr));
409 for(i = 0; i<res->u.fIntVector.fCount; i++) {
411 /* T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC)); */
412 len=itostr(buf,res->u.fIntVector.fArray[i],10,0);
413 T_FileStream_write(out,buf,len);
414 /* T_FileStream_write(out,"),",2); */
415 /* T_FileStream_write(out,"\n",1); */
416 T_FileStream_write(out,",\n",2);
421 T_FileStream_write(out,"},\n",3);
425 int_write_java(struct SResource *res,UErrorCode *status) {
426 const char* intC = "new Integer(";
431 /* write the binary data */
433 T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC));
434 len=itostr(buf, res->u.fIntValue.fValue, 10, 0);
435 T_FileStream_write(out,buf,len);
436 T_FileStream_write(out,"),\n",3 );
441 bytes_write_java( struct SResource *res, UErrorCode *status) {
442 const char* type = "new byte[] {";
443 const char* byteDecl = "%i, ";
444 char byteBuffer[100] = { 0 };
445 uint8_t* byteArray = NULL;
446 int byteIterator = 0;
448 int32_t srcLen=res->u.fBinaryValue.fLength;
452 byteArray = res->u.fBinaryValue.fData;
455 T_FileStream_write(out, type, (int32_t)uprv_strlen(type));
456 T_FileStream_write(out, "\n", 1);
459 for (;byteIterator<srcLen;byteIterator++)
461 if (byteIterator%16 == 0)
466 if (byteArray[byteIterator] < 128)
468 sprintf(byteBuffer, byteDecl, byteArray[byteIterator]);
472 sprintf(byteBuffer, byteDecl, (byteArray[byteIterator]-256));
475 T_FileStream_write(out, byteBuffer, (int32_t)uprv_strlen(byteBuffer));
477 if (byteIterator%16 == 15)
479 T_FileStream_write(out, "\n", 1);
484 if (((byteIterator-1)%16) != 15)
486 T_FileStream_write(out, "\n", 1);
491 T_FileStream_write(out, "},\n", 3);
498 T_FileStream_write(out,type,(int32_t)uprv_strlen(type));
499 T_FileStream_write(out,"},\n",3);
504 static UBool start = TRUE;
507 table_write_java(struct SResource *res, UErrorCode *status) {
509 struct SResource *current = NULL;
510 const char* obj = "new Object[][]{\n";
512 if (U_FAILURE(*status)) {
516 if (res->u.fTable.fCount > 0) {
519 T_FileStream_write(out, obj, (int32_t)uprv_strlen(obj));
523 current = res->u.fTable.fFirst;
527 while (current != NULL) {
528 char currentKeyBuffer[8];
529 const char *currentKeyString = res_getKeyString(srBundle, current, currentKeyBuffer);
531 assert(i < res->u.fTable.fCount);
534 T_FileStream_write(out, openBrace, 2);
540 if(currentKeyString != NULL) {
541 T_FileStream_write(out, "\"", 1);
542 T_FileStream_write(out, currentKeyString,
543 (int32_t)uprv_strlen(currentKeyString));
544 T_FileStream_write(out, "\",\n", 2);
546 T_FileStream_write(out, "\n", 1);
548 res_write_java(current, status);
549 if(U_FAILURE(*status)){
553 current = current->fNext;
556 T_FileStream_write(out, "},\n", 3);
561 T_FileStream_write(out, "},\n", 3);
566 T_FileStream_write(out,obj,(int32_t)uprv_strlen(obj));
569 T_FileStream_write(out,"},\n",3);
576 res_write_java(struct SResource *res,UErrorCode *status) {
578 if (U_FAILURE(*status)) {
583 switch (res->fType) {
585 string_write_java (res, status);
588 printf("Encountered unsupported resource type %d of alias\n", res->fType);
589 *status = U_UNSUPPORTED_ERROR;
591 case URES_INT_VECTOR:
592 intvector_write_java (res, status);
595 bytes_write_java (res, status);
598 int_write_java (res, status);
601 array_write_java (res, status);
604 table_write_java (res, status);
611 *status = U_INTERNAL_PROGRAM_ERROR;
615 bundle_write_java(struct SRBRoot *bundle, const char *outputDir,const char* outputEnc,
616 char *writtenFilename, int writtenFilenameLen,
617 const char* packageName, const char* bundleName,
618 UErrorCode *status) {
620 char fileName[256] = {'\0'};
621 char className[256]={'\0'};
622 /*char constructor[1000] = { 0 };*/
624 /*outDir = outputDir;*/
626 start = TRUE; /* Reset the start indictor*/
628 bName = (bundleName==NULL) ? "LocaleElements" : bundleName;
629 pName = (packageName==NULL)? "com.ibm.icu.impl.data" : packageName;
631 uprv_strcpy(className, bName);
633 if(uprv_strcmp(srBundle->fLocale,"root")!=0){
634 uprv_strcat(className,"_");
635 uprv_strcat(className,srBundle->fLocale);
638 uprv_strcpy(fileName, outputDir);
639 if(outputDir[uprv_strlen(outputDir)-1] !=U_FILE_SEP_CHAR){
640 uprv_strcat(fileName,U_FILE_SEP_STRING);
642 uprv_strcat(fileName,className);
643 uprv_strcat(fileName,".java");
645 uprv_strcat(fileName,className);
646 uprv_strcat(fileName,".java");
649 if (writtenFilename) {
650 uprv_strncpy(writtenFilename, fileName, writtenFilenameLen);
653 if (U_FAILURE(*status)) {
657 out= T_FileStream_open(fileName,"w");
660 *status = U_FILE_ACCESS_ERROR;
663 if(getIncludeCopyright()){
664 T_FileStream_write(out, copyRight, (int32_t)uprv_strlen(copyRight));
665 T_FileStream_write(out, warningMsg, (int32_t)uprv_strlen(warningMsg));
667 T_FileStream_write(out,"package ",(int32_t)uprv_strlen("package "));
668 T_FileStream_write(out,pName,(int32_t)uprv_strlen(pName));
669 T_FileStream_write(out,";\n\n",3);
670 T_FileStream_write(out, javaClass, (int32_t)uprv_strlen(javaClass));
671 T_FileStream_write(out, className, (int32_t)uprv_strlen(className));
672 T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1));
675 T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1));
677 sprintf(constructor,javaClassICU,className);
678 T_FileStream_write(out, constructor, (int32_t)uprv_strlen(constructor));
682 if(outputEnc && *outputEnc!='\0'){
683 /* store the output encoding */
685 conv=ucnv_open(enc,status);
686 if(U_FAILURE(*status)){
690 res_write_java(bundle->fRoot, status);
692 T_FileStream_write(out, closeClass, (int32_t)uprv_strlen(closeClass));
694 T_FileStream_close(out);