source: pacpusframework/branches/0.0.x/src/PacpusLib/XmlConfigFile.cpp

Last change on this file was 91, checked in by DHERBOMEZ Gérald, 11 years ago

Improvement of the build system to avoid some workarounds

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