From 4bc87cb8d1c8a57a2f1ddf5d2b9b79240187105c Mon Sep 17 00:00:00 2001 From: randomdan Date: Mon, 19 Aug 2013 00:17:30 +0000 Subject: Allow specifying a custom date format in an XML presenter declaration, used to correct the date format used in GB RSS feeds --- project2/xml/xmlPresenter.cpp | 18 ++++++++++++------ project2/xml/xmlPresenter.h | 1 + 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/project2/xml/xmlPresenter.cpp b/project2/xml/xmlPresenter.cpp index 32960a1..d0281f7 100644 --- a/project2/xml/xmlPresenter.cpp +++ b/project2/xml/xmlPresenter.cpp @@ -45,19 +45,21 @@ DECLARE_GENERIC_LOADER("xml", PresenterLoader, XmlPresenter) XmlPresenter::XmlPresenter(const Glib::ustring & responseRootNodeName, const Glib::ustring & responseStyle, const Glib::ustring & ct) : Presenter(Default), ContentPresenter(ct), + customDateFormat(Null()), root(responseRootNodeName), style(responseStyle.empty() ? Null() : VariableType(responseStyle)) { } -XmlPresenter::XmlPresenter(ScriptNodePtr e, ObjectSource os, ExecContext *) : +XmlPresenter::XmlPresenter(ScriptNodePtr e, ObjectSource os, ExecContext * ec) : TransformSource(e, os), Presenter(os), - ContentPresenter(e->value("contenttype", "application/xml", NULL)), + ContentPresenter(e->value("contenttype", "application/xml", ec)), SourceOf(e, os), SourceOf<_xmlDoc>(e, os), SourceOf >(e, os), SourceOf(e, os), + customDateFormat(e->value("dateformat", XmlPresenter::customFormat ? VariableType(*XmlPresenter::customFormat) : Null(), ec)), root(e, "root"), style(e, "style", Null()) { @@ -155,7 +157,10 @@ XmlPresenter::addNamedValue(const Glib::ustring & name, const Glib::ustring & ns class XmlNodeWriter : public boost::static_visitor { public: - XmlNodeWriter(xmlpp::Element * n) : node(n) { } + XmlNodeWriter(xmlpp::Element * n, const VariableType & df) : + node(n), + dateFormat(df) + { } bool operator()(const Null &) const { addType(XmlPresenter::typeidNull, "null"); return false; @@ -184,11 +189,11 @@ class XmlNodeWriter : public boost::static_visitor { if (XmlPresenter::dateAttr) { node->set_attribute("date", boost::gregorian::to_iso_extended_string(i.date())); } - if (!!XmlPresenter::customFormat) { + if (!dateFormat.isNull()) { std::stringstream ss; boost::date_time::time_facet * ft = new boost::date_time::time_facet(); ss.imbue(std::locale(ss.getloc(), ft)); - ft->format(XmlPresenter::customFormat->c_str()); + ft->format(dateFormat); ss << boost::get(i); node->set_attribute("custom", ss.str()); } @@ -203,12 +208,13 @@ class XmlNodeWriter : public boost::static_visitor { } private: xmlpp::Element * node; + const VariableType dateFormat; }; void XmlPresenter::addText(const VariableType & value) const { - if (boost::apply_visitor(XmlNodeWriter(nodeStack.back()), value)) { + if (boost::apply_visitor(XmlNodeWriter(nodeStack.back(), customDateFormat), value)) { nodeStack.back()->add_child_text(value); } } diff --git a/project2/xml/xmlPresenter.h b/project2/xml/xmlPresenter.h index de3f65b..6dbee7e 100644 --- a/project2/xml/xmlPresenter.h +++ b/project2/xml/xmlPresenter.h @@ -53,6 +53,7 @@ class XmlPresenter : public Presenter, public ContentPresenter, public SourceOf< void createDoc(const Glib::ustring & responseRootNodeName, const Glib::ustring & responseStyle) const; XmlDocumentPtr responseDoc; mutable std::vector nodeStack; + const VariableType customDateFormat; const Variable root; const Variable style; -- cgit v1.2.3