arabica/include/XSLT/impl/handler/xslt_variable_handler.hpp

104 lines
3.3 KiB
C++
Raw Normal View History

2007-07-19 19:01:42 +02:00
#ifndef ARABICA_XSLT_VARIABLES_HANDLER_HPP
#define ARABICA_XSLT_VARIABLES_HANDLER_HPP
#include "../xslt_param.hpp"
#include "../xslt_variable.hpp"
#include "xslt_item_container_handler.hpp"
namespace Arabica
{
namespace XSLT
{
template<class VType>
class VariableHandler : public ItemContainerHandler<VType>
{
typedef StylesheetConstant<string_type, string_adaptor> SC;
2007-07-19 19:01:42 +02:00
public:
2012-11-09 20:17:13 +01:00
typedef typename VType::string_type string_type;
typedef typename VType::string_adaptor string_adaptor;
typedef ItemContainerHandler<VType> baseT;
VariableHandler(CompilationContext<string_type, string_adaptor>& context) :
baseT(context),
has_select_(false),
precedence_(Precedence::FrozenPrecedence())
{
} // VariableHandler
2012-11-09 20:17:13 +01:00
VariableHandler(CompilationContext<string_type, string_adaptor>& context, const Precedence& precedence) :
baseT(context),
has_select_(false),
precedence_(precedence)
2007-07-19 19:01:42 +02:00
{
} // VariableHandler
protected:
2012-11-09 20:17:13 +01:00
virtual VType* createContainer(const string_type& /* namespaceURI */,
const string_type& /* localName */,
const string_type& qName,
const SAX::Attributes<string_type, string_adaptor>& atts)
2007-07-19 19:01:42 +02:00
{
static const ValueRule<string_type> rules[] = { { SC::name, true, 0, 0 },
{ SC::select, false, 0, 0 },
{ string_adaptor::empty_string(), false, 0, 0 } };
2007-07-19 19:01:42 +02:00
2012-11-09 20:17:13 +01:00
std::map<string_type, string_type> attrs = gatherAttributes(qName, atts, rules);
2007-07-19 19:01:42 +02:00
const string_type& select = atts.getValue(SC::select);
2012-11-09 20:17:13 +01:00
Arabica::XPath::XPathExpressionPtr<string_type, string_adaptor> xpath;
if(select != string_adaptor::empty_string())
{
2012-11-09 20:17:13 +01:00
xpath = baseT::context().xpath_expression(select);
has_select_ = true;
} // if ...
2007-07-19 19:01:42 +02:00
string_type name = baseT::context().processInternalQName(attrs[SC::name]).clarkName();
return new VType(name, xpath, precedence_);
2007-07-19 19:01:42 +02:00
} // createContainer
2012-11-09 20:17:13 +01:00
virtual void characters(const string_type& ch)
{
if(has_select_)
{
2012-11-09 20:17:13 +01:00
for(string_type::const_iterator i = ch.begin(), e = ch.end(); i != e; ++i)
2008-08-05 16:32:40 +02:00
if(!Arabica::XML::is_space(*i))
throw SAX::SAXException("A variable or param can not have both a select attribute and text context");
}
ItemContainerHandler<VType>::characters(ch);
} // characters
private:
bool has_select_;
const Precedence precedence_;
2007-07-19 19:01:42 +02:00
}; // class VariableHandler
template<class VType>
class TopLevelVariableHandler : public VariableHandler<VType>
{
public:
2012-11-09 20:17:13 +01:00
typedef typename VType::string_type string_type;
typedef typename VType::string_adaptor string_adaptor;
typedef VariableHandler<VType> baseT;
TopLevelVariableHandler(CompilationContext<string_type, string_adaptor>& context) :
baseT(context, context.precedence())
2007-07-19 19:01:42 +02:00
{
} // VariableHandler
2012-11-09 20:17:13 +01:00
virtual void endElement(const string_type& /* namespaceURI */,
const string_type& /* localName */,
const string_type& /* qName */)
2007-07-19 19:01:42 +02:00
{
2012-11-09 20:17:13 +01:00
baseT::context().stylesheet().add_variable(baseT::container());
baseT::context().pop();
2007-07-19 19:01:42 +02:00
} // endElement
}; // class TopLevelVariableHandler
} // namespace XSLT
} // namespace Arabica
#endif