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