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
use std::{
collections::{BTreeMap, HashMap},
num::NonZeroUsize,
sync::Arc,
};
use crate::{
XMLVersion,
sax::NamespaceStack,
uri::URIStr,
xsdtypes::{
FacetType, SimpleTypeDefinition, XML_SCHEMA_DATATYPES_NAMESPACE,
find_builtin_type_definition,
},
};
pub trait RelaxNGDatatypeLibrary: Send + Sync {
/// If a type named `type_name` exists in the library, return `true`,
/// otherwise return `false`.
fn contains(&self, type_name: &str) -> bool;
/// If a type named `type_name` exists in the library, return [`Some`] wrapped around
/// [`true`] if `value` is a valid representation of that type, or [`false`] otherwise.
///
/// Even if a type exists, if the arguments sufficient for determination are not included
/// in `params`, [`None`] is returned.
///
/// If a type named `type_name` does not exist in the library, return [`None`].
///
/// # Reference
/// ISO/IEC 19757-2:2008 9.3.8 data and value pattern
fn validate(
&self,
type_name: &str,
params: &[(Arc<str>, Arc<str>)],
value: &str,
context: &(Arc<URIStr>, BTreeMap<Arc<str>, Arc<str>>),
) -> Option<bool>;
/// If a type named `type_name` exists in the library, return [`Some`] wrapped around
/// [`true`] if `params` is a valid parameter list of that type, or [`false`] otherwise.
///
/// If a type named `type_name` does not exist in the library, return [`None`].
fn validate_params(
&self,
type_name: &str,
params: &[(Arc<str>, Arc<str>)],
context: &(Arc<URIStr>, BTreeMap<Arc<str>, Arc<str>>),
) -> Option<bool>;
/// If a type named `type_name` exists in the library, return [`Some`] wrapped around
/// [`true`] if `lhs` and `rhs` are equal as representations of that type,
/// or [`false`] if they are not equal.
///
/// If a type named `type_name` does not exist in the library, returns [`None`].
///
/// # Reference
/// ISO/IEC 19757-2:2008 9.3.8 data and value pattern
fn eq(
&self,
type_name: &str,
lhs: &str,
cx1: &(Arc<URIStr>, BTreeMap<Arc<str>, Arc<str>>),
rhs: &str,
cx2: &(Arc<URIStr>, BTreeMap<Arc<str>, Arc<str>>),
) -> Option<bool>;
}
pub struct RelaxNGBuiltinDatatypeLibrary;
impl RelaxNGDatatypeLibrary for RelaxNGBuiltinDatatypeLibrary {
fn contains(&self, type_name: &str) -> bool {
matches!(type_name, "string" | "token")
}
fn validate(
&self,
type_name: &str,
_params: &[(Arc<str>, Arc<str>)],
_value: &str,
_context: &(Arc<URIStr>, BTreeMap<Arc<str>, Arc<str>>),
) -> Option<bool> {
match type_name {
"string" | "token" => Some(true),
_ => None,
}
}
fn validate_params(
&self,
type_name: &str,
params: &[(Arc<str>, Arc<str>)],
_context: &(Arc<URIStr>, BTreeMap<Arc<str>, Arc<str>>),
) -> Option<bool> {
self.contains(type_name).then_some(params.is_empty())
}
fn eq(
&self,
type_name: &str,
lhs: &str,
_cx1: &(Arc<URIStr>, BTreeMap<Arc<str>, Arc<str>>),
rhs: &str,
_cx2: &(Arc<URIStr>, BTreeMap<Arc<str>, Arc<str>>),
) -> Option<bool> {
match type_name {
"string" => Some(lhs == rhs),
"token" => {
let lhs = lhs
.split(|c: char| XMLVersion::default().is_whitespace(c))
.filter(|s| !s.is_empty())
.collect::<Vec<_>>();
let rhs = rhs
.split(|c: char| XMLVersion::default().is_whitespace(c))
.filter(|s| !s.is_empty())
.collect::<Vec<_>>();
Some(lhs == rhs)
}
_ => None,
}
}
}
/// # Reference
/// - [Guidelines for using W3C XML Schema Datatypes with RELAX NG](https://relaxng.org/xsd-20010907.html)
pub struct XSDTypeLibrary;
impl XSDTypeLibrary {
fn get(
&self,
type_name: &str,
params: &[(Arc<str>, Arc<str>)],
namespaces: &NamespaceStack,
) -> Option<Arc<SimpleTypeDefinition>> {
let typedef = find_builtin_type_definition(type_name)?;
let mut builder = typedef.clone().derive_by_restriction().ok()?;
for (k, v) in params {
builder = match k.parse::<FacetType>().ok()? {
FacetType::Length => {
let length = v.parse::<usize>().ok()?;
builder.length(length).ok()?
}
FacetType::MinLength => {
let length = v.parse::<usize>().ok()?;
builder.min_length(length).ok()?
}
FacetType::MaxLength => {
let length = v.parse::<usize>().ok()?;
builder.max_length(length).ok()?
}
FacetType::Pattern => {
// Since processing `pattern` increases the number of derivation steps,
// it must be performed last.
//
// As indicated by [SCC: length and minLength or maxLength], there are
// sets of facets that cannot be specified at the same time in different
// derivation steps; therefore, facets other than `pattern` must be
// treated as if they were specified in a single derivation step.
builder
}
FacetType::Enumeration | FacetType::WhiteSpace => {
// 2. Parameters
// - whiteSpace (the builtin derived datatype that specifies the
// desired value for the whiteSpace facet should be used instead)
// - enumeration (the value element should be used instead)
return None;
}
FacetType::MaxInclusive => {
let value = typedef.parse(v.as_ref(), namespaces).ok()?;
builder.max_inclusive(value).ok()?
}
FacetType::MaxExclusive => {
let value = typedef.parse(v.as_ref(), namespaces).ok()?;
builder.max_exclusive(value).ok()?
}
FacetType::MinExclusive => {
let value = typedef.parse(v.as_ref(), namespaces).ok()?;
builder.min_exclusive(value).ok()?
}
FacetType::MinInclusive => {
let value = typedef.parse(v.as_ref(), namespaces).ok()?;
builder.min_inclusive(value).ok()?
}
FacetType::TotalDigits => {
let digits = v.parse().ok().and_then(NonZeroUsize::new)?;
builder.total_digits(digits).ok()?
}
FacetType::FractionDigits => {
let digits = v.parse::<usize>().ok()?;
builder.fraction_digits(digits).ok()?
}
};
}
for (k, v) in params {
if let Ok(FacetType::Pattern) = k.parse() {
// Unlike the XML representation of XSD type definitions, since multiple
// `pattern`s are combined with an AND condition, each `pattern` must be
// treated as if it were specified in a separate derivation step.
//
// 2. Parameters
// If the pattern parameter is specified more than once for a single data element,
// then a string matches the data element only if it matches all of the patterns.
builder = builder
.pattern(v.as_ref())
.ok()?
.build()
.derive_by_restriction()
.ok()?;
}
}
Some(builder.build())
}
}
impl RelaxNGDatatypeLibrary for XSDTypeLibrary {
fn contains(&self, type_name: &str) -> bool {
find_builtin_type_definition(type_name).is_some()
}
fn validate(
&self,
type_name: &str,
params: &[(Arc<str>, Arc<str>)],
value: &str,
context: &(Arc<URIStr>, BTreeMap<Arc<str>, Arc<str>>),
) -> Option<bool> {
let mut namespaces = NamespaceStack::default();
for (k, v) in &context.1 {
namespaces.push(k.as_ref(), v.as_ref());
}
let t = self.get(type_name, params, &namespaces)?;
Some(t.parse(value, &namespaces).is_ok())
}
fn validate_params(
&self,
type_name: &str,
params: &[(Arc<str>, Arc<str>)],
context: &(Arc<URIStr>, BTreeMap<Arc<str>, Arc<str>>),
) -> Option<bool> {
// This line is necessary because `None` is returned only
// when there is no type corresponding to the type name.
find_builtin_type_definition(type_name)?;
let mut namespaces = NamespaceStack::default();
for (k, v) in &context.1 {
namespaces.push(k.as_ref(), v.as_ref());
}
Some(self.get(type_name, params, &namespaces).is_some())
}
fn eq(
&self,
type_name: &str,
lhs: &str,
cx1: &(Arc<URIStr>, BTreeMap<Arc<str>, Arc<str>>),
rhs: &str,
cx2: &(Arc<URIStr>, BTreeMap<Arc<str>, Arc<str>>),
) -> Option<bool> {
let typedef = find_builtin_type_definition(type_name)?;
let mut ns1 = NamespaceStack::default();
for (k, v) in &cx1.1 {
ns1.push(k.as_ref(), v.as_ref());
}
let mut ns2 = NamespaceStack::default();
for (k, v) in &cx2.1 {
ns2.push(k.as_ref(), v.as_ref());
}
Some(
typedef
.parse(lhs, &ns1)
.ok()
.zip(typedef.parse(rhs, &ns2).ok())
.is_some_and(|(lh, rh)| lh == rh),
)
}
}
#[derive(Clone)]
pub struct RelaxNGDatatypeLibraries {
map: HashMap<Arc<str>, Arc<dyn RelaxNGDatatypeLibrary>>,
}
impl RelaxNGDatatypeLibraries {
pub(super) fn get(&self, namespace_name: &str) -> Option<&dyn RelaxNGDatatypeLibrary> {
self.map.get(namespace_name).map(|library| &**library)
}
fn insert(
&mut self,
namespace_name: Arc<str>,
library: Arc<dyn RelaxNGDatatypeLibrary>,
) -> Option<Arc<dyn RelaxNGDatatypeLibrary>> {
self.map.insert(namespace_name, library)
}
}
impl Default for RelaxNGDatatypeLibraries {
fn default() -> Self {
let mut libraries = Self {
map: HashMap::new(),
};
libraries.insert("".into(), Arc::new(RelaxNGBuiltinDatatypeLibrary));
libraries.insert(
XML_SCHEMA_DATATYPES_NAMESPACE.into(),
Arc::new(XSDTypeLibrary),
);
libraries
}
}