fastxml 0.8.1

A fast, memory-efficient XML library with XPath and XSD validation support
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
//! Schema export utilities.
//!
//! This module provides functionality to resolve and export schemas
//! to a local directory with rewritten import/include paths.
//!
//! This is useful for tools like libxml that need all schemas
//! in a single directory with relative paths.

use std::collections::HashMap;
use std::path::Path;

use crate::error::Result;
use crate::schema::fetcher::{FetchResult, SchemaFetcher};

/// Result of schema export operation.
#[derive(Debug, Clone)]
pub struct ExportResult {
    /// Number of schemas exported.
    pub schema_count: usize,
    /// Map of original URIs to local filenames.
    pub uri_to_filename: HashMap<String, String>,
    /// The entry schema filename (first schema).
    pub entry_filename: Option<String>,
}

/// Exports schemas from xsi:schemaLocation to a local directory.
///
/// This function:
/// 1. Parses the XML to extract xsi:schemaLocation
/// 2. Fetches all referenced schemas (including imports/includes)
/// 3. Rewrites import/include schemaLocation attributes to relative paths
/// 4. Writes all schemas to the output directory
///
/// # Arguments
///
/// * `xml_content` - The XML document content
/// * `output_dir` - Directory to write schemas to
/// * `fetcher` - Schema fetcher for downloading schemas
///
/// # Returns
///
/// Export result with schema count and filename mappings
///
/// # Example
///
/// ```ignore
/// use fastxml::schema::export::export_schemas_from_xml;
/// use fastxml::schema::DefaultFetcher;
///
/// let xml = std::fs::read("document.xml")?;
/// let fetcher = DefaultFetcher::new();
/// let result = export_schemas_from_xml(&xml, Path::new("./schemas"), &fetcher)?;
/// println!("Exported {} schemas", result.schema_count);
/// ```
pub fn export_schemas_from_xml<F: SchemaFetcher>(
    xml_content: &[u8],
    output_dir: &Path,
    fetcher: &F,
) -> Result<ExportResult> {
    // Stream only the root element to extract schema locations (no full DOM parse)
    let locations = crate::parser::parse_schema_locations_from_reader(xml_content)?;

    if locations.is_empty() {
        return Ok(ExportResult {
            schema_count: 0,
            uri_to_filename: HashMap::new(),
            entry_filename: None,
        });
    }

    // Create output directory if it doesn't exist
    std::fs::create_dir_all(output_dir)?;

    // Use HashMap to collect all schemas
    let mut schemas: HashMap<String, Vec<u8>> = HashMap::new();
    let mut entry_uri = None;

    // Fetch and resolve all schemas
    for (_namespace, location) in &locations {
        match fetcher.fetch(location) {
            Ok(result) => {
                // Track the first successfully fetched schema as the entry point
                if entry_uri.is_none() {
                    entry_uri = Some(result.final_url.clone());
                }

                schemas.insert(result.final_url.clone(), result.content.clone());

                // Parse and resolve imports recursively
                let _ = resolve_imports_recursive(
                    &result.final_url,
                    &result.content,
                    fetcher,
                    &mut schemas,
                );
            }
            Err(_) => {
                // Skip schemas that can't be fetched
                continue;
            }
        }
    }

    // Build URI to filename mapping
    let mut uri_to_filename: HashMap<String, String> = HashMap::new();
    let mut existing_filenames: std::collections::HashSet<String> =
        std::collections::HashSet::new();
    let uris: Vec<String> = schemas.keys().cloned().collect();

    for uri in &uris {
        let filename = uri_to_safe_filename(uri, &existing_filenames);
        existing_filenames.insert(filename.clone());
        uri_to_filename.insert(uri.clone(), filename);
    }

    // Rewrite and export each schema
    for uri in &uris {
        if let Some(content) = schemas.get(uri) {
            let filename = uri_to_filename.get(uri).unwrap();
            let rewritten = rewrite_schema_locations(content, uri, &uri_to_filename)?;
            let output_path = output_dir.join(filename);
            std::fs::write(&output_path, rewritten)?;
        }
    }

    let entry_filename = entry_uri.and_then(|uri| uri_to_filename.get(&uri).cloned());

    Ok(ExportResult {
        schema_count: uri_to_filename.len(),
        uri_to_filename,
        entry_filename,
    })
}

/// Recursively resolves imports and includes from a schema.
fn resolve_imports_recursive<F: SchemaFetcher>(
    base_uri: &str,
    content: &[u8],
    fetcher: &F,
    schemas: &mut HashMap<String, Vec<u8>>,
) -> Result<()> {
    // Parse schema to find imports/includes
    let content_str = std::str::from_utf8(content).unwrap_or("");

    // Extract import schemaLocation attributes
    for location in extract_schema_locations(content_str) {
        let resolved_uri = resolve_uri(base_uri, &location)?;

        if !schemas.contains_key(&resolved_uri) {
            match fetcher.fetch(&resolved_uri) {
                Ok(FetchResult {
                    content: fetched_content,
                    final_url,
                    ..
                }) => {
                    schemas.insert(final_url.clone(), fetched_content.clone());
                    if final_url != resolved_uri {
                        schemas.insert(resolved_uri, fetched_content.clone());
                    }
                    // Recurse
                    resolve_imports_recursive(&final_url, &fetched_content, fetcher, schemas)?;
                }
                Err(_) => {
                    // Skip schemas that can't be fetched
                    continue;
                }
            }
        }
    }

    Ok(())
}

/// Extracts schemaLocation values from import/include elements.
fn extract_schema_locations(content: &str) -> Vec<String> {
    let mut locations = Vec::new();

    // Simple regex-like extraction for schemaLocation attributes
    // Matches: schemaLocation="..." or schemaLocation='...'
    let patterns = [r#"schemaLocation=""#, r#"schemaLocation='"#];

    for pattern in patterns {
        let quote = if pattern.ends_with('"') { '"' } else { '\'' };
        let mut remaining = content;

        while let Some(start) = remaining.find(pattern) {
            let after_pattern = &remaining[start + pattern.len()..];
            if let Some(end) = after_pattern.find(quote) {
                let location = &after_pattern[..end];
                // Skip xsi:schemaLocation (contains spaces for namespace-location pairs)
                if !location.contains(' ') && !location.is_empty() {
                    locations.push(location.to_string());
                }
                remaining = &after_pattern[end + 1..];
            } else {
                break;
            }
        }
    }

    locations
}

/// Rewrites schemaLocation attributes in a schema to use local filenames.
///
/// `base_uri` is the URI of the schema being rewritten, used to resolve relative paths.
fn rewrite_schema_locations(
    content: &[u8],
    base_uri: &str,
    uri_to_filename: &HashMap<String, String>,
) -> Result<Vec<u8>> {
    let content_str = std::str::from_utf8(content).unwrap_or("");
    let mut result = content_str.to_string();

    // First, rewrite absolute URIs directly
    for (uri, filename) in uri_to_filename {
        // Replace in schemaLocation="..."
        let old_double = format!(r#"schemaLocation="{}""#, uri);
        let new_double = format!(r#"schemaLocation="{}""#, filename);
        result = result.replace(&old_double, &new_double);

        // Replace in schemaLocation='...'
        let old_single = format!(r#"schemaLocation='{}'"#, uri);
        let new_single = format!(r#"schemaLocation='{}'"#, filename);
        result = result.replace(&old_single, &new_single);
    }

    // Now handle relative paths by resolving them and finding the matching filename
    // Extract all remaining schemaLocation values and try to resolve them
    let locations = extract_schema_locations(&result);
    for location in locations {
        // Skip if it's already just a filename (already rewritten)
        if !location.contains('/') && !location.contains('\\') {
            continue;
        }

        // Try to resolve this relative path against the base URI
        if let Ok(resolved) = resolve_uri(base_uri, &location) {
            // Look up in our mapping
            if let Some(filename) = uri_to_filename.get(&resolved) {
                // Replace this relative path with the filename
                let old_double = format!(r#"schemaLocation="{}""#, location);
                let new_double = format!(r#"schemaLocation="{}""#, filename);
                result = result.replace(&old_double, &new_double);

                let old_single = format!(r#"schemaLocation='{}'"#, location);
                let new_single = format!(r#"schemaLocation='{}'"#, filename);
                result = result.replace(&old_single, &new_single);
            }
        }
    }

    Ok(result.into_bytes())
}

/// Converts a URI to a safe filename, ensuring uniqueness.
fn uri_to_safe_filename(
    uri: &str,
    existing_filenames: &std::collections::HashSet<String>,
) -> String {
    // Remove protocol
    let without_protocol = uri
        .strip_prefix("http://")
        .or_else(|| uri.strip_prefix("https://"))
        .or_else(|| uri.strip_prefix("file://"))
        .unwrap_or(uri);

    // Get the last path component or use a hash
    let base_filename = Path::new(without_protocol)
        .file_name()
        .and_then(|n| n.to_str())
        .map(|s| s.to_string())
        .unwrap_or_else(|| {
            // Use hash for complex URIs
            format!("schema_{:x}.xsd", hash_uri(uri))
        });

    // Ensure .xsd extension
    let base_filename = if base_filename.ends_with(".xsd") {
        base_filename
    } else {
        format!("{}.xsd", base_filename)
    };

    // Make filename unique if it already exists
    if !existing_filenames.contains(&base_filename) {
        return base_filename;
    }

    // Add hash suffix to make it unique
    let stem = base_filename.strip_suffix(".xsd").unwrap_or(&base_filename);
    let hash_suffix = format!("{:08x}", hash_uri(uri) as u32);
    format!("{}_{}.xsd", stem, hash_suffix)
}

/// Simple hash function for URIs.
fn hash_uri(uri: &str) -> u64 {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    let mut hasher = DefaultHasher::new();
    uri.hash(&mut hasher);
    hasher.finish()
}

/// Resolves a relative URI against a base URI.
fn resolve_uri(base: &str, relative: &str) -> Result<String> {
    // If relative is already absolute, use it directly
    if relative.starts_with("http://")
        || relative.starts_with("https://")
        || relative.starts_with("file://")
    {
        return Ok(relative.to_string());
    }

    // Handle file:// base URIs
    if let Some(base_path) = base.strip_prefix("file://") {
        let base_dir = Path::new(base_path).parent().unwrap_or(Path::new("."));
        let resolved = base_dir.join(relative);
        let canonical = resolved.canonicalize().unwrap_or_else(|_| resolved.clone());
        return Ok(format!("file://{}", canonical.display()));
    }

    // Handle http(s) base URIs
    if base.starts_with("http://") || base.starts_with("https://") {
        // Find the last slash in the path
        if let Some(last_slash) = base.rfind('/') {
            let base_dir = &base[..=last_slash];
            let combined = format!("{}{}", base_dir, relative);
            return Ok(normalize_url_path(&combined));
        }
    }

    // Fallback: just append
    Ok(format!("{}/{}", base, relative))
}

/// Normalizes a URL path by resolving `.` and `..` components.
fn normalize_url_path(url: &str) -> String {
    // Split URL into protocol+host and path
    let (prefix, path) = if let Some(pos) = url.find("://") {
        let after_protocol = &url[pos + 3..];
        if let Some(slash_pos) = after_protocol.find('/') {
            let host_end = pos + 3 + slash_pos;
            (&url[..host_end], &url[host_end..])
        } else {
            return url.to_string();
        }
    } else {
        return url.to_string();
    };

    // Normalize the path
    let mut segments: Vec<&str> = Vec::new();
    for segment in path.split('/') {
        match segment {
            "" | "." => {}
            ".." => {
                segments.pop();
            }
            s => segments.push(s),
        }
    }

    format!("{}/{}", prefix, segments.join("/"))
}

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

    #[test]
    fn test_uri_to_safe_filename() {
        use std::collections::HashSet;
        let empty: HashSet<String> = HashSet::new();

        assert_eq!(
            uri_to_safe_filename("http://example.com/schemas/types.xsd", &empty),
            "types.xsd"
        );
        assert_eq!(
            uri_to_safe_filename("https://schemas.opengis.net/gml/3.2.1/gml.xsd", &empty),
            "gml.xsd"
        );
        assert_eq!(
            uri_to_safe_filename("file:///path/to/schema.xsd", &empty),
            "schema.xsd"
        );
    }

    #[test]
    fn test_uri_to_safe_filename_uniqueness() {
        use std::collections::HashSet;
        let mut existing: HashSet<String> = HashSet::new();
        existing.insert("types.xsd".to_string());

        // Should add hash suffix when filename already exists
        let filename = uri_to_safe_filename("http://example.com/other/types.xsd", &existing);
        assert!(filename.starts_with("types_"));
        assert!(filename.ends_with(".xsd"));
        assert_ne!(filename, "types.xsd");
    }

    #[test]
    fn test_extract_schema_locations() {
        let content = r#"
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:import namespace="http://example.com" schemaLocation="types.xsd"/>
                <xs:include schemaLocation='common.xsd'/>
            </xs:schema>
        "#;
        let locations = extract_schema_locations(content);
        assert_eq!(locations.len(), 2);
        assert!(locations.contains(&"types.xsd".to_string()));
        assert!(locations.contains(&"common.xsd".to_string()));
    }

    #[test]
    fn test_extract_schema_locations_skips_xsi() {
        let content = r#"
            <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://example.com http://example.com/schema.xsd">
            </root>
        "#;
        let locations = extract_schema_locations(content);
        // Should skip xsi:schemaLocation (contains space)
        assert!(locations.is_empty());
    }

    #[test]
    fn test_resolve_uri_absolute() {
        let result = resolve_uri("http://base.com/path/", "http://other.com/schema.xsd").unwrap();
        assert_eq!(result, "http://other.com/schema.xsd");
    }

    #[test]
    fn test_resolve_uri_relative_http() {
        let result = resolve_uri("http://example.com/schemas/main.xsd", "types.xsd").unwrap();
        assert_eq!(result, "http://example.com/schemas/types.xsd");
    }

    #[test]
    fn test_rewrite_schema_locations() {
        let content = br#"<xs:import schemaLocation="http://example.com/types.xsd"/>"#;
        let mut mapping = HashMap::new();
        mapping.insert(
            "http://example.com/types.xsd".to_string(),
            "types.xsd".to_string(),
        );

        let result =
            rewrite_schema_locations(content, "http://example.com/main.xsd", &mapping).unwrap();
        let result_str = std::str::from_utf8(&result).unwrap();

        assert!(result_str.contains(r#"schemaLocation="types.xsd""#));
    }

    #[test]
    fn test_rewrite_schema_locations_relative_path() {
        let content = br#"<xs:import schemaLocation="../types/common.xsd"/>"#;
        let mut mapping = HashMap::new();
        mapping.insert(
            "http://example.com/types/common.xsd".to_string(),
            "common.xsd".to_string(),
        );

        let result =
            rewrite_schema_locations(content, "http://example.com/schemas/main.xsd", &mapping)
                .unwrap();
        let result_str = std::str::from_utf8(&result).unwrap();

        assert!(result_str.contains(r#"schemaLocation="common.xsd""#));
    }
}