arabica/include/XSLT/impl/xslt_choose.hpp

113 lines
2.9 KiB
C++
Raw Permalink Normal View History

2007-07-19 19:01:42 +02:00
#ifndef ARABICA_XSLT_CHOOSE_HPP
#define ARABICA_XSLT_CHOOSE_HPP
#include "xslt_item.hpp"
#include "xslt_if.hpp"
namespace Arabica
{
namespace XSLT
{
2012-11-04 23:34:40 +01:00
template<class stringT, class adaptorT>
2012-11-08 17:18:49 +01:00
class When : public ItemContainer<stringT, adaptorT>
2007-07-19 19:01:42 +02:00
{
public:
2012-11-04 23:34:40 +01:00
typedef stringT string_type;
typedef adaptorT string_adaptor;
2012-11-02 23:46:11 +01:00
When(Arabica::XPath::XPathExpressionPtr<string_type, string_adaptor> test) :
2007-07-19 19:01:42 +02:00
test_(test)
{
} // When
virtual ~When() { }
2012-11-02 23:46:11 +01:00
bool is_met(const DOM::Node<string_type, string_adaptor>& node,
2012-11-08 18:13:33 +01:00
ExecutionContext<string_type, string_adaptor>& context) const
2007-07-19 19:01:42 +02:00
{
return test_->evaluateAsBool(node, context.xpathContext());
} // is_met
2012-11-02 23:46:11 +01:00
virtual void execute(const DOM::Node<string_type, string_adaptor>& node,
2012-11-08 18:13:33 +01:00
ExecutionContext<string_type, string_adaptor>& context) const
2007-07-19 19:01:42 +02:00
{
2013-01-05 22:26:35 +01:00
this->execute_children(node, context);
2007-07-19 19:01:42 +02:00
} // execute
private:
2012-11-02 23:46:11 +01:00
Arabica::XPath::XPathExpressionPtr<string_type, string_adaptor> test_;
2007-07-19 19:01:42 +02:00
}; // class When
2012-11-04 23:34:40 +01:00
template<class stringT, class adaptorT>
2012-11-08 17:18:49 +01:00
class Otherwise : public ItemContainer<stringT, adaptorT>
2007-07-19 19:01:42 +02:00
{
public:
2012-11-04 23:34:40 +01:00
typedef stringT string_type;
typedef adaptorT string_adaptor;
2012-11-02 23:46:11 +01:00
virtual void execute(const DOM::Node<string_type, string_adaptor>& node,
2012-11-08 18:13:33 +01:00
ExecutionContext<string_type, string_adaptor>& context) const
2007-07-19 19:01:42 +02:00
{
2013-01-05 22:26:35 +01:00
this->execute_children(node, context);
2007-07-19 19:01:42 +02:00
} // execute
}; // class Otherwise
2012-11-04 23:34:40 +01:00
template<class stringT, class adaptorT>
2012-11-08 17:18:49 +01:00
class Choose : public Item<stringT, adaptorT>
2007-07-19 19:01:42 +02:00
{
public:
2012-11-04 23:34:40 +01:00
typedef stringT string_type;
typedef adaptorT string_adaptor;
2007-07-19 19:01:42 +02:00
Choose() :
when_(),
otherwise_(0)
{
} // Choose
virtual ~Choose()
{
2012-11-03 00:16:43 +01:00
for(typename WhenList::const_iterator w = when_.begin(), e = when_.end(); w != e; ++w)
delete (*w);
2007-07-19 19:01:42 +02:00
delete otherwise_;
} // ~Choose
2012-11-02 23:46:11 +01:00
virtual void execute(const DOM::Node<string_type, string_adaptor>& node,
2012-11-08 18:13:33 +01:00
ExecutionContext<string_type, string_adaptor>& context) const
2007-07-19 19:01:42 +02:00
{
2012-11-08 18:13:33 +01:00
ChainStackFrame<string_type, string_adaptor> frame(context);
2012-11-03 00:16:43 +01:00
for(typename WhenList::const_iterator w = when_.begin(), e = when_.end(); w != e; ++w)
if((*w)->is_met(node, context))
2007-07-19 19:01:42 +02:00
{
(*w)->execute(node, context);
2007-07-19 19:01:42 +02:00
return;
} // if ...
if(otherwise_)
otherwise_->execute(node, context);
} // execute
public:
2012-11-02 23:46:11 +01:00
void add_when(When<string_type, string_adaptor>* child)
2007-07-19 19:01:42 +02:00
{
when_.push_back(child);
} // add_when
2012-11-02 23:46:11 +01:00
void set_otherwise(Otherwise<string_type, string_adaptor>* otherwise)
2007-07-19 19:01:42 +02:00
{
otherwise_ = otherwise;
} // set_otherwise
private:
2012-11-02 23:46:11 +01:00
typedef std::vector<When<string_type, string_adaptor>*> WhenList;
WhenList when_;
2012-11-02 23:46:11 +01:00
Otherwise<string_type, string_adaptor>* otherwise_;
2007-07-19 19:01:42 +02:00
}; // class Choose
} // namespace XSLT
} // namespace Arabica
#endif