PandA-2024.02
xml_attribute.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_ATTRIBUTE_HPP
44 #define XML_ATTRIBUTE_HPP
45 
47 #include <string>
48 
50 #include "custom_map.hpp"
51 #include <list>
52 
54 #include "refcount.hpp"
55 #include <cassert>
56 
58 #include "xml_node.hpp"
59 
66 
67 class xml_attribute : public xml_node
68 {
69  std::string at_value;
70 
71  public:
73  explicit xml_attribute(const std::string& _name) : xml_node(_name)
74  {
75  }
76 
83  void print(std::ostream& os, bool, simple_indent*) const override
84  {
85  std::string escaped(at_value);
86  convert_unescaped(escaped);
88  std::string::size_type lPos = 0;
89  while((lPos = escaped.find('\n', lPos)) != std::string::npos)
90  {
91  escaped.replace(lPos++, 1, "\\n");
92  }
93  os << get_name() << "=\"" << escaped << "\"";
94  }
95 
100  std::string get_value() const
101  {
102  return at_value;
103  }
108  void set_value(const std::string& value)
109  {
110  at_value = value;
111  }
112 };
113 
115 {
116  using attribute_list = std::list<xml_attribute*>;
121  void print_attributes(std::ostream& os) const
122  {
123  auto it_end = a_list.end();
124  for(auto it = a_list.begin(); it != it_end; ++it)
125  {
126  os << " ";
127  (*it)->print(os, false, nullptr);
128  }
129  }
130 
137  xml_attribute* set_attribute(const std::string& name, const std::string& value)
138  {
139  assert("" != name);
140  if(a_map.find(name) != a_map.end())
141  {
142  xml_attribute* at = a_map.find(name)->second.get();
143  at->set_value(value);
144  return at;
145  }
146  else
147  {
148  auto* at = new xml_attribute(name);
149  xml_attributeRef at_ref(at);
150  a_list.push_back(at);
151  a_map[name] = at_ref;
152  at->set_value(value);
153  return at;
154  }
155  }
156 
161  xml_attribute* get_attribute(const std::string& name) const
162  {
163  if(a_map.find(name) == a_map.end())
164  {
165  return nullptr;
166  }
167  else
168  {
169  return a_map.find(name)->second.get();
170  }
171  }
175  /*void remove_attribute(const std::string& name)
176  {
177  if(a_map.find(name) != a_map.end())
178  {
179  xml_attribute * at = a_map.find(name)->second.get();
180  a_map.erase(name);
181  a_list.remove(at);
182  }
183  }*/
184 
189  {
190  return a_list;
191  }
192 
197  {
198  return a_list;
199  }
200 
204  bool has_attributes() const
205  {
206  return !a_list.empty();
207  }
208 
209  private:
211  std::map<std::string, xml_attributeRef> a_map;
212 };
213 
214 #endif
std::string get_value() const
Get the value of this attribute.
std::string at_value
std::list< xml_attribute * > attribute_list
bool has_attributes() const
attribute_list get_attributes()
Remove the attribute with this name.
xml_attribute(const std::string &_name)
constructor
static void convert_unescaped(std::string &ioString)
Convert unescaped characters.
Definition: xml_node.hpp:162
const attribute_list & get_attributes() const
Obtain the list of attributes for this element.
redefinition of map to manage ordered/unordered structures
std::string get_name() const
Get the name of this node.
Definition: xml_node.hpp:132
void print_attributes(std::ostream &os) const
Print the class.
xml_attribute * get_attribute(const std::string &name) const
Obtain the attribute with this name.
Template definition of refcount.
Very simple pretty printer functor.
std::map< std::string, xml_attributeRef > a_map
REF_FORWARD_DECL(xml_attribute)
void print(std::ostream &os, bool, simple_indent *) const override
Print the class.
xml_attribute * set_attribute(const std::string &name, const std::string &value)
Set the value of the attribute with this name, and optionally with this namespace.
attribute_list a_list
std::string name
name of the node
Definition: xml_node.hpp:74
void set_value(const std::string &value)
Set the value of this attribute.

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