From 8f46b6bbd723f8775310da6fa5432b122487256e Mon Sep 17 00:00:00 2001 From: randomdan Date: Mon, 18 Mar 2013 19:00:20 +0000 Subject: Have xml cache documents owned by a xmlpp instance Extend rawview with a variant that allows the source document to be of remote source --- project2/xml/pch.hpp | 1 - project2/xml/rawView.cpp | 111 +++++++++++++++++++++++++------------- project2/xml/rawView.h | 22 -------- project2/xml/xmlDocumentCache.cpp | 6 +-- project2/xml/xmlDocumentCache.h | 4 +- project2/xml/xpathRows.cpp | 2 +- 6 files changed, 79 insertions(+), 67 deletions(-) delete mode 100644 project2/xml/rawView.h diff --git a/project2/xml/pch.hpp b/project2/xml/pch.hpp index 329d949..3e9c8f1 100644 --- a/project2/xml/pch.hpp +++ b/project2/xml/pch.hpp @@ -9,7 +9,6 @@ #include "iHaveParameters.h" #include "logger.h" #include "presenter.h" -#include "rawView.h" #include "rowProcessor.h" #include "safeMapFind.h" #include "variables.h" diff --git a/project2/xml/rawView.cpp b/project2/xml/rawView.cpp index cd65cfa..5226caf 100644 --- a/project2/xml/rawView.cpp +++ b/project2/xml/rawView.cpp @@ -1,55 +1,90 @@ #include #include "exceptions.h" -#include "rawView.h" #include "xml.h" #include "presenter.h" #include "scriptLoader.h" -#include "environment.h" -#include "appEngine.h" +#include "xmlDocumentCache.h" #include "xmlScriptParser.h" #include #include -DECLARE_LOADER("rawview", RawView); +class RawViewBase : public View { + public: + RawViewBase(ScriptNodePtr p) : + SourceObject(p), + View(p) { } + void execute(const MultiRowSetPresenter * mp) const + { + if (const Presenter * p = dynamic_cast(mp)) { + BOOST_FOREACH(const auto * node, getCopyRoot()->get_children()) { + const xmlpp::Element * e = dynamic_cast(node); + if (e) { + copyNode(p, e); + } + } + } + } -RawView::RawView(ScriptNodePtr p) : - SourceObject(p), - View(p), - copyRoot(boost::dynamic_pointer_cast(p)->xmlElement()) -{ -} + protected: + virtual const xmlpp::Element * getCopyRoot() const = 0; -void -RawView::execute(const MultiRowSetPresenter * mp) const -{ - if (const Presenter * p = dynamic_cast(mp)) { - BOOST_FOREACH(xmlpp::Node * node, copyRoot->get_children()) { - const xmlpp::Element * e = dynamic_cast(node); - if (e) { - copyNode(p, e); + private: + void copyNode(const Presenter * p, const xmlpp::Element * n) const + { + p->pushSub(n->get_name()); + p->setNamespace(n->get_namespace_prefix(), n->get_namespace_uri()); + xmlpp::Element::AttributeList al = n->get_attributes(); + BOOST_FOREACH(const xmlpp::Attribute * a, al) { + p->addAttribute(a->get_name(), a->get_value()); + } + const xmlpp::Node::NodeList ch = n->get_children(); + BOOST_FOREACH(const xmlpp::Node * c, ch) { + if (const xmlpp::Element * e = dynamic_cast(c)) { + copyNode(p, e); + } + else if (const xmlpp::TextNode * t = dynamic_cast(c)) { + p->addText(t->get_content()); + } } + p->popSub(); } - } -} +}; -void -RawView::copyNode(const Presenter * p, const xmlpp::Element * n) const -{ - p->pushSub(n->get_name()); - p->setNamespace(n->get_namespace_prefix(), n->get_namespace_uri()); - xmlpp::Element::AttributeList al = n->get_attributes(); - BOOST_FOREACH(const xmlpp::Attribute * a, al) { - p->addAttribute(a->get_name(), a->get_value()); - } - const xmlpp::Node::NodeList ch = n->get_children(); - BOOST_FOREACH(const xmlpp::Node * c, ch) { - if (const xmlpp::Element * e = dynamic_cast(c)) { - copyNode(p, e); +class RawView : public RawViewBase { + public: + RawView(ScriptNodePtr p) : + SourceObject(p), + RawViewBase(p), + copyRoot(boost::dynamic_pointer_cast(p)->xmlElement()) + { } - else if (const xmlpp::TextNode * t = dynamic_cast(c)) { - p->addText(t->get_content()); + protected: + const xmlpp::Element * getCopyRoot() const { return copyRoot; } + private: + const xmlpp::Element * copyRoot; +}; +DECLARE_LOADER("rawview", RawView); + +class XmlResourceView : public RawViewBase, XmlDocumentCache, VariableCurlHelper { + public: + XmlResourceView(ScriptNodePtr p) : + SourceObject(p), + RawViewBase(p), + VariableCurlHelper(p), + encoding(p, "encoding", Null()) + { } - } - p->popSub(); -} + protected: + const xmlpp::Element * getCopyRoot() const + { + return getDocument(url(), encoding())->get_root_node(); + } + bool asHtml() const { return false; } + bool withWarnings() const { return true; } + CurlPtr newCurl() const { return VariableCurlHelper::newCurl(); } + + private: + Variable encoding; +}; +DECLARE_LOADER("xmlresourceview", XmlResourceView); diff --git a/project2/xml/rawView.h b/project2/xml/rawView.h deleted file mode 100644 index 67d5628..0000000 --- a/project2/xml/rawView.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef RAWVIEW_H -#define RAWVIEW_H - -#include -#include -#include -#include "view.h" - -/// Project2 component to create output based on its own XML tree node -class RawView : public View { - public: - RawView(ScriptNodePtr p); - void execute(const MultiRowSetPresenter *) const; - - private: - void copyNode(const Presenter *, const xmlpp::Element *) const; - const xmlpp::Element * copyRoot; -}; - -#endif - - diff --git a/project2/xml/xmlDocumentCache.cpp b/project2/xml/xmlDocumentCache.cpp index 73d07bc..96848e7 100644 --- a/project2/xml/xmlDocumentCache.cpp +++ b/project2/xml/xmlDocumentCache.cpp @@ -51,7 +51,7 @@ class XmlDocumentCachePopulator : public CurlCompleteCallback { boost::bind(helperThrow, std::string(xmlGetLastError()->message)))); } XmlDocumentCache::documents.insert(XmlDocumentCache::Documents::value_type(url, - boost::bind(helperReturnDocument, XmlDocumentCache::DocumentPtr(doc, xmlFreeDoc)))); + boost::bind(helperReturnDocument, XmlDocumentCache::DocumentPtr(new xmlpp::Document(doc))))); Logger()->messagebf(LOG_DEBUG, "Download of '%s' completed, stored", url); } void error(CurlBulkFetcher *, const char * error) @@ -74,7 +74,7 @@ class XmlDocumentCachePopulator : public CurlCompleteCallback { char * encoding; }; -xmlDocPtr +XmlDocumentCache::DocumentPtr XmlDocumentCache::getDocument(const Glib::ustring & url, const char * encoding) const { Documents::const_iterator i = documents.find(url); @@ -83,7 +83,7 @@ XmlDocumentCache::getDocument(const Glib::ustring & url, const char * encoding) cbf.perform(); queued.clear(); } - return safeMapLookup(documents, url)().get(); + return safeMapLookup(documents, url)(); } void diff --git a/project2/xml/xmlDocumentCache.h b/project2/xml/xmlDocumentCache.h index 190c989..e6a0b29 100644 --- a/project2/xml/xmlDocumentCache.h +++ b/project2/xml/xmlDocumentCache.h @@ -11,7 +11,7 @@ class XmlDocumentCache { public: typedef std::set Queued; - typedef boost::shared_ptr DocumentPtr; + typedef boost::shared_ptr DocumentPtr; typedef boost::function0 ReturnDocument; typedef std::map Documents; @@ -26,7 +26,7 @@ class XmlDocumentCache { virtual bool withWarnings() const = 0; protected: - xmlDocPtr getDocument(const Glib::ustring & url, const char * encoding) const; + DocumentPtr getDocument(const Glib::ustring & url, const char * encoding) const; private: static CurlBulkFetcher cbf; diff --git a/project2/xml/xpathRows.cpp b/project2/xml/xpathRows.cpp index 2d8ea8a..eedfa8e 100644 --- a/project2/xml/xpathRows.cpp +++ b/project2/xml/xpathRows.cpp @@ -62,7 +62,7 @@ XPathRows::execute(const Glib::ustring & filter, const RowProcessor * rp) const typedef boost::shared_ptr xmlXPathObjectSPtr; typedef boost::shared_ptr xmlXPathContextSPtr; - xmlDocPtr doc = getDocument(url(), encoding()); + xmlDocPtr doc = getDocument(url(), encoding())->cobj(); xmlXPathContextSPtr xpathCtx = xmlXPathContextSPtr(xmlXPathNewContext(doc), xmlXPathFreeContext); if (!xpathCtx) { throw XpathInitError(xmlGetLastError()->message); -- cgit v1.2.3