[3] | 1 | /*********************************************************************
|
---|
| 2 | // created: 2006/01/30
|
---|
| 3 | // created: 2006/01/30 - 13:49
|
---|
| 4 | // filename: xmlconfigfile.cpp
|
---|
| 5 | //
|
---|
| 6 | // author: Gerald Dherbomez
|
---|
| 7 | //
|
---|
| 8 | // purpose: Implementation de la classe XmlConfigFile
|
---|
| 9 | *********************************************************************/
|
---|
| 10 |
|
---|
| 11 | #include "kernel/XmlConfigFile.h"
|
---|
| 12 |
|
---|
| 13 | #include "kernel/Log.h"
|
---|
| 14 |
|
---|
| 15 | #include <QFile>
|
---|
| 16 | #include <QTextStream>
|
---|
| 17 |
|
---|
| 18 | namespace pacpus {
|
---|
| 19 |
|
---|
| 20 | using namespace std;
|
---|
| 21 |
|
---|
| 22 | DECLARE_STATIC_LOGGER("pacpus.core.XmlConfigFile");
|
---|
| 23 |
|
---|
| 24 | XmlConfigFile * XmlConfigFile::_xmlConfigFile = NULL;
|
---|
| 25 |
|
---|
| 26 | static const string kPropertyPluginList = "list";
|
---|
| 27 |
|
---|
| 28 | static const string kXmlConfigFilename = "pacpus_config.xml";
|
---|
| 29 |
|
---|
| 30 | #define componentSection "components"
|
---|
| 31 | #define parameterSection "parameters"
|
---|
| 32 | #define rootSection "pacpus"
|
---|
| 33 | #define pluginsNode "plugins"
|
---|
| 34 |
|
---|
| 35 | XmlConfigFile::XmlConfigFile()
|
---|
| 36 | : _file(NULL)
|
---|
| 37 | , _numberOfComponents(0)
|
---|
| 38 | {
|
---|
| 39 | LOG_TRACE("constructor");
|
---|
| 40 |
|
---|
| 41 | // create the root of the XML tree
|
---|
| 42 | _document.appendChild(_document.createElement(rootSection));
|
---|
| 43 | // create the sections
|
---|
| 44 | _document.documentElement().appendChild(_document.createElement(parameterSection));
|
---|
| 45 | _document.documentElement().appendChild(_document.createElement(componentSection));
|
---|
| 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 |
|
---|
| 54 | XmlConfigFile::~XmlConfigFile()
|
---|
| 55 | {
|
---|
| 56 | LOG_TRACE("destructor");
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | XmlConfigFile * XmlConfigFile::create()
|
---|
| 60 | {
|
---|
| 61 | if (NULL ==_xmlConfigFile) {
|
---|
| 62 | _xmlConfigFile = new XmlConfigFile();
|
---|
| 63 | }
|
---|
| 64 | return _xmlConfigFile;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | void XmlConfigFile::destroy()
|
---|
| 68 | {
|
---|
| 69 | delete _xmlConfigFile;
|
---|
| 70 | _xmlConfigFile = NULL;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | void XmlConfigFile::addComponent(QDomElement component)
|
---|
| 74 | {
|
---|
| 75 | _mutex.lock();
|
---|
| 76 |
|
---|
| 77 | if (_document.documentElement().namedItem(componentSection).namedItem(component.tagName()).isNull()) {
|
---|
| 78 | LOG_WARN("component " << component.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 |
|
---|
| 88 | void 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.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 |
|
---|
| 104 | QDomElement 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 |
|
---|
| 113 | void 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 |
|
---|
| 130 | int 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 |
|
---|
| 168 | QDomElement XmlConfigFile::getComponent(QString componentName)
|
---|
| 169 | {
|
---|
| 170 | LOG_DEBUG("getting component " << componentName);
|
---|
| 171 |
|
---|
| 172 | QDomNode node = _document.documentElement().namedItem(componentSection).namedItem(componentName);
|
---|
| 173 | if (node.isNull()) {
|
---|
| 174 | LOG_WARN("cannot get component " << componentName << ": document does not contain the component");
|
---|
| 175 | }
|
---|
| 176 | return node.toElement();
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /// @return a list of all names of components declared in the XML tree
|
---|
| 180 | QStringList XmlConfigFile::getAllComponents()
|
---|
| 181 | {
|
---|
| 182 | QStringList componentNameList;
|
---|
| 183 | QDomNode xmlNode;
|
---|
| 184 | xmlNode = _document.documentElement().namedItem(componentSection).firstChild();
|
---|
| 185 |
|
---|
| 186 | for (int i = 0; i < _numberOfComponents; i++) {
|
---|
| 187 | componentNameList.append(xmlNode.toElement().tagName());
|
---|
| 188 | xmlNode = xmlNode.nextSibling();
|
---|
| 189 | }
|
---|
| 190 | return componentNameList;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | QStringList XmlConfigFile::getAllPlugins()
|
---|
| 194 | {
|
---|
| 195 | QDomElement node = _document.documentElement().namedItem(parameterSection).namedItem(pluginsNode).toElement();
|
---|
| 196 |
|
---|
| 197 | if (!node.hasAttribute(kPropertyPluginList.c_str())) {
|
---|
| 198 | LOG_WARN("cannot retrieve plugins list, node 'plugins' may be misformed or attribute '" << kPropertyPluginList.c_str() << "' may be missing. "
|
---|
| 199 | << "Use the character pipe '|' to separate the different plugins ");
|
---|
| 200 | return QStringList();
|
---|
| 201 | } else {
|
---|
| 202 | QString plugins = node.attribute(kPropertyPluginList.c_str());
|
---|
| 203 | if (plugins.isEmpty()) {
|
---|
| 204 | LOG_WARN("no plugins were specified");
|
---|
| 205 | return QStringList();
|
---|
| 206 | } else {
|
---|
| 207 | return plugins.split("|", QString::SkipEmptyParts);
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | } // namespace pacpus
|
---|