exml 0.7.3-deprecated

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
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
// Copyright of the original code is the following.
// --------
// Summary: interfaces for tree manipulation
// Description: this module describes the structures found in an tree resulting
//              from an XML or HTML parsing, as well as the API provided for
//              various processing on that tree
//
// Copy: See Copyright for the status of this software.
//
// Author: Daniel Veillard
// --------
// tree.c : implementation of access function for an XML tree.
//
// References:
//   XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/
//
// See Copyright for the status of this software.
//
// daniel@veillard.com

use std::{cell::RefCell, io::Write, mem::take, rc::Rc};

use crate::{
    encoding::find_encoding_handler,
    error::XmlParserErrors,
    io::XmlOutputBuffer,
    parser::xml_init_parser,
    save::{
        XmlSaveCtxt, XmlSaveOption, xml_node_dump_output_internal, xml_save_err,
        xml_save_err_memory,
    },
    tree::XmlNodePtr,
};
#[cfg(feature = "html")]
use crate::{html::tree::html_node_dump_output, save::xhtml_node_dump_output, tree::is_xhtml};

use super::{XmlDoc, XmlDocPtr, XmlElementType, XmlGenericNodePtr, XmlNode};

impl XmlDoc {
    /// Dump the current DOM tree into memory using the character encoding specified by the caller.  
    ///
    /// Note it is up to the caller of this function to free the allocated memory with xml_free().
    ///
    /// Note that `format` = 1 provide node indenting only if xmlIndentTreeOutput = 1
    /// or xmlKeepBlanksDefault(0) was called
    #[doc(alias = "xmlDocDumpFormatMemoryEnc")]
    pub unsafe fn dump_format_memory_enc(
        &mut self,
        txt: &mut Vec<u8>,
        txt_encoding: Option<&str>,
        format: i32,
    ) {
        unsafe {
            let mut ctxt = XmlSaveCtxt::default();

            // Validate the encoding value, if provided.
            // This logic is copied from xmlSaveFileEnc.
            let encoding = txt_encoding.map_or_else(
                || self.encoding.as_ref().map(|e| e.to_owned()),
                |e| Some(e.to_owned()),
            );
            let conv_hdlr = if let Some(encoding) = encoding.as_deref() {
                let Some(handler) = find_encoding_handler(encoding) else {
                    xml_save_err(
                        XmlParserErrors::XmlSaveUnknownEncoding,
                        XmlGenericNodePtr::from_raw(self),
                        Some(encoding),
                    );
                    return;
                };
                Some(handler)
            } else {
                None
            };

            let Some(out_buff) = XmlOutputBuffer::new(conv_hdlr) else {
                xml_save_err_memory("creating buffer");
                return;
            };

            ctxt.buf = out_buff;
            ctxt.level = 0;
            ctxt.format = (format != 0) as i32;
            ctxt.encoding = encoding;
            ctxt.init();
            ctxt.options |= XmlSaveOption::XmlSaveAsXML as i32;
            ctxt.doc_content_dump_output(XmlDocPtr::from_raw(self).unwrap().unwrap());
            ctxt.buf.flush();
            if ctxt.buf.encoder.is_some() {
                txt.extend(&ctxt.buf.conv);
            } else {
                txt.extend(&ctxt.buf.buffer);
            }
        }
    }

    /// Dump an XML document in memory and return the `*mut XmlChar` and it's size.  
    /// It's up to the caller to free the memory with xml_free().
    ///
    /// Note that `format = 1` provide node indenting only if `xmlIndentTreeOutput = 1`
    /// or `xmlKeepBlanksDefault(0)` was called
    #[doc(alias = "xmlDocDumpFormatMemory")]
    pub unsafe fn dump_format_memory(&mut self, mem: &mut Vec<u8>, format: i32) {
        unsafe {
            self.dump_format_memory_enc(mem, None, format);
        }
    }

    /// Dump an XML document in memory and return the `*mut XmlChar` and it's size in bytes.   
    /// It's up to the caller to free the memory with xml_free().
    ///
    /// The resulting byte array is zero terminated, though the last 0 is not
    /// included in the returned size.
    #[doc(alias = "xmlDocDumpMemory")]
    pub unsafe fn dump_memory(&mut self, mem: &mut Vec<u8>) {
        unsafe {
            self.dump_format_memory_enc(mem, None, 0);
        }
    }

    /// Dump the current DOM tree into memory using the character encoding specified by the caller.  
    ///
    /// Note it is up to the caller of this function to free the allocated memory with `xml_free()`.
    #[doc(alias = "xmlDocDumpMemoryEnc")]
    pub unsafe fn dump_memory_enc(&mut self, txt: &mut Vec<u8>, txt_encoding: Option<&str>) {
        unsafe {
            self.dump_format_memory_enc(txt, txt_encoding, 0);
        }
    }

    /// Dump an XML document to an open FILE.
    ///
    /// returns: the number of bytes written or -1 in case of failure.
    ///
    /// Note that `format = 1` provide node indenting only if `xmlIndentTreeOutput = 1`
    /// or `xmlKeepBlanksDefault(0)` was called
    #[doc(alias = "xmlDocFormatDump")]
    pub unsafe fn dump_format_file(&mut self, f: &mut impl Write, format: i32) -> i32 {
        unsafe {
            let mut encoding = self.encoding.clone();

            let handler = if let Some(enc) = self.encoding.as_deref() {
                if let Some(handler) = find_encoding_handler(enc) {
                    Some(handler)
                } else {
                    self.encoding = None;
                    encoding = None;
                    None
                }
            } else {
                None
            };
            let Some(buf) = XmlOutputBuffer::from_writer(f, handler) else {
                return -1;
            };

            let mut ctxt = XmlSaveCtxt {
                buf,
                level: 0,
                format: (format != 0) as i32,
                encoding,
                handler: None,
                filename: None,
                ..Default::default()
            };
            ctxt.init();
            ctxt.options |= XmlSaveOption::XmlSaveAsXML as i32;
            ctxt.doc_content_dump_output(XmlDocPtr::from_raw(self).unwrap().unwrap());

            let XmlSaveCtxt { ref mut buf, .. } = ctxt;

            if buf.error.is_ok() {
                buf.flush();
                buf.written
            } else {
                -1
            }
        }
    }

    /// Dump an XML document to an open FILE.
    ///
    /// returns: the number of bytes written or -1 in case of failure.
    #[doc(alias = "xmlDocDump")]
    pub unsafe fn dump_file<'a>(&mut self, f: &mut (impl Write + 'a)) -> i32 {
        unsafe { self.dump_format_file(f, 0) }
    }

    /// Dump an XML document to a file or an URL.
    ///
    /// Returns the number of bytes written or -1 in case of error.  
    ///
    /// Note that `format` = 1 provide node indenting only if `xmlIndentTreeOutput = 1`
    /// or `xmlKeepBlanksDefault(0)` was called
    #[doc(alias = "xmlSaveFormatFileEnc")]
    pub unsafe fn save_format_file_enc(
        &mut self,
        filename: &str,
        encoding: Option<&str>,
        format: i32,
    ) -> i32 {
        unsafe {
            let encoding = encoding.map_or_else(
                || self.encoding.as_ref().map(|e| e.to_owned()),
                |e| Some(e.to_owned()),
            );
            let handler = if let Some(enc) = encoding.as_deref() {
                let Some(handler) = find_encoding_handler(enc) else {
                    return -1;
                };
                Some(handler)
            } else {
                None
            };

            // save the content to a temp buffer.
            let Some(buf) =
                XmlOutputBuffer::from_uri(filename, handler.map(|e| Rc::new(RefCell::new(e))))
            else {
                return -1;
            };
            let mut ctxt = XmlSaveCtxt {
                buf,
                level: 0,
                format: (format != 0) as i32,
                encoding,
                handler: None,
                filename: None,
                ..Default::default()
            };
            ctxt.init();
            ctxt.options |= XmlSaveOption::XmlSaveAsXML as i32;
            ctxt.doc_content_dump_output(XmlDocPtr::from_raw(self).unwrap().unwrap());

            let XmlSaveCtxt { ref mut buf, .. } = ctxt;
            if buf.error.is_ok() {
                buf.flush();
                buf.written
            } else {
                -1
            }
        }
    }

    /// Dump an XML document to a file. Will use compression if compiled in and enabled.  
    ///
    /// If `filename` is "-" the stdout file is used.   
    /// If `format` is set then the document will be indented on output.
    ///
    /// Note that `format = 1` provide node indenting only if `xmlIndentTreeOutput = 1`
    /// or `xmlKeepBlanksDefault(0)` was called.
    ///
    /// returns: the number of bytes written or -1 in case of failure.
    #[doc(alias = "xmlSaveFormatFile")]
    pub unsafe fn save_format_file(&mut self, filename: &str, format: i32) -> i32 {
        unsafe { self.save_format_file_enc(filename, None, format) }
    }

    /// Dump an XML document, converting it to the given encoding.
    ///
    /// returns: the number of bytes written or -1 in case of failure.
    #[doc(alias = "xmlSaveFileEnc")]
    pub unsafe fn save_file_enc(&mut self, filename: &str, encoding: Option<&str>) -> i32 {
        unsafe { self.save_format_file_enc(filename, encoding, 0) }
    }

    /// Dump an XML document to a file. Will use compression if compiled in and enabled.  
    ///
    /// If `filename` is "-" the stdout file is used.  
    /// returns: the number of bytes written or -1 in case of failure.
    #[doc(alias = "xmlSaveFile")]
    pub unsafe fn save_file(&mut self, filename: &str) -> i32 {
        unsafe { self.save_format_file_enc(filename, None, 0) }
    }

    /// Dump an XML document to an I/O buffer.
    ///
    /// returns: the number of bytes written or -1 in case of failure.
    ///
    /// # Warning
    /// This call xmlOutputBufferClose() on buf which is not available after this call.
    #[doc(alias = "xmlSaveFormatFileTo")]
    pub unsafe fn save_format_file_to(
        &mut self,
        buf: XmlOutputBuffer,
        encoding: Option<&str>,
        format: i32,
    ) -> i32 {
        unsafe {
            if !matches!(
                self.typ,
                XmlElementType::XmlDocumentNode | XmlElementType::XmlHTMLDocumentNode
            ) {
                return -1;
            }
            let mut ctxt = XmlSaveCtxt {
                buf,
                level: 0,
                format: (format != 0) as i32,
                encoding: encoding.map(|e| e.to_owned()),
                handler: None,
                filename: None,
                ..Default::default()
            };
            ctxt.init();
            ctxt.options |= XmlSaveOption::XmlSaveAsXML as i32;
            ctxt.doc_content_dump_output(XmlDocPtr::from_raw(self).unwrap().unwrap());

            let XmlSaveCtxt { ref mut buf, .. } = ctxt;
            if buf.error.is_ok() {
                buf.flush();
                buf.written
            } else {
                -1
            }
        }
    }

    /// Dump an XML document to an I/O buffer.
    ///
    /// returns: the number of bytes written or -1 in case of failure.
    ///
    /// # Warning
    ///  This call xmlOutputBufferClose() on buf which is not available after this call.
    #[doc(alias = "xmlSaveFileTo")]
    pub unsafe fn save_file_to(&mut self, buf: XmlOutputBuffer, encoding: Option<&str>) -> i32 {
        unsafe {
            let mut ctxt = XmlSaveCtxt {
                buf,
                level: 0,
                format: 0,
                encoding: encoding.map(|e| e.to_owned()),
                handler: None,
                filename: None,
                ..Default::default()
            };
            ctxt.init();
            ctxt.options |= XmlSaveOption::XmlSaveAsXML as i32;
            ctxt.doc_content_dump_output(XmlDocPtr::from_raw(self).unwrap().unwrap());

            let XmlSaveCtxt { ref mut buf, .. } = ctxt;
            if buf.error.is_ok() {
                buf.flush();
                buf.written
            } else {
                -1
            }
        }
    }
}

impl XmlNode {
    /// Dump an XML node, recursive behaviour, children are printed too.  
    ///
    /// Note that `format = 1` provide node indenting only if `xmlIndentTreeOutput = 1`
    /// or `xmlKeepBlanksDefault(0)` was called.
    #[doc(alias = "xmlNodeDumpOutput")]
    pub unsafe fn dump_output(
        &mut self,
        buf: XmlOutputBuffer,
        #[cfg_attr(not(feature = "html"), allow(unused))] doc: Option<XmlDocPtr>,
        level: i32,
        format: i32,
        mut encoding: Option<&str>,
    ) {
        unsafe {
            xml_init_parser();

            if encoding.is_none() {
                encoding = Some("UTF-8");
            }

            let mut ctxt = XmlSaveCtxt {
                buf,
                level,
                format: (format != 0) as i32,
                encoding: encoding.map(|e| e.to_owned()),
                handler: None,
                filename: None,
                ..Default::default()
            };
            ctxt.init();
            ctxt.options |= XmlSaveOption::XmlSaveAsXML as i32;

            #[cfg(feature = "html")]
            {
                let mut is_html = false;
                let dtd = doc.and_then(|doc| doc.get_int_subset());
                if let Some(dtd) = dtd {
                    is_html = is_xhtml(dtd.system_id.as_deref(), dtd.external_id.as_deref());
                }

                if is_html {
                    xhtml_node_dump_output(
                        &mut ctxt,
                        XmlNodePtr::from_raw(self).unwrap().unwrap().into(),
                    );
                } else {
                    xml_node_dump_output_internal(
                        &mut ctxt as _,
                        XmlNodePtr::from_raw(self).unwrap().unwrap().into(),
                    );
                }
            }
            #[cfg(not(feature = "html"))]
            {
                xml_node_dump_output_internal(
                    &mut ctxt as _,
                    XmlNodePtr::from_raw(self).unwrap().unwrap().into(),
                );
            }
            ctxt.buf.flush();
        }
    }

    /// Dump an XML/HTML node, recursive behaviour, children are printed too.
    #[doc(alias = "xmlElemDump")]
    pub unsafe fn dump_file<'a>(&mut self, f: &mut (impl Write + 'a), doc: Option<XmlDocPtr>) {
        unsafe {
            xml_init_parser();

            let Some(mut outbuf) = XmlOutputBuffer::from_writer(f, None) else {
                return;
            };
            #[cfg_attr(not(feature = "html"), allow(unused))]
            if let Some(doc) =
                doc.filter(|doc| matches!(doc.typ, XmlElementType::XmlHTMLDocumentNode))
            {
                #[cfg(feature = "html")]
                {
                    html_node_dump_output(
                        &mut outbuf,
                        Some(doc),
                        XmlGenericNodePtr::from_raw(self).unwrap(),
                        None,
                    );
                }
                #[cfg(not(feature = "html"))]
                {
                    xml_save_err(
                        XmlParserErrors::XmlErrInternalError,
                        XmlGenericNodePtr::from_raw(self),
                        Some("HTML support not compiled in\n"),
                    );
                }
                outbuf.flush();
            } else {
                self.dump_output(outbuf, doc, 0, 1, None);
            }
        }
    }

    /// Dump an XML node, recursive behaviour,children are printed too.
    ///
    /// Note that `format = 1` provide node indenting only if `xmlIndentTreeOutput = 1`
    /// or `xmlKeepBlanksDefault(0)` was called.
    ///
    /// Returns the number of bytes written to the buffer, in case of error 0
    /// is returned or `buf` stores the error.
    #[doc(alias = "xmlBufNodeDump")]
    pub unsafe fn dump_memory(
        &mut self,
        buf: &mut Vec<u8>,
        doc: Option<XmlDocPtr>,
        level: i32,
        format: i32,
    ) -> usize {
        unsafe {
            xml_init_parser();

            let Some(outbuf) = XmlOutputBuffer::from_writer(&mut *buf, None) else {
                return usize::MAX;
            };

            self.dump_output(outbuf, doc, level, format, None);
            buf.len()
        }
    }
}

impl XmlGenericNodePtr {
    /// Dump an XML node, recursive behaviour, children are printed too.  
    ///
    /// Note that `format = 1` provide node indenting only if `xmlIndentTreeOutput = 1`
    /// or `xmlKeepBlanksDefault(0)` was called.
    #[doc(alias = "xmlNodeDumpOutput")]
    pub unsafe fn dump_output<'a>(
        self,
        buf: XmlOutputBuffer<'a>,
        #[cfg_attr(not(feature = "html"), allow(unused))] doc: Option<XmlDocPtr>,
        level: i32,
        format: i32,
        mut encoding: Option<&str>,
    ) -> XmlOutputBuffer<'a> {
        unsafe {
            xml_init_parser();

            if encoding.is_none() {
                encoding = Some("UTF-8");
            }

            let mut ctxt = XmlSaveCtxt {
                buf,
                level,
                format: (format != 0) as i32,
                encoding: encoding.map(|e| e.to_owned()),
                handler: None,
                filename: None,
                ..Default::default()
            };
            ctxt.init();
            ctxt.options |= XmlSaveOption::XmlSaveAsXML as i32;

            #[cfg(feature = "html")]
            {
                let mut is_html = false;
                let dtd = doc.and_then(|doc| doc.get_int_subset());
                if let Some(dtd) = dtd {
                    is_html = is_xhtml(dtd.system_id.as_deref(), dtd.external_id.as_deref());
                }

                if is_html {
                    xhtml_node_dump_output(&mut ctxt, self);
                } else {
                    xml_node_dump_output_internal(&mut ctxt as _, self);
                }
            }
            #[cfg(not(feature = "html"))]
            {
                xml_node_dump_output_internal(&mut ctxt as _, self);
            }
            ctxt.buf.flush();
            take(&mut ctxt.buf)
        }
    }

    /// Dump an XML/HTML node, recursive behaviour, children are printed too.
    #[doc(alias = "xmlElemDump")]
    pub unsafe fn dump_file<'a>(self, f: &mut (impl Write + 'a), doc: Option<XmlDocPtr>) {
        unsafe {
            xml_init_parser();

            let Some(mut outbuf) = XmlOutputBuffer::from_writer(f, None) else {
                return;
            };
            #[cfg_attr(not(feature = "html"), allow(unused))]
            if let Some(doc) =
                doc.filter(|doc| matches!(doc.typ, XmlElementType::XmlHTMLDocumentNode))
            {
                #[cfg(feature = "html")]
                {
                    html_node_dump_output(&mut outbuf, Some(doc), self, None);
                }
                #[cfg(not(feature = "html"))]
                {
                    xml_save_err(
                        XmlParserErrors::XmlErrInternalError,
                        Some(self),
                        Some("HTML support not compiled in\n"),
                    );
                }
                outbuf.flush();
            } else {
                self.dump_output(outbuf, doc, 0, 1, None);
            }
        }
    }

    /// Dump an XML node, recursive behaviour,children are printed too.
    ///
    /// Note that `format = 1` provide node indenting only if `xmlIndentTreeOutput = 1`
    /// or `xmlKeepBlanksDefault(0)` was called.
    ///
    /// Returns the number of bytes written to the buffer, in case of error 0
    /// is returned or `buf` stores the error.
    #[doc(alias = "xmlBufNodeDump")]
    pub unsafe fn dump_memory(
        self,
        buf: &mut Vec<u8>,
        doc: Option<XmlDocPtr>,
        level: i32,
        format: i32,
    ) -> usize {
        unsafe {
            xml_init_parser();

            let Some(outbuf) = XmlOutputBuffer::from_writer(&mut *buf, None) else {
                return usize::MAX;
            };

            self.dump_output(outbuf, doc, level, format, None);
            buf.len()
        }
    }
}