/
XMLRuntime.cpp
219 lines (184 loc) · 7.42 KB
/
XMLRuntime.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
/*
Project FreeAX25
Copyright (C) 2015 tania@df9ry.de
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ServerEndPoint.h>
#include <ClientEndPoint.h>
#include <Plugin.h>
#include <Instance.h>
#include <Setting.h>
#include <Environment.h>
#include "XMLRuntime.h"
#include "DOMTreeErrorReporter.h"
#include <xercesc/dom/DOM.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <exception>
#include <string>
using namespace std;
using namespace XERCES_CPP_NAMESPACE;
void run(const string& filename, FreeAX25::Runtime::Configuration& conf) {
XMLIO::XMLRuntime rt{};
rt.read(filename, conf);
}
namespace XMLIO {
static inline const XMLCh* toX(const string& s) {
return XMLString::transcode(s.c_str());
}
static inline const string fmX(const XMLCh* x) {
return string(XMLString::transcode(x));
}
XMLRuntime::XMLRuntime() { XMLPlatformUtils::Initialize(); }
XMLRuntime::~XMLRuntime() { XMLPlatformUtils::Terminate(); }
static void readSettings(
const std::string& id,
XERCES_CPP_NAMESPACE::DOMElement* element,
FreeAX25::Runtime::UniquePointerDict<FreeAX25::Runtime::Setting>& settings);
static void readPlugins(
const std::string& id,
XERCES_CPP_NAMESPACE::DOMElement* element,
FreeAX25::Runtime::UniquePointerDict<FreeAX25::Runtime::Plugin>& plugins);
static void readInstances(
const std::string& id,
XERCES_CPP_NAMESPACE::DOMElement* element,
FreeAX25::Runtime::UniquePointerDict<FreeAX25::Runtime::Instance>& instances);
void XMLRuntime::read(const string& filename, FreeAX25::Runtime::Configuration& config) {
XercesDOMParser parser;
parser.setValidationScheme(XercesDOMParser::Val_Always);
parser.setDoNamespaces(true);
parser.setDoSchema(true);
parser.setDoXInclude(true);
parser.setHandleMultipleImports(true);
parser.setValidationSchemaFullChecking(true);
parser.setCreateEntityReferenceNodes(false);
parser.setIncludeIgnorableWhitespace(false);
DOMTreeErrorReporter errReporter;
parser.setErrorHandler(&errReporter);
parser.parse(filename.c_str());
if (errReporter.getSawErrors())
throw exception();
// Now read configuration from the DOM Tree:
XERCES_CPP_NAMESPACE::DOMDocument* doc = parser.getDocument();
assert(doc != nullptr);
auto rootElement = doc->getDocumentElement();
auto configName = rootElement->getAttribute(toX("name"));
config.setId(fmX(configName));
{ // Get settings:
auto nodeList = rootElement->getElementsByTagName(toX("Settings"));
if (nodeList->getLength() > 0)
readSettings(
"",
static_cast<DOMElement*>(nodeList->item(0)),
config.settings);
}
{ // Get plugins:
auto nodeList = rootElement->getElementsByTagName(toX("Plugins"));
if (nodeList->getLength() > 0)
readPlugins(
"",
static_cast<DOMElement*>(nodeList->item(0)),
config.plugins);
}
}
static void readSettings(const string& id, DOMElement* element,
FreeAX25::Runtime::UniquePointerDict<FreeAX25::Runtime::Setting>& settings)
{
if (element == nullptr) return;
auto nodeList = element->getElementsByTagName(toX("Setting"));
for (uint32_t i = 0; i < nodeList->getLength(); ++i) {
auto settingElement = static_cast<DOMElement*>(nodeList->item(i));
string name = fmX(settingElement->getAttribute(toX("name")));
string value = fmX(settingElement->getTextContent());
FreeAX25::Runtime::env().logDebug(
"Setting " + id + "/" + name + " = \"" + value + "\"");
settings.insertNew(name, new FreeAX25::Runtime::Setting(name, value));
} // end for //
}
static void readPlugins(const string& id, DOMElement* element,
FreeAX25::Runtime::UniquePointerDict<FreeAX25::Runtime::Plugin>& plugins)
{
if (element == nullptr) return;
auto nodeList = element->getElementsByTagName(toX("Plugin"));
for (uint32_t i = 0; i < nodeList->getLength(); ++i) {
auto pluginNode = static_cast<DOMElement*>(nodeList->item(i));
string name = fmX(pluginNode->getAttribute(toX("name")));
auto _file = pluginNode->getAttribute(toX("file"));
string file = (_file != nullptr) ? fmX(_file) : "";
FreeAX25::Runtime::env().logDebug(
"Define plugin " + id + "/" + name + "(" + file + ")");
auto plugin = new FreeAX25::Runtime::Plugin(name, file);
{ // Get settings:
auto nodeList = pluginNode->getElementsByTagName(toX("Settings"));
if (nodeList->getLength() > 0)
readSettings(
id + "/" + name,
static_cast<DOMElement*>(nodeList->item(0)),
plugin->settings);
}
{ // Get instances:
auto nodeList = pluginNode->getElementsByTagName(toX("Instances"));
if (nodeList->getLength() > 0)
readInstances(
id + "/" + name,
static_cast<DOMElement*>(nodeList->item(0)),
plugin->instances);
}
plugins.insertNew(name, plugin);
} // end for //
}
static void readInstances(const string& id, DOMElement* element,
FreeAX25::Runtime::UniquePointerDict<FreeAX25::Runtime::Instance>& instances)
{
if (element == nullptr) return;
auto nodeList = element->getElementsByTagName(toX("Instance"));
for (uint32_t i = 0; i < nodeList->getLength(); ++i) {
auto instanceNode = static_cast<DOMElement*>(nodeList->item(i));
string name = fmX(instanceNode->getAttribute(toX("name")));
FreeAX25::Runtime::env().logDebug(
"Define instance " + id + "/" + name);
auto instance = new FreeAX25::Runtime::Instance(name);
{ // Get client endpoints:
auto nodeList = instanceNode->getElementsByTagName(toX("ClientEndPoint"));
for (uint32_t i = 0; i < nodeList->getLength(); ++i) {
auto instanceNode = static_cast<DOMElement*>(nodeList->item(i));
string name = fmX(instanceNode->getAttribute(toX("name")));
string url = fmX(instanceNode->getAttribute(toX("url")));
FreeAX25::Runtime::env().logDebug(
"Define client endpoint " + id + "/" + name + " as " + url);
auto endpoint = new FreeAX25::Runtime::ClientEndPoint(name, url);
instance->clientEndPoints.insertNew(name, endpoint);
} // end for //
}
{ // Get server endpoints:
auto nodeList = instanceNode->getElementsByTagName(toX("ServerEndPoint"));
for (uint32_t i = 0; i < nodeList->getLength(); ++i) {
auto instanceNode = static_cast<DOMElement*>(nodeList->item(i));
string name = fmX(instanceNode->getAttribute(toX("name")));
string url = fmX(instanceNode->getAttribute(toX("url")));
FreeAX25::Runtime::env().logDebug(
"Define server endpoint " + id + "/" + name + " as " + url);
auto endpoint = new FreeAX25::Runtime::ServerEndPoint(name, url);
instance->serverEndPoints.insertNew(name, endpoint);
} // end for //
}
{ // Get settings:
auto nodeList = instanceNode->getElementsByTagName(toX("Settings"));
if (nodeList->getLength() > 0)
readSettings(
id + "/" + name,
static_cast<DOMElement*>(nodeList->item(0)),
instance->settings);
}
instances.insertNew(name, instance);
} // end for //
}
} /* namespace XMLIO */