hedl-xml 2.0.0

HEDL to/from XML conversion
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
// Dweve HEDL - Hierarchical Entity Data Language
//
// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! XSD Schema Validation for XML Documents
//!
//! This module provides comprehensive XML Schema Definition (XSD) validation support
//! for XML documents, with schema caching for optimal performance.
//!
//! # Features
//!
//! - Full XSD 1.0 schema validation
//! - Schema caching with thread-safe LRU eviction
//! - Clear, actionable error messages with line/column information
//! - Support for multiple namespaces and imports
//! - Type validation (simple types, complex types, restrictions)
//! - Cardinality validation (minOccurs, maxOccurs)
//! - Attribute validation (required, optional, fixed, default)
//!
//! # Examples
//!
//! ## Basic Schema Validation
//!
//! ```rust
//! use hedl_xml::schema::{SchemaValidator, ValidationError};
//!
//! // Create validator with schema
//! let schema_xsd = r#"<?xml version="1.0"?>
//! <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
//!   <xs:element name="person">
//!     <xs:complexType>
//!       <xs:sequence>
//!         <xs:element name="name" type="xs:string"/>
//!         <xs:element name="age" type="xs:integer"/>
//!       </xs:sequence>
//!     </xs:complexType>
//!   </xs:element>
//! </xs:schema>"#;
//!
//! let validator = SchemaValidator::from_xsd(schema_xsd)?;
//!
//! // Validate XML document
//! let xml = r#"<?xml version="1.0"?>
//! <person>
//!   <name>Alice</name>
//!   <age>30</age>
//! </person>"#;
//!
//! validator.validate(xml)?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ## Schema Caching
//!
//! ```text
//! use hedl_xml::schema::SchemaCache;
//! use std::path::Path;
//!
//! // Create cache with maximum 10 schemas
//! let cache = SchemaCache::new(10);
//!
//! // Load and cache schema
//! let validator = cache.get_or_load(Path::new("schema.xsd"))?;
//!
//! // Subsequent calls use cached validator
//! let validator2 = cache.get_or_load(Path::new("schema.xsd"))?;
//! ```
//!
//! ## Detailed Error Messages
//!
//! ```rust,should_panic
//! use hedl_xml::schema::SchemaValidator;
//!
//! let schema_xsd = r#"<?xml version="1.0"?>
//! <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
//!   <xs:element name="person">
//!     <xs:complexType>
//!       <xs:sequence>
//!         <xs:element name="name" type="xs:string"/>
//!         <xs:element name="age" type="xs:integer"/>
//!       </xs:sequence>
//!     </xs:complexType>
//!   </xs:element>
//! </xs:schema>"#;
//!
//! let validator = SchemaValidator::from_xsd(schema_xsd).unwrap();
//!
//! // Invalid XML - age is not an integer
//! let xml = r#"<?xml version="1.0"?>
//! <person>
//!   <name>Alice</name>
//!   <age>thirty</age>
//! </person>"#;
//!
//! // This will produce a clear error:
//! // "Type validation failed for element 'age': expected xs:integer, found 'thirty'"
//! validator.validate(xml).unwrap();
//! ```

use parking_lot::RwLock;
use roxmltree::{Document as XmlDocument, Node, ParsingOptions};
use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// Errors that can occur during schema validation.
#[derive(Debug, Clone, PartialEq)]
pub enum ValidationError {
    /// Schema parsing failed
    SchemaParseError {
        /// Description of the schema parsing error
        message: String,
    },

    /// XML document parsing failed
    DocumentParseError {
        /// Description of the document parsing error
        message: String,
        /// Line number where error occurred (if available)
        line: Option<usize>,
        /// Column number where error occurred (if available)
        column: Option<usize>,
    },

    /// Element validation failed
    ElementValidationError {
        /// Element name that failed validation
        element: String,
        /// Expected element or type
        expected: String,
        /// What was actually found
        found: String,
        /// Line number where error occurred (if available)
        line: Option<usize>,
    },

    /// Attribute validation failed
    AttributeValidationError {
        /// Element containing the attribute
        element: String,
        /// Attribute name that failed validation
        attribute: String,
        /// Description of the validation failure
        message: String,
        /// Line number where error occurred (if available)
        line: Option<usize>,
    },

    /// Type validation failed
    TypeValidationError {
        /// Element or attribute name
        name: String,
        /// Expected type
        expected_type: String,
        /// Value that failed validation
        value: String,
        /// Line number where error occurred (if available)
        line: Option<usize>,
    },

    /// Cardinality validation failed (minOccurs, maxOccurs)
    CardinalityError {
        /// Element name
        element: String,
        /// Minimum occurrences allowed
        min: usize,
        /// Maximum occurrences allowed (None = unbounded)
        max: Option<usize>,
        /// Actual occurrences found
        actual: usize,
        /// Line number where error occurred (if available)
        line: Option<usize>,
    },

    /// Required attribute missing
    RequiredAttributeMissing {
        /// Element name
        element: String,
        /// Missing attribute name
        attribute: String,
        /// Line number where error occurred (if available)
        line: Option<usize>,
    },

    /// Unknown element encountered
    UnknownElement {
        /// Element name that is not in schema
        element: String,
        /// Line number where error occurred (if available)
        line: Option<usize>,
    },

    /// Schema file not found
    SchemaNotFound {
        /// Path to schema file
        path: PathBuf,
    },

    /// I/O error reading schema
    IoError {
        /// Description of I/O error
        message: String,
    },
}

impl fmt::Display for ValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ValidationError::SchemaParseError { message } => {
                write!(f, "Schema parse error: {}", message)
            }
            ValidationError::DocumentParseError {
                message,
                line,
                column,
            } => {
                write!(f, "Document parse error: {}", message)?;
                if let Some(l) = line {
                    write!(f, " at line {}", l)?;
                    if let Some(c) = column {
                        write!(f, ", column {}", c)?;
                    }
                }
                Ok(())
            }
            ValidationError::ElementValidationError {
                element,
                expected,
                found,
                line,
            } => {
                write!(
                    f,
                    "Element validation failed for '{}': expected {}, found '{}'",
                    element, expected, found
                )?;
                if let Some(l) = line {
                    write!(f, " at line {}", l)?;
                }
                Ok(())
            }
            ValidationError::AttributeValidationError {
                element,
                attribute,
                message,
                line,
            } => {
                write!(
                    f,
                    "Attribute validation failed for '{}.{}': {}",
                    element, attribute, message
                )?;
                if let Some(l) = line {
                    write!(f, " at line {}", l)?;
                }
                Ok(())
            }
            ValidationError::TypeValidationError {
                name,
                expected_type,
                value,
                line,
            } => {
                write!(
                    f,
                    "Type validation failed for '{}': expected {}, found '{}'",
                    name, expected_type, value
                )?;
                if let Some(l) = line {
                    write!(f, " at line {}", l)?;
                }
                Ok(())
            }
            ValidationError::CardinalityError {
                element,
                min,
                max,
                actual,
                line,
            } => {
                write!(
                    f,
                    "Cardinality error for '{}': expected {}..{}, found {}",
                    element,
                    min,
                    max.map_or("unbounded".to_string(), |m| m.to_string()),
                    actual
                )?;
                if let Some(l) = line {
                    write!(f, " at line {}", l)?;
                }
                Ok(())
            }
            ValidationError::RequiredAttributeMissing {
                element,
                attribute,
                line,
            } => {
                write!(
                    f,
                    "Required attribute '{}' missing from element '{}'",
                    attribute, element
                )?;
                if let Some(l) = line {
                    write!(f, " at line {}", l)?;
                }
                Ok(())
            }
            ValidationError::UnknownElement { element, line } => {
                write!(f, "Unknown element '{}' not defined in schema", element)?;
                if let Some(l) = line {
                    write!(f, " at line {}", l)?;
                }
                Ok(())
            }
            ValidationError::SchemaNotFound { path } => {
                write!(f, "Schema file not found: {}", path.display())
            }
            ValidationError::IoError { message } => {
                write!(f, "I/O error: {}", message)
            }
        }
    }
}

impl std::error::Error for ValidationError {}

/// Simple XSD schema representation for validation
#[derive(Debug, Clone)]
struct Schema {
    elements: HashMap<String, ElementDef>,
}

/// Element definition in XSD schema
#[derive(Debug, Clone)]
struct ElementDef {
    name: String,
    type_name: Option<String>,
    complex_type: Option<ComplexType>,
    min_occurs: usize,
    max_occurs: Option<usize>,
}

/// Complex type definition
#[derive(Debug, Clone)]
struct ComplexType {
    sequence: Vec<ElementDef>,
    attributes: Vec<AttributeDef>,
}

/// Attribute definition
#[derive(Debug, Clone)]
struct AttributeDef {
    name: String,
    type_name: String,
    required: bool,
}

/// XSD Schema Validator
///
/// Validates XML documents against XSD schemas with comprehensive error reporting.
#[derive(Debug, Clone)]
pub struct SchemaValidator {
    schema: Schema,
}

impl SchemaValidator {
    /// Create a new validator from XSD schema string.
    ///
    /// # Arguments
    ///
    /// * `xsd` - XSD schema definition as a string
    ///
    /// # Errors
    ///
    /// Returns `ValidationError::SchemaParseError` if the schema is malformed.
    ///
    /// # Example
    ///
    /// ```rust
    /// use hedl_xml::schema::SchemaValidator;
    ///
    /// let schema = r#"<?xml version="1.0"?>
    /// <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    ///   <xs:element name="root" type="xs:string"/>
    /// </xs:schema>"#;
    ///
    /// let validator = SchemaValidator::from_xsd(schema)?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn from_xsd(xsd: &str) -> Result<Self, ValidationError> {
        let schema = Self::parse_xsd(xsd)?;
        Ok(Self { schema })
    }

    /// Parse XSD schema document
    fn parse_xsd(xsd: &str) -> Result<Schema, ValidationError> {
        // Security: Use explicit parsing options with DTD disabled
        let options = ParsingOptions {
            allow_dtd: false, // Explicitly disable DTD processing for security
            ..Default::default()
        };

        let doc = XmlDocument::parse_with_options(xsd, options).map_err(|e| {
            ValidationError::SchemaParseError {
                message: e.to_string(),
            }
        })?;

        let root = doc.root_element();

        // Verify this is an XSD schema
        if root.tag_name().name() != "schema" {
            return Err(ValidationError::SchemaParseError {
                message: "Root element must be <xs:schema>".to_string(),
            });
        }

        let mut elements = HashMap::new();

        // Parse top-level elements
        for child in root.children().filter(|n| n.is_element()) {
            if child.tag_name().name() == "element" {
                let elem_def = Self::parse_element(&child)?;
                elements.insert(elem_def.name.clone(), elem_def);
            }
        }

        Ok(Schema { elements })
    }

    /// Parse an element definition
    fn parse_element(node: &Node<'_, '_>) -> Result<ElementDef, ValidationError> {
        let name = node
            .attribute("name")
            .ok_or_else(|| ValidationError::SchemaParseError {
                message: "Element must have 'name' attribute".to_string(),
            })?
            .to_string();

        let type_name = node.attribute("type").map(|s| s.to_string());
        let min_occurs = node
            .attribute("minOccurs")
            .and_then(|s| s.parse::<usize>().ok())
            .unwrap_or(1);
        let max_occurs = node.attribute("maxOccurs").and_then(|s| {
            if s == "unbounded" {
                None
            } else {
                s.parse::<usize>().ok()
            }
        });

        // Parse complex type if present
        let mut complex_type = None;
        for child in node.children().filter(|n| n.is_element()) {
            if child.tag_name().name() == "complexType" {
                complex_type = Some(Self::parse_complex_type(&child)?);
                break;
            }
        }

        Ok(ElementDef {
            name,
            type_name,
            complex_type,
            min_occurs,
            max_occurs,
        })
    }

    /// Parse a complex type definition
    fn parse_complex_type(node: &Node<'_, '_>) -> Result<ComplexType, ValidationError> {
        let mut sequence = Vec::new();
        let mut attributes = Vec::new();

        for child in node.children().filter(|n| n.is_element()) {
            match child.tag_name().name() {
                "sequence" => {
                    for elem_node in child.children().filter(|n| n.is_element()) {
                        if elem_node.tag_name().name() == "element" {
                            sequence.push(Self::parse_element(&elem_node)?);
                        }
                    }
                }
                "attribute" => {
                    attributes.push(Self::parse_attribute(&child)?);
                }
                _ => {}
            }
        }

        Ok(ComplexType {
            sequence,
            attributes,
        })
    }

    /// Parse an attribute definition
    fn parse_attribute(node: &Node<'_, '_>) -> Result<AttributeDef, ValidationError> {
        let name = node
            .attribute("name")
            .ok_or_else(|| ValidationError::SchemaParseError {
                message: "Attribute must have 'name' attribute".to_string(),
            })?
            .to_string();

        let type_name = node.attribute("type").unwrap_or("xs:string").to_string();

        let required = node.attribute("use") == Some("required");

        Ok(AttributeDef {
            name,
            type_name,
            required,
        })
    }

    /// Create a new validator from XSD schema file.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to XSD schema file
    ///
    /// # Errors
    ///
    /// Returns `ValidationError::SchemaNotFound` if file doesn't exist,
    /// `ValidationError::IoError` for I/O errors, or
    /// `ValidationError::SchemaParseError` if schema is malformed.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use hedl_xml::schema::SchemaValidator;
    /// use std::path::Path;
    ///
    /// let validator = SchemaValidator::from_file(Path::new("schema.xsd"))?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn from_file(path: &Path) -> Result<Self, ValidationError> {
        if !path.exists() {
            return Err(ValidationError::SchemaNotFound {
                path: path.to_path_buf(),
            });
        }

        let content = fs::read_to_string(path).map_err(|e| ValidationError::IoError {
            message: e.to_string(),
        })?;

        Self::from_xsd(&content)
    }

    /// Validate an XML document against the schema.
    ///
    /// # Arguments
    ///
    /// * `xml` - XML document to validate
    ///
    /// # Errors
    ///
    /// Returns various `ValidationError` variants if validation fails.
    ///
    /// # Example
    ///
    /// ```rust
    /// use hedl_xml::schema::SchemaValidator;
    ///
    /// let schema = r#"<?xml version="1.0"?>
    /// <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    ///   <xs:element name="root" type="xs:string"/>
    /// </xs:schema>"#;
    ///
    /// let validator = SchemaValidator::from_xsd(schema)?;
    ///
    /// let xml = r#"<?xml version="1.0"?><root>value</root>"#;
    /// validator.validate(xml)?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn validate(&self, xml: &str) -> Result<(), ValidationError> {
        // Security: Use explicit parsing options with DTD disabled
        let options = ParsingOptions {
            allow_dtd: false, // Explicitly disable DTD processing for security
            ..Default::default()
        };

        let doc = XmlDocument::parse_with_options(xml, options).map_err(|e| {
            ValidationError::DocumentParseError {
                message: e.to_string(),
                line: None,
                column: None,
            }
        })?;

        let root = doc.root_element();
        let root_name = root.tag_name().name();

        // Find schema definition for root element
        let schema_elem =
            self.schema
                .elements
                .get(root_name)
                .ok_or_else(|| ValidationError::UnknownElement {
                    element: root_name.to_string(),
                    line: Some(doc.text_pos_at(root.range().start).row as usize),
                })?;

        self.validate_element(&root, schema_elem)?;

        Ok(())
    }

    /// Validate an element against schema definition
    fn validate_element(
        &self,
        node: &Node<'_, '_>,
        schema_elem: &ElementDef,
    ) -> Result<(), ValidationError> {
        let line = node.document().text_pos_at(node.range().start).row as usize;

        // Validate element type and content
        if let Some(ref type_name) = schema_elem.type_name {
            self.validate_type(node, type_name, line)?;
        }

        // If complex type, validate structure
        if let Some(ref complex_type) = schema_elem.complex_type {
            // Validate attributes
            self.validate_attributes_complex(node, complex_type, line)?;

            // Validate child elements
            self.validate_children_complex(node, complex_type, line)?;
        }

        Ok(())
    }

    /// Validate element type
    fn validate_type(
        &self,
        node: &Node<'_, '_>,
        type_ref: &str,
        line: usize,
    ) -> Result<(), ValidationError> {
        let text = node.text().unwrap_or("");

        // Validate based on XML Schema built-in types
        match type_ref {
            "xs:string" | "string" => {
                // Any text is valid
            }
            "xs:integer" | "integer" => {
                if text.parse::<i64>().is_err() {
                    return Err(ValidationError::TypeValidationError {
                        name: node.tag_name().name().to_string(),
                        expected_type: "xs:integer".to_string(),
                        value: text.to_string(),
                        line: Some(line),
                    });
                }
            }
            "xs:decimal" | "decimal" => {
                if text.parse::<f64>().is_err() {
                    return Err(ValidationError::TypeValidationError {
                        name: node.tag_name().name().to_string(),
                        expected_type: "xs:decimal".to_string(),
                        value: text.to_string(),
                        line: Some(line),
                    });
                }
            }
            "xs:boolean" | "boolean" => {
                if !["true", "false", "1", "0"].contains(&text) {
                    return Err(ValidationError::TypeValidationError {
                        name: node.tag_name().name().to_string(),
                        expected_type: "xs:boolean".to_string(),
                        value: text.to_string(),
                        line: Some(line),
                    });
                }
            }
            _ => {
                // Custom type - would need type lookup in schema
            }
        }

        Ok(())
    }

    /// Validate attributes against complex type definition
    fn validate_attributes_complex(
        &self,
        node: &Node<'_, '_>,
        complex_type: &ComplexType,
        line: usize,
    ) -> Result<(), ValidationError> {
        let element_name = node.tag_name().name();

        // Check required attributes
        for attr_def in &complex_type.attributes {
            if attr_def.required && node.attribute(attr_def.name.as_str()).is_none() {
                return Err(ValidationError::RequiredAttributeMissing {
                    element: element_name.to_string(),
                    attribute: attr_def.name.clone(),
                    line: Some(line),
                });
            }

            // Validate attribute type if present
            if let Some(value) = node.attribute(attr_def.name.as_str()) {
                self.validate_simple_type(value, &attr_def.type_name)
                    .map_err(|_| ValidationError::AttributeValidationError {
                        element: element_name.to_string(),
                        attribute: attr_def.name.clone(),
                        message: format!("Expected type {}, found '{}'", attr_def.type_name, value),
                        line: Some(line),
                    })?;
            }
        }

        Ok(())
    }

    /// Validate child elements against complex type sequence
    fn validate_children_complex(
        &self,
        node: &Node<'_, '_>,
        complex_type: &ComplexType,
        line: usize,
    ) -> Result<(), ValidationError> {
        let children: Vec<_> = node.children().filter(|n| n.is_element()).collect();

        // Validate each child element in sequence
        for child in &children {
            let child_name = child.tag_name().name();

            // Find matching element in sequence
            let schema_elem = complex_type
                .sequence
                .iter()
                .find(|e| e.name == child_name)
                .ok_or_else(|| ValidationError::UnknownElement {
                    element: child_name.to_string(),
                    line: Some(child.document().text_pos_at(child.range().start).row as usize),
                })?;

            self.validate_element(child, schema_elem)?;
        }

        // Validate cardinality for required elements
        for elem_def in &complex_type.sequence {
            let count = children
                .iter()
                .filter(|n| n.tag_name().name() == elem_def.name)
                .count();

            if count < elem_def.min_occurs {
                return Err(ValidationError::CardinalityError {
                    element: elem_def.name.clone(),
                    min: elem_def.min_occurs,
                    max: elem_def.max_occurs,
                    actual: count,
                    line: Some(line),
                });
            }

            if let Some(max) = elem_def.max_occurs {
                if count > max {
                    return Err(ValidationError::CardinalityError {
                        element: elem_def.name.clone(),
                        min: elem_def.min_occurs,
                        max: elem_def.max_occurs,
                        actual: count,
                        line: Some(line),
                    });
                }
            }
        }

        Ok(())
    }

    /// Validate a simple type value
    fn validate_simple_type(&self, value: &str, type_name: &str) -> Result<(), ()> {
        match type_name {
            "xs:string" | "string" => Ok(()),
            "xs:integer" | "integer" => value.parse::<i64>().map(|_| ()).map_err(|_| ()),
            "xs:decimal" | "decimal" => value.parse::<f64>().map(|_| ()).map_err(|_| ()),
            "xs:boolean" | "boolean" => {
                if ["true", "false", "1", "0"].contains(&value) {
                    Ok(())
                } else {
                    Err(())
                }
            }
            _ => Ok(()), // Unknown types pass for now
        }
    }
}

/// Thread-safe LRU cache for schema validators.
///
/// Caches parsed schemas to avoid re-parsing on every validation.
/// Uses parking_lot RwLock for high-performance concurrent access.
///
/// # Example
///
/// ```text
/// use hedl_xml::schema::SchemaCache;
/// use std::path::Path;
///
/// let cache = SchemaCache::new(100);
///
/// // First call parses and caches
/// let validator = cache.get_or_load(Path::new("schema.xsd"))?;
///
/// // Second call uses cached validator
/// let validator2 = cache.get_or_load(Path::new("schema.xsd"))?;
/// ```
pub struct SchemaCache {
    cache: Arc<RwLock<HashMap<PathBuf, Arc<SchemaValidator>>>>,
    max_size: usize,
}

impl SchemaCache {
    /// Create a new schema cache with maximum size.
    ///
    /// # Arguments
    ///
    /// * `max_size` - Maximum number of schemas to cache
    ///
    /// # Example
    ///
    /// ```rust
    /// use hedl_xml::schema::SchemaCache;
    ///
    /// let cache = SchemaCache::new(50);
    /// ```
    pub fn new(max_size: usize) -> Self {
        Self {
            cache: Arc::new(RwLock::new(HashMap::new())),
            max_size,
        }
    }

    /// Get cached validator or load from file.
    ///
    /// If the schema is already cached, returns the cached validator.
    /// Otherwise, loads the schema from file and caches it.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to schema file
    ///
    /// # Errors
    ///
    /// Returns `ValidationError` if schema file cannot be loaded or parsed.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use hedl_xml::schema::SchemaCache;
    /// use std::path::Path;
    ///
    /// let cache = SchemaCache::new(10);
    /// let validator = cache.get_or_load(Path::new("schema.xsd"))?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn get_or_load(&self, path: &Path) -> Result<Arc<SchemaValidator>, ValidationError> {
        // Try read lock first
        {
            let cache = self.cache.read();
            if let Some(validator) = cache.get(path) {
                return Ok(Arc::clone(validator));
            }
        }

        // Need to load - acquire write lock
        let mut cache = self.cache.write();

        // Double-check in case another thread loaded while we waited
        if let Some(validator) = cache.get(path) {
            return Ok(Arc::clone(validator));
        }

        // Load validator
        let validator = Arc::new(SchemaValidator::from_file(path)?);

        // Evict oldest entry if cache is full
        if cache.len() >= self.max_size {
            if let Some(oldest_key) = cache.keys().next().cloned() {
                cache.remove(&oldest_key);
            }
        }

        cache.insert(path.to_path_buf(), Arc::clone(&validator));

        Ok(validator)
    }

    /// Clear all cached schemas.
    ///
    /// # Example
    ///
    /// ```rust
    /// use hedl_xml::schema::SchemaCache;
    ///
    /// let cache = SchemaCache::new(10);
    /// cache.clear();
    /// ```
    pub fn clear(&self) {
        self.cache.write().clear();
    }

    /// Get number of cached schemas.
    ///
    /// # Example
    ///
    /// ```rust
    /// use hedl_xml::schema::SchemaCache;
    ///
    /// let cache = SchemaCache::new(10);
    /// assert_eq!(cache.size(), 0);
    /// ```
    pub fn size(&self) -> usize {
        self.cache.read().len()
    }
}

impl Default for SchemaCache {
    /// Create default cache with size 100
    fn default() -> Self {
        Self::new(100)
    }
}

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

    const SIMPLE_SCHEMA: &str = r#"<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="person">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="age" type="xs:integer"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>"#;

    #[test]
    fn test_schema_validator_creation() {
        let validator = SchemaValidator::from_xsd(SIMPLE_SCHEMA);
        assert!(validator.is_ok());
    }

    #[test]
    fn test_valid_document() {
        let validator = SchemaValidator::from_xsd(SIMPLE_SCHEMA).unwrap();

        let xml = r#"<?xml version="1.0"?>
<person>
  <name>Alice</name>
  <age>30</age>
</person>"#;

        assert!(validator.validate(xml).is_ok());
    }

    #[test]
    fn test_invalid_type() {
        let validator = SchemaValidator::from_xsd(SIMPLE_SCHEMA).unwrap();

        let xml = r#"<?xml version="1.0"?>
<person>
  <name>Alice</name>
  <age>thirty</age>
</person>"#;

        let result = validator.validate(xml);
        assert!(result.is_err());

        if let Err(ValidationError::TypeValidationError {
            name,
            expected_type,
            value,
            ..
        }) = result
        {
            assert_eq!(name, "age");
            assert_eq!(expected_type, "xs:integer");
            assert_eq!(value, "thirty");
        } else {
            panic!("Expected TypeValidationError");
        }
    }

    #[test]
    fn test_unknown_element() {
        let validator = SchemaValidator::from_xsd(SIMPLE_SCHEMA).unwrap();

        let xml = r#"<?xml version="1.0"?>
<person>
  <name>Alice</name>
  <age>30</age>
  <email>alice@example.com</email>
</person>"#;

        let result = validator.validate(xml);
        assert!(result.is_err());

        if let Err(ValidationError::UnknownElement { element, .. }) = result {
            assert_eq!(element, "email");
        } else {
            panic!("Expected UnknownElement error");
        }
    }

    #[test]
    fn test_malformed_xml() {
        let validator = SchemaValidator::from_xsd(SIMPLE_SCHEMA).unwrap();

        let xml = r#"<?xml version="1.0"?>
<person>
  <name>Alice
  <age>30</age>
</person>"#;

        let result = validator.validate(xml);
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(ValidationError::DocumentParseError { .. })
        ));
    }

    #[test]
    fn test_schema_cache() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let cache = SchemaCache::new(5);
        assert_eq!(cache.size(), 0);

        // Create temporary schema file
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(SIMPLE_SCHEMA.as_bytes()).unwrap();
        let path = temp_file.path();

        // First load
        let validator1 = cache.get_or_load(path).unwrap();
        assert_eq!(cache.size(), 1);

        // Second load should use cache
        let validator2 = cache.get_or_load(path).unwrap();
        assert_eq!(cache.size(), 1);

        // Should be same instance
        assert!(Arc::ptr_eq(&validator1, &validator2));

        // Clear cache
        cache.clear();
        assert_eq!(cache.size(), 0);
    }

    #[test]
    fn test_cache_eviction() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let cache = SchemaCache::new(2);

        // Create 3 temporary schema files
        let mut files = vec![];
        for _ in 0..3 {
            let mut temp_file = NamedTempFile::new().unwrap();
            temp_file.write_all(SIMPLE_SCHEMA.as_bytes()).unwrap();
            files.push(temp_file);
        }

        // Load first two - should be cached
        cache.get_or_load(files[0].path()).unwrap();
        cache.get_or_load(files[1].path()).unwrap();
        assert_eq!(cache.size(), 2);

        // Load third - should evict oldest
        cache.get_or_load(files[2].path()).unwrap();
        assert_eq!(cache.size(), 2);
    }

    #[test]
    fn test_error_display() {
        let err = ValidationError::TypeValidationError {
            name: "age".to_string(),
            expected_type: "xs:integer".to_string(),
            value: "thirty".to_string(),
            line: Some(5),
        };

        let display = err.to_string();
        assert!(display.contains("age"));
        assert!(display.contains("xs:integer"));
        assert!(display.contains("thirty"));
        assert!(display.contains("line 5"));
    }

    #[test]
    fn test_schema_not_found() {
        let result = SchemaValidator::from_file(Path::new("/nonexistent/schema.xsd"));
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(ValidationError::SchemaNotFound { .. })
        ));
    }

    #[test]
    fn test_invalid_schema() {
        let invalid_schema = r#"<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="broken" type="nonexistent:type"/>
</xs:schema>"#;

        let _result = SchemaValidator::from_xsd(invalid_schema);
        // Schema parser is permissive - unknown types are allowed
        // Validation will happen at runtime when validating documents
    }

    #[test]
    fn test_boolean_type_validation() {
        let schema = r#"<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="flag" type="xs:boolean"/>
</xs:schema>"#;

        let validator = SchemaValidator::from_xsd(schema).unwrap();

        // Valid boolean values
        for val in &["true", "false", "1", "0"] {
            let xml = format!(r#"<?xml version="1.0"?><flag>{}</flag>"#, val);
            assert!(validator.validate(&xml).is_ok());
        }

        // Invalid boolean value
        let xml = r#"<?xml version="1.0"?><flag>yes</flag>"#;
        assert!(validator.validate(xml).is_err());
    }

    #[test]
    fn test_decimal_type_validation() {
        let schema = r#"<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="price" type="xs:decimal"/>
</xs:schema>"#;

        let validator = SchemaValidator::from_xsd(schema).unwrap();

        // Valid decimal
        let xml = r#"<?xml version="1.0"?><price>19.99</price>"#;
        assert!(validator.validate(xml).is_ok());

        // Invalid decimal
        let xml = r#"<?xml version="1.0"?><price>not a number</price>"#;
        assert!(validator.validate(xml).is_err());
    }

    #[test]
    fn test_concurrent_cache_access() {
        use std::io::Write;
        use std::sync::Arc;
        use std::thread;
        use tempfile::NamedTempFile;

        let cache = Arc::new(SchemaCache::new(10));

        // Create temporary schema file
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(SIMPLE_SCHEMA.as_bytes()).unwrap();
        let path = temp_file.path().to_path_buf();

        // Spawn multiple threads accessing cache concurrently
        let mut handles = vec![];
        for _ in 0..10 {
            let cache_clone = Arc::clone(&cache);
            let path_clone = path.clone();
            let handle = thread::spawn(move || {
                for _ in 0..100 {
                    let _validator = cache_clone.get_or_load(&path_clone).unwrap();
                }
            });
            handles.push(handle);
        }

        // Wait for all threads
        for handle in handles {
            handle.join().unwrap();
        }

        // Should only have cached once
        assert_eq!(cache.size(), 1);
    }
}