| [79] | 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}
 | 
|---|
| [91] | 5 | /// @author  Gerald Dherbomez <firstname.surname@utc.fr>
 | 
|---|
| [79] | 6 | /// @version    $Id: XmlComponentConfig.cpp 118 2013-06-26 16:16:28Z morasjul $
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #include <Pacpus/kernel/XmlComponentConfig.h>
 | 
|---|
| [118] | 9 | 
 | 
|---|
| [79] | 10 | #include <Pacpus/kernel/Log.h>
 | 
|---|
 | 11 | #include <Pacpus/kernel/XmlConfigFile.h>
 | 
|---|
 | 12 | 
 | 
|---|
 | 13 | using namespace pacpus;
 | 
|---|
 | 14 | using namespace std;
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | DECLARE_STATIC_LOGGER("pacpus.core.XmlComponentConfig");
 | 
|---|
 | 17 | 
 | 
|---|
 | 18 | static const string kPropertyComponentType = "type";
 | 
|---|
 | 19 | 
 | 
|---|
 | 20 | XmlComponentConfig::XmlComponentConfig(const QString& name)
 | 
|---|
 | 21 | {
 | 
|---|
 | 22 |     LOG_TRACE("XmlComponentConfig(QString)");
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 |     // Get the pointer to the document and create the component.
 | 
|---|
 | 25 |     parentDocument_ = XmlConfigFile::create();
 | 
|---|
 | 26 |     if (name != QString::null)
 | 
|---|
 | 27 |       component_ = parentDocument_->createComponent(name);
 | 
|---|
 | 28 | }
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | /// Destructor.
 | 
|---|
 | 31 | XmlComponentConfig::~XmlComponentConfig()
 | 
|---|
 | 32 | {
 | 
|---|
 | 33 |     LOG_TRACE("~XmlComponentConfig()");
 | 
|---|
 | 34 | }
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | void XmlComponentConfig::addProperty(const QString& name)
 | 
|---|
 | 37 | {
 | 
|---|
 | 38 |     if (hasProperty(name))
 | 
|---|
 | 39 |     {
 | 
|---|
 | 40 |         LOG_ERROR("cannot add component property:"
 | 
|---|
 | 41 |                   << " component '" << component_.tagName() << "'"
 | 
|---|
 | 42 |                   << " already contains property '" << name << "'"
 | 
|---|
 | 43 |                   << " and its value is '" << component_.attribute(name) << "'"
 | 
|---|
 | 44 |                   );
 | 
|---|
 | 45 |         return;
 | 
|---|
 | 46 |     }
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 |     // The property does not exist, it can be added.
 | 
|---|
 | 49 |     component_.setAttribute(name, 0);
 | 
|---|
 | 50 |     LOG_INFO("property '" << name << "'"
 | 
|---|
 | 51 |               << " was added to the component " << component_.tagName() << "'"
 | 
|---|
 | 52 |               << " and set to '" << component_.attribute(name) << "'"
 | 
|---|
 | 53 |               );
 | 
|---|
 | 54 | }
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 | int XmlComponentConfig::delProperty(const QString& name)
 | 
|---|
 | 57 | {
 | 
|---|
| [116] | 58 |     if (!hasProperty(name)) {
 | 
|---|
 | 59 |         LOG_WARN("cannot delete component property '" << name << "'"
 | 
|---|
| [79] | 60 |                  << " of component '" << component_.tagName() << "'"
 | 
|---|
 | 61 |                  << ". Component does not contain this property."
 | 
|---|
 | 62 |                  );
 | 
|---|
 | 63 |         return false;
 | 
|---|
 | 64 |     }
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 |     // The property exists, it can be removed.
 | 
|---|
 | 67 |     component_.removeAttribute(name);
 | 
|---|
 | 68 |     LOG_INFO("property '" << name << "' "
 | 
|---|
 | 69 |       << " of component '" << component_.tagName() << "'"
 | 
|---|
 | 70 |       << " was deleted"
 | 
|---|
 | 71 |       );
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 |     return true;
 | 
|---|
 | 74 | }
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 | QString XmlComponentConfig::getProperty(const QString& name, const QString& defaultValue) const
 | 
|---|
 | 77 | {
 | 
|---|
 | 78 |     if (!hasProperty(name))
 | 
|---|
 | 79 |     {
 | 
|---|
 | 80 |         LOG_WARN("cannot retrieve component property '" << name << "'"
 | 
|---|
 | 81 |                  << " of component '" << component_.tagName() << "'"
 | 
|---|
 | 82 |                  << ". Component does not contain this property."
 | 
|---|
 | 83 |                  );
 | 
|---|
 | 84 |         return defaultValue;
 | 
|---|
 | 85 |     }
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 |     // The property exists, the value can be retrieved.
 | 
|---|
 | 88 |     return component_.attribute(name);
 | 
|---|
 | 89 | }
 | 
|---|
 | 90 | 
 | 
|---|
| [78] | 91 | bool XmlComponentConfig::getBoolProperty(const QString& name, bool defaultValue) const
 | 
|---|
| [73] | 92 | {
 | 
|---|
| [3] | 93 |   return hasProperty(name) ? getProperty(name) == "true" : defaultValue;
 | 
|---|
 | 94 | }
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 | int XmlComponentConfig::getIntProperty(const QString& name, int defaultValue) const
 | 
|---|
 | 97 | {
 | 
|---|
 | 98 |   return hasProperty(name) ? getProperty(name).toInt() : defaultValue;
 | 
|---|
 | 99 | }
 | 
|---|
 | 100 | 
 | 
|---|
| [79] | 101 | double XmlComponentConfig::getDoubleProperty(const QString& name, double defaultValue) const
 | 
|---|
 | 102 | {
 | 
|---|
 | 103 |   return hasProperty(name) ? getProperty(name).toDouble() : defaultValue;
 | 
|---|
 | 104 | }
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 | void XmlComponentConfig::setProperty(const QString& name, const QString& value)
 | 
|---|
 | 107 | {
 | 
|---|
 | 108 |     component_.setAttribute(name, value);
 | 
|---|
 | 109 |     LOG_INFO("property " << name
 | 
|---|
 | 110 |              << " of the component " << component_.tagName()
 | 
|---|
 | 111 |              << " was set to : " << value
 | 
|---|
 | 112 |              );
 | 
|---|
 | 113 | }
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 | bool XmlComponentConfig::hasProperty(const QString& name) const
 | 
|---|
 | 116 | {
 | 
|---|
 | 117 |     return component_.hasAttribute(name);
 | 
|---|
 | 118 | }
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 | QDomElement XmlComponentConfig::qDomElement() const
 | 
|---|
 | 121 | {
 | 
|---|
 | 122 |     return component_;
 | 
|---|
 | 123 | }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 | void XmlComponentConfig::localCopy(const QDomElement& elementToCopy)
 | 
|---|
 | 126 | {
 | 
|---|
 | 127 |     component_ = elementToCopy;
 | 
|---|
 | 128 | }
 | 
|---|
 | 129 | 
 | 
|---|
 | 130 | QString const XmlComponentConfig::getComponentName() const
 | 
|---|
 | 131 | {
 | 
|---|
 | 132 |     return component_.tagName();
 | 
|---|
 | 133 | }
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 | QString const XmlComponentConfig::getComponentType() const
 | 
|---|
 | 136 | {
 | 
|---|
 | 137 |     return getProperty(kPropertyComponentType.c_str());
 | 
|---|
 | 138 | }
 | 
|---|