[31] | 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 | */
|
---|
[3] | 10 |
|
---|
[31] | 11 | #include <Pacpus/kernel/XmlConfigFile.h>
|
---|
| 12 | #include <Pacpus/kernel/Log.h>
|
---|
[3] | 13 |
|
---|
| 14 | #include <QFile>
|
---|
| 15 | #include <QTextStream>
|
---|
| 16 |
|
---|
[31] | 17 | using namespace pacpus;
|
---|
[3] | 18 | using namespace std;
|
---|
| 19 |
|
---|
| 20 | DECLARE_STATIC_LOGGER("pacpus.core.XmlConfigFile");
|
---|
| 21 |
|
---|
| 22 | XmlConfigFile * XmlConfigFile::_xmlConfigFile = NULL;
|
---|
| 23 |
|
---|
| 24 | static const string kPropertyPluginList = "list";
|
---|
| 25 |
|
---|
| 26 | static const string kXmlConfigFilename = "pacpus_config.xml";
|
---|
| 27 |
|
---|
| 28 | #define componentSection "components"
|
---|
| 29 | #define parameterSection "parameters"
|
---|
| 30 | #define rootSection "pacpus"
|
---|
| 31 | #define pluginsNode "plugins"
|
---|
| 32 |
|
---|
| 33 | XmlConfigFile::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 |
|
---|
| 52 | XmlConfigFile::~XmlConfigFile()
|
---|
| 53 | {
|
---|
| 54 | LOG_TRACE("destructor");
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | XmlConfigFile * XmlConfigFile::create()
|
---|
| 58 | {
|
---|
| 59 | if (NULL ==_xmlConfigFile) {
|
---|
| 60 | _xmlConfigFile = new XmlConfigFile();
|
---|
| 61 | }
|
---|
| 62 | return _xmlConfigFile;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void XmlConfigFile::destroy()
|
---|
| 66 | {
|
---|
| 67 | delete _xmlConfigFile;
|
---|
| 68 | _xmlConfigFile = NULL;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | void 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 |
|
---|
| 86 | void 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 |
|
---|
| 102 | QDomElement 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 |
|
---|
| 111 | void 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 |
|
---|
| 128 | int 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 |
|
---|
| 166 | QDomElement 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 | QStringList XmlConfigFile::getAllComponents()
|
---|
| 178 | {
|
---|
| 179 | QStringList componentNameList;
|
---|
| 180 | QDomNode xmlNode;
|
---|
| 181 | xmlNode = _document.documentElement().namedItem(componentSection).firstChild();
|
---|
| 182 |
|
---|
| 183 | for (int i = 0; i < _numberOfComponents; i++) {
|
---|
| 184 | componentNameList.append(xmlNode.toElement().tagName());
|
---|
| 185 | xmlNode = xmlNode.nextSibling();
|
---|
| 186 | }
|
---|
| 187 | return componentNameList;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | QStringList XmlConfigFile::getAllPlugins()
|
---|
| 191 | {
|
---|
| 192 | QDomElement node = _document.documentElement().namedItem(parameterSection).namedItem(pluginsNode).toElement();
|
---|
| 193 |
|
---|
| 194 | if (!node.hasAttribute(kPropertyPluginList.c_str())) {
|
---|
| 195 | LOG_WARN("cannot retrieve plugins list, node 'plugins' may be misformed or attribute '" << kPropertyPluginList.c_str() << "' may be missing. "
|
---|
| 196 | << "Use the character pipe '|' to separate the different plugins ");
|
---|
| 197 | return QStringList();
|
---|
| 198 | } else {
|
---|
| 199 | QString plugins = node.attribute(kPropertyPluginList.c_str());
|
---|
| 200 | if (plugins.isEmpty()) {
|
---|
| 201 | LOG_WARN("no plugins were specified");
|
---|
| 202 | return QStringList();
|
---|
| 203 | } else {
|
---|
| 204 | return plugins.split("|", QString::SkipEmptyParts);
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | }
|
---|