-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cc
81 lines (75 loc) · 2.12 KB
/
Config.cc
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
#include <algorithm>
#include <xercesc/dom/DOM.hpp>
#include "DomUtils.hh"
#include "Config.hh"
const YdbStr Config::YDB_CONFIG("ydb-config");
const YdbStr Config::SCHEMA_PATH("schema-path");
const YdbStr Config::SCHEMA("schema");
using namespace xercesc;
Config::Config(DomUtils& domUtils) :
_domUtils(domUtils)
{
char* file = getenv(_ydbConfigEnvVar);
if (file == nullptr) {
std::cerr << __PRETTY_FUNCTION__
<< ", Environment variable: " << _ydbConfigEnvVar
<< " isNotSet using default:" << _ydbConfigFile
<< std::endl;
} else {
_ydbConfigFile = file;
}
read();
}
void
Config::read()
{
DOMDocument* document = nullptr;
try {
DOMDocument* doc = _domUtils.getParser()->parseURI(
_ydbConfigFile.c_str());
if (doc == nullptr) {
std::cerr << __PRETTY_FUNCTION__ << ", " << _ydbConfigFile
<< " is empty" << std::endl;
return;
}
DOMNode* ydbConfig = _domUtils.findChild(YDB_CONFIG, doc);
if (ydbConfig == nullptr) {
std::cerr << __PRETTY_FUNCTION__ << " can not find: "
<< YDB_CONFIG << " in: " << _ydbConfigFile << std::endl;
return;
}
_schemas.clear();
DOMNodeList* nodes = ydbConfig->getChildNodes();
for (int i = 0, size = nodes->getLength(); i < size; i++) {
DOMNode* n = nodes->item(i);
YdbStr localName(n->getLocalName());
if (localName == SCHEMA_PATH) {
YdbStr path(_domUtils.getText(n));
if (!path.str().empty()) {
_schemaPath = path;
}
} else if (localName == SCHEMA) {
YdbStr name(_domUtils.getText(n));
if (!name.str().empty()) {
_schemas.push_back(name);
}
}
}
std::transform(
_schemas.begin(),
_schemas.end(),
_schemas.begin(),
[&](auto&& n) { return _schemaPath + "/" + n; });
} catch (const XMLException& e) {
YdbStr message(e.getMessage());
std::cerr << "READ: got exception " << message << std::endl;
} catch (const DOMException& e) {
YdbStr message(e.getMessage());
std::cerr << "READ: got exception " << message << std::endl;
} catch (...) {
std::cerr << "READ: Unexpected Exception" << std::endl;
}
if (document != nullptr) {
document->release();
}
}