1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2000-2015, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
13 * Modification History:
15 * Date Name Description
16 * 01/11/02 Ram Creation.
17 * 02/12/08 Spieth Fix errant 'new Object[][]{' insertion
18 * 02/19/08 Spieth Removed ICUListResourceBundle dependancy
19 *******************************************************************************
23 #include "unicode/unistr.h"
26 #include "unicode/ures.h"
30 #include "unicode/ucnv.h"
35 #include "unicode/ustring.h"
37 void res_write_java(struct SResource *res,UErrorCode *status);
40 static const char copyRight[] =
42 " *******************************************************************************\n"
44 " * Copyright (C) International Business Machines\n"
45 " * Corporation and others. All Rights Reserved.\n"
47 " *******************************************************************************\n"
50 " * $" "Revision: $ \n"
51 " *******************************************************************************\n"
53 static const char warningMsg[] =
54 "/*********************************************************************\n"
55 "######################################################################\n"
57 " WARNING: This file is generated by genrb Version " GENRB_VERSION ".\n"
58 " If you edit this file, please make sure that, the source\n"
59 " of this file (XXXX.txt in LocaleElements_XXXX.java)\n"
61 "######################################################################\n"
62 " *********************************************************************\n"
64 static const char* openBrace="{\n";
65 static const char* closeClass=" };\n"
68 static const char* javaClass = "import java.util.ListResourceBundle;\n\n"
71 static const char* javaClass1= " extends ListResourceBundle {\n\n"
73 " * Overrides ListResourceBundle \n"
75 " public final Object[][] getContents() { \n"
78 " private static Object[][] contents = {\n";
79 /*static const char* javaClassICU= " extends ListResourceBundle {\n\n"
81 " super.contents = data;\n"
83 " static final Object[][] data = new Object[][] { \n";*/
84 static int tabCount = 3;
86 static FileStream* out=NULL;
87 static struct SRBRoot* srBundle ;
88 /*static const char* outDir = NULL;*/
90 static const char* bName=NULL;
91 static const char* pName=NULL;
93 static void write_tabs(FileStream* os){
95 for(;i<=tabCount;i++){
96 T_FileStream_write(os," ",4);
102 static const char* enc ="";
103 static UConverter* conv = NULL;
106 uCharsToChars(char *target, int32_t targetLen, const UChar *source, int32_t sourceLen, UErrorCode *status) {
110 if (source[i] == '\n') {
111 if (j + 2 < targetLen) {
112 uprv_strcat(target, "\\n");
115 }else if(source[i]==0x0D){
117 uprv_strcat(target,"\\f");
120 }else if(source[i] == '"'){
121 if(source[i-1]=='\''){
123 uprv_strcat(target,"\\");
124 target[j+1]= (char)source[i];
127 }else if(source[i-1]!='\\'){
130 uprv_strcat(target,"\\");
131 target[j+1]= (char)source[i];
134 }else if(source[i-1]=='\\'){
135 target[j++]= (char)source[i];
137 }else if(source[i]=='\\'){
150 uprv_strcat(target,"\\\\");
157 uprv_strcat(target,"\\\\\\");
163 target[j]=(char)source[i];
170 uprv_strcat(target,"\\\\");
174 }else if(source[i]>=0x20 && source[i]<0x7F/*ASCII*/){
176 target[j] = (char) source[i];
180 if(*enc =='\0' || source[i]==0x0000){
181 uprv_strcpy(str,"\\u");
182 itostr(str+2,source[i],16,4);
184 uprv_strcat(target,str);
189 int retVal=ucnv_fromUChars(conv,dest,30,source+i,1,status);
190 if(U_FAILURE(*status)){
193 if(j+retVal<targetLen){
194 uprv_strcat(target,dest);
206 strrch(const char* source,uint32_t sourceLen,char find){
207 const char* tSourceEnd =source + (sourceLen-1);
208 while(tSourceEnd>= source){
209 if(*tSourceEnd==find){
210 return (uint32_t)(tSourceEnd-source);
214 return (uint32_t)(tSourceEnd-source);
217 static int32_t getColumnCount(int32_t len){
218 int32_t columnCount = 80;
219 int32_t maxLines = 3000;
220 int32_t adjustedLen = len*5; /* assume that every codepoint is represented in \uXXXX format*/
222 * calculate the number of lines that
223 * may be required if column count is 80
225 if (maxLines < (adjustedLen / columnCount) ){
226 columnCount = adjustedLen / maxLines;
231 str_write_java(const UChar *src, int32_t srcLen, UBool printEndLine, UErrorCode *status) {
233 uint32_t length = srcLen*8;
235 uint32_t columnCount;
236 char* buf = (char*) malloc(sizeof(char)*length);
239 *status = U_MEMORY_ALLOCATION_ERROR;
243 columnCount = getColumnCount(srcLen);
244 memset(buf,0,length);
246 bufLen = uCharsToChars(buf,length,src,srcLen,status);
251 if(U_FAILURE(*status)){
256 if(bufLen+(tabCount*4) > columnCount ){
261 add = columnCount-(tabCount*4)-5/* for ", +\n */;
263 if (add < (bufLen-len)) {
264 uint32_t idx = strrch(current,add,'\\');
271 if(current[num]=='\\'){
277 if ((idx-num)%2==0) {
280 seqLen = (current[idx+1]=='u') ? 6 : 2;
281 if ((add-idx) < seqLen) {
286 T_FileStream_write(out,"\"",1);
288 T_FileStream_write(out,current,add);
289 T_FileStream_write(out,"\" +\n",4);
292 T_FileStream_write(out,current,bufLen-len);
297 T_FileStream_write(out,"\"",1);
298 T_FileStream_write(out, buf,bufLen);
301 T_FileStream_write(out,"\",\n",3);
303 T_FileStream_write(out,"\"",1);
308 /* Writing Functions */
310 string_write_java(const StringResource *res,UErrorCode *status) {
311 (void)res->getKeyString(srBundle);
313 str_write_java(res->getBuffer(), res->length(), TRUE, status);
317 array_write_java(const ArrayResource *res, UErrorCode *status) {
320 const char* arr ="new String[] { \n";
321 struct SResource *current = NULL;
322 UBool allStrings = TRUE;
324 if (U_FAILURE(*status)) {
328 if (res->fCount > 0) {
330 current = res->fFirst;
332 while(current != NULL){
333 if(!current->isString()){
337 current= current->fNext;
340 current = res->fFirst;
341 if(allStrings==FALSE){
342 const char* object = "new Object[]{\n";
344 T_FileStream_write(out, object, (int32_t)uprv_strlen(object));
348 T_FileStream_write(out, arr, (int32_t)uprv_strlen(arr));
351 while (current != NULL) {
352 /*if(current->isString()){
355 res_write_java(current, status);
356 if(U_FAILURE(*status)){
360 current = current->fNext;
362 T_FileStream_write(out,"\n",1);
366 T_FileStream_write(out,"},\n",3);
370 T_FileStream_write(out,arr,(int32_t)uprv_strlen(arr));
372 T_FileStream_write(out,"},\n",3);
377 intvector_write_java(const IntVectorResource *res, UErrorCode * /*status*/) {
379 const char* intArr = "new int[] {\n";
380 /* const char* intC = "new Integer("; */
381 const char* stringArr = "new String[]{\n";
382 const char *resname = res->getKeyString(srBundle);
388 if(resname != NULL && uprv_strcmp(resname,"DateTimeElements")==0){
389 T_FileStream_write(out, stringArr, (int32_t)uprv_strlen(stringArr));
391 for(i = 0; i<res->fCount; i++) {
393 len=itostr(buf,res->fArray[i],10,0);
394 T_FileStream_write(out,"\"",1);
395 T_FileStream_write(out,buf,len);
396 T_FileStream_write(out,"\",",2);
397 T_FileStream_write(out,"\n",1);
400 T_FileStream_write(out, intArr, (int32_t)uprv_strlen(intArr));
402 for(i = 0; i<res->fCount; i++) {
404 /* T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC)); */
405 len=itostr(buf,res->fArray[i],10,0);
406 T_FileStream_write(out,buf,len);
407 /* T_FileStream_write(out,"),",2); */
408 /* T_FileStream_write(out,"\n",1); */
409 T_FileStream_write(out,",\n",2);
414 T_FileStream_write(out,"},\n",3);
418 int_write_java(const IntResource *res, UErrorCode * /*status*/) {
419 const char* intC = "new Integer(";
424 /* write the binary data */
426 T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC));
427 len=itostr(buf, res->fValue, 10, 0);
428 T_FileStream_write(out,buf,len);
429 T_FileStream_write(out,"),\n",3 );
434 bytes_write_java(const BinaryResource *res, UErrorCode * /*status*/) {
435 const char* type = "new byte[] {";
436 const char* byteDecl = "%i, ";
437 char byteBuffer[100] = { 0 };
438 uint8_t* byteArray = NULL;
439 int byteIterator = 0;
441 int32_t srcLen=res->fLength;
445 byteArray = res->fData;
448 T_FileStream_write(out, type, (int32_t)uprv_strlen(type));
449 T_FileStream_write(out, "\n", 1);
452 for (;byteIterator<srcLen;byteIterator++)
454 if (byteIterator%16 == 0)
459 if (byteArray[byteIterator] < 128)
461 sprintf(byteBuffer, byteDecl, byteArray[byteIterator]);
465 sprintf(byteBuffer, byteDecl, (byteArray[byteIterator]-256));
468 T_FileStream_write(out, byteBuffer, (int32_t)uprv_strlen(byteBuffer));
470 if (byteIterator%16 == 15)
472 T_FileStream_write(out, "\n", 1);
477 if (((byteIterator-1)%16) != 15)
479 T_FileStream_write(out, "\n", 1);
484 T_FileStream_write(out, "},\n", 3);
491 T_FileStream_write(out,type,(int32_t)uprv_strlen(type));
492 T_FileStream_write(out,"},\n",3);
497 static UBool start = TRUE;
500 table_write_java(const TableResource *res, UErrorCode *status) {
502 struct SResource *current = NULL;
503 const char* obj = "new Object[][]{\n";
505 if (U_FAILURE(*status)) {
509 if (res->fCount > 0) {
512 T_FileStream_write(out, obj, (int32_t)uprv_strlen(obj));
516 current = res->fFirst;
520 while (current != NULL) {
521 const char *currentKeyString = current->getKeyString(srBundle);
523 assert(i < res->fCount);
526 T_FileStream_write(out, openBrace, 2);
532 if(currentKeyString != NULL) {
533 T_FileStream_write(out, "\"", 1);
534 T_FileStream_write(out, currentKeyString,
535 (int32_t)uprv_strlen(currentKeyString));
536 T_FileStream_write(out, "\",\n", 2);
538 T_FileStream_write(out, "\n", 1);
540 res_write_java(current, status);
541 if(U_FAILURE(*status)){
545 current = current->fNext;
548 T_FileStream_write(out, "},\n", 3);
553 T_FileStream_write(out, "},\n", 3);
558 T_FileStream_write(out,obj,(int32_t)uprv_strlen(obj));
561 T_FileStream_write(out,"},\n",3);
568 res_write_java(struct SResource *res,UErrorCode *status) {
570 if (U_FAILURE(*status)) {
575 switch (res->fType) {
577 string_write_java (static_cast<const StringResource *>(res), status);
580 printf("Encountered unsupported resource type %d of alias\n", res->fType);
581 *status = U_UNSUPPORTED_ERROR;
583 case URES_INT_VECTOR:
584 intvector_write_java (static_cast<const IntVectorResource *>(res), status);
587 bytes_write_java (static_cast<const BinaryResource *>(res), status);
590 int_write_java (static_cast<const IntResource *>(res), status);
593 array_write_java (static_cast<const ArrayResource *>(res), status);
596 table_write_java (static_cast<const TableResource *>(res), status);
603 *status = U_INTERNAL_PROGRAM_ERROR;
607 bundle_write_java(struct SRBRoot *bundle, const char *outputDir,const char* outputEnc,
608 char *writtenFilename, int writtenFilenameLen,
609 const char* packageName, const char* bundleName,
610 UErrorCode *status) {
612 char fileName[256] = {'\0'};
613 char className[256]={'\0'};
614 /*char constructor[1000] = { 0 };*/
616 /*outDir = outputDir;*/
618 start = TRUE; /* Reset the start indictor*/
620 bName = (bundleName==NULL) ? "LocaleElements" : bundleName;
621 pName = (packageName==NULL)? "com.ibm.icu.impl.data" : packageName;
623 uprv_strcpy(className, bName);
625 if(uprv_strcmp(srBundle->fLocale,"root")!=0){
626 uprv_strcat(className,"_");
627 uprv_strcat(className,srBundle->fLocale);
630 uprv_strcpy(fileName, outputDir);
631 if(outputDir[uprv_strlen(outputDir)-1] !=U_FILE_SEP_CHAR){
632 uprv_strcat(fileName,U_FILE_SEP_STRING);
634 uprv_strcat(fileName,className);
635 uprv_strcat(fileName,".java");
637 uprv_strcat(fileName,className);
638 uprv_strcat(fileName,".java");
641 if (writtenFilename) {
642 uprv_strncpy(writtenFilename, fileName, writtenFilenameLen);
645 if (U_FAILURE(*status)) {
649 out= T_FileStream_open(fileName,"w");
652 *status = U_FILE_ACCESS_ERROR;
655 if(getIncludeCopyright()){
656 T_FileStream_write(out, copyRight, (int32_t)uprv_strlen(copyRight));
657 T_FileStream_write(out, warningMsg, (int32_t)uprv_strlen(warningMsg));
659 T_FileStream_write(out,"package ",(int32_t)uprv_strlen("package "));
660 T_FileStream_write(out,pName,(int32_t)uprv_strlen(pName));
661 T_FileStream_write(out,";\n\n",3);
662 T_FileStream_write(out, javaClass, (int32_t)uprv_strlen(javaClass));
663 T_FileStream_write(out, className, (int32_t)uprv_strlen(className));
664 T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1));
667 T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1));
669 sprintf(constructor,javaClassICU,className);
670 T_FileStream_write(out, constructor, (int32_t)uprv_strlen(constructor));
674 if(outputEnc && *outputEnc!='\0'){
675 /* store the output encoding */
677 conv=ucnv_open(enc,status);
678 if(U_FAILURE(*status)){
682 res_write_java(bundle->fRoot, status);
684 T_FileStream_write(out, closeClass, (int32_t)uprv_strlen(closeClass));
686 T_FileStream_close(out);