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

Last change on this file since 63 was 62, checked in by Marek Kurdej, 12 years ago

Update: license info.

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