-
Notifications
You must be signed in to change notification settings - Fork 2
/
XmlPath.h
165 lines (149 loc) · 4.61 KB
/
XmlPath.h
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
/****************************************************************************
**
** Copyright (C) 2015 Jos van den Oever
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 3 as published by the Free Software Foundation and appearing
** in the file lgpl-3.0.txt included in the packaging of this file. Please
** review the following information to ensure the GNU Lesser General Public
** License requirements will be met: https://www.gnu.org/licenses/lgpl.html.
**
****************************************************************************/
#ifndef XMLPATH_H
#define XMLPATH_H
#include <LiteralXml.h>
#include <utility>
#include <QDomDocument>
// TODO: use terminology NodeTest (e.g. NameTest, NodeType) and Predicate
template <typename Predicate>
struct NotPredicate {
const Predicate predicate;
NotPredicate(const Predicate& p) :predicate(p) {}
};
template <typename Tag>
NotPredicate<AttributeName<Tag>>
operator!(const AttributeName<Tag>& a) {
return NotPredicate<AttributeName<Tag>>(a);
}
template <typename Tag1, typename Tag2>
typename std::enable_if<Tag1::is_tag && Tag2::is_tag,std::pair<Tag1,Tag2>>::type
operator|(const Tag1& tag1, const Tag2& tag2) {
return std::make_pair(tag1, tag2);
}
template <typename Tag>
typename std::enable_if<Tag::is_tag,bool>::type
node_filter_matches(const QDomNode& n, const Tag& tag) {
return n.nodeType() == QDomNode::ElementNode && tag.ns() == n.namespaceURI() && tag.name() == n.localName();
}
template <typename Tag>
bool
node_filter_matches(const QDomNode& n, const AttributeName<Tag>& a) {
return n.nodeType() == QDomNode::AttributeNode && a.tag.ns() == n.namespaceURI() && a.tag.name() == n.localName();
}
template <typename A, typename Predicate>
bool
node_filter_matches(const QDomNode& n, const NameTestWithPredicate<A,NotPredicate<Predicate>>& t) {
bool m = node_filter_matches(n, t.tag);
bool cm = true;
if (m) {
const auto atts = n.attributes();
const int l = atts.length();
int i = 0;
while (cm && i < l) {
auto a = atts.item(i);
cm = node_filter_matches(a, t.predicate.predicate);
++i;
}
QDomNode c = n.firstChild();
while (cm && !c.isNull()) {
cm = node_filter_matches(c, t.predicate.predicate);
c = c.nextSibling();
}
}
return cm;
}
template <typename A, typename B>
bool
node_filter_matches(const QDomNode& n, const std::pair<A,B>& p) {
return node_filter_matches(n, p.first) || node_filter_matches(n, p.second);
}
template <typename A, typename B>
bool
node_filter_matches(const QDomNode& n, const NameTestWithPredicate<A,B>& t) {
bool m = node_filter_matches(n, t.tag);
bool cm = false;
if (m) {
const auto atts = n.attributes();
const int l = atts.length();
int i = 0;
while (!cm && i < l) {
auto a = atts.item(i);
cm = node_filter_matches(a, t.predicate);
++i;
}
QDomNode c = n.firstChild();
while (!cm && !c.isNull()) {
cm = node_filter_matches(c, t.predicate);
c = c.nextSibling();
}
}
return cm;
}
template <typename Filter>
std::list<QDomNode>
operator/(const QDomNode& node, const Filter& filter) {
std::list<QDomNode> list;
QDomNode n = node.firstChild();
while (!n.isNull()) {
if (node_filter_matches(n, filter)) {
list.push_back(n);
}
n = n.nextSibling();
}
return list;
}
template <typename Filter>
std::list<QDomNode>
operator/(const std::list<QDomNode>& nodes, const Filter& filter) {
std::list<QDomNode> list;
for (const auto& l: nodes) {
auto i = l/filter;
for (const auto& n: i) {
list.push_back(n);
}
}
return list;
}
// % is used instead of the xpath expression //
// // means self-or-descendant
template <typename Filter>
std::list<QDomNode>
operator%(const QDomNode& node, const Filter& filter) {
std::list<QDomNode> list;
// check self::
if (node_filter_matches(node, filter)) {
list.push_back(node);
}
// recurse
QDomNode n = node.firstChild();
while (!n.isNull()) {
for (const auto& cn: n%filter) {
list.push_back(cn);
}
n = n.nextSibling();
}
return list;
}
template <typename Filter>
std::list<QDomNode>
operator%(const std::list<QDomNode>& nodes, const Filter& filter) {
std::list<QDomNode> list;
for (const auto& l: nodes) {
auto i = l%filter;
for (const auto& n: i) {
list.push_back(n);
}
}
return list;
}
#endif