exml 0.7.2

Pure Rust XML library based on libxml2
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
use std::{
    borrow::Cow,
    collections::HashMap,
    ffi::c_void,
    ptr::{null, null_mut},
};

use crate::{
    dict::{XmlDictPtr, xml_dict_free, xml_dict_reference},
    libxml::{
        schemas_internals::{
            XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION, XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION,
            XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION, XML_SCHEMAS_FINAL_DEFAULT_EXTENSION,
            XML_SCHEMAS_FINAL_DEFAULT_LIST, XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION,
            XML_SCHEMAS_FINAL_DEFAULT_UNION, XML_SCHEMAS_QUALIF_ATTR, XML_SCHEMAS_QUALIF_ELEM,
            XmlSchemaAnnotPtr, XmlSchemaTypeType, xml_schema_free_annot,
        },
        xmlschemas::{
            XML_SCHEMA_NS, XML_SCHEMAS_NO_NAMESPACE, XmlSchemaBucketPtr, XmlSchemaImportPtr,
            xml_schema_bucket_free,
        },
        xmlschemastypes::xml_schema_get_predefined_type,
    },
    tree::XmlDocPtr,
    xmlschemas::item_list::{XmlSchemaItemListPtr, xml_schema_item_list_free},
};

use super::{
    context::XmlSchemaParserCtxt,
    items::{
        XmlSchemaAttributeGroupPtr, XmlSchemaAttributePtr, XmlSchemaBasicItemPtr,
        XmlSchemaElementPtr, XmlSchemaIDCPtr, XmlSchemaModelGroupDefPtr, XmlSchemaNotationPtr,
        XmlSchemaTypePtr,
    },
};

#[doc(alias = "xmlSchemaPtr")]
pub type XmlSchemaPtr = *mut XmlSchema;
/// A Schemas definition
#[doc(alias = "xmlSchema")]
#[repr(C)]
pub struct XmlSchema {
    pub(crate) name: Option<Cow<'static, str>>, /* schema name */
    pub(crate) target_namespace: Option<Cow<'static, str>>, /* the target namespace */
    pub(crate) version: *const u8,
    pub(crate) id: *const u8, /* Obsolete */
    pub(crate) doc: Option<XmlDocPtr>,
    pub(crate) annot: XmlSchemaAnnotPtr,
    pub(crate) flags: i32,

    pub(crate) type_decl: HashMap<String, XmlSchemaTypePtr>,
    pub(crate) attr_decl: HashMap<String, XmlSchemaAttributePtr>,
    pub(crate) attrgrp_decl: HashMap<String, XmlSchemaAttributeGroupPtr>,
    pub(crate) elem_decl: HashMap<String, XmlSchemaElementPtr>,
    pub(crate) nota_decl: HashMap<String, XmlSchemaNotationPtr>,

    pub(crate) schemas_imports: HashMap<String, XmlSchemaImportPtr>,

    pub(crate) _private: *mut c_void, /* unused by the library for users or bindings */
    pub(crate) group_decl: HashMap<String, XmlSchemaModelGroupDefPtr>,
    pub(crate) dict: XmlDictPtr,
    pub(crate) includes: *mut c_void, /* the includes, this is opaque for now */
    pub(crate) preserve: i32,         /* whether to free the document */
    pub(crate) counter: i32,          /* used to give anonymous components unique names */
    pub(crate) idc_def: HashMap<String, XmlSchemaIDCPtr>, /* All identity-constraint defs. */
    pub(crate) volatiles: *mut c_void, /* Obsolete */
}

impl XmlSchema {
    /// Lookup a type in the schemas or the predefined types
    ///
    /// Returns the group definition or NULL if not found.
    #[doc(alias = "xmlSchemaGetType")]
    pub(crate) unsafe fn get_type(&self, name: &str, ns_name: Option<&str>) -> XmlSchemaTypePtr {
        unsafe {
            // First try the built-in types.
            if let Some(ns_name) =
                ns_name.filter(|&ns_name| ns_name == XML_SCHEMA_NS.to_str().unwrap())
            {
                let ret = xml_schema_get_predefined_type(name, ns_name);
                if !ret.is_null() {
                    return ret;
                }
                // Note that we try the parsed schemas as well here
                // since one might have parsed the S4S, which contain more
                // than the built-in types.
                // TODO: Can we optimize this?
            }
            if ns_name == self.target_namespace.as_deref() {
                if let Some(&ret) = self.type_decl.get(name) {
                    return ret;
                }
            }
            let import: XmlSchemaImportPtr = if let Some(ns_name) = ns_name {
                self.schemas_imports
                    .get(ns_name)
                    .copied()
                    .unwrap_or(null_mut())
            } else {
                self.schemas_imports
                    .get(XML_SCHEMAS_NO_NAMESPACE)
                    .copied()
                    .unwrap_or(null_mut())
            };
            if import.is_null() {
                return null_mut();
            }
            *(*(*import).schema)
                .type_decl
                .get(name)
                .unwrap_or(&null_mut())
        }
    }

    #[doc(alias = "xmlSchemaGetIDC")]
    pub(crate) unsafe fn get_idc(&self, name: &str, ns_name: Option<&str>) -> XmlSchemaIDCPtr {
        unsafe {
            if ns_name == self.target_namespace.as_deref() {
                if let Some(&ret) = self.idc_def.get(name) {
                    return ret;
                }
            }
            let import: XmlSchemaImportPtr = if let Some(ns_name) = ns_name {
                self.schemas_imports
                    .get(ns_name)
                    .copied()
                    .unwrap_or(null_mut())
            } else {
                self.schemas_imports
                    .get(XML_SCHEMAS_NO_NAMESPACE)
                    .copied()
                    .unwrap_or(null_mut())
            };
            if import.is_null() {
                return null_mut();
            }
            *(*(*import).schema).idc_def.get(name).unwrap_or(&null_mut())
        }
    }

    /// Lookup a global element declaration in the schema.
    ///
    /// Returns the element declaration or NULL if not found.
    #[doc(alias = "xmlSchemaGetElem")]
    pub(crate) unsafe fn get_elem(&self, name: &str, ns_name: Option<&str>) -> XmlSchemaElementPtr {
        unsafe {
            if ns_name == self.target_namespace.as_deref() {
                if let Some(&ret) = self.elem_decl.get(name) {
                    return ret;
                }
            }
            let import: XmlSchemaImportPtr = if let Some(ns_name) = ns_name {
                self.schemas_imports
                    .get(ns_name)
                    .copied()
                    .unwrap_or(null_mut())
            } else {
                self.schemas_imports
                    .get(XML_SCHEMAS_NO_NAMESPACE)
                    .copied()
                    .unwrap_or(null_mut())
            };
            if import.is_null() {
                return null_mut();
            }
            (*(*import).schema)
                .elem_decl
                .get(name)
                .copied()
                .unwrap_or(null_mut())
        }
    }

    /// Lookup a an attribute in the schema or imported schemas
    ///
    /// Returns the attribute declaration or NULL if not found.
    #[doc(alias = "xmlSchemaGetAttributeDecl")]
    pub(crate) unsafe fn get_attribute_decl(
        &self,
        name: &str,
        ns_name: Option<&str>,
    ) -> XmlSchemaAttributePtr {
        unsafe {
            if ns_name == self.target_namespace.as_deref() {
                if let Some(&ret) = self.attr_decl.get(name) {
                    return ret;
                }
            }
            let import: XmlSchemaImportPtr = if let Some(ns_name) = ns_name {
                self.schemas_imports
                    .get(ns_name)
                    .copied()
                    .unwrap_or(null_mut())
            } else {
                self.schemas_imports
                    .get(XML_SCHEMAS_NO_NAMESPACE)
                    .copied()
                    .unwrap_or(null_mut())
            };
            if import.is_null() {
                return null_mut();
            }
            (*(*import).schema)
                .attr_decl
                .get(name)
                .copied()
                .unwrap_or(null_mut())
        }
    }

    /// Lookup a group in the schema or imported schemas
    ///
    /// Returns the group definition or NULL if not found.
    #[doc(alias = "xmlSchemaGetGroup")]
    pub(crate) unsafe fn get_group(
        &self,
        name: &str,
        ns_name: Option<&str>,
    ) -> XmlSchemaModelGroupDefPtr {
        unsafe {
            if ns_name == self.target_namespace.as_deref() {
                if let Some(&ret) = self.group_decl.get(name) {
                    return ret;
                }
            }
            let import: XmlSchemaImportPtr = if let Some(ns_name) = ns_name {
                self.schemas_imports
                    .get(ns_name)
                    .copied()
                    .unwrap_or(null_mut())
            } else {
                self.schemas_imports
                    .get(XML_SCHEMAS_NO_NAMESPACE)
                    .copied()
                    .unwrap_or(null_mut())
            };
            if import.is_null() {
                return null_mut();
            }
            (*(*import).schema)
                .group_decl
                .get(name)
                .copied()
                .unwrap_or(null_mut())
        }
    }

    /// Lookup a an attribute group in the schema or imported schemas
    ///
    /// Returns the attribute group definition or NULL if not found.
    #[doc(alias = "xmlSchemaGetAttributeGroup")]
    pub(crate) unsafe fn get_attribute_group(
        &self,
        name: &str,
        ns_name: Option<&str>,
    ) -> XmlSchemaAttributeGroupPtr {
        unsafe {
            if ns_name == self.target_namespace.as_deref() {
                if let Some(&ret) = self.attrgrp_decl.get(name) {
                    return ret;
                }
            }
            let import: XmlSchemaImportPtr = if let Some(ns_name) = ns_name {
                self.schemas_imports
                    .get(ns_name)
                    .copied()
                    .unwrap_or(null_mut())
            } else {
                self.schemas_imports
                    .get(XML_SCHEMAS_NO_NAMESPACE)
                    .copied()
                    .unwrap_or(null_mut())
            };
            if import.is_null() {
                return null_mut();
            }
            (*(*import).schema)
                .attrgrp_decl
                .get(name)
                .copied()
                .unwrap_or(null_mut())
        }
    }

    #[doc(alias = "xmlSchemaGetNotation")]
    pub(crate) unsafe fn get_notation(
        &self,
        name: &str,
        ns_name: Option<&str>,
    ) -> XmlSchemaNotationPtr {
        unsafe {
            if ns_name == self.target_namespace.as_deref() {
                if let Some(&ret) = self.nota_decl.get(name) {
                    return ret;
                }
            }
            let import: XmlSchemaImportPtr = if let Some(ns_name) = ns_name {
                self.schemas_imports
                    .get(ns_name)
                    .copied()
                    .unwrap_or(null_mut())
            } else {
                self.schemas_imports
                    .get(XML_SCHEMAS_NO_NAMESPACE)
                    .copied()
                    .unwrap_or(null_mut())
            };
            if import.is_null() {
                return null_mut();
            }
            (*(*import).schema)
                .nota_decl
                .get(name)
                .copied()
                .unwrap_or(null_mut())
        }
    }

    /// Lookup a group in the schema or imported schemas
    ///
    /// Returns the group definition or NULL if not found.
    #[doc(alias = "xmlSchemaGetNamedComponent")]
    pub(crate) unsafe fn get_named_component(
        &self,
        item_type: XmlSchemaTypeType,
        name: &str,
        target_ns: Option<&str>,
    ) -> XmlSchemaBasicItemPtr {
        unsafe {
            match item_type {
                XmlSchemaTypeType::XmlSchemaTypeGroup => {
                    self.get_group(name, target_ns) as XmlSchemaBasicItemPtr
                }
                XmlSchemaTypeType::XmlSchemaTypeElement => {
                    self.get_elem(name, target_ns) as XmlSchemaBasicItemPtr
                }
                _ => {
                    // TODO
                    null_mut()
                }
            }
        }
    }

    #[doc(alias = "xmlSchemaClearSchemaDefaults")]
    pub(crate) fn clear_flags(&mut self) {
        self.flags &= !XML_SCHEMAS_QUALIF_ELEM;
        self.flags &= !XML_SCHEMAS_QUALIF_ATTR;
        self.flags &= !XML_SCHEMAS_FINAL_DEFAULT_EXTENSION;
        self.flags &= !XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION;
        self.flags &= !XML_SCHEMAS_FINAL_DEFAULT_LIST;
        self.flags &= !XML_SCHEMAS_FINAL_DEFAULT_UNION;
        self.flags &= !XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION;
        self.flags &= !XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION;
        self.flags &= !XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION;
    }
}

impl Default for XmlSchema {
    fn default() -> Self {
        Self {
            name: None,
            target_namespace: None,
            version: null(),
            id: null(),
            doc: None,
            annot: null_mut(),
            flags: 0,
            type_decl: HashMap::new(),
            attr_decl: HashMap::new(),
            attrgrp_decl: HashMap::new(),
            elem_decl: HashMap::new(),
            nota_decl: HashMap::new(),
            schemas_imports: HashMap::new(),
            _private: null_mut(),
            group_decl: HashMap::new(),
            dict: null_mut(),
            includes: null_mut(),
            preserve: 0,
            counter: 0,
            idc_def: HashMap::new(),
            volatiles: null_mut(),
        }
    }
}

impl XmlSchemaParserCtxt<'_> {
    /// Allocate a new Schema structure.
    ///
    /// Returns the newly allocated structure or NULL in case or error
    #[doc(alias = "xmlSchemaNewSchema")]
    pub(crate) fn new_schema(&mut self) -> XmlSchemaPtr {
        let mut ret = Box::new(XmlSchema::default());
        ret.dict = self.dict;
        xml_dict_reference(ret.dict);
        Box::leak(ret)
    }
}

/// Deallocate a Schema structure.
#[doc(alias = "xmlSchemaFree")]
pub unsafe fn xml_schema_free(schema: XmlSchemaPtr) {
    unsafe {
        if schema.is_null() {
            return;
        }
        // @volatiles is not used anymore :-/
        if !(*schema).volatiles.is_null() {
            // TODO
            todo!()
        }

        for (_, entry) in (*schema).schemas_imports.drain() {
            xml_schema_bucket_free(entry as XmlSchemaBucketPtr);
        }
        if !(*schema).includes.is_null() {
            let list: XmlSchemaItemListPtr<*mut c_void> =
                (*schema).includes as XmlSchemaItemListPtr<*mut c_void>;
            for &item in &(*list).items {
                xml_schema_bucket_free(item as XmlSchemaBucketPtr);
            }
            xml_schema_item_list_free(list);
        }
        if !(*schema).annot.is_null() {
            xml_schema_free_annot((*schema).annot);
        }
        // Never free the doc here, since this will be done by the buckets.

        xml_dict_free((*schema).dict);
        let _ = Box::from_raw(schema);
    }
}