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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use std::borrow::Cow;
use std::iter::Rev;
use std::collections::hash_map::{HashMap, Entry};
use std::collections::hash_map::Iter as Entries;
use std::collections::HashSet;
use std::slice::Iter;
use util::{OptionBorrowExt, IteratorClonedPairwiseExt};
pub const NS_XMLNS_PREFIX: &'static str = "xmlns";
pub const NS_XMLNS_URI: &'static str = "http://www.w3.org/2000/xmlns/";
pub const NS_XML_PREFIX: &'static str = "xml";
pub const NS_XML_URI: &'static str = "http://www.w3.org/XML/1998/namespace";
pub const NS_EMPTY_URI: &'static str = "";
pub type UriMapping<'a> = (Option<&'a str>, &'a str);
pub trait NamespaceIterable<'a> {
type Iter: Iterator<Item=UriMapping<'a>>;
fn uri_mappings(&'a self) -> Self::Iter;
}
#[derive(PartialEq, Clone)]
pub struct Namespace(pub HashMap<Option<String>, String>);
impl Namespace {
#[inline]
pub fn empty() -> Namespace { Namespace(HashMap::with_capacity(3)) }
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn is_essentially_empty(&self) -> bool {
if self.0.len() > 3 { return false; }
self.0.iter().all(|(k, v)| match (k.borrow_internals(), &v[..]) {
(None, NS_EMPTY_URI) => true,
(Some(NS_XMLNS_PREFIX), NS_XMLNS_URI) => true,
(Some(NS_XML_PREFIX), NS_XML_URI) => true,
_ => false
})
}
pub fn put<'s1, 's2, S1, S2>(&mut self, prefix: Option<S1>, uri: S2) -> bool
where S1: Into<Cow<'s1, str>>, S2: Into<Cow<'s2, str>> {
match self.0.entry(prefix.map(|v| v.into().into_owned())) {
Entry::Occupied(_) => false,
Entry::Vacant(ve) => {
ve.insert(uri.into().into_owned());
true
}
}
}
pub fn force_put<'s1, 's2, S1, S2>(&mut self, prefix: Option<S1>, uri: S2) -> Option<String>
where S1: Into<Cow<'s1, str>>, S2: Into<Cow<'s2, str>> {
self.0.insert(prefix.map(|v| v.into().into_owned()), uri.into().into_owned())
}
pub fn get<'a>(&'a self, prefix: &Option<String>) -> Option<&'a str> {
self.0.get(prefix).map(|s| &**s)
}
}
pub struct NamespaceMappings<'a> {
entries: Entries<'a, Option<String>, String>
}
impl<'a> Iterator for NamespaceMappings<'a> {
type Item = (Option<&'a str>, &'a str);
fn next(&mut self) -> Option<(Option<&'a str>, &'a str)> {
self.entries.next().map(|(prefix, uri)| {
(prefix.as_ref().map(|p| &p[..]), &uri[..])
})
}
}
impl<'a> NamespaceIterable<'a> for Namespace {
type Iter = NamespaceMappings<'a>;
fn uri_mappings(&'a self) -> NamespaceMappings<'a> {
NamespaceMappings { entries: self.0.iter() }
}
}
#[derive(Clone, PartialEq)]
pub struct NamespaceStack(pub Vec<Namespace>);
impl NamespaceStack {
#[inline]
pub fn empty() -> NamespaceStack { NamespaceStack(Vec::with_capacity(2)) }
#[inline]
pub fn default() -> NamespaceStack {
let mut nst = NamespaceStack::empty();
nst.push_empty();
nst.put(Some(NS_XML_PREFIX.to_string()), NS_XML_URI.to_string());
nst.put(Some(NS_XMLNS_PREFIX.to_string()), NS_XMLNS_URI.to_string());
nst.put(None, NS_EMPTY_URI.to_string());
nst
}
#[inline]
pub fn push_empty(&mut self) {
self.0.push(Namespace::empty());
}
#[inline]
pub fn pop(&mut self) -> Namespace {
self.0.pop().unwrap()
}
#[inline]
pub fn peek<'a>(&'a mut self) -> &'a mut Namespace {
self.0.last_mut().unwrap()
}
#[inline]
pub fn put(&mut self, prefix: Option<String>, uri: String) -> bool {
self.0.last_mut().unwrap().put(prefix, uri)
}
#[inline]
pub fn get<'a>(&'a self, prefix: &Option<String>) -> Option<&'a str> {
for ns in self.0.iter().rev() {
match ns.get(prefix) {
None => {},
r => return r,
}
}
None
}
pub fn squash(&self) -> Namespace {
let mut result = HashMap::new();
for ns in self.0.iter() {
result.extend(ns.0.iter().cloned_pairwise());
}
Namespace(result)
}
}
pub struct NamespaceStackMappings<'a> {
namespaces: Rev<Iter<'a, Namespace>>,
current_namespace: Option<NamespaceMappings<'a>>,
used_keys: HashSet<Option<&'a str>>
}
impl<'a> NamespaceStackMappings<'a> {
fn to_next_namespace(&mut self) -> bool {
self.current_namespace = self.namespaces.next().map(|ns| ns.uri_mappings());
self.current_namespace.is_some()
}
}
impl<'a> Iterator for NamespaceStackMappings<'a> {
type Item = (Option<&'a str>, &'a str);
fn next(&mut self) -> Option<(Option<&'a str>, &'a str)> {
if self.current_namespace.is_none() && !self.to_next_namespace() {
return None;
}
let next_item = self.current_namespace.as_mut().unwrap().next();
match next_item {
Some((k, v)) => if self.used_keys.contains(&k) {
self.next()
} else {
self.used_keys.insert(k);
Some((k, v))
},
None => if self.to_next_namespace() {
self.next()
} else {
None
}
}
}
}
impl<'a> NamespaceIterable<'a> for NamespaceStack {
type Iter = NamespaceStackMappings<'a>;
fn uri_mappings(&'a self) -> NamespaceStackMappings<'a> {
NamespaceStackMappings {
namespaces: self.0.iter().rev(),
current_namespace: None,
used_keys: HashSet::new()
}
}
}