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

Last change on this file since 57 was 31, checked in by sgosseli, 12 years ago

Huge commit: use the new includes style in all the files, add the license header in all the headers, and in some cpp.

File size: 3.9 KB
Line 
1/**
2 *
3 * Distributed under the UTC Heudiascy Pacpus License, Version 1.0.
4 * Copyright (c) UTC Heudiasyc 2010 - 2013. All rights reserved.
5 *
6 * See the LICENSE file for more information or a copy at:
7 * http://www.hds.utc.fr/~kurdejma/LICENSE_1_0.txt
8 *
9 */
10
11#include <Pacpus/kernel/XmlComponentConfig.h>
12#include <Pacpus/kernel/Log.h>
13#include <Pacpus/kernel/XmlConfigFile.h>
14
15using namespace pacpus;
16using namespace std;
17
18DECLARE_STATIC_LOGGER("pacpus.core.XmlComponentConfig");
19
20static const string kPropertyComponentType = "type";
21
22XmlComponentConfig::XmlComponentConfig(const QString& name)
23{
24 LOG_TRACE("XmlComponentConfig(QString)");
25
26 // Get the pointer to the document and create the component.
27 parentDocument_ = XmlConfigFile::create();
28 if (name != QString::null)
29 component_ = parentDocument_->createComponent(name);
30}
31
32/// Destructor.
33XmlComponentConfig::~XmlComponentConfig()
34{
35 LOG_TRACE("~XmlComponentConfig()");
36}
37
38void XmlComponentConfig::addProperty(const QString& name)
39{
40 if (hasProperty(name))
41 {
42 LOG_ERROR("cannot add component property:"
43 << " component '" << component_.tagName() << "'"
44 << " already contains property '" << name << "'"
45 << " and its value is '" << component_.attribute(name) << "'"
46 );
47 return;
48 }
49
50 // The property does not exist, it can be added.
51 component_.setAttribute(name, 0);
52 LOG_INFO("property '" << name << "'"
53 << " was added to the component " << component_.tagName() << "'"
54 << " and set to '" << component_.attribute(name) << "'"
55 );
56}
57
58int XmlComponentConfig::delProperty(const QString& name)
59{
60 if (!hasProperty(name))
61 {
62 LOG_WARN("cannot delete compoenent property '" << name << "'"
63 << " of component '" << component_.tagName() << "'"
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_.tagName() << "'"
73 << " was deleted"
74 );
75
76 return true;
77}
78
79QString 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_.tagName() << "'"
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
94int XmlComponentConfig::getBoolProperty(const QString& name, bool defaultValue) const
95{
96 return hasProperty(name) ? getProperty(name) == "true" : defaultValue;
97}
98
99int XmlComponentConfig::getIntProperty(const QString& name, int defaultValue) const
100{
101 return hasProperty(name) ? getProperty(name).toInt() : defaultValue;
102}
103
104int XmlComponentConfig::getDoubleProperty(const QString& name, double defaultValue) const
105{
106 return hasProperty(name) ? getProperty(name).toDouble() : defaultValue;
107}
108
109void XmlComponentConfig::setProperty(const QString& name, const QString& value)
110{
111 component_.setAttribute(name, value);
112 LOG_INFO("property " << name
113 << " of the component " << component_.tagName()
114 << " was set to : " << value
115 );
116}
117
118bool XmlComponentConfig::hasProperty(const QString& name) const
119{
120 return component_.hasAttribute(name);
121}
122
123QDomElement XmlComponentConfig::qDomElement() const
124{
125 return component_;
126}
127
128void XmlComponentConfig::localCopy(const QDomElement& elementToCopy)
129{
130 component_ = elementToCopy;
131}
132
133QString const XmlComponentConfig::getComponentName() const
134{
135 return component_.tagName();
136}
137
138QString const XmlComponentConfig::getComponentType() const
139{
140 return getProperty(kPropertyComponentType.c_str());
141}
Note: See TracBrowser for help on using the repository browser.