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