Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 2000-2012, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ******************************************************************************* |
michael@0 | 8 | * |
michael@0 | 9 | * File wrtjava.c |
michael@0 | 10 | * |
michael@0 | 11 | * Modification History: |
michael@0 | 12 | * |
michael@0 | 13 | * Date Name Description |
michael@0 | 14 | * 01/11/02 Ram Creation. |
michael@0 | 15 | * 02/12/08 Spieth Fix errant 'new Object[][]{' insertion |
michael@0 | 16 | * 02/19/08 Spieth Removed ICUListResourceBundle dependancy |
michael@0 | 17 | ******************************************************************************* |
michael@0 | 18 | */ |
michael@0 | 19 | |
michael@0 | 20 | #include <assert.h> |
michael@0 | 21 | #include "reslist.h" |
michael@0 | 22 | #include "unewdata.h" |
michael@0 | 23 | #include "unicode/ures.h" |
michael@0 | 24 | #include "errmsg.h" |
michael@0 | 25 | #include "filestrm.h" |
michael@0 | 26 | #include "cstring.h" |
michael@0 | 27 | #include "unicode/ucnv.h" |
michael@0 | 28 | #include "genrb.h" |
michael@0 | 29 | #include "rle.h" |
michael@0 | 30 | #include "uhash.h" |
michael@0 | 31 | #include "uresimp.h" |
michael@0 | 32 | #include "unicode/ustring.h" |
michael@0 | 33 | |
michael@0 | 34 | void res_write_java(struct SResource *res,UErrorCode *status); |
michael@0 | 35 | |
michael@0 | 36 | |
michael@0 | 37 | static const char copyRight[] = |
michael@0 | 38 | "/* \n" |
michael@0 | 39 | " *******************************************************************************\n" |
michael@0 | 40 | " *\n" |
michael@0 | 41 | " * Copyright (C) International Business Machines\n" |
michael@0 | 42 | " * Corporation and others. All Rights Reserved.\n" |
michael@0 | 43 | " *\n" |
michael@0 | 44 | " *******************************************************************************\n" |
michael@0 | 45 | " * $" "Source: $ \n" |
michael@0 | 46 | " * $" "Date: $ \n" |
michael@0 | 47 | " * $" "Revision: $ \n" |
michael@0 | 48 | " *******************************************************************************\n" |
michael@0 | 49 | " */\n\n"; |
michael@0 | 50 | static const char warningMsg[] = |
michael@0 | 51 | "/*********************************************************************\n" |
michael@0 | 52 | "######################################################################\n" |
michael@0 | 53 | "\n" |
michael@0 | 54 | " WARNING: This file is generated by genrb Version " GENRB_VERSION ".\n" |
michael@0 | 55 | " If you edit this file, please make sure that, the source\n" |
michael@0 | 56 | " of this file (XXXX.txt in LocaleElements_XXXX.java)\n" |
michael@0 | 57 | " is also edited.\n" |
michael@0 | 58 | "######################################################################\n" |
michael@0 | 59 | " *********************************************************************\n" |
michael@0 | 60 | " */\n\n"; |
michael@0 | 61 | static const char* openBrace="{\n"; |
michael@0 | 62 | static const char* closeClass=" };\n" |
michael@0 | 63 | "}\n"; |
michael@0 | 64 | |
michael@0 | 65 | static const char* javaClass = "import java.util.ListResourceBundle;\n\n" |
michael@0 | 66 | "public class "; |
michael@0 | 67 | |
michael@0 | 68 | static const char* javaClass1= " extends ListResourceBundle {\n\n" |
michael@0 | 69 | " /**\n" |
michael@0 | 70 | " * Overrides ListResourceBundle \n" |
michael@0 | 71 | " */\n" |
michael@0 | 72 | " public final Object[][] getContents() { \n" |
michael@0 | 73 | " return contents;\n" |
michael@0 | 74 | " }\n\n" |
michael@0 | 75 | " private static Object[][] contents = {\n"; |
michael@0 | 76 | /*static const char* javaClassICU= " extends ListResourceBundle {\n\n" |
michael@0 | 77 | " public %s () {\n" |
michael@0 | 78 | " super.contents = data;\n" |
michael@0 | 79 | " }\n" |
michael@0 | 80 | " static final Object[][] data = new Object[][] { \n";*/ |
michael@0 | 81 | static int tabCount = 3; |
michael@0 | 82 | |
michael@0 | 83 | static FileStream* out=NULL; |
michael@0 | 84 | static struct SRBRoot* srBundle ; |
michael@0 | 85 | /*static const char* outDir = NULL;*/ |
michael@0 | 86 | |
michael@0 | 87 | static const char* bName=NULL; |
michael@0 | 88 | static const char* pName=NULL; |
michael@0 | 89 | |
michael@0 | 90 | static void write_tabs(FileStream* os){ |
michael@0 | 91 | int i=0; |
michael@0 | 92 | for(;i<=tabCount;i++){ |
michael@0 | 93 | T_FileStream_write(os," ",4); |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | #define ZERO 0x30 |
michael@0 | 98 | |
michael@0 | 99 | static const char* enc =""; |
michael@0 | 100 | static UConverter* conv = NULL; |
michael@0 | 101 | |
michael@0 | 102 | static int32_t |
michael@0 | 103 | uCharsToChars( char* target,int32_t targetLen, UChar* source, int32_t sourceLen,UErrorCode* status){ |
michael@0 | 104 | int i=0, j=0; |
michael@0 | 105 | char str[30]={'\0'}; |
michael@0 | 106 | while(i<sourceLen){ |
michael@0 | 107 | if (source[i] == '\n') { |
michael@0 | 108 | if (j + 2 < targetLen) { |
michael@0 | 109 | uprv_strcat(target, "\\n"); |
michael@0 | 110 | } |
michael@0 | 111 | j += 2; |
michael@0 | 112 | }else if(source[i]==0x0D){ |
michael@0 | 113 | if(j+2<targetLen){ |
michael@0 | 114 | uprv_strcat(target,"\\f"); |
michael@0 | 115 | } |
michael@0 | 116 | j+=2; |
michael@0 | 117 | }else if(source[i] == '"'){ |
michael@0 | 118 | if(source[i-1]=='\''){ |
michael@0 | 119 | if(j+2<targetLen){ |
michael@0 | 120 | uprv_strcat(target,"\\"); |
michael@0 | 121 | target[j+1]= (char)source[i]; |
michael@0 | 122 | } |
michael@0 | 123 | j+=2; |
michael@0 | 124 | }else if(source[i-1]!='\\'){ |
michael@0 | 125 | |
michael@0 | 126 | if(j+2<targetLen){ |
michael@0 | 127 | uprv_strcat(target,"\\"); |
michael@0 | 128 | target[j+1]= (char)source[i]; |
michael@0 | 129 | } |
michael@0 | 130 | j+=2; |
michael@0 | 131 | }else if(source[i-1]=='\\'){ |
michael@0 | 132 | target[j++]= (char)source[i]; |
michael@0 | 133 | } |
michael@0 | 134 | }else if(source[i]=='\\'){ |
michael@0 | 135 | if(i+1<sourceLen){ |
michael@0 | 136 | switch(source[i+1]){ |
michael@0 | 137 | case ',': |
michael@0 | 138 | case '!': |
michael@0 | 139 | case '?': |
michael@0 | 140 | case '#': |
michael@0 | 141 | case '.': |
michael@0 | 142 | case '%': |
michael@0 | 143 | case '&': |
michael@0 | 144 | case ':': |
michael@0 | 145 | case ';': |
michael@0 | 146 | if(j+2<targetLen){ |
michael@0 | 147 | uprv_strcat(target,"\\\\"); |
michael@0 | 148 | } |
michael@0 | 149 | j+=2; |
michael@0 | 150 | break; |
michael@0 | 151 | case '"': |
michael@0 | 152 | case '\'': |
michael@0 | 153 | if(j+3<targetLen){ |
michael@0 | 154 | uprv_strcat(target,"\\\\\\"); |
michael@0 | 155 | } |
michael@0 | 156 | j+=3; |
michael@0 | 157 | break; |
michael@0 | 158 | default : |
michael@0 | 159 | if(j<targetLen){ |
michael@0 | 160 | target[j]=(char)source[i]; |
michael@0 | 161 | } |
michael@0 | 162 | j++; |
michael@0 | 163 | break; |
michael@0 | 164 | } |
michael@0 | 165 | }else{ |
michael@0 | 166 | if(j<targetLen){ |
michael@0 | 167 | uprv_strcat(target,"\\\\"); |
michael@0 | 168 | } |
michael@0 | 169 | j+=2; |
michael@0 | 170 | } |
michael@0 | 171 | }else if(source[i]>=0x20 && source[i]<0x7F/*ASCII*/){ |
michael@0 | 172 | if(j<targetLen){ |
michael@0 | 173 | target[j] = (char) source[i]; |
michael@0 | 174 | } |
michael@0 | 175 | j++; |
michael@0 | 176 | }else{ |
michael@0 | 177 | if(*enc =='\0' || source[i]==0x0000){ |
michael@0 | 178 | uprv_strcpy(str,"\\u"); |
michael@0 | 179 | itostr(str+2,source[i],16,4); |
michael@0 | 180 | if(j+6<targetLen){ |
michael@0 | 181 | uprv_strcat(target,str); |
michael@0 | 182 | } |
michael@0 | 183 | j+=6; |
michael@0 | 184 | }else{ |
michael@0 | 185 | char dest[30] = {0}; |
michael@0 | 186 | int retVal=ucnv_fromUChars(conv,dest,30,source+i,1,status); |
michael@0 | 187 | if(U_FAILURE(*status)){ |
michael@0 | 188 | return 0; |
michael@0 | 189 | } |
michael@0 | 190 | if(j+retVal<targetLen){ |
michael@0 | 191 | uprv_strcat(target,dest); |
michael@0 | 192 | } |
michael@0 | 193 | j+=retVal; |
michael@0 | 194 | } |
michael@0 | 195 | } |
michael@0 | 196 | i++; |
michael@0 | 197 | } |
michael@0 | 198 | return j; |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | |
michael@0 | 202 | static uint32_t |
michael@0 | 203 | strrch(const char* source,uint32_t sourceLen,char find){ |
michael@0 | 204 | const char* tSourceEnd =source + (sourceLen-1); |
michael@0 | 205 | while(tSourceEnd>= source){ |
michael@0 | 206 | if(*tSourceEnd==find){ |
michael@0 | 207 | return (uint32_t)(tSourceEnd-source); |
michael@0 | 208 | } |
michael@0 | 209 | tSourceEnd--; |
michael@0 | 210 | } |
michael@0 | 211 | return (uint32_t)(tSourceEnd-source); |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | static int32_t getColumnCount(int32_t len){ |
michael@0 | 215 | int32_t columnCount = 80; |
michael@0 | 216 | int32_t maxLines = 3000; |
michael@0 | 217 | int32_t adjustedLen = len*5; /* assume that every codepoint is represented in \uXXXX format*/ |
michael@0 | 218 | /* |
michael@0 | 219 | * calculate the number of lines that |
michael@0 | 220 | * may be required if column count is 80 |
michael@0 | 221 | */ |
michael@0 | 222 | if (maxLines < (adjustedLen / columnCount) ){ |
michael@0 | 223 | columnCount = adjustedLen / maxLines; |
michael@0 | 224 | } |
michael@0 | 225 | return columnCount; |
michael@0 | 226 | } |
michael@0 | 227 | static void |
michael@0 | 228 | str_write_java( uint16_t* src, int32_t srcLen, UBool printEndLine, UErrorCode *status){ |
michael@0 | 229 | |
michael@0 | 230 | uint32_t length = srcLen*8; |
michael@0 | 231 | uint32_t bufLen = 0; |
michael@0 | 232 | uint32_t columnCount; |
michael@0 | 233 | char* buf = (char*) malloc(sizeof(char)*length); |
michael@0 | 234 | |
michael@0 | 235 | if(buf == NULL) { |
michael@0 | 236 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 237 | return; |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | columnCount = getColumnCount(srcLen); |
michael@0 | 241 | memset(buf,0,length); |
michael@0 | 242 | |
michael@0 | 243 | bufLen = uCharsToChars(buf,length,src,srcLen,status); |
michael@0 | 244 | |
michael@0 | 245 | if(printEndLine) |
michael@0 | 246 | write_tabs(out); |
michael@0 | 247 | |
michael@0 | 248 | if(U_FAILURE(*status)){ |
michael@0 | 249 | uprv_free(buf); |
michael@0 | 250 | return; |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | if(bufLen+(tabCount*4) > columnCount ){ |
michael@0 | 254 | uint32_t len = 0; |
michael@0 | 255 | char* current = buf; |
michael@0 | 256 | uint32_t add; |
michael@0 | 257 | while(len < bufLen){ |
michael@0 | 258 | add = columnCount-(tabCount*4)-5/* for ", +\n */; |
michael@0 | 259 | current = buf +len; |
michael@0 | 260 | if (add < (bufLen-len)) { |
michael@0 | 261 | uint32_t idx = strrch(current,add,'\\'); |
michael@0 | 262 | if (idx > add) { |
michael@0 | 263 | idx = add; |
michael@0 | 264 | } else { |
michael@0 | 265 | int32_t num =idx-1; |
michael@0 | 266 | uint32_t seqLen; |
michael@0 | 267 | while(num>0){ |
michael@0 | 268 | if(current[num]=='\\'){ |
michael@0 | 269 | num--; |
michael@0 | 270 | }else{ |
michael@0 | 271 | break; |
michael@0 | 272 | } |
michael@0 | 273 | } |
michael@0 | 274 | if ((idx-num)%2==0) { |
michael@0 | 275 | idx--; |
michael@0 | 276 | } |
michael@0 | 277 | seqLen = (current[idx+1]=='u') ? 6 : 2; |
michael@0 | 278 | if ((add-idx) < seqLen) { |
michael@0 | 279 | add = idx + seqLen; |
michael@0 | 280 | } |
michael@0 | 281 | } |
michael@0 | 282 | } |
michael@0 | 283 | T_FileStream_write(out,"\"",1); |
michael@0 | 284 | if(len+add<bufLen){ |
michael@0 | 285 | T_FileStream_write(out,current,add); |
michael@0 | 286 | T_FileStream_write(out,"\" +\n",4); |
michael@0 | 287 | write_tabs(out); |
michael@0 | 288 | }else{ |
michael@0 | 289 | T_FileStream_write(out,current,bufLen-len); |
michael@0 | 290 | } |
michael@0 | 291 | len+=add; |
michael@0 | 292 | } |
michael@0 | 293 | }else{ |
michael@0 | 294 | T_FileStream_write(out,"\"",1); |
michael@0 | 295 | T_FileStream_write(out, buf,bufLen); |
michael@0 | 296 | } |
michael@0 | 297 | if(printEndLine){ |
michael@0 | 298 | T_FileStream_write(out,"\",\n",3); |
michael@0 | 299 | }else{ |
michael@0 | 300 | T_FileStream_write(out,"\"",1); |
michael@0 | 301 | } |
michael@0 | 302 | uprv_free(buf); |
michael@0 | 303 | } |
michael@0 | 304 | |
michael@0 | 305 | /* Writing Functions */ |
michael@0 | 306 | static void |
michael@0 | 307 | string_write_java(struct SResource *res,UErrorCode *status) { |
michael@0 | 308 | char resKeyBuffer[8]; |
michael@0 | 309 | const char *resname = res_getKeyString(srBundle, res, resKeyBuffer); |
michael@0 | 310 | |
michael@0 | 311 | str_write_java(res->u.fString.fChars,res->u.fString.fLength,TRUE,status); |
michael@0 | 312 | |
michael@0 | 313 | if(resname != NULL && uprv_strcmp(resname,"Rule")==0) |
michael@0 | 314 | { |
michael@0 | 315 | UChar* buf = (UChar*) uprv_malloc(sizeof(UChar)*res->u.fString.fLength); |
michael@0 | 316 | uprv_memcpy(buf,res->u.fString.fChars,res->u.fString.fLength); |
michael@0 | 317 | uprv_free(buf); |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | static void |
michael@0 | 323 | array_write_java( struct SResource *res, UErrorCode *status) { |
michael@0 | 324 | |
michael@0 | 325 | uint32_t i = 0; |
michael@0 | 326 | const char* arr ="new String[] { \n"; |
michael@0 | 327 | struct SResource *current = NULL; |
michael@0 | 328 | UBool allStrings = TRUE; |
michael@0 | 329 | |
michael@0 | 330 | if (U_FAILURE(*status)) { |
michael@0 | 331 | return; |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | if (res->u.fArray.fCount > 0) { |
michael@0 | 335 | |
michael@0 | 336 | current = res->u.fArray.fFirst; |
michael@0 | 337 | i = 0; |
michael@0 | 338 | while(current != NULL){ |
michael@0 | 339 | if(current->fType!=URES_STRING){ |
michael@0 | 340 | allStrings = FALSE; |
michael@0 | 341 | break; |
michael@0 | 342 | } |
michael@0 | 343 | current= current->fNext; |
michael@0 | 344 | } |
michael@0 | 345 | |
michael@0 | 346 | current = res->u.fArray.fFirst; |
michael@0 | 347 | if(allStrings==FALSE){ |
michael@0 | 348 | const char* object = "new Object[]{\n"; |
michael@0 | 349 | write_tabs(out); |
michael@0 | 350 | T_FileStream_write(out, object, (int32_t)uprv_strlen(object)); |
michael@0 | 351 | tabCount++; |
michael@0 | 352 | }else{ |
michael@0 | 353 | write_tabs(out); |
michael@0 | 354 | T_FileStream_write(out, arr, (int32_t)uprv_strlen(arr)); |
michael@0 | 355 | tabCount++; |
michael@0 | 356 | } |
michael@0 | 357 | while (current != NULL) { |
michael@0 | 358 | /*if(current->fType==URES_STRING){ |
michael@0 | 359 | write_tabs(out); |
michael@0 | 360 | }*/ |
michael@0 | 361 | res_write_java(current, status); |
michael@0 | 362 | if(U_FAILURE(*status)){ |
michael@0 | 363 | return; |
michael@0 | 364 | } |
michael@0 | 365 | i++; |
michael@0 | 366 | current = current->fNext; |
michael@0 | 367 | } |
michael@0 | 368 | T_FileStream_write(out,"\n",1); |
michael@0 | 369 | |
michael@0 | 370 | tabCount--; |
michael@0 | 371 | write_tabs(out); |
michael@0 | 372 | T_FileStream_write(out,"},\n",3); |
michael@0 | 373 | |
michael@0 | 374 | } else { |
michael@0 | 375 | write_tabs(out); |
michael@0 | 376 | T_FileStream_write(out,arr,(int32_t)uprv_strlen(arr)); |
michael@0 | 377 | write_tabs(out); |
michael@0 | 378 | T_FileStream_write(out,"},\n",3); |
michael@0 | 379 | } |
michael@0 | 380 | } |
michael@0 | 381 | |
michael@0 | 382 | static void |
michael@0 | 383 | intvector_write_java( struct SResource *res, UErrorCode *status) { |
michael@0 | 384 | uint32_t i = 0; |
michael@0 | 385 | const char* intArr = "new int[] {\n"; |
michael@0 | 386 | /* const char* intC = "new Integer("; */ |
michael@0 | 387 | const char* stringArr = "new String[]{\n"; |
michael@0 | 388 | char resKeyBuffer[8]; |
michael@0 | 389 | const char *resname = res_getKeyString(srBundle, res, resKeyBuffer); |
michael@0 | 390 | char buf[100]; |
michael@0 | 391 | int len =0; |
michael@0 | 392 | buf[0]=0; |
michael@0 | 393 | write_tabs(out); |
michael@0 | 394 | |
michael@0 | 395 | if(resname != NULL && uprv_strcmp(resname,"DateTimeElements")==0){ |
michael@0 | 396 | T_FileStream_write(out, stringArr, (int32_t)uprv_strlen(stringArr)); |
michael@0 | 397 | tabCount++; |
michael@0 | 398 | for(i = 0; i<res->u.fIntVector.fCount; i++) { |
michael@0 | 399 | write_tabs(out); |
michael@0 | 400 | len=itostr(buf,res->u.fIntVector.fArray[i],10,0); |
michael@0 | 401 | T_FileStream_write(out,"\"",1); |
michael@0 | 402 | T_FileStream_write(out,buf,len); |
michael@0 | 403 | T_FileStream_write(out,"\",",2); |
michael@0 | 404 | T_FileStream_write(out,"\n",1); |
michael@0 | 405 | } |
michael@0 | 406 | }else{ |
michael@0 | 407 | T_FileStream_write(out, intArr, (int32_t)uprv_strlen(intArr)); |
michael@0 | 408 | tabCount++; |
michael@0 | 409 | for(i = 0; i<res->u.fIntVector.fCount; i++) { |
michael@0 | 410 | write_tabs(out); |
michael@0 | 411 | /* T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC)); */ |
michael@0 | 412 | len=itostr(buf,res->u.fIntVector.fArray[i],10,0); |
michael@0 | 413 | T_FileStream_write(out,buf,len); |
michael@0 | 414 | /* T_FileStream_write(out,"),",2); */ |
michael@0 | 415 | /* T_FileStream_write(out,"\n",1); */ |
michael@0 | 416 | T_FileStream_write(out,",\n",2); |
michael@0 | 417 | } |
michael@0 | 418 | } |
michael@0 | 419 | tabCount--; |
michael@0 | 420 | write_tabs(out); |
michael@0 | 421 | T_FileStream_write(out,"},\n",3); |
michael@0 | 422 | } |
michael@0 | 423 | |
michael@0 | 424 | static void |
michael@0 | 425 | int_write_java(struct SResource *res,UErrorCode *status) { |
michael@0 | 426 | const char* intC = "new Integer("; |
michael@0 | 427 | char buf[100]; |
michael@0 | 428 | int len =0; |
michael@0 | 429 | buf[0]=0; |
michael@0 | 430 | |
michael@0 | 431 | /* write the binary data */ |
michael@0 | 432 | write_tabs(out); |
michael@0 | 433 | T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC)); |
michael@0 | 434 | len=itostr(buf, res->u.fIntValue.fValue, 10, 0); |
michael@0 | 435 | T_FileStream_write(out,buf,len); |
michael@0 | 436 | T_FileStream_write(out,"),\n",3 ); |
michael@0 | 437 | |
michael@0 | 438 | } |
michael@0 | 439 | |
michael@0 | 440 | static void |
michael@0 | 441 | bytes_write_java( struct SResource *res, UErrorCode *status) { |
michael@0 | 442 | const char* type = "new byte[] {"; |
michael@0 | 443 | const char* byteDecl = "%i, "; |
michael@0 | 444 | char byteBuffer[100] = { 0 }; |
michael@0 | 445 | uint8_t* byteArray = NULL; |
michael@0 | 446 | int byteIterator = 0; |
michael@0 | 447 | |
michael@0 | 448 | int32_t srcLen=res->u.fBinaryValue.fLength; |
michael@0 | 449 | |
michael@0 | 450 | if(srcLen>0 ) |
michael@0 | 451 | { |
michael@0 | 452 | byteArray = res->u.fBinaryValue.fData; |
michael@0 | 453 | |
michael@0 | 454 | write_tabs(out); |
michael@0 | 455 | T_FileStream_write(out, type, (int32_t)uprv_strlen(type)); |
michael@0 | 456 | T_FileStream_write(out, "\n", 1); |
michael@0 | 457 | tabCount++; |
michael@0 | 458 | |
michael@0 | 459 | for (;byteIterator<srcLen;byteIterator++) |
michael@0 | 460 | { |
michael@0 | 461 | if (byteIterator%16 == 0) |
michael@0 | 462 | { |
michael@0 | 463 | write_tabs(out); |
michael@0 | 464 | } |
michael@0 | 465 | |
michael@0 | 466 | if (byteArray[byteIterator] < 128) |
michael@0 | 467 | { |
michael@0 | 468 | sprintf(byteBuffer, byteDecl, byteArray[byteIterator]); |
michael@0 | 469 | } |
michael@0 | 470 | else |
michael@0 | 471 | { |
michael@0 | 472 | sprintf(byteBuffer, byteDecl, (byteArray[byteIterator]-256)); |
michael@0 | 473 | } |
michael@0 | 474 | |
michael@0 | 475 | T_FileStream_write(out, byteBuffer, (int32_t)uprv_strlen(byteBuffer)); |
michael@0 | 476 | |
michael@0 | 477 | if (byteIterator%16 == 15) |
michael@0 | 478 | { |
michael@0 | 479 | T_FileStream_write(out, "\n", 1); |
michael@0 | 480 | } |
michael@0 | 481 | |
michael@0 | 482 | } |
michael@0 | 483 | |
michael@0 | 484 | if (((byteIterator-1)%16) != 15) |
michael@0 | 485 | { |
michael@0 | 486 | T_FileStream_write(out, "\n", 1); |
michael@0 | 487 | } |
michael@0 | 488 | |
michael@0 | 489 | tabCount--; |
michael@0 | 490 | write_tabs(out); |
michael@0 | 491 | T_FileStream_write(out, "},\n", 3); |
michael@0 | 492 | |
michael@0 | 493 | } |
michael@0 | 494 | else |
michael@0 | 495 | { |
michael@0 | 496 | /* Empty array */ |
michael@0 | 497 | write_tabs(out); |
michael@0 | 498 | T_FileStream_write(out,type,(int32_t)uprv_strlen(type)); |
michael@0 | 499 | T_FileStream_write(out,"},\n",3); |
michael@0 | 500 | } |
michael@0 | 501 | |
michael@0 | 502 | } |
michael@0 | 503 | |
michael@0 | 504 | static UBool start = TRUE; |
michael@0 | 505 | |
michael@0 | 506 | static void |
michael@0 | 507 | table_write_java(struct SResource *res, UErrorCode *status) { |
michael@0 | 508 | uint32_t i = 0; |
michael@0 | 509 | struct SResource *current = NULL; |
michael@0 | 510 | const char* obj = "new Object[][]{\n"; |
michael@0 | 511 | |
michael@0 | 512 | if (U_FAILURE(*status)) { |
michael@0 | 513 | return ; |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | if (res->u.fTable.fCount > 0) { |
michael@0 | 517 | if(start==FALSE){ |
michael@0 | 518 | write_tabs(out); |
michael@0 | 519 | T_FileStream_write(out, obj, (int32_t)uprv_strlen(obj)); |
michael@0 | 520 | tabCount++; |
michael@0 | 521 | } |
michael@0 | 522 | start = FALSE; |
michael@0 | 523 | current = res->u.fTable.fFirst; |
michael@0 | 524 | i = 0; |
michael@0 | 525 | |
michael@0 | 526 | |
michael@0 | 527 | while (current != NULL) { |
michael@0 | 528 | char currentKeyBuffer[8]; |
michael@0 | 529 | const char *currentKeyString = res_getKeyString(srBundle, current, currentKeyBuffer); |
michael@0 | 530 | |
michael@0 | 531 | assert(i < res->u.fTable.fCount); |
michael@0 | 532 | write_tabs(out); |
michael@0 | 533 | |
michael@0 | 534 | T_FileStream_write(out, openBrace, 2); |
michael@0 | 535 | |
michael@0 | 536 | |
michael@0 | 537 | tabCount++; |
michael@0 | 538 | |
michael@0 | 539 | write_tabs(out); |
michael@0 | 540 | if(currentKeyString != NULL) { |
michael@0 | 541 | T_FileStream_write(out, "\"", 1); |
michael@0 | 542 | T_FileStream_write(out, currentKeyString, |
michael@0 | 543 | (int32_t)uprv_strlen(currentKeyString)); |
michael@0 | 544 | T_FileStream_write(out, "\",\n", 2); |
michael@0 | 545 | |
michael@0 | 546 | T_FileStream_write(out, "\n", 1); |
michael@0 | 547 | } |
michael@0 | 548 | res_write_java(current, status); |
michael@0 | 549 | if(U_FAILURE(*status)){ |
michael@0 | 550 | return; |
michael@0 | 551 | } |
michael@0 | 552 | i++; |
michael@0 | 553 | current = current->fNext; |
michael@0 | 554 | tabCount--; |
michael@0 | 555 | write_tabs(out); |
michael@0 | 556 | T_FileStream_write(out, "},\n", 3); |
michael@0 | 557 | } |
michael@0 | 558 | if(tabCount>4){ |
michael@0 | 559 | tabCount--; |
michael@0 | 560 | write_tabs(out); |
michael@0 | 561 | T_FileStream_write(out, "},\n", 3); |
michael@0 | 562 | } |
michael@0 | 563 | |
michael@0 | 564 | } else { |
michael@0 | 565 | write_tabs(out); |
michael@0 | 566 | T_FileStream_write(out,obj,(int32_t)uprv_strlen(obj)); |
michael@0 | 567 | |
michael@0 | 568 | write_tabs(out); |
michael@0 | 569 | T_FileStream_write(out,"},\n",3); |
michael@0 | 570 | |
michael@0 | 571 | } |
michael@0 | 572 | |
michael@0 | 573 | } |
michael@0 | 574 | |
michael@0 | 575 | void |
michael@0 | 576 | res_write_java(struct SResource *res,UErrorCode *status) { |
michael@0 | 577 | |
michael@0 | 578 | if (U_FAILURE(*status)) { |
michael@0 | 579 | return ; |
michael@0 | 580 | } |
michael@0 | 581 | |
michael@0 | 582 | if (res != NULL) { |
michael@0 | 583 | switch (res->fType) { |
michael@0 | 584 | case URES_STRING: |
michael@0 | 585 | string_write_java (res, status); |
michael@0 | 586 | return; |
michael@0 | 587 | case URES_ALIAS: |
michael@0 | 588 | printf("Encountered unsupported resource type %d of alias\n", res->fType); |
michael@0 | 589 | *status = U_UNSUPPORTED_ERROR; |
michael@0 | 590 | return; |
michael@0 | 591 | case URES_INT_VECTOR: |
michael@0 | 592 | intvector_write_java (res, status); |
michael@0 | 593 | return; |
michael@0 | 594 | case URES_BINARY: |
michael@0 | 595 | bytes_write_java (res, status); |
michael@0 | 596 | return; |
michael@0 | 597 | case URES_INT: |
michael@0 | 598 | int_write_java (res, status); |
michael@0 | 599 | return; |
michael@0 | 600 | case URES_ARRAY: |
michael@0 | 601 | array_write_java (res, status); |
michael@0 | 602 | return; |
michael@0 | 603 | case URES_TABLE: |
michael@0 | 604 | table_write_java (res, status); |
michael@0 | 605 | return; |
michael@0 | 606 | default: |
michael@0 | 607 | break; |
michael@0 | 608 | } |
michael@0 | 609 | } |
michael@0 | 610 | |
michael@0 | 611 | *status = U_INTERNAL_PROGRAM_ERROR; |
michael@0 | 612 | } |
michael@0 | 613 | |
michael@0 | 614 | void |
michael@0 | 615 | bundle_write_java(struct SRBRoot *bundle, const char *outputDir,const char* outputEnc, |
michael@0 | 616 | char *writtenFilename, int writtenFilenameLen, |
michael@0 | 617 | const char* packageName, const char* bundleName, |
michael@0 | 618 | UErrorCode *status) { |
michael@0 | 619 | |
michael@0 | 620 | char fileName[256] = {'\0'}; |
michael@0 | 621 | char className[256]={'\0'}; |
michael@0 | 622 | /*char constructor[1000] = { 0 };*/ |
michael@0 | 623 | /*UBool j1 =FALSE;*/ |
michael@0 | 624 | /*outDir = outputDir;*/ |
michael@0 | 625 | |
michael@0 | 626 | start = TRUE; /* Reset the start indictor*/ |
michael@0 | 627 | |
michael@0 | 628 | bName = (bundleName==NULL) ? "LocaleElements" : bundleName; |
michael@0 | 629 | pName = (packageName==NULL)? "com.ibm.icu.impl.data" : packageName; |
michael@0 | 630 | |
michael@0 | 631 | uprv_strcpy(className, bName); |
michael@0 | 632 | srBundle = bundle; |
michael@0 | 633 | if(uprv_strcmp(srBundle->fLocale,"root")!=0){ |
michael@0 | 634 | uprv_strcat(className,"_"); |
michael@0 | 635 | uprv_strcat(className,srBundle->fLocale); |
michael@0 | 636 | } |
michael@0 | 637 | if(outputDir){ |
michael@0 | 638 | uprv_strcpy(fileName, outputDir); |
michael@0 | 639 | if(outputDir[uprv_strlen(outputDir)-1] !=U_FILE_SEP_CHAR){ |
michael@0 | 640 | uprv_strcat(fileName,U_FILE_SEP_STRING); |
michael@0 | 641 | } |
michael@0 | 642 | uprv_strcat(fileName,className); |
michael@0 | 643 | uprv_strcat(fileName,".java"); |
michael@0 | 644 | }else{ |
michael@0 | 645 | uprv_strcat(fileName,className); |
michael@0 | 646 | uprv_strcat(fileName,".java"); |
michael@0 | 647 | } |
michael@0 | 648 | |
michael@0 | 649 | if (writtenFilename) { |
michael@0 | 650 | uprv_strncpy(writtenFilename, fileName, writtenFilenameLen); |
michael@0 | 651 | } |
michael@0 | 652 | |
michael@0 | 653 | if (U_FAILURE(*status)) { |
michael@0 | 654 | return; |
michael@0 | 655 | } |
michael@0 | 656 | |
michael@0 | 657 | out= T_FileStream_open(fileName,"w"); |
michael@0 | 658 | |
michael@0 | 659 | if(out==NULL){ |
michael@0 | 660 | *status = U_FILE_ACCESS_ERROR; |
michael@0 | 661 | return; |
michael@0 | 662 | } |
michael@0 | 663 | if(getIncludeCopyright()){ |
michael@0 | 664 | T_FileStream_write(out, copyRight, (int32_t)uprv_strlen(copyRight)); |
michael@0 | 665 | T_FileStream_write(out, warningMsg, (int32_t)uprv_strlen(warningMsg)); |
michael@0 | 666 | } |
michael@0 | 667 | T_FileStream_write(out,"package ",(int32_t)uprv_strlen("package ")); |
michael@0 | 668 | T_FileStream_write(out,pName,(int32_t)uprv_strlen(pName)); |
michael@0 | 669 | T_FileStream_write(out,";\n\n",3); |
michael@0 | 670 | T_FileStream_write(out, javaClass, (int32_t)uprv_strlen(javaClass)); |
michael@0 | 671 | T_FileStream_write(out, className, (int32_t)uprv_strlen(className)); |
michael@0 | 672 | T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1)); |
michael@0 | 673 | |
michael@0 | 674 | /* if(j1){ |
michael@0 | 675 | T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1)); |
michael@0 | 676 | }else{ |
michael@0 | 677 | sprintf(constructor,javaClassICU,className); |
michael@0 | 678 | T_FileStream_write(out, constructor, (int32_t)uprv_strlen(constructor)); |
michael@0 | 679 | } |
michael@0 | 680 | */ |
michael@0 | 681 | |
michael@0 | 682 | if(outputEnc && *outputEnc!='\0'){ |
michael@0 | 683 | /* store the output encoding */ |
michael@0 | 684 | enc = outputEnc; |
michael@0 | 685 | conv=ucnv_open(enc,status); |
michael@0 | 686 | if(U_FAILURE(*status)){ |
michael@0 | 687 | return; |
michael@0 | 688 | } |
michael@0 | 689 | } |
michael@0 | 690 | res_write_java(bundle->fRoot, status); |
michael@0 | 691 | |
michael@0 | 692 | T_FileStream_write(out, closeClass, (int32_t)uprv_strlen(closeClass)); |
michael@0 | 693 | |
michael@0 | 694 | T_FileStream_close(out); |
michael@0 | 695 | |
michael@0 | 696 | ucnv_close(conv); |
michael@0 | 697 | } |