GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 _______________________________________________ */
38 class 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  ***************************************************************************/
48 class GXmlElement : public GXmlNode {
49 
50  // Friend classes
51  friend class GXml;
52 
53 public:
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 
88 protected:
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  ***************************************************************************/
108 inline
109 std::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  ***************************************************************************/
120 inline
121 const 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  ***************************************************************************/
132 inline
133 void 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  ***************************************************************************/
145 inline
146 std::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  ***************************************************************************/
157 inline
158 double 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  ***************************************************************************/
169 inline
170 int 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  ***************************************************************************/
181 inline
183 {
184  return (NT_ELEMENT);
185 }
186 
187 
188 /***********************************************************************//**
189  * @brief Return number of attributes
190  *
191  * @return Number of attributes.
192  ***************************************************************************/
193 inline
194 int GXmlElement::attributes(void) const
195 {
196  return (int)(m_attr.size());
197 }
198 
199 #endif /* GXMLELEMENT_HPP */
Abstract XML node base class.
Definition: GXmlNode.hpp:57
virtual std::string print(const GChatter &chatter=NORMAL, const int &indent=0) const
Print XML element.
std::string string(void) const
Return string value.
std::string value(void) const
Return string value.
std::vector< GXmlAttribute * > m_attr
Attributes.
Definition: GXmlElement.hpp:99
XML element node class.
Definition: GXmlElement.hpp:48
XML attribute class.
Gammalib tools definition.
int integer(void) const
Return integer value.
const std::string & name(void) const
Return XML element name.
virtual void clear(void)
Clear XML element.
virtual ~GXmlElement(void)
Destructor.
Abstract URL base class.
Definition: GUrl.hpp:44
void parse_attribute(size_t *pos, const std::string &segment)
Parse element attribute.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
void remove_attribute(const std::string &name)
Remove attribute from element.
XML class.
Definition: GXml.hpp:172
bool has_attribute(const std::string &name) const
Check if element has a given attribute.
void init_members(void)
Initialise class members.
double real(void) const
Return floating point value.
GChatter
Definition: GTypemaps.hpp:33
void copy_members(const GXmlElement &node)
Copy class members.
GXmlElement(void)
Void constructor.
Definition: GXmlElement.cpp:65
virtual NodeType type(void) const
Return XML node type.
void free_members(void)
Delete class members.
GXmlElement & operator=(const GXmlElement &node)
Assignment operator.
std::string m_name
Element name.
Definition: GXmlElement.hpp:98
virtual void write(GUrl &url, const int &indent=0) const
Write element into URL.
void parse_start(const std::string &segment)
Parse element start segment string.
int toint(const std::string &arg)
Convert string into integer value.
Definition: GTools.cpp:821
virtual std::string classname(void) const
Return class name.
void parse_stop(const std::string &segment)
Parse element stop segment string.
Abstract XML node base class interface definition.
int attributes(void) const
Return number of attributes.
virtual GXmlElement * clone(void) const
Clone XML element.
XML attribute class interface definition.
double todouble(const std::string &arg)
Convert string into double precision value.
Definition: GTools.cpp:926