libzypp 17.37.17
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-core/base/Easy.h>
25
27namespace 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 using Pair = std::pair<std::string, std::string>;
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 };
56
57
84 struct Node
85 {
87 using Attr = NodeAttr;
88
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), { std::move(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, { std::move(attr_r) } )
110 {}
111
114 {
115 if ( isComment() )
116 _out.get() << "-->";
117 else
118 {
119 if ( _hasContent )
120 _out.get() << "</" << _name << ">";
121 else
122 _out.get() << "/>";
123 }
124 }
125
128
132 Node & addAttr( const std::initializer_list<Attr> & attrs_r = {} )
133 {
134 if ( _hasContent )
135 throw HasContentException();
136 printAttr( attrs_r );
137 return *this;
138 }
139
141 Node & addAttr( const Attr & attr_r )
142 { return addAttr( { attr_r } ); }
143
144
146 std::ostream & operator*()
147 {
148 if ( ! _hasContent )
149 {
150 _hasContent = true;
151 if ( isComment() )
152 _out.get() << "|";
153 else
154 _out.get() << ">";
155 }
156 return _out;
157 }
158
159 private:
160 void printStart( const std::initializer_list<Attr> & attrs_r )
161 {
162 if ( _name.empty() || _name[0] == '!' )
163 {
164 _out.get() << "<!--" << _name;
165 _name.clear(); // a comment
166 }
167 else
168 _out.get() << "<" << _name;
169
170 printAttr( attrs_r );
171
172 if ( !isComment() && _hasContent )
173 _out.get() << ">";
174 }
175
176 void printAttr( const std::initializer_list<Attr> & attrs_r )
177 {
178 for ( const auto & pair : attrs_r )
179 _out.get() << " " << pair.first << "=\"" << xml::escape( pair.second ) << "\"";
180 }
181
182 bool isComment() const
183 { return _name.empty(); }
184
185 private:
186 // use a reference wrapper, otherwise the defaulted move operator is deleted
187 std::reference_wrapper<std::ostream> _out;
188 std::string _name;
190 };
191
192
198 inline std::ostream & node( std::ostream & out_r, const std::string & name_r, const std::initializer_list<Node::Attr> & attrs_r = {} )
199 {
200 Node( out_r, name_r, Node::optionalContent, attrs_r );
201 return out_r;
202 }
203
204 inline std::ostream & node( std::ostream & out_r, const std::string & name_r, Node::Attr attr_r )
205 { return node( out_r, name_r, { std::move(attr_r) } ); }
206
207 } // namespace xmlout
209
212
213 template <class Tp>
214 inline std::ostream & dumpAsXmlOn( std::ostream & str, const Tp & obj, const std::string & name_r )
215 {
217 const std::string & content( asString( obj ) );
218 if ( ! content.empty() ) *guard << xml::escape( content );
219 return str;
220 }
221
222 //
223} // namespace zypp
225#endif // ZYPP_BASE_XML_H
Definition Arch.h:364
String related utilities and Regular expression matching.
ZYPP_API detail::EscapedString escape(const std::string &in_r)
Escape xml special charaters (& -> &; from IoBind library).
Definition XmlEscape.h:51
std::string unescape(const std::string &in_r)
Unescape xml special charaters (& -> &; from IoBind library)
Definition XmlEscape.cc:113
std::ostream & node(std::ostream &out_r, const std::string &name_r, Node::Attr attr_r)
Definition Xml.h:204
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & dumpAsXmlOn(std::ostream &str, const Repository &obj)
std::string asString(const Patch::Category &obj)
Definition Patch.cc:122
(Key, Value) string pair of XML node attributes
Definition Xml.h:44
std::pair< std::string, std::string > Pair
Definition Xml.h:45
NodeAttr(std::string key_r, const Tp &val_r)
Definition Xml.h:48
std::string asXmlNodeAttr(const Tp &val_r)
NODE ATTRIBUTE representation of types [asString].
Definition Xml.h:37
NodeAttr(std::string key_r, std::string val_r)
Definition Xml.h:52
Exception type thrown if attributes are added to a closed start node.
Definition Xml.h:127
RAII writing a nodes start/end tag.
Definition Xml.h:85
void printStart(const std::initializer_list< Attr > &attrs_r)
Definition Xml.h:160
Node(std::ostream &out_r, std::string name_r, Attr attr_r)
Convenience ctor for one attribute pair.
Definition Xml.h:98
std::ostream & operator*()
Return the output stream.
Definition Xml.h:146
Node & addAttr(const Attr &attr_r)
Definition Xml.h:141
void printAttr(const std::initializer_list< Attr > &attrs_r)
Definition Xml.h:176
bool isComment() const
Definition Xml.h:182
Node & addAttr(const std::initializer_list< Attr > &attrs_r={})
Add additional attributes (requires OptionalContentType)
Definition Xml.h:132
std::reference_wrapper< std::ostream > _out
Definition Xml.h:187
std::string _name
Definition Xml.h:188
bool _hasContent
Definition Xml.h:189
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()
Dtor wrting end tag.
Definition Xml.h:113
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, const std::initializer_list< Node::Attr > &attrs_r={})
Write a leaf node without PCDATA.
Definition Xml.h:198
NodeAttr Attr
Definition Xml.h:87
static constexpr OptionalContentType optionalContent
Definition Xml.h:90
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