Click here to Skip to main content
15,886,724 members
Articles / Programming Languages / XML

XmlBind: putting PugXML on steroïds !

Rate me:
Please Sign up or sign in to vote.
3.36/5 (10 votes)
2 Oct 20035 min read 131.8K   580   31  
A mutant XML parser using IoBind, EDOM and PugXML.
// iobind library
//
// author: Jonathan de Halleux, 2003

#ifndef IOBIND_PARSER_UTILS_HPP
#define IOBIND_PARSER_UTILS_HPP

#include <boost/spirit.hpp>
#include <boost/call_traits.hpp>
#include <string>

namespace iobind{
namespace parser{
namespace detail{

template<
	typename Container,
	typename Policy
> 
class append_with_policy
{
public:
    append_with_policy( Container& container_, Policy const& policy_)
        : m_container(container_), m_policy(policy_)
    {};

    // the method called by the parser    
    template <typename IteratorT>
    void operator()(IteratorT const& first, IteratorT const& last) const
    {
		m_container.insert(m_container.end(), m_policy.encode( std::string(first, last) ) );
    }

private:
    Container& m_container;
	Policy const& m_policy;
};

template<
	typename Container,
	typename Policy
> 
class insert_with_policy
{
public:
    insert_with_policy( size_t& index_, Container& container_, Policy const& policy_)
        : m_index(index_), m_container(container_), m_policy(policy_)
    {};

    // the method called by the parser    
    template <typename IteratorT>
    void operator()(IteratorT const& first, IteratorT const& last) const
    {
		if (m_index < m_container.size())
			m_container[m_index++]=m_policy.encode( std::string(first, last) );
#ifdef _DEBUG
		else
			std::cerr<<"insert_with_policy: could not add data"<<std::endl;
#endif
	}

private:
	size_t& m_index;
    Container& m_container;
	Policy const& m_policy;
};

template<
	typename Pair,
	typename FirstPolicy,
	typename SecondPolicy
> 
class assign_pair_with_policy
{
public:
 
	explicit assign_pair_with_policy( 
		Pair& pair_, 
		FirstPolicy const& first_policy_,
		SecondPolicy const& second_policy_,
		std::string const& first_,
		std::string const& second_
		)
        : 
		m_pair(pair_), 
		m_first_policy(first_policy_),
		m_second_policy(second_policy_),
		m_first(first_),
		m_second(second_)
    {};

    // the method called by the parser    
    template <typename IteratorT>
    void operator()(IteratorT first, IteratorT last) const
    {
		m_pair=Pair(
			m_first_policy.encode(m_first.c_str()),
			m_second_policy.encode(m_second.c_str())
			);
    }

private:
    Pair& m_pair;
	FirstPolicy const& m_first_policy;
	SecondPolicy const& m_second_policy;
	std::string const& m_first;
	std::string const& m_second;
};

class concat_string
{
public:
    // key_ and val_ should point to the string modified in keyvalue_grammar
    // kvc_ is the map of key - values
    concat_string( std::ostream& out_)
        : out(out_)
    {  };

    // the method called by the parser    
    template <typename IteratorT>
    void operator()(IteratorT first, IteratorT last) const
    {
		out<<std::string(first,last);
    }

	template <typename IteratorT>
	void operator()(IteratorT single) const
	{
		out<<single;
	}
private:
    std::ostream& out;
};

class concat_symbol
{
public:
    // key_ and val_ should point to the string modified in keyvalue_grammar
    // kvc_ is the map of key - values
    concat_symbol( std::ostream& out_)
        : out(out_)
    {  };

    // the method called by the parser    
	void operator()(std::string const& str) const
	{
		out<<str;
	}
private:
    std::ostream& out;
};

};//details
};//parser
};//iobind

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions