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