PandA-2024.02
xml_node.cpp
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  */
42 #include "xml_node.hpp"
43 #include "xml_att_decl_node.hpp"
44 #include "xml_comment_node.hpp"
45 #include "xml_element.hpp"
46 #include "xml_text_node.hpp"
47 
49 #include <vector>
50 
52 #include "string_manipulation.hpp"
53 
54 xml_element* xml_child::add_child_element(const std::string& _name)
55 {
56  auto* new_el = new xml_element(_name);
57  xml_nodeRef new_ref(new_el);
58  child_list.push_back(new_ref);
59  return new_el;
60 }
61 
62 xml_element* xml_child::add_child_element(const xml_nodeRef& node)
63 {
64  child_list.push_back(node);
65  return GetPointer<xml_element>(node);
66 }
67 
68 xml_text_node* xml_child::add_child_text(const std::string& content)
69 {
70  auto* new_el = new xml_text_node(content);
71  if(!first_text)
72  {
73  first_text = new_el;
74  }
75  xml_nodeRef new_ref(new_el);
76  child_list.push_back(new_ref);
77  return new_el;
78 }
79 
80 xml_comment_node* xml_child::add_child_comment(const std::string& content)
81 {
82  auto* new_el = new xml_comment_node(content);
83  xml_nodeRef new_ref(new_el);
84  child_list.push_back(new_ref);
85  return new_el;
86 }
87 
89 {
90  auto* new_el = new xml_att_decl_node(_name);
91  xml_nodeRef new_ref(new_el);
92  child_list.push_back(new_ref);
93  return new_el;
94 }
95 
96 void xml_node::set_line(int _line)
97 {
98  line = _line;
99 }
100 
102 {
103  return line;
104 }
105 
106 const CustomSet<xml_nodeRef> xml_child::CGetDescendants(const std::string& path) const
107 {
109  std::vector<std::string> splitted = SplitString(path, "/");
110  CustomSet<xml_nodeRef> iteration_input_nodes, iteration_output_nodes;
111  for(const auto& child : get_children())
112  {
113  const auto* child_xml = GetPointer<const xml_element>(child);
114  if(not child_xml)
115  {
116  continue;
117  }
118  iteration_input_nodes.insert(child);
119  }
120  for(size_t level = 0; level < splitted.size(); level++)
121  {
122  const auto current_level_tag = splitted[level];
123  for(const auto& iteration_input_node : iteration_input_nodes)
124  {
125  if(iteration_input_node->get_name() == current_level_tag)
126  {
127  if(level == splitted.size() - 1)
128  {
129  ret.insert(iteration_input_node);
130  }
131  else
132  {
133  for(const auto& child : GetPointer<xml_child>(iteration_input_node)->get_children())
134  {
135  const auto* child_xml = GetPointer<const xml_element>(child);
136  if(not child_xml)
137  {
138  continue;
139  }
140  iteration_output_nodes.insert(child);
141  }
142  }
143  }
144  }
145  iteration_input_nodes = iteration_output_nodes;
146  }
147  return ret;
148 }
const std::vector< std::string > SplitString(const std::string &input, const std::string &separators)
Function which splits a string into tokens.
node_list child_list
Definition: xml_node.hpp:339
const CustomSet< xml_nodeRef > CGetDescendants(const std::string &path) const
Return the set of descendants with a specific path.
Definition: xml_node.cpp:106
xml_text_node * add_child_text(const std::string &content)
Append a new text node.
Definition: xml_node.cpp:68
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
Auxiliary methods for manipulating string.
xml_att_decl_node * add_child_attribute_declaration(const std::string &name)
Append a new attribute declaration node.
Definition: xml_node.cpp:88
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
xml_comment_node * add_child_comment(const std::string &content)
Append a new comment node.
Definition: xml_node.cpp:80
void set_line(int _line)
Set the line this node occurs in the XML file.
Definition: xml_node.cpp:96
int level
Definition: main.c:98
node_list const & get_children()
Obtain the list of child nodes.
Definition: xml_node.hpp:310
xml_element * add_child_element(const std::string &name)
Add a child element to this node.
Definition: xml_node.cpp:54

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