xee_xpath/documents.rs
1use iri_string::types::IriStr;
2use xee_interpreter::{
3 context::DocumentsRef,
4 xml::{DocumentHandle, DocumentsError},
5};
6use xot::Xot;
7
8/// A collection of XML documents as can be used by XPath and XSLT.
9///
10/// This collection can be prepared before any XPath or XSLT processing begins.
11///
12/// Alternatively this collection can be added to incrementally during
13/// processing using the `fn:doc` function for instance. Once a document under
14/// a URL is present, it won't be changed.
15#[derive(Debug)]
16pub struct Documents {
17 pub(crate) xot: Xot,
18 pub(crate) documents: DocumentsRef,
19}
20
21impl Documents {
22 /// Create a new empty collection of documents.
23 pub fn new() -> Self {
24 Self {
25 xot: Xot::new(),
26 documents: DocumentsRef::new(),
27 }
28 }
29
30 /// Load a string as an XML document. Designate it with a URI.
31 ///
32 /// Something may go wrong during processing of the XML document; this is
33 /// a [`xot::Error`].
34 pub fn add_string(
35 &mut self,
36 uri: &IriStr,
37 xml: &str,
38 ) -> Result<DocumentHandle, DocumentsError> {
39 self.documents
40 .borrow_mut()
41 .add_string(&mut self.xot, Some(uri), xml)
42 }
43
44 /// Load a string as an XML document without designating it with a URI.
45 ///
46 /// Something may go wrong during processing of the XML document; this is
47 /// a [`xot::Error`].
48 pub fn add_string_without_uri(&mut self, xml: &str) -> Result<DocumentHandle, DocumentsError> {
49 self.documents
50 .borrow_mut()
51 .add_string(&mut self.xot, None, xml)
52 }
53
54 /// Given a handle give back the document node
55 pub fn document_node(&self, handle: DocumentHandle) -> Option<xot::Node> {
56 self.documents.borrow().get_node_by_handle(handle)
57 }
58
59 /// Get a reference to the documents
60 pub fn documents(&self) -> &DocumentsRef {
61 &self.documents
62 }
63
64 /// Get a reference to the Xot arena
65 pub fn xot(&self) -> &Xot {
66 &self.xot
67 }
68
69 /// Get a mutable reference to the Xot arena
70 pub fn xot_mut(&mut self) -> &mut Xot {
71 &mut self.xot
72 }
73}
74
75impl Default for Documents {
76 fn default() -> Self {
77 Self::new()
78 }
79}