summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/WhatsAPI++/ProtocolTreeNode.cpp
blob: 5ca7bd7f0e20ebee747e523c2b7e61d729299127 (plain)
1
2
3
4
5
6
7
8
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
88
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
/*
 * ProtocolTreeNode.cpp
 *
 *  Created on: 26/06/2012
 *      Author: Antonio
 */

#include "WAException.h"
#include "ProtocolTreeNode.h"

static std::string nilstr;

ProtocolTreeNode::ProtocolTreeNode(const string& tag, map<string, string> *attributes, vector<unsigned char>* data, vector<ProtocolTreeNode*> *children) {
	this->tag = tag;
	this->data = data;
	this->attributes = attributes;
	this->children = children;
}

ProtocolTreeNode::ProtocolTreeNode(const string& tag, map<string, string> *attributes, ProtocolTreeNode* child) {
	this->tag = tag;
	this->data = NULL;
	this->attributes = attributes;
	this->children = new std::vector<ProtocolTreeNode*>(1);
	(*this->children)[0] = child;
}

ProtocolTreeNode::~ProtocolTreeNode() {
	if (this->attributes != NULL)
		delete this->attributes;
	if (this->children != NULL) {
		for (size_t i = 0; i < this->children->size(); i++)
			if (this->children->at(i) != NULL)
				delete this->children->at(i);
		delete this->children;
	}
	if (this->data != NULL)
		delete data;
}


string ProtocolTreeNode::toString() {
	string out;
	out += "<" + this->tag;
	if (this->attributes != NULL) {
		map<string,string>::iterator ii;
		for (ii = attributes->begin(); ii != attributes->end(); ii++)
			out += "" + ii->first + "=\"" + ii->second + "\"";
	}
	out += ">\n";
	out += getDataAsString();

	if (this->children != NULL) {
		vector<ProtocolTreeNode*>::iterator ii;
		for (ii = children->begin(); ii != children->end(); ii++)
			out += (*ii)->toString();
	}

	out += "</" + this->tag + ">\n";

	return out;
}

ProtocolTreeNode* ProtocolTreeNode::getChild(const string& id) {
	if (this->children == NULL || this->children->size() == 0)
		return NULL;

	for (std::size_t i = 0; i < this->children->size(); i++)
		if (id.compare((*children)[i]->tag) == 0)
			return (*children)[i];

	return NULL;
}

ProtocolTreeNode* ProtocolTreeNode::getChild(size_t id) {
	if (this->children == NULL || this->children->size() == 0)
		return NULL;

	if (children->size() > id)
		return (*children)[id];

	return NULL;
}

const string& ProtocolTreeNode::getAttributeValue(const string& attribute)
{
	if (this->attributes == NULL)
		return nilstr;

	map<string,string>::iterator it = attributes->find(attribute);
	if (it == attributes->end())
		return nilstr;

	return it->second;
}

vector<ProtocolTreeNode*> ProtocolTreeNode::getAllChildren()
{
	if (this->children == NULL)
		return vector<ProtocolTreeNode*>();

	return *this->children;
}

std::string ProtocolTreeNode::getDataAsString()
{
	if (this->data == NULL)
		return nilstr;
	return std::string(this->data->begin(), this->data->end());
}

vector<ProtocolTreeNode*> ProtocolTreeNode::getAllChildren(const string &tag)
{
	vector<ProtocolTreeNode*> ret;

	if (this->children != NULL)
		for (size_t i = 0; i < this->children->size(); i++)
			if (tag.compare((*children)[i]->tag) == 0)
				ret.push_back((*children)[i]);

	return ret;
}

bool ProtocolTreeNode::tagEquals(ProtocolTreeNode *node, const string& tag)
{
	return (node != NULL && node->tag.compare(tag) == 0);
}

void ProtocolTreeNode::require(ProtocolTreeNode *node, const string& tag)
{
   if (!tagEquals(node, tag))
	   throw WAException("failed require. node:" + node->toString() + "tag: " + tag, WAException::CORRUPT_STREAM_EX, 0);
}