babbel_xml 0.2.2

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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! # XSD Schema Validator
//!
//! Parses XSD schema documents (`xs:schema`) and validates DOM documents against elements, types, and restriction facets.

use crate::alloc_prelude::*;
use crate::document::Document;
use crate::error::{Result, XmlError};
use crate::node::{NodeId, NodeKind};
use crate::validator::XmlValidator;

#[cfg(not(feature = "std"))]
use alloc::collections::BTreeMap as HashMap;
#[cfg(feature = "std")]
use std::collections::HashMap;

/// XSD simple type restriction facets.
#[derive(Debug, Clone, Default)]
pub struct XsdRestriction {
    /// Minimum inclusive numeric bound (`xs:minInclusive`).
    pub min_inclusive: Option<i64>,
    /// Maximum inclusive numeric bound (`xs:maxInclusive`).
    pub max_inclusive: Option<i64>,
    /// Minimum string character length (`xs:minLength`).
    pub min_length: Option<usize>,
    /// Maximum string character length (`xs:maxLength`).
    pub max_length: Option<usize>,
    /// Allowed enumeration values (`xs:enumeration`).
    pub enumerations: Vec<String>,
    /// Pattern regex/string restriction (`xs:pattern`).
    pub pattern: Option<String>,
}

/// Model group compositors supported by XSD.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Compositor {
    /// `<xs:sequence>`
    Sequence,
    /// `<xs:choice>`
    Choice,
    /// `<xs:all>`
    All,
}

/// Attribute declaration rule in XSD.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct XsdAttributeRule {
    /// Attribute name.
    pub name: String,
    /// Attribute type (`xs:string`, `xs:integer`, etc.).
    pub attr_type: String,
    /// Whether attribute is required (`use="required"`).
    pub required: bool,
    /// Default literal value if any.
    pub default: Option<String>,
}

/// Complex type definition containing compositors, child elements, and attributes.
#[derive(Debug, Clone, Default)]
pub struct XsdComplexType {
    /// Type name (empty for anonymous/inline complex types).
    pub name: String,
    /// Model compositor (`Sequence`, `Choice`, or `All`).
    pub compositor: Option<Compositor>,
    /// Child element rules within the compositor.
    pub elements: Vec<XsdElementRule>,
    /// Attribute rules on the element.
    pub attributes: Vec<XsdAttributeRule>,
}

/// XSD schema element declaration rule.
#[derive(Debug, Clone)]
pub struct XsdElementRule {
    /// Element name.
    pub name: String,
    /// Type declaration (`xs:string`, `xs:integer`, `xs:boolean`, or complex type name).
    pub elem_type: String,
    /// Minimum occurrences.
    pub min_occurs: usize,
    /// Maximum occurrences (None = unbounded).
    pub max_occurs: Option<usize>,
    /// Associated restriction facets.
    pub restriction: XsdRestriction,
    /// Associated inline or referenced complex type.
    pub complex_type: Option<XsdComplexType>,
}

/// XSD Schema Validator.
///
/// # Examples
///
/// ```
/// use babbel_xml::{parse, XsdValidator};
///
/// let schema = r#"
/// <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
///   <xs:element name="note" type="xs:string"/>
/// </xs:schema>
/// "#;
/// let mut validator = XsdValidator::new();
/// validator.parse_schema(schema).unwrap();
/// let doc = parse("<note>hello</note>").unwrap();
/// assert!(validator.validate(&doc).is_ok());
/// ```
#[derive(Debug, Clone, Default)]
pub struct XsdValidator {
    elements: HashMap<String, XsdElementRule>,
    complex_types: HashMap<String, XsdComplexType>,
}

impl XsdValidator {
    /// Instantiates a new [`XsdValidator`].
    pub fn new() -> Self {
        Self::default()
    }

    /// Parses an XSD schema string slice (`<xs:schema ...>`).
    pub fn parse_schema(&mut self, schema_xml: &str) -> Result<()> {
        let source = crate::io::source::XmlSource::from_string(schema_xml);
        let options = crate::options::ParseOptions::default();
        let mut parser = crate::parser::XmlParser::new(source, options);
        let schema_doc = parser.parse()?;

        if let Some(root_elem_id) = schema_doc.root_element_id() {
            self.collect_schema_components(&schema_doc, root_elem_id);
        }
        Ok(())
    }

    fn parse_occurs(min_str: Option<&str>, max_str: Option<&str>) -> (usize, Option<usize>) {
        let min = min_str.and_then(|s| s.parse::<usize>().ok()).unwrap_or(1);
        let max = match max_str {
            Some("unbounded") => None,
            Some(s) => s.parse::<usize>().ok(),
            None => Some(1),
        };
        (min, max)
    }

    fn collect_schema_components(&mut self, schema_doc: &Document, root_id: NodeId) {
        if let Some(root_node) = schema_doc.get_node(root_id) {
            for &c_id in &root_node.children {
                if let Some(c) = schema_doc.get_node(c_id) {
                    if let NodeKind::Element { name, .. } = &c.kind {
                        let local = name.split(':').last().unwrap_or(name);
                        if local == "complexType" {
                            if let Some(ct_name) = schema_doc.get_attribute(c_id, "name") {
                                let mut ct = self.parse_complex_type(schema_doc, c_id);
                                ct.name = ct_name.to_string();
                                for elem in &ct.elements {
                                    self.elements
                                        .entry(elem.name.clone())
                                        .or_insert_with(|| elem.clone());
                                }
                                self.complex_types.insert(ct_name.to_string(), ct);
                            }
                        } else if local == "element" {
                            let rule = self.parse_element_node(schema_doc, c_id);
                            if !rule.name.is_empty() {
                                if let Some(ct) = &rule.complex_type {
                                    for elem in &ct.elements {
                                        self.elements
                                            .entry(elem.name.clone())
                                            .or_insert_with(|| elem.clone());
                                    }
                                }
                                self.elements.insert(rule.name.clone(), rule);
                            }
                        }
                    }
                }
            }
        }
    }

    fn parse_element_node(&self, schema_doc: &Document, node_id: NodeId) -> XsdElementRule {
        let elem_name = schema_doc
            .get_attribute(node_id, "name")
            .unwrap_or_default()
            .to_string();
        let elem_type = schema_doc
            .get_attribute(node_id, "type")
            .unwrap_or("")
            .to_string();
        let min_str = schema_doc.get_attribute(node_id, "minOccurs");
        let max_str = schema_doc.get_attribute(node_id, "maxOccurs");
        let (min_occurs, max_occurs) = Self::parse_occurs(min_str, max_str);

        let restriction = self.extract_restriction(schema_doc, node_id);

        let mut complex_type = None;
        if let Some(node) = schema_doc.get_node(node_id) {
            for &c_id in &node.children {
                if let Some(c) = schema_doc.get_node(c_id) {
                    if let NodeKind::Element { name, .. } = &c.kind {
                        if name.split(':').last().unwrap_or(name) == "complexType" {
                            complex_type = Some(self.parse_complex_type(schema_doc, c_id));
                            break;
                        }
                    }
                }
            }
        }

        XsdElementRule {
            name: elem_name,
            elem_type: if elem_type.is_empty() {
                "xs:string".to_string()
            } else {
                elem_type
            },
            min_occurs,
            max_occurs,
            restriction,
            complex_type,
        }
    }

    fn parse_complex_type(&self, schema_doc: &Document, ct_id: NodeId) -> XsdComplexType {
        let mut ct = XsdComplexType::default();

        if let Some(node) = schema_doc.get_node(ct_id) {
            for &c_id in &node.children {
                if let Some(c) = schema_doc.get_node(c_id) {
                    if let NodeKind::Element { name, .. } = &c.kind {
                        let local = name.split(':').last().unwrap_or(name);
                        match local {
                            "sequence" => {
                                ct.compositor = Some(Compositor::Sequence);
                                ct.elements = self.collect_group_elements(schema_doc, c_id);
                            }
                            "choice" => {
                                ct.compositor = Some(Compositor::Choice);
                                ct.elements = self.collect_group_elements(schema_doc, c_id);
                            }
                            "all" => {
                                ct.compositor = Some(Compositor::All);
                                ct.elements = self.collect_group_elements(schema_doc, c_id);
                            }
                            "attribute" => {
                                ct.attributes
                                    .push(self.parse_attribute_node(schema_doc, c_id));
                            }
                            _ => {}
                        }
                    }
                }
            }
        }

        ct
    }

    fn collect_group_elements(
        &self,
        schema_doc: &Document,
        group_id: NodeId,
    ) -> Vec<XsdElementRule> {
        let mut elements = Vec::new();
        if let Some(node) = schema_doc.get_node(group_id) {
            for &c_id in &node.children {
                if let Some(c) = schema_doc.get_node(c_id) {
                    if let NodeKind::Element { name, .. } = &c.kind {
                        if name.split(':').last().unwrap_or(name) == "element" {
                            elements.push(self.parse_element_node(schema_doc, c_id));
                        }
                    }
                }
            }
        }
        elements
    }

    fn parse_attribute_node(&self, schema_doc: &Document, attr_id: NodeId) -> XsdAttributeRule {
        let name = schema_doc
            .get_attribute(attr_id, "name")
            .unwrap_or_default()
            .to_string();
        let attr_type = schema_doc
            .get_attribute(attr_id, "type")
            .unwrap_or("xs:string")
            .to_string();
        let use_val = schema_doc
            .get_attribute(attr_id, "use")
            .unwrap_or("optional");
        let default = schema_doc
            .get_attribute(attr_id, "default")
            .map(|s| s.to_string());

        XsdAttributeRule {
            name,
            attr_type,
            required: use_val == "required",
            default,
        }
    }

    fn extract_restriction(&self, doc: &Document, elem_id: NodeId) -> XsdRestriction {
        let mut rest = XsdRestriction::default();
        let mut stack = vec![elem_id];

        while let Some(nid) = stack.pop() {
            if let Some(node) = doc.get_node(nid) {
                if let NodeKind::Element { name, .. } = &node.kind {
                    let local_name = name.split(':').last().unwrap_or(name);
                    let get_val = || doc.get_attribute(nid, "value");

                    match local_name {
                        "minInclusive" => {
                            if let Some(v) = get_val().and_then(|s| s.parse::<i64>().ok()) {
                                rest.min_inclusive = Some(v);
                            }
                        }
                        "maxInclusive" => {
                            if let Some(v) = get_val().and_then(|s| s.parse::<i64>().ok()) {
                                rest.max_inclusive = Some(v);
                            }
                        }
                        "minLength" => {
                            if let Some(v) = get_val().and_then(|s| s.parse::<usize>().ok()) {
                                rest.min_length = Some(v);
                            }
                        }
                        "maxLength" => {
                            if let Some(v) = get_val().and_then(|s| s.parse::<usize>().ok()) {
                                rest.max_length = Some(v);
                            }
                        }
                        "enumeration" => {
                            if let Some(v) = get_val() {
                                rest.enumerations.push(v.to_string());
                            }
                        }
                        "pattern" => {
                            if let Some(v) = get_val() {
                                rest.pattern = Some(v.to_string());
                            }
                        }
                        _ => {}
                    }
                }
                stack.extend(node.children.iter().copied());
            }
        }
        rest
    }

    /// Maximum element nesting depth allowed during validation (512 frames).
    pub const MAX_VALIDATION_DEPTH: usize = 512;

    /// Validates a DOM [`Document`] against loaded XSD schema rules.
    pub fn validate(&self, doc: &Document) -> Result<()> {
        if let Some(root_id) = doc.root_element_id() {
            self.validate_element(doc, root_id, 0)?;
        }
        Ok(())
    }

    fn validate_element(&self, doc: &Document, elem_id: NodeId, depth: usize) -> Result<()> {
        if depth > Self::MAX_VALIDATION_DEPTH {
            return Err(XmlError::XsdError(
                "Document exceeds maximum validation nesting depth (512)".into(),
            ));
        }
        let node = doc
            .get_node(elem_id)
            .ok_or_else(|| XmlError::XsdError("Invalid node".into()))?;

        if let NodeKind::Element { name, .. } = &node.kind {
            if let Some(rule) = self.elements.get(&**name) {
                // Resolve complex type
                let ct_opt = rule
                    .complex_type
                    .as_ref()
                    .or_else(|| self.complex_types.get(&rule.elem_type));

                if let Some(ct) = ct_opt {
                    // Attribute validation
                    for attr_rule in &ct.attributes {
                        let attr_val = doc.get_attribute(elem_id, &attr_rule.name);
                        if attr_rule.required && attr_val.is_none() {
                            return Err(XmlError::XsdError(format!(
                                "Required attribute '{}' missing on <{name}>",
                                attr_rule.name
                            )));
                        }
                        if let Some(val) = attr_val {
                            if attr_rule.attr_type == "xs:integer" && val.parse::<i64>().is_err() {
                                return Err(XmlError::XsdError(format!(
                                    "Attribute '{}' on <{name}> value '{val}' is not a valid integer",
                                    attr_rule.name
                                )));
                            }
                        }
                    }

                    // Child elements and compositor validation
                    let child_elements: Vec<(NodeId, String)> = node
                        .children
                        .iter()
                        .filter_map(|&c_id| {
                            doc.get_node(c_id).and_then(|c| match &c.kind {
                                NodeKind::Element {
                                    name: child_name, ..
                                } => Some((c_id, child_name.to_string())),
                                _ => None,
                            })
                        })
                        .collect();

                    match ct.compositor {
                        Some(Compositor::Choice) => {
                            if child_elements.is_empty()
                                && ct.elements.iter().any(|e| e.min_occurs > 0)
                            {
                                return Err(XmlError::XsdError(format!(
                                    "Element <{name}> requires at least one choice child element"
                                )));
                            }
                            for (_, c_name) in &child_elements {
                                if !ct.elements.iter().any(|e| e.name == *c_name) {
                                    return Err(XmlError::XsdError(format!(
                                        "Child element <{c_name}> is not allowed in choice group of <{name}>"
                                    )));
                                }
                            }
                        }
                        Some(Compositor::Sequence) => {
                            for expected in &ct.elements {
                                let count = child_elements
                                    .iter()
                                    .filter(|(_, cn)| *cn == expected.name)
                                    .count();
                                if count < expected.min_occurs {
                                    return Err(XmlError::XsdError(format!(
                                        "Element <{name}> missing required sequence child <{}> (expected {}, found {})",
                                        expected.name, expected.min_occurs, count
                                    )));
                                }
                                if let Some(max) = expected.max_occurs {
                                    if count > max {
                                        return Err(XmlError::XsdError(format!(
                                            "Element <{name}> exceeds maxOccurs ({max}) for child <{}> (found {count})",
                                            expected.name
                                        )));
                                    }
                                }
                            }
                        }
                        Some(Compositor::All) => {
                            for expected in &ct.elements {
                                let count = child_elements
                                    .iter()
                                    .filter(|(_, cn)| *cn == expected.name)
                                    .count();
                                if count < expected.min_occurs {
                                    return Err(XmlError::XsdError(format!(
                                        "Element <{name}> missing required child <{}> in xs:all group",
                                        expected.name
                                    )));
                                }
                                if count > expected.max_occurs.unwrap_or(1) {
                                    return Err(XmlError::XsdError(format!(
                                        "Element <{name}> exceeds maxOccurs for child <{}> in xs:all group",
                                        expected.name
                                    )));
                                }
                            }
                        }
                        None => {}
                    }
                }

                // Simple type restrictions
                let text = self.get_element_text(doc, elem_id);
                let trimmed = text.trim();

                // Primitive Type Validation
                if rule.elem_type == "xs:integer" || rule.elem_type == "xsd:integer" {
                    if !trimmed.is_empty() {
                        let parsed = trimmed.parse::<i64>().map_err(|_| {
                            XmlError::XsdError(format!(
                                "Element <{name}> value '{trimmed}' is not a valid integer"
                            ))
                        })?;

                        if let Some(min) = rule.restriction.min_inclusive {
                            if parsed < min {
                                return Err(XmlError::XsdError(format!(
                                    "Element <{name}> value '{parsed}' violates minInclusive ({min})"
                                )));
                            }
                        }
                        if let Some(max) = rule.restriction.max_inclusive {
                            if parsed > max {
                                return Err(XmlError::XsdError(format!(
                                    "Element <{name}> value '{parsed}' violates maxInclusive ({max})"
                                )));
                            }
                        }
                    }
                } else if rule.elem_type == "xs:boolean" || rule.elem_type == "xsd:boolean" {
                    if !trimmed.is_empty()
                        && trimmed != "true"
                        && trimmed != "false"
                        && trimmed != "1"
                        && trimmed != "0"
                    {
                        return Err(XmlError::XsdError(format!(
                            "Element <{name}> value '{trimmed}' is not a valid boolean"
                        )));
                    }
                }

                // String Restrictions
                if let Some(min_len) = rule.restriction.min_length {
                    if trimmed.chars().count() < min_len {
                        return Err(XmlError::XsdError(format!(
                            "Element <{name}> length violates minLength ({min_len})"
                        )));
                    }
                }
                if let Some(max_len) = rule.restriction.max_length {
                    if trimmed.chars().count() > max_len {
                        return Err(XmlError::XsdError(format!(
                            "Element <{name}> length violates maxLength ({max_len})"
                        )));
                    }
                }
                if !rule.restriction.enumerations.is_empty() {
                    if !rule.restriction.enumerations.iter().any(|e| e == trimmed) {
                        return Err(XmlError::XsdError(format!(
                            "Element <{name}> value '{trimmed}' not in enumeration list"
                        )));
                    }
                }
            }

            for &c_id in &node.children {
                if let Some(c) = doc.get_node(c_id) {
                    if matches!(c.kind, NodeKind::Element { .. }) {
                        self.validate_element(doc, c_id, depth + 1)?;
                    }
                }
            }
        }
        Ok(())
    }

    fn get_element_text(&self, doc: &Document, elem_id: NodeId) -> String {
        let mut text = String::new();
        if let Some(node) = doc.get_node(elem_id) {
            for &c_id in &node.children {
                if let Some(c) = doc.get_node(c_id) {
                    if let NodeKind::Text(t) = &c.kind {
                        text.push_str(t);
                    }
                }
            }
        }
        text
    }
}

impl XmlValidator for XsdValidator {
    fn validate(&self, doc: &Document) -> Result<()> {
        self.validate(doc)
    }
}