From 2f3c11f757a1aab3f1fe47a5111aaaa3a38a07bf Mon Sep 17 00:00:00 2001
From: PeiYong Zhang <peiyongz@apache.org>
Date: Mon, 30 Sep 2002 22:20:40 +0000
Subject: [PATCH] Build with ICU MsgLoader

git-svn-id: https://svn.apache.org/repos/asf/xerces/c/trunk@174262 13f79535-47bb-0310-9956-ffa450edef68
---
 .../util/MsgLoaders/ICU/ICUMsgLoader.cpp      | 143 +++++++++---------
 .../util/MsgLoaders/ICU/ICUMsgLoader.hpp      |  25 +--
 2 files changed, 84 insertions(+), 84 deletions(-)

diff --git a/src/xercesc/util/MsgLoaders/ICU/ICUMsgLoader.cpp b/src/xercesc/util/MsgLoaders/ICU/ICUMsgLoader.cpp
index b126ab3ab..b1affd1bf 100644
--- a/src/xercesc/util/MsgLoaders/ICU/ICUMsgLoader.cpp
+++ b/src/xercesc/util/MsgLoaders/ICU/ICUMsgLoader.cpp
@@ -56,8 +56,11 @@
 
 /*
  * $Log$
- * Revision 1.1  2002/02/01 22:22:19  peiyongz
- * Initial revision
+ * Revision 1.2  2002/09/30 22:20:40  peiyongz
+ * Build with ICU MsgLoader
+ *
+ * Revision 1.1.1.1  2002/02/01 22:22:19  peiyongz
+ * sane_include
  *
  * Revision 1.7  2002/01/21 14:52:25  tng
  * [Bug 5847] ICUMsgLoader can't be compiled with gcc 3.0.3 and ICU2.  And also fix the memory leak introduced by Bug 2730 fix.
@@ -99,41 +102,67 @@
 #include <xercesc/util/PlatformUtils.hpp>
 #include <xercesc/util/XMLMsgLoader.hpp>
 #include <xercesc/util/XMLString.hpp>
+#include <xercesc/util/XMLUniDefs.hpp>
 #include "ICUMsgLoader.hpp"
 
 #include "string.h"
-
+#include <stdio.h>
+#include <stdlib.h>
 
 // ---------------------------------------------------------------------------
 //  Local static methods
 // ---------------------------------------------------------------------------
-static const XMLCh* mapId(  const   XMLMsgLoader::XMLMsgId   msgToLoad
-                            , const XMLCh*                   bundleType)
-{
-    static const XMLCh test[] = { 0x65, 0x66, 0 };
-    return test;
-}
-
 
 // ---------------------------------------------------------------------------
 //  Public Constructors and Destructor
 // ---------------------------------------------------------------------------
-ICUMsgLoader::ICUMsgLoader(const XMLCh* const  toLoad) :
-    fBundle(0)
-    , fBundleType((XMLCh*)toLoad)
+ICUMsgLoader::ICUMsgLoader(const XMLCh* const  msgDomain) 
+:fRootBundle(0)
+,fDomainBundle(0)
 {
-    // Ok, lets try to create the bundle now
+    // validation on msgDomain
+    if (XMLString::compareString(msgDomain, XMLUni::fgXMLErrDomain)
+    &&  XMLString::compareString(msgDomain, XMLUni::fgExceptDomain)
+    &&  XMLString::compareString(msgDomain, XMLUni::fgValidityDomain))
+    {
+        XMLPlatformUtils::panic(XMLPlatformUtils::Panic_UnknownMsgDomain);
+    }
+
+    //
+    // we hardcode the path for "root.res" for now
+    // assuming that Makefile would copy root.res from $ICUROOT/bin to $XERCESCROOT/bin
+    //
+    char tempBuf[1024];
+    strcpy(tempBuf, getenv("XERCESCROOT"));
+    strcat(tempBuf, "/bin/root.res");
+   
     UErrorCode err = U_ZERO_ERROR;
-    fBundle = new ResourceBundle(0, err);
-    if (!U_SUCCESS(err))
+    fRootBundle = ures_open(tempBuf, 0, &err);
+    if (!U_SUCCESS(err) || fRootBundle == NULL)
+    {
+        XMLPlatformUtils::panic(XMLPlatformUtils::Panic_CantLoadMsgDomain);
+    }
+    
+     //strip off path information, if any
+     int   index = XMLString::lastIndexOf(msgDomain, chForwardSlash);
+     char *domainName = XMLString::transcode(&(msgDomain[index + 1]));
+
+     // get the resource bundle for the domain
+     fDomainBundle = ures_getByKey(fRootBundle, domainName, NULL, &err);
+
+     delete [] domainName;
+ 
+     if (!U_SUCCESS(err) || fDomainBundle == NULL)
     {
-        // <TBD> Need to panic again here?
+        XMLPlatformUtils::panic(XMLPlatformUtils::Panic_CantLoadMsgDomain);
     }
+
 }
 
 ICUMsgLoader::~ICUMsgLoader()
 {
-    delete fBundle;
+    ures_close(fDomainBundle);
+    ures_close(fRootBundle);
 }
 
 
@@ -141,70 +170,38 @@ ICUMsgLoader::~ICUMsgLoader()
 //  Implementation of the virtual message loader API
 // ---------------------------------------------------------------------------
 bool ICUMsgLoader::loadMsg( const   XMLMsgLoader::XMLMsgId  msgToLoad
-                            ,       XMLCh* const            toFill
-                            , const unsigned int           maxChars)
+                          ,         XMLCh* const            toFill
+                          , const   unsigned int            maxChars)
 {
-    //
-    //  Map the passed id to the required message bundle key name. We use
-    //  a local array to map the message id to the correct string. This array
-    //  is generated from a little utility program that can kick out the
-    //  message text in a couple of different formats.
-    //
-    const XMLCh* const keyStr = mapId(msgToLoad, fBundleType);
-    if (!keyStr)
-        return false;
-
-    // And now try to load that key's related message
-    UErrorCode err;
-//    UnicodeString keyVal(keyStr);
-//    const UnicodeString* msgString = fBundle->getStringEx(keyVal, err);
-//    if (!msgString)
-//        return false;
+    UErrorCode   err = U_ZERO_ERROR;
+    int32_t      strLen = 0;
 
-    char* tempKeyStr = XMLString::transcode(keyStr);
-    UnicodeString msgString = fBundle->getStringEx(tempKeyStr, err);
-    delete [] tempKeyStr;
+    // Assuming array format
+    const UChar *name = ures_getStringByIndex(fDomainBundle, (int32_t)msgToLoad-1, &strLen, &err);
 
-    // Extract out from the UnicodeString to the passed buffer
-    const unsigned int len = msgString.length();
-    const unsigned int lesserLen = (len < maxChars) ? len : maxChars;
-
-    //
-    //  And now do the extract. This works differently according to
-    //  whether XMLCh and UChar are the same size or not.
-    //
-    UChar* startTarget;
-    if (sizeof(XMLCh) == sizeof(UChar))
-        startTarget = (UChar*)toFill;
-     else
-        startTarget = new UChar[maxChars];
-    UChar* orgTarget = startTarget;
+    if (!U_SUCCESS(err) || (name == NULL))
+    {
+        return false;
+    }
 
-    msgString.extract
-    (
-        0
-        , lesserLen
-        , startTarget
-    );
+    int retStrLen = strLen > maxChars ? maxChars : strLen;
 
-    //
-    //  If XMLCh and UChar are not the same size, then we need to copy over
-    //  the temp buffer to the new one.
-    //
-    if (sizeof(UChar) != sizeof(XMLCh))
+    if (sizeof(UChar)==sizeof(XMLCh))
     {
-        XMLCh* outPtr = toFill;
-        startTarget = orgTarget;
-        for (unsigned int index = 0; index < lesserLen; index++)
-            *outPtr++ = XMLCh(*startTarget++);
-
-        // And delete the temp buffer
-        delete [] orgTarget;
+        XMLString::moveChars(toFill, (XMLCh*)name, retStrLen);
+        toFill[retStrLen] = (XMLCh) 0;
     }
+    else
+    {
+        XMLCh* retStr = toFill;
+        const UChar *srcPtr = name;
 
+        while (retStrLen--)
+           *retStr++ = *srcPtr++;
+
+        *retStr = 0;
+    }
 
-    // Cap it off and return success
-    toFill[lesserLen] = 0;
     return true;
 }
 
diff --git a/src/xercesc/util/MsgLoaders/ICU/ICUMsgLoader.hpp b/src/xercesc/util/MsgLoaders/ICU/ICUMsgLoader.hpp
index c72e92a70..91cc04998 100644
--- a/src/xercesc/util/MsgLoaders/ICU/ICUMsgLoader.hpp
+++ b/src/xercesc/util/MsgLoaders/ICU/ICUMsgLoader.hpp
@@ -56,8 +56,11 @@
 
 /*
  * $Log$
- * Revision 1.1  2002/02/01 22:22:19  peiyongz
- * Initial revision
+ * Revision 1.2  2002/09/30 22:20:40  peiyongz
+ * Build with ICU MsgLoader
+ *
+ * Revision 1.1.1.1  2002/02/01 22:22:19  peiyongz
+ * sane_include
  *
  * Revision 1.5  2002/01/21 14:52:25  tng
  * [Bug 5847] ICUMsgLoader can't be compiled with gcc 3.0.3 and ICU2.  And also fix the memory leak introduced by Bug 2730 fix.
@@ -91,7 +94,7 @@
 
 #include <xercesc/util/XercesDefs.hpp>
 #include <xercesc/util/XMLMsgLoader.hpp>
-#include "unicode/resbund.h"
+#include "unicode/ures.h"
 
 
 //
@@ -104,7 +107,7 @@ public :
     // -----------------------------------------------------------------------
     //  Public Constructors and Destructor
     // -----------------------------------------------------------------------
-    ICUMsgLoader(const XMLCh* const  toLoad);
+    ICUMsgLoader(const XMLCh* const  msgDomain);
     ~ICUMsgLoader();
 
 
@@ -153,15 +156,15 @@ private :
     // -----------------------------------------------------------------------
     //  Private data members
     //
-    //  fBundle
-    //      This is the ICU resource bundle that we use to track messages.
+    //  fRootBundle
+    //      bundle to the 'root' of the specified locale_country
+    //
+    //  fDomainBundle
+    //      bundle to the resource for the corresponding domain 
     //
-    //  fBundleType
-    //      This is the bundle that we are using. This is used to map message
-    //      ids to key values.
     // -----------------------------------------------------------------------
-    ResourceBundle*       fBundle;
-    XMLCh*                fBundleType;
+    UResourceBundle*      fRootBundle;
+    UResourceBundle*      fDomainBundle;
 };
 
 #endif
-- 
GitLab