GammaLib  2.0.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-2018 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 "GXmlNode.hpp"
34 #include "GXmlAttribute.hpp"
35 
36 /* __ Forward declarations _______________________________________________ */
37 class GUrl;
38 
39 
40 /***********************************************************************//**
41  * @class GXmlElement
42  *
43  * @brief XML element node class
44  *
45  * This class implements an XML element with it's associated attributes.
46  ***************************************************************************/
47 class GXmlElement : public GXmlNode {
48 
49  // Friend classes
50  friend class GXml;
51 
52 public:
53  // Constructors and destructors
54  GXmlElement(void);
55  GXmlElement(const GXmlElement& node);
56  explicit GXmlElement(const std::string& segment);
57  virtual ~GXmlElement(void);
58 
59  // Operators
60  GXmlElement& operator=(const GXmlElement& node);
61 
62  // Methods
63  virtual void clear(void);
64  virtual GXmlElement* clone(void) const;
65  virtual std::string classname(void) const;
66  const std::string& name(void) const;
67  void name(const std::string& name);
68  int attributes(void) const;
69  const GXmlAttribute* attribute(const int& index) const;
70  std::string attribute(const std::string& name) const;
71  void attribute(const std::string& name,
72  const std::string& value);
73  bool has_attribute(const std::string& name) const;
74  void remove_attribute(const std::string& name);
75  virtual void write(GUrl& url, const int& indent = 0) const;
76  virtual NodeType type(void) const;
77  virtual std::string print(const GChatter& chatter = NORMAL,
78  const int& indent = 0) const;
79 
80 protected:
81  // Protected methods
82  void init_members(void);
83  void copy_members(const GXmlElement& node);
84  void free_members(void);
85  void parse_start(const std::string& segment);
86  void parse_stop(const std::string& segment);
87  void parse_attribute(size_t* pos, const std::string& segment);
88 
89  // Protected data members
90  std::string m_name; //!< Element name
91  std::vector<GXmlAttribute*> m_attr; //!< Attributes
92 };
93 
94 
95 /***********************************************************************//**
96  * @brief Return class name
97  *
98  * @return String containing the class name ("GXmlElement").
99  ***************************************************************************/
100 inline
101 std::string GXmlElement::classname(void) const
102 {
103  return ("GXmlElement");
104 }
105 
106 
107 /***********************************************************************//**
108  * @brief Return XML element name
109  *
110  * @return XML element name.
111  ***************************************************************************/
112 inline
113 const std::string& GXmlElement::name(void) const
114 {
115  return (m_name);
116 }
117 
118 
119 /***********************************************************************//**
120  * @brief Set XML element name
121  *
122  * @param[in] name XML element name.
123  ***************************************************************************/
124 inline
125 void GXmlElement::name(const std::string& name)
126 {
127  m_name = name;
128  return;
129 }
130 
131 
132 /***********************************************************************//**
133  * @brief Return XML node type
134  *
135  * @return XML node type (NT_ELEMENT).
136  ***************************************************************************/
137 inline
139 {
140  return (NT_ELEMENT);
141 }
142 
143 
144 /***********************************************************************//**
145  * @brief Return number of attributes
146  *
147  * @return Number of attributes.
148  ***************************************************************************/
149 inline
150 int GXmlElement::attributes(void) const
151 {
152  return (int)(m_attr.size());
153 }
154 
155 #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::vector< GXmlAttribute * > m_attr
Attributes.
Definition: GXmlElement.hpp:91
XML element node class.
Definition: GXmlElement.hpp:47
XML attribute class.
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.
GChatter
Definition: GTypemaps.hpp:33
void copy_members(const GXmlElement &node)
Copy class members.
GXmlElement(void)
Void constructor.
Definition: GXmlElement.cpp:63
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:90
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.
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.