libzypp  15.28.6
Xml.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_BASE_XML_H
13 #define ZYPP_BASE_XML_H
14 
15 #include <iosfwd>
16 #include <string>
17 #include <vector>
18 #include <list>
19 #include <set>
20 #include <map>
21 
22 #include "zypp/base/Easy.h"
23 #include "zypp/base/String.h"
25 
27 namespace zypp
28 {
30  namespace xmlout
31  {
32  using xml::escape;
33  using xml::unescape;
34 
36  template <class Tp>
37  std::string asXmlNodeAttr( const Tp & val_r )
38  { return asString( val_r ); }
39 
43  struct NodeAttr : public std::pair<std::string,std::string>
44  {
45  typedef std::pair<std::string,std::string> Pair;
46 
47  template <typename Tp>
48  NodeAttr( std::string key_r, const Tp & val_r )
49  : Pair( std::move(key_r), asXmlNodeAttr(val_r) )
50  {}
51 
52  NodeAttr( std::string key_r, std::string val_r )
53  : Pair( std::move(key_r), std::move(val_r) )
54  {}
55  };
57 
84  struct Node
85  {
87  typedef NodeAttr Attr;
88 
89  struct OptionalContentType {};
91 
93  Node( std::ostream & out_r, std::string name_r, const std::initializer_list<Attr> & attrs_r = {} )
94  : _out( out_r ), _name( std::move(name_r) ), _hasContent( true )
95  { printStart( attrs_r ); }
96 
98  Node( std::ostream & out_r, std::string name_r, Attr attr_r )
99  : Node( out_r, std::move(name_r), { attr_r } )
100  {}
101 
103  Node( std::ostream & out_r, std::string name_r, OptionalContentType, const std::initializer_list<Attr> & attrs_r = {} )
104  : _out( out_r ), _name( std::move(name_r) ), _hasContent( false )
105  { printStart( attrs_r ); }
106 
108  Node( std::ostream & out_r, std::string name_r, OptionalContentType, Attr attr_r )
109  : Node( out_r, std::move(name_r), optionalContent, { attr_r } )
110  {}
111 
113  ~Node()
114  {
115  if ( _name.empty() )
116  _out << "-->";
117  else
118  {
119  if ( _hasContent )
120  _out << "</" << _name << ">";
121  else
122  _out << "/>";
123  }
124  }
125 
127  std::ostream & operator*()
128  {
129  if ( ! _hasContent )
130  {
131  _hasContent = true;
132  if ( _name.empty() )
133  _out << "|";
134  else
135  _out << ">";
136  }
137  return _out;
138  }
139 
140  private:
141  void printStart( const std::initializer_list<Attr> & attrs_r )
142  {
143  if ( _name.empty() || _name[0] == '!' )
144  {
145  _out << "<!--" << _name;
146  _name.clear();
147  }
148  else
149  _out << "<" << _name;
150 
151  for ( const auto & pair : attrs_r )
152  _out << " " << pair.first << "=\"" << xml::escape( pair.second ) << "\"";
153 
154  if ( ! _name.empty() && _hasContent )
155  _out << ">";
156  }
157  private:
158  std::ostream & _out;
159  std::string _name;
161  };
163 
169  inline std::ostream & node( std::ostream & out_r, const std::string & name_r, const std::initializer_list<Node::Attr> & attrs_r = {} )
170  {
171  Node( out_r, name_r, Node::optionalContent, attrs_r );
172  return out_r;
173  }
175  inline std::ostream & node( std::ostream & out_r, const std::string & name_r, Node::Attr attr_r )
176  { return node( out_r, name_r, { attr_r } ); }
177 
178  } // namespace xmlout
180 
183 
184  template <class Tp>
185  inline std::ostream & dumpAsXmlOn( std::ostream & str, const Tp & obj, const std::string & name_r )
186  {
187  xmlout::Node guard( str, name_r, xmlout::Node::optionalContent );
188  const std::string & content( asString( obj ) );
189  if ( ! content.empty() ) *guard << xml::escape( content );
190  return str;
191  }
193  //
194 } // namespace zypp
196 #endif // ZYPP_BASE_XML_H
std::string asString(const Patch::Category &obj)
Definition: Patch.cc:117
Node(std::ostream &out_r, std::string name_r, OptionalContentType, const std::initializer_list< Attr > &attrs_r={})
Optional content ctor taking nodename and attribute list.
Definition: Xml.h:103
std::ostream & node(std::ostream &out_r, const std::string &name_r, Node::Attr attr_r)
Definition: Xml.h:175
RAII writing a nodes start/end tag.
Definition: Xml.h:84
std::string unescape(const std::string &in_r)
Unescape xml special charaters (& -> &; from IoBind library)
Definition: XmlEscape.cc:113
(Key, Value) string pair of XML node attributes
Definition: Xml.h:43
std::pair< std::string, std::string > Pair
Definition: Xml.h:45
bool _hasContent
Definition: Xml.h:160
static constexpr OptionalContentType optionalContent
Definition: Xml.h:90
NodeAttr(std::string key_r, std::string val_r)
Definition: Xml.h:52
NodeAttr Attr
Definition: Xml.h:87
NodeAttr(std::string key_r, const Tp &val_r)
Definition: Xml.h:48
std::ostream & node(std::ostream &out_r, const std::string &name_r, const std::initializer_list< Node::Attr > &attrs_r={})
Definition: Xml.h:169
std::string _name
Definition: Xml.h:110
Node(std::ostream &out_r, std::string name_r, OptionalContentType, Attr attr_r)
Optional content Convenience ctor for one attribute pair.
Definition: Xml.h:108
Node(std::ostream &out_r, std::string name_r, const std::initializer_list< Attr > &attrs_r={})
Ctor taking nodename and attribute list.
Definition: Xml.h:93
std::string asXmlNodeAttr(const Tp &val_r)
Definition: Xml.h:37
Node(std::ostream &out_r, std::string name_r, Attr attr_r)
Convenience ctor for one attribute pair.
Definition: Xml.h:98
std::ostream & dumpAsXmlOn(std::ostream &str, const Tp &obj, const std::string &name_r)
Definition: Xml.h:185
detail::EscapedString escape(const std::string &in_r)
Escape xml special charaters (& -> &; from IoBind library).
Definition: XmlEscape.h:51