xml/namespace.rs
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
//! Contains namespace manipulation types and functions.
use std::borrow::Cow;
use std::collections::btree_map::Iter as Entries;
use std::collections::btree_map::{BTreeMap, Entry};
use std::collections::HashSet;
use std::iter::{Map, Rev};
use std::slice::Iter;
/// Designates prefix for namespace definitions.
///
/// See [Namespaces in XML][namespace] spec for more information.
///
/// [namespace]: http://www.w3.org/TR/xml-names/#ns-decl
pub const NS_XMLNS_PREFIX: &str = "xmlns";
/// Designates the standard URI for `xmlns` prefix.
///
/// See [A Namespace Name for xmlns Attributes][namespace] for more information.
///
/// [namespace]: http://www.w3.org/2000/xmlns/
pub const NS_XMLNS_URI: &str = "http://www.w3.org/2000/xmlns/";
/// Designates prefix for a namespace containing several special predefined attributes.
///
/// See [2.10 White Space handling][1], [2.1 Language Identification][2],
/// [XML Base specification][3] and [xml:id specification][4] for more information.
///
/// [1]: http://www.w3.org/TR/REC-xml/#sec-white-space
/// [2]: http://www.w3.org/TR/REC-xml/#sec-lang-tag
/// [3]: http://www.w3.org/TR/xmlbase/
/// [4]: http://www.w3.org/TR/xml-id/
pub const NS_XML_PREFIX: &str = "xml";
/// Designates the standard URI for `xml` prefix.
///
/// See `NS_XML_PREFIX` documentation for more information.
pub const NS_XML_URI: &str = "http://www.w3.org/XML/1998/namespace";
/// Designates the absence of prefix in a qualified name.
///
/// This constant should be used to define or query default namespace which should be used
/// for element or attribute names without prefix. For example, if a namespace mapping
/// at a particular point in the document contains correspondence like
///
/// ```none
/// NS_NO_PREFIX --> urn:some:namespace
/// ```
///
/// then all names declared without an explicit prefix `urn:some:namespace` is assumed as
/// a namespace URI.
///
/// By default empty prefix corresponds to absence of namespace, but this can change either
/// when writing an XML document (manually) or when reading an XML document (based on namespace
/// declarations).
pub const NS_NO_PREFIX: &str = "";
/// Designates an empty namespace URI, which is equivalent to absence of namespace.
///
/// This constant should not usually be used directly; it is used to designate that
/// empty prefix corresponds to absent namespace in `NamespaceStack` instances created with
/// `NamespaceStack::default()`. Therefore, it can be used to restore `NS_NO_PREFIX` mapping
/// in a namespace back to its default value.
pub const NS_EMPTY_URI: &str = "";
/// Namespace is a map from prefixes to namespace URIs.
///
/// No prefix (i.e. default namespace) is designated by `NS_NO_PREFIX` constant.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Namespace(pub BTreeMap<String, String>);
impl Namespace {
/// Returns an empty namespace.
#[inline]
#[must_use]
pub fn empty() -> Self {
Self(BTreeMap::new())
}
/// Checks whether this namespace is empty.
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Checks whether this namespace is essentially empty, that is, it does not contain
/// anything but default mappings.
#[must_use]
pub fn is_essentially_empty(&self) -> bool {
// a shortcut for a namespace which is definitely not empty
if self.0.len() > 3 { return false; }
self.0.iter().all(|(k, v)| matches!((&**k, &**v),
(NS_NO_PREFIX, NS_EMPTY_URI) |
(NS_XMLNS_PREFIX, NS_XMLNS_URI) |
(NS_XML_PREFIX, NS_XML_URI))
)
}
/// Checks whether this namespace mapping contains the given prefix.
///
/// # Parameters
/// * `prefix` --- namespace prefix.
///
/// # Return value
/// `true` if this namespace contains the given prefix, `false` otherwise.
#[inline]
pub fn contains<P: ?Sized + AsRef<str>>(&self, prefix: &P) -> bool {
self.0.contains_key(prefix.as_ref())
}
/// Puts a mapping into this namespace.
///
/// This method does not override any already existing mappings.
///
/// Returns a boolean flag indicating whether the map already contained
/// the given prefix.
///
/// # Parameters
/// * `prefix` --- namespace prefix;
/// * `uri` --- namespace URI.
///
/// # Return value
/// `true` if `prefix` has been inserted successfully; `false` if the `prefix`
/// was already present in the namespace.
pub fn put<P, U>(&mut self, prefix: P, uri: U) -> bool
where P: Into<String>, U: Into<String>
{
match self.0.entry(prefix.into()) {
Entry::Occupied(_) => false,
Entry::Vacant(ve) => {
ve.insert(uri.into());
true
},
}
}
/// Puts a mapping into this namespace forcefully.
///
/// This method, unlike `put()`, does replace an already existing mapping.
///
/// Returns previous URI which was assigned to the given prefix, if it is present.
///
/// # Parameters
/// * `prefix` --- namespace prefix;
/// * `uri` --- namespace URI.
///
/// # Return value
/// `Some(uri)` with `uri` being a previous URI assigned to the `prefix`, or
/// `None` if such prefix was not present in the namespace before.
pub fn force_put<P, U>(&mut self, prefix: P, uri: U) -> Option<String>
where P: Into<String>, U: Into<String>
{
self.0.insert(prefix.into(), uri.into())
}
/// Queries the namespace for the given prefix.
///
/// # Parameters
/// * `prefix` --- namespace prefix.
///
/// # Return value
/// Namespace URI corresponding to the given prefix, if it is present.
pub fn get<'a, P: ?Sized + AsRef<str>>(&'a self, prefix: &P) -> Option<&'a str> {
self.0.get(prefix.as_ref()).map(|s| &**s)
}
/// Borrowed namespace for the writer
#[must_use]
pub const fn borrow(&self) -> Cow<'_, Self> {
Cow::Borrowed(self)
}
/// Namespace mappings contained in a namespace.
pub fn iter(&self) -> NamespaceMappings<'_> {
self.into_iter()
}
}
/// An alias for iterator type for namespace mappings contained in a namespace.
pub type NamespaceMappings<'a> = Map<
Entries<'a, String, String>,
for<'b> fn((&'b String, &'b String)) -> UriMapping<'b>
>;
impl<'a> IntoIterator for &'a Namespace {
type IntoIter = NamespaceMappings<'a>;
type Item = UriMapping<'a>;
fn into_iter(self) -> Self::IntoIter {
fn mapper<'a>((prefix, uri): (&'a String, &'a String)) -> UriMapping<'a> {
(prefix, uri)
}
self.0.iter().map(mapper)
}
}
/// Namespace stack is a sequence of namespaces.
///
/// Namespace stack is used to represent cumulative namespace consisting of
/// combined namespaces from nested elements.
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct NamespaceStack(pub Vec<Namespace>);
impl NamespaceStack {
/// Returns an empty namespace stack.
#[inline]
#[must_use]
pub fn empty() -> Self {
Self(Vec::with_capacity(2))
}
/// Returns a namespace stack with default items in it.
///
/// Default items are the following:
///
/// * `xml` → `http://www.w3.org/XML/1998/namespace`;
/// * `xmlns` → `http://www.w3.org/2000/xmlns/`.
#[inline]
#[must_use]
#[allow(clippy::should_implement_trait)]
pub fn default() -> Self {
let mut nst = Self::empty();
nst.push_empty();
// xml namespace
nst.put(NS_XML_PREFIX, NS_XML_URI);
// xmlns namespace
nst.put(NS_XMLNS_PREFIX, NS_XMLNS_URI);
// empty namespace
nst.put(NS_NO_PREFIX, NS_EMPTY_URI);
nst
}
/// Adds an empty namespace to the top of this stack.
#[inline]
pub fn push_empty(&mut self) -> &mut Self {
self.0.push(Namespace::empty());
self
}
/// Removes the topmost namespace in this stack.
///
/// Panics if the stack is empty.
#[inline]
#[track_caller]
pub fn pop(&mut self) -> Namespace {
self.0.pop().unwrap()
}
/// Removes the topmost namespace in this stack.
///
/// Returns `Some(namespace)` if this stack is not empty and `None` otherwise.
#[inline]
pub fn try_pop(&mut self) -> Option<Namespace> {
self.0.pop()
}
/// Borrows the topmost namespace mutably, leaving the stack intact.
///
/// Panics if the stack is empty.
#[inline]
#[track_caller]
pub fn peek_mut(&mut self) -> &mut Namespace {
self.0.last_mut().unwrap()
}
/// Borrows the topmost namespace immutably, leaving the stack intact.
///
/// Panics if the stack is empty.
#[inline]
#[must_use]
#[track_caller]
pub fn peek(&self) -> &Namespace {
self.0.last().unwrap()
}
/// Puts a mapping into the topmost namespace if this stack does not already contain one.
///
/// Returns a boolean flag indicating whether the insertion has completed successfully.
/// Note that both key and value are matched and the mapping is inserted if either
/// namespace prefix is not already mapped, or if it is mapped, but to a different URI.
///
/// # Parameters
/// * `prefix` --- namespace prefix;
/// * `uri` --- namespace URI.
///
/// # Return value
/// `true` if `prefix` has been inserted successfully; `false` if the `prefix`
/// was already present in the namespace stack.
pub fn put_checked<P, U>(&mut self, prefix: P, uri: U) -> bool
where P: Into<String> + AsRef<str>,
U: Into<String> + AsRef<str>
{
if self.0.iter().any(|ns| ns.get(&prefix) == Some(uri.as_ref())) {
false
} else {
self.put(prefix, uri);
true
}
}
/// Puts a mapping into the topmost namespace in this stack.
///
/// This method does not override a mapping in the topmost namespace if it is
/// already present, however, it does not depend on other namespaces in the stack,
/// so it is possible to put a mapping which is present in lower namespaces.
///
/// Returns a boolean flag indicating whether the insertion has completed successfully.
///
/// # Parameters
/// * `prefix` --- namespace prefix;
/// * `uri` --- namespace URI.
///
/// # Return value
/// `true` if `prefix` has been inserted successfully; `false` if the `prefix`
/// was already present in the namespace.
#[inline]
pub fn put<P, U>(&mut self, prefix: P, uri: U) -> bool
where P: Into<String>, U: Into<String>
{
if let Some(ns) = self.0.last_mut() {
ns.put(prefix, uri)
} else {
false
}
}
/// Performs a search for the given prefix in the whole stack.
///
/// This method walks the stack from top to bottom, querying each namespace
/// in order for the given prefix. If none of the namespaces contains the prefix,
/// `None` is returned.
///
/// # Parameters
/// * `prefix` --- namespace prefix.
#[inline]
pub fn get<'a, P: ?Sized + AsRef<str>>(&'a self, prefix: &P) -> Option<&'a str> {
let prefix = prefix.as_ref();
for ns in self.0.iter().rev() {
match ns.get(prefix) {
None => {},
r => return r,
}
}
None
}
/// Combines this stack of namespaces into a single namespace.
///
/// Namespaces are combined in left-to-right order, that is, rightmost namespace
/// elements take priority over leftmost ones.
#[must_use]
pub fn squash(&self) -> Namespace {
let mut result = BTreeMap::new();
for ns in &self.0 {
result.extend(ns.0.iter().map(|(k, v)| (k.clone(), v.clone())));
}
Namespace(result)
}
/// Returns an object which implements `Extend` using `put_checked()` instead of `put()`.
///
/// See `CheckedTarget` for more information.
#[inline]
pub fn checked_target(&mut self) -> CheckedTarget<'_> {
CheckedTarget(self)
}
/// Returns an iterator over all mappings in this namespace stack.
#[inline]
#[must_use]
pub fn iter(&self) -> NamespaceStackMappings<'_> {
self.into_iter()
}
}
/// An iterator over mappings from prefixes to URIs in a namespace stack.
///
/// # Example
/// ```
/// # use xml::namespace::NamespaceStack;
/// let mut nst = NamespaceStack::empty();
/// nst.push_empty();
/// nst.put("a", "urn:A");
/// nst.put("b", "urn:B");
/// nst.push_empty();
/// nst.put("c", "urn:C");
///
/// assert_eq!(vec![("c", "urn:C"), ("a", "urn:A"), ("b", "urn:B")], nst.iter().collect::<Vec<_>>());
/// ```
pub struct NamespaceStackMappings<'a> {
namespaces: Rev<Iter<'a, Namespace>>,
current_namespace: Option<NamespaceMappings<'a>>,
used_keys: HashSet<&'a str>,
}
impl NamespaceStackMappings<'_> {
fn go_to_next_namespace(&mut self) -> bool {
self.current_namespace = self.namespaces.next().map(|ns| ns.into_iter());
self.current_namespace.is_some()
}
}
impl<'a> Iterator for NamespaceStackMappings<'a> {
type Item = UriMapping<'a>;
fn next(&mut self) -> Option<UriMapping<'a>> {
// If there is no current namespace and no next namespace, we're finished
if self.current_namespace.is_none() && !self.go_to_next_namespace() {
return None;
}
let next_item = self.current_namespace.as_mut()?.next();
match next_item {
// There is an element in the current namespace
Some((k, v)) => if self.used_keys.contains(&k) {
// If the current key is used, go to the next one
self.next()
} else {
// Otherwise insert the current key to the set of used keys and
// return the mapping
self.used_keys.insert(k);
Some((k, v))
},
// Current namespace is exhausted
None => if self.go_to_next_namespace() {
// If there is next namespace, continue from it
self.next()
} else {
// No next namespace, exiting
None
}
}
}
}
impl<'a> IntoIterator for &'a NamespaceStack {
type IntoIter = NamespaceStackMappings<'a>;
type Item = UriMapping<'a>;
fn into_iter(self) -> Self::IntoIter {
NamespaceStackMappings {
namespaces: self.0.iter().rev(),
current_namespace: None,
used_keys: HashSet::new(),
}
}
}
/// A type alias for a pair of `(prefix, uri)` values returned by namespace iterators.
pub type UriMapping<'a> = (&'a str, &'a str);
impl<'a> Extend<UriMapping<'a>> for Namespace {
fn extend<T>(&mut self, iterable: T) where T: IntoIterator<Item=UriMapping<'a>> {
for (prefix, uri) in iterable {
self.put(prefix, uri);
}
}
}
impl<'a> Extend<UriMapping<'a>> for NamespaceStack {
fn extend<T>(&mut self, iterable: T) where T: IntoIterator<Item=UriMapping<'a>> {
for (prefix, uri) in iterable {
self.put(prefix, uri);
}
}
}
/// A wrapper around `NamespaceStack` which implements `Extend` using `put_checked()`.
///
/// # Example
///
/// ```
/// # use xml::namespace::NamespaceStack;
///
/// let mut nst = NamespaceStack::empty();
/// nst.push_empty();
/// nst.put("a", "urn:A");
/// nst.put("b", "urn:B");
/// nst.push_empty();
/// nst.put("c", "urn:C");
///
/// nst.checked_target().extend(vec![("a", "urn:Z"), ("b", "urn:B"), ("c", "urn:Y"), ("d", "urn:D")]);
/// assert_eq!(
/// vec![("a", "urn:Z"), ("c", "urn:C"), ("d", "urn:D"), ("b", "urn:B")],
/// nst.iter().collect::<Vec<_>>()
/// );
/// ```
///
/// Compare:
///
/// ```
/// # use xml::namespace::NamespaceStack;
/// # let mut nst = NamespaceStack::empty();
/// # nst.push_empty();
/// # nst.put("a", "urn:A");
/// # nst.put("b", "urn:B");
/// # nst.push_empty();
/// # nst.put("c", "urn:C");
///
/// nst.extend(vec![("a", "urn:Z"), ("b", "urn:B"), ("c", "urn:Y"), ("d", "urn:D")]);
/// assert_eq!(
/// vec![("a", "urn:Z"), ("b", "urn:B"), ("c", "urn:C"), ("d", "urn:D")],
/// nst.iter().collect::<Vec<_>>()
/// );
/// ```
pub struct CheckedTarget<'a>(&'a mut NamespaceStack);
impl<'b> Extend<UriMapping<'b>> for CheckedTarget<'_> {
fn extend<T>(&mut self, iterable: T) where T: IntoIterator<Item=UriMapping<'b>> {
for (prefix, uri) in iterable {
self.0.put_checked(prefix, uri);
}
}
}