summaryrefslogtreecommitdiff
path: root/project2/xslRows.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'project2/xslRows.cpp')
-rw-r--r--project2/xslRows.cpp81
1 files changed, 25 insertions, 56 deletions
diff --git a/project2/xslRows.cpp b/project2/xslRows.cpp
index 999f196..07d57b6 100644
--- a/project2/xslRows.cpp
+++ b/project2/xslRows.cpp
@@ -1,4 +1,5 @@
#include "xslRows.h"
+#include "safeMapFind.h"
#include "rowProcessor.h"
#include "logger.h"
#include "xml.h"
@@ -46,16 +47,6 @@ XslRows::loadComplete(const CommonObjects *)
{
}
-void
-XslRows::setFilter(const Glib::ustring & f)
-{
- FilterViews::const_iterator i = fvs.find(f);
- if (i == fvs.end()) {
- throw FilterNotFound(f);
- }
- fv = i->second;
-}
-
bool
XslRows::asHtml() const
{
@@ -75,8 +66,10 @@ XslRows::newCurl() const
}
void
-XslRows::execute(const RowProcessor * rp) const
+XslRows::execute(const Glib::ustring & filter, const RowProcessor * rp) const
{
+ FilterViewPtr fv = safeMapFind<FilterNotFound>(fvs, filter)->second;
+
typedef boost::shared_ptr<xmlXPathObject> xmlXPathObjectSPtr;
typedef boost::shared_ptr<xmlXPathContext> xmlXPathContextSPtr;
xmlDocPtr doc = getDocument(url(), encoding());
@@ -91,16 +84,15 @@ XslRows::execute(const RowProcessor * rp) const
if (!xpathObj || !xpathObj->nodesetval) {
throw XpathEvalError(xmlGetLastError()->message);
}
- rowNum = 1;
Logger()->messagef(LOG_INFO, "%d nodes matched %s", xpathObj->nodesetval->nodeNr, (const char *)(fv->root()));
+ XslState xs(fv);
for (int row = 0; row < xpathObj->nodesetval->nodeNr; row += 1) {
xmlNodePtr rowRoot = xpathObj->nodesetval->nodeTab[row];
xpathCtx->node = rowRoot;
- values.clear();
- BOOST_FOREACH(const FilterView::XPaths::value_type & xp, fv->xpaths) {
- VariableType path(xp.second());
+ BOOST_FOREACH(const Columns::value_type & _xp, fv->columns.get<byColIdx>()) {
+ const FilterViewColumn * xp = static_cast<const FilterViewColumn *>(_xp.get());
+ const VariableType & path(xp->path);
if (boost::get<Null>(&path)) {
- values[xp.first] = Null();
continue;
}
xmlXPathObjectSPtr xpathObjI = xmlXPathObjectSPtr(xmlXPathEvalExpression(path, xpathCtx.get()), xmlXPathFreeObject);
@@ -108,10 +100,10 @@ XslRows::execute(const RowProcessor * rp) const
throw XpathEvalError(xmlGetLastError()->message);
}
if (xpathObjI->floatval) {
- values[xp.first] = xpathObjI->floatval;
+ xs.fields[xp->idx] = xpathObjI->floatval;
}
else if (xpathObjI->stringval) {
- values[xp.first] = Glib::ustring((const char *)xpathObjI->stringval);
+ xs.fields[xp->idx] = Glib::ustring((const char *)xpathObjI->stringval);
}
else if (xpathObjI->nodesetval) {
Glib::ustring str;
@@ -127,64 +119,41 @@ XslRows::execute(const RowProcessor * rp) const
}
}
}
- values[xp.first] = str;
- }
- else {
- values[xp.first] = Null();
+ xs.fields[xp->idx] = str;
}
}
- rp->rowReady();
- rowNum += 1;
+ xs.process(rp);
}
}
XslRows::FilterView::FilterView(const xmlpp::Element * p) :
+ DefinedColumns(p, "field", boost::bind(XslRows::FilterViewColumn::make, _1, _2)),
name(p->get_attribute_value("name")),
root(p, "root")
{
- BOOST_FOREACH(const xmlpp::Node * node, p->find("field")) {
- const xmlpp::Element * elem = dynamic_cast<const xmlpp::Element *>(node);
- if (elem) {
- xpaths.insert(XPaths::value_type(elem->get_attribute_value("name"), Variable(elem, "xpath")));
- }
- }
}
-VariableType
-XslRows::getCurrentValue(const Glib::ustring & id) const
+XslRows::FilterViewColumn::FilterViewColumn(unsigned int idx, const xmlpp::Element * p) :
+ Column(idx, p),
+ path(p, "xpath")
{
- return values.find(id)->second;
}
-VariableType
-XslRows::getCurrentValue(unsigned int col) const
-{
- return getCurrentValue(getColumnName(col));
-}
-
-bool
-XslRows::isNull(unsigned int col) const
-{
- return isNull(getColumnName(col));
-}
-
-bool
-XslRows::isNull(const Glib::ustring & col) const
+XslRows::FilterViewColumn *
+XslRows::FilterViewColumn::make(unsigned int idx, const xmlpp::Element * p)
{
- return (values.find(col) == values.end());
+ return new FilterViewColumn(idx, p);
}
-unsigned int
-XslRows::columnCount() const
+XslRows::XslState::XslState(FilterViewCPtr f) :
+ fv(f)
{
- return fv->xpaths.size();
+ fields.resize(f->columns.size());
}
-const Glib::ustring &
-XslRows::getColumnName(unsigned int col) const
+const Columns &
+XslRows::XslState::getColumns() const
{
- FilterView::XPaths::const_iterator i = fv->xpaths.begin();
- while (col--) i++;
- return i->first;
+ return fv->columns;
}