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 |
|
---|
11 | using namespace pacpus;
|
---|
12 | using namespace std;
|
---|
13 |
|
---|
14 | DECLARE_STATIC_LOGGER("pacpus.core.XmlComponentConfig");
|
---|
15 |
|
---|
16 | static const string kPropertyComponentName = "name";
|
---|
17 | static const string kPropertyComponentType = "type";
|
---|
18 | static const string kPropertyConnectionType = "type";
|
---|
19 | static const string kPropertyConnectionInput = "input";
|
---|
20 | static const string kPropertyConnectionOutput = "output";
|
---|
21 | static const string kPropertyConnectionPriority = "priority";
|
---|
22 | static const string kPropertyConnectionInputC = "inputC";
|
---|
23 | static const string kPropertyConnectionInputP = "inputP";
|
---|
24 | static const string kPropertyConnectionOutputC = "outputC";
|
---|
25 | static const string kPropertyConnectionOutputP = "outputP";
|
---|
26 |
|
---|
27 | XmlComponentConfig::XmlComponentConfig(const QString& name)
|
---|
28 | {
|
---|
29 | LOG_TRACE("XmlComponentConfig(QString)");
|
---|
30 |
|
---|
31 | // Get the pointer to the document and create the component.
|
---|
32 | parentDocument_ = XmlConfigFile::create();
|
---|
33 | if (name != QString::null)
|
---|
34 | component_ = parentDocument_->createComponent(name);
|
---|
35 | }
|
---|
36 |
|
---|
37 | /// Destructor.
|
---|
38 | XmlComponentConfig::~XmlComponentConfig()
|
---|
39 | {
|
---|
40 | LOG_TRACE("~XmlComponentConfig()");
|
---|
41 | }
|
---|
42 |
|
---|
43 | void XmlComponentConfig::addProperty(const QString& name)
|
---|
44 | {
|
---|
45 | if (hasProperty(name))
|
---|
46 | {
|
---|
47 | LOG_ERROR("cannot add component property:"
|
---|
48 | << " component '" << component_.attribute(kPropertyComponentName.c_str()) << "'"
|
---|
49 | << " already contains property '" << name << "'"
|
---|
50 | << " and its value is '" << component_.attribute(name) << "'"
|
---|
51 | );
|
---|
52 | return;
|
---|
53 | }
|
---|
54 |
|
---|
55 | // The property does not exist, it can be added.
|
---|
56 | component_.setAttribute(name, 0);
|
---|
57 | LOG_INFO("property '" << name << "'"
|
---|
58 | << " was added to the component " << component_.attribute(kPropertyComponentName.c_str()) << "'"
|
---|
59 | << " and set to '" << component_.attribute(name) << "'"
|
---|
60 | );
|
---|
61 | }
|
---|
62 |
|
---|
63 | int XmlComponentConfig::delProperty(const QString& name)
|
---|
64 | {
|
---|
65 | if (!hasProperty(name))
|
---|
66 | {
|
---|
67 | LOG_WARN("cannot delete compoenent property '" << name << "'"
|
---|
68 | << " of component '" << component_.attribute(kPropertyComponentName.c_str()) << "'"
|
---|
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.c_str()) << "'"
|
---|
78 | << " was deleted"
|
---|
79 | );
|
---|
80 |
|
---|
81 | return true;
|
---|
82 | }
|
---|
83 |
|
---|
84 | QString XmlComponentConfig::getProperty(const QString& name, const QString& defaultValue) const
|
---|
85 | {
|
---|
86 | if (!hasProperty(name))
|
---|
87 | {
|
---|
88 | LOG_WARN("cannot retrieve component property '" << name << "'"
|
---|
89 | << " of component '" << component_.attribute(kPropertyComponentName.c_str()) << "'"
|
---|
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(const QString& name, bool defaultValue) const
|
---|
100 | {
|
---|
101 | return hasProperty(name) ? getProperty(name) == "true" : defaultValue;
|
---|
102 | }
|
---|
103 |
|
---|
104 | int XmlComponentConfig::getIntProperty(const QString& name, int defaultValue) const
|
---|
105 | {
|
---|
106 | return hasProperty(name) ? getProperty(name).toInt() : defaultValue;
|
---|
107 | }
|
---|
108 |
|
---|
109 | double XmlComponentConfig::getDoubleProperty(const QString& name, double defaultValue) const
|
---|
110 | {
|
---|
111 | return hasProperty(name) ? getProperty(name).toDouble() : defaultValue;
|
---|
112 | }
|
---|
113 |
|
---|
114 | void XmlComponentConfig::setProperty(const QString& name, const QString& value)
|
---|
115 | {
|
---|
116 | component_.setAttribute(name, value);
|
---|
117 | LOG_INFO("property " << name
|
---|
118 | << " of the component " << component_.attribute(kPropertyComponentName.c_str())
|
---|
119 | << " was set to : " << value
|
---|
120 | );
|
---|
121 | }
|
---|
122 |
|
---|
123 | bool XmlComponentConfig::hasProperty(const QString& name) const
|
---|
124 | {
|
---|
125 | return component_.hasAttribute(name);
|
---|
126 | }
|
---|
127 |
|
---|
128 | QDomElement XmlComponentConfig::qDomElement() 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 component_.attribute(kPropertyComponentName.c_str());
|
---|
141 | return getProperty(kPropertyComponentName.c_str());
|
---|
142 | }
|
---|
143 |
|
---|
144 | QString const XmlComponentConfig::getComponentType() const
|
---|
145 | {
|
---|
146 | return getProperty(kPropertyComponentType.c_str());
|
---|
147 | }
|
---|
148 |
|
---|
149 | QString const XmlComponentConfig::getConnectionType() const
|
---|
150 | {
|
---|
151 | return getProperty(kPropertyConnectionType.c_str());
|
---|
152 | }
|
---|
153 |
|
---|
154 | QString const XmlComponentConfig::getConnectionInput() const
|
---|
155 | {
|
---|
156 | return getProperty(kPropertyConnectionInput.c_str());
|
---|
157 | }
|
---|
158 |
|
---|
159 | QString const XmlComponentConfig::getConnectionOutput() const
|
---|
160 | {
|
---|
161 | return getProperty(kPropertyConnectionOutput.c_str());
|
---|
162 | }
|
---|
163 |
|
---|
164 | int const XmlComponentConfig::getConnectionPriority() const
|
---|
165 | {
|
---|
166 | return getIntProperty(kPropertyConnectionPriority.c_str(),0);
|
---|
167 | }
|
---|