Win32MsgLoader.cpp
上传用户:huihehuasu
上传日期:2007-01-10
资源大小:6948k
文件大小:9k
源码类别:

xml/soap/webservice

开发平台:

C/C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  *
  26.  * 4. The names "Xerces" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation, and was
  51.  * originally based on software copyright (c) 1999, International
  52.  * Business Machines, Inc., http://www.ibm.com .  For more information
  53.  * on the Apache Software Foundation, please see
  54.  * <http://www.apache.org/>.
  55.  */
  56. /*
  57.  * $Id: Win32MsgLoader.cpp,v 1.6 2001/10/18 20:51:10 tng Exp $
  58.  */
  59. // ---------------------------------------------------------------------------
  60. //  Includes
  61. // ---------------------------------------------------------------------------
  62. #include <util/XercesDefs.hpp>
  63. #include <util/PlatformUtils.hpp>
  64. #include <util/XMLMsgLoader.hpp>
  65. #include <util/XMLString.hpp>
  66. #include <util/XMLUni.hpp>
  67. #include <windows.h>
  68. #include "Win32MsgLoader.hpp"
  69. // ---------------------------------------------------------------------------
  70. //  Public Constructors and Destructor
  71. // ---------------------------------------------------------------------------
  72. HINSTANCE globalModuleHandle;
  73. BOOL APIENTRY DllMain(HINSTANCE hModule,
  74.                       DWORD  ul_reason_for_call,
  75.                       LPVOID lpReserved)
  76. {
  77.   switch (ul_reason_for_call) {
  78.   case DLL_PROCESS_ATTACH:
  79.     globalModuleHandle = hModule;
  80.     break;
  81.   case DLL_THREAD_ATTACH:
  82.     break;
  83.   case DLL_THREAD_DETACH:
  84.     break;
  85.   case DLL_PROCESS_DETACH:
  86.     break;
  87.   }
  88.   return TRUE;
  89. }
  90. // ---------------------------------------------------------------------------
  91. //  Global module handle
  92. // ---------------------------------------------------------------------------
  93. Win32MsgLoader::Win32MsgLoader(const XMLCh* const msgDomain) :
  94.     fDomainOfs(0)
  95.     , fModHandle(0)
  96.     , fMsgDomain(0)
  97. {
  98.     // Try to get the module handle
  99.     fModHandle = globalModuleHandle;
  100.     if (!fModHandle)
  101.     {
  102.         //
  103.         //  If we didn't find it, its probably because its a development
  104.         //  build which is built as separate DLLs, so lets look for the DLL
  105.         //  that we are part of.
  106.         //
  107.         static const char* const privDLLName = "IXUTIL";
  108.         fModHandle = ::GetModuleHandleA(privDLLName);
  109.         // If neither exists, then we give up
  110.         if (!fModHandle)
  111.         {
  112.             // Probably have to call panic here
  113.         }
  114.     }
  115.     // Store the domain name
  116.     fMsgDomain = XMLString::replicate(msgDomain);
  117.     // And precalc the id offset we use for this domain
  118.     if (!XMLString::compareString(fMsgDomain, XMLUni::fgXMLErrDomain))
  119.         fDomainOfs = 0;
  120.     else if (!XMLString::compareString(fMsgDomain, XMLUni::fgExceptDomain))
  121.         fDomainOfs = 0x2000;
  122.     else if (!XMLString::compareString(fMsgDomain, XMLUni::fgValidityDomain))
  123.         fDomainOfs = 0x4000;
  124.     else
  125.         XMLPlatformUtils::panic(XMLPlatformUtils::Panic_UnknownMsgDomain);
  126. }
  127. Win32MsgLoader::~Win32MsgLoader()
  128. {
  129.     delete [] fMsgDomain;
  130. }
  131. // ---------------------------------------------------------------------------
  132. //  Implementation of the virtual message loader API
  133. // ---------------------------------------------------------------------------
  134. //
  135. //  This is the method that actually does the work of loading a message from
  136. //  the attached resources. Note that we don't use LoadStringW here, since it
  137. //  won't work on Win98. So we go the next level down and do what LoadStringW
  138. //  would have done, since this will work on either platform.
  139. //
  140. bool Win32MsgLoader::loadMsg(const  XMLMsgLoader::XMLMsgId  msgToLoad
  141.                             ,       XMLCh* const            toFill
  142.                             , const unsigned int            maxChars)
  143. {
  144.     // In case we error return, and they don't check it...
  145.     toFill[0] = 0;
  146.     // Adjust the message id by the domain offset
  147.     const unsigned int theMsgId = msgToLoad + fDomainOfs;
  148.     //
  149.     //  Figure out the actual id the id, adjusting it by the domain offset.
  150.     //  Then first we calculate the particular 16 string block that this id
  151.     //  is in, and the offset within that block of the string in question.
  152.     //
  153.     const unsigned int theBlock = (theMsgId >> 4) + 1;
  154.     const unsigned int theOfs   = theMsgId & 0x000F;
  155.     // Try to find this resource. If we fail to find it, return false
  156.     HRSRC hMsgRsc = ::FindResourceExA
  157.     (
  158.         fModHandle
  159.         , RT_STRING
  160.         , MAKEINTRESOURCE(theBlock)
  161.         , MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)
  162.     );
  163.     if (!hMsgRsc)
  164.         return false;
  165.     // We found it, so load the block. If this fails, also return a false
  166.     HGLOBAL hGbl = ::LoadResource(fModHandle, hMsgRsc);
  167.     if (!hGbl)
  168.         return false;
  169.     // Lock this resource into memory. Again, if it fails, just return false
  170.     const XMLCh* pBlock = (const XMLCh*)::LockResource(hGbl);
  171.     if (!pBlock)
  172.         return false;
  173.     //
  174.     //  Look through the block for our desired message. Its stored such that
  175.     //  the zeroth entry has the length minus the separator null.
  176.     //
  177.     for (unsigned int index = 0; index < theOfs; index++)
  178.         pBlock += *pBlock + 1;
  179.     // Calculate how many actual chars we will end up with
  180.     const unsigned int actualChars = ((maxChars < *pBlock) ? maxChars : *pBlock);
  181.     // Ok, finally now copy as much as we can into the caller's buffer
  182.     wcsncpy(toFill, pBlock + 1, actualChars);
  183.     toFill[actualChars] = 0;
  184.     return true;
  185. }
  186. bool Win32MsgLoader::loadMsg(const  XMLMsgLoader::XMLMsgId  msgToLoad
  187.                             ,       XMLCh* const            toFill
  188.                             , const unsigned int            maxChars
  189.                             , const XMLCh* const            repText1
  190.                             , const XMLCh* const            repText2
  191.                             , const XMLCh* const            repText3
  192.                             , const XMLCh* const            repText4)
  193. {
  194.     // Call the other version to load up the message
  195.     if (!loadMsg(msgToLoad, toFill, maxChars))
  196.         return false;
  197.     // And do the token replacement
  198.     XMLString::replaceTokens(toFill, maxChars, repText1, repText2, repText3, repText4);
  199.     return true;
  200. }
  201. bool Win32MsgLoader::loadMsg(const  XMLMsgLoader::XMLMsgId  msgToLoad
  202.                             ,       XMLCh* const            toFill
  203.                             , const unsigned int            maxChars
  204.                             , const char* const             repText1
  205.                             , const char* const             repText2
  206.                             , const char* const             repText3
  207.                             , const char* const             repText4)
  208. {
  209.     //
  210.     //  Transcode the provided parameters and call the other version,
  211.     //  which will do the replacement work.
  212.     //
  213.     XMLCh* tmp1 = 0;
  214.     XMLCh* tmp2 = 0;
  215.     XMLCh* tmp3 = 0;
  216.     XMLCh* tmp4 = 0;
  217.     bool bRet = false;
  218.     if (repText1)
  219.         tmp1 = XMLString::transcode(repText1);
  220.     if (repText2)
  221.         tmp2 = XMLString::transcode(repText2);
  222.     if (repText3)
  223.         tmp3 = XMLString::transcode(repText3);
  224.     if (repText4)
  225.         tmp4 = XMLString::transcode(repText4);
  226.     bRet = loadMsg(msgToLoad, toFill, maxChars, tmp1, tmp2, tmp3, tmp4);
  227.     if (tmp1)
  228.         delete [] tmp1;
  229.     if (tmp2)
  230.         delete [] tmp2;
  231.     if (tmp3)
  232.         delete [] tmp3;
  233.     if (tmp4)
  234.         delete [] tmp4;
  235.     return bRet;
  236. }