PandA-2024.02
xml_node.hpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
43 #ifndef XML_NODE_HPP
44 #define XML_NODE_HPP
45 
46 #include "refcount.hpp"
47 #include "simple_indent.hpp"
48 
49 #include <iosfwd>
50 #include <list>
51 #include <string>
52 #include <utility>
53 
55 #include "custom_set.hpp"
56 
62 class xml_element;
63 class xml_text_node;
64 class xml_comment_node;
65 class xml_att_decl_node;
67 
68 #define XML_TAB_SIZE 2
69 
70 class xml_node
71 {
72  private:
74  std::string name;
75 
77  int line;
78 
79  public:
83  explicit xml_node(const std::string& _name) : name(_name), line(0)
84  {
85  }
87  virtual ~xml_node() = default;
88 
90  using node_list = std::list<xml_nodeRef>;
91 
98  virtual void print(std::ostream& os, bool formatted, simple_indent* pp) const = 0;
99 
103  friend std::ostream& operator<<(std::ostream& os, const xml_node& s)
104  {
106  s.print(os, true, &PP);
107  return os;
108  }
112  friend std::ostream& operator<<(std::ostream& os, const xml_node* s)
113  {
115  s->print(os, true, &PP);
116  return os;
117  }
121  friend std::ostream& operator<<(std::ostream& os, const xml_nodeRef& s)
122  {
124  s->print(os, true, &PP);
125  return os;
126  }
127 
132  std::string get_name() const
133  {
134  return name;
135  }
136 
141  void set_name(const std::string& _name)
142  {
143  name = _name;
144  }
145 
149  int get_line() const;
150 
155  void set_line(int _line);
156 
162  static void convert_unescaped(std::string& ioString)
163  {
164  std::string::size_type lPos = 0;
165  while((lPos = ioString.find_first_of("&<>'\"", lPos)) != std::string::npos)
166  {
167  switch(ioString[lPos])
168  {
169  case '&':
170  ioString.replace(lPos++, 1, "&amp;");
171  break;
172  case '<':
173  ioString.replace(lPos++, 1, "&lt;");
174  break;
175  case '>':
176  ioString.replace(lPos++, 1, "&gt;");
177  break;
178  case '\'':
179  ioString.replace(lPos++, 1, "&apos;");
180  break;
181  case '"':
182  ioString.replace(lPos++, 1, "&quot;");
183  break;
184  default:
185  {
186  // Do nothing
187  }
188  }
189  }
190  }
197  static void convert_escaped(std::string& ioString)
198  {
199  std::string::size_type lPos = 0;
200  while((lPos = ioString.find("&amp;", lPos)) != std::string::npos)
201  {
202  ioString.replace(lPos++, 5, "&");
203  }
204  lPos = 0;
205  while((lPos = ioString.find("&lt;", lPos)) != std::string::npos)
206  {
207  ioString.replace(lPos++, 4, "<");
208  }
209  lPos = 0;
210  while((lPos = ioString.find("&gt;", lPos)) != std::string::npos)
211  {
212  ioString.replace(lPos++, 4, ">");
213  }
214  lPos = 0;
215  while((lPos = ioString.find("&apos;", lPos)) != std::string::npos)
216  {
217  ioString.replace(lPos++, 6, "\'");
218  }
219  lPos = 0;
220  while((lPos = ioString.find("&quot;", lPos)) != std::string::npos)
221  {
222  ioString.replace(lPos++, 6, "\"");
223  }
224  }
225 };
226 
227 class xml_child : public xml_node
228 {
229  public:
233  explicit xml_child(const std::string& _name) : xml_node(_name), first_text(nullptr)
234  {
235  }
236 
240  void print(std::ostream& os, bool formatted, simple_indent* pp) const override
241  {
242  auto it_end = child_list.end();
243  for(auto it = child_list.begin(); it != it_end; ++it)
244  {
245  (*it)->print(os, formatted, pp);
246  }
247  }
248 
253  xml_element* add_child_element(const std::string& name);
254 
259  xml_element* add_child_element(const xml_nodeRef& node);
260 
265  xml_text_node* add_child_text(const std::string& content);
266 
273  {
274  return first_text;
275  }
276 
283  {
284  return first_text;
285  }
286 
291  xml_comment_node* add_child_comment(const std::string& content);
292 
297  xml_att_decl_node* add_child_attribute_declaration(const std::string& name);
298 
303  {
304  child_list.remove(xml_nodeRef(el, null_deleter()));
305  }
306 
311  {
312  return child_list;
313  }
314 
318  const node_list& get_children() const
319  {
320  return child_list;
321  }
322 
326  bool has_child() const
327  {
328  return !child_list.empty();
329  }
330 
336  const CustomSet<xml_nodeRef> CGetDescendants(const std::string& path) const;
337 
338  private:
341 };
342 
343 #endif
Simple pretty print functor.
node_list child_list
Definition: xml_node.hpp:339
static void convert_unescaped(std::string &ioString)
Convert unescaped characters.
Definition: xml_node.hpp:162
xml_child(const std::string &_name)
constructor
Definition: xml_node.hpp:233
const node_list & get_children() const
Obtain the list of child nodes.
Definition: xml_node.hpp:318
int line
The line number in the XML file (not unsigned because lineno function of the lexer returns an int) ...
Definition: xml_node.hpp:77
REF_FORWARD_DECL(xml_node)
friend std::ostream & operator<<(std::ostream &os, const xml_node &s)
Friend definition of the << operator.
Definition: xml_node.hpp:103
std::string get_name() const
Get the name of this node.
Definition: xml_node.hpp:132
void set_name(const std::string &_name)
Set the name of this node.
Definition: xml_node.hpp:141
bool has_child() const
Definition: xml_node.hpp:326
friend std::ostream & operator<<(std::ostream &os, const xml_nodeRef &s)
Friend definition of the << operator.
Definition: xml_node.hpp:121
redefinition of set to manage ordered/unordered structures
xml_text_node * first_text
Definition: xml_node.hpp:340
int get_line() const
Discover at what line number this node occurs in the XML file.
Definition: xml_node.cpp:101
virtual ~xml_node()=default
destructor
std::list< xml_nodeRef > node_list
type for list of xml nodes
Definition: xml_node.hpp:90
#define STD_OPENING_CHAR
STD include.
virtual void print(std::ostream &os, bool formatted, simple_indent *pp) const =0
Print the class.
Template definition of refcount.
friend std::ostream & operator<<(std::ostream &os, const xml_node *s)
Friend definition of the << operator.
Definition: xml_node.hpp:112
null deleter
Definition: refcount.hpp:51
Very simple pretty printer functor.
#define STD_CLOSING_CHAR
Special closing character used to close the current nested level.
void set_line(int _line)
Set the line this node occurs in the XML file.
Definition: xml_node.cpp:96
int el
Definition: adpcm.c:105
xml_text_node * get_child_text()
Get the first child text content node.
Definition: xml_node.hpp:272
xml_node(const std::string &_name)
constructor
Definition: xml_node.hpp:83
void print(std::ostream &os, bool formatted, simple_indent *pp) const override
Print the class.
Definition: xml_node.hpp:240
#define XML_TAB_SIZE
Definition: xml_node.hpp:68
node_list const & get_children()
Obtain the list of child nodes.
Definition: xml_node.hpp:310
static void convert_escaped(std::string &ioString)
Convert escaped characters.
Definition: xml_node.hpp:197
const xml_text_node * get_child_text() const
Get the first child text content node.
Definition: xml_node.hpp:282
void remove_child(xml_node *el)
Remove the child node.
Definition: xml_node.hpp:302
std::string name
name of the node
Definition: xml_node.hpp:74

Generated on Mon Feb 12 2024 13:02:55 for PandA-2024.02 by doxygen 1.8.13