Skip to main content

NamespaceResolver

Struct NamespaceResolver 

Source
pub struct NamespaceResolver { /* private fields */ }
Expand description

Storage for the currently defined namespace bindings, used to resolve prefixes into namespaces. Holds all the internal logic to push and pop namespaces with their levels.

Implementations§

Source§

impl NamespaceResolver

Source

pub fn add( &mut self, prefix: PrefixDeclaration<'_>, namespace: Namespace<'_>, ) -> Result<(), NamespaceError>

Adds a new binding of prefix to namespace at the current nesting level.

The binding is removed when the level is popped out. May fail if you try to (re-)declare the reserved prefixes xml and xmlns.

let mut resolver = NamespaceResolver::default();
assert_eq!(resolver.resolve_element(QName(b"name")).0, ResolveResult::Unbound);
assert_eq!(
    resolver.resolve_element(QName(b"ns:name")).0,
    ResolveResult::Unknown(b"ns".to_vec()),
);

resolver.add(PrefixDeclaration::Default, Namespace(b"example.com")).expect("doc example");
resolver.add(PrefixDeclaration::Named(b"ns"), Namespace(b"my:namespace")).expect("doc example");

assert_eq!(
    resolver.resolve_element(QName(b"name")).0,
    ResolveResult::Bound(Namespace(b"example.com")),
);
assert_eq!(
    resolver.resolve_element(QName(b"ns:name")).0,
    ResolveResult::Bound(Namespace(b"my:namespace")),
);
Source

pub fn push(&mut self, start: &BytesStart<'_>) -> Result<(), NamespaceError>

Begins a new scope and adds to it all namespace bindings found in the specified start element.

The namespace name a declaration binds is its attribute’s normalized value: Namespaces in XML §3 defines it as “the attribute value in the namespace declaration”, which XML §3.3.3 has already normalized by the time the value reaches an application. So a declaration written with a literal tab in it binds a name with a space where the tab was, while one written urn:a&#9;b binds a name that still holds the tab — a character reference stands for the character it names, and is not white space the algorithm folds. Either way the binding is the very string Attribute::normalized_value reports for the same attribute: one document cannot have two answers.

Source

pub const fn max_declarations_per_element(&self) -> usize

Returns the maximum number of xmlns / xmlns:* declarations that push will accept on a single start tag.

Source

pub fn set_max_declarations_per_element(&mut self, limit: usize) -> &mut Self

Sets the maximum number of xmlns / xmlns:* declarations that push will accept on a single start tag. Pass usize::MAX to disable the limit.

Source

pub fn pop(&mut self)

Ends the top-most scope, popping all bindings added by the last call to push or add.

Source

pub fn set_level(&mut self, level: usize)

Sets the number of push calls not yet followed by a pop. Setting a value lower than the current level behaves as if pop were called until the level matches.

let mut resolver = NamespaceResolver::default();
assert_eq!(resolver.level(), 0);
resolver.push(&BytesStart::new("tag")).expect("doc example");
assert_eq!(resolver.level(), 1);
resolver.set_level(10);
assert_eq!(resolver.level(), 10);
resolver.pop();
assert_eq!(resolver.level(), 9);
Source

pub fn resolve<'n>( &self, name: QName<'n>, use_default: bool, ) -> (ResolveResult<'_>, LocalName<'n>)

Resolves a potentially qualified element or attribute name into (namespace name, local name).

§Parameters
  • name: the possibly qualified name to resolve;
  • use_default: whether to translate an absent prefix to the current default namespace. Should be true for element names, false for attribute names.
Source

pub fn resolve_element<'n>( &self, name: QName<'n>, ) -> (ResolveResult<'_>, LocalName<'n>)

Convenience for resolve(name, true), expressing that an element name is being resolved.

Source

pub fn resolve_attribute<'n>( &self, name: QName<'n>, ) -> (ResolveResult<'_>, LocalName<'n>)

Convenience for resolve(name, false), expressing that an attribute name is being resolved.

Source

pub fn resolve_event<'i>( &self, event: Event<'i>, ) -> (ResolveResult<'_>, Event<'i>)

Finds a namespace name for a given event, if applicable.

A namespace is resolved only for Start, Empty and End events; for all other events ResolveResult::Unbound is returned.

Source

pub fn resolve_prefix( &self, prefix: Option<Prefix<'_>>, use_default: bool, ) -> ResolveResult<'_>

Resolves the given optional prefix (usually obtained from QName::prefix) into a corresponding namespace.

Source

pub const fn bindings(&self) -> NamespaceBindingsIter<'_> ⓘ

Returns all the bindings currently in effect except the default xml and xmlns bindings.

let mut resolver = NamespaceResolver::default();
assert_eq!(resolver.bindings().collect::<Vec<_>>(), vec![]);

resolver.push(&BytesStart::from_content("a xmlns=\"a1\" xmlns:a=\"a2\"", 1)).expect("doc example");
assert_eq!(resolver.bindings().collect::<Vec<_>>(), vec![
    (PrefixDeclaration::Default, Namespace(b"a1")),
    (PrefixDeclaration::Named(b"a"), Namespace(b"a2")),
]);
Source

pub const fn bindings_of( &self, level: usize, ) -> NamespaceBindingsOfLevelIter<'_> ⓘ

Returns all the bindings on the specified level, including the default xml and xmlns bindings (which live at level 0).

Source

pub const fn level(&self) -> usize

Returns the number of push calls not yet followed by a pop.

Trait Implementations§

Source§

impl Clone for NamespaceResolver

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NamespaceResolver

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for NamespaceResolver

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.