babbel_xml 0.3.0

Full-featured XML parser, validator, stringifier, and XPath 1.0 engine in pure 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! XML element and attribute parsing implementation.

use crate::alloc_prelude::*;
use crate::document::Document;
use crate::error::{Result, XmlError};
use crate::io::{is_valid_xml_char, is_xml_name_char, is_xml_name_start};
use crate::namespace::QName;
use crate::node::{Attribute, NodeId, NodeKind};

use super::XmlParser;

impl<'a> XmlParser<'a> {
    /// Parses a single XML element tag, its attributes, and child content recursively.
    pub(crate) fn parse_element(
        &mut self,
        doc: &mut Document,
        parent_id: NodeId,
        depth: usize,
    ) -> Result<()> {
        self.namespace_scope.push_scope();
        let res = self.parse_element_internal(doc, parent_id, depth);
        self.namespace_scope.pop_scope();
        res
    }

    fn parse_element_internal(
        &mut self,
        doc: &mut Document,
        parent_id: NodeId,
        depth: usize,
    ) -> Result<()> {
        self.options.check_nesting_depth(depth)?;

        self.element_count += 1;
        self.options.check_element_count(self.element_count)?;

        if !self.source.consume("<") {
            return Err(XmlError::SyntaxError {
                message: "Expected '<' at start of element".into(),
                line: self.source.line(),
                col: self.source.col(),
            });
        }

        let name = self.parse_name()?;
        if name.is_empty() {
            return Err(XmlError::SyntaxError {
                message: "Empty element tag name".into(),
                line: self.source.line(),
                col: self.source.col(),
            });
        }

        // Namespaces in XML 1.0 ยง3: Elements must not have the prefix 'xmlns'
        if name.starts_with("xmlns:") {
            return Err(XmlError::SyntaxError {
                message: format!("Element <{name}> must not have prefix 'xmlns'"),
                line: self.source.line(),
                col: self.source.col(),
            });
        }

        self.source.skip_whitespace();
        let mut attributes = Vec::new();

        while !self.source.is_eof()
            && !self.source.starts_with(">")
            && !self.source.starts_with("/>")
        {
            let (attr_name, attr_value) = self.parse_attribute()?;

            // Check duplicate attribute error
            if attributes.iter().any(|a: &Attribute| *a.name == attr_name) {
                return Err(XmlError::SyntaxError {
                    message: format!("Duplicate attribute '{attr_name}' on element <{name}>"),
                    line: self.source.line(),
                    col: self.source.col(),
                });
            }

            // Register namespaces declared on this element
            if self.options.namespace_aware {
                if attr_name == "xmlns" {
                    self.namespace_scope.declare(None, attr_value.trim());
                } else if let Some(prefix) = attr_name.strip_prefix("xmlns:") {
                    self.namespace_scope
                        .declare(Some(prefix), attr_value.trim());
                }
            }

            attributes.push(Attribute::new(attr_name, attr_value));

            self.total_attribute_count += 1;
            self.options.check_attribute_count(attributes.len())?;
            self.options
                .check_total_attribute_count(self.total_attribute_count)?;

            let ws = self.source.skip_whitespace();
            if self.source.starts_with(">") || self.source.starts_with("/>") {
                break;
            }
            if ws == 0 {
                return Err(XmlError::SyntaxError {
                    message: "Whitespace required between attributes in element tag".into(),
                    line: self.source.line(),
                    col: self.source.col(),
                });
            }
        }

        // Validate namespace prefixes and collisions
        if self.options.namespace_aware {
            // Check element prefix
            let (elem_pfx, _) = QName::split_prefix(&name);
            if let Some(pfx) = elem_pfx {
                if self.namespace_scope.resolve_prefix(Some(pfx)).is_none() {
                    return Err(XmlError::SyntaxError {
                        message: format!("Undeclared element namespace prefix '{pfx}'"),
                        line: self.source.line(),
                        col: self.source.col(),
                    });
                }
            }

            // Check attribute prefixes and attribute namespace collisions
            let mut resolved_attrs: Vec<(Option<String>, String)> = Vec::new();
            for attr in &attributes {
                let (attr_pfx, local) = QName::split_prefix(&attr.name);
                let uri_opt = if let Some(pfx) = attr_pfx {
                    if pfx == "xmlns" {
                        None
                    } else {
                        match self.namespace_scope.resolve_prefix(Some(pfx)) {
                            Some(u) => Some(u.to_string()),
                            None => {
                                return Err(XmlError::SyntaxError {
                                    message: format!(
                                        "Undeclared attribute namespace prefix '{pfx}'"
                                    ),
                                    line: self.source.line(),
                                    col: self.source.col(),
                                });
                            }
                        }
                    }
                } else {
                    None
                };

                if let Some(uri) = &uri_opt {
                    if resolved_attrs
                        .iter()
                        .any(|(u, l)| u.as_deref() == Some(uri.as_str()) && l == local)
                    {
                        return Err(XmlError::SyntaxError {
                            message: format!(
                                "Attribute collision: multiple attributes with namespace '{uri}' and local name '{local}'"
                            ),
                            line: self.source.line(),
                            col: self.source.col(),
                        });
                    }
                }
                resolved_attrs.push((uri_opt, local.to_string()));
            }
        }

        let elem_id = doc.add_node(NodeKind::Element {
            name: name.clone().into_boxed_str(),
            attributes,
        });
        doc.append_child(parent_id, elem_id)?;

        if self.source.consume("/>") {
            return Ok(());
        }

        if !self.source.consume(">") {
            return Err(XmlError::SyntaxError {
                message: format!("Expected '>' or '/>' for element <{name}>"),
                line: self.source.line(),
                col: self.source.col(),
            });
        }

        // Parse element body / child content
        loop {
            if self.source.is_eof() {
                return Err(XmlError::SyntaxError {
                    message: format!("Unclosed element <{name}>"),
                    line: self.source.line(),
                    col: self.source.col(),
                });
            }

            if self.source.starts_with("</") {
                self.source.consume("</");
                let end_name = self.parse_name()?;
                self.source.skip_whitespace();

                if end_name != name {
                    return Err(XmlError::SyntaxError {
                        message: format!(
                            "Mismatched closing tag: expected </{name}>, found </{end_name}>"
                        ),
                        line: self.source.line(),
                        col: self.source.col(),
                    });
                }

                if !self.source.consume(">") {
                    return Err(XmlError::SyntaxError {
                        message: format!("Expected '>' after closing tag </{name}>"),
                        line: self.source.line(),
                        col: self.source.col(),
                    });
                }

                break;
            } else if self.source.starts_with("<![CDATA[") {
                let cdata_id = self.parse_cdata(doc)?;
                doc.append_child(elem_id, cdata_id)?;
            } else if self.source.starts_with("<!--") {
                let comment_id = self.parse_comment(doc)?;
                doc.append_child(elem_id, comment_id)?;
            } else if self.source.starts_with("<?") {
                let pi_id = self.parse_pi(doc)?;
                doc.append_child(elem_id, pi_id)?;
            } else if self.source.starts_with("<") {
                self.parse_element(doc, elem_id, depth + 1)?;
            } else {
                let text_id = self.parse_text(doc)?;
                if let Some(t_node) = doc.get_node(text_id) {
                    if let NodeKind::Text(t) = &t_node.kind {
                        if !t.is_empty() {
                            doc.append_child(elem_id, text_id)?;
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Parses attribute key-value pair (`key="value"`).
    pub(crate) fn parse_attribute(&mut self) -> Result<(String, String)> {
        let key = self.parse_name()?;
        if key.is_empty() {
            return Err(XmlError::SyntaxError {
                message: "Attribute name cannot be empty".into(),
                line: self.source.line(),
                col: self.source.col(),
            });
        }
        self.source.skip_whitespace();

        if !self.source.consume("=") {
            return Err(XmlError::SyntaxError {
                message: format!("Expected '=' after attribute '{key}'"),
                line: self.source.line(),
                col: self.source.col(),
            });
        }

        self.source.skip_whitespace();
        let quote = self
            .source
            .next_char()
            .ok_or_else(|| XmlError::SyntaxError {
                message: format!("Expected quote after '=' for attribute '{key}'"),
                line: self.source.line(),
                col: self.source.col(),
            })?;

        if quote != '"' && quote != '\'' {
            return Err(XmlError::SyntaxError {
                message: format!("Invalid attribute quote character '{quote}'"),
                line: self.source.line(),
                col: self.source.col(),
            });
        }

        let mut raw_val = String::new();
        while let Some(ch) = self.source.next_char() {
            if ch == '<' {
                return Err(XmlError::SyntaxError {
                    message: format!("Unescaped '<' is forbidden in attribute value for '{key}'"),
                    line: self.source.line(),
                    col: self.source.col(),
                });
            }
            if !is_valid_xml_char(ch) {
                return Err(XmlError::SyntaxError {
                    message: format!(
                        "Forbidden XML character '\\u{{{:x}}}' in attribute value",
                        ch as u32
                    ),
                    line: self.source.line(),
                    col: self.source.col(),
                });
            }
            if ch == quote {
                self.check_attribute_entities(&raw_val)?;
                let expanded_val = self.entity_mapper.expand(&raw_val)?;

                // Namespaces in XML 1.0 validation checks
                if key == "xmlns" {
                    if expanded_val == "http://www.w3.org/XML/1998/namespace" {
                        return Err(XmlError::SyntaxError {
                            message:
                                "The xml namespace must not be declared as the default namespace"
                                    .into(),
                            line: self.source.line(),
                            col: self.source.col(),
                        });
                    }
                    if expanded_val == "http://www.w3.org/2000/xmlns/" {
                        return Err(XmlError::SyntaxError {
                            message: "The xmlns namespace must not be declared as a namespace"
                                .into(),
                            line: self.source.line(),
                            col: self.source.col(),
                        });
                    }
                } else if key == "xmlns:xml" {
                    if expanded_val != "http://www.w3.org/XML/1998/namespace" {
                        return Err(XmlError::SyntaxError {
                            message: "The prefix 'xml' can only be bound to 'http://www.w3.org/XML/1998/namespace'".into(),
                            line: self.source.line(),
                            col: self.source.col(),
                        });
                    }
                } else if key == "xmlns:xmlns" {
                    return Err(XmlError::SyntaxError {
                        message: "The prefix 'xmlns' must not be declared".into(),
                        line: self.source.line(),
                        col: self.source.col(),
                    });
                } else if key.starts_with("xmlns:") {
                    if expanded_val == "http://www.w3.org/XML/1998/namespace" {
                        return Err(XmlError::SyntaxError {
                            message: "The xml namespace must not be bound to any other prefix"
                                .into(),
                            line: self.source.line(),
                            col: self.source.col(),
                        });
                    }
                    if expanded_val == "http://www.w3.org/2000/xmlns/" {
                        return Err(XmlError::SyntaxError {
                            message: "Prefix cannot be bound to the xmlns namespace".into(),
                            line: self.source.line(),
                            col: self.source.col(),
                        });
                    }
                    if expanded_val.is_empty() {
                        return Err(XmlError::SyntaxError {
                            message:
                                "Empty namespace URI is illegal for prefixed namespace in XML 1.0"
                                    .into(),
                            line: self.source.line(),
                            col: self.source.col(),
                        });
                    }
                }

                return Ok((key, expanded_val));
            }
            raw_val.push(ch);
        }

        Err(XmlError::SyntaxError {
            message: format!("Unterminated value for attribute '{key}'"),
            line: self.source.line(),
            col: self.source.col(),
        })
    }

    pub(crate) fn check_qname(&self, name: &str) -> Result<()> {
        if !self.options.namespace_aware {
            return Ok(());
        }
        let colons = name.matches(':').count();
        if colons > 1 {
            return Err(XmlError::SyntaxError {
                message: format!("QName '{name}' cannot contain multiple colons"),
                line: self.source.line(),
                col: self.source.col(),
            });
        }
        if name.starts_with(':') || name.ends_with(':') {
            return Err(XmlError::SyntaxError {
                message: format!("QName '{name}' cannot start or end with a colon"),
                line: self.source.line(),
                col: self.source.col(),
            });
        }
        Ok(())
    }

    /// Helper parsing an identifier / tag name string.
    pub(crate) fn parse_name(&mut self) -> Result<String> {
        let start = self.source.position();
        if let Some(ch) = self.source.peek() {
            if !is_xml_name_start(ch) {
                return Ok(String::new());
            }
            self.source.next_char();
        }
        while let Some(ch) = self.source.peek() {
            if is_xml_name_char(ch) {
                self.source.next_char();
            } else {
                break;
            }
        }
        let end = self.source.position();
        let name = self.source.slice_range(start, end).to_string();
        if !name.is_empty() {
            self.check_qname(&name)?;
        }
        Ok(name)
    }

    /// Helper parsing a single- or double-quoted string.
    pub(crate) fn parse_quoted_string(&mut self) -> Result<String> {
        let quote = self
            .source
            .next_char()
            .ok_or_else(|| XmlError::SyntaxError {
                message: "Expected quote for string literal".into(),
                line: self.source.line(),
                col: self.source.col(),
            })?;

        let mut s = String::new();
        while let Some(ch) = self.source.next_char() {
            if ch == quote {
                return Ok(s);
            }
            if !is_valid_xml_char(ch) {
                return Err(XmlError::SyntaxError {
                    message: format!(
                        "Forbidden XML character '\\u{{{:x}}}' in string literal",
                        ch as u32
                    ),
                    line: self.source.line(),
                    col: self.source.col(),
                });
            }
            s.push(ch);
        }

        Err(XmlError::SyntaxError {
            message: "Unterminated string literal".into(),
            line: self.source.line(),
            col: self.source.col(),
        })
    }
}