GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GXmlNode.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GXmlNode.hpp - Abstract XML node base class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-2016 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 GXmlNode.hpp
23  * @brief Abstract XML node base class interface definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GXMLNODE_HPP
28 #define GXMLNODE_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include <vector>
33 #include "GContainer.hpp"
34 #include "GUrl.hpp"
35 #include "GFilename.hpp"
36 
37 /* __ Forward declarations _______________________________________________ */
38 class GXmlElement;
39 
40 
41 /***********************************************************************//**
42  * @class GXmlNode
43  *
44  * @brief Abstract XML node base class
45  *
46  * This class defines the interface for all XML nodes. Each XML node can be
47  * the container for a number of child nodes. The GXmlNode class is thus
48  * designed as a container class that holds a list of pointers of type
49  * GXmlNode. This allows implementing an arbitrary complex tree structure.
50  *
51  * The only member of this class is a list of XML node pointers. GXmlNode
52  * handles the proper allocation and deallocation of the node memory.
53  *
54  * Most methods are identical to those of the GXml class. Please refer to
55  * the documentation of this class for a description of the methods.
56  ***************************************************************************/
57 class GXmlNode : public GContainer {
58 
59 public:
60  // Constructors and destructors
61  GXmlNode(void);
62  GXmlNode(const GXmlNode& node);
63  virtual ~GXmlNode(void);
64 
65  // Operators
66  GXmlNode& operator=(const GXmlNode& node);
67  GXmlNode* operator[](const int& index);
68  const GXmlNode* operator[](const int& index) const;
69 
70  // Public enumerators
71  enum NodeType {
80  };
81 
82  // Methods
83  virtual void clear(void) = 0;
84  virtual GXmlNode* clone(void) const = 0;
85  virtual std::string classname(void) const = 0;
86  virtual int size(void) const;
87  virtual bool is_empty(void) const;
88  virtual GXmlNode* set(const int& index, const GXmlNode& node);
89  virtual GXmlNode* append(const GXmlNode& node);
90  virtual GXmlElement* append(const std::string& segment);
91  virtual GXmlNode* insert(const int& index, const GXmlNode& node);
92  virtual void remove(const int& index);
93  virtual void reserve(const int& num);
94  virtual void extend(const GXmlNode& node);
95  GXmlNode* parent(void) const;
96  void parent(GXmlNode* parent);
97  GFilename filename(void) const;
98  virtual int elements(void) const;
99  virtual int elements(const std::string& name) const;
100  virtual GXmlElement* element(const int& index);
101  virtual const GXmlElement* element(const int& index) const;
102  virtual GXmlElement* element(const std::string& name);
103  virtual const GXmlElement* element(const std::string& name) const;
104  virtual GXmlElement* element(const std::string& name,
105  const int& index);
106  virtual const GXmlElement* element(const std::string& name,
107  const int& index) const;
108  virtual void write(GUrl& url, const int& indent) const = 0;
109  virtual NodeType type(void) const = 0;
110  virtual std::string print(const GChatter& chatter = NORMAL,
111  const int& indent = 0) const = 0;
112  virtual std::string print(const GChatter& chatter = NORMAL) const;
113 
114 protected:
115  // Protected methods
116  void init_members(void);
117  void copy_members(const GXmlNode& node);
118  void free_members(void);
119  int extract_index(std::string& tag) const;
120 
121  // Protected data members
122  GXmlNode* m_parent; //!< Pointer on parent node
123  std::vector<GXmlNode*> m_nodes; //!< Pointer to child nodes
124 };
125 
126 
127 /***********************************************************************//**
128  * @brief Return number of child nodes
129  *
130  * @return Number of child nodes in node.
131  ***************************************************************************/
132 inline
133 int GXmlNode::size(void) const
134 {
135  return (int)m_nodes.size();
136 }
137 
138 
139 /***********************************************************************//**
140  * @brief Signals if node has no child nodes
141  *
142  * @return True if node has no child nodes.
143  ***************************************************************************/
144 inline
145 bool GXmlNode::is_empty(void) const
146 {
147  return m_nodes.empty();
148 }
149 
150 
151 /***********************************************************************//**
152  * @brief Reserve space for child nodes
153  *
154  * @param[in] num Number of child nodes for which space should be reserved.
155  ***************************************************************************/
156 inline
157 void GXmlNode::reserve(const int& num)
158 {
159  m_nodes.reserve(num);
160  return;
161 }
162 
163 
164 /***********************************************************************//**
165  * @brief Return parent XML node
166  *
167  * @return Parent of XML node.
168  ***************************************************************************/
169 inline
171 {
172  return (m_parent);
173 }
174 
175 
176 /***********************************************************************//**
177  * @brief Set parent of XML node
178  *
179  * @param[in] parent Parent of XML node.
180  ***************************************************************************/
181 inline
183 {
184  m_parent = parent;
185  return;
186 }
187 
188 #endif /* GXMLNODE_HPP */
Abstract XML node base class.
Definition: GXmlNode.hpp:57
GXmlNode * parent(void) const
Return parent XML node.
Definition: GXmlNode.hpp:170
GXmlNode & operator=(const GXmlNode &node)
Assignment operator.
Definition: GXmlNode.cpp:118
GFilename filename(void) const
Return filename of XML file.
Definition: GXmlNode.cpp:546
std::vector< GXmlNode * > m_nodes
Pointer to child nodes.
Definition: GXmlNode.hpp:123
virtual GXmlNode * set(const int &index, const GXmlNode &node)
Set XML child node.
Definition: GXmlNode.cpp:214
void init_members(void)
Initialise class members.
Definition: GXmlNode.cpp:880
XML element node class.
Definition: GXmlElement.hpp:48
virtual int elements(void) const
Return number of GXMLElement children of node.
Definition: GXmlNode.cpp:586
void free_members(void)
Delete class members.
Definition: GXmlNode.cpp:921
virtual void clear(void)=0
Clear object.
virtual void extend(const GXmlNode &node)
Append all XML child nodes from another XML node.
Definition: GXmlNode.cpp:510
virtual GXmlNode * insert(const int &index, const GXmlNode &node)
Insert XML child node.
Definition: GXmlNode.cpp:407
Abstract URL base class interface definition.
Abstract URL base class.
Definition: GUrl.hpp:44
virtual int size(void) const
Return number of child nodes.
Definition: GXmlNode.hpp:133
virtual GXmlNode * clone(void) const =0
Clones object.
Filename class.
Definition: GFilename.hpp:62
virtual void reserve(const int &num)
Reserve space for child nodes.
Definition: GXmlNode.hpp:157
GChatter
Definition: GTypemaps.hpp:33
virtual NodeType type(void) const =0
GXmlNode * m_parent
Pointer on parent node.
Definition: GXmlNode.hpp:122
void copy_members(const GXmlNode &node)
Copy class members.
Definition: GXmlNode.cpp:898
virtual std::string classname(void) const =0
Return class name.
virtual std::string print(const GChatter &chatter=NORMAL, const int &indent=0) const =0
virtual GXmlElement * element(const int &index)
Return pointer to GXMLElement child.
Definition: GXmlNode.cpp:640
virtual void write(GUrl &url, const int &indent) const =0
GXmlNode * operator[](const int &index)
Return pointer to XML child node.
Definition: GXmlNode.cpp:150
virtual bool is_empty(void) const
Signals if node has no child nodes.
Definition: GXmlNode.hpp:145
int extract_index(std::string &tag) const
Extract index from tag.
Definition: GXmlNode.cpp:950
Definition of interface for container classes.
virtual GXmlNode * append(const GXmlNode &node)
Append XML child node.
Definition: GXmlNode.cpp:287
GXmlNode(void)
Void constructor.
Definition: GXmlNode.cpp:65
Interface class for container classes.
Definition: GContainer.hpp:52
Filename class interface definition.
virtual ~GXmlNode(void)
Destructor.
Definition: GXmlNode.cpp:96