Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

langloadstreamer.cpp

Go to the documentation of this file.
00001 //============================================================================= 
00002 //
00003 // XDFLengine library
00004 //
00005 //-----------------------------------------------------------------------------
00006 //  VALUESHANDLER.CPP
00007 //-----------------------------------------------------------------------------
00013 //_____________________________________________________________________________
00014 //
00015 //  Copyright (C) 2003 Guillaume Baurand. All Rights Reserved.
00016 //
00017 //  This file is part of the XDFLengine project.
00018 //
00019 //  The XDFLengine is free software; you can redistribute it and/or modify
00020 //  it under the terms of the GNU General Public License as published by
00021 //  the Free Software Foundation; either version 2 of the License, or
00022 //  (at your option) any later version.
00023 //
00024 //  This program is distributed in the hope that it will be useful,
00025 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00026 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00027 //  GNU General Public License for more details.
00028 //
00029 //  You should have received a copy of the GNU General Public License
00030 //  along with this program; if not, write to the Free Software
00031 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
00032 //  USA.
00033 //
00034 //  For more information, 
00035 //      contact  : guillaume@baurand.net 
00036 //      or visit : http://xdflengine.sourceforge.net
00037 //
00038 //=============================================================================
00039 
00040 #   include "langloadstreamer.hpp"
00041 #   include "flow/consumers/streamsax2parser.hpp"
00042 #   include "processor/xmlflowcontext.hpp"
00043 #   include "streamers/generic/saxstreamer.hpp"
00044 
00045 BEGIN_XDFLENGINE_NS
00046 
00047 
00048 //=============================================================================
00049 //  CLASS LangLoadStreamerFactory
00050 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00051 
00052     //_____________________________________________________________________
00053     //  LangLoadStreamerFactory
00054     //---------------------------------------------------------------------
00055     LangLoadStreamerFactory::LangLoadStreamerFactory()
00056     {
00057         m_pCurDictionnary = 0;
00058         m_pCurLangPack = 0;
00059         m_pszCurLangPack = copyCharBuffer("");
00060         m_pszCurDictionnary = copyCharBuffer("");
00061     }
00062 
00063     //_____________________________________________________________________
00064     //  ~LangLoadStreamerFactory
00065     //---------------------------------------------------------------------
00066     LangLoadStreamerFactory::~LangLoadStreamerFactory()
00067     {
00068         VAlangPack *        l_pLangPack = 0;
00069         VAdictionnary *     l_pDictionnary = 0;
00070 
00071         while(!m_vaLangPacks.empty())
00072         {
00073             l_pLangPack = m_vaLangPacks.pop();
00074             while(! l_pLangPack->empty())
00075             {
00076                 l_pDictionnary = l_pLangPack->pop();
00077                 while(! l_pDictionnary->empty()) releaseCharBuffer( l_pDictionnary->pop());
00078                 delete l_pDictionnary;
00079             }
00080             delete l_pLangPack;
00081         }
00082 
00083         m_pszCurLangPack = releaseCharBuffer(m_pszCurLangPack);
00084         m_pszCurDictionnary = releaseCharBuffer(m_pszCurDictionnary);
00085 
00086     }
00087 
00088 
00089     //_____________________________________________________________________
00090     //  ~getStreamer
00091     //---------------------------------------------------------------------
00092     XMLStreamer* LangLoadStreamerFactory::getStreamer(
00093         StreamerParams*         p_pParameters, 
00094         XMLFlowContext*         p_pStreamContext, 
00095         XMLStreamConsumer*      p_pStreamConsumer) const
00096     {
00097         //---------------------------------------------------------------------
00098         SAXLangHandler* l_pSaxRawHandler;
00099         //---------------------------------------------------------------------
00100 
00101         l_pSaxRawHandler = new SAXLangHandler( p_pParameters, p_pStreamContext, this);
00102         return (XMLStreamer*) new SaxStreamer(  this, p_pParameters, p_pStreamContext, p_pStreamConsumer, l_pSaxRawHandler, true);
00103     }
00104 
00105 
00106 
00107     //_____________________________________________________________________
00108     //  setDictionnaryEntry
00109     //---------------------------------------------------------------------
00110     void LangLoadStreamerFactory::setDictionnaryEntry( const char* p_pszLangCode, const char* p_pszDicName, const char* p_pszKey, char* p_pszWord)
00111     {
00112         
00113         if( strcmp( m_pszCurLangPack, p_pszLangCode) != 0 )
00114         {
00115             // language pack requested is not last accessed : get the requested language pack
00116             m_pCurLangPack = m_vaLangPacks.get( p_pszLangCode);
00117 
00118             if( !m_pCurLangPack)
00119             {
00120                 //create new language pack
00121                 m_pCurLangPack = new VAlangPack;
00122                 m_vaLangPacks.add( m_pCurLangPack, p_pszLangCode);
00123             }
00124 
00125             m_pszCurLangPack = importCharBuffer( m_pszCurLangPack,p_pszLangCode);
00126             m_pszCurDictionnary = importCharBuffer( m_pszCurDictionnary,"");
00127             m_pCurDictionnary = 0;
00128 
00129         }
00130 
00131         if( strcmp( m_pszCurDictionnary, p_pszDicName) != 0 )
00132         {
00133             // dictionnary requested is not last accessed : get the requested dictionnary
00134             m_pCurDictionnary = m_pCurLangPack->get( p_pszDicName);
00135 
00136             if( !m_pCurDictionnary)
00137             {
00138                 //create new dictionnary
00139                 m_pCurDictionnary = new VAdictionnary;
00140                 m_pCurLangPack->add( m_pCurDictionnary, p_pszDicName);
00141                 logout << "\t" << LANGLOADSTREAMER_TAGNAME<< "   :   " << "Loading "<< m_pszCurLangPack << "." << p_pszDicName << " language dictionnary.\n";
00142             }
00143 
00144             m_pszCurDictionnary = importCharBuffer( m_pszCurDictionnary,p_pszDicName);
00145 
00146         }
00147 
00148         m_pCurDictionnary->add( copyCharBuffer(p_pszWord), p_pszKey);
00149     }
00150 
00151     //_____________________________________________________________________
00152     //  getDictionnaryEntry
00153     //---------------------------------------------------------------------
00154     const char* LangLoadStreamerFactory::getDictionnaryEntry( const char* p_pszLangCode, const char* p_pszDicName, const char* p_pszKey) const
00155     {
00156         VAlangPack *        l_pLangPack = 0;
00157         VAdictionnary *     l_pDictionnary = 0;
00158         const char*         l_pszValue = 0;
00159 
00160         l_pLangPack = m_vaLangPacks.get( p_pszLangCode);
00161         if( l_pLangPack) l_pDictionnary = l_pLangPack->get( p_pszDicName);
00162         if( l_pDictionnary ) l_pszValue = l_pDictionnary->get( p_pszKey);
00163         if(l_pszValue) return l_pszValue;
00164         else return "";
00165     }
00166 
00167 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00168 //=============================================================================
00169 
00170 
00171 
00172 
00173 
00174 //=============================================================================
00175 //  CLASS SAXVALUESHANDLER
00176 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00177 
00178 
00179     //_________________________________________________________________________
00180     //  SAXVALUESHANDLER
00181     //-------------------------------------------------------------------------
00182     SAXLangHandler::SAXLangHandler( StreamerParams* p_pParameters,XMLFlowContext*   p_pContext, const LangLoadStreamerFactory* p_pParent)
00183     {   
00184 
00185         m_pContext = p_pContext;
00186         m_intCurLevel = 0;
00187         m_pParent = (LangLoadStreamerFactory*) p_pParent;
00188         m_pszCurLangPack=0;
00189         m_pszCurDictionnary=0;
00190 
00191         DEBUG_CREATE(SAXLangHandler)
00192     }
00193 
00194     //_________________________________________________________________________
00195     //  ~SAXVALUESHANDLER
00196     //-------------------------------------------------------------------------
00197     SAXLangHandler::~SAXLangHandler()
00198     {
00199 
00200         DEBUG_DEL(SAXLangHandler)
00201         m_pszCurLangPack=releaseCharBuffer( m_pszCurLangPack);
00202         m_pszCurDictionnary=releaseCharBuffer( m_pszCurDictionnary);
00203         while(!m_vaValues.empty()) releaseCharBuffer( m_vaValues.pop());
00204     }
00205 
00206 
00207     //  CONTENTHANDLER  
00208 
00209     //_________________________________________________________________________
00210     //  CHARACTERS
00211     //-------------------------------------------------------------------------
00212     void SAXLangHandler::characters(const XMLCh* const chars ,const unsigned int length)
00213     {
00214         //---------------------------------------------------------------------
00215         char*   l_pszChars = 0;
00216         char*   l_pszTrimmedChars = 0;
00217         char*   l_pszValue = 0;
00218         char*   l_pszName;
00219         //---------------------------------------------------------------------
00220 
00221         DEBUG_IN(SAXLangHandler::characters)
00222 
00223         if(m_intCurLevel>3)
00224         {
00225             l_pszChars = XSTR(m_pContext->getTranscoder(), chars, length);
00226             l_pszTrimmedChars = trimCharBuffer(l_pszChars);
00227             releaseCharBuffer( l_pszChars);
00228              
00229             if(( strlen( l_pszTrimmedChars) > 0 ) && (! m_vaValues.empty() )) 
00230             {
00231                 l_pszName = copyCharBuffer( m_vaValues.getKey( m_vaValues.size() -1));
00232                 l_pszValue = m_vaValues.top();
00233                 m_vaValues.pop();
00234                 
00235                 l_pszValue = concatCharBuffer( l_pszValue, l_pszTrimmedChars);          
00236                 m_vaValues.add(l_pszValue, l_pszName);
00237                 l_pszName = releaseCharBuffer( l_pszName);
00238             }
00239 
00240             l_pszTrimmedChars = releaseCharBuffer( l_pszTrimmedChars);
00241         }
00242 
00243         DEBUG_OUT(SAXLangHandler::characters)
00244     }
00245 
00246     //_________________________________________________________________________
00247     //  STARTELEMENT
00248     //-------------------------------------------------------------------------
00249     void SAXLangHandler::startElement(  const XMLCh* const uri,  const XMLCh* const localname,   const XMLCh* const qname, const Attributes& attrs)
00250     {
00251         char*   l_pszValName = 0;
00252         char    l_szTagName[XMLPROCESSOR_MAX_TAG_SIZE];
00253 
00254         m_intCurLevel++;
00255 
00256         DEBUG_IN(SAXLangHandler::startElement)
00257 
00258         XSTR(m_pContext->getTranscoder(), localname, (char*) &l_szTagName);
00259 
00260         if(m_intCurLevel==2) m_pszCurLangPack = importCharBuffer(m_pszCurLangPack, l_szTagName);
00261         if(m_intCurLevel==3) m_pszCurDictionnary = importCharBuffer(m_pszCurDictionnary, l_szTagName);
00262         if(m_intCurLevel>3)
00263         {
00264             l_pszValName = 0;
00265             if(! m_vaValues.empty())
00266             {
00267                 l_pszValName = copyCharBuffer( m_vaValues.getKey( m_vaValues.size() -1));
00268                 l_pszValName = concatCharBuffer( l_pszValName, LANGLOADSTREAMER_VALSEP);
00269             }
00270 
00271             l_pszValName = concatCharBuffer( l_pszValName, (char*) &l_szTagName);
00272             m_vaValues.add( copyCharBuffer(""), l_pszValName);
00273             releaseCharBuffer( l_pszValName);
00274         }
00275 
00276         DEBUG_OUT(SAXLangHandler::startElement)
00277     }
00278 
00279     //_________________________________________________________________________
00280     //  ENDELEMENT
00281     //-------------------------------------------------------------------------
00282     void SAXLangHandler::endElement(  const XMLCh* const uri,    const XMLCh* const localname,   const XMLCh* const qname)
00283     {
00284         //---------------------------------------------------------------------
00285         char*           l_pszName = 0;
00286         char*           l_pszValue = 0;
00287         //---------------------------------------------------------------------
00288 
00289         DEBUG_IN(SAXLangHandler::endElement)
00290 
00291         if(m_intCurLevel>3)
00292         {
00293             if( !m_vaValues.empty()) 
00294             {   
00295                 l_pszName = copyCharBuffer(m_vaValues.getKey( m_vaValues.size() -1));
00296                 l_pszValue = m_vaValues.pop();          
00297                 if(strlen(l_pszValue)>0)
00298                 {
00299                     m_pParent->setDictionnaryEntry( m_pszCurLangPack, m_pszCurDictionnary, l_pszName, l_pszValue);
00300                 }
00301                 l_pszName = releaseCharBuffer( l_pszName);
00302                 l_pszValue = releaseCharBuffer( l_pszValue);
00303             }
00304         }
00305 
00306         m_intCurLevel--;
00307 
00308         DEBUG_OUT(SAXLangHandler::endElement)
00309     }
00310 
00311 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00312 //=============================================================================
00313 
00314 
00315 END_XDFLENGINE_NS
00316 
00317 
00318 

Generated on Sat Oct 4 13:19:59 2003 for XDFLengine by doxygen1.3-rc2