gloox  1.0.16
stanza.cpp
1 /*
2  Copyright (c) 2005-2015 by Jakob Schröter <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 }
void removeExtensions()
Definition: stanza.cpp:64
void clearList(std::list< T * > &L)
Definition: util.h:152
This is the base class for XMPP stanza abstractions.
Definition: stanza.h:33
void addExtension(const StanzaExtension *se)
Definition: stanza.cpp:52
A stanza error abstraction implemented as a StanzaExtension.
Definition: error.h:34
virtual ~Stanza()
Definition: stanza.cpp:42
Stanza * embeddedStanza() const
Definition: stanza.cpp:69
bool setJID(const std::string &jid)
Definition: jid.cpp:21
The namespace for the gloox library.
Definition: adhoc.cpp:27
This class abstracts a stanza extension, which is usually an element in a specific namespace...
Stanza(Tag *tag)
Definition: stanza.cpp:31
const std::string & findAttribute(const std::string &name) const
Definition: tag.cpp:588
std::map< std::string, std::string > StringMap
Definition: gloox.h:1254
const std::string cdata() const
Definition: tag.cpp:496
An abstraction of a JID.
Definition: jid.h:30
const Error * error() const
Definition: stanza.cpp:47
const std::string EmptyString
Definition: gloox.cpp:123
Tag * embeddedTag() const
Definition: stanza.cpp:76
const StanzaExtension * findExtension(int type) const
Definition: stanza.cpp:57
This is an abstraction of an XML element.
Definition: tag.h:46