libzypp 17.31.23
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>
23#include <zypp-core/base/String.h>
24#include <zypp-core/parser/xml/XmlEscape.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 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 {};
90 static constexpr OptionalContentType optionalContent = 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 ( isComment() )
116 _out << "-->";
117 else
118 {
119 if ( _hasContent )
120 _out << "</" << _name << ">";
121 else
122 _out << "/>";
123 }
124 }
125
127 struct HasContentException{};
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 << "|";
153 else
154 _out << ">";
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 << "<!--" << _name;
165 _name.clear(); // a comment
166 }
167 else
168 _out << "<" << _name;
169
170 printAttr( attrs_r );
171
172 if ( !isComment() && _hasContent )
173 _out << ">";
174 }
175
176 void printAttr( const std::initializer_list<Attr> & attrs_r )
177 {
178 for ( const auto & pair : attrs_r )
179 _out << " " << pair.first << "=\"" << xml::escape( pair.second ) << "\"";
180 }
181
182 bool isComment() const
183 { return _name.empty(); }
184
185 private:
186 std::ostream & _out;
187 std::string _name;
188 bool _hasContent;
189 };
191
197 inline std::ostream & node( std::ostream & out_r, const std::string & name_r, const std::initializer_list<Node::Attr> & attrs_r = {} )
198 {
199 Node( out_r, name_r, Node::optionalContent, attrs_r );
200 return out_r;
201 }
203 inline std::ostream & node( std::ostream & out_r, const std::string & name_r, Node::Attr attr_r )
204 { return node( out_r, name_r, { attr_r } ); }
205
206 } // namespace xmlout
208
211
212 template <class Tp>
213 inline std::ostream & dumpAsXmlOn( std::ostream & str, const Tp & obj, const std::string & name_r )
214 {
215 xmlout::Node guard( str, name_r, xmlout::Node::optionalContent );
216 const std::string & content( asString( obj ) );
217 if ( ! content.empty() ) *guard << xml::escape( content );
218 return str;
219 }
221 //
222} // namespace zypp
224#endif // ZYPP_BASE_XML_H
Definition: Arch.h:361
String related utilities and Regular expression matching.
std::string asString(TInt val, char zero='0', char one='1')
For printing bits.
Definition: Bit.h:57
std::string unescape(const std::string &in_r)
Unescape xml special charaters (& -> &; from IoBind library)
Definition: XmlEscape.cc:113
detail::EscapedString escape(const std::string &in_r)
Escape xml special charaters (& -> &; from IoBind library).
Definition: XmlEscape.h:51
std::ostream & node(std::ostream &out_r, const std::string &name_r, Node::Attr attr_r)
Definition: Xml.h:203
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
std::ostream & dumpAsXmlOn(std::ostream &str, const Repository &obj)
Definition: Repository.cc:406
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
void printStart(const std::initializer_list< Attr > &attrs_r)
Definition: Xml.h:160
std::ostream & operator*()
Return the output stream.
Definition: Xml.h:146
NodeAttr Attr
Definition: Xml.h:87
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::string _name
Definition: Xml.h:187
std::ostream & _out
Definition: Xml.h:186
bool _hasContent
Definition: Xml.h:188
~Node()
Dtor wrting end tag.
Definition: Xml.h:113
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
#define NON_COPYABLE_BUT_MOVE(CLASS)
Delete copy ctor and copy assign but enable default move.
Definition: Easy.h:79