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 91 2013-05-19 10:32:48Z gdherbom $
|
---|
7 |
|
---|
8 | #include <Pacpus/kernel/XmlComponentConfig.h>
|
---|
9 | #include <Pacpus/kernel/Log.h>
|
---|
10 | #include <Pacpus/kernel/XmlConfigFile.h>
|
---|
11 |
|
---|
12 | using namespace pacpus;
|
---|
13 | using namespace std;
|
---|
14 |
|
---|
15 | DECLARE_STATIC_LOGGER("pacpus.core.XmlComponentConfig");
|
---|
16 |
|
---|
17 | static const string kPropertyComponentType = "type";
|
---|
18 |
|
---|
19 | XmlComponentConfig::XmlComponentConfig(const QString& name)
|
---|
20 | {
|
---|
21 | LOG_TRACE("XmlComponentConfig(QString)");
|
---|
22 |
|
---|
23 | // Get the pointer to the document and create the component.
|
---|
24 | parentDocument_ = XmlConfigFile::create();
|
---|
25 | if (name != QString::null)
|
---|
26 | component_ = parentDocument_->createComponent(name);
|
---|
27 | }
|
---|
28 |
|
---|
29 | /// Destructor.
|
---|
30 | XmlComponentConfig::~XmlComponentConfig()
|
---|
31 | {
|
---|
32 | LOG_TRACE("~XmlComponentConfig()");
|
---|
33 | }
|
---|
34 |
|
---|
35 | void XmlComponentConfig::addProperty(const QString& name)
|
---|
36 | {
|
---|
37 | if (hasProperty(name))
|
---|
38 | {
|
---|
39 | LOG_ERROR("cannot add component property:"
|
---|
40 | << " component '" << component_.tagName() << "'"
|
---|
41 | << " already contains property '" << name << "'"
|
---|
42 | << " and its value is '" << component_.attribute(name) << "'"
|
---|
43 | );
|
---|
44 | return;
|
---|
45 | }
|
---|
46 |
|
---|
47 | // The property does not exist, it can be added.
|
---|
48 | component_.setAttribute(name, 0);
|
---|
49 | LOG_INFO("property '" << name << "'"
|
---|
50 | << " was added to the component " << component_.tagName() << "'"
|
---|
51 | << " and set to '" << component_.attribute(name) << "'"
|
---|
52 | );
|
---|
53 | }
|
---|
54 |
|
---|
55 | int XmlComponentConfig::delProperty(const QString& name)
|
---|
56 | {
|
---|
57 | if (!hasProperty(name))
|
---|
58 | {
|
---|
59 | LOG_WARN("cannot delete compoenent 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 |
|
---|
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 |
|
---|
91 | bool XmlComponentConfig::getBoolProperty(const QString& name, bool defaultValue) const
|
---|
92 | {
|
---|
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 |
|
---|
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 | }
|
---|