1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/Xml.h
12 #ifndef ZYPP_BASE_XML_H
13 #define ZYPP_BASE_XML_H
22 #include "zypp/base/Easy.h"
23 #include "zypp/base/String.h"
24 #include "zypp/parser/xml/XmlEscape.h"
26 ///////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////
35 /** \relates NodeAttr NODE ATTRIBUTE representation of types [str::asString] */
37 std::string asXmlNodeAttr( const _Tp & val_r )
38 { return str::asString( val_r ); }
40 ///////////////////////////////////////////////////////////////////
42 /// \brief (Key, Value) string pair of XML node attributes
43 struct NodeAttr : public std::pair<std::string,std::string>
45 typedef std::pair<std::string,std::string> Pair;
47 template <typename _Type>
48 NodeAttr( std::string key_r, const _Type & val_r )
49 : Pair( std::move(key_r), asXmlNodeAttr(val_r) )
52 NodeAttr( std::string key_r, std::string val_r )
53 : Pair( std::move(key_r), std::move(val_r) )
56 ///////////////////////////////////////////////////////////////////
58 ///////////////////////////////////////////////////////////////////
60 /// \brief RAII writing a nodes start/end tag
63 /// Node node( std::cout, "node", { "attr", "val" } ); // <node attr="val">
64 /// *node << "write nodes body...."
69 NON_COPYABLE_BUT_MOVE( Node );
71 /** Ctor taking nodename and attribute list */
72 Node( std::ostream & out_r, std::string name_r, const std::initializer_list<NodeAttr> & attrs_r = {} )
73 : _out( out_r ), _name( std::move(name_r) )
75 if ( ! _name.empty() )
78 for ( const auto & pair : attrs_r )
79 _out << " " << pair.first << "=\"" << xml::escape( pair.second ) << "\"";
84 /** Convenience ctor for one attribute pair */
85 Node( std::ostream & out_r, std::string name_r, NodeAttr attr_r )
86 : Node( out_r, std::move(name_r), { attr_r } )
89 /** Dtor wrting end tag */
91 { _out << "</" << _name << ">"; }
93 /** Return the output stream */
94 std::ostream & operator*() { return _out; }
100 ///////////////////////////////////////////////////////////////////
102 /** \relates Node Write a leaf node without PCDATA
107 void node( std::ostream & out_r, const std::string & name_r, const std::initializer_list<NodeAttr> & attrs_r = {} )
109 out_r << "<" << name_r;
110 for ( const auto & pair : attrs_r )
111 out_r << " " << pair.first << "=\"" << xml::escape( pair.second ) << "\"";
114 /** \overload for one attribute pair */
115 void node( const std::string & name_r, NodeAttr attr_r )
116 { node( name_r, { attr_r } ); }
118 } // namespace xmlout
119 ///////////////////////////////////////////////////////////////////
121 ///////////////////////////////////////////////////////////////////
122 #endif // ZYPP_BASE_XML_H