gloox  1.0.1
stanza.cpp
1 /*
2  Copyright (c) 2005-2012 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 #include "stanza.h"
15 #include "error.h"
16 #include "jid.h"
17 #include "util.h"
18 #include "stanzaextension.h"
19 #include "stanzaextensionfactory.h"
20 
21 #include <cstdlib>
22 
23 namespace gloox
24 {
25 
26  Stanza::Stanza( const JID& to )
27  : m_xmllang( "default" ), m_to( to )
28  {
29  }
30 
32  : m_xmllang( "default" )
33  {
34  if( !tag )
35  return;
36 
37  m_from.setJID( tag->findAttribute( "from" ) );
38  m_to.setJID( tag->findAttribute( "to" ) );
39  m_id = tag->findAttribute( "id" );
40  }
41 
43  {
45  }
46 
47  const Error* Stanza::error() const
48  {
49  return findExtension<Error>( ExtError );
50  }
51 
53  {
54  m_extensionList.push_back( se );
55  }
56 
57  const StanzaExtension* Stanza::findExtension( int type ) const
58  {
59  StanzaExtensionList::const_iterator it = m_extensionList.begin();
60  for( ; it != m_extensionList.end() && (*it)->extensionType() != type; ++it ) ;
61  return it != m_extensionList.end() ? (*it) : 0;
62  }
63 
65  {
66  util::clearList( m_extensionList );
67  }
68 
69  void Stanza::setLang( StringMap** map,
70  std::string& defaultLang,
71  const Tag* tag )
72  {
73  const std::string& lang = tag ? tag->findAttribute( "xml:lang" ) : EmptyString;
74  setLang( map, defaultLang, tag ? tag->cdata() : EmptyString, lang );
75  }
76 
77  void Stanza::setLang( StringMap** map,
78  std::string& defaultLang,
79  const std::string& data,
80  const std::string& xmllang )
81  {
82  if( data.empty() )
83  return;
84 
85  if( xmllang.empty() )
86  defaultLang = data;
87  else
88  {
89  if( !*map )
90  *map = new StringMap();
91  (**map)[xmllang] = data;
92  }
93  }
94 
95  const std::string& Stanza::findLang( const StringMap* map,
96  const std::string& defaultData,
97  const std::string& lang )
98  {
99  if( map && lang != "default" )
100  {
101  StringMap::const_iterator it = map->find( lang );
102  if( it != map->end() )
103  return (*it).second;
104  }
105  return defaultData;
106  }
107 
108  void Stanza::getLangs( const StringMap* map,
109  const std::string& defaultData,
110  const std::string& name,
111  Tag* tag )
112  {
113  if( !defaultData.empty() )
114  new Tag( tag, name, defaultData );
115 
116  if( !map )
117  return;
118 
119  StringMap::const_iterator it = map->begin();
120  for( ; it != map->end(); ++it )
121  {
122  Tag* t = new Tag( tag, name, "xml:lang", (*it).first );
123  t->setCData( (*it).second );
124  }
125  }
126 
127 }