-
Notifications
You must be signed in to change notification settings - Fork 1
/
vdom_adaptor.hpp
129 lines (101 loc) · 3.75 KB
/
vdom_adaptor.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
// 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_VDOM_ADAPTOR_HPP
#define MEDIASEQUENCER_PLUGIN_UTIL_XPATH_VDOM_ADAPTOR_HPP
#include "xpath.hpp"
#include <mediasequencer/vdom/node.hpp>
#include <mediasequencer/vdom/treeutil.hpp>
#include <mediasequencer/vdom/xml_convert.hpp>
#include <soul/compact_map.hpp>
namespace mediasequencer { namespace plugin { namespace util { namespace xpath {
namespace vdom = mediasequencer::vdom;
/// Adapts the VDOM library for use with XTpath. Include this file,
/// then instansiate an XTpath context node with the context(vdom::node)
/// function, which is defined here.
struct VdomAdaptor {
typedef mediasequencer::vdom::node node_type;
struct attribute_is_namespace {
bool operator() (std::pair<std::string, std::string> const& a) const {
std::string const& name(a.first);
return name == "xmlns" ||
(name.size() > 6 &&
name.substr(0,6) == "xmlns:");
};
typedef bool result_type;
};
struct namespace_to_pair {
std::pair<std::string, std::string> operator()
(std::pair<std::string, std::string> const& a) const {
return a.first.size() == 5 ? std::make_pair("", a.second) : std::make_pair(a.first.substr(6), a.second);
};
typedef std::pair<std::string, std::string> result_type;
};
static vdom::node null() {
return vdom::node::null;
}
static vdom::node first_child(vdom::node const& node) {
return node.is_null() ? vdom::node::null : node.first_child();
}
static vdom::node next_sibling(vdom::node const& node) {
return node.next_sibling();
}
static vdom::node parent(vdom::node const& node) {
return node.is_null() ? vdom::node::null : node.parent();
}
static bool has_children(vdom::node const& node) {
return node.number_of_children() > 0;
}
static bool has_next_sibling(vdom::node const& node) {
return !node.next_sibling().is_null();
}
static bool is_root(vdom::node const& node) {
return !node.is_null() && node.parent().is_null();
}
static bool is_null(vdom::node const& node) {
return node.is_null();
}
static std::string to_text(vdom::node const& node) {
return mediasequencer::vdom::treeutil::element_to_xml(node);
}
static std::string text(vdom::node const& node) {
auto child = node.first_child();
while (! child.is_null()) {
if (child.type() == vdom::node::TEXT) {
return child.value();
}
child = child.next_sibling();
}
return "";
}
using attribute_range = mediasequencer::vdom::node::attribute_range;
static
attribute_range attributes(vdom::node const& node)
{
return node.plain_attribute_range();
}
static std::string attribute(vdom::node const& node, std::string const& name) {
return node.get_attribute(name);
}
static
boost::range_detail::transformed_range
<namespace_to_pair,
const boost::range_detail::filtered_range
<attribute_is_namespace,
const attribute_range> >
namespace_declarations(vdom::node const& node)
{
return node.plain_attribute_range()
| filtered(attribute_is_namespace())
| transformed(namespace_to_pair());
}
};
namespace {
inline
_context<VdomAdaptor> context(mediasequencer::vdom::node const &n) {
return _context<VdomAdaptor>(n);
}
}
}}}}
#endif // MEDIASEQUENCER_PLUGIN_UTIL_XPATH_VDOM_ADAPTOR_HPP