source: pacpusframework/branches/2.0-beta1/src/PacpusLib/XmlConfigFile.cpp@ 113

Last change on this file since 113 was 110, checked in by morasjul, 11 years ago

Beta-2 : Fix CMakeList (link), remove useless code and add improvement

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