]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/tools/genrb/wrtjava.c
ICU-551.24.tar.gz
[apple/icu.git] / icuSources / tools / genrb / wrtjava.c
index a4293ae2807cfa3938c6e0bddc1544edb72d000e..6239569d85e8939ccd8c6fe7642220108051c5e2 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *******************************************************************************
 *
-*   Copyright (C) 2000-2006, International Business Machines
+*   Copyright (C) 2000-2012, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
@@ -11,7 +11,9 @@
 * Modification History:
 *
 *   Date        Name        Description
-*   01/11/02    Ram        Creation.
+*   01/11/02    Ram         Creation.
+*   02/12/08    Spieth      Fix errant 'new Object[][]{' insertion
+*   02/19/08    Spieth      Removed ICUListResourceBundle dependancy
 *******************************************************************************
 */
 
@@ -25,7 +27,6 @@
 #include "unicode/ucnv.h"
 #include "genrb.h"
 #include "rle.h"
-#include "ucol_tok.h"
 #include "uhash.h"
 #include "uresimp.h"
 #include "unicode/ustring.h"
@@ -61,28 +62,27 @@ static const char* openBrace="{\n";
 static const char* closeClass="    };\n"
                               "}\n";
 
-static const char* javaClass =  "import java.util.ListResourceBundle;\n"
-                                "import com.ibm.icu.impl.ICUListResourceBundle;\n\n"
+static const char* javaClass =  "import java.util.ListResourceBundle;\n\n"
                                 "public class ";
 
-static const char* javaClass1=  " extends ICUListResourceBundle {\n\n"
+static const char* javaClass1=  " extends ListResourceBundle {\n\n"
                                 "    /**\n"
                                 "     * Overrides ListResourceBundle \n"
                                 "     */\n"
                                 "    public final Object[][] getContents() { \n"
                                 "          return  contents;\n"
-                                "    }\n"
+                                "    }\n\n"
                                 "    private static Object[][] contents = {\n";
-static const char* javaClassICU= " extends ICUListResourceBundle {\n\n"
+/*static const char* javaClassICU= " extends ListResourceBundle {\n\n"
                                  "    public %s  () {\n"
                                  "          super.contents = data;\n"
                                  "    }\n"
-                                 "    static final Object[][] data = new Object[][] { \n";
+                                 "    static final Object[][] data = new Object[][] { \n";*/
 static int tabCount = 3;
 
 static FileStream* out=NULL;
 static struct SRBRoot* srBundle ;
-static const char* outDir = NULL;
+/*static const char* outDir = NULL;*/
 
 static const char* bName=NULL;
 static const char* pName=NULL;
@@ -98,7 +98,6 @@ static void write_tabs(FileStream* os){
 
 static const char* enc ="";
 static UConverter* conv = NULL;
-static char NUMBER = ZERO;
 
 static int32_t
 uCharsToChars( char* target,int32_t targetLen, UChar* source, int32_t sourceLen,UErrorCode* status){
@@ -259,11 +258,11 @@ str_write_java( uint16_t* src, int32_t srcLen, UBool printEndLine, UErrorCode *s
             add = columnCount-(tabCount*4)-5/* for ", +\n */;
             current = buf +len;
             if (add < (bufLen-len)) {
-                uint32_t index = strrch(current,add,'\\');
-                if (index > add) {
-                    index = add;
+                uint32_t idx = strrch(current,add,'\\');
+                if (idx > add) {
+                    idx = add;
                 } else {
-                    int32_t num =index-1;
+                    int32_t num =idx-1;
                     uint32_t seqLen;
                     while(num>0){
                         if(current[num]=='\\'){
@@ -272,12 +271,12 @@ str_write_java( uint16_t* src, int32_t srcLen, UBool printEndLine, UErrorCode *s
                             break;
                         }
                     }
-                    if ((index-num)%2==0) {
-                        index--;
+                    if ((idx-num)%2==0) {
+                        idx--;
                     }
-                    seqLen = (current[index+1]=='u') ? 6 : 2;
-                    if ((add-index) < seqLen) {
-                        add = index + seqLen;
+                    seqLen = (current[idx+1]=='u') ? 6 : 2;
+                    if ((add-idx) < seqLen) {
+                        add = idx + seqLen;
                     }
                 }
             }
@@ -303,108 +302,29 @@ str_write_java( uint16_t* src, int32_t srcLen, UBool printEndLine, UErrorCode *s
     uprv_free(buf);
 }
 
-static void
-write_utf8_file(struct SResource *res, const char *file, UErrorCode *status){
-        char fileName[1024] ={0};
-        FileStream* datFile = NULL;
-        const char* type = "new ICUListResourceBundle.ResourceString(";
-        char* dest  = (char*) uprv_malloc( 8 * res->u.fString.fLength);
-        int32_t len = 0;
-        if(outDir){
-            uprv_strcat(fileName,outDir);
-            if(outDir[uprv_strlen(outDir)-1]!=U_FILE_SEP_CHAR){
-                uprv_strcat(fileName,U_FILE_SEP_STRING);
-            }
-        }
-        uprv_strcat(fileName,file);/* UCARULES.utf8 UTF-8 file */
-
-        write_tabs(out);
-
-        T_FileStream_write(out, type, (int32_t)uprv_strlen(type));
-        T_FileStream_write(out, "\"", 1);
-        T_FileStream_write(out, file, (int32_t)uprv_strlen(file));
-        T_FileStream_write(out, "\")\n", 3);
-        datFile=T_FileStream_open(fileName,"w");
-
-        if(!dest){
-            *status=U_MEMORY_ALLOCATION_ERROR;
-        }
-
-        u_strToUTF8(dest,8*res->u.fString.fLength,&len,res->u.fString.fChars,res->u.fString.fLength,status);
-        if(U_FAILURE(*status)){
-            T_FileStream_close(datFile);
-            uprv_free(dest);
-            return;
-        }
-        T_FileStream_write(datFile,dest,len);
-        T_FileStream_close(datFile);
-        uprv_free(dest);
-}
-#define MAX_SEQUENCE_LENGTH 30000
 /* Writing Functions */
 static void
 string_write_java(struct SResource *res,UErrorCode *status) {
-    if(res->fKey > 0 && uprv_strcmp(srBundle->fKeys+res->fKey,"%%UCARULES")==0 ){
-
-        const char* file = "UCARules.utf8";
-        write_utf8_file(res, file, status);
-    }else if(res->fKey > 0 && uprv_strcmp(srBundle->fKeys+res->fKey,"Sequence")==0
-             && res->fType == RES_STRING
-             && res->u.fString.fLength > MAX_SEQUENCE_LENGTH){
-        char file[1024] = {0};
-        uprv_strcpy(file, "CollationSequence_");
-        uprv_strcat(file, srBundle->fLocale);
-        uprv_strcat(file, ".utf8");
-        write_utf8_file(res, file, status);
-
-    }else{
-        str_write_java(res->u.fString.fChars,res->u.fString.fLength,TRUE,status);
-
-        if(res->fKey > 0 && uprv_strcmp(srBundle->fKeys+res->fKey,"Rule")==0){
-            UChar* buf = (UChar*) uprv_malloc(sizeof(UChar)*res->u.fString.fLength);
-            uprv_memcpy(buf,res->u.fString.fChars,res->u.fString.fLength);
-            uprv_free(buf);
-        }
+    char resKeyBuffer[8];
+    const char *resname = res_getKeyString(srBundle, res, resKeyBuffer);
+
+    str_write_java(res->u.fString.fChars,res->u.fString.fLength,TRUE,status);
+    
+       if(resname != NULL && uprv_strcmp(resname,"Rule")==0)
+       {
+        UChar* buf = (UChar*) uprv_malloc(sizeof(UChar)*res->u.fString.fLength);
+        uprv_memcpy(buf,res->u.fString.fChars,res->u.fString.fLength);
+        uprv_free(buf);
     }
 
 }
 
-static void
-alias_write_java(struct SResource *res,UErrorCode *status) {
-    static const char str[] = "new ICUListResourceBundle.Alias(";
-    write_tabs(out);
-    T_FileStream_write(out,str,(int32_t)uprv_strlen(str));
-
-    /*str_write_java(res->u.fString.fChars,res->u.fString.fLength,FALSE,status);*/
-    /*if(*res->u.fString.fChars == RES_PATH_SEPARATOR) {*/
-        /* there is a path included
-        locale = u_strchr(res->u.fString.fChars +1, RES_PATH_SEPARATOR);
-        *locale = 0;
-        locale++;
-
-        T_FileStream_write(out,"\"/",2);
-        T_FileStream_write(out,apName,(int32_t)uprv_strlen(apName));
-        T_FileStream_write(out,"/",1);
-        T_FileStream_write(out,abName,(int32_t)uprv_strlen(abName));
-        T_FileStream_write(out,"/\"+",3);
-        str_write_java(locale,res->u.fString.fLength-(locale-res->u.fString.fChars),FALSE,status);
-    } else {
-        str_write_java(res->u.fString.fChars,res->u.fString.fLength,FALSE,status);
-    }*/
-
-    str_write_java(res->u.fString.fChars,res->u.fString.fLength,FALSE,status);
-
-    T_FileStream_write(out,"),\n",3);
-}
-
 static void
 array_write_java( struct SResource *res, UErrorCode *status) {
 
     uint32_t  i         = 0;
     const char* arr ="new String[] { \n";
     struct SResource *current = NULL;
-    struct SResource *first =NULL;
-    UBool decrementTabs = FALSE;
     UBool allStrings    = TRUE;
 
     if (U_FAILURE(*status)) {
@@ -429,13 +349,11 @@ array_write_java( struct SResource *res, UErrorCode *status) {
             write_tabs(out);
             T_FileStream_write(out, object, (int32_t)uprv_strlen(object));
             tabCount++;
-            decrementTabs = TRUE;
         }else{
             write_tabs(out);
             T_FileStream_write(out, arr, (int32_t)uprv_strlen(arr));
             tabCount++;
         }
-        first=current;
         while (current != NULL) {
             /*if(current->fType==URES_STRING){
                 write_tabs(out);
@@ -464,15 +382,17 @@ array_write_java( struct SResource *res, UErrorCode *status) {
 static void
 intvector_write_java( struct SResource *res, UErrorCode *status) {
     uint32_t i = 0;
-    const char* intArr = "new Integer[] {\n";
-    const char* intC   = "new Integer(";
+    const char* intArr = "new int[] {\n";
+    /* const char* intC   = "new Integer(";   */
     const char* stringArr = "new String[]{\n";
+    char resKeyBuffer[8];
+    const char *resname = res_getKeyString(srBundle, res, resKeyBuffer);
     char buf[100];
     int len =0;
     buf[0]=0;
     write_tabs(out);
 
-    if(res->fKey > 0 && uprv_strcmp(srBundle->fKeys+res->fKey,"DateTimeElements")==0){
+    if(resname != NULL && uprv_strcmp(resname,"DateTimeElements")==0){
         T_FileStream_write(out, stringArr, (int32_t)uprv_strlen(stringArr));
         tabCount++;
         for(i = 0; i<res->u.fIntVector.fCount; i++) {
@@ -488,11 +408,12 @@ intvector_write_java( struct SResource *res, UErrorCode *status) {
         tabCount++;
         for(i = 0; i<res->u.fIntVector.fCount; i++) {
             write_tabs(out);
-            T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC));
+            /* T_FileStream_write(out, intC, (int32_t)uprv_strlen(intC)); */
             len=itostr(buf,res->u.fIntVector.fArray[i],10,0);
             T_FileStream_write(out,buf,len);
-            T_FileStream_write(out,"),",2);
-            T_FileStream_write(out,"\n",1);
+            /* T_FileStream_write(out,"),",2);  */
+            /* T_FileStream_write(out,"\n",1);  */
+            T_FileStream_write(out,",\n",2);
         }
     }
     tabCount--;
@@ -517,136 +438,75 @@ int_write_java(struct SResource *res,UErrorCode *status) {
 }
 
 static void
-bin_write_java( struct SResource *res, UErrorCode *status) {
-    const char* type = "new ICUListResourceBundle.CompressedBinary(";
-    const char* ext;
+bytes_write_java( struct SResource *res, UErrorCode *status) {
+       const char* type  = "new byte[] {";
+       const char* byteDecl = "%i, ";
+    char byteBuffer[100] = { 0 };
+       uint8_t*  byteArray = NULL;
+    int byteIterator = 0;
+       
     int32_t srcLen=res->u.fBinaryValue.fLength;
+       
+    if(srcLen>0 )
+       {
+        byteArray = res->u.fBinaryValue.fData;
 
-    if(srcLen>0 ){
-        uint16_t* target=NULL;
-        uint16_t* saveTarget = NULL;
-        int32_t tgtLen = 0;
-
-        if(res->fKey > 0 && (uprv_strcmp(srBundle->fKeys+res->fKey,"%%CollationBin")==0 || uprv_strcmp(srBundle->fKeys+res->fKey,"BreakDictionaryData")==0)){
-            char fileName[1024] ={0};
-            char fn[1024] =  {0};
-            FileStream* datFile = NULL;
-            if(uprv_strcmp(srBundle->fKeys+res->fKey,"BreakDictionaryData")==0){
-                uprv_strcat(fileName,"BreakDictionaryData");
-                ext = ".brk";
-            }else{
-                uprv_strcat(fileName,"CollationElements");
-                ext=".col";
-            }
-            if(uprv_strcmp(srBundle->fLocale,"root")!=0){
-                uprv_strcat(fileName,"_");
-                uprv_strcat(fileName,srBundle->fLocale);
-                if(NUMBER > ZERO){
-                    uprv_strcat(fileName, "_");
-                    uprv_strcat(fileName, &NUMBER);
-                }
-                NUMBER++;
-            }
-
-            uprv_strcat(fileName,ext);
-            if(outDir ){
-                uprv_strcat(fn,outDir);
-                if(outDir[uprv_strlen(outDir)-1]!=U_FILE_SEP_CHAR){
-                    uprv_strcat(fn,U_FILE_SEP_STRING);
-                }
-            }
-            uprv_strcat(fn,fileName);
-            type = "new ICUListResourceBundle.ResourceBinary(";
-            write_tabs(out);
-            T_FileStream_write(out, type, (int32_t)uprv_strlen(type));
-            T_FileStream_write(out, "\"", 1);
-            T_FileStream_write(out, fileName, (int32_t)uprv_strlen(fileName));
-            T_FileStream_write(out, "\"),\n", 4);
-
-            datFile=T_FileStream_open(fn,"w");
-            T_FileStream_write(datFile, res->u.fBinaryValue.fData, res->u.fBinaryValue.fLength);
-            T_FileStream_close(datFile);
-
-        }else{
-
-            srcLen = res->u.fBinaryValue.fLength;
-            tgtLen = srcLen * 2;
-            target = (uint16_t*)malloc(sizeof(uint16_t) * tgtLen);
-            saveTarget  = target;
-            if(target){
-                tgtLen = byteArrayToRLEString(res->u.fBinaryValue.fData,
-                                              srcLen,target, tgtLen,status);
-                if(U_FAILURE(*status)){
-                     printf("Could not encode got error : %s \n", u_errorName(*status));
-                     return;
-                }
-#if DEBUG
-                /***************** Test Roundtripping *********************/
-                {
-                    int32_t myTargetLen = rleStringToByteArray(target,tgtLen,NULL,0,status);
-                    uint8_t* myTarget = (uint8_t*) malloc(sizeof(uint8_t) * myTargetLen);
-
-                    /* test for NULL */
-                    if(myTarget == NULL) {
-                        *status = U_MEMORY_ALLOCATION_ERROR;
-                        return;
-                    }
-
-                    int i=0;
-                    int32_t retVal=0;
-
-                    *status = U_ZERO_ERROR;
-                    retVal=rleStringToByteArray(target,tgtLen,myTarget,myTargetLen,status);
-                    if(U_SUCCESS(*status)){
+        write_tabs(out);
+        T_FileStream_write(out, type, (int32_t)uprv_strlen(type));
+        T_FileStream_write(out, "\n", 1);
+        tabCount++;
 
-                        for(i=0; i< srcLen;i++){
-                            if(res->u.fBinaryValue.fData[i]!= myTarget[i]){
-                                printf("the encoded string cannot be decoded Expected : 0x%02X Got : %: 0x%02X at %i\n",res->u.fBinaryValue.fData[i],myTarget[i], i);
-                            }
-                        }
-                    }else{
-                        printf("Could not decode got error : %s \n", u_errorName(*status));
-                    }
-                    free(myTarget);
+               for (;byteIterator<srcLen;byteIterator++)
+               {
+            if (byteIterator%16 == 0)
+                       {
+                           write_tabs(out);
+                       }
+
+                       if (byteArray[byteIterator] < 128)
+                       {
+                sprintf(byteBuffer, byteDecl, byteArray[byteIterator]);
+                       }
+                       else
+                       {
+                sprintf(byteBuffer, byteDecl, (byteArray[byteIterator]-256));
+                       }
+
+            T_FileStream_write(out, byteBuffer, (int32_t)uprv_strlen(byteBuffer));
+
+                       if (byteIterator%16 == 15)
+                       {
+                T_FileStream_write(out, "\n", 1);
+                       }
 
-                }
-#endif
+               }
 
-            }else{
-                *status = U_MEMORY_ALLOCATION_ERROR;
-                return;
-            }
-
-            write_tabs(out);
-            T_FileStream_write(out, type, (int32_t)uprv_strlen(type));
+        if (((byteIterator-1)%16) != 15)
+               {
             T_FileStream_write(out, "\n", 1);
-            tabCount++;
-            write_tabs(out);
-            str_write_java(target, tgtLen,FALSE, status);
-            tabCount--;
-            T_FileStream_write(out, "),\n", 3);
-
-            free(target);
+           }
 
-        }
+               tabCount--;
+        write_tabs(out);
+               T_FileStream_write(out, "},\n", 3);
 
-   }else{
+       }
+       else
+    {
+               /* Empty array */
         write_tabs(out);
         T_FileStream_write(out,type,(int32_t)uprv_strlen(type));
-        T_FileStream_write(out,"null),\n",7);
-   }
+               T_FileStream_write(out,"},\n",3);
+    }
 
 }
 
-
 static UBool start = TRUE;
 
 static void
 table_write_java(struct SResource *res, UErrorCode *status) {
     uint32_t  i         = 0;
-    UBool allStrings =TRUE;
     struct SResource *current = NULL;
-    struct SResource *save = NULL;
     const char* obj = "new Object[][]{\n";
 
     if (U_FAILURE(*status)) {
@@ -660,11 +520,14 @@ table_write_java(struct SResource *res, UErrorCode *status) {
             tabCount++;
         }
         start = FALSE;
-        save = current = res->u.fTable.fFirst;
+        current = res->u.fTable.fFirst;
         i       = 0;
 
 
         while (current != NULL) {
+            char currentKeyBuffer[8];
+            const char *currentKeyString = res_getKeyString(srBundle, current, currentKeyBuffer);
+
             assert(i < res->u.fTable.fCount);
             write_tabs(out);
 
@@ -672,13 +535,12 @@ table_write_java(struct SResource *res, UErrorCode *status) {
 
 
             tabCount++;
-            allStrings=FALSE;
 
             write_tabs(out);
-            if(current->fKey > 0){
+            if(currentKeyString != NULL) {
                 T_FileStream_write(out, "\"", 1);
-                T_FileStream_write(out, srBundle->fKeys+current->fKey,
-                                   (int32_t)uprv_strlen(srBundle->fKeys+current->fKey));
+                T_FileStream_write(out, currentKeyString,
+                                   (int32_t)uprv_strlen(currentKeyString));
                 T_FileStream_write(out, "\",\n", 2);
 
                 T_FileStream_write(out, "\n", 1);
@@ -723,13 +585,14 @@ res_write_java(struct SResource *res,UErrorCode *status) {
              string_write_java    (res, status);
              return;
         case URES_ALIAS:
-             alias_write_java     (res, status);
-             return;
+             printf("Encountered unsupported resource type %d of alias\n", res->fType);
+             *status = U_UNSUPPORTED_ERROR;
+                        return;
         case URES_INT_VECTOR:
              intvector_write_java (res, status);
              return;
         case URES_BINARY:
-             bin_write_java       (res, status);
+             bytes_write_java     (res, status);
              return;
         case URES_INT:
              int_write_java       (res, status);
@@ -738,10 +601,8 @@ res_write_java(struct SResource *res,UErrorCode *status) {
              array_write_java     (res, status);
              return;
         case URES_TABLE:
-        case URES_TABLE32:
              table_write_java     (res, status);
              return;
-
         default:
             break;
         }
@@ -758,9 +619,11 @@ bundle_write_java(struct SRBRoot *bundle, const char *outputDir,const char* outp
 
     char fileName[256] = {'\0'};
     char className[256]={'\0'};
-    char constructor[1000] = { 0 };
-    UBool j1 =FALSE;
-    outDir = outputDir;
+    /*char constructor[1000] = { 0 };*/
+    /*UBool j1 =FALSE;*/
+    /*outDir = outputDir;*/
+
+    start = TRUE;                        /* Reset the start indictor*/
 
     bName = (bundleName==NULL) ? "LocaleElements" : bundleName;
     pName = (packageName==NULL)? "com.ibm.icu.impl.data" : packageName;
@@ -806,12 +669,15 @@ bundle_write_java(struct SRBRoot *bundle, const char *outputDir,const char* outp
     T_FileStream_write(out,";\n\n",3);
     T_FileStream_write(out, javaClass, (int32_t)uprv_strlen(javaClass));
     T_FileStream_write(out, className, (int32_t)uprv_strlen(className));
-    if(j1){
-        T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1));
-    }else{
-        sprintf(constructor,javaClassICU,className);
-        T_FileStream_write(out, constructor, (int32_t)uprv_strlen(constructor));
-    }
+       T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1));
+
+    /* if(j1){
+          T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1));
+       }else{
+           sprintf(constructor,javaClassICU,className);
+           T_FileStream_write(out, constructor, (int32_t)uprv_strlen(constructor));
+       }
+    */
 
     if(outputEnc && *outputEnc!='\0'){
         /* store the output encoding */