GME  13
MsgCatalogLoader.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: MsgCatalogLoader.cpp 614259 2008-01-22 16:59:21Z amassari $
00020  */
00021 
00022 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #include <xercesc/util/XercesDefs.hpp>
00027 #include <xercesc/util/PlatformUtils.hpp>
00028 #include <xercesc/util/XMLMsgLoader.hpp>
00029 #include <xercesc/util/XMLString.hpp>
00030 #include <xercesc/util/XMLUniDefs.hpp>
00031 #include <xercesc/util/XMLUni.hpp>
00032 #include "MsgCatalogLoader.hpp"
00033 #include "XMLMsgCat_Ids.hpp"
00034 
00035 #include <locale.h>
00036 #include <stdlib.h>
00037 #include <stdio.h>
00038 #include <string.h>
00039 
00040 XERCES_CPP_NAMESPACE_BEGIN
00041 
00042 // ---------------------------------------------------------------------------
00043 //  Public Constructors and Destructor
00044 // ---------------------------------------------------------------------------
00045 MsgCatalogLoader::MsgCatalogLoader(const XMLCh* const msgDomain)
00046 :fCatalogHandle(0)
00047 ,fMsgSet(0)
00048 {
00049     if (!XMLString::equals(msgDomain, XMLUni::fgXMLErrDomain)
00050     &&  !XMLString::equals(msgDomain, XMLUni::fgExceptDomain)
00051     &&  !XMLString::equals(msgDomain, XMLUni::fgXMLDOMMsgDomain)
00052     &&  !XMLString::equals(msgDomain, XMLUni::fgValidityDomain))
00053     {
00054         XMLPlatformUtils::panic(PanicHandler::Panic_UnknownMsgDomain);
00055     }
00056 
00057     // Prepare the path info
00058     char locationBuf[1024];
00059     memset(locationBuf, 0, sizeof locationBuf);
00060     const char *nlsHome = XMLMsgLoader::getNLSHome();
00061     
00062     if (nlsHome)
00063     {
00064         strcpy(locationBuf, nlsHome);
00065         strcat(locationBuf, "/");
00066     }
00067     else
00068     {
00069         nlsHome = getenv("XERCESC_NLS_HOME");
00070         if (nlsHome)
00071         {
00072             strcpy(locationBuf, nlsHome);
00073             strcat(locationBuf, "/");
00074         }
00075         else
00076         {
00077             nlsHome = getenv("XERCESCROOT");
00078             if (nlsHome)
00079             {                           
00080                 strcpy(locationBuf, nlsHome);
00081                 strcat(locationBuf, "/msg/");
00082             }
00083         }    
00084     }
00085     
00086     // Prepare user-specified locale specific cat file
00087     char catuser[1024];
00088     memset(catuser, 0, sizeof catuser);
00089     strcpy(catuser, locationBuf);
00090     strcat(catuser, "XercesMessages_");
00091     strcat(catuser, XMLMsgLoader::getLocale());
00092     strcat(catuser, ".cat");
00093         
00094     char catdefault[1024];
00095     memset(catdefault, 0, sizeof catdefault);
00096     strcpy(catdefault, locationBuf);
00097     strcat(catdefault, "XercesMessages_en_US.cat");
00098 
00103     if ( ((fCatalogHandle=catopen(catuser, 0)) == (nl_catd)-1) &&
00104          ((fCatalogHandle=catopen(catdefault, 0)) == (nl_catd)-1)   )
00105     {
00106         // Probably have to call panic here
00107         printf("Could not open catalog:\n %s\n or %s\n", catuser, catdefault);
00108         XMLPlatformUtils::panic(PanicHandler::Panic_CantLoadMsgDomain);
00109     }
00110 
00111     if (XMLString::equals(msgDomain, XMLUni::fgXMLErrDomain))
00112         fMsgSet = CatId_XMLErrs;
00113     else if (XMLString::equals(msgDomain, XMLUni::fgExceptDomain))
00114         fMsgSet = CatId_XMLExcepts;
00115     else if (XMLString::equals(msgDomain, XMLUni::fgValidityDomain))
00116         fMsgSet = CatId_XMLValid;
00117     else if (XMLString::equals(msgDomain, XMLUni::fgXMLDOMMsgDomain))
00118         fMsgSet = CatId_XMLDOMMsg;
00119 }
00120 
00121 MsgCatalogLoader::~MsgCatalogLoader()
00122 {
00123     catclose(fCatalogHandle);   
00124 }
00125 
00126 
00127 // ---------------------------------------------------------------------------
00128 //  Implementation of the virtual message loader API
00129 // ---------------------------------------------------------------------------
00130 bool MsgCatalogLoader::loadMsg(const  XMLMsgLoader::XMLMsgId  msgToLoad
00131                               ,       XMLCh*   const          toFill
00132                               , const XMLSize_t               maxChars)
00133 {
00134     char msgString[100];
00135     sprintf(msgString, "Could not find message ID %d from message set %d\n", msgToLoad, fMsgSet);
00136     char* catMessage = catgets( fCatalogHandle, fMsgSet, (int)msgToLoad, msgString);
00137 
00138     // catgets returns a pointer to msgString if it fails to locate the message
00139     // from the message catalog
00140     if (XMLString::equals(catMessage, msgString))
00141         return false;
00142     else
00143     {
00144         XMLString::transcode(catMessage, toFill, maxChars);
00145         return true;
00146     }
00147         
00148 }
00149 
00150 bool MsgCatalogLoader::loadMsg(const  XMLMsgLoader::XMLMsgId  msgToLoad
00151                             ,       XMLCh* const            toFill
00152                             , const XMLSize_t               maxChars
00153                             , const XMLCh* const            repText1
00154                             , const XMLCh* const            repText2
00155                             , const XMLCh* const            repText3
00156                             , const XMLCh* const            repText4
00157                             , MemoryManager* const          manager)
00158 {
00159     // Call the other version to load up the message
00160     if (!loadMsg(msgToLoad, toFill, maxChars))
00161         return false;
00162 
00163     // And do the token replacement
00164     XMLString::replaceTokens(toFill, maxChars, repText1, repText2, repText3, repText4, manager);
00165     return true;
00166 }
00167 
00168 
00169 bool MsgCatalogLoader::loadMsg(const  XMLMsgLoader::XMLMsgId  msgToLoad
00170                             ,       XMLCh* const            toFill
00171                             , const XMLSize_t               maxChars
00172                             , const char* const             repText1
00173                             , const char* const             repText2
00174                             , const char* const             repText3
00175                             , const char* const             repText4
00176                             , MemoryManager * const         manager) 
00177 {
00178     //
00179     //  Transcode the provided parameters and call the other version,
00180     //  which will do the replacement work.
00181     //
00182     XMLCh* tmp1 = 0;
00183     XMLCh* tmp2 = 0;
00184     XMLCh* tmp3 = 0;
00185     XMLCh* tmp4 = 0;    
00186 
00187     bool bRet = false;
00188     if (repText1)
00189         tmp1 = XMLString::transcode(repText1, manager);
00190     if (repText2)
00191         tmp2 = XMLString::transcode(repText2, manager);
00192     if (repText3)
00193         tmp3 = XMLString::transcode(repText3, manager);
00194     if (repText4)
00195         tmp4 = XMLString::transcode(repText4, manager);
00196 
00197     bRet = loadMsg(msgToLoad, toFill, maxChars, tmp1, tmp2, tmp3, tmp4, manager);
00198 
00199     if (tmp1)
00200         manager->deallocate(tmp1);//delete [] tmp1;
00201     if (tmp2)
00202         manager->deallocate(tmp2);//delete [] tmp2;
00203     if (tmp3)
00204         manager->deallocate(tmp3);//delete [] tmp3;
00205     if (tmp4)
00206         manager->deallocate(tmp4);//delete [] tmp4;
00207 
00208     return bRet;
00209 }
00210 
00211 XERCES_CPP_NAMESPACE_END