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
use anyhow::Result;
use iri_string::types::{IriAbsoluteStr, IriReferenceString, IriString};
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::{Path, PathBuf};
use xee_xpath::{context, Documents, Queries, Query};
use xee_xpath_load::{convert_string, ContextLoadable};
use crate::catalog::LoadContext;
use crate::metadata::Metadata;
use crate::paths::Mode;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Source {
pub(crate) metadata: Metadata,
// note that in a collection source the role can be ommitted, so
// we may need to define this differently
pub(crate) role: SourceRole,
pub(crate) content: SourceContent,
// this can be optional at least in XSLT mode
pub(crate) validation: Option<Validation>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum SourceContent {
// load from file
Path(PathBuf),
// load from directly included content (XSLT only)
Content(String),
// execute string as xpath expression, should result in singleton (XSLT only)
Select(String),
// content and then select xpath expression
ContentAndSelect(String, String),
}
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Validation {
Strict,
Lax,
Skip,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum SourceRole {
Context,
Var(String), // only in XPath
ContextAndDoc(IriReferenceString), // context & doc combined
}
impl Source {
pub(crate) fn node(
&self,
base_dir: &Path,
documents: &mut Documents,
base_uri: Option<&IriAbsoluteStr>,
) -> Result<xot::Node> {
// if we have a role that requires a URI we need to resolve it
let uri: Option<IriString> = match &self.role {
SourceRole::ContextAndDoc(uri) => {
if let Some(base_uri) = base_uri {
Some(uri.resolve_against(base_uri).into())
} else {
panic!("Cannot resolve relative URL")
}
}
_ => None,
};
match &self.content {
SourceContent::Path(path) => {
// this path resolution code is decidedly ugly
// TODO: would be nice if we could get rid of options somewhere
// down the line earlier and resolve earlier.
let full_path = base_dir.join(path);
// try to get the cached version of the document
{
// scope borrowed_documents so we drop it afterward
let borrowed_documents = documents.documents().borrow();
// when we load something from a path, we first check if we
// happen to know it under a URI already
if let Some(uri) = &uri {
// if we know it, we try to look it up
let root = borrowed_documents.get_node_by_uri(uri);
if let Some(root) = root {
return Ok(root);
}
}
}
// could not get cached version, so load up document
let xml_file = File::open(&full_path)?;
let mut buf_reader = BufReader::new(xml_file);
let mut xml = String::new();
buf_reader.read_to_string(&mut xml)?;
let documents_ref = documents.documents().clone();
let handle = documents_ref.borrow_mut().add_string(
documents.xot_mut(),
uri.as_deref(),
&xml,
)?;
Ok(documents
.documents()
.borrow()
.get_node_by_handle(handle)
.unwrap())
}
SourceContent::Content(value) => {
// we don't try to get a cached version of the document, as
// that would be different each time. we just add it to documents
// and return it
// TODO: is this right?
let documents_ref = documents.documents().clone();
let handle = documents_ref.borrow_mut().add_string(
documents.xot_mut(),
uri.as_deref(),
value,
)?;
Ok(documents
.documents()
.borrow()
.get_node_by_handle(handle)
.unwrap())
}
SourceContent::ContentAndSelect(_value, _select) => {
todo!("Don't know yet how to execute xpath here")
}
SourceContent::Select(_value) => {
todo!("Don't know yet how to execute xpath here")
}
}
}
}
pub(crate) struct Sources {
pub(crate) sources: Vec<Source>,
}
impl ContextLoadable<LoadContext> for Sources {
fn static_context_builder(context: &LoadContext) -> context::StaticContextBuilder {
let mut builder = context::StaticContextBuilder::default();
builder.default_element_namespace(context.catalog_ns);
builder
}
fn load_with_context(queries: &Queries, context: &LoadContext) -> Result<impl Query<Self>> {
let file_query = queries.option("@file/string()", convert_string)?;
let xpath_content_query = queries.one("content/string()", convert_string)?;
let role_query = queries.option("@role/string()", convert_string)?;
let uri_query = queries.option("@uri/string()", convert_string)?;
let metadata_query = Metadata::load_with_context(queries, context)?;
let xslt_select_query = queries.option("@select/string()", convert_string)?;
let xslt_content_query = queries.option("content/string()", convert_string)?;
let sources_query = queries.many("source", move |documents, item| {
let content = if let Some(file) = file_query.execute(documents, item)? {
SourceContent::Path(PathBuf::from(file))
} else {
// HACK: we'd prefer to avoid mode dependence in the
// code, but unfortunately source is parsed differently
// based on the mode and this is the easiest way
match context.mode {
Mode::XPath => {
// if we're in xpath mode, we take the content inside as an xpath expression
let s = xpath_content_query.execute(documents, item)?;
SourceContent::Content(s)
}
Mode::Xslt => {
// we can get content from the content element
let content = xslt_content_query.execute(documents, item)?;
let select = xslt_select_query.execute(documents, item)?;
match (content, select) {
(Some(content), Some(select)) => {
SourceContent::ContentAndSelect(content, select)
}
(Some(content), None) => SourceContent::Content(content),
// we cannot execute the select xpath expression here, we do it later
(None, Some(select)) => SourceContent::Select(select),
(None, None) => panic!("Must have content or select"),
}
}
}
};
let role = role_query.execute(documents, item)?;
let uri = uri_query.execute(documents, item)?;
let uri: Option<IriReferenceString> = if let Some(uri) = uri {
Some(uri.try_into().unwrap())
} else {
// HACK: this is a weird hack. if there's no uri attribute
// then we wildly turn the path into the URI.
// This is required for a few tests that depend on
// the environment works-mod for instance,
// which even it doesn't have a URI attribute in its
// source, still seems fn-document-uri-20 to result in
// a document with works-mod in its URI
match &content {
// if there is no uri attribute, use the file attribute as the url
SourceContent::Path(path) => {
let uri = path.to_string_lossy().to_string();
Some(uri.try_into().unwrap())
}
SourceContent::Content(_)
| SourceContent::Select(_)
| SourceContent::ContentAndSelect(_, _) => None,
}
};
let metadata = metadata_query.execute(documents, item)?;
let source = if let Some(role) = role {
if role == "." {
// it's possible to have a uri and a . role
// at the same time
if let Some(uri) = uri {
Source {
metadata: metadata.clone(),
role: SourceRole::ContextAndDoc(uri),
content: content.clone(),
validation: None,
}
} else {
Source {
metadata: metadata.clone(),
role: SourceRole::Context,
content: content.clone(),
validation: None,
}
}
} else {
Source {
metadata: metadata.clone(),
role: SourceRole::Var(role),
content: content.clone(),
validation: None,
}
}
} else if let Some(uri) = uri {
Source {
metadata: metadata.clone(),
role: SourceRole::ContextAndDoc(uri),
content: content.clone(),
validation: None,
}
} else {
Source {
metadata: metadata.clone(),
role: SourceRole::Context,
content: content.clone(),
validation: None,
}
};
Ok(source)
})?;
let all_sources_query = queries.one(".", move |documents, item| {
let sources = sources_query.execute(documents, item)?;
Ok(Sources { sources })
})?;
Ok(all_sources_query)
}
}