From 3f9e46a0cd99c148a4b45b0447207cee7b098786 Mon Sep 17 00:00:00 2001 From: Michael Andres Date: Mon, 27 Jan 2014 17:44:01 +0100 Subject: [PATCH] Use zypp:xmlout in classes providing xml output. --- src/output/Out.h | 91 +++++++++----------------------------------------------- 1 file changed, 14 insertions(+), 77 deletions(-) diff --git a/src/output/Out.h b/src/output/Out.h index c7d5282..7278d8a 100644 --- a/src/output/Out.h +++ b/src/output/Out.h @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include #include @@ -27,28 +27,6 @@ class Zypper; /////////////////////////////////////////////////////////////////// namespace out { - /** \relates XmlNodeAttrFormater XML NODE ATTRIBUTE representation of types [str::asString] */ - template - std::string asXmlNodeAttr( const _Tp & val_r ) - { using str::asString; return asString( val_r ); } - - /////////////////////////////////////////////////////////////////// - /// \class XmlNodeAttrFormater - /// \brief Default representation of types as XmlNodeAttr [asXmlNodeAttr] - /////////////////////////////////////////////////////////////////// - struct XmlNodeAttrFormater - { - template - std::string operator()( const _Tp & val_r ) const - { return asXmlNodeAttr( val_r ); } - }; - -} // namespace out -/////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////// -namespace out -{ /** \relates ListFormater NORMAL representation of types in lists [no default] */ template std::string asListElement( const _Tp & val_r ); @@ -305,7 +283,6 @@ protected: /// \brief Convenience base class storing the back reference to Out. struct ParentOut { - NON_COPYABLE_BUT_MOVE( ParentOut ); ParentOut( Out & out_r ) : _out( out_r ) {} Out & out() { return _out; } private: @@ -314,23 +291,6 @@ protected: public: /////////////////////////////////////////////////////////////////// - /// \class OnBlockEnd - /// \brief RAII writing out a remembered string when going out of scope - /// Helper class to store an end tag after the start tag was written. - /// As it's usually used in a RAII context the class is not copyable, - /// but it is movable. Use it like a std::ostringstream. - struct OnBlockEnd : protected ParentOut - { - NON_COPYABLE( OnBlockEnd ); - OnBlockEnd( Out & out_r ) : ParentOut( out_r ) {} - OnBlockEnd( OnBlockEnd && rhs ) : ParentOut( rhs.out() ){ if ( rhs._strp ) _strp.swap( rhs._strp ); } - ~OnBlockEnd() { if ( _strp ) std::cout << _strp->str(); } - std::ostream & operator*() { if ( !_strp ) _strp.reset( new std::ostringstream ); return *_strp; } - private: - boost::scoped_ptr _strp; - }; - - /////////////////////////////////////////////////////////////////// /// \class XmlNode /// \brief XML only: RAII writing a XML nodes start/end tag /// \code @@ -341,41 +301,26 @@ public: /// \endcode struct XmlNode : protected ParentOut { - /////////////////////////////////////////////////////////////////// - /// \class AttrDefType - /// \brief (Key, Value) string pair of XML node attributes - struct AttrDefType : public std::pair - { - typedef std::pair Pair; - - template - AttrDefType( std::string key_r, _Type && val_r ) - : Pair( std::move(key_r), ::out::asXmlNodeAttr( std::forward<_Type>(val_r) ) ) - {} - }; - /////////////////////////////////////////////////////////////////// + typedef zypp::xmlout::Node::Attr Attr; /** Ctor taking nodename and attribute list. */ - XmlNode( Out & out_r, const std::string & name_r, const std::initializer_list & attrs_r = {} ) - : ParentOut( out_r ), _onend( out_r ) + XmlNode( Out & out_r, const std::string & name_r, const std::initializer_list & attrs_r = {} ) + : ParentOut( out_r ) { if ( out().typeXML() && ! name_r.empty() ) - { - std::cout << "<" << name_r; - for ( const auto & pair : attrs_r ) - std::cout << " " << pair.first << "=\"" << xml::escape( pair.second ) << "\""; - std::cout << ">" << std::endl; - *_onend << "" << std::endl; - } + { _node.reset( new zypp::xmlout::Node( std::cout, name_r, attrs_r ) ); } } /** Convenience ctor for one attribute pair */ - XmlNode( Out & out_r, const std::string & name_r, AttrDefType attr_r ) + XmlNode( Out & out_r, const std::string & name_r, Attr attr_r ) : XmlNode( out_r, name_r, { attr_r } ) {} + /** Move ctor */ + XmlNode( XmlNode && rhs ) : ParentOut( rhs ) { _node.swap( rhs._node ); } + private: - OnBlockEnd _onend; + scoped_ptr _node; }; /////////////////////////////////////////////////////////////////// @@ -384,18 +329,10 @@ public: * * \endcode */ - void xmlNode( const std::string & name_r, const std::initializer_list & attrs_r = {} ) - { - if ( typeXML() ) - { - std::cout << "<" << name_r; - for ( const auto & pair : attrs_r ) - std::cout << " " << pair.first << "=\"" << xml::escape(pair.second) << "\""; - std::cout << "/>" << std::endl; - } - } + void xmlNode( const std::string & name_r, const std::initializer_list & attrs_r = {} ) + { if ( typeXML() ) { zypp::xmlout::node( std::cout, name_r, attrs_r ); } } /** \overload for one attribute pair */ - void xmlNode( const std::string & name_r, XmlNode::AttrDefType attr_r ) + void xmlNode( const std::string & name_r, XmlNode::Attr attr_r ) { xmlNode( name_r, { attr_r } ); } /////////////////////////////////////////////////////////////////// @@ -472,7 +409,7 @@ public: template void list( const std::string & nodeName_r, const std::string & title_r, const _Container & container_r, _ListFormater && formater_r = _ListFormater() ) { - TitleNode guard( XmlNode( *this, nodeName_r, XmlNode::AttrDefType( "size", str::numstring( container_r.size() ) ) ), + TitleNode guard( XmlNode( *this, nodeName_r, XmlNode::Attr( "size", str::numstring( container_r.size() ) ) ), (boost::formatNAC( title_r ) % container_r.size()).str() ); list( container_r, std::forward<_ListFormater>(formater_r) ); } -- 2.7.4