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
//! Synchronous schema resolver.
//!
//! This module provides the synchronous implementation of schema resolution
//! for import/include chains.
use std::collections::{HashSet, VecDeque};
use indexmap::IndexMap;
use crate::error::Result;
use crate::schema::fetcher::SchemaFetcher;
use super::super::parser::parse_xsd_ast;
use super::super::types::XsdSchema;
use super::common::resolve_uri;
/// Schema resolver that handles import/include chains.
pub struct SchemaResolver<'a, F: SchemaFetcher> {
fetcher: &'a F,
/// Resolved schemas by URI, in stable discovery order. An `IndexMap`
/// (not `HashMap`) is essential: `take_all_schemas` feeds the compiler,
/// and when two documents declare the same local name in different
/// namespaces (e.g. `foo` in a target namespace vs. `foo` in no namespace)
/// the last-compiled one wins the key collision. HashMap iteration order
/// varies per process, which made such schemas validate nondeterministically
/// (see wildG031). Insertion order keeps the verdict stable.
schemas: IndexMap<String, XsdSchema>,
/// URIs currently being resolved (for cycle detection)
resolving: HashSet<String>,
}
impl<'a, F: SchemaFetcher> SchemaResolver<'a, F> {
/// Creates a new schema resolver.
pub fn new(fetcher: &'a F) -> Self {
Self {
fetcher,
schemas: IndexMap::new(),
resolving: HashSet::new(),
}
}
/// Resolves all dependencies starting from an entry schema.
///
/// Returns all resolved schemas in dependency order (dependencies first).
pub fn resolve_all(&mut self, entry_content: &[u8], entry_uri: &str) -> Result<Vec<XsdSchema>> {
// Parse the entry schema
let entry_schema = parse_xsd_ast(entry_content)?;
// Store and track the entry
self.schemas.insert(entry_uri.to_string(), entry_schema);
// Use BFS to resolve all dependencies
let mut queue: VecDeque<String> = VecDeque::new();
queue.push_back(entry_uri.to_string());
while let Some(current_uri) = queue.pop_front() {
if self.resolving.contains(¤t_uri) {
return Err(crate::schema::error::SchemaError::CircularDependency {
uri: current_uri,
}
.into());
}
self.resolving.insert(current_uri.clone());
// Get imports and includes from the current schema
let (imports, includes) = {
let schema = self.schemas.get(¤t_uri).ok_or_else(|| {
crate::schema::error::SchemaError::SchemaNotFound {
uri: current_uri.clone(),
}
})?;
{
// xs:redefine references a schema document exactly like
// xs:include does; fetch those locations too.
let mut includes = schema.includes.clone();
includes.extend(schema.redefines.iter().map(|r| {
crate::schema::xsd::types::XsdInclude {
schema_location: r.schema_location.clone(),
}
}));
(schema.imports.clone(), includes)
}
};
// Process imports
for import in imports {
if let Some(location) = &import.schema_location {
let resolved_uri = resolve_uri(¤t_uri, location)?;
if !self.schemas.contains_key(&resolved_uri) {
let content = self.fetch_schema(&resolved_uri)?;
let schema = parse_xsd_ast(&content)?;
self.schemas.insert(resolved_uri.clone(), schema);
queue.push_back(resolved_uri);
}
}
}
// Process includes
for include in includes {
let resolved_uri = resolve_uri(¤t_uri, &include.schema_location)?;
if !self.schemas.contains_key(&resolved_uri) {
let content = self.fetch_schema(&resolved_uri)?;
let schema = parse_xsd_ast(&content)?;
self.schemas.insert(resolved_uri.clone(), schema);
queue.push_back(resolved_uri);
}
}
self.resolving.remove(¤t_uri);
}
// Return schemas in order (entry last for easier compilation)
let mut result: Vec<XsdSchema> = Vec::new();
// First add all non-entry schemas
for (uri, schema) in &self.schemas {
if uri != entry_uri {
result.push(schema.clone());
}
}
// Add entry schema last
if let Some(entry) = self.schemas.shift_remove(entry_uri) {
result.push(entry);
}
Ok(result)
}
/// Fetches a schema via the fetcher (caching is handled by the fetcher).
fn fetch_schema(&self, uri: &str) -> Result<Vec<u8>> {
let result = self.fetcher.fetch(uri)?;
Ok(result.content)
}
/// Resolves an entry schema and accumulates it along with its dependencies.
///
/// Unlike [`Self::resolve_all`], this method does not return schemas immediately.
/// Instead, it accumulates them internally so that multiple entry schemas
/// can share resolved dependencies (avoiding duplicate fetches).
///
/// Call [`Self::take_all_schemas`] after all entries have been resolved.
///
/// # Arguments
///
/// * `entry_content` - The entry XSD file content as bytes
/// * `entry_uri` - URI for the entry schema (used for resolving relative imports)
pub fn resolve_entry(&mut self, entry_content: &[u8], entry_uri: &str) -> Result<()> {
// Skip if already resolved
if self.schemas.contains_key(entry_uri) {
return Ok(());
}
// Parse the entry schema
let entry_schema = parse_xsd_ast(entry_content)?;
// Store and track the entry
self.schemas.insert(entry_uri.to_string(), entry_schema);
// Use BFS to resolve all dependencies
let mut queue: VecDeque<String> = VecDeque::new();
queue.push_back(entry_uri.to_string());
while let Some(current_uri) = queue.pop_front() {
if self.resolving.contains(¤t_uri) {
return Err(crate::schema::error::SchemaError::CircularDependency {
uri: current_uri,
}
.into());
}
self.resolving.insert(current_uri.clone());
// Get imports and includes from the current schema
let (imports, includes) = {
let schema = self.schemas.get(¤t_uri).ok_or_else(|| {
crate::schema::error::SchemaError::SchemaNotFound {
uri: current_uri.clone(),
}
})?;
{
// xs:redefine references a schema document exactly like
// xs:include does; fetch those locations too.
let mut includes = schema.includes.clone();
includes.extend(schema.redefines.iter().map(|r| {
crate::schema::xsd::types::XsdInclude {
schema_location: r.schema_location.clone(),
}
}));
(schema.imports.clone(), includes)
}
};
// Process imports
for import in imports {
if let Some(location) = &import.schema_location {
let resolved_uri = resolve_uri(¤t_uri, location)?;
if !self.schemas.contains_key(&resolved_uri) {
let content = self.fetch_schema(&resolved_uri)?;
let schema = parse_xsd_ast(&content)?;
self.schemas.insert(resolved_uri.clone(), schema);
queue.push_back(resolved_uri);
}
}
}
// Process includes
for include in includes {
let resolved_uri = resolve_uri(¤t_uri, &include.schema_location)?;
if !self.schemas.contains_key(&resolved_uri) {
let content = self.fetch_schema(&resolved_uri)?;
let schema = parse_xsd_ast(&content)?;
self.schemas.insert(resolved_uri.clone(), schema);
queue.push_back(resolved_uri);
}
}
self.resolving.remove(¤t_uri);
}
Ok(())
}
/// Consumes the resolver and returns all accumulated schemas as a Vec.
///
/// Use this after calling [`Self::resolve_entry`] one or more times to get
/// all resolved schemas for compilation.
pub fn take_all_schemas(self) -> Vec<XsdSchema> {
self.schemas.into_values().collect()
}
/// Consumes the resolver and returns the resolved schemas.
pub fn into_schemas(self) -> IndexMap<String, XsdSchema> {
self.schemas
}
}