xmpp_stanza.cpp 5.31 KB
Newer Older
1 2 3 4 5 6 7
#include <xmpp/xmpp_stanza.hpp>

#include <utils/encoding.hpp>
#include <utils/split.hpp>

#include <stdexcept>
#include <iostream>
8
#include <sstream>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

#include <string.h>

std::string xml_escape(const std::string& data)
{
  std::string res;
  res.reserve(data.size());
  for (size_t pos = 0; pos != data.size(); ++pos)
    {
      switch(data[pos])
        {
        case '&':
          res += "&amp;";
          break;
        case '<':
          res += "&lt;";
          break;
        case '>':
          res += "&gt;";
          break;
        case '\"':
          res += "&quot;";
          break;
        case '\'':
          res += "&apos;";
          break;
        default:
          res += data[pos];
          break;
        }
    }
  return res;
}

std::string xml_unescape(const std::string& data)
{
  std::string res;
  res.reserve(data.size());
  const char* str = data.c_str();
  while (str && *str && static_cast<size_t>(str - data.c_str()) < data.size())
    {
      if (*str == '&')
        {
          if (strncmp(str+1, "amp;", 4) == 0)
            {
              res += "&";
              str += 4;
            }
          else if (strncmp(str+1, "lt;", 3) == 0)
            {
              res += "<";
              str += 3;
            }
          else if (strncmp(str+1, "gt;", 3) == 0)
            {
              res += ">";
              str += 3;
            }
          else if (strncmp(str+1, "quot;", 5) == 0)
            {
              res += "\"";
              str += 5;
            }
          else if (strncmp(str+1, "apos;", 5) == 0)
            {
              res += "'";
              str += 5;
            }
          else
            res += "&";
        }
      else
        res += *str;
      str++;
    }
  return res;
}

XmlNode::XmlNode(const std::string& name, XmlNode* parent):
louiz’'s avatar
louiz’ committed
88
  parent(parent)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
{
  // split the namespace and the name
  auto n = name.rfind(":");
  if (n == std::string::npos)
    this->name = name;
  else
    {
      this->name = name.substr(n+1);
      this->attributes["xmlns"] = name.substr(0, n);
    }
}

XmlNode::XmlNode(const std::string& name):
  XmlNode(name, nullptr)
{
}

void XmlNode::delete_all_children()
{
  this->children.clear();
}

void XmlNode::set_attribute(const std::string& name, const std::string& value)
{
  this->attributes[name] = value;
}

void XmlNode::set_tail(const std::string& data)
{
  this->tail = data;
}

void XmlNode::add_to_tail(const std::string& data)
{
  this->tail += data;
}

void XmlNode::set_inner(const std::string& data)
{
  this->inner = data;
}

void XmlNode::add_to_inner(const std::string& data)
{
  this->inner += data;
}

std::string XmlNode::get_inner() const
{
  return this->inner;
}

std::string XmlNode::get_tail() const
{
  return this->tail;
}

146
const XmlNode* XmlNode::get_child(const std::string& name, const std::string& xmlns) const
147
{
148
  for (const auto& child: this->children)
149 150
    {
      if (child->name == name && child->get_tag("xmlns") == xmlns)
151
        return child.get();
152 153 154 155
    }
  return nullptr;
}

156
std::vector<const XmlNode*> XmlNode::get_children(const std::string& name, const std::string& xmlns) const
157
{
158 159
  std::vector<const XmlNode*> res;
  for (const auto& child: this->children)
160 161
    {
      if (child->name == name && child->get_tag("xmlns") == xmlns)
162
        res.push_back(child.get());
163 164 165 166
    }
  return res;
}

167
XmlNode* XmlNode::add_child(std::unique_ptr<XmlNode> child)
168 169
{
  child->parent = this;
170 171 172
  auto ret = child.get();
  this->children.push_back(std::move(child));
  return ret;
173 174 175 176
}

XmlNode* XmlNode::add_child(XmlNode&& child)
{
177 178
  auto new_node = std::make_unique<XmlNode>(std::move(child));
  return this->add_child(std::move(new_node));
179 180
}

181 182 183 184 185 186
XmlNode* XmlNode::add_child(const XmlNode& child)
{
  auto new_node = std::make_unique<XmlNode>(child);
  return this->add_child(std::move(new_node));
}

187 188
XmlNode* XmlNode::get_last_child() const
{
189
  return this->children.back().get();
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
}

XmlNode* XmlNode::get_parent() const
{
  return this->parent;
}

void XmlNode::set_name(const std::string& name)
{
  this->name = name;
}

const std::string XmlNode::get_name() const
{
  return this->name;
}

std::string XmlNode::to_string() const
{
209 210
  std::ostringstream res;
  res << "<" << this->name;
211
  for (const auto& it: this->attributes)
212
    res << " " << it.first << "='" << sanitize(it.second) + "'";
louiz’'s avatar
louiz’ committed
213
  if (!this->has_children() && this->inner.empty())
214
    res << "/>";
215 216
  else
    {
217
      res << ">" + sanitize(this->inner);
218
      for (const auto& child: this->children)
219 220
        res << child->to_string();
      res << "</" << this->get_name() << ">";
221
    }
222 223
  res << sanitize(this->tail);
  return res.str();
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
}

bool XmlNode::has_children() const
{
  return !this->children.empty();
}

const std::string XmlNode::get_tag(const std::string& name) const
{
  try
    {
      const auto& value = this->attributes.at(name);
      return value;
    }
  catch (const std::out_of_range& e)
    {
      return "";
    }
}

bool XmlNode::del_tag(const std::string& name)
{
  if (this->attributes.erase(name) != 0)
    return true;
  return false;
}

std::string& XmlNode::operator[](const std::string& name)
{
  return this->attributes[name];
}

std::string sanitize(const std::string& data)
{
  if (utils::is_valid_utf8(data.data()))
    return xml_escape(utils::remove_invalid_xml_chars(data));
  else
    return xml_escape(utils::remove_invalid_xml_chars(utils::convert_to_utf8(data, "ISO-8859-1")));
}