GammaLib 2.0.0
Loading...
Searching...
No Matches
GXmlElement.hpp
Go to the documentation of this file.
1/***************************************************************************
2 * GXmlElement.hpp - XML element node class definition *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2010-2020 by Juergen Knoedlseder *
5 * ----------------------------------------------------------------------- *
6 * *
7 * This program is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21/**
22 * @file GXmlElement.hpp
23 * @brief XML element node class interface definition
24 * @author Juergen Knoedlseder
25 */
26
27#ifndef GXMLELEMENT_HPP
28#define GXMLELEMENT_HPP
29
30/* __ Includes ___________________________________________________________ */
31#include <string>
32#include <vector>
33#include "GTools.hpp"
34#include "GXmlNode.hpp"
35#include "GXmlAttribute.hpp"
36
37/* __ Forward declarations _______________________________________________ */
38class GUrl;
39
40
41/***********************************************************************//**
42 * @class GXmlElement
43 *
44 * @brief XML element node class
45 *
46 * This class implements an XML element with it's associated attributes.
47 ***************************************************************************/
48class GXmlElement : public GXmlNode {
49
50 // Friend classes
51 friend class GXml;
52
53public:
54 // Constructors and destructors
55 GXmlElement(void);
56 GXmlElement(const GXmlElement& node);
57 explicit GXmlElement(const std::string& segment);
58 GXmlElement(const std::string& name, const int& value);
59 GXmlElement(const std::string& name, const double& value);
60 GXmlElement(const std::string& name, const std::string& value);
61 virtual ~GXmlElement(void);
62
63 // Operators
64 GXmlElement& operator=(const GXmlElement& node);
65
66 // Methods
67 virtual void clear(void);
68 virtual GXmlElement* clone(void) const;
69 virtual std::string classname(void) const;
70 const std::string& name(void) const;
71 void name(const std::string& name);
72 std::string value(void) const;
73 std::string string(void) const;
74 double real(void) const;
75 int integer(void) const;
76 int attributes(void) const;
77 const GXmlAttribute* attribute(const int& index) const;
78 std::string attribute(const std::string& name) const;
79 void attribute(const std::string& name,
80 const std::string& value);
81 bool has_attribute(const std::string& name) const;
82 void remove_attribute(const std::string& name);
83 virtual void write(GUrl& url, const int& indent = 0) const;
84 virtual NodeType type(void) const;
85 virtual std::string print(const GChatter& chatter = NORMAL,
86 const int& indent = 0) const;
87
88protected:
89 // Protected methods
90 void init_members(void);
91 void copy_members(const GXmlElement& node);
92 void free_members(void);
93 void parse_start(const std::string& segment);
94 void parse_stop(const std::string& segment);
95 void parse_attribute(size_t* pos, const std::string& segment);
96
97 // Protected data members
98 std::string m_name; //!< Element name
99 std::vector<GXmlAttribute*> m_attr; //!< Attributes
100};
101
102
103/***********************************************************************//**
104 * @brief Return class name
105 *
106 * @return String containing the class name ("GXmlElement").
107 ***************************************************************************/
108inline
109std::string GXmlElement::classname(void) const
110{
111 return ("GXmlElement");
112}
113
114
115/***********************************************************************//**
116 * @brief Return XML element name
117 *
118 * @return XML element name.
119 ***************************************************************************/
120inline
121const std::string& GXmlElement::name(void) const
122{
123 return (m_name);
124}
125
126
127/***********************************************************************//**
128 * @brief Set XML element name
129 *
130 * @param[in] name XML element name.
131 ***************************************************************************/
132inline
133void GXmlElement::name(const std::string& name)
134{
135 m_name = name;
136 return;
137}
138
139
140/***********************************************************************//**
141 * @brief Return string value
142 *
143 * @return String value.
144 ***************************************************************************/
145inline
146std::string GXmlElement::string(void) const
147{
148 return (value());
149}
150
151
152/***********************************************************************//**
153 * @brief Return floating point value
154 *
155 * @return Floating point value.
156 ***************************************************************************/
157inline
158double GXmlElement::real(void) const
159{
160 return (gammalib::todouble(value()));
161}
162
163
164/***********************************************************************//**
165 * @brief Return integer value
166 *
167 * @return Integer value.
168 ***************************************************************************/
169inline
170int GXmlElement::integer(void) const
171{
172 return (gammalib::toint(value()));
173}
174
175
176/***********************************************************************//**
177 * @brief Return XML node type
178 *
179 * @return XML node type (NT_ELEMENT).
180 ***************************************************************************/
181inline
183{
184 return (NT_ELEMENT);
185}
186
187
188/***********************************************************************//**
189 * @brief Return number of attributes
190 *
191 * @return Number of attributes.
192 ***************************************************************************/
193inline
195{
196 return (int)(m_attr.size());
197}
198
199#endif /* GXMLELEMENT_HPP */
Gammalib tools definition.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
XML attribute class interface definition.
Abstract XML node base class interface definition.
Abstract URL base class.
Definition GUrl.hpp:44
XML attribute class.
XML element node class.
void parse_stop(const std::string &segment)
Parse element stop segment string.
std::string m_name
Element name.
std::vector< GXmlAttribute * > m_attr
Attributes.
virtual void clear(void)
Clear XML element.
virtual std::string classname(void) const
Return class name.
void parse_start(const std::string &segment)
Parse element start segment string.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
virtual ~GXmlElement(void)
Destructor.
void free_members(void)
Delete class members.
virtual void write(GUrl &url, const int &indent=0) const
Write element into URL.
GXmlElement(void)
Void constructor.
int integer(void) const
Return integer value.
GXmlElement & operator=(const GXmlElement &node)
Assignment operator.
void copy_members(const GXmlElement &node)
Copy class members.
bool has_attribute(const std::string &name) const
Check if element has a given attribute.
void parse_attribute(size_t *pos, const std::string &segment)
Parse element attribute.
std::string value(void) const
Return string value.
void remove_attribute(const std::string &name)
Remove attribute from element.
std::string string(void) const
Return string value.
virtual GXmlElement * clone(void) const
Clone XML element.
const std::string & name(void) const
Return XML element name.
virtual NodeType type(void) const
Return XML node type.
int attributes(void) const
Return number of attributes.
virtual std::string print(const GChatter &chatter=NORMAL, const int &indent=0) const
Print XML element.
double real(void) const
Return floating point value.
void init_members(void)
Initialise class members.
Abstract XML node base class.
Definition GXmlNode.hpp:57
XML class.
Definition GXml.hpp:172
double todouble(const std::string &arg)
Convert string into double precision value.
Definition GTools.cpp:926
int toint(const std::string &arg)
Convert string into integer value.
Definition GTools.cpp:821