stanza.cpp

00001 /*
00002   Copyright (c) 2005-2009 by Jakob Schroeter <js@camaya.net>
00003   This file is part of the gloox library. http://camaya.net/gloox
00004 
00005   This software is distributed under a license. The full license
00006   agreement can be found in the file LICENSE in this distribution.
00007   This software may not be copied, modified, sold or distributed
00008   other than expressed in the named license agreement.
00009 
00010   This software is distributed without any warranty.
00011 */
00012 
00013 
00014 #include "stanza.h"
00015 #include "error.h"
00016 #include "jid.h"
00017 #include "util.h"
00018 #include "stanzaextension.h"
00019 #include "stanzaextensionfactory.h"
00020 
00021 #include <cstdlib>
00022 
00023 namespace gloox
00024 {
00025 
00026   Stanza::Stanza( const JID& to )
00027     : m_xmllang( "default" ), m_to( to )
00028   {
00029   }
00030 
00031   Stanza::Stanza( Tag* tag )
00032     : m_xmllang( "default" )
00033   {
00034     if( !tag )
00035       return;
00036 
00037     m_from.setJID( tag->findAttribute( "from" ) );
00038     m_to.setJID( tag->findAttribute( "to" ) );
00039     m_id = tag->findAttribute( "id" );
00040   }
00041 
00042   Stanza::~Stanza()
00043   {
00044     removeExtensions();
00045   }
00046 
00047   const Error* Stanza::error() const
00048   {
00049     return findExtension<Error>( ExtError );
00050   }
00051 
00052   void Stanza::addExtension( const StanzaExtension* se )
00053   {
00054     m_extensionList.push_back( se );
00055   }
00056 
00057   const StanzaExtension* Stanza::findExtension( int type ) const
00058   {
00059     StanzaExtensionList::const_iterator it = m_extensionList.begin();
00060     for( ; it != m_extensionList.end() && (*it)->extensionType() != type; ++it ) ;
00061     return it != m_extensionList.end() ? (*it) : 0;
00062   }
00063 
00064   void Stanza::removeExtensions()
00065   {
00066     util::clearList( m_extensionList );
00067   }
00068 
00069   void Stanza::setLang( StringMap** map,
00070                         std::string& defaultLang,
00071                         const Tag* tag )
00072   {
00073     const std::string& lang = tag ? tag->findAttribute( "xml:lang" ) : EmptyString;
00074     setLang( map, defaultLang, tag ? tag->cdata() : EmptyString, lang );
00075   }
00076 
00077   void Stanza::setLang( StringMap** map,
00078                         std::string& defaultLang,
00079                         const std::string& data,
00080                         const std::string& xmllang )
00081   {
00082     if( data.empty() )
00083       return;
00084 
00085     if( xmllang.empty() )
00086       defaultLang = data;
00087     else
00088     {
00089       if( !*map )
00090         *map = new StringMap();
00091       (**map)[xmllang] = data;
00092     }
00093   }
00094 
00095   const std::string& Stanza::findLang( const StringMap* map,
00096                                        const std::string& defaultData,
00097                                        const std::string& lang )
00098   {
00099     if( map && lang != "default" )
00100     {
00101       StringMap::const_iterator it = map->find( lang );
00102       if( it != map->end() )
00103         return (*it).second;
00104     }
00105     return defaultData;
00106   }
00107 
00108   void Stanza::getLangs( const StringMap* map,
00109                          const std::string& defaultData,
00110                          const std::string& name,
00111                          Tag* tag )
00112   {
00113     if( !defaultData.empty() )
00114       new Tag( tag, name, defaultData );
00115 
00116     if( !map )
00117       return;
00118 
00119     StringMap::const_iterator it = map->begin();
00120     for( ; it != map->end(); ++it )
00121     {
00122       Tag* t = new Tag( tag, name, "xml:lang", (*it).first );
00123       t->setCData( (*it).second );
00124     }
00125   }
00126 
00127 }
Generated on Tue Jun 8 23:37:54 2010 for gloox by  doxygen 1.6.3