#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 const ChildElement* AllowedChildren(); template SAX::DefaultHandler* createInlineElementHandler(CompilationContext& context); 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_type::const_iterator i = ch.begin(), e = ch.end(); 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) { for(const ChildElement* c = AllowedChildren(); c->name != string_adaptor::empty_string(); ++c) if(c->name == localName) { context_.push(container_, c->createHandler(context_), namespaceURI, localName, qName, atts); return true; } // if ... 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 ChildElement* AllowedChildren() { typedef StylesheetConstant SC; static const ChildElement allowedChildren[] = { { SC::apply_imports, CreateHandler > }, { SC::apply_templates, CreateHandler > }, { SC::attribute, CreateHandler > }, { SC::call_template, CreateHandler > }, { SC::choose, CreateHandler > }, { SC::comment, CreateHandler > }, { SC::copy, CreateHandler > }, { SC::copy_of, CreateHandler > }, { SC::element, CreateHandler > }, { SC::fallback, CreateHandler >}, { SC::for_each, CreateHandler > }, { SC::if_, CreateHandler > }, { SC::message, CreateHandler >}, { SC::number, CreateHandler >}, { SC::processing_instruction, CreateHandler > }, { SC::text, CreateHandler > }, { SC::value_of, CreateHandler > }, { SC::variable, CreateHandler > > }, { string_adaptor::empty_string(), 0 } }; return allowedChildren; } // AllowedChildren template SAX::DefaultHandler* createInlineElementHandler(CompilationContext& context) { return new InlineElementHandler(context); } // InlineElementHandler } // namespace XSLT } // namespace Arabica #endif