improved the node printing function

pull/40/head
Matchman Green 2012-05-07 00:03:33 +08:00
parent 6ad573942a
commit d621aa4296
3 changed files with 57 additions and 24 deletions

View File

@ -135,6 +135,11 @@ public :
return m_children.end() ;
}
std::pair<iterator, iterator> Attr()
{
return std::make_pair( m_attr.begin(), m_attr.end() ) ;
}
const std::string& Name() const
{
return m_name ;
@ -177,6 +182,10 @@ private :
ImplVec m_children ;
} ;
Node::iterator::iterator( )
{
}
Node::iterator::iterator( ImplVec::iterator it ) : m_it( it )
{
}
@ -332,37 +341,56 @@ std::string Node::Value() const
return m_ptr->Value() ;
}
std::vector<Node> Node::Children() const
{
std::vector<Node> result ;
for ( Impl::iterator i = m_ptr->Begin() ; i != m_ptr->End() ; ++i )
result.push_back( Node( (*i)->AddRef() ) ) ;
return result ;
}
std::ostream& operator<<( std::ostream& os, const Node& node )
{
if ( node.GetType() == Node::element )
{
os << '<' << node.Name() << ' ' ;
os << '<' << node.Name() ;
// print attributes
Node::Range attrs = node.Attr() ;
if ( attrs.first != attrs.second )
os << ' ' ;
std::copy( attrs.first, attrs.second, std::ostream_iterator<Node>(os, " ") ) ;
os << '>' ;
// recursively print children
for ( Node::iterator i = node.begin() ; i != node.end() ; ++i )
{
if ( (*i).GetType() != Node::attr )
os << *i ;
}
os << "</" << node.Name() << '>' ;
}
else if ( node.GetType() == Node::attr )
{
os << node.Name() << "=\"" << node.Value() << "\"" ;
}
else
{
os << node.Value() ;
}
std::vector<Node> c = node.Children() ;
std::copy( c.begin(), c.end(), std::ostream_iterator<Node>(os, "\n") ) ;
return os ;
}
Node::iterator Node::begin()
Node::iterator Node::begin() const
{
return iterator( m_ptr->Begin() ) ;
}
Node::iterator Node::end()
Node::iterator Node::end() const
{
return iterator( m_ptr->End() ) ;
}
Node::Range Node::Attr() const
{
std::pair<Impl::iterator, Impl::iterator> is = m_ptr->Attr() ;
return std::make_pair( iterator(is.first), iterator(is.second) ) ;
}
} } // end namespace

View File

@ -21,6 +21,7 @@
#include <string>
#include <vector>
#include <utility>
namespace gr { namespace xml {
@ -28,6 +29,7 @@ class Node
{
public :
class iterator ;
typedef std::pair<Node::iterator, Node::iterator> Range ;
public :
Node() ;
@ -57,14 +59,10 @@ public :
static bool IsCompatible( Type parent, Type child ) ;
// TODO: implement iterator begin/end functions instead
std::vector<Node> Children() const ;
iterator begin() const ;
iterator end() const ;
// TODO: implement iterator begin/end functions instead
std::vector<Node> Attr() const ;
iterator begin() ;
iterator end() ;
Range Attr() const ;
private :
class Impl ;
@ -74,10 +72,15 @@ public :
class iterator
{
public :
iterator() ;
explicit iterator( std::vector< gr::xml::Node::Impl* >::iterator it ) ;
typedef Node value_type ;
typedef Node value_type ;
typedef std::forward_iterator_tag iterator_category ;
typedef std::ptrdiff_t difference_type;
typedef Node* pointer;
typedef Node& reference;
value_type operator*() const ;
iterator operator++() ;
iterator operator++(int) ;

View File

@ -69,6 +69,8 @@ void NodeTest::TestParseFile( )
CPPUNIT_ASSERT_EQUAL( std::string("href"), (*i).Name() ) ;
++i ;
}
std::cout << n << std::endl ;
}
} // end of namespace grut