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

Last change on this file since 116 was 116, checked in by Marek Kurdej, 11 years ago

Added: PacpusException - base class for all exceptions. DbiteExceptions inherits from it.
Added: PacpusLibConfig.h - dllimport/dllexport clauses separated from pacpus.h.
Update: comments.

  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
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/// @author Gerald Dherbomez <firstname.surname@utc.fr>
6/// @version $Id: XmlComponentConfig.cpp 116 2013-06-25 11:44:25Z kurdejma $
7
8#include <Pacpus/kernel/XmlComponentConfig.h>
9#include <Pacpus/kernel/Log.h>
10#include <Pacpus/kernel/XmlConfigFile.h>
11
12using namespace pacpus;
13using namespace std;
14
15DECLARE_STATIC_LOGGER("pacpus.core.XmlComponentConfig");
16
17static const string kPropertyComponentType = "type";
18
19XmlComponentConfig::XmlComponentConfig(const QString& name)
20{
21 LOG_TRACE("XmlComponentConfig(QString)");
22
23 // Get the pointer to the document and create the component.
24 parentDocument_ = XmlConfigFile::create();
25 if (name != QString::null)
26 component_ = parentDocument_->createComponent(name);
27}
28
29/// Destructor.
30XmlComponentConfig::~XmlComponentConfig()
31{
32 LOG_TRACE("~XmlComponentConfig()");
33}
34
35void XmlComponentConfig::addProperty(const QString& name)
36{
37 if (hasProperty(name))
38 {
39 LOG_ERROR("cannot add component property:"
40 << " component '" << component_.tagName() << "'"
41 << " already contains property '" << name << "'"
42 << " and its value is '" << component_.attribute(name) << "'"
43 );
44 return;
45 }
46
47 // The property does not exist, it can be added.
48 component_.setAttribute(name, 0);
49 LOG_INFO("property '" << name << "'"
50 << " was added to the component " << component_.tagName() << "'"
51 << " and set to '" << component_.attribute(name) << "'"
52 );
53}
54
55int XmlComponentConfig::delProperty(const QString& name)
56{
57 if (!hasProperty(name)) {
58 LOG_WARN("cannot delete component property '" << name << "'"
59 << " of component '" << component_.tagName() << "'"
60 << ". Component does not contain this property."
61 );
62 return false;
63 }
64
65 // The property exists, it can be removed.
66 component_.removeAttribute(name);
67 LOG_INFO("property '" << name << "' "
68 << " of component '" << component_.tagName() << "'"
69 << " was deleted"
70 );
71
72 return true;
73}
74
75QString XmlComponentConfig::getProperty(const QString& name, const QString& defaultValue) const
76{
77 if (!hasProperty(name))
78 {
79 LOG_WARN("cannot retrieve component property '" << name << "'"
80 << " of component '" << component_.tagName() << "'"
81 << ". Component does not contain this property."
82 );
83 return defaultValue;
84 }
85
86 // The property exists, the value can be retrieved.
87 return component_.attribute(name);
88}
89
90bool XmlComponentConfig::getBoolProperty(const QString& name, bool defaultValue) const
91{
92 return hasProperty(name) ? getProperty(name) == "true" : defaultValue;
93}
94
95int XmlComponentConfig::getIntProperty(const QString& name, int defaultValue) const
96{
97 return hasProperty(name) ? getProperty(name).toInt() : defaultValue;
98}
99
100double XmlComponentConfig::getDoubleProperty(const QString& name, double defaultValue) const
101{
102 return hasProperty(name) ? getProperty(name).toDouble() : defaultValue;
103}
104
105void XmlComponentConfig::setProperty(const QString& name, const QString& value)
106{
107 component_.setAttribute(name, value);
108 LOG_INFO("property " << name
109 << " of the component " << component_.tagName()
110 << " was set to : " << value
111 );
112}
113
114bool XmlComponentConfig::hasProperty(const QString& name) const
115{
116 return component_.hasAttribute(name);
117}
118
119QDomElement XmlComponentConfig::qDomElement() const
120{
121 return component_;
122}
123
124void XmlComponentConfig::localCopy(const QDomElement& elementToCopy)
125{
126 component_ = elementToCopy;
127}
128
129QString const XmlComponentConfig::getComponentName() const
130{
131 return component_.tagName();
132}
133
134QString const XmlComponentConfig::getComponentType() const
135{
136 return getProperty(kPropertyComponentType.c_str());
137}
Note: See TracBrowser for help on using the repository browser.