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 char* kPropertyComponentName = "name";
|
---|
17 | static const char* kPropertyComponentType = "type";
|
---|
18 | static const char* kPropertyConnectionType = "type";
|
---|
19 | static const char* kPropertyConnectionInput = "input";
|
---|
20 | static const char* kPropertyConnectionOutput = "output";
|
---|
21 | static const char* kPropertyConnectionPriority = "priority";
|
---|
22 |
|
---|
23 | XmlComponentConfig::XmlComponentConfig(const QString& name)
|
---|
24 | {
|
---|
25 | LOG_TRACE("XmlComponentConfig(QString)");
|
---|
26 |
|
---|
27 | // Get the pointer to the document and create the component.
|
---|
28 | parentDocument_ = XmlConfigFile::create();
|
---|
29 | if (name != QString::null)
|
---|
30 | component_ = parentDocument_->createComponent(name);
|
---|
31 | }
|
---|
32 |
|
---|
33 | /// Destructor.
|
---|
34 | XmlComponentConfig::~XmlComponentConfig()
|
---|
35 | {
|
---|
36 | LOG_TRACE("~XmlComponentConfig()");
|
---|
37 | }
|
---|
38 |
|
---|
39 | void XmlComponentConfig::addProperty(const QString& name)
|
---|
40 | {
|
---|
41 | if (hasProperty(name))
|
---|
42 | {
|
---|
43 | LOG_ERROR("cannot add component property:"
|
---|
44 | << " component '" << component_.attribute(kPropertyComponentName) << "'"
|
---|
45 | << " already contains property '" << name << "'"
|
---|
46 | << " and its value is '" << component_.attribute(name) << "'"
|
---|
47 | );
|
---|
48 | return;
|
---|
49 | }
|
---|
50 |
|
---|
51 | // The property does not exist, it can be added.
|
---|
52 | component_.setAttribute(name, 0);
|
---|
53 | LOG_INFO("property '" << name << "'"
|
---|
54 | << " was added to the component " << component_.attribute(kPropertyComponentName) << "'"
|
---|
55 | << " and set to '" << component_.attribute(name) << "'"
|
---|
56 | );
|
---|
57 | }
|
---|
58 |
|
---|
59 | int XmlComponentConfig::delProperty(const QString& name)
|
---|
60 | {
|
---|
61 | if (!hasProperty(name)) {
|
---|
62 | LOG_WARN("cannot delete compoenent property '" << name << "'"
|
---|
63 | << " of component '" << component_.attribute(kPropertyComponentName) << "'"
|
---|
64 | << ". Component does not contain this property."
|
---|
65 | );
|
---|
66 | return false;
|
---|
67 | }
|
---|
68 |
|
---|
69 | // The property exists, it can be removed.
|
---|
70 | component_.removeAttribute(name);
|
---|
71 | LOG_INFO("property '" << name << "' "
|
---|
72 | << " of component '" << component_.attribute(kPropertyComponentName) << "'"
|
---|
73 | << " was deleted"
|
---|
74 | );
|
---|
75 |
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 |
|
---|
79 | QString XmlComponentConfig::getProperty(const QString& name, const QString& defaultValue) const
|
---|
80 | {
|
---|
81 | if (!hasProperty(name))
|
---|
82 | {
|
---|
83 | LOG_WARN("cannot retrieve component property '" << name << "'"
|
---|
84 | << " of component '" << component_.attribute(kPropertyComponentName) << "'"
|
---|
85 | << ". Component does not contain this property."
|
---|
86 | );
|
---|
87 | return defaultValue;
|
---|
88 | }
|
---|
89 |
|
---|
90 | // The property exists, the value can be retrieved.
|
---|
91 | return component_.attribute(name);
|
---|
92 | }
|
---|
93 |
|
---|
94 | bool XmlComponentConfig::getBoolProperty(const QString& name, bool defaultValue) const
|
---|
95 | {
|
---|
96 | return hasProperty(name) ? getProperty(name) == "true" : defaultValue;
|
---|
97 | }
|
---|
98 |
|
---|
99 | int XmlComponentConfig::getIntProperty(const QString& name, int defaultValue) const
|
---|
100 | {
|
---|
101 | return hasProperty(name) ? getProperty(name).toInt() : defaultValue;
|
---|
102 | }
|
---|
103 |
|
---|
104 | double XmlComponentConfig::getDoubleProperty(const QString& name, double defaultValue) const
|
---|
105 | {
|
---|
106 | return hasProperty(name) ? getProperty(name).toDouble() : defaultValue;
|
---|
107 | }
|
---|
108 |
|
---|
109 | void XmlComponentConfig::setProperty(const QString& name, const QString& value)
|
---|
110 | {
|
---|
111 | component_.setAttribute(name, value);
|
---|
112 | LOG_INFO("property " << name
|
---|
113 | << " of the component " << component_.attribute(kPropertyComponentName)
|
---|
114 | << " was set to : " << value
|
---|
115 | );
|
---|
116 | }
|
---|
117 |
|
---|
118 | bool XmlComponentConfig::hasProperty(const QString& name) const
|
---|
119 | {
|
---|
120 | return component_.hasAttribute(name);
|
---|
121 | }
|
---|
122 |
|
---|
123 | QDomElement XmlComponentConfig::qDomElement() const
|
---|
124 | {
|
---|
125 | return component_;
|
---|
126 | }
|
---|
127 |
|
---|
128 | void XmlComponentConfig::localCopy(const QDomElement& elementToCopy)
|
---|
129 | {
|
---|
130 | component_ = elementToCopy;
|
---|
131 | }
|
---|
132 |
|
---|
133 | QString const XmlComponentConfig::getComponentName() const
|
---|
134 | {
|
---|
135 | return getProperty(kPropertyComponentName);
|
---|
136 | }
|
---|
137 |
|
---|
138 | QString const XmlComponentConfig::getComponentType() const
|
---|
139 | {
|
---|
140 | return getProperty(kPropertyComponentType);
|
---|
141 | }
|
---|
142 |
|
---|
143 | QString const XmlComponentConfig::getConnectionType() const
|
---|
144 | {
|
---|
145 | return getProperty(kPropertyConnectionType);
|
---|
146 | }
|
---|
147 |
|
---|
148 | QString const XmlComponentConfig::getConnectionInput() const
|
---|
149 | {
|
---|
150 | return getProperty(kPropertyConnectionInput);
|
---|
151 | }
|
---|
152 |
|
---|
153 | QString const XmlComponentConfig::getConnectionOutput() const
|
---|
154 | {
|
---|
155 | return getProperty(kPropertyConnectionOutput);
|
---|
156 | }
|
---|
157 |
|
---|
158 | int const XmlComponentConfig::getConnectionPriority() const
|
---|
159 | {
|
---|
160 | return getIntProperty(kPropertyConnectionPriority, /*defaultValue=*/0);
|
---|
161 | }
|
---|