1 | // %pacpus:license{
|
---|
2 | // This file is part of the PACPUS framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %pacpus:license}
|
---|
5 | /// @version $Id: XmlConfigFile.cpp 76 2013-01-10 17:05:10Z kurdejma $
|
---|
6 |
|
---|
7 | #include <Pacpus/kernel/XmlConfigFile.h>
|
---|
8 | #include <Pacpus/kernel/Log.h>
|
---|
9 |
|
---|
10 | #include <QFile>
|
---|
11 | #include <QTextStream>
|
---|
12 |
|
---|
13 | using namespace pacpus;
|
---|
14 | using namespace std;
|
---|
15 |
|
---|
16 | DECLARE_STATIC_LOGGER("pacpus.core.XmlConfigFile");
|
---|
17 |
|
---|
18 | XmlConfigFile * XmlConfigFile::_xmlConfigFile = NULL;
|
---|
19 |
|
---|
20 | static const string kPropertyPluginList = "list";
|
---|
21 |
|
---|
22 | static const string kXmlConfigFilename = "pacpus_config.xml";
|
---|
23 |
|
---|
24 | #define componentSection "components"
|
---|
25 | #define parameterSection "parameters"
|
---|
26 | #define rootSection "pacpus"
|
---|
27 | #define pluginsNode "plugins"
|
---|
28 |
|
---|
29 | XmlConfigFile::XmlConfigFile()
|
---|
30 | : _file(NULL)
|
---|
31 | , _numberOfComponents(0)
|
---|
32 | {
|
---|
33 | LOG_TRACE("constructor");
|
---|
34 |
|
---|
35 | // create the root of the XML tree
|
---|
36 | _document.appendChild(_document.createElement(rootSection));
|
---|
37 | // create the sections
|
---|
38 | _document.documentElement().appendChild(_document.createElement(parameterSection));
|
---|
39 | _document.documentElement().appendChild(_document.createElement(componentSection));
|
---|
40 | _file = new QFile(kXmlConfigFilename.c_str());
|
---|
41 | if (NULL != _file) {
|
---|
42 | LOG_INFO("XML document " << kXmlConfigFilename.c_str() << " was created");
|
---|
43 | } else {
|
---|
44 | LOG_WARN("cannot create XML document " << kXmlConfigFilename.c_str());
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | XmlConfigFile::~XmlConfigFile()
|
---|
49 | {
|
---|
50 | LOG_TRACE("destructor");
|
---|
51 | }
|
---|
52 |
|
---|
53 | XmlConfigFile * XmlConfigFile::create()
|
---|
54 | {
|
---|
55 | if (NULL ==_xmlConfigFile) {
|
---|
56 | _xmlConfigFile = new XmlConfigFile();
|
---|
57 | }
|
---|
58 | return _xmlConfigFile;
|
---|
59 | }
|
---|
60 |
|
---|
61 | void XmlConfigFile::destroy()
|
---|
62 | {
|
---|
63 | delete _xmlConfigFile;
|
---|
64 | _xmlConfigFile = NULL;
|
---|
65 | }
|
---|
66 |
|
---|
67 | void XmlConfigFile::addComponent(QDomElement component)
|
---|
68 | {
|
---|
69 | _mutex.lock();
|
---|
70 |
|
---|
71 | if (_document.documentElement().namedItem(componentSection).namedItem(component.tagName()).isNull()) {
|
---|
72 | LOG_WARN("component " << component.tagName() << " exists already in the document");
|
---|
73 | } else {
|
---|
74 | QDomNode node = _document.documentElement().namedItem(componentSection).appendChild(component);
|
---|
75 | ++_numberOfComponents;
|
---|
76 | LOG_INFO("component " << node.nodeName() << " has been added to the section "
|
---|
77 | << _document.documentElement().namedItem(componentSection).nodeName());
|
---|
78 | }
|
---|
79 | _mutex.unlock();
|
---|
80 | }
|
---|
81 |
|
---|
82 | void XmlConfigFile::delComponent(QDomElement component)
|
---|
83 | {
|
---|
84 | _mutex.lock();
|
---|
85 |
|
---|
86 | QDomNode node = _document.documentElement().namedItem(componentSection).removeChild(component);
|
---|
87 | if (node.isNull()) {
|
---|
88 | LOG_WARN("component " << component.tagName() << " doesn't exist in the document.");
|
---|
89 | } else {
|
---|
90 | LOG_INFO("component " << node.nodeName() << " has been removed from the section "
|
---|
91 | << _document.documentElement().namedItem(componentSection).nodeName());
|
---|
92 | --_numberOfComponents;
|
---|
93 | }
|
---|
94 |
|
---|
95 | _mutex.unlock();
|
---|
96 | }
|
---|
97 |
|
---|
98 | QDomElement XmlConfigFile::createComponent(QString name)
|
---|
99 | {
|
---|
100 | LOG_DEBUG("creating component " << name);
|
---|
101 |
|
---|
102 | QMutexLocker mutexLocker(&_mutex);
|
---|
103 | Q_UNUSED(mutexLocker);
|
---|
104 | return _document.createElement(name);
|
---|
105 | }
|
---|
106 |
|
---|
107 | void XmlConfigFile::saveFile(QString fileName)
|
---|
108 | {
|
---|
109 | QMutexLocker mutexLocker(&_mutex);
|
---|
110 | Q_UNUSED(mutexLocker);
|
---|
111 |
|
---|
112 | _file->setFileName(fileName);
|
---|
113 | {
|
---|
114 | _file->open(QIODevice::WriteOnly);
|
---|
115 | {
|
---|
116 | QTextStream ts(_file);
|
---|
117 | ts << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" + _document.toString();
|
---|
118 | }
|
---|
119 | _file->close();
|
---|
120 | }
|
---|
121 | LOG_DEBUG("file \"" << _file->fileName() << "\" has been saved");
|
---|
122 | }
|
---|
123 |
|
---|
124 | int XmlConfigFile::loadFile(QString fileName)
|
---|
125 | {
|
---|
126 | QMutexLocker mutexLocker(&_mutex);
|
---|
127 | Q_UNUSED(mutexLocker);
|
---|
128 |
|
---|
129 | int line = 0, col = 0;
|
---|
130 | QString errorMsg;
|
---|
131 |
|
---|
132 | if (_numberOfComponents != 0) {
|
---|
133 | LOG_WARN("XML document contained " << _numberOfComponents << " components that will be lost!");
|
---|
134 | }
|
---|
135 |
|
---|
136 | _file->setFileName(fileName);
|
---|
137 | if (!_file->open( QIODevice::ReadOnly )) {
|
---|
138 | LOG_ERROR("cannot open file " << fileName);
|
---|
139 | return 0;
|
---|
140 | }
|
---|
141 | if (!_document.setContent( _file, true, &errorMsg, &line, &col )) {
|
---|
142 | LOG_ERROR("cannot get data into the file " << fileName);
|
---|
143 | LOG_ERROR(errorMsg << " at position " << line << ":" << col << " (line:col)");
|
---|
144 | _file->close();
|
---|
145 | return 0;
|
---|
146 | }
|
---|
147 | _file->close();
|
---|
148 |
|
---|
149 | // get the number of components in the loaded tree
|
---|
150 | _numberOfComponents = _document.documentElement().namedItem(componentSection).childNodes().count();
|
---|
151 |
|
---|
152 | LOG_INFO("XML file \"" << fileName << "\" has been loaded. Number of components = " << _numberOfComponents);
|
---|
153 | LOG_DEBUG("XML file content:\n"
|
---|
154 | << "BEGIN============================================================================\n"
|
---|
155 | << _document.toString()
|
---|
156 | << "END==============================================================================\n"
|
---|
157 | );
|
---|
158 |
|
---|
159 | return _numberOfComponents;
|
---|
160 | }
|
---|
161 |
|
---|
162 | QDomElement XmlConfigFile::getComponent(QString componentName)
|
---|
163 | {
|
---|
164 | LOG_DEBUG("getting component " << componentName);
|
---|
165 |
|
---|
166 | QDomNode node = _document.documentElement().namedItem(componentSection).namedItem(componentName);
|
---|
167 | if (node.isNull()) {
|
---|
168 | LOG_WARN("cannot get component " << componentName << ": document does not contain the component");
|
---|
169 | }
|
---|
170 | return node.toElement();
|
---|
171 | }
|
---|
172 |
|
---|
173 | QStringList XmlConfigFile::getAllComponents()
|
---|
174 | {
|
---|
175 | QStringList componentNameList;
|
---|
176 | QDomNode xmlNode;
|
---|
177 | xmlNode = _document.documentElement().namedItem(componentSection).firstChild();
|
---|
178 |
|
---|
179 | for (int i = 0; i < _numberOfComponents; i++) {
|
---|
180 | componentNameList.append(xmlNode.toElement().tagName());
|
---|
181 | xmlNode = xmlNode.nextSibling();
|
---|
182 | }
|
---|
183 | return componentNameList;
|
---|
184 | }
|
---|
185 |
|
---|
186 | QStringList XmlConfigFile::getAllPlugins()
|
---|
187 | {
|
---|
188 | QDomElement node = _document.documentElement().namedItem(parameterSection).namedItem(pluginsNode).toElement();
|
---|
189 |
|
---|
190 | if (!node.hasAttribute(kPropertyPluginList.c_str())) {
|
---|
191 | LOG_WARN("cannot retrieve plugins list, node 'plugins' may be misformed or attribute '" << kPropertyPluginList.c_str() << "' may be missing. "
|
---|
192 | << "Use the character pipe '|' to separate the different plugins ");
|
---|
193 | return QStringList();
|
---|
194 | } else {
|
---|
195 | QString plugins = node.attribute(kPropertyPluginList.c_str());
|
---|
196 | if (plugins.isEmpty()) {
|
---|
197 | LOG_WARN("no plugins were specified");
|
---|
198 | return QStringList();
|
---|
199 | } else {
|
---|
200 | return plugins.split("|", QString::SkipEmptyParts);
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|