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

96 lines
3.8 KiB
C++
Raw Permalink Normal View History

#ifndef ARABICA_XSLT_OUTPUT_HANDLER_HPP
#define ARABICA_XSLT_OUTPUT_HANDLER_HPP
namespace Arabica
{
namespace XSLT
{
2012-11-06 20:09:20 +01:00
template<class string_type, class string_adaptor>
class OutputHandler : public SAX::DefaultHandler<string_type, string_adaptor>
{
typedef StylesheetConstant<string_type, string_adaptor> SC;
2012-11-15 23:03:42 +01:00
typedef AttributeValidators<string_type, string_adaptor> AV;
public:
2012-11-06 20:09:20 +01:00
OutputHandler(CompilationContext<string_type, string_adaptor>& context) :
context_(context)
{
} // OutputHandler
2012-11-06 20:09:20 +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)
{
if(settings_.empty())
{
2012-11-15 23:03:42 +01:00
static const AV rules = AV::rule(SC::method, false, SC::xml, AllowedValues<string_type>(SC::xml, SC::html, SC::text))
.rule(SC::version, false, SC::Version)
.rule(SC::encoding, false, SC::utf8)
.rule(SC::omit_xml_declaration, false, SC::no, AllowedValues<string_type>(SC::yes, SC::no))
.rule(SC::standalone, false, string_adaptor::empty_string(), AllowedValues<string_type>(SC::yes, SC::no))
.rule(SC::doctype_public, false, string_adaptor::empty_string())
.rule(SC::doctype_system, false, string_adaptor::empty_string())
.rule(SC::cdata_section_elements, false, string_adaptor::empty_string())
.rule(SC::indent, false, SC::no, AllowedValues<string_type>(SC::yes, SC::no))
.rule(SC::media_type, false, string_adaptor::empty_string());
settings_ = rules.gather(qName, atts);
cdataElements_ = extractCDATAElements(settings_[SC::cdata_section_elements]);
return;
} // if(settings_ == 0)
throw SAX::SAXException(string_adaptor::asStdString(qName) + " can not contain elements");
} // startElement
2012-11-06 20:09:20 +01:00
virtual void endElement(const string_type& /* namespaceURI */,
const string_type& /* localName */,
const string_type& /* qName */)
{
context_.stylesheet().output_settings(settings_, cdataElements_);
context_.pop();
} // endElement
2012-11-06 20:09:20 +01:00
virtual void characters(const string_type& ch)
{
verifyNoCharacterData<string_type, string_adaptor>(ch, SC::output);
} // characters
private:
2012-11-06 20:29:22 +01:00
typedef typename Output<string_type, string_adaptor>::CDATAElements CDATAElements;
typedef typename Output<string_type, string_adaptor>::Settings Settings;
CDATAElements extractCDATAElements(const string_type& cdata_section_elements) const
{
2012-11-06 20:29:22 +01:00
CDATAElements elements;
2012-11-21 07:59:40 +01:00
if(string_adaptor::empty(cdata_section_elements))
return elements;
2012-11-21 07:59:40 +01:00
string_type norm_cdata_sec_elements = text::normalize_whitespace<string_type, string_adaptor>(cdata_section_elements);
2012-11-21 12:12:35 +01:00
std::basic_stringstream<typename string_adaptor::value_type> is;
is << norm_cdata_sec_elements;
while(!is.eof())
{
2012-11-21 07:59:40 +01:00
std::basic_string<typename string_adaptor::value_type> e;
is >> e;
2012-11-21 07:59:40 +01:00
XML::QualifiedName<string_type, string_adaptor> qualifiedName = context_.processElementQName(string_adaptor::construct(e));
2012-11-06 21:15:35 +01:00
elements.insert(QName<string_type, string_adaptor>::create(qualifiedName));
} // while
return elements;
} // extractCDATAElements
2012-11-06 20:09:20 +01:00
CompilationContext<string_type, string_adaptor>& context_;
2012-11-06 20:29:22 +01:00
Settings settings_;
CDATAElements cdataElements_;
}; // class OutputHandler
} // namespace XSLT
} // namespace Arabica
#endif