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

167 lines
5.6 KiB
C++
Raw Normal View History

2007-07-19 19:01:42 +02:00
#ifndef ARABICA_XSLT_CHOOSE_HANDLER_HPP
#define ARABICA_XSLT_CHOOSE_HANDLER_HPP
#include "../xslt_choose.hpp"
namespace Arabica
{
namespace XSLT
{
2012-11-02 23:46:11 +01:00
template<class string_type, class string_adaptor>
class WhenHandler : public ItemContainerHandler<When<string_type, string_adaptor> >
2007-07-19 19:01:42 +02:00
{
public:
2012-11-02 23:46:11 +01:00
WhenHandler(Choose<string_type, string_adaptor>* choose,
CompilationContext<string_type, string_adaptor>& context) :
ItemContainerHandler<When<string_type, string_adaptor> >(context),
2007-07-19 19:01:42 +02:00
choose_(choose)
{
} // WhenHandler
2012-11-02 23:46:11 +01:00
virtual When<string_type, string_adaptor>* 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
{
2010-01-10 23:02:43 +01:00
static const ValueRule rules[] = { { "test", true, 0, 0 },
2010-01-10 22:25:35 +01:00
{ 0, false, 0, 0 } };
2012-11-02 23:46:11 +01:00
string_type test = gatherAttributes(qName, atts, rules)["test"];
2007-07-19 19:01:42 +02:00
2012-11-02 23:46:11 +01:00
return new When<string_type, string_adaptor>(ItemContainerHandler<When<string_type, string_adaptor> >::context().xpath_expression(test));
2007-07-19 19:01:42 +02:00
} // startElement
2012-11-02 23:46:11 +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-02 23:46:11 +01:00
choose_->add_when(ItemContainerHandler<When<string_type, string_adaptor> >::container());
2007-07-19 19:01:42 +02:00
context().pop();
} // endElement
private:
2012-11-02 23:46:11 +01:00
Choose<string_type, string_adaptor>* choose_;
2007-07-19 19:01:42 +02:00
}; // class WhenHandler
2012-11-02 23:46:11 +01:00
template<class string_type, class string_adaptor>
class OtherwiseHandler : public ItemContainerHandler<Otherwise<string_type, string_adaptor> >
2007-07-19 19:01:42 +02:00
{
public:
2012-11-02 23:46:11 +01:00
OtherwiseHandler(Choose<string_type, string_adaptor>* choose,
CompilationContext<string_type, string_adaptor>& context) :
ItemContainerHandler<Otherwise<string_type, string_adaptor> >(context),
2007-07-19 19:01:42 +02:00
choose_(choose)
{
} // OtherwiseHandler
2012-11-02 23:46:11 +01:00
virtual Otherwise<string_type, string_adaptor>* 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
{
if(atts.getLength())
throw SAX::SAXException("xsl:otherwise may not have any attributes");
2012-11-02 23:46:11 +01:00
return new Otherwise<string_type, string_adaptor>();
2007-07-19 19:01:42 +02:00
} // createContainer
2012-11-02 23:46:11 +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
{
choose_->set_otherwise(container());
context().pop();
} // endElement
private:
2012-11-02 23:46:11 +01:00
Choose<string_type, string_adaptor>* choose_;
2007-07-19 19:01:42 +02:00
}; // class OtherwiseHandler
2012-11-02 23:46:11 +01:00
template<class string_type, class string_adaptor>
class ChooseHandler : public SAX::DefaultHandler<string_type, string_adaptor>
2007-07-19 19:01:42 +02:00
{
public:
2012-11-02 23:46:11 +01:00
ChooseHandler(CompilationContext<string_type, string_adaptor>& context) :
2007-07-19 19:01:42 +02:00
context_(context),
choose_(0),
seenWhere_(false),
2007-07-19 19:01:42 +02:00
seenOtherwise_(false)
{
} // ChooseHandler
2012-11-02 23:46:11 +01:00
virtual void startElement(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
{
if(!choose_)
{
if(atts.getLength() != 0)
throw SAX::SAXException("xsl:choose can not have attributes");
2012-11-02 23:46:11 +01:00
choose_ = new Choose<string_type, string_adaptor>();
2007-07-19 19:01:42 +02:00
return;
} // if ...
if(namespaceURI == StylesheetConstant::NamespaceURI())
{
if(localName == "when")
{
seenWhere_ = true;
2007-07-19 19:01:42 +02:00
if(seenOtherwise_)
throw SAX::SAXException("xsl:otherwise must be the last element in an xsl:choose");
context_.push(0,
2012-11-02 23:46:11 +01:00
new WhenHandler<string_type, string_adaptor>(choose_, context_),
2007-07-19 19:01:42 +02:00
namespaceURI,
localName,
qName,
2007-07-19 19:01:42 +02:00
atts);
return;
} // if(localName == "when")
if(localName == "otherwise")
{
if(seenOtherwise_)
throw SAX::SAXException("xsl:choose may only have one xsl:otherwise element");
2007-07-19 19:01:42 +02:00
seenOtherwise_ = true;
context_.push(0,
2012-11-02 23:46:11 +01:00
new OtherwiseHandler<string_type, string_adaptor>(choose_, context_),
2007-07-19 19:01:42 +02:00
namespaceURI,
localName,
qName,
2007-07-19 19:01:42 +02:00
atts);
return;
} // if(localName == "otherwise")
} // if ...
throw SAX::SAXException("xsl:choose can not contain " + qName + ". Only xsl:when and xsl:otherwise are allowed");
} // startElement
2012-11-02 23:46:11 +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
{
if(!seenWhere_)
throw SAX::SAXException("xsl:choose must contain at least one xsl:where element");
2007-07-19 19:01:42 +02:00
context_.parentContainer().add_item(choose_);
context_.pop();
} // endElement
2012-11-02 23:46:11 +01:00
virtual void characters(const string_type& ch)
2007-07-19 19:01:42 +02:00
{
verifyNoCharacterData(ch, "xsl:choose");
2007-07-19 19:01:42 +02:00
} // characters
private:
2012-11-02 23:46:11 +01:00
CompilationContext<string_type, string_adaptor>& context_;
Choose<string_type, string_adaptor>* choose_;
bool seenWhere_;
2007-07-19 19:01:42 +02:00
bool seenOtherwise_;
}; // class ChooseHandler
} // namespace XSLT
} // namespace Arabica
#endif