gloox  1.0.9
stanza.cpp
1 /*
2  Copyright (c) 2005-2013 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 ), m_hasEmbeddedStanza( false )
28  {
29  }
30 
32  : m_xmllang( "default" ), m_hasEmbeddedStanza( false )
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 
70  {
71  StanzaExtensionList::const_iterator it = m_extensionList.begin();
72  for( ; it != m_extensionList.end() && !(*it)->embeddedStanza(); ++it ) ;
73  return it != m_extensionList.end() ? (*it)->embeddedStanza() : 0;
74  }
75 
77  {
78  StanzaExtensionList::const_iterator it = m_extensionList.begin();
79  for( ; it != m_extensionList.end() && !(*it)->embeddedTag(); ++it ) ;
80  return it != m_extensionList.end() ? (*it)->embeddedTag() : 0;
81  }
82 
83  void Stanza::setLang( StringMap** map,
84  std::string& defaultLang,
85  const Tag* tag )
86  {
87  const std::string& lang = tag ? tag->findAttribute( "xml:lang" ) : EmptyString;
88  setLang( map, defaultLang, tag ? tag->cdata() : EmptyString, lang );
89  }
90 
91  void Stanza::setLang( StringMap** map,
92  std::string& defaultLang,
93  const std::string& data,
94  const std::string& xmllang )
95  {
96  if( data.empty() )
97  return;
98 
99  if( xmllang.empty() )
100  defaultLang = data;
101  else
102  {
103  if( !*map )
104  *map = new StringMap();
105  (**map)[xmllang] = data;
106  }
107  }
108 
109  const std::string& Stanza::findLang( const StringMap* map,
110  const std::string& defaultData,
111  const std::string& lang )
112  {
113  if( map && lang != "default" )
114  {
115  StringMap::const_iterator it = map->find( lang );
116  if( it != map->end() )
117  return (*it).second;
118  }
119  return defaultData;
120  }
121 
122  void Stanza::getLangs( const StringMap* map,
123  const std::string& defaultData,
124  const std::string& name,
125  Tag* tag )
126  {
127  if( !defaultData.empty() )
128  new Tag( tag, name, defaultData );
129 
130  if( !map )
131  return;
132 
133  StringMap::const_iterator it = map->begin();
134  for( ; it != map->end(); ++it )
135  {
136  Tag* t = new Tag( tag, name, "xml:lang", (*it).first );
137  t->setCData( (*it).second );
138  }
139  }
140 
141 }