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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/*
* DOMParser.cpp
*****************************************************************************
* Copyright (C) 2012, bitmovin Softwareentwicklung OG, All Rights Reserved
*
* Email: libdash-dev@vicky.bitmovin.net
*
* This source code and its use and distribution, is subject to the terms
* and conditions of the applicable license agreement.
*****************************************************************************/
#include "DOMParser.h"
using namespace dash::xml;
using namespace dash::helpers;
DOMParser::DOMParser (std::string url) :
url (url),
reader (NULL),
root (NULL)
{
this->Init();
}
DOMParser::~DOMParser ()
{
xmlCleanupParser();
delete(this->root);
}
Node* DOMParser::GetRootNode () const
{
return this->root;
}
bool DOMParser::Parse (std::string path)
{
this->reader = xmlReaderForFile(this->url.c_str(), NULL, 0);
if(this->reader == NULL)
return false;
if(xmlTextReaderRead(this->reader))
this->root = this->ProcessNode(path);
if(this->root == NULL)
return false;
xmlFreeTextReader(this->reader);
return true;
}
Node* DOMParser::ProcessNode (std::string path)
{
int type = xmlTextReaderNodeType(this->reader);
if(type != WhiteSpace && type != Text)
{
while (type == Comment || type == WhiteSpace)
{
xmlTextReaderRead(this->reader);
type = xmlTextReaderNodeType(this->reader);
}
Node *node = new Node();
node->SetType(type);
if(!(strcmp("",path.c_str())))
node->SetMPDPath(Path::GetDirectoryPath(url));
else
node->SetMPDPath(Path::GetDirectoryPath(path));
if(xmlTextReaderConstName(this->reader) == NULL)
{
delete node;
return NULL;
}
std::string name = (const char *) xmlTextReaderConstName(this->reader);
int isEmpty = xmlTextReaderIsEmptyElement(this->reader);
node->SetName(name);
this->AddAttributesToNode(node);
if(isEmpty)
return node;
Node *subnode = NULL;
int ret = xmlTextReaderRead(this->reader);
while(ret == 1)
{
if(!strcmp(name.c_str(), (const char *) xmlTextReaderConstName(this->reader)))
{
return node;
}
subnode = this->ProcessNode(path);
if(subnode != NULL)
node->AddSubNode(subnode);
ret = xmlTextReaderRead(this->reader);
}
return node;
} else if (type == Text)
{
const char* text = (const char *) xmlTextReaderReadString(this->reader);
if(text != NULL)
{
Node *node = new Node();
node->SetType(type);
node->SetText(text);
return node;
}
}
return NULL;
}
void DOMParser::AddAttributesToNode (Node *node)
{
if(xmlTextReaderHasAttributes(this->reader))
{
while(xmlTextReaderMoveToNextAttribute(this->reader))
{
std::string key = (const char *) xmlTextReaderConstName(this->reader);
std::string value = (const char *) xmlTextReaderConstValue(this->reader);
node->AddAttribute(key, value);
}
}
}
void DOMParser::Print (Node *node, int offset)
{
std::stringstream ss;
for(int i = 0; i < offset; i++)
ss << " ";
ss << node->GetName();
std::vector<std::string> keys = node->GetAttributeKeys();
ss.clear();
for(unsigned int i = 0; i < keys.size(); i++)
{
ss << " " << keys.at(i) << "=" << node->GetAttributeValue(keys.at(i));
}
offset++;
for(unsigned int i = 0; i < node->GetSubNodes().size(); i++)
{
this->Print(node->GetSubNodes().at(i), offset);
}
}
void DOMParser::Init ()
{
this->root = NULL;
this->reader = NULL;
}
void DOMParser::Print ()
{
this->Print(this->root, 0);
}
|