#ifndef ARABICA_XSLT_ITEM_CONTAINER_HPP #define ARABICA_XSLT_ITEM_CONTAINER_HPP #include "xslt_create_handler.hpp" #include "../xslt_text.hpp" namespace Arabica { namespace XSLT { template SAX::DefaultHandler* createInlineElementHandler(CompilationContext& context); template const ChildElements& AllowedChildren(); template class ItemContainerHandler : public SAX::DefaultHandler { public: typedef typename container_type::string_type string_type; typedef typename container_type::string_adaptor string_adaptor; protected: ItemContainerHandler(CompilationContext& context) : context_(context), container_(0) { } // ItemContainerHandler ItemContainerHandler(CompilationContext& context, container_type* container) : context_(context), container_(container) { } // ItemContainerHandler virtual container_type* createContainer(const string_type& namespaceURI, const string_type& localName, const string_type& qName, const SAX::Attributes& atts) = 0; CompilationContext& context() const { return context_; } container_type* container() const { return container_; } public: virtual void startElement(const string_type& namespaceURI, const string_type& localName, const string_type& qName, const SAX::Attributes& atts) { if(container_ == 0) { container_ = createContainer(namespaceURI, localName, qName, atts); return; } // if(item_ == 0) if(createChild(namespaceURI, localName, qName, atts)) return; throw SAX::SAXException(string_adaptor::asStdString(qName) + " <- Sorry, don't know about that yet :)"); } // startElement virtual void endElement(const string_type& /* namespaceURI */, const string_type& /* localName */, const string_type& /* qName */) { context_.parentContainer().add_item(container_); context_.pop(); } // endElement virtual void characters(const string_type& ch) { for(typename string_adaptor::const_iterator i = string_adaptor::begin(ch), e = string_adaptor::end(ch); i != e; ++i) if(!Arabica::XML::is_space(*i)) { container_->add_item(new Text(ch)); return; } // if ... } // characters protected: virtual bool createChild(const string_type& namespaceURI, const string_type& localName, const string_type& qName, const SAX::Attributes& atts) { if(namespaceURI == StylesheetConstant::NamespaceURI) { SAX::DefaultHandler* child = AllowedChildren().create(localName, context_); if(child != 0) { context_.push(container_, child, namespaceURI, localName, qName, atts); return true; } return false; } // if(namespaceURI ... context_.push(container_, createInlineElementHandler(context_), namespaceURI, localName, qName, atts); return true; } // createChild private: CompilationContext& context_; container_type* container_; }; // class ItemContainerHandler } // namespace XSLT } // namespace Arabica #include "xslt_apply_imports_handler.hpp" #include "xslt_apply_templates_handler.hpp" #include "xslt_attribute_handler.hpp" #include "xslt_call_template_handler.hpp" #include "xslt_choose_handler.hpp" #include "xslt_comment_handler.hpp" #include "xslt_copy_handler.hpp" #include "xslt_element_handler.hpp" #include "xslt_for_each_handler.hpp" #include "xslt_if_handler.hpp" #include "xslt_message_handler.hpp" #include "xslt_processing_instruction_handler.hpp" #include "xslt_text_handler.hpp" #include "xslt_value_of_handler.hpp" #include "xslt_variable_handler.hpp" #include "xslt_inline_element_handler.hpp" namespace Arabica { namespace XSLT { template const ChildElements& AllowedChildren() { typedef StylesheetConstant SC; static const ChildElements allowedChildren = ChildElements::add(SC::apply_imports, CreateHandler >) .add(SC::apply_templates, CreateHandler >) .add(SC::attribute, CreateHandler >) .add(SC::call_template, CreateHandler >) .add(SC::choose, CreateHandler >) .add(SC::comment, CreateHandler >) .add(SC::copy, CreateHandler >) .add(SC::copy_of, CreateHandler >) .add(SC::element, CreateHandler >) .add(SC::fallback, CreateHandler >) .add(SC::for_each, CreateHandler >) .add(SC::if_, CreateHandler >) .add(SC::message, CreateHandler >) .add(SC::number, CreateHandler >) .add(SC::processing_instruction, CreateHandler >) .add(SC::text, CreateHandler >) .add(SC::value_of, CreateHandler >) .add(SC::variable, CreateHandler > >); return allowedChildren; } // AllowedChildren template SAX::DefaultHandler* createInlineElementHandler(CompilationContext& context) { return new InlineElementHandler(context); } // InlineElementHandler } // namespace XSLT } // namespace Arabica #endif