-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomparser.cpp
More file actions
181 lines (135 loc) · 4.55 KB
/
domparser.cpp
File metadata and controls
181 lines (135 loc) · 4.55 KB
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
#include "domparser.h"
#include <QRegExp>
DomParser::DomParser() {}
DomParser::~DomParser() {}
DomModel *DomParser::fromDom(QDomDocument d)
{
// get root element
QDomElement root = d.documentElement();
// check cosistency
if (root.tagName() != "plist")
{
qWarning("The file is not a plist!");
return NULL;
}
// get dict element
QDomElement dict = root.firstChildElement();
// create model
DomModel *m = new DomModel();
// create root item in model
DomItem *i = m->getRoot()->addChild();
i->setName("plist");
i->setType("dict");
// parse doc recursively
parseElement(dict, i);
return m;
}
QDomDocument DomParser::toDom(DomModel *m)
{
// create doc with doctype
QDomDocument doc("plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
// create and add processing instruction
QDomProcessingInstruction instr = doc.createProcessingInstruction(
"xml", "version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(instr);
// create root item in doc
QDomElement doc_root = doc.createElement("plist");
QDomAttr version = doc.createAttribute("version");
version.setValue("1.0");
doc_root.setAttributeNode(version);
QDomElement doc_dict = doc.createElement("dict");
doc_root.appendChild(doc_dict);
// add root item to doc
doc.appendChild(doc_root);
// get root item from model
DomItem *model_root = m->getRoot()->child(0);
// parse model recursively
parseItem(model_root, doc_dict, doc);
return doc;
}
void DomParser::parseElement(QDomElement &n, DomItem *item)
{
if (n.hasChildNodes())
{
QDomElement element = n.firstChildElement();
while (!element.isNull())
{
QString tagName = element.tagName();
if (tagName == "key" || tagName == "array" || tagName == "dict")
{
// create child
DomItem *domItem = item->addChild();
// counter for items without keys
int nC = 1;
if (tagName == "key")
{
// get key value
QString eName = element.firstChild().nodeValue();
// get val element
QDomElement val = element.nextSiblingElement();
// get type and val
QString eType = val.tagName();
QString eValue = val.firstChild().nodeValue();
// set data
domItem->setData(eName, eType, eValue);
parseElement(val, domItem);
element = val.nextSiblingElement();
}
else
{
domItem->setType(tagName);
domItem->setName(QString("Item %1").arg(nC++));
parseElement(element, domItem);
element = element.nextSiblingElement();
}
}
else
{
qWarning("No value matching the key");
break;
}
}
}
}
void DomParser::parseItem(DomItem *item, QDomElement &n, QDomDocument &doc)
{
QDomElement element;
QString name = item->getName();
QString type = item->getType();
QString value = item->getValue();
if (name != "plist")
{
QRegExp rp("Item\\s\\d+");
// item without key
if (rp.exactMatch(name))
{
QDomElement container = doc.createElement(type);
n.appendChild(container);
element = container;
}
else
{
QDomElement key = doc.createElement("key");
QDomText keyText = doc.createTextNode(name);
key.appendChild(keyText);
n.appendChild(key);
QDomElement val = doc.createElement(type);
n.appendChild(val);
if (type != "array" && type != "dict")
{
QDomText valText = doc.createTextNode(value);
val.appendChild(valText);
}
element = val;
}
}
else element = n;
// get children count
int c = item->childCount();
// iterate through children
for (int i = 0; i < c; ++i)
{
DomItem *child = item->child(i);
parseItem(child, element, doc);
}
}