xot 0.31.2

Full-featured XML tree library for Rust
Documentation
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// when we render a element or attribute name, we need to render it with the
// right prefix information. we also want to keep track of prefix declarations.
// This data structure maintains this information and avoids having to wander
// the tree to get it.

use std::borrow::Cow;

use crate::{Error, NameId, NamespaceId, PrefixId, Xot};

pub(crate) type NamespaceDeclarations = Vec<(PrefixId, NamespaceId)>;

#[derive(Debug)]
struct FullnameInfo {
    all_namespaces: NamespaceDeclarations,
}

impl FullnameInfo {
    fn new(node_namespaces: NamespaceDeclarations, current_fullname_info: &FullnameInfo) -> Self {
        let current_namespaces = current_fullname_info
            .all_namespaces
            .iter()
            // filter out any overridden prefixes
            .filter(|(p, _)| !node_namespaces.iter().any(|(p2, _)| p2 == p));

        let all_namespaces = current_namespaces
            .chain(node_namespaces.iter())
            .copied()
            .collect();
        Self { all_namespaces }
    }

    fn prefixes_by_namespace(&self, namespace: NamespaceId) -> impl Iterator<Item = PrefixId> + '_ {
        self.all_namespaces
            .iter()
            .rev()
            .filter(move |(_, n)| *n == namespace)
            .map(|(p, _)| *p)
    }

    // look for the prefix. prefer the empty prefix, and if that isn't there, the
    // most recently defined prefix
    fn element_prefix_by_namespace(&self, xot: &Xot, namespace: NamespaceId) -> Option<PrefixId> {
        if self
            .prefixes_by_namespace(namespace)
            .any(|p| p == xot.empty_prefix())
        {
            Some(xot.empty_prefix())
        } else {
            self.prefixes_by_namespace(namespace).next()
        }
    }

    // look for the prefix, but only if it's not the empty prefix, as this is
    // for attributes which cannot be unprefixed and still in a namespace
    fn attribute_prefix_by_namespace(&self, xot: &Xot, namespace: NamespaceId) -> Option<PrefixId> {
        self.prefixes_by_namespace(namespace)
            .find(|&prefix| prefix != xot.empty_prefix())
    }
}

pub(crate) struct FullnameSerializer<'a> {
    xot: &'a Xot,
    stack: Vec<FullnameInfo>,
}

impl<'a> FullnameSerializer<'a> {
    pub(crate) fn new(xot: &'a Xot, defined_namespaces: NamespaceDeclarations) -> Self {
        Self {
            xot,
            stack: vec![FullnameInfo {
                all_namespaces: defined_namespaces,
            }],
        }
    }

    pub(crate) fn push(&mut self, defined_namespaces: NamespaceDeclarations) {
        // optimization; we don't need to recalculate anything if we
        // already have the same namespaces. the cost is that we need
        // to keep track of whether this node defined namespaces for pop as well.
        if defined_namespaces.is_empty() {
            return;
        }
        let current_fullname_info = self.stack.last().unwrap();
        self.stack
            .push(FullnameInfo::new(defined_namespaces, current_fullname_info));
    }

    pub(crate) fn has_empty_prefix(&self, namespace_id: NamespaceId) -> bool {
        let prefix_id = self
            .top()
            .element_prefix_by_namespace(self.xot, namespace_id);
        if let Some(prefix_id) = prefix_id {
            prefix_id == self.xot.empty_prefix()
        } else {
            false
        }
    }

    // pub(crate) fn has_ancestor_empty_prefix(&self, namespace_id: NamespaceId) -> bool {
    //     if self.stack.len() < 2 {
    //         return false;
    //     }
    //     let prefix_id =
    //         self.stack[self.stack.len() - 2].element_prefix_by_namespace(self.xot, namespace_id);
    //     if let Some(prefix_id) = prefix_id {
    //         prefix_id == self.xot.empty_prefix()
    //     } else {
    //         false
    //     }
    // }

    // this is handy for the HTML rendering system, which insists some namespaces
    // should be in the empty prefix (xhtml, mathml, svg)
    pub(crate) fn add_empty_prefix(&mut self, namespace_id: NamespaceId) {
        let current_fullname_info = self.stack.last_mut().unwrap();
        let empty_entry = (self.xot.empty_prefix(), namespace_id);
        current_fullname_info.all_namespaces.push(empty_entry);
    }

    pub(crate) fn pop(&mut self, has_namespaces: bool) {
        if has_namespaces {
            self.stack.pop();
        }
    }

    fn top(&self) -> &FullnameInfo {
        self.stack.last().unwrap()
    }

    pub(crate) fn element_prefix(&self, name_id: NameId) -> Result<Option<PrefixId>, Error> {
        let namespace_id = self.xot.namespace_for_name(name_id);
        if namespace_id == self.xot.no_namespace_id {
            // no namespace, therefore no prefix to show
            Ok(None)
        } else {
            let prefix_id = self
                .top()
                .element_prefix_by_namespace(self.xot, namespace_id);
            if let Some(prefix_id) = prefix_id {
                if prefix_id == self.xot.empty_prefix() {
                    // empty prefix, therefore default namespace
                    Ok(None)
                } else {
                    // a prefix
                    Ok(Some(prefix_id))
                }
            } else {
                // missing prefix
                Err(Error::MissingPrefix(
                    self.xot.namespace_str(namespace_id).to_string(),
                ))
            }
        }
    }

    // get the fullname. if None, we cannot generate the fullname due to a missing
    // prefix
    pub(crate) fn element_fullname(&self, name_id: NameId) -> Result<Cow<'a, str>, Error> {
        if let Some(prefix) = self.element_prefix(name_id)? {
            Ok(Cow::Owned(format!(
                "{}:{}",
                self.xot.prefix_str(prefix),
                self.xot.local_name_str(name_id)
            )))
        } else {
            Ok(Cow::Borrowed(self.xot.local_name_str(name_id)))
        }
    }

    pub(crate) fn attribute_prefix(&self, name_id: NameId) -> Result<Option<PrefixId>, Error> {
        let namespace_id = self.xot.namespace_for_name(name_id);
        if namespace_id == self.xot.no_namespace_id {
            // no namespace, therefore no prefix to show
            Ok(None)
        } else {
            let prefix_id = self
                .top()
                .attribute_prefix_by_namespace(self.xot, namespace_id);
            if let Some(prefix_id) = prefix_id {
                // a prefix, which cannot be empty
                Ok(Some(prefix_id))
            } else {
                // missing prefix
                Err(Error::MissingPrefix(
                    self.xot.namespace_str(namespace_id).to_string(),
                ))
            }
        }
    }

    pub(crate) fn attribute_fullname(&self, name_id: NameId) -> Result<Cow<'a, str>, Error> {
        if let Some(prefix) = self.attribute_prefix(name_id)? {
            Ok(Cow::Owned(format!(
                "{}:{}",
                self.xot.prefix_str(prefix),
                self.xot.local_name_str(name_id)
            )))
        } else {
            Ok(Cow::Borrowed(self.xot.local_name_str(name_id)))
        }
    }

    pub(crate) fn is_namespace_known(&self, namespace_id: NamespaceId) -> bool {
        self.top()
            .all_namespaces
            .iter()
            .any(|(_, ns)| *ns == namespace_id)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_element_no_namespace() {
        let mut xot = Xot::new();

        let a = xot.add_name("a");
        let fullname_serializer = FullnameSerializer::new(&xot, vec![]);

        assert_eq!(
            fullname_serializer.element_fullname(a).unwrap(),
            Cow::Borrowed("a")
        );
        assert_eq!(fullname_serializer.element_prefix(a).unwrap(), None)
    }

    #[test]
    fn test_attribute_no_namespace() {
        let mut xot = Xot::new();

        let a = xot.add_name("a");
        let fullname_serializer = FullnameSerializer::new(&xot, vec![]);

        assert_eq!(
            fullname_serializer.attribute_fullname(a).unwrap(),
            Cow::Borrowed("a")
        );
        assert_eq!(fullname_serializer.element_prefix(a).unwrap(), None)
    }

    #[test]
    fn test_element_with_prefix() {
        let mut xot = Xot::new();

        let ns = xot.add_namespace("ns");
        let a = xot.add_name_ns("a", ns);
        let prefix = xot.add_prefix("p");
        let fullname_serializer = FullnameSerializer::new(&xot, vec![(prefix, ns)]);

        assert_eq!(
            fullname_serializer.element_fullname(a).unwrap(),
            Cow::Owned::<str>("p:a".to_string())
        );
        assert_eq!(fullname_serializer.element_prefix(a).unwrap(), Some(prefix))
    }

    #[test]
    fn test_attribute_with_prefix() {
        let mut xot = Xot::new();

        let ns = xot.add_namespace("ns");
        let a = xot.add_name_ns("a", ns);
        let prefix = xot.add_prefix("p");
        let fullname_serializer = FullnameSerializer::new(&xot, vec![(prefix, ns)]);

        assert_eq!(
            fullname_serializer.attribute_fullname(a).unwrap(),
            Cow::Owned::<str>("p:a".to_string())
        );
        assert_eq!(fullname_serializer.element_prefix(a).unwrap(), Some(prefix))
    }

    #[test]
    fn test_element_default_namespace() {
        let mut xot = Xot::new();

        let ns = xot.add_namespace("ns");
        let a = xot.add_name_ns("a", ns);
        let fullname_serializer = FullnameSerializer::new(&xot, vec![(xot.empty_prefix(), ns)]);

        assert_eq!(
            fullname_serializer.element_fullname(a).unwrap(),
            Cow::Borrowed("a")
        );
        assert_eq!(fullname_serializer.element_prefix(a).unwrap(), None)
    }

    #[test]
    fn test_element_default_namespace_empty_prefix_preferred() {
        let mut xot = Xot::new();

        let ns = xot.add_namespace("ns");
        let a = xot.add_name_ns("a", ns);
        let p = xot.add_prefix("p");
        let fullname_serializer =
            FullnameSerializer::new(&xot, vec![(xot.empty_prefix(), ns), (p, ns)]);

        assert_eq!(
            fullname_serializer.element_fullname(a).unwrap(),
            Cow::Borrowed("a")
        );
        assert_eq!(fullname_serializer.element_prefix(a).unwrap(), None)
    }

    #[test]
    fn test_element_most_recently_defined_prefix_preferred() {
        let mut xot = Xot::new();

        let ns = xot.add_namespace("ns");
        let a = xot.add_name_ns("a", ns);
        let p1 = xot.add_prefix("p1");
        let p2 = xot.add_prefix("p2");
        let fullname_serializer = FullnameSerializer::new(&xot, vec![(p1, ns), (p2, ns)]);

        assert_eq!(
            fullname_serializer.element_fullname(a).unwrap(),
            Cow::Owned::<str>("p2:a".to_string())
        );
        assert_eq!(fullname_serializer.element_prefix(a).unwrap(), Some(p2))
    }

    #[test]
    fn test_element_missing_prefix() {
        let mut xot = Xot::new();

        let ns = xot.add_namespace("ns");
        let a = xot.add_name_ns("a", ns);
        let fullname_serializer = FullnameSerializer::new(&xot, vec![]);

        assert!(fullname_serializer.element_fullname(a).is_err());
        assert!(fullname_serializer.element_prefix(a).is_err());
    }

    #[test]
    fn test_attribute_explicit_prefix_used_over_empty_prefix() {
        let mut xot = Xot::new();

        let ns = xot.add_namespace("ns");
        let a = xot.add_name_ns("a", ns);
        let p = xot.add_prefix("p");
        let fullname_serializer =
            FullnameSerializer::new(&xot, vec![(xot.empty_prefix(), ns), (p, ns)]);

        assert_eq!(
            fullname_serializer.attribute_fullname(a).unwrap(),
            Cow::Owned::<str>("p:a".to_string())
        );
        assert_eq!(fullname_serializer.attribute_prefix(a).unwrap(), Some(p));
    }

    #[test]
    fn test_attribute_explicit_prefix_used_over_empty_prefix2() {
        let mut xot = Xot::new();

        let ns = xot.add_namespace("ns");
        let a = xot.add_name_ns("a", ns);
        let p = xot.add_prefix("p");
        let fullname_serializer =
            FullnameSerializer::new(&xot, vec![(p, ns), (xot.empty_prefix(), ns)]);

        assert_eq!(
            fullname_serializer.attribute_fullname(a).unwrap(),
            Cow::Owned::<str>("p:a".to_string())
        );
        assert_eq!(fullname_serializer.attribute_prefix(a).unwrap(), Some(p));
    }

    #[test]
    fn test_attribute_without_explicit_prefix_default_not_found() {
        let mut xot = Xot::new();

        let ns = xot.add_namespace("ns");
        let a = xot.add_name_ns("a", ns);

        let fullname_serializer = FullnameSerializer::new(&xot, vec![(xot.empty_prefix(), ns)]);

        assert!(fullname_serializer.attribute_fullname(a).is_err());
        assert!(fullname_serializer.attribute_prefix(a).is_err());
    }

    #[test]
    fn test_overridden_prefix() {
        let mut xot = Xot::new();

        let ns1 = xot.add_namespace("ns1");
        let ns2 = xot.add_namespace("ns2");
        let a1 = xot.add_name_ns("a", ns1);
        let a2 = xot.add_name_ns("a", ns2);

        let p = xot.add_prefix("p");
        // override the prefix p so that ns1 isn't accessible anymore
        let mut fullname_serializer = FullnameSerializer::new(&xot, vec![(p, ns1)]);
        fullname_serializer.push(vec![(p, ns2)]);

        assert_eq!(
            fullname_serializer.element_fullname(a2).unwrap(),
            Cow::Owned::<str>("p:a".to_string())
        );
        assert!(fullname_serializer.attribute_fullname(a1).is_err());
        assert!(fullname_serializer.attribute_prefix(a1).is_err());
    }
}