libzypp  13.10.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 str::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 _Type>
48  NodeAttr( std::string key_r, const _Type & 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 
82  struct Node
83  {
85  typedef NodeAttr Attr;
86 
87  struct OptionalContentType {};
89 
91  Node( std::ostream & out_r, std::string name_r, const std::initializer_list<Attr> & attrs_r = {} )
92  : _out( out_r ), _name( std::move(name_r) ), _hasContent( true )
93  { printStart( attrs_r ); }
94 
96  Node( std::ostream & out_r, std::string name_r, Attr attr_r )
97  : Node( out_r, std::move(name_r), { attr_r } )
98  {}
99 
101  Node( std::ostream & out_r, std::string name_r, OptionalContentType, const std::initializer_list<Attr> & attrs_r = {} )
102  : _out( out_r ), _name( std::move(name_r) ), _hasContent( false )
103  { printStart( attrs_r ); }
104 
106  Node( std::ostream & out_r, std::string name_r, OptionalContentType, Attr attr_r )
107  : Node( out_r, std::move(name_r), optionalContent, { attr_r } )
108  {}
109 
111  ~Node()
112  {
113  if ( ! _name.empty() )
114  {
115  if ( _hasContent )
116  _out << "</" << _name << ">";
117  else
118  _out << "/>";
119  }
120  }
121 
123  std::ostream & operator*()
124  {
125  if ( ! _hasContent )
126  {
127  _hasContent = true;
128  if ( ! _name.empty() )
129  _out << ">";
130  }
131  return _out;
132  }
133 
134  private:
135  void printStart( const std::initializer_list<Attr> & attrs_r )
136  {
137  if ( ! _name.empty() )
138  {
139  _out << "<" << _name;
140  for ( const auto & pair : attrs_r )
141  _out << " " << pair.first << "=\"" << xml::escape( pair.second ) << "\"";
142  if ( _hasContent )
143  _out << ">";
144  }
145  }
146  private:
147  std::ostream & _out;
148  std::string _name;
150  };
152 
158  inline std::ostream & node( std::ostream & out_r, const std::string & name_r, const std::initializer_list<Node::Attr> & attrs_r = {} )
159  {
160  Node( out_r, name_r, Node::optionalContent, attrs_r );
161  return out_r;
162  }
164  inline std::ostream & node( std::ostream & out_r, const std::string & name_r, Node::Attr attr_r )
165  { return node( out_r, name_r, { attr_r } ); }
166 
167  } // namespace xmlout
169 } // namespace zypp
171 #endif // ZYPP_BASE_XML_H
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:101
std::ostream & node(std::ostream &out_r, const std::string &name_r, Node::Attr attr_r)
Definition: Xml.h:164
RAII writing a nodes start/end tag.
Definition: Xml.h:82
std::string asXmlNodeAttr(const _Tp &val_r)
Definition: Xml.h:37
std::string unescape(const std::string &in_r)
Unescape xml special charaters (&amp; -&gt; &amp;; from IoBind library)
Definition: XmlEscape.cc:109
(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:149
std::string escape(const std::string &in_r)
Escape xml special charaters (&amp; -&gt; &amp;; from IoBind library).
Definition: XmlEscape.cc:106
static constexpr OptionalContentType optionalContent
Definition: Xml.h:88
NodeAttr(std::string key_r, std::string val_r)
Definition: Xml.h:52
NodeAttr Attr
Definition: Xml.h:85
NodeAttr(std::string key_r, const _Type &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:158
std::string _name
Definition: Xml.h:108
Node(std::ostream &out_r, std::string name_r, OptionalContentType, Attr attr_r)
Optional content Convenience ctor for one attribute pair.
Definition: Xml.h:106
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:91
std::string asString(const std::string &t)
Global asString() that works with std::string too.
Definition: String.h:125
Node(std::ostream &out_r, std::string name_r, Attr attr_r)
Convenience ctor for one attribute pair.
Definition: Xml.h:96