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
//! XSD schema builder — constructs an `XsdValidator` from a parsed XSD document.
//!
//! Entry points are `XsdValidator::from_schema` (simple) and
//! `from_schema_with_base_path` (supports external `schemaLocation` resolution).
//!
//! The build proceeds in multiple passes:
//! 0. Schema composition (`xs:include`, `xs:redefine`, `xs:import`)
//! 0.5. Global attribute declarations (needed by attributeGroup parsing)
//! 1. Attribute-group and model-group definitions
//! 2. All other top-level declarations (elements, complex/simple types, attributes)
//! 3. Substitution-group map construction (direct + transitive membership)
//! 4. List-type resolution passes (base-type propagation, item-type facets,
//! inline list-type facets in elements and content-model particles)
use std::collections::HashMap;
use std::path::Path;
use crate::dom::{Document, NodeKind};
use crate::error::{XmlError, XmlResult};
use super::composition::{process_schema_composition, CompositionState};
use super::facet_resolution::{
resolve_content_model_list_item_facets, resolve_inline_list_item_facets,
};
use super::parser::{
parse_attribute_group_def, parse_complex_type, parse_element_decl, parse_model_group_def,
parse_simple_type, resolve_type_name,
};
use super::types::{AttributeDecl, BuiltInType, Facet, TypeDef, TypeRef, XsdValidator};
use super::XS_NAMESPACE;
impl XsdValidator {
/// Build a validator from a parsed XSD schema document.
///
/// Equivalent to `from_schema_with_base_path(schema_doc, None)`.
pub fn from_schema(schema_doc: &Document) -> XmlResult<Self> {
Self::from_schema_with_base_path(schema_doc, None)
}
/// Set whether length/minLength/maxLength facets on QName and NOTATION types
/// are enforced. Default is `true` (enforce). Set to `false` to ignore them,
/// which matches the NIST test suite interpretation of W3C Bug #4009.
pub fn set_enforce_qname_length_facets(&mut self, enforce: bool) {
self.enforce_qname_length_facets = enforce;
}
/// Build a validator from a parsed XSD schema document, with a base path
/// for resolving `schemaLocation` attributes in `xs:include` and `xs:redefine`.
///
/// # Passes
///
/// 1. **Pass 0** — schema composition: `xs:include` / `xs:redefine` / `xs:import`
/// 2. **Pass 0.5** — global attribute declarations (needed before attributeGroup parsing)
/// 3. **Pass 1** — attribute-group and model-group definitions
/// 4. **Pass 2** — top-level elements, complex types, simple types, remaining attributes
/// 5. **Substitution-group construction** — direct + transitive membership map
/// 6. **List-type resolution** — three sub-passes to propagate `is_list`, `item_type`,
/// and `item_facets` through the type graph and into inline declarations
pub fn from_schema_with_base_path(
schema_doc: &Document,
base_path: Option<&Path>,
) -> XmlResult<Self> {
// Public entry creates fresh composition state (visited paths +
// depth counter) and delegates to the internal variant. The
// state is what lets `xs:include` / `xs:import` chains detect
// cycles and enforce a nesting cap.
let mut state = CompositionState::new(base_path);
Self::from_schema_with_composition_state(schema_doc, base_path, &mut state)
}
/// Internal entry used by `from_schema_with_base_path` and by
/// recursive composition. Threads the `CompositionState` so visited
/// paths and depth survive across nested `xs:include` / `xs:import`
/// calls.
pub(super) fn from_schema_with_composition_state(
schema_doc: &Document,
base_path: Option<&Path>,
state: &mut CompositionState,
) -> XmlResult<Self> {
let mut validator = XsdValidator {
elements: HashMap::new(),
types: HashMap::new(),
global_attributes: HashMap::new(),
attribute_groups: HashMap::new(),
model_groups: HashMap::new(),
target_namespace: None,
block_default_extension: false,
block_default_restriction: false,
enforce_qname_length_facets: true,
substitution_groups: HashMap::new(),
};
let schema_elem = schema_doc
.document_element()
.ok_or_else(|| XmlError::validation("Schema document has no root element"))?;
// Get target namespace and elementFormDefault
let mut element_form_qualified = false;
if let Some(elem) = schema_doc.element(schema_elem) {
validator.target_namespace =
elem.get_attribute("targetNamespace").map(|s| s.to_string());
element_form_qualified = elem.get_attribute("elementFormDefault") == Some("qualified");
// Parse blockDefault
if let Some(block_default) = elem.get_attribute("blockDefault") {
for token in block_default.split_whitespace() {
match token {
"extension" => validator.block_default_extension = true,
"restriction" => validator.block_default_restriction = true,
"#all" => {
validator.block_default_extension = true;
validator.block_default_restriction = true;
}
_ => {}
}
}
}
}
// Determine the effective namespace for local element declarations:
// If elementFormDefault="qualified", local elements inherit the target namespace.
let local_elem_ns = if element_form_qualified {
validator.target_namespace.clone()
} else {
None
};
// Pass 0: Process xs:include and xs:redefine to merge external schema declarations
if base_path.is_some() {
process_schema_composition(schema_doc, schema_elem, &mut validator, base_path, state)?;
}
// Pass 0.5: Parse global attribute declarations first, since attributeGroup
// definitions may reference them via <attribute ref="..."/>.
for child in schema_doc.children(schema_elem) {
if let Some(NodeKind::Element(elem)) = schema_doc.node_kind(child) {
let is_xs = elem.name.namespace_uri.as_deref() == Some(XS_NAMESPACE)
|| elem.name.prefix.as_deref() == Some("xs")
|| elem.name.prefix.as_deref() == Some("xsd");
if !is_xs {
continue;
}
if elem.name.local_name == "attribute" {
if let Some(attr_elem) = schema_doc.element(child) {
if let Some(name) = attr_elem.get_attribute("name") {
let type_ref = if let Some(type_attr) = attr_elem.get_attribute("type")
{
resolve_type_name(type_attr, &validator.target_namespace)
} else {
// Check for inline simpleType child
let mut inline_type = None;
for gc in schema_doc.children(child) {
if let Some(NodeKind::Element(ge)) = schema_doc.node_kind(gc) {
if ge.name.local_name == "simpleType" {
if let Ok(td) = parse_simple_type(schema_doc, gc) {
inline_type = Some(TypeRef::Inline(Box::new(td)));
}
}
}
}
inline_type.unwrap_or(TypeRef::BuiltIn(BuiltInType::String))
};
let required = attr_elem.get_attribute("use") == Some("required");
let default = attr_elem.get_attribute("default").map(|s| s.to_string());
let decl = AttributeDecl {
name: name.to_string(),
type_ref,
required,
default,
prohibited: false,
};
let key = (validator.target_namespace.clone(), name.to_string());
validator.global_attributes.insert(key, decl);
}
}
}
}
}
// Pass 1: Parse attribute group and model group definitions
// (both needed by complexType parsing in Pass 2)
for child in schema_doc.children(schema_elem) {
if let Some(NodeKind::Element(elem)) = schema_doc.node_kind(child) {
let is_xs = elem.name.namespace_uri.as_deref() == Some(XS_NAMESPACE)
|| elem.name.prefix.as_deref() == Some("xs")
|| elem.name.prefix.as_deref() == Some("xsd");
if !is_xs {
continue;
}
if elem.name.local_name == "attributeGroup" {
if let Some(ag_elem) = schema_doc.element(child) {
if let Some(name) = ag_elem.get_attribute("name") {
let ag_def = parse_attribute_group_def(
schema_doc,
child,
&validator.target_namespace,
&validator.global_attributes,
&validator.attribute_groups,
)?;
let key = (validator.target_namespace.clone(), name.to_string());
validator.attribute_groups.insert(key, ag_def);
}
}
}
if elem.name.local_name == "group" {
if let Some(g_elem) = schema_doc.element(child) {
if let Some(name) = g_elem.get_attribute("name") {
let mg_def = parse_model_group_def(
schema_doc,
child,
&local_elem_ns,
&validator.target_namespace,
&validator.attribute_groups,
&validator.model_groups,
validator.block_default_extension,
validator.block_default_restriction,
)?;
let key = (validator.target_namespace.clone(), name.to_string());
validator.model_groups.insert(key, mg_def);
}
}
}
}
}
// Pass 2: Process all other top-level children
for child in schema_doc.children(schema_elem) {
if let Some(NodeKind::Element(elem)) = schema_doc.node_kind(child) {
let local = &elem.name.local_name;
let is_xs = elem.name.namespace_uri.as_deref() == Some(XS_NAMESPACE)
|| elem.name.prefix.as_deref() == Some("xs")
|| elem.name.prefix.as_deref() == Some("xsd");
if !is_xs {
continue;
}
match &**local {
"element" => {
let decl = parse_element_decl(
schema_doc,
child,
&validator.target_namespace,
&local_elem_ns,
&validator.target_namespace,
&validator.attribute_groups,
&validator.model_groups,
validator.block_default_extension,
validator.block_default_restriction,
)?;
let key = (validator.target_namespace.clone(), decl.name.clone());
validator.elements.insert(key, decl);
}
"complexType" => {
let type_def = parse_complex_type(
schema_doc,
child,
&local_elem_ns,
&validator.target_namespace,
&validator.target_namespace,
&validator.attribute_groups,
&validator.model_groups,
validator.block_default_extension,
validator.block_default_restriction,
)?;
if let TypeDef::Complex(ref ct) = type_def {
if let Some(name) = &ct.name {
let key = (validator.target_namespace.clone(), name.clone());
validator.types.insert(key, type_def);
}
}
}
"simpleType" => {
let type_def = parse_simple_type(schema_doc, child)?;
if let TypeDef::Simple(ref st) = type_def {
if let Some(name) = &st.name {
let key = (validator.target_namespace.clone(), name.clone());
validator.types.insert(key, type_def);
}
}
}
"attribute" => {
// Parse global attribute declarations
if let Some(attr_elem) = schema_doc.element(child) {
if let Some(name) = attr_elem.get_attribute("name") {
let type_ref = if let Some(type_attr) =
attr_elem.get_attribute("type")
{
resolve_type_name(type_attr, &validator.target_namespace)
} else {
// Check for inline simpleType child
let mut inline_type = None;
for gc in schema_doc.children(child) {
if let Some(NodeKind::Element(ge)) =
schema_doc.node_kind(gc)
{
if ge.name.local_name == "simpleType" {
if let Ok(td) = parse_simple_type(schema_doc, gc) {
inline_type =
Some(TypeRef::Inline(Box::new(td)));
}
}
}
}
inline_type.unwrap_or(TypeRef::BuiltIn(BuiltInType::String))
};
let required = attr_elem.get_attribute("use") == Some("required");
let default =
attr_elem.get_attribute("default").map(|s| s.to_string());
let decl = AttributeDecl {
name: name.to_string(),
type_ref,
required,
default,
prohibited: false,
};
let key = (validator.target_namespace.clone(), name.to_string());
validator.global_attributes.insert(key, decl);
}
}
}
_ => {
// Ignore other top-level declarations for now
}
}
}
}
// Build substitution group map from element declarations.
// First, collect direct memberships: member -> head.
let mut direct_head: HashMap<(Option<String>, String), (Option<String>, String)> =
HashMap::new();
for (key, decl) in &validator.elements {
if let Some(ref sg_head) = decl.substitution_group {
direct_head.insert(key.clone(), sg_head.clone());
}
}
// Build transitive map: for each element that is a substitution group head,
// collect all (direct and transitive) members.
// An element E is a member of head H if:
// - E.substitutionGroup == H (direct), or
// - E.substitutionGroup == M where M is a member of H (transitive)
for member_key in direct_head.keys() {
// Walk up the chain from member to find all heads
let mut current = member_key.clone();
let mut chain = vec![member_key.clone()];
while let Some(head) = direct_head.get(¤t) {
// Add member_key as a member of head
validator
.substitution_groups
.entry(head.clone())
.or_default()
.push(member_key.clone());
current = head.clone();
// Prevent infinite loops
if chain.contains(¤t) {
break;
}
chain.push(current.clone());
}
}
// Deduplicate members
for members in validator.substitution_groups.values_mut() {
members.sort_by(|a, b| a.1.cmp(&b.1).then_with(|| a.0.cmp(&b.0)));
members.dedup();
}
debug_log!("substitution_groups: {:?}", validator.substitution_groups);
// Resolution pass: propagate list type info from base types to derived types.
// Types that restrict a list type inherit is_list and item_type.
let type_keys: Vec<_> = validator.types.keys().cloned().collect();
for key in &type_keys {
let base_local = {
if let Some(TypeDef::Simple(st)) = validator.types.get(key) {
st._base_type_local.clone()
} else {
None
}
};
if let Some(base_name) = base_local {
// Look up the base type in the same namespace
let base_key = (key.0.clone(), base_name);
let (is_list, item_type) = {
if let Some(TypeDef::Simple(base_st)) = validator.types.get(&base_key) {
(base_st.is_list, base_st.item_type.clone())
} else {
(false, None)
}
};
if is_list {
if let Some(TypeDef::Simple(st)) = validator.types.get_mut(key) {
st.is_list = true;
if st.item_type.is_none() {
st.item_type = item_type;
}
}
}
}
}
// Resolution pass 2: resolve item type facets for list types whose item type
// is a user-defined simple type (not a built-in).
let type_keys2: Vec<_> = validator.types.keys().cloned().collect();
for key in &type_keys2 {
let item_local = {
if let Some(TypeDef::Simple(st)) = validator.types.get(key) {
st._item_type_local.clone()
} else {
None
}
};
if let Some(item_name) = item_local {
// Look up the item type in the same namespace
let item_key = (key.0.clone(), item_name);
let resolved = {
if let Some(TypeDef::Simple(item_st)) = validator.types.get(&item_key) {
Some((item_st.base.clone(), item_st.facets.clone()))
} else {
None
}
};
if let Some((item_base, item_facets)) = resolved {
if let Some(TypeDef::Simple(st)) = validator.types.get_mut(key) {
st.item_type = Some(item_base);
st.item_facets = item_facets;
}
}
}
}
// Resolution pass 3: resolve item type facets for inline list types embedded in
// element declarations (both global elements and particles inside complex types).
// Collect resolved item types from the types map first.
let resolved_items: HashMap<(Option<String>, String), (BuiltInType, Vec<Facet>)> =
validator
.types
.iter()
.filter_map(|(k, td)| {
if let TypeDef::Simple(st) = td {
Some((k.clone(), (st.base.clone(), st.facets.clone())))
} else {
None
}
})
.collect();
// Resolve inline list types in global element declarations
for elem_decl in validator.elements.values_mut() {
resolve_inline_list_item_facets(
&mut elem_decl.type_ref,
&resolved_items,
&validator.target_namespace,
);
}
// Resolve inline list types in complex type content models
let type_keys3: Vec<_> = validator.types.keys().cloned().collect();
for key in type_keys3 {
if let Some(TypeDef::Complex(ct)) = validator.types.get_mut(&key) {
resolve_content_model_list_item_facets(
&mut ct.content,
&resolved_items,
&validator.target_namespace,
);
}
}
Ok(validator)
}
}