[89] | 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>
|
---|
[146] | 8 |
|
---|
[89] | 9 | #include <Pacpus/kernel/Log.h>
|
---|
[280] | 10 | #include <Pacpus/kernel/PacpusException.h>
|
---|
[89] | 11 |
|
---|
[231] | 12 | #include <cassert>
|
---|
[89] | 13 | #include <QFile>
|
---|
| 14 | #include <QTextStream>
|
---|
| 15 |
|
---|
[146] | 16 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 17 |
|
---|
[89] | 18 | using namespace pacpus;
|
---|
| 19 | using namespace std;
|
---|
| 20 |
|
---|
| 21 | DECLARE_STATIC_LOGGER("pacpus.core.XmlConfigFile");
|
---|
| 22 |
|
---|
[146] | 23 | XmlConfigFile * XmlConfigFile::m_xmlConfigFile = NULL;
|
---|
[89] | 24 |
|
---|
[147] | 25 | static const char * kXmlConfigFilename = "pacpus_config.xml";
|
---|
[89] | 26 |
|
---|
[146] | 27 | static const char * kRootSection = "pacpus";
|
---|
[89] | 28 |
|
---|
[146] | 29 | static const char * kComponentSection = "components";
|
---|
| 30 | static const char * kConnectionSection = "connections";
|
---|
| 31 | static const char * kParameterSection = "parameters";
|
---|
[165] | 32 | static const char * kPluginSection = "plugins";
|
---|
[146] | 33 |
|
---|
| 34 | static const char * kComponentNode = "component";
|
---|
| 35 | static const char * kConnectionNode = "connection";
|
---|
[165] | 36 | static const char * kParameterNode = "parameter";
|
---|
[146] | 37 | static const char * kPluginNode = "plugin";
|
---|
| 38 |
|
---|
| 39 | static const char * kNameAttribute = "name"; // <component name="MyComponent1" type="MyComponentType"/>
|
---|
| 40 | static const char * kLibAttribute = "lib"; // <plugin lib="MyLibrary.dll"/>
|
---|
| 41 | static const char * kExtensionAttribute = "extension"; // <parameter extension=".dll">
|
---|
| 42 | static const char * kPrefixAttribute = "prefix"; // <parameter prefix="lib">
|
---|
| 43 | static const char * kPostfixAttribute = "postfix"; // <parameter postfix="_d">
|
---|
| 44 |
|
---|
| 45 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 46 | // helper method
|
---|
| 47 | QDomNode getNamedItemFromDomDocument(const QDomDocument & document, const char * itemName);
|
---|
| 48 |
|
---|
| 49 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 50 |
|
---|
| 51 | // CTOR/DTOR
|
---|
[89] | 52 | XmlConfigFile::XmlConfigFile()
|
---|
[146] | 53 | : m_numberOfComponents(0)
|
---|
[89] | 54 | {
|
---|
| 55 | LOG_TRACE("constructor");
|
---|
| 56 |
|
---|
| 57 | // create the root of the XML tree
|
---|
[146] | 58 | m_document.appendChild(m_document.createElement(kRootSection));
|
---|
[89] | 59 | // create the sections
|
---|
[146] | 60 | m_document.documentElement().appendChild(m_document.createElement(kParameterSection));
|
---|
[165] | 61 | m_document.documentElement().appendChild(m_document.createElement(kPluginSection));
|
---|
[146] | 62 | m_document.documentElement().appendChild(m_document.createElement(kComponentSection));
|
---|
| 63 | m_document.documentElement().appendChild(m_document.createElement(kConnectionSection));
|
---|
[147] | 64 | m_file.setFileName(kXmlConfigFilename);
|
---|
[146] | 65 | bool isOpenedSuccessfully = m_file.open(QIODevice::ReadWrite); // FIXME: ReadOnly ?
|
---|
| 66 | if (isOpenedSuccessfully) {
|
---|
| 67 | LOG_INFO("XML document " << kXmlConfigFilename << " was created");
|
---|
[89] | 68 | } else {
|
---|
[146] | 69 | LOG_ERROR("cannot open XML document " << kXmlConfigFilename);
|
---|
[280] | 70 | BOOST_THROW_EXCEPTION(PacpusException("cannot open XML document file"));
|
---|
[89] | 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | XmlConfigFile::~XmlConfigFile()
|
---|
| 75 | {
|
---|
| 76 | LOG_TRACE("destructor");
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[146] | 79 | // CREATE/DESTROY
|
---|
[89] | 80 | XmlConfigFile * XmlConfigFile::create()
|
---|
| 81 | {
|
---|
[146] | 82 | if (NULL ==m_xmlConfigFile) {
|
---|
| 83 | m_xmlConfigFile = new XmlConfigFile();
|
---|
[89] | 84 | }
|
---|
[146] | 85 | return m_xmlConfigFile;
|
---|
[89] | 86 | }
|
---|
| 87 |
|
---|
| 88 | void XmlConfigFile::destroy()
|
---|
| 89 | {
|
---|
[146] | 90 | delete m_xmlConfigFile;
|
---|
| 91 | m_xmlConfigFile = NULL;
|
---|
[89] | 92 | }
|
---|
| 93 |
|
---|
[146] | 94 | // COMPONENTS ADD/REMOVE/CREATE
|
---|
[89] | 95 | void XmlConfigFile::addComponent(QDomElement component)
|
---|
| 96 | {
|
---|
[146] | 97 | QMutexLocker mutexLocker(&m_mutex); // locks mutex, unlocks it in destructor
|
---|
| 98 | (void) mutexLocker; // unused
|
---|
| 99 |
|
---|
[165] | 100 | // TODO: change .tagName => .attribute(kPropertyComponentName)
|
---|
[146] | 101 | QDomNode componentSectionNode = getNamedItemFromDomDocument(m_document, kComponentSection);
|
---|
| 102 | if (componentSectionNode.namedItem(component.attribute(kNameAttribute)/*.tagName()*/).isNull()) {
|
---|
| 103 | LOG_WARN("component " << component.attribute(kNameAttribute)/*tagName()*/ << " exists already in the document");
|
---|
[89] | 104 | } else {
|
---|
[146] | 105 | QDomNode node = getNamedItemFromDomDocument(m_document, kComponentSection).appendChild(component);
|
---|
| 106 | ++m_numberOfComponents;
|
---|
[89] | 107 | LOG_INFO("component " << node.nodeName() << " has been added to the section "
|
---|
[146] | 108 | << getNamedItemFromDomDocument(m_document, kComponentSection).nodeName());
|
---|
[89] | 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | void XmlConfigFile::delComponent(QDomElement component)
|
---|
| 113 | {
|
---|
[146] | 114 | removeComponent(component);
|
---|
| 115 | }
|
---|
[89] | 116 |
|
---|
[146] | 117 | void XmlConfigFile::removeComponent(QDomElement component)
|
---|
| 118 | {
|
---|
| 119 | QMutexLocker mutexLocker(&m_mutex); // locks mutex, unlocks it in destructor
|
---|
| 120 | (void) mutexLocker; // unused
|
---|
| 121 |
|
---|
| 122 | QDomNode node = getNamedItemFromDomDocument(m_document, kComponentSection).removeChild(component);
|
---|
[89] | 123 | if (node.isNull()) {
|
---|
[146] | 124 | LOG_WARN("component " << component.attribute(kNameAttribute)/*tagName()*/ << " doesn't exist in the document.");
|
---|
[89] | 125 | } else {
|
---|
| 126 | LOG_INFO("component " << node.nodeName() << " has been removed from the section "
|
---|
[146] | 127 | << getNamedItemFromDomDocument(m_document, kComponentSection).nodeName());
|
---|
| 128 | --m_numberOfComponents;
|
---|
[89] | 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | QDomElement XmlConfigFile::createComponent(QString name)
|
---|
| 133 | {
|
---|
| 134 | LOG_DEBUG("creating component " << name);
|
---|
| 135 |
|
---|
[146] | 136 | QMutexLocker mutexLocker(&m_mutex); // locks mutex, unlocks it in destructor
|
---|
| 137 | (void) mutexLocker; // unused
|
---|
| 138 | return m_document.createElement(name);
|
---|
[89] | 139 | }
|
---|
| 140 |
|
---|
[146] | 141 | // FILE I/O
|
---|
[89] | 142 | void XmlConfigFile::saveFile(QString fileName)
|
---|
| 143 | {
|
---|
[146] | 144 | QMutexLocker mutexLocker(&m_mutex);
|
---|
| 145 | (void) mutexLocker; // unused
|
---|
[89] | 146 |
|
---|
[146] | 147 | m_file.close();
|
---|
| 148 | assert(!m_file.isOpen());
|
---|
| 149 | m_file.setFileName(fileName);
|
---|
[89] | 150 | {
|
---|
[146] | 151 | // open file
|
---|
| 152 | bool isOpenedSuccessfully = m_file.open(QIODevice::WriteOnly);
|
---|
| 153 | if (!isOpenedSuccessfully) {
|
---|
| 154 | LOG_ERROR("cannot open file '" << m_file.fileName() << "' for writing");
|
---|
| 155 | return;
|
---|
[89] | 156 | }
|
---|
[146] | 157 | QTextStream ts(&m_file);
|
---|
| 158 | ts << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
|
---|
| 159 | ts << m_document.toString();
|
---|
| 160 | m_file.close();
|
---|
[89] | 161 | }
|
---|
[146] | 162 | LOG_DEBUG("file \"" << m_file.fileName() << "\" has been saved");
|
---|
[89] | 163 | }
|
---|
| 164 |
|
---|
| 165 | int XmlConfigFile::loadFile(QString fileName)
|
---|
| 166 | {
|
---|
[146] | 167 | // lock access
|
---|
| 168 | QMutexLocker mutexLocker(&m_mutex);
|
---|
| 169 | (void) mutexLocker; // unused
|
---|
[89] | 170 |
|
---|
[146] | 171 | // check if there are components already
|
---|
| 172 | if (0 != m_numberOfComponents) {
|
---|
| 173 | LOG_WARN("XML document contained " << m_numberOfComponents << " components that will be lost!");
|
---|
[89] | 174 | }
|
---|
| 175 |
|
---|
[146] | 176 | m_file.close();
|
---|
| 177 | assert(!m_file.isOpen());
|
---|
| 178 | m_file.setFileName(fileName);
|
---|
| 179 | {
|
---|
| 180 | // open file
|
---|
| 181 | bool isOpenedSuccessfully = m_file.open(QIODevice::ReadOnly);
|
---|
| 182 | if (!isOpenedSuccessfully) {
|
---|
| 183 | LOG_ERROR("cannot open file '" << m_file.fileName() << "' for reading");
|
---|
| 184 | return 0;
|
---|
| 185 | }
|
---|
| 186 | // read and parse input XML file
|
---|
| 187 | QString errorMsg;
|
---|
| 188 | int errorLine = 0;
|
---|
| 189 | int errorColumn = 0;
|
---|
| 190 | if (!m_document.setContent(&m_file, /*namespaceProcessing=*/true, &errorMsg, &errorLine, &errorColumn)) {
|
---|
| 191 | LOG_ERROR("cannot parse XML file " << m_file.fileName());
|
---|
| 192 | LOG_ERROR(errorMsg << " at " << errorLine << ":" << errorColumn << " (line:col)");
|
---|
| 193 | m_file.close();
|
---|
| 194 | return 0;
|
---|
| 195 | }
|
---|
| 196 | // close file
|
---|
| 197 | m_file.close();
|
---|
[89] | 198 | }
|
---|
| 199 |
|
---|
| 200 | // get the number of components in the loaded tree
|
---|
[146] | 201 | m_numberOfComponents = getAllComponents().count();
|
---|
[89] | 202 |
|
---|
[146] | 203 | LOG_INFO("XML file \"" << m_file.fileName() << "\" has been loaded. Number of components = " << m_numberOfComponents);
|
---|
[89] | 204 | LOG_DEBUG("XML file content:\n"
|
---|
[146] | 205 | << "BEGIN============================================================================\n"
|
---|
| 206 | << m_document.toString()
|
---|
| 207 | << "END==============================================================================\n"
|
---|
| 208 | );
|
---|
[89] | 209 |
|
---|
[146] | 210 | return m_numberOfComponents;
|
---|
[89] | 211 | }
|
---|
| 212 |
|
---|
[165] | 213 | // XML
|
---|
| 214 | QDomElement XmlConfigFile::getSection(const char * name) const
|
---|
[89] | 215 | {
|
---|
[165] | 216 | QDomElement sectionElement = getNamedItemFromDomDocument(m_document, name).toElement();
|
---|
| 217 | if (sectionElement.isNull()) {
|
---|
| 218 | LOG_WARN("there is no '" << name << "' section in the XML");
|
---|
| 219 | return QDomElement();
|
---|
[146] | 220 | }
|
---|
[165] | 221 | return sectionElement;
|
---|
| 222 | }
|
---|
[89] | 223 |
|
---|
[165] | 224 | QDomNodeList XmlConfigFile::getNodesInSection(const char * sectionName, const char * nodeName) const
|
---|
| 225 | {
|
---|
| 226 | QDomElement sectionElement = getSection(sectionName);
|
---|
| 227 | return sectionElement.elementsByTagName(nodeName);
|
---|
[146] | 228 | }
|
---|
[89] | 229 |
|
---|
[165] | 230 | // COMPONENTS
|
---|
| 231 | QDomNodeList XmlConfigFile::getAllComponents() const
|
---|
| 232 | {
|
---|
| 233 | return getNodesInSection(kComponentSection, kComponentNode);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[146] | 236 | QStringList XmlConfigFile::getAllComponentsNames() const
|
---|
| 237 | {
|
---|
| 238 | // get component nodes
|
---|
| 239 | QDomNodeList componentNodes = getAllComponents();
|
---|
| 240 | // get component names
|
---|
| 241 | QStringList componentNameList;
|
---|
| 242 | for (int i = 0; i < componentNodes.size(); ++i) {
|
---|
| 243 | QDomElement componentElement = componentNodes.at(i).toElement();
|
---|
| 244 | componentNameList.append(componentElement.attribute(kNameAttribute));
|
---|
[89] | 245 | }
|
---|
[146] | 246 | return componentNameList;
|
---|
| 247 | }
|
---|
[89] | 248 |
|
---|
[146] | 249 | QDomElement XmlConfigFile::getComponent(QString componentName) const
|
---|
| 250 | {
|
---|
| 251 | LOG_DEBUG("getting component " << componentName);
|
---|
[89] | 252 |
|
---|
[146] | 253 | QDomNodeList componentNodes = getAllComponents();
|
---|
| 254 | for (int i = 0; i < componentNodes.size(); ++i) {
|
---|
| 255 | QDomElement componentElement = componentNodes.at(i).toElement();
|
---|
| 256 | if (componentName == componentElement.attribute(kNameAttribute)) {
|
---|
| 257 | return componentElement;
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | LOG_WARN("cannot get component " << componentName << ": document does not contain a component with this name");
|
---|
| 261 |
|
---|
[89] | 262 | return QDomElement();
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[146] | 265 | // CONNECTIONS
|
---|
| 266 | QDomNodeList XmlConfigFile::getAllConnections() const
|
---|
[89] | 267 | {
|
---|
[165] | 268 | return getNodesInSection(kConnectionSection, kConnectionNode);
|
---|
[89] | 269 | }
|
---|
| 270 |
|
---|
[146] | 271 | QDomElement XmlConfigFile::getConnection(QString name) const
|
---|
[89] | 272 | {
|
---|
[146] | 273 | LOG_DEBUG("getting connection " << name);
|
---|
| 274 |
|
---|
| 275 | QDomNodeList connectionNodes = getAllConnections();
|
---|
| 276 | for (int i = 0; i < connectionNodes.size(); ++i) {
|
---|
| 277 | QDomElement connectionElement = connectionNodes.at(i).toElement();
|
---|
| 278 | // TODO: name by attribute 'name'
|
---|
| 279 | if (name == connectionElement.attribute(kNameAttribute)) {
|
---|
| 280 | return connectionElement;
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | LOG_WARN("cannot get connection " << name << ": document does not contain a connection with this name");
|
---|
| 284 |
|
---|
| 285 | return QDomElement();
|
---|
[89] | 286 | }
|
---|
| 287 |
|
---|
[165] | 288 | // PARAMETERS
|
---|
| 289 | QDomNodeList XmlConfigFile::getAllParameters() const
|
---|
| 290 | {
|
---|
| 291 | return getNodesInSection(kParameterSection, kParameterNode);
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[146] | 294 | // PLUGINS
|
---|
| 295 | QDomNodeList XmlConfigFile::getAllPlugins()
|
---|
[89] | 296 | {
|
---|
[165] | 297 | // get section
|
---|
| 298 | QDomElement pluginsElement = getSection(kPluginSection);
|
---|
[89] | 299 |
|
---|
[146] | 300 | // get attributes
|
---|
[165] | 301 | m_libraryExtension = pluginsElement.attribute(kExtensionAttribute);
|
---|
[146] | 302 | if (!m_libraryExtension.isEmpty()) {
|
---|
| 303 | // prefix with a dot '.' if there is no one
|
---|
| 304 | if ('.' != m_libraryExtension.at(0)) {
|
---|
| 305 | m_libraryExtension = '.' + m_libraryExtension;
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
[165] | 308 | m_libraryPrefix = pluginsElement.attribute(kPrefixAttribute);
|
---|
| 309 | m_libraryPostfix = pluginsElement.attribute(kPostfixAttribute);
|
---|
[146] | 310 |
|
---|
[165] | 311 | // get nodes
|
---|
| 312 | return getNodesInSection(kPluginSection, kPluginNode);
|
---|
[89] | 313 | }
|
---|
| 314 |
|
---|
[146] | 315 | QStringList XmlConfigFile::getAllPluginsNames()
|
---|
[89] | 316 | {
|
---|
[146] | 317 | QDomNodeList pluginList = getAllPlugins();
|
---|
| 318 | if (0 == pluginList.size()) {
|
---|
| 319 | LOG_ERROR("no plugins were specified");
|
---|
| 320 | return QStringList();
|
---|
| 321 | }
|
---|
[165] | 322 | LOG_INFO("there are " << pluginList.size() << " plugins");
|
---|
[146] | 323 |
|
---|
| 324 | // get plugin library paths
|
---|
| 325 | QStringList pluginLibraryNames;
|
---|
| 326 | for (int i = 0; i < pluginList.size(); ++i) {
|
---|
| 327 | QDomElement pluginElement = pluginList.at(i).toElement();
|
---|
| 328 | QString libraryFileName = libraryPrefix() + pluginElement.attribute(kLibAttribute) + libraryPostfix() + libraryExtension();
|
---|
| 329 | pluginLibraryNames.append(libraryFileName);
|
---|
| 330 | }
|
---|
| 331 | return pluginLibraryNames;
|
---|
[89] | 332 | }
|
---|
[146] | 333 |
|
---|
| 334 | QString XmlConfigFile::libraryExtension() const
|
---|
| 335 | {
|
---|
| 336 | return m_libraryExtension;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | QString XmlConfigFile::libraryPrefix() const
|
---|
| 340 | {
|
---|
| 341 | return m_libraryPrefix;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | QString XmlConfigFile::libraryPostfix() const
|
---|
| 345 | {
|
---|
| 346 | return m_libraryPostfix;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 350 | // HELPER METHODS
|
---|
| 351 | QDomNode getNamedItemFromDomDocument(const QDomDocument & document, const char * itemName)
|
---|
| 352 | {
|
---|
| 353 | return document.documentElement().namedItem(itemName);
|
---|
| 354 | }
|
---|