source: pacpusframework/trunk/src/PacpusLib/XmlComponentConfig.cpp@ 118

Last change on this file since 118 was 118, checked in by morasjul, 11 years ago

Minor.

  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
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/// @author Gerald Dherbomez <firstname.surname@utc.fr>
6/// @version $Id: XmlComponentConfig.cpp 118 2013-06-26 16:16:28Z morasjul $
7
8#include <Pacpus/kernel/XmlComponentConfig.h>
9
10#include <Pacpus/kernel/Log.h>
11#include <Pacpus/kernel/XmlConfigFile.h>
12
13using namespace pacpus;
14using namespace std;
15
16DECLARE_STATIC_LOGGER("pacpus.core.XmlComponentConfig");
17
18static const string kPropertyComponentType = "type";
19
20XmlComponentConfig::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.
31XmlComponentConfig::~XmlComponentConfig()
32{
33 LOG_TRACE("~XmlComponentConfig()");
34}
35
36void 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
56int XmlComponentConfig::delProperty(const QString& name)
57{
58 if (!hasProperty(name)) {
59 LOG_WARN("cannot delete component property '" << name << "'"
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
76QString 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
91bool XmlComponentConfig::getBoolProperty(const QString& name, bool defaultValue) const
92{
93 return hasProperty(name) ? getProperty(name) == "true" : defaultValue;
94}
95
96int XmlComponentConfig::getIntProperty(const QString& name, int defaultValue) const
97{
98 return hasProperty(name) ? getProperty(name).toInt() : defaultValue;
99}
100
101double XmlComponentConfig::getDoubleProperty(const QString& name, double defaultValue) const
102{
103 return hasProperty(name) ? getProperty(name).toDouble() : defaultValue;
104}
105
106void 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
115bool XmlComponentConfig::hasProperty(const QString& name) const
116{
117 return component_.hasAttribute(name);
118}
119
120QDomElement XmlComponentConfig::qDomElement() const
121{
122 return component_;
123}
124
125void XmlComponentConfig::localCopy(const QDomElement& elementToCopy)
126{
127 component_ = elementToCopy;
128}
129
130QString const XmlComponentConfig::getComponentName() const
131{
132 return component_.tagName();
133}
134
135QString const XmlComponentConfig::getComponentType() const
136{
137 return getProperty(kPropertyComponentType.c_str());
138}
Note: See TracBrowser for help on using the repository browser.