-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsources.cpp
314 lines (203 loc) · 6.24 KB
/
sources.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "rapidxml/rapidxml-1.13/rapidxml.hpp"
#include<iostream>
#ifndef _ERROR_
#include "Error.cpp"
#endif
#include <list>
#include<cstring>
#include<string>
#include <unordered_map>
using namespace rapidxml;
using namespace std;
const char* TITLE_ELEMENT="title";
const char* ATTRIBUTE_LAYOUT_HEIGHT="layout_height";
const char* ATTRIBUTE_LAYOUT_WIDTH="layout_width";
const char* ATTRIBUTE_ORIENTATION="orientation";
const char* LINEAR_LAYOUT="LinearLayout";
const char* ATTRIBUTE_ID="id";
class Style{
private:
unordered_map<string , string> attributes;
public:
void setAttribute(string name , string value) {
attributes[name]=value;
}
string getAttrValue(string name){
return attributes[name];
}
string getHardStyle(){
char quote='"';
string st="style="+quote;
for(auto it:attributes){
st=st+it.first+":"+it.second+"; ";
}
st=st+quote;
return st;
}
int getAttrsize(){
return attributes.size();
}
};
int fillAttributesToStyle(Style* style , xml_node<>* node){
xml_attribute<>* attr=node->first_attribute();
while(attr!=0){
style->setAttribute(attr->name() , attr->value());
attr=attr->next_attribute();
}
}
class View{
private:
Style style;
public:
xml_node<>* node=NULL;
View(xml_node<>* node){
if(isView(node)){
this->node=node;
}
else
std::cout<<"View()::"<<INVALID_VIEW<<std::endl;
fillAttributesToStyle(&style , node);
}
const char* getValue(const char* attribute){
if(node==NULL){
std::cout<<"View::getValue(const char*)::ERROR -> xml_node<>* is NULL , check is passed xml_node<>* is valid or not"<<std::endl;
return 0;
}
return node->first_attribute(attribute)->value();
}
virtual const char* getHTML(){
//here it will return html of the view
string node_name=this->node->name();
string html_start="<"+node_name+" "+style.getHardStyle()+" "+">"+style.getAttrsize("text")+"</"+node_name+">"
return html_start;
}
static bool isView(xml_node<>* node){
//checking is Valid
// it has layout_width and layout_height attributes
if(node->first_attribute(ATTRIBUTE_LAYOUT_WIDTH)==0)
return false;
if(node->first_attribute(ATTRIBUTE_LAYOUT_WIDTH)==0)
return false;
return true;
}
bool isValid(){
if(node==NULL)
return false;
else
return true;
}
};
void parseChilds(xml_node<>* node, list<View*> list);
class LinearLayout:public View{
private:
std::list<View*> child_list;
public:
LinearLayout(xml_node<>* node) : View(node){
if(node==0)
std::cout<<"LinearLayout():passed xml is null";
if(isLinearLayout(node)){
this->node=node;
parseChilds(node , child_list);
}
else
std::cout<<"LinearLayout()::ERROR -> passed xml_node<>* is not a LinearLayout"<<std::endl;
}
const char* getValue(const char* attribute){
if(node==NULL){
std::cout<<"LinearLayout::getValue(const char*)::ERROR -> xml_node<>* is NULL , check is passed xml_node<>* is valid or not"<<std::endl;
return 0;
}
return node->first_attribute(attribute)->value();
}
string getHTML(){
//here it will return html of the view
string node_name=this->node->name();
string html_start="<div"+style.getHardStyle()+" "+">"+style.getAttrsize("text")+"</div>"
return html_start;
}
//for adding childs
void addChild(View* v){
if(v->isValid())
child_list.push_back(v);
}
std::list<View*> getChild(){
return this->child_list;
}
static bool isLinearLayout(xml_node<>* node){
//checking is Valid
// it will be valid if has attribute orientation
if(node->first_attribute(ATTRIBUTE_ORIENTATION)==0)
return false;
return true;
}
};
class ChildParser{
public:
static int parse(xml_node<>* root, std::list<View*> list){
//parsing elements
int count=0;
xml_node<>* child=root->first_node();
if(child == 0)
return count;
while (child!=0)
{
if(strcmp(child->name() , LINEAR_LAYOUT)==0){
list.push_back(new LinearLayout(child));
}
else
list.push_back(new View(child));
count++;
child=child->next_sibling();
}
return count;
}
};
void parseChilds(xml_node<>* node, list<View*> list){
int c= ChildParser::parse(node , list) ;
std::cout<<"LinearLayout : child count:"<<c<<std::endl;
}
class SourceParser
{
private:
xml_node<>* node;
View* parent_layout;
const char* title="TITLE";
//returns 1 on success or 0 on error
int parseComponents(){
//considering node* as the root node
//getting title node
xml_node<>* title_node=node->first_node(TITLE_ELEMENT);
if(title_node==0){
std::cout<<"parser error:"<<TITLE_ELEMENT_NOT_FOUND;
return 0;
}else{
this->title=title_node->value();
}
//now getting parent_layout
//if there are more layout calsses than each layout manager need to be checked using else if() statement as any layout manager can be use as a parent layout
xml_node<>* pnode=node->first_node(LINEAR_LAYOUT);
if(pnode!=0)
parent_layout=new LinearLayout(pnode);
else{
cout<<"SourceParser():"<<PARENT_LAYOUT_NOT_FOUND<<endl;
}
return 1;
}
int parse_result;
public:
SourceParser(xml_node<>* root){
node=root;
this->parse_result= this->parseComponents();
};
const char* getTitle(){
return this->title;
}
View* getParentLayout(){
return this->parent_layout;
}
bool isParseSuccess(){
if(this->parse_result==1)
return true;
return false;
}
};