-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxpath.hpp
281 lines (233 loc) · 9 KB
/
xpath.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright Morten Bendiksen 2014 - 2016.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef MEDIASEQUENCER_PLUGIN_UTIL_XPATH_XPATH_HPP
#define MEDIASEQUENCER_PLUGIN_UTIL_XPATH_XPATH_HPP
#include "context.hpp"
#include "singleton_iterator.hpp"
#include "child_selector.hpp"
#include "ancestor_selector.hpp"
#include "descendant_selector.hpp"
#include "parent_selector.hpp"
#include "name_selector.hpp"
#include "namespace_selector.hpp"
#include "attribute_selector.hpp"
#include "text_selector.hpp"
#include <boost/range/join.hpp>
namespace mediasequencer { namespace plugin {
/// Namespace for helpfull libraries
namespace util {
/// Namespace for xtpath. See \ref xtpath for usage.
namespace xpath {
// Used to capture the left and right side of the pipe operator
// inide a subexpression (i.e. one used inside where() or where_not())
template <typename Left, typename Right>
struct piped_expression {
Left left;
Right right;
piped_expression(Left left, Right right): left(left), right(right) {
}
};
// Used to capture the left and right side of the double pipe operator
// inide a subexpression (i.e. one used inside where() or where_not())
template <typename Left, typename Right>
struct or_expression {
Left left;
Right right;
or_expression(Left left, Right right): left(left), right(right) {
}
};
// Implements the double pipe operator for subexpressions, e.g.
// range | where ( child("foo") || attribute("bar")). Returns an
// or_expression which captures the left and right side of the
// subexpression.
template<typename Left, typename Right,
typename = typename std::enable_if<is_expr<Left>::value>::type>
or_expression<Left, Right>
operator || (Left const& left, Right const& right) {
return or_expression<Left, Right>(left, right);
}
// Used to evaluate subexpressions that were created with the
// double pipe operator, i.e. captured in an or_expression. Called
// from the where_predicate or where_not_predicate.
template<typename Range, typename Left, typename Right>
boost::range::joined_range
<const decltype(std::declval<const Range>()|std::declval<const Left>()),
const decltype(std::declval<const Range>()|std::declval<const Right>())>
operator | (Range const& range, or_expression<Left, Right> const& or_expression)
{
return join(range|or_expression.left, range|or_expression.right);
}
// Enables expressions with the pipe operator to be considered
// as subexpressions (e.g. to be passed to where() and where_not())
template <typename Left, typename Right>
struct is_expr<piped_expression<Left, Right> >: std::true_type {
};
// Enables expressions with the double pipe operator to be considered
// as subexpressions (e.g. to be passed to where() and where_not())
template <typename Left, typename Right>
struct is_expr<or_expression<Left, Right> >: std::true_type {
};
// constructs piped_expressions from sub expressions using the pipe
// operator
template <typename Left, typename Right,
typename = typename std::enable_if<is_expr<Left>::value>::type>
piped_expression<Left, Right> operator|(Left left, Right right) {
return piped_expression<Left, Right>(left, right);
}
// used to identify where() expressions as well as capture the subexpression
template <typename Expression>
class _where {
public:
Expression e;
_where(Expression e): e(e) {
}
};
// used to identify where_not() expressions as well as capture the subexpression
template <typename Expression>
class _where_not {
public:
Expression e;
_where_not(Expression e): e(e) {
}
};
/// Constructs a subexpression that can be used to filter
/// a range to remove entries that do not satisfy the subexpression.
/// E.g. 'range | where (child("foo"))', will filter away nodes
/// in the range that do not have a child with the name "foo".
// Returns an instance of _where that captures the expression,
// and is used later to construct the where_predicate given to
// the filtered_range.
template <typename Expression>
_where<Expression> where(Expression e) {
return _where<Expression>(e);
}
/// Constructs a subexpression that can be used to filter
/// a range to remove entries that satisfy the subexpression.
/// E.g. 'range | where_not (child("foo"))', will filter away nodes
/// in the range that have a child with the name "foo".
// Returns an instance of _where_not that captures the expression,
// and is used later to construct the where_not_predicate given to
// the filtered_range.
template <typename Expression>
_where_not<Expression> where_not(Expression e) {
return _where_not<Expression>(e);
}
// The predicate given to the boost filtered_range, when
// evaluating a where() subexpression.
template <typename Expression, typename Input>
class where_predicate {
public:
Expression e;
where_predicate(Expression e) : e(e){
}
bool operator()(Input i) const {
auto range = singleton(i) |e ;
return range.begin() != range.end();
}
};
// The predicate given to the boost filtered_range, when
// evaluating a where_not() subexpression.
template <typename Expression, typename Input>
class where_not_predicate {
public:
Expression e;
where_not_predicate(Expression e) : e(e){
}
bool operator()(Input i) const {
auto range = singleton(i) |e ;
return range.begin() == range.end();
}
};
// Used to evaluate subexpressions that were created with the
// pipe operator, i.e. captured in a piped_expression. Called
// from the where_predicate or where_not_predicate.
template<typename Range, typename Left, typename Right,
typename = typename boost::range_iterator<Range>::type>
auto
operator|(Range const& r, piped_expression<Left, Right> e)
-> decltype((r | std::move(e.left)) | std::move(e.right))
{
return (r | std::move(e.left)) | std::move(e.right);
}
// Implements the | operator for expressions using
// 'where' sub-expressions, e.g. 'range | where(child("foo"))'
// Captures the expression inside the where() in a where_predicate,
// Returns a filtered_range which uses the where_predicate as the
// predicate to filter on.
template <typename Range, typename Expression>
boost::range_detail::filtered_range
<where_predicate<Expression, typename Range::iterator::reference>,
const Range>
operator|(Range const& r, _where<Expression> w)
{
return r | boost::adaptors::filtered(
where_predicate<Expression, typename Range::iterator::reference>(w.e));
}
// Implements the | operator for expressions using
// 'where_not' sub-expressions, e.g. 'range | where_not(child("foo"))'
// Captures the expression inside the where() in a where_not_predicate,
// Returns a filtered_range which uses the where_not_predicate as the
// predicate to filter on.
template <typename Range, typename Expression>
boost::range_detail::filtered_range
<where_not_predicate<Expression, typename Range::iterator::reference>,
const Range>
operator|(Range const& r, _where_not<Expression> w)
{
return r | boost::adaptors::filtered(
where_not_predicate<Expression, typename Range::iterator::reference>(w.e));
}
// transformation used for boost::transformed_range, to transform from
// a range of context to a range of the underlying DOM nodes.
template <typename Context>
struct contextToNode {
typename Context::node_type operator()(Context const& context) const {
return context.get_node();
}
typedef typename Context::node_type result_type;
};
// Type for the toNode selector
struct _toNode { };
// Type for the first selector
struct _first { };
namespace {
/// Selects the node from the underlying DOM implementation,
/// If this is used, your code will likely not be
/// directly usable with other DOM implementations without
/// a few changes.
const _toNode toNode;
/// Selects the first instance in the range, or the default
/// constructed object of the value_type from the range.
/// Example: 'range | first'. Returns a context-node if the
/// child, ancestor, parent or similar selectors were the
/// previous selector. Returns a string if any of the text
/// selectors were used.
const _first first;
}
// Implements the '|' operator in sub-expressions with the 'toNode'
// selector, e.g. 'range | toNode'. Returns a transformed_range with
// contextToNode as the transformation.
template <typename Range>
boost::range_detail::transformed_range
<contextToNode<typename Range::iterator::value_type>,
const Range>
operator|(Range const& range, _toNode toNode)
{
return range | transformed(
contextToNode<typename Range::iterator::value_type>());
}
// Implements the '|' operator in sub-expressions with the 'first'
// selector, e.g. 'range | first'.
template <typename Range>
typename Range::iterator::value_type
operator|(Range const& range, _first)
{
return
range.begin() != range.end() ?
*(range.begin()) :
typename Range::iterator::value_type();
}
}}}}
#endif // MEDIASEQUENCER_PLUGIN_UTIL_XPATH_XPATH_HPP