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
//! Top-level `BufferDocument` struct assembling all storage primitives.
use std::collections::HashMap;
use bumpalo::Bump;
use crate::namespace::NameTable;
use crate::schema::SchemaSet;
use super::{
BindingRemapTable, BufferDocumentOptions, DocumentKind, ElementIndex, NamespacePageFactory,
Node, NodePages, NodeSourceSpans, NsRef, QNameTable, StringStore, NULL,
};
/// Compact, cache-friendly XML document representation.
///
/// Built on a flat array of 16-byte [`Node`] structs with power-of-2
/// page addressing. All string data lives in the arena or in the
/// [`StringStore`]; qualified names are deduplicated via [`QNameTable`].
#[allow(dead_code)] // fields used by builder/navigator in later steps
pub struct BufferDocument<'a> {
pub(crate) arena: &'a Bump,
pub(crate) kind: DocumentKind,
pub(crate) names: &'a NameTable,
pub(crate) nodes: NodePages<'a>,
pub(crate) qname_table: QNameTable,
pub(crate) strings: StringStore<'a>,
pub(crate) binding_remap: BindingRemapTable,
pub(crate) root: u32,
pub(crate) options: BufferDocumentOptions,
// Side tables
pub(crate) namespace_pages: NamespacePageFactory<'a>,
pub(crate) xml_namespace: NsRef,
pub(crate) element_namespaces: HashMap<u32, NsRef>,
pub(crate) element_index: ElementIndex,
pub(crate) source_spans: NodeSourceSpans,
pub(crate) id_elements: HashMap<Box<str>, u32>,
pub(crate) schema_set: Option<&'a SchemaSet>,
/// Document-level base URI surfaced by `BufferDocNavigator::base_uri()`
/// when no `xml:base` is found and the cursor reaches the document root.
/// Used by CTA fragment evaluation to expose the instance file URI to
/// `fn:base-uri(.)` while leaving the static base URI in
/// `XPathContext::base_uri` free to carry the schema document URI.
pub(crate) fragment_base_uri: Option<&'a str>,
}
impl<'a> BufferDocument<'a> {
// ── Accessors ──────────────────────────────────────────────────────
/// Returns the document kind (full or fragment).
#[inline]
pub fn kind(&self) -> DocumentKind {
self.kind
}
/// Returns the construction options.
#[inline]
pub fn options(&self) -> &BufferDocumentOptions {
&self.options
}
/// Returns the root node index.
#[inline]
pub fn root(&self) -> u32 {
self.root
}
/// Returns the associated schema set, if any.
#[inline]
pub fn schema_set(&self) -> Option<&'a SchemaSet> {
self.schema_set
}
/// Returns the shared name table.
#[inline]
pub fn names(&self) -> &'a NameTable {
self.names
}
// ── Navigation helpers ─────────────────────────────────────────────
/// Returns the first child of `parent` (always `parent + 1`).
///
/// This relies on the document-order layout: the first child node
/// is stored immediately after its parent.
#[inline]
pub fn first_child_of(&self, parent: u32) -> u32 {
parent + 1
}
/// Returns the first content (non-attribute) child of `parent`, or
/// `None` if the element has no children.
///
/// Attribute pairs precede content children in document order.
/// This method skips over them by walking the `next_sibling` chain
/// of attribute nodes until a non-attribute child is found.
pub fn first_content_child_of(&self, parent: u32) -> Option<u32> {
let node = self.nodes.get(parent);
if !node.has_flag(Node::HAS_CHILDREN) {
return None;
}
if !node.has_flag(Node::HAS_ATTRIBUTE) {
return Some(parent + 1);
}
// Walk the attribute next_sibling chain.
// Each attribute is a 2-node pair (Attribute + ChildValue).
let mut cursor = parent + 1; // first attribute
loop {
let attr = self.nodes.get(cursor);
if attr.next_sibling == NULL {
// Last attribute pair — content starts after its ChildValue node.
return Some(cursor + 2);
}
cursor = attr.next_sibling;
}
}
/// Returns the flat index one past the last node in the subtree
/// rooted at `elem`.
///
/// Walks ancestors until a node with a `next_sibling` is found and
/// returns that sibling. If the root is reached without finding a
/// sibling, returns `self.nodes.len()` (end of document).
pub fn subtree_end(&self, elem: u32) -> u32 {
let mut cursor = elem;
loop {
let node = self.nodes.get(cursor);
if node.next_sibling != NULL {
return node.next_sibling;
}
if node.parent == NULL {
return self.nodes.len();
}
cursor = node.parent;
}
}
/// Looks up an element node by its `xml:id` value.
pub fn get_element_by_id(&self, id: &str) -> Option<u32> {
self.id_elements.get(id).copied()
}
// ── CTA fragment configuration ─────────────────────────────────────
/// Reconfigure this document so that `elem_ref` is the root of the
/// XDM tree visible to the navigator (XSD 1.1 §3.12.4 CTA XDM
/// instance shape).
///
/// After this call, `move_to_parent()` from `elem_ref` returns `false`
/// (the synthetic Root node at index 0 is severed from the tree),
/// `move_to_root()` lands on `elem_ref`, and the `following::*` /
/// `preceding::*` axes cannot escape the subtree anchored at
/// `elem_ref`. The optional `base_uri` argument is surfaced by
/// `BufferDocNavigator::base_uri()` when no `xml:base` is found,
/// so `fn:base-uri(.)` can return the instance file URI even
/// though the static base URI in the XPath context is set to the
/// schema document URI.
pub(crate) fn set_cta_fragment(&mut self, elem_ref: u32, base_uri: Option<&'a str>) {
self.kind = DocumentKind::Fragment;
self.root = elem_ref;
self.nodes.update(elem_ref, |n| n.parent = NULL);
self.fragment_base_uri = base_uri;
}
// ── Navigator factory ─────────────────────────────────────────────
/// Creates a navigator positioned at the document root.
pub fn create_navigator(&self) -> super::navigator::BufferDocNavigator<'_> {
super::navigator::BufferDocNavigator::new(self, self.root)
}
/// Creates a navigator positioned at the given node reference.
pub fn create_navigator_at(&self, node_ref: u32) -> super::navigator::BufferDocNavigator<'_> {
super::navigator::BufferDocNavigator::new(self, node_ref)
}
// ── Parsing helpers ───────────────────────────────────────────────
/// Parses an XML document from a reader into a `BufferDocument`.
pub fn from_reader<R: std::io::BufRead>(
reader: R,
arena: &'a Bump,
names: &'a NameTable,
options: BufferDocumentOptions,
schema_set: Option<&'a SchemaSet>,
) -> Result<Self, super::BufferDocumentError> {
let builder =
super::builder::BufferDocumentBuilder::new(arena, names, schema_set, options)?;
builder.build(reader)
}
/// Parses an XML document from a reader with default options.
pub fn from_reader_default<R: std::io::BufRead>(
reader: R,
arena: &'a Bump,
names: &'a NameTable,
) -> Result<Self, super::BufferDocumentError> {
Self::from_reader(reader, arena, names, BufferDocumentOptions::default(), None)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::document::{Node, NodeType, NULL};
/// Helper: builds a minimal `BufferDocument` for testing.
fn make_doc<'a>(arena: &'a Bump, names: &'a NameTable) -> BufferDocument<'a> {
BufferDocument {
arena,
kind: DocumentKind::default(),
names,
nodes: NodePages::new(arena),
qname_table: QNameTable::new(),
strings: StringStore::new(arena),
binding_remap: BindingRemapTable::new(),
root: 0,
options: BufferDocumentOptions::default(),
namespace_pages: NamespacePageFactory::new(arena),
xml_namespace: NsRef::NULL,
element_namespaces: HashMap::new(),
element_index: ElementIndex::new(),
source_spans: NodeSourceSpans::new(),
id_elements: HashMap::new(),
schema_set: None,
fragment_base_uri: None,
}
}
/// Helper: allocate a node and write it.
fn push_node(doc: &mut BufferDocument<'_>, node: Node) -> u32 {
let idx = doc.nodes.alloc().unwrap();
doc.nodes.set(idx, node);
idx
}
/// Helper: create a node with specific type and flags.
fn make_node(nt: NodeType, parent: u32, next_sibling: u32, flags: u32) -> Node {
let mut n = Node::default();
n.set_node_type(nt);
n.parent = parent;
n.next_sibling = next_sibling;
n.props_type |= flags;
n
}
#[test]
fn first_child_of() {
let arena = Bump::new();
let names = NameTable::new();
let doc = make_doc(&arena, &names);
assert_eq!(doc.first_child_of(0), 1);
assert_eq!(doc.first_child_of(5), 6);
assert_eq!(doc.first_child_of(100), 101);
}
#[test]
fn first_content_child_of_no_children() {
let arena = Bump::new();
let names = NameTable::new();
let mut doc = make_doc(&arena, &names);
// Element without HAS_CHILDREN
let elem = make_node(NodeType::Element, NULL, NULL, 0);
push_node(&mut doc, elem);
assert_eq!(doc.first_content_child_of(0), None);
}
#[test]
fn first_content_child_of_no_attrs() {
let arena = Bump::new();
let names = NameTable::new();
let mut doc = make_doc(&arena, &names);
// Element with children but no attributes
let elem = make_node(NodeType::Element, NULL, NULL, Node::HAS_CHILDREN);
push_node(&mut doc, elem); // 0
// First child is text
let text = make_node(NodeType::Text, 0, NULL, 0);
push_node(&mut doc, text); // 1
assert_eq!(doc.first_content_child_of(0), Some(1));
}
#[test]
fn first_content_child_of_with_attrs() {
let arena = Bump::new();
let names = NameTable::new();
let mut doc = make_doc(&arena, &names);
// Element with both attributes and children
let elem = make_node(
NodeType::Element,
NULL,
NULL,
Node::HAS_CHILDREN | Node::HAS_ATTRIBUTE,
);
push_node(&mut doc, elem); // 0
// Attribute 1 (pair: Attribute + ChildValue)
// next_sibling points to the next attribute at index 3
let attr1 = make_node(NodeType::Attribute, 0, 3, 0);
push_node(&mut doc, attr1); // 1
let val1 = make_node(NodeType::ChildValue, 0, NULL, 0);
push_node(&mut doc, val1); // 2
// Attribute 2 (pair: Attribute + ChildValue)
// next_sibling = NULL → last attribute
let attr2 = make_node(NodeType::Attribute, 0, NULL, 0);
push_node(&mut doc, attr2); // 3
let val2 = make_node(NodeType::ChildValue, 0, NULL, 0);
push_node(&mut doc, val2); // 4
// Content child (text) at index 5
let text = make_node(NodeType::Text, 0, NULL, 0);
push_node(&mut doc, text); // 5
// first_content_child_of should skip both attribute pairs → index 5
assert_eq!(doc.first_content_child_of(0), Some(5));
}
#[test]
fn subtree_end_with_sibling() {
let arena = Bump::new();
let names = NameTable::new();
let mut doc = make_doc(&arena, &names);
// Root at 0
let root = make_node(NodeType::Root, NULL, NULL, Node::HAS_CHILDREN);
push_node(&mut doc, root); // 0
// Element at 1, has sibling at 2
let elem = make_node(NodeType::Element, 0, 2, 0);
push_node(&mut doc, elem); // 1
// Sibling element at 2
let sib = make_node(NodeType::Element, 0, NULL, 0);
push_node(&mut doc, sib); // 2
assert_eq!(doc.subtree_end(1), 2);
}
#[test]
fn subtree_end_walks_ancestors() {
let arena = Bump::new();
let names = NameTable::new();
let mut doc = make_doc(&arena, &names);
// Root at 0, has sibling at 5 (hypothetical)
let root = make_node(NodeType::Root, NULL, NULL, Node::HAS_CHILDREN);
push_node(&mut doc, root); // 0
// Parent element at 1, has next_sibling at 4
let parent = make_node(NodeType::Element, 0, 4, Node::HAS_CHILDREN);
push_node(&mut doc, parent); // 1
// Nested child element at 2, no sibling
let child = make_node(NodeType::Element, 1, NULL, 0);
push_node(&mut doc, child); // 2
// Text at 3 (unused, just to fill space)
let text = make_node(NodeType::Text, 1, NULL, 0);
push_node(&mut doc, text); // 3
// Sibling of parent at 4
let uncle = make_node(NodeType::Element, 0, NULL, 0);
push_node(&mut doc, uncle); // 4
// child(2) has no sibling → walk to parent(1) which has sibling 4
assert_eq!(doc.subtree_end(2), 4);
}
#[test]
fn subtree_end_at_document_end() {
let arena = Bump::new();
let names = NameTable::new();
let mut doc = make_doc(&arena, &names);
// Root at 0, no sibling, parent = NULL
let root = make_node(NodeType::Root, NULL, NULL, Node::HAS_CHILDREN);
push_node(&mut doc, root); // 0
// Single child at 1, no sibling
let elem = make_node(NodeType::Element, 0, NULL, 0);
push_node(&mut doc, elem); // 1
// elem(1) has no sibling → walk to root(0) which has parent = NULL → nodes.len()
assert_eq!(doc.subtree_end(1), doc.nodes.len());
assert_eq!(doc.subtree_end(1), 2);
}
#[test]
fn get_element_by_id_found() {
let arena = Bump::new();
let names = NameTable::new();
let mut doc = make_doc(&arena, &names);
doc.id_elements.insert("foo".into(), 42);
assert_eq!(doc.get_element_by_id("foo"), Some(42));
}
#[test]
fn get_element_by_id_not_found() {
let arena = Bump::new();
let names = NameTable::new();
let doc = make_doc(&arena, &names);
assert_eq!(doc.get_element_by_id("nonexistent"), None);
}
}