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