CBMC
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 structured_datat;
19 
20 class xmlt
21 {
22 public:
23  xmlt()
24  { }
25 
26  explicit xmlt(const std::string &_name):name(_name)
27  { }
28 
29  typedef std::list<xmlt> elementst;
30  typedef std::map<std::string, std::string> attributest;
31 
32  xmlt(std::string &&_name, attributest &&_attributes, elementst &&_elements)
33  : name(std::move(_name)),
34  attributes(std::move(_attributes)),
35  elements(std::move(_elements))
36  {
37  }
38 
39  std::string name, data;
40 
43 
44  elementst::const_iterator find(const std::string &key) const;
45  elementst::iterator find(const std::string &key);
46 
47  void set_attribute(
48  const std::string &attribute,
49  unsigned value);
50 
51  void set_attribute(
52  const std::string &attribute,
53  unsigned long value);
54 
55  void set_attribute(
56  const std::string &attribute,
57  unsigned long long value);
58 
59  void set_attribute(
60  const std::string &attribute,
61  const std::string &value);
62 
63  std::string get_attribute(
64  const std::string &attribute) const
65  {
66  attributest::const_iterator i=attributes.find(attribute);
67  if(i!=attributes.end())
68  return i->second;
69  return "";
70  }
71 
73  const std::string &attribute,
74  bool value)
75  {
76  set_attribute(attribute, value?"true":"false");
77  }
78 
79  bool get_attribute_bool(const std::string &attribute) const
80  {
81  attributest::const_iterator i=attributes.find(attribute);
82  if(i!=attributes.end())
83  return (i->second=="true");
84  return false;
85  }
86 
87  std::string get_element(const std::string &element) const
88  {
89  elementst::const_iterator i=find(element);
90  if(i!=elements.end())
91  return i->data;
92  return "";
93  }
94 
95  xmlt &new_element(const std::string &key)
96  {
97  elements.push_back(xmlt());
98  elements.back().name = key;
99  return elements.back();
100  }
101 
103  {
104  elements.push_back(xml);
105  return elements.back();
106  }
107 
109  {
110  elements.push_back(xmlt());
111  return elements.back();
112  }
113 
114  void swap(xmlt &xml);
115  void clear();
116 
117  void output(
118  std::ostream &out,
119  unsigned indent=0) const;
120 
121  static void escape(const std::string &s, std::ostream &out);
122  static std::string unescape(const std::string &s);
123 
124  static void escape_attribute(const std::string &s, std::ostream &out);
125 
131  static bool is_printable_xml(const std::string &s);
132 
133 protected:
134  static void do_indent(
135  std::ostream &out,
136  unsigned indent);
137 };
138 
139 inline std::ostream &operator<<(
140  std::ostream &out,
141  const xmlt &xml)
142 {
143  xml.output(out);
144  return out;
145 }
146 
147 bool operator==(const xmlt &a, const xmlt &b);
148 bool operator!=(const xmlt &a, const xmlt &b);
149 
170 xmlt to_xml(const structured_datat &data);
171 
172 #endif // CPROVER_UTIL_XML_H
A way of representing nested key/value data.
Definition: xml.h:21
bool get_attribute_bool(const std::string &attribute) const
Definition: xml.h:79
xmlt(const std::string &_name)
Definition: xml.h:26
std::string get_attribute(const std::string &attribute) const
Definition: xml.h:63
xmlt(std::string &&_name, attributest &&_attributes, elementst &&_elements)
Definition: xml.h:32
std::map< std::string, std::string > attributest
Definition: xml.h:30
std::list< xmlt > elementst
Definition: xml.h:29
static std::string unescape(const std::string &s)
takes a string and unescapes any xml style escaped symbols
Definition: xml.cpp:237
void set_attribute_bool(const std::string &attribute, bool value)
Definition: xml.h:72
xmlt & new_element(const xmlt &xml)
Definition: xml.h:102
void set_attribute(const std::string &attribute, unsigned value)
Definition: xml.cpp:198
void swap(xmlt &xml)
Definition: xml.cpp:25
attributest attributes
Definition: xml.h:41
void clear()
Definition: xml.cpp:17
xmlt & new_element(const std::string &key)
Definition: xml.h:95
elementst elements
Definition: xml.h:42
std::string get_element(const std::string &element) const
Definition: xml.h:87
std::string data
Definition: xml.h:39
static bool is_printable_xml(const std::string &s)
Determine whether s does not contain any characters that cannot be escaped in XML 1....
Definition: xml.cpp:160
elementst::const_iterator find(const std::string &key) const
Definition: xml.cpp:176
void output(std::ostream &out, unsigned indent=0) const
Definition: xml.cpp:33
static void escape(const std::string &s, std::ostream &out)
escaping for XML elements
Definition: xml.cpp:79
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:121
std::string name
Definition: xml.h:39
xmlt()
Definition: xml.h:23
xmlt & new_element()
Definition: xml.h:108
static void do_indent(std::ostream &out, unsigned indent)
Definition: xml.cpp:171
xmlt xml(const irep_idt &property_id, const property_infot &property_info)
Definition: properties.cpp:110
xmlt to_xml(const structured_datat &data)
Convert the structured_datat into an xml object.
Definition: xml.cpp:303
bool operator==(const xmlt &a, const xmlt &b)
Definition: xml.cpp:275
std::ostream & operator<<(std::ostream &out, const xmlt &xml)
Definition: xml.h:139
bool operator!=(const xmlt &a, const xmlt &b)
Definition: xml.cpp:280