source: pacpusframework/trunk/src/PacpusLib/XmlComponentConfig.cpp@ 63

Last change on this file since 63 was 62, checked in by Marek Kurdej, 12 years ago

Update: license info.

File size: 3.8 KB
Line 
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
10using namespace pacpus;
11using namespace std;
12
13DECLARE_STATIC_LOGGER("pacpus.core.XmlComponentConfig");
14
15static const string kPropertyComponentType = "type";
16
17XmlComponentConfig::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.
28XmlComponentConfig::~XmlComponentConfig()
29{
30 LOG_TRACE("~XmlComponentConfig()");
31}
32
33void 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
53int 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
74QString 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
89int XmlComponentConfig::getBoolProperty(const QString& name, bool defaultValue) const
90{
91 return hasProperty(name) ? getProperty(name) == "true" : defaultValue;
92}
93
94int XmlComponentConfig::getIntProperty(const QString& name, int defaultValue) const
95{
96 return hasProperty(name) ? getProperty(name).toInt() : defaultValue;
97}
98
99int XmlComponentConfig::getDoubleProperty(const QString& name, double defaultValue) const
100{
101 return hasProperty(name) ? getProperty(name).toDouble() : defaultValue;
102}
103
104void 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
113bool XmlComponentConfig::hasProperty(const QString& name) const
114{
115 return component_.hasAttribute(name);
116}
117
118QDomElement XmlComponentConfig::qDomElement() const
119{
120 return component_;
121}
122
123void XmlComponentConfig::localCopy(const QDomElement& elementToCopy)
124{
125 component_ = elementToCopy;
126}
127
128QString const XmlComponentConfig::getComponentName() const
129{
130 return component_.tagName();
131}
132
133QString const XmlComponentConfig::getComponentType() const
134{
135 return getProperty(kPropertyComponentType.c_str());
136}
Note: See TracBrowser for help on using the repository browser.