forked from loaded02/ICF_Report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomparser.cpp
141 lines (122 loc) · 4.37 KB
/
domparser.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
#include "domparser.h"
DomParser::DomParser(QList<Person*>* daten)
:daten(daten){
}
DomParser::DomParser(QList<Report *> *daten)
:reports(daten){
}
DomParser::DomParser(std::map<QString, QString> *daten)
:icfCodes(daten){
}
bool DomParser::readFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
std::cerr << "Error: Cannot read file " << qPrintable(fileName)
<< ": " << qPrintable(file.errorString())
<< std::endl;
return false;
}
QString errorStr;
int errorLine;
int errorColumn;
QDomDocument doc;
if (!doc.setContent(&file, false, &errorStr, &errorLine,
&errorColumn)) {
std::cerr << "Error: Parse error at line " << errorLine << ", "
<< "column " << errorColumn << ": "
<< qPrintable(errorStr) << std::endl;
return false;
}
QDomElement root = doc.documentElement();
if (root.tagName() == "person" || root.tagName() == "reports" || root.tagName() == "icfcodes") {
parseRootElement(root);
}
else {
std::cerr << "Error: Not a known file" << std::endl;
return false;
}
return true;
}
void DomParser::parseRootElement(const QDomElement &element)
{
QDomNode child = element.firstChild();
while (!child.isNull()) {
if (child.toElement().tagName() == "therapist") {
parseTherapist(child.toElement());
}
else if (child.toElement().tagName() == "patient") {
parsePatient(child.toElement());
}
else if (child.toElement().tagName() == "report") {
parseReport(child.toElement());
}
else if (child.toElement().tagName() == "icfcode") {
parseIcfCode(child.toElement());
}
child = child.nextSibling();
}
}
void DomParser::parseIcfCode(const QDomElement &element)
{
QString id = element.attribute("id");
QString description;
QDomNode child = element.firstChild();
if (child.toElement().tagName() == "description") {
description = child.toElement().text();
}
icfCodes->insert(std::pair<QString,QString>(id,description));
}
void DomParser::parseTherapist(const QDomElement &element)
{
Therapist* item = new Therapist(element.attribute("surname"));
item->setId(element.attribute("id").toInt());
item->setName(element.attribute("name"));
item->setCompany(element.attribute("company"));
daten->append(item);
}
void DomParser::parsePatient(const QDomElement &element)
{
Patient* item = new Patient(element.attribute("surname"));
item->setId(element.attribute("id").toInt());
item->setName(element.attribute("name"));
item->setDiagnosis(element.attribute("diagnosis"));
item->setDob(QDate::fromString(element.attribute("dob")));
daten->append(item);
}
void DomParser::parseReport(const QDomElement element)
{
Report* item = new Report(QDate::fromString(element.attribute("date")));
item->setId(element.attribute("id").toInt());
item->setPatientId(element.attribute("patient").toInt());
item->setTherapistId(element.attribute("therapist").toInt());
item->setType(element.attribute("type"));
QDomNode child = element.firstChild();
while (!child.isNull()) {
Function* func = new Function();
if (child.toElement().tagName() == "function") {
func->setArt(Function::function);
} else if (child.toElement().tagName() == "structure") {
func->setArt(Function::structure);
} else if (child.toElement().tagName() == "partizipation") {
func->setArt(Function::partizipation);
} else if (child.toElement().tagName() == "context") {
func->setArt(Function::context);
} else if (child.toElement().tagName() == "freetext") {
item->setFreeText(child.toElement().text());
child = child.nextSibling();
continue;
}
parseFunction(child.toElement(),func);
item->addFunction(func);
child = child.nextSibling();
}
reports->append(item);
}
void DomParser::parseFunction(const QDomElement &element, Function *parent)
{
parent->setId(element.attribute("id"));
parent->setDescription(element.attribute("description"));
parent->setValue(element.attribute("value").toInt());
parent->setText(element.text());
}