cprover
xml.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_UTIL_XML_H
11 #define CPROVER_UTIL_XML_H
12 
13 #include <list>
14 #include <map>
15 #include <string>
16 #include <iosfwd>
17 
18 class xmlt
19 {
20 public:
21  xmlt()
22  { }
23 
24  explicit xmlt(const std::string &_name):name(_name)
25  { }
26 
27  typedef std::list<xmlt> elementst;
28  typedef std::map<std::string, std::string> attributest;
29 
30  xmlt(std::string &&_name, attributest &&_attributes, elementst &&_elements)
31  : name(std::move(_name)),
32  attributes(std::move(_attributes)),
33  elements(std::move(_elements))
34  {
35  }
36 
37  std::string name, data;
38 
41 
42  elementst::const_iterator find(const std::string &key) const;
43  elementst::iterator find(const std::string &key);
44 
45  void set_attribute(
46  const std::string &attribute,
47  unsigned value);
48 
49  void set_attribute(
50  const std::string &attribute,
51  unsigned long value);
52 
53  void set_attribute(
54  const std::string &attribute,
55  unsigned long long value);
56 
57  void set_attribute(
58  const std::string &attribute,
59  const std::string &value);
60 
61  std::string get_attribute(
62  const std::string &attribute) const
63  {
64  attributest::const_iterator i=attributes.find(attribute);
65  if(i!=attributes.end())
66  return i->second;
67  return "";
68  }
69 
71  const std::string &attribute,
72  bool value)
73  {
74  set_attribute(attribute, value?"true":"false");
75  }
76 
77  bool get_attribute_bool(const std::string &attribute) const
78  {
79  attributest::const_iterator i=attributes.find(attribute);
80  if(i!=attributes.end())
81  return (i->second=="true");
82  return false;
83  }
84 
85  std::string get_element(const std::string &element) const
86  {
87  elementst::const_iterator i=find(element);
88  if(i!=elements.end())
89  return i->data;
90  return "";
91  }
92 
93  xmlt &new_element(const std::string &key)
94  {
95  elements.push_back(xmlt());
96  elements.back().name = key;
97  return elements.back();
98  }
99 
101  {
102  elements.push_back(xml);
103  return elements.back();
104  }
105 
107  {
108  elements.push_back(xmlt());
109  return elements.back();
110  }
111 
112  void swap(xmlt &xml);
113  void clear();
114 
115  void output(
116  std::ostream &out,
117  unsigned indent=0) const;
118 
119  static void escape(const std::string &s, std::ostream &out);
120  static std::string unescape(const std::string &s);
121 
122  static void escape_attribute(const std::string &s, std::ostream &out);
123 
124 protected:
125  static void do_indent(
126  std::ostream &out,
127  unsigned indent);
128 };
129 
130 inline std::ostream &operator<<(
131  std::ostream &out,
132  const xmlt &xml)
133 {
134  xml.output(out);
135  return out;
136 }
137 
138 #endif // CPROVER_UTIL_XML_H
xmlt::elements
elementst elements
Definition: xml.h:40
xmlt::get_attribute_bool
bool get_attribute_bool(const std::string &attribute) const
Definition: xml.h:77
xmlt::new_element
xmlt & new_element(const xmlt &xml)
Definition: xml.h:100
xmlt::xmlt
xmlt(std::string &&_name, attributest &&_attributes, elementst &&_elements)
Definition: xml.h:30
xmlt::escape
static void escape(const std::string &s, std::ostream &out)
escaping for XML elements
Definition: xml.cpp:78
xmlt::attributest
std::map< std::string, std::string > attributest
Definition: xml.h:28
xmlt::xmlt
xmlt()
Definition: xml.h:21
xmlt::output
void output(std::ostream &out, unsigned indent=0) const
Definition: xml.cpp:32
xml
xmlt xml(const irep_idt &property_id, const property_infot &property_info)
Definition: properties.cpp:105
xmlt::name
std::string name
Definition: xml.h:37
xmlt::elementst
std::list< xmlt > elementst
Definition: xml.h:27
xmlt::do_indent
static void do_indent(std::ostream &out, unsigned indent)
Definition: xml.cpp:147
xmlt::escape_attribute
static void escape_attribute(const std::string &s, std::ostream &out)
escaping for XML attributes, assuming that double quotes " are used consistently, not single quotes
Definition: xml.cpp:115
xmlt::new_element
xmlt & new_element()
Definition: xml.h:106
xmlt::clear
void clear()
Definition: xml.cpp:16
xmlt
Definition: xml.h:19
xmlt::get_attribute
std::string get_attribute(const std::string &attribute) const
Definition: xml.h:61
xmlt::get_element
std::string get_element(const std::string &element) const
Definition: xml.h:85
xmlt::find
elementst::const_iterator find(const std::string &key) const
Definition: xml.cpp:152
xmlt::set_attribute
void set_attribute(const std::string &attribute, unsigned value)
Definition: xml.cpp:174
xmlt::set_attribute_bool
void set_attribute_bool(const std::string &attribute, bool value)
Definition: xml.h:70
xmlt::swap
void swap(xmlt &xml)
Definition: xml.cpp:24
xmlt::attributes
attributest attributes
Definition: xml.h:39
xmlt::data
std::string data
Definition: xml.h:37
xmlt::unescape
static std::string unescape(const std::string &s)
takes a string and unescapes any xml style escaped symbols
Definition: xml.cpp:213
xmlt::new_element
xmlt & new_element(const std::string &key)
Definition: xml.h:93
operator<<
std::ostream & operator<<(std::ostream &out, const xmlt &xml)
Definition: xml.h:130
xmlt::xmlt
xmlt(const std::string &_name)
Definition: xml.h:24