libzypp: zypp-core/base/Xml.h Source File

libzypp 17.38.5

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 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 {
86 NON_COPYABLE_BUT_MOVE( Node );
87 using Attr = NodeAttr;
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), { 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
113 ~Node()
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
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.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;
189 bool _hasContent;
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 {
216 xmlout::Node guard( str, name_r, xmlout::Node::optionalContent );
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
Easy.h
String.h
XmlEscape.h
std
Definition ansi.h:855
str
String related utilities and Regular expression matching.
zypp::xml::escape
ZYPP_API detail::EscapedString escape(const std::string &in_r)
Escape xml special charaters (& -> &; from IoBind library).
Definition XmlEscape.h:51
zypp::xml::unescape
std::string unescape(const std::string &in_r)
Unescape xml special charaters (& -> &; from IoBind library).
Definition XmlEscape.cc:113
zypp::xmlout
Definition Xml.cc:17
zypp::xmlout::node
std::ostream & node(std::ostream &out_r, const std::string &name_r, Node::Attr attr_r)
Definition Xml.h:204
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::dumpAsXmlOn
std::ostream & dumpAsXmlOn(std::ostream &str, const Repository &obj)
Definition Repository.cc:409
zypp::asString
std::string asString(const Patch::Category &obj)
Definition Patch.cc:122
zypp::xmlout::NodeAttr
(Key, Value) string pair of XML node attributes
Definition Xml.h:44
zypp::xmlout::NodeAttr::Pair
std::pair< std::string, std::string > Pair
Definition Xml.h:45
zypp::xmlout::NodeAttr::NodeAttr
NodeAttr(std::string key_r, const Tp &val_r)
Definition Xml.h:48
zypp::xmlout::NodeAttr::asXmlNodeAttr
std::string asXmlNodeAttr(const Tp &val_r)
NODE ATTRIBUTE representation of types [asString].
Definition Xml.h:37
zypp::xmlout::NodeAttr::NodeAttr
NodeAttr(std::string key_r, std::string val_r)
Definition Xml.h:52
zypp::xmlout::Node::HasContentException
Exception type thrown if attributes are added to a closed start node.
Definition Xml.h:127
zypp::xmlout::Node::OptionalContentType
Ctor arg type.
Definition Xml.h:89
zypp::xmlout::Node
RAII writing a nodes start/end tag.
Definition Xml.h:85
zypp::xmlout::Node::printStart
void printStart(const std::initializer_list< Attr > &attrs_r)
Definition Xml.h:160
zypp::xmlout::Node::Node
Node(std::ostream &out_r, std::string name_r, Attr attr_r)
Convenience ctor for one attribute pair.
Definition Xml.h:98
zypp::xmlout::Node::operator*
std::ostream & operator*()
Return the output stream.
Definition Xml.h:146
zypp::xmlout::Node::addAttr
Node & addAttr(const Attr &attr_r)
Definition Xml.h:141
zypp::xmlout::Node::printAttr
void printAttr(const std::initializer_list< Attr > &attrs_r)
Definition Xml.h:176
zypp::xmlout::Node::isComment
bool isComment() const
Definition Xml.h:182
zypp::xmlout::Node::addAttr
Node & addAttr(const std::initializer_list< Attr > &attrs_r={})
Add additional attributes (requires OptionalContentType).
Definition Xml.h:132
zypp::xmlout::Node::_out
std::reference_wrapper< std::ostream > _out
Definition Xml.h:187
zypp::xmlout::Node::_name
std::string _name
Definition Xml.h:188
zypp::xmlout::Node::_hasContent
bool _hasContent
Definition Xml.h:189
zypp::xmlout::Node::Node
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
zypp::xmlout::Node::NON_COPYABLE_BUT_MOVE
NON_COPYABLE_BUT_MOVE(Node)
zypp::xmlout::Node::~Node
~Node()
Dtor wrting end tag.
Definition Xml.h:113
zypp::xmlout::Node::Node
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
zypp::xmlout::Node::node
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
zypp::xmlout::Node::Attr
NodeAttr Attr
Definition Xml.h:87
zypp::xmlout::Node::optionalContent
static constexpr OptionalContentType optionalContent
Definition Xml.h:90
zypp::xmlout::Node::Node
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