source: pacpusframework/trunk/src/PacpusLib/XmlConfigFile.cpp@ 31

Last change on this file since 31 was 31, checked in by sgosseli, 11 years ago

Huge commit: use the new includes style in all the files, add the license header in all the headers, and in some cpp.

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