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 | /// @version $Id: XmlComponentConfig.cpp 79 2013-01-14 08:52:06Z kurdejma $
|
---|
6 |
|
---|
7 | #include <Pacpus/kernel/XmlComponentConfig.h>
|
---|
8 | #include <Pacpus/kernel/Log.h>
|
---|
9 | #include <Pacpus/kernel/XmlConfigFile.h>
|
---|
10 | #include <QDomNamedNodeMap>
|
---|
11 |
|
---|
12 | using namespace pacpus;
|
---|
13 | using namespace std;
|
---|
14 |
|
---|
15 | DECLARE_STATIC_LOGGER("pacpus.core.XmlComponentConfig");
|
---|
16 |
|
---|
17 | static const char* kPropertyComponentName = "name";
|
---|
18 | static const char* kPropertyComponentType = "type";
|
---|
19 | static const char* kPropertyConnectionType = "type";
|
---|
20 | static const char* kPropertyConnectionInput = "input";
|
---|
21 | static const char* kPropertyConnectionOutput = "output";
|
---|
22 | static const char* kPropertyConnectionPriority = "priority";
|
---|
23 |
|
---|
24 | XmlComponentConfig::XmlComponentConfig(QString const& 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 | QDomNamedNodeMap XmlComponentConfig::getProperties() const
|
---|
41 | {
|
---|
42 | return component_.attributes();
|
---|
43 | }
|
---|
44 |
|
---|
45 | void XmlComponentConfig::addProperty(QString const& name)
|
---|
46 | {
|
---|
47 | if (hasProperty(name)) {
|
---|
48 | LOG_ERROR("cannot add component property:"
|
---|
49 | << " component '" << component_.attribute(kPropertyComponentName) << "'"
|
---|
50 | << " already contains property '" << name << "'"
|
---|
51 | << " and its value is '" << component_.attribute(name) << "'"
|
---|
52 | );
|
---|
53 | return;
|
---|
54 | }
|
---|
55 |
|
---|
56 | // The property does not exist, it can be added.
|
---|
57 | component_.setAttribute(name, 0);
|
---|
58 | LOG_INFO("property '" << name << "'"
|
---|
59 | << " was added to the component " << component_.attribute(kPropertyComponentName) << "'"
|
---|
60 | << " and set to '" << component_.attribute(name) << "'"
|
---|
61 | );
|
---|
62 | }
|
---|
63 |
|
---|
64 | int XmlComponentConfig::delProperty(QString const& name)
|
---|
65 | {
|
---|
66 | if (!hasProperty(name)) {
|
---|
67 | LOG_WARN("cannot delete compoenent property '" << name << "'"
|
---|
68 | << " of component '" << component_.attribute(kPropertyComponentName) << "'"
|
---|
69 | << ". Component does not contain this property."
|
---|
70 | );
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 |
|
---|
74 | // The property exists, it can be removed.
|
---|
75 | component_.removeAttribute(name);
|
---|
76 | LOG_INFO("property '" << name << "' "
|
---|
77 | << " of component '" << component_.attribute(kPropertyComponentName) << "'"
|
---|
78 | << " was deleted"
|
---|
79 | );
|
---|
80 |
|
---|
81 | return true;
|
---|
82 | }
|
---|
83 |
|
---|
84 | QString XmlComponentConfig::getProperty(QString const& name, QString const& defaultValue) const
|
---|
85 | {
|
---|
86 | if (!hasProperty(name))
|
---|
87 | {
|
---|
88 | LOG_WARN("cannot retrieve component property '" << name << "'"
|
---|
89 | << " of component '" << component_.attribute(kPropertyComponentName) << "'"
|
---|
90 | << ". Component does not contain this property."
|
---|
91 | );
|
---|
92 | return defaultValue;
|
---|
93 | }
|
---|
94 |
|
---|
95 | // The property exists, the value can be retrieved.
|
---|
96 | return component_.attribute(name);
|
---|
97 | }
|
---|
98 |
|
---|
99 | bool XmlComponentConfig::getBoolProperty(QString const& name, bool defaultValue) const
|
---|
100 | {
|
---|
101 | return hasProperty(name) ? getProperty(name) == "true" : defaultValue;
|
---|
102 | }
|
---|
103 |
|
---|
104 | int XmlComponentConfig::getIntProperty(QString const& name, int defaultValue) const
|
---|
105 | {
|
---|
106 | return hasProperty(name) ? getProperty(name).toInt() : defaultValue;
|
---|
107 | }
|
---|
108 |
|
---|
109 | double XmlComponentConfig::getDoubleProperty(QString const& name, double defaultValue) const
|
---|
110 | {
|
---|
111 | return hasProperty(name) ? getProperty(name).toDouble() : defaultValue;
|
---|
112 | }
|
---|
113 |
|
---|
114 | void XmlComponentConfig::setProperty(QString const& name, QString const& value)
|
---|
115 | {
|
---|
116 | component_.setAttribute(name, value);
|
---|
117 | LOG_INFO("property " << name
|
---|
118 | << " of the component " << component_.attribute(kPropertyComponentName)
|
---|
119 | << " was set to : " << value
|
---|
120 | );
|
---|
121 | }
|
---|
122 |
|
---|
123 | bool XmlComponentConfig::hasProperty(QString const& name) const
|
---|
124 | {
|
---|
125 | return component_.hasAttribute(name);
|
---|
126 | }
|
---|
127 |
|
---|
128 | QDomElement const& XmlComponentConfig::getDomElement() const
|
---|
129 | {
|
---|
130 | return component_;
|
---|
131 | }
|
---|
132 |
|
---|
133 | void XmlComponentConfig::localCopy(const QDomElement& elementToCopy)
|
---|
134 | {
|
---|
135 | component_ = elementToCopy;
|
---|
136 | }
|
---|
137 |
|
---|
138 | QString const XmlComponentConfig::getComponentName() const
|
---|
139 | {
|
---|
140 | return getProperty(kPropertyComponentName);
|
---|
141 | }
|
---|
142 |
|
---|
143 | QString const XmlComponentConfig::getComponentType() const
|
---|
144 | {
|
---|
145 | return getProperty(kPropertyComponentType);
|
---|
146 | }
|
---|
147 |
|
---|
148 | QString const XmlComponentConfig::getConnectionType() const
|
---|
149 | {
|
---|
150 | return getProperty(kPropertyConnectionType);
|
---|
151 | }
|
---|
152 |
|
---|
153 | QString const XmlComponentConfig::getConnectionInput() const
|
---|
154 | {
|
---|
155 | return getProperty(kPropertyConnectionInput);
|
---|
156 | }
|
---|
157 |
|
---|
158 | QString const XmlComponentConfig::getConnectionOutput() const
|
---|
159 | {
|
---|
160 | return getProperty(kPropertyConnectionOutput);
|
---|
161 | }
|
---|
162 |
|
---|
163 | int const XmlComponentConfig::getConnectionPriority() const
|
---|
164 | {
|
---|
165 | return getIntProperty(kPropertyConnectionPriority, /*defaultValue=*/0);
|
---|
166 | }
|
---|