Started working on an XPath visitor class
This commit is contained in:
parent
3244c693f8
commit
ff43b1d135
4 changed files with 48 additions and 5 deletions
|
|
@ -259,7 +259,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual value_t to_value() const {
|
virtual value_t to_value() const {
|
||||||
return text();
|
return value_t(text(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print(std::ostream& out) const;
|
void print(std::ostream& out) const;
|
||||||
|
|
|
||||||
|
|
@ -1565,6 +1565,9 @@ void value_t::print(std::ostream& out, const int first_width,
|
||||||
case AMOUNT:
|
case AMOUNT:
|
||||||
case STRING:
|
case STRING:
|
||||||
case POINTER:
|
case POINTER:
|
||||||
|
// jww (2007-05-14): I need a version of this print just for XPath
|
||||||
|
// expression, since amounts and strings need to be output with
|
||||||
|
// special syntax.
|
||||||
out << *this;
|
out << *this;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2150,8 +2150,6 @@ void xpath_t::op_t::dump(std::ostream& out, const int depth) const
|
||||||
} else {
|
} else {
|
||||||
assert(! right());
|
assert(! right());
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
assert(! left());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
46
src/xpath.h
46
src/xpath.h
|
|
@ -231,7 +231,7 @@ public:
|
||||||
|
|
||||||
typedef std::list<element_t>::const_iterator element_iterator;
|
typedef std::list<element_t>::const_iterator element_iterator;
|
||||||
|
|
||||||
struct node_appender_t {
|
struct value_node_appender_t {
|
||||||
value_t::sequence_t& sequence;
|
value_t::sequence_t& sequence;
|
||||||
node_appender_t(value_t::sequence_t& _sequence)
|
node_appender_t(value_t::sequence_t& _sequence)
|
||||||
: sequence(_sequence) {}
|
: sequence(_sequence) {}
|
||||||
|
|
@ -245,7 +245,7 @@ public:
|
||||||
|
|
||||||
void find_all(value_t::sequence_t& result,
|
void find_all(value_t::sequence_t& result,
|
||||||
node_t& start, scope_t * scope) {
|
node_t& start, scope_t * scope) {
|
||||||
visit(start, scope, node_appender_t(result));
|
visit(start, scope, value_node_appender_t(result));
|
||||||
}
|
}
|
||||||
|
|
||||||
void visit(node_t& start, scope_t * scope,
|
void visit(node_t& start, scope_t * scope,
|
||||||
|
|
@ -261,6 +261,36 @@ public:
|
||||||
scope_t * scope, const function<void (node_t&)>& func);
|
scope_t * scope, const function<void (node_t&)>& func);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class path_iterator_t
|
||||||
|
{
|
||||||
|
path_t path;
|
||||||
|
std::vector<node_t *> sequence;
|
||||||
|
|
||||||
|
struct node_appender_t {
|
||||||
|
std::vector<node_t *>& sequence;
|
||||||
|
node_appender_t(std::vector<node_t *>& _sequence)
|
||||||
|
: sequence(_sequence) {}
|
||||||
|
void operator()(node_t& node) {
|
||||||
|
sequence.push_back(&node);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
typedef std::vector<node_t *>::iterator iterator;
|
||||||
|
typedef std::vector<node_t *>::const_iterator const_iterator;
|
||||||
|
|
||||||
|
path_iterator_t(const xpath_t& path_expr, node_t& start, scope_t * scope)
|
||||||
|
: path(path_expr) {
|
||||||
|
path.visit(start, scope, node_appender_t(sequence));
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator begin() { return sequence.begin(); }
|
||||||
|
const_iterator begin() const { return sequence.begin(); }
|
||||||
|
|
||||||
|
iterator end() { return sequence.end(); }
|
||||||
|
const_iterator end() const { return sequence.end(); }
|
||||||
|
};
|
||||||
|
|
||||||
struct op_t : public noncopyable
|
struct op_t : public noncopyable
|
||||||
{
|
{
|
||||||
enum kind_t {
|
enum kind_t {
|
||||||
|
|
@ -704,6 +734,18 @@ public:
|
||||||
return temp.calc(top, scope);
|
return temp.calc(top, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void find_all(value_t::sequence_t& result,
|
||||||
|
node_t& start, scope_t * scope) {
|
||||||
|
path_t path(*this);
|
||||||
|
path.find_all(result, start, scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
void visit(node_t& start, scope_t * scope,
|
||||||
|
const function<void (node_t&)>& func) {
|
||||||
|
path_t path(*this);
|
||||||
|
path.visit(start, scope, func);
|
||||||
|
}
|
||||||
|
|
||||||
void print(std::ostream& out, xml::document_t& document) const {
|
void print(std::ostream& out, xml::document_t& document) const {
|
||||||
print(out, document, true, NULL, NULL, NULL);
|
print(out, document, true, NULL, NULL, NULL);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue