summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2013-03-18 19:00:20 +0000
committerrandomdan <randomdan@localhost>2013-03-18 19:00:20 +0000
commita6a7d7659f68eceb3055df3bcf782d767b8400c9 (patch)
tree4b30737545ede2b07ace99c7098dee530dc197ad
parentAdd simple pwd variable (diff)
downloadproject2-a6a7d7659f68eceb3055df3bcf782d767b8400c9.tar.bz2
project2-a6a7d7659f68eceb3055df3bcf782d767b8400c9.tar.xz
project2-a6a7d7659f68eceb3055df3bcf782d767b8400c9.zip
Have xml cache documents owned by a xmlpp instance
Extend rawview with a variant that allows the source document to be of remote source
-rw-r--r--project2/xml/pch.hpp1
-rw-r--r--project2/xml/rawView.cpp111
-rw-r--r--project2/xml/rawView.h22
-rw-r--r--project2/xml/xmlDocumentCache.cpp6
-rw-r--r--project2/xml/xmlDocumentCache.h4
-rw-r--r--project2/xml/xpathRows.cpp2
6 files changed, 79 insertions, 67 deletions
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 <pch.hpp>
#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 <boost/foreach.hpp>
#include <libxml++/nodes/textnode.h>
-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<const Presenter *>(mp)) {
+ BOOST_FOREACH(const auto * node, getCopyRoot()->get_children()) {
+ const xmlpp::Element * e = dynamic_cast<const xmlpp::Element *>(node);
+ if (e) {
+ copyNode(p, e);
+ }
+ }
+ }
+ }
-RawView::RawView(ScriptNodePtr p) :
- SourceObject(p),
- View(p),
- copyRoot(boost::dynamic_pointer_cast<const XmlScriptNode>(p)->xmlElement())
-{
-}
+ protected:
+ virtual const xmlpp::Element * getCopyRoot() const = 0;
-void
-RawView::execute(const MultiRowSetPresenter * mp) const
-{
- if (const Presenter * p = dynamic_cast<const Presenter *>(mp)) {
- BOOST_FOREACH(xmlpp::Node * node, copyRoot->get_children()) {
- const xmlpp::Element * e = dynamic_cast<const xmlpp::Element *>(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<const xmlpp::Element *>(c)) {
+ copyNode(p, e);
+ }
+ else if (const xmlpp::TextNode * t = dynamic_cast<const xmlpp::TextNode *>(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<const xmlpp::Element *>(c)) {
- copyNode(p, e);
+class RawView : public RawViewBase {
+ public:
+ RawView(ScriptNodePtr p) :
+ SourceObject(p),
+ RawViewBase(p),
+ copyRoot(boost::dynamic_pointer_cast<const XmlScriptNode>(p)->xmlElement())
+ {
}
- else if (const xmlpp::TextNode * t = dynamic_cast<const xmlpp::TextNode *>(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 <libxml++/nodes/element.h>
-#include <boost/intrusive_ptr.hpp>
-#include <map>
-#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<XmlParseError>, 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<DownloadFailed>(documents, url)().get();
+ return safeMapLookup<DownloadFailed>(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<Glib::ustring> Queued;
- typedef boost::shared_ptr<xmlDoc> DocumentPtr;
+ typedef boost::shared_ptr<xmlpp::Document> DocumentPtr;
typedef boost::function0<DocumentPtr> ReturnDocument;
typedef std::map<const Glib::ustring, ReturnDocument> 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<xmlXPathObject> xmlXPathObjectSPtr;
typedef boost::shared_ptr<xmlXPathContext> xmlXPathContextSPtr;
- xmlDocPtr doc = getDocument(url(), encoding());
+ xmlDocPtr doc = getDocument(url(), encoding())->cobj();
xmlXPathContextSPtr xpathCtx = xmlXPathContextSPtr(xmlXPathNewContext(doc), xmlXPathFreeContext);
if (!xpathCtx) {
throw XpathInitError(xmlGetLastError()->message);