2017-03-08 19:34:17 +00:00
|
|
|
//! Provides an `Element` type, which represents DOM nodes, and a builder to create them with.
|
|
|
|
|
2017-06-07 20:40:53 +00:00
|
|
|
use std::io:: Write;
|
|
|
|
use std::collections::{btree_map, BTreeMap};
|
2017-03-08 19:34:17 +00:00
|
|
|
|
2017-06-07 20:40:53 +00:00
|
|
|
use std::str;
|
2017-07-28 21:58:03 +00:00
|
|
|
use std::rc::Rc;
|
2017-08-12 00:13:32 +00:00
|
|
|
use std::borrow::Cow;
|
2017-03-08 19:34:17 +00:00
|
|
|
|
2017-05-22 17:20:01 +00:00
|
|
|
use error::{Error, ErrorKind, Result};
|
2017-03-08 19:34:17 +00:00
|
|
|
|
2017-06-07 20:40:53 +00:00
|
|
|
use quick_xml::reader::Reader as EventReader;
|
|
|
|
use quick_xml::events::{Event, BytesStart};
|
|
|
|
|
|
|
|
use std::io::BufRead;
|
2017-03-08 19:34:17 +00:00
|
|
|
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
use std::slice;
|
|
|
|
|
|
|
|
use convert::{IntoElements, IntoAttributeValue, ElementEmitter};
|
2017-08-13 15:29:38 +00:00
|
|
|
use namespace_set::NamespaceSet;
|
2017-03-08 19:34:17 +00:00
|
|
|
|
2017-08-12 00:05:18 +00:00
|
|
|
/// Escape XML text
|
|
|
|
pub fn write_escaped<W: Write>(writer: &mut W, input: &str) -> Result<()> {
|
|
|
|
for c in input.chars() {
|
|
|
|
match c {
|
|
|
|
'&' => write!(writer, "&")?,
|
|
|
|
'<' => write!(writer, "<")?,
|
|
|
|
'>' => write!(writer, ">")?,
|
|
|
|
'\'' => write!(writer, "'")?,
|
|
|
|
'"' => write!(writer, """)?,
|
|
|
|
_ => write!(writer, "{}", c)?,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-05-14 14:38:56 +00:00
|
|
|
/// A node in an element tree.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum Node {
|
|
|
|
/// An `Element`.
|
|
|
|
Element(Element),
|
|
|
|
/// A text node.
|
|
|
|
Text(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Node {
|
|
|
|
/// Turns this into an `Element` if possible, else returns None.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use minidom::Node;
|
|
|
|
///
|
|
|
|
/// let elm = Node::Element("<meow />".parse().unwrap());
|
|
|
|
/// let txt = Node::Text("meow".to_owned());
|
|
|
|
///
|
|
|
|
/// assert_eq!(elm.as_element().unwrap().name(), "meow");
|
|
|
|
/// assert_eq!(txt.as_element(), None);
|
|
|
|
/// ```
|
2017-05-22 16:45:43 +00:00
|
|
|
pub fn as_element(&self) -> Option<&Element> {
|
2017-05-14 14:38:56 +00:00
|
|
|
match *self {
|
|
|
|
Node::Element(ref e) => Some(e),
|
|
|
|
Node::Text(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Turns this into a `String` if possible, else returns None.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use minidom::Node;
|
|
|
|
///
|
|
|
|
/// let elm = Node::Element("<meow />".parse().unwrap());
|
|
|
|
/// let txt = Node::Text("meow".to_owned());
|
|
|
|
///
|
|
|
|
/// assert_eq!(elm.as_text(), None);
|
|
|
|
/// assert_eq!(txt.as_text().unwrap(), "meow");
|
|
|
|
/// ```
|
2017-05-22 16:45:43 +00:00
|
|
|
pub fn as_text(&self) -> Option<&str> {
|
2017-05-14 14:38:56 +00:00
|
|
|
match *self {
|
|
|
|
Node::Element(_) => None,
|
|
|
|
Node::Text(ref s) => Some(s),
|
|
|
|
}
|
|
|
|
}
|
2017-06-07 20:40:53 +00:00
|
|
|
|
2017-07-28 21:58:03 +00:00
|
|
|
fn write_to_inner<W: Write>(&self, writer: &mut W) -> Result<()>{
|
2017-06-07 20:40:53 +00:00
|
|
|
match *self {
|
2017-07-28 21:58:03 +00:00
|
|
|
Node::Element(ref elmt) => elmt.write_to_inner(writer)?,
|
2017-08-12 00:05:18 +00:00
|
|
|
Node::Text(ref s) => write_escaped(writer, s)?,
|
2017-06-07 20:40:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-05-14 14:38:56 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 20:40:53 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2017-03-08 19:34:17 +00:00
|
|
|
/// A struct representing a DOM Element.
|
|
|
|
pub struct Element {
|
2017-07-28 21:58:03 +00:00
|
|
|
prefix: Option<String>,
|
2017-03-08 19:34:17 +00:00
|
|
|
name: String,
|
2017-07-28 21:58:03 +00:00
|
|
|
namespaces: Rc<NamespaceSet>,
|
2017-04-26 21:44:58 +00:00
|
|
|
attributes: BTreeMap<String, String>,
|
2017-03-08 19:34:17 +00:00
|
|
|
children: Vec<Node>,
|
|
|
|
}
|
|
|
|
|
2017-05-02 15:30:31 +00:00
|
|
|
impl<'a> From<&'a Element> for String {
|
|
|
|
fn from(elem: &'a Element) -> String {
|
2017-06-07 20:40:53 +00:00
|
|
|
let mut writer = Vec::new();
|
|
|
|
elem.write_to(&mut writer).unwrap();
|
|
|
|
String::from_utf8(writer).unwrap()
|
2017-05-02 15:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:34:17 +00:00
|
|
|
|
|
|
|
impl FromStr for Element {
|
|
|
|
type Err = Error;
|
|
|
|
|
2017-05-22 17:20:01 +00:00
|
|
|
fn from_str(s: &str) -> Result<Element> {
|
2017-06-07 20:40:53 +00:00
|
|
|
let mut reader = EventReader::from_str(s);
|
2017-03-08 19:34:17 +00:00
|
|
|
Element::from_reader(&mut reader)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Element {
|
2017-07-28 21:58:03 +00:00
|
|
|
fn new<NS: Into<NamespaceSet>>(name: String, prefix: Option<String>, namespaces: NS, attributes: BTreeMap<String, String>, children: Vec<Node>) -> Element {
|
2017-03-08 19:34:17 +00:00
|
|
|
Element {
|
2017-07-28 21:58:03 +00:00
|
|
|
prefix, name,
|
|
|
|
namespaces: Rc::new(namespaces.into()),
|
2017-03-08 19:34:17 +00:00
|
|
|
attributes: attributes,
|
|
|
|
children: children,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a builder for an `Element` with the given `name`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-05-14 14:38:56 +00:00
|
|
|
/// ```rust
|
2017-03-08 19:34:17 +00:00
|
|
|
/// use minidom::Element;
|
|
|
|
///
|
|
|
|
/// let elem = Element::builder("name")
|
|
|
|
/// .ns("namespace")
|
|
|
|
/// .attr("name", "value")
|
|
|
|
/// .append("inner")
|
|
|
|
/// .build();
|
|
|
|
///
|
|
|
|
/// assert_eq!(elem.name(), "name");
|
2017-07-28 21:58:03 +00:00
|
|
|
/// assert_eq!(elem.ns(), Some("namespace".to_owned()));
|
2017-03-08 19:34:17 +00:00
|
|
|
/// assert_eq!(elem.attr("name"), Some("value"));
|
|
|
|
/// assert_eq!(elem.attr("inexistent"), None);
|
|
|
|
/// assert_eq!(elem.text(), "inner");
|
|
|
|
/// ```
|
2017-07-28 21:58:03 +00:00
|
|
|
pub fn builder<S: AsRef<str>>(name: S) -> ElementBuilder {
|
|
|
|
let (prefix, name) = split_element_name(name).unwrap();
|
2017-03-08 19:34:17 +00:00
|
|
|
ElementBuilder {
|
2017-07-28 21:58:03 +00:00
|
|
|
root: Element::new(name, prefix, None, BTreeMap::new(), Vec::new()),
|
|
|
|
namespaces: Default::default(),
|
2017-03-08 19:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a bare minimum `Element` with this name.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-05-14 14:38:56 +00:00
|
|
|
/// ```rust
|
2017-03-08 19:34:17 +00:00
|
|
|
/// use minidom::Element;
|
|
|
|
///
|
|
|
|
/// let bare = Element::bare("name");
|
|
|
|
///
|
|
|
|
/// assert_eq!(bare.name(), "name");
|
|
|
|
/// assert_eq!(bare.ns(), None);
|
|
|
|
/// assert_eq!(bare.attr("name"), None);
|
|
|
|
/// assert_eq!(bare.text(), "");
|
|
|
|
/// ```
|
|
|
|
pub fn bare<S: Into<String>>(name: S) -> Element {
|
|
|
|
Element {
|
2017-07-28 21:58:03 +00:00
|
|
|
prefix: None,
|
2017-03-08 19:34:17 +00:00
|
|
|
name: name.into(),
|
2017-07-28 21:58:03 +00:00
|
|
|
namespaces: Rc::new(NamespaceSet::default()),
|
2017-04-26 21:44:58 +00:00
|
|
|
attributes: BTreeMap::new(),
|
2017-03-08 19:34:17 +00:00
|
|
|
children: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reference to the name of this element.
|
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reference to the namespace of this element, if it has one, else `None`.
|
2017-07-28 21:58:03 +00:00
|
|
|
pub fn ns(&self) -> Option<String> {
|
|
|
|
self.namespaces.get(&self.prefix)
|
2017-03-08 19:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reference to the value of the given attribute, if it exists, else `None`.
|
|
|
|
pub fn attr(&self, name: &str) -> Option<&str> {
|
2017-04-26 21:44:58 +00:00
|
|
|
if let Some(value) = self.attributes.get(name) {
|
2017-05-22 16:45:43 +00:00
|
|
|
return Some(value)
|
2017-03-08 19:34:17 +00:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2017-05-14 14:38:56 +00:00
|
|
|
/// Returns an iterator over the attributes of this element.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use minidom::Element;
|
|
|
|
///
|
|
|
|
/// let elm: Element = "<elem a=\"b\" />".parse().unwrap();
|
|
|
|
///
|
|
|
|
/// let mut iter = elm.attrs();
|
|
|
|
///
|
|
|
|
/// assert_eq!(iter.next().unwrap(), ("a", "b"));
|
|
|
|
/// assert_eq!(iter.next(), None);
|
|
|
|
/// ```
|
2017-05-22 16:45:43 +00:00
|
|
|
pub fn attrs(&self) -> Attrs {
|
2017-05-14 14:38:56 +00:00
|
|
|
Attrs {
|
|
|
|
iter: self.attributes.iter(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over the attributes of this element, with the value being a mutable
|
|
|
|
/// reference.
|
2017-05-22 16:45:43 +00:00
|
|
|
pub fn attrs_mut(&mut self) -> AttrsMut {
|
2017-05-14 14:38:56 +00:00
|
|
|
AttrsMut {
|
|
|
|
iter: self.attributes.iter_mut(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:34:17 +00:00
|
|
|
/// Modifies the value of an attribute.
|
|
|
|
pub fn set_attr<S: Into<String>, V: IntoAttributeValue>(&mut self, name: S, val: V) {
|
|
|
|
let name = name.into();
|
|
|
|
let val = val.into_attribute_value();
|
2017-04-26 21:44:58 +00:00
|
|
|
|
|
|
|
if let Some(value) = self.attributes.get_mut(&name) {
|
|
|
|
*value = val.expect("removing existing value via set_attr, this is not yet supported (TODO)"); // TODO
|
|
|
|
return;
|
2017-03-08 19:34:17 +00:00
|
|
|
}
|
2017-04-26 21:44:58 +00:00
|
|
|
|
2017-03-08 19:34:17 +00:00
|
|
|
if let Some(val) = val {
|
2017-04-26 21:44:58 +00:00
|
|
|
self.attributes.insert(name, val);
|
2017-03-08 19:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether the element has the given name and namespace.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-05-14 14:38:56 +00:00
|
|
|
/// ```rust
|
2017-03-08 19:34:17 +00:00
|
|
|
/// use minidom::Element;
|
|
|
|
///
|
|
|
|
/// let elem = Element::builder("name").ns("namespace").build();
|
|
|
|
///
|
|
|
|
/// assert_eq!(elem.is("name", "namespace"), true);
|
|
|
|
/// assert_eq!(elem.is("name", "wrong"), false);
|
|
|
|
/// assert_eq!(elem.is("wrong", "namespace"), false);
|
|
|
|
/// assert_eq!(elem.is("wrong", "wrong"), false);
|
|
|
|
/// ```
|
|
|
|
pub fn is<N: AsRef<str>, NS: AsRef<str>>(&self, name: N, namespace: NS) -> bool {
|
2017-07-28 21:58:03 +00:00
|
|
|
self.name == name.as_ref() &&
|
|
|
|
self.namespaces.has(&self.prefix, namespace)
|
2017-03-08 19:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a document from an `EventReader`.
|
2017-06-07 20:40:53 +00:00
|
|
|
pub fn from_reader<R: BufRead>(reader: &mut EventReader<R>) -> Result<Element> {
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
|
2017-08-18 23:34:44 +00:00
|
|
|
let root: Element = loop {
|
2017-06-07 20:40:53 +00:00
|
|
|
let e = reader.read_event(&mut buf)?;
|
2017-03-08 19:34:17 +00:00
|
|
|
match e {
|
2017-06-07 20:40:53 +00:00
|
|
|
Event::Empty(ref e) | Event::Start(ref e) => {
|
2017-08-18 23:34:44 +00:00
|
|
|
break build_element(e)?;
|
2017-03-08 19:34:17 +00:00
|
|
|
},
|
2017-06-07 20:40:53 +00:00
|
|
|
Event::Eof => {
|
2017-05-22 17:20:01 +00:00
|
|
|
bail!(ErrorKind::EndOfDocument);
|
2017-03-08 19:34:17 +00:00
|
|
|
},
|
|
|
|
_ => () // TODO: may need more errors
|
|
|
|
}
|
2017-06-07 20:40:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut stack = vec![root];
|
2017-03-08 19:34:17 +00:00
|
|
|
|
|
|
|
loop {
|
2017-06-07 20:40:53 +00:00
|
|
|
match reader.read_event(&mut buf)? {
|
|
|
|
Event::Empty(ref e) => {
|
|
|
|
let elem = build_element(e)?;
|
|
|
|
// Since there is no Event::End after, directly append it to the current node
|
|
|
|
stack.last_mut().unwrap().append_child(elem);
|
2017-03-08 19:34:17 +00:00
|
|
|
},
|
2017-06-07 20:40:53 +00:00
|
|
|
Event::Start(ref e) => {
|
|
|
|
let elem = build_element(e)?;
|
|
|
|
stack.push(elem);
|
2017-03-08 19:34:17 +00:00
|
|
|
},
|
2017-06-07 20:40:53 +00:00
|
|
|
Event::End(ref e) => {
|
|
|
|
if stack.len() <= 1 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let elem = stack.pop().unwrap();
|
|
|
|
if let Some(to) = stack.last_mut() {
|
|
|
|
if elem.name().as_bytes() != e.name() {
|
|
|
|
bail!(ErrorKind::InvalidElementClosed);
|
|
|
|
}
|
|
|
|
to.append_child(elem);
|
|
|
|
}
|
2017-03-08 19:34:17 +00:00
|
|
|
},
|
2017-06-07 20:40:53 +00:00
|
|
|
Event::Text(s) | Event::CData(s) => {
|
|
|
|
let text = s.unescape_and_decode(reader)?;
|
|
|
|
if text != "" {
|
|
|
|
let mut current_elem = stack.last_mut().unwrap();
|
|
|
|
current_elem.append_text_node(text);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Event::Eof => {
|
|
|
|
break;
|
2017-03-08 19:34:17 +00:00
|
|
|
},
|
|
|
|
_ => (), // TODO: may need to implement more
|
|
|
|
}
|
|
|
|
}
|
2017-06-07 20:40:53 +00:00
|
|
|
Ok(stack.pop().unwrap())
|
2017-03-08 19:34:17 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 20:40:53 +00:00
|
|
|
/// Output a document to a `Writer`.
|
|
|
|
pub fn write_to<W: Write>(&self, writer: &mut W) -> Result<()> {
|
|
|
|
write!(writer, "<?xml version=\"1.0\" encoding=\"utf-8\"?>")?;
|
2017-07-28 21:58:03 +00:00
|
|
|
self.write_to_inner(writer)
|
2017-06-07 20:40:53 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 21:58:03 +00:00
|
|
|
/// Like `write_to()` but without the `<?xml?>` prelude
|
|
|
|
pub fn write_to_inner<W: Write>(&self, writer: &mut W) -> Result<()> {
|
|
|
|
let name = match &self.prefix {
|
2017-08-12 00:13:32 +00:00
|
|
|
&None => Cow::Borrowed(&self.name),
|
|
|
|
&Some(ref prefix) => Cow::Owned(format!("{}:{}", prefix, self.name)),
|
2017-07-28 21:58:03 +00:00
|
|
|
};
|
|
|
|
write!(writer, "<{}", name)?;
|
|
|
|
|
2017-08-13 00:35:24 +00:00
|
|
|
for (prefix, ns) in self.namespaces.declared_ns() {
|
2017-07-28 21:58:03 +00:00
|
|
|
match prefix {
|
|
|
|
&None => {
|
|
|
|
write!(writer, " xmlns=\"")?;
|
|
|
|
write_escaped(writer, ns)?;
|
|
|
|
write!(writer, "\"")?;
|
|
|
|
},
|
|
|
|
&Some(ref prefix) => {
|
|
|
|
write!(writer, " xmlns:{}=\"", prefix)?;
|
|
|
|
write_escaped(writer, ns)?;
|
|
|
|
write!(writer, "\"")?;
|
|
|
|
},
|
2017-06-07 20:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (key, value) in &self.attributes {
|
2017-08-12 00:05:18 +00:00
|
|
|
write!(writer, " {}=\"", key)?;
|
|
|
|
write_escaped(writer, value)?;
|
|
|
|
write!(writer, "\"")?;
|
2017-03-08 19:34:17 +00:00
|
|
|
}
|
2017-06-07 20:40:53 +00:00
|
|
|
|
|
|
|
if self.children.is_empty() {
|
|
|
|
write!(writer, " />")?;
|
|
|
|
return Ok(())
|
2017-03-08 19:34:17 +00:00
|
|
|
}
|
2017-06-07 20:40:53 +00:00
|
|
|
|
|
|
|
write!(writer, ">")?;
|
|
|
|
|
2017-03-08 19:34:17 +00:00
|
|
|
for child in &self.children {
|
2017-07-28 21:58:03 +00:00
|
|
|
child.write_to_inner(writer)?;
|
2017-03-08 19:34:17 +00:00
|
|
|
}
|
2017-06-07 20:40:53 +00:00
|
|
|
|
2017-07-28 21:58:03 +00:00
|
|
|
write!(writer, "</{}>", name)?;
|
2017-03-08 19:34:17 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-05-14 14:38:56 +00:00
|
|
|
/// Returns an iterator over references to every child node of this element.
|
2017-03-08 19:34:17 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-05-14 14:38:56 +00:00
|
|
|
/// ```rust
|
2017-06-07 20:40:53 +00:00
|
|
|
/// use minidom::Element;
|
2017-05-14 14:38:56 +00:00
|
|
|
///
|
|
|
|
/// let elem: Element = "<root>a<c1 />b<c2 />c</root>".parse().unwrap();
|
|
|
|
///
|
|
|
|
/// let mut iter = elem.nodes();
|
|
|
|
///
|
|
|
|
/// assert_eq!(iter.next().unwrap().as_text().unwrap(), "a");
|
|
|
|
/// assert_eq!(iter.next().unwrap().as_element().unwrap().name(), "c1");
|
|
|
|
/// assert_eq!(iter.next().unwrap().as_text().unwrap(), "b");
|
|
|
|
/// assert_eq!(iter.next().unwrap().as_element().unwrap().name(), "c2");
|
|
|
|
/// assert_eq!(iter.next().unwrap().as_text().unwrap(), "c");
|
|
|
|
/// assert_eq!(iter.next(), None);
|
2017-03-08 19:34:17 +00:00
|
|
|
/// ```
|
2017-05-22 16:45:43 +00:00
|
|
|
#[inline] pub fn nodes(&self) -> Nodes {
|
2017-05-14 14:38:56 +00:00
|
|
|
self.children.iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over mutable references to every child node of this element.
|
2017-05-22 16:45:43 +00:00
|
|
|
#[inline] pub fn nodes_mut(&mut self) -> NodesMut {
|
2017-05-14 14:38:56 +00:00
|
|
|
self.children.iter_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over references to every child element of this element.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
2017-03-08 19:34:17 +00:00
|
|
|
/// use minidom::Element;
|
|
|
|
///
|
2017-05-14 14:38:56 +00:00
|
|
|
/// let elem: Element = "<root>hello<child1 />this<child2 />is<child3 />ignored</root>".parse().unwrap();
|
2017-03-08 19:34:17 +00:00
|
|
|
///
|
|
|
|
/// let mut iter = elem.children();
|
|
|
|
/// assert_eq!(iter.next().unwrap().name(), "child1");
|
|
|
|
/// assert_eq!(iter.next().unwrap().name(), "child2");
|
|
|
|
/// assert_eq!(iter.next().unwrap().name(), "child3");
|
|
|
|
/// assert_eq!(iter.next(), None);
|
|
|
|
/// ```
|
2017-05-22 16:45:43 +00:00
|
|
|
#[inline] pub fn children(&self) -> Children {
|
2017-03-08 19:34:17 +00:00
|
|
|
Children {
|
|
|
|
iter: self.children.iter(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-14 14:38:56 +00:00
|
|
|
/// Returns an iterator over mutable references to every child element of this element.
|
2017-05-22 16:45:43 +00:00
|
|
|
#[inline] pub fn children_mut(&mut self) -> ChildrenMut {
|
2017-03-08 19:34:17 +00:00
|
|
|
ChildrenMut {
|
|
|
|
iter: self.children.iter_mut(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-14 14:38:56 +00:00
|
|
|
/// Returns an iterator over references to every text node of this element.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use minidom::Element;
|
|
|
|
///
|
|
|
|
/// let elem: Element = "<root>hello<c /> world!</root>".parse().unwrap();
|
|
|
|
///
|
|
|
|
/// let mut iter = elem.texts();
|
|
|
|
/// assert_eq!(iter.next().unwrap(), "hello");
|
|
|
|
/// assert_eq!(iter.next().unwrap(), " world!");
|
|
|
|
/// assert_eq!(iter.next(), None);
|
|
|
|
/// ```
|
2017-05-22 16:45:43 +00:00
|
|
|
#[inline] pub fn texts(&self) -> Texts {
|
2017-05-14 14:38:56 +00:00
|
|
|
Texts {
|
|
|
|
iter: self.children.iter(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over mutable references to every text node of this element.
|
2017-05-22 16:45:43 +00:00
|
|
|
#[inline] pub fn texts_mut(&mut self) -> TextsMut {
|
2017-05-14 14:38:56 +00:00
|
|
|
TextsMut {
|
|
|
|
iter: self.children.iter_mut(),
|
2017-03-08 19:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Appends a child node to the `Element`, returning the appended node.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-05-14 14:38:56 +00:00
|
|
|
/// ```rust
|
2017-03-08 19:34:17 +00:00
|
|
|
/// use minidom::Element;
|
|
|
|
///
|
|
|
|
/// let mut elem = Element::bare("root");
|
|
|
|
///
|
|
|
|
/// assert_eq!(elem.children().count(), 0);
|
|
|
|
///
|
|
|
|
/// elem.append_child(Element::bare("child"));
|
|
|
|
///
|
|
|
|
/// {
|
|
|
|
/// let mut iter = elem.children();
|
|
|
|
/// assert_eq!(iter.next().unwrap().name(), "child");
|
|
|
|
/// assert_eq!(iter.next(), None);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let child = elem.append_child(Element::bare("new"));
|
|
|
|
///
|
|
|
|
/// assert_eq!(child.name(), "new");
|
|
|
|
/// ```
|
2017-07-28 21:58:03 +00:00
|
|
|
pub fn append_child(&mut self, child: Element) -> &mut Element {
|
2017-08-13 00:28:44 +00:00
|
|
|
child.namespaces.set_parent(self.namespaces.clone());
|
2017-07-28 21:58:03 +00:00
|
|
|
|
2017-03-08 19:34:17 +00:00
|
|
|
self.children.push(Node::Element(child));
|
|
|
|
if let Node::Element(ref mut cld) = *self.children.last_mut().unwrap() {
|
|
|
|
cld
|
2017-07-28 21:58:03 +00:00
|
|
|
} else {
|
2017-03-08 19:34:17 +00:00
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Appends a text node to an `Element`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-05-14 14:38:56 +00:00
|
|
|
/// ```rust
|
2017-03-08 19:34:17 +00:00
|
|
|
/// use minidom::Element;
|
|
|
|
///
|
|
|
|
/// let mut elem = Element::bare("node");
|
|
|
|
///
|
|
|
|
/// assert_eq!(elem.text(), "");
|
|
|
|
///
|
|
|
|
/// elem.append_text_node("text");
|
|
|
|
///
|
|
|
|
/// assert_eq!(elem.text(), "text");
|
|
|
|
/// ```
|
|
|
|
pub fn append_text_node<S: Into<String>>(&mut self, child: S) {
|
|
|
|
self.children.push(Node::Text(child.into()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Appends a node to an `Element`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-05-14 14:38:56 +00:00
|
|
|
/// ```rust
|
2017-03-08 19:34:17 +00:00
|
|
|
/// use minidom::{Element, Node};
|
|
|
|
///
|
|
|
|
/// let mut elem = Element::bare("node");
|
|
|
|
///
|
|
|
|
/// elem.append_node(Node::Text("hello".to_owned()));
|
|
|
|
///
|
|
|
|
/// assert_eq!(elem.text(), "hello");
|
|
|
|
/// ```
|
|
|
|
pub fn append_node(&mut self, node: Node) {
|
|
|
|
self.children.push(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the concatenation of all text nodes in the `Element`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-05-14 14:38:56 +00:00
|
|
|
/// ```rust
|
2017-03-08 19:34:17 +00:00
|
|
|
/// use minidom::Element;
|
|
|
|
///
|
2017-05-14 14:38:56 +00:00
|
|
|
/// let elem: Element = "<node>hello,<split /> world!</node>".parse().unwrap();
|
2017-03-08 19:34:17 +00:00
|
|
|
///
|
|
|
|
/// assert_eq!(elem.text(), "hello, world!");
|
|
|
|
/// ```
|
|
|
|
pub fn text(&self) -> String {
|
2017-05-14 14:38:56 +00:00
|
|
|
self.texts().fold(String::new(), |ret, new| ret + new)
|
2017-03-08 19:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reference to the first child element with the specific name and namespace, if it
|
|
|
|
/// exists in the direct descendants of this `Element`, else returns `None`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-05-14 14:38:56 +00:00
|
|
|
/// ```rust
|
2017-03-08 19:34:17 +00:00
|
|
|
/// use minidom::Element;
|
|
|
|
///
|
|
|
|
/// let elem: Element = r#"<node xmlns="ns"><a /><a xmlns="other_ns" /><b /></node>"#.parse().unwrap();
|
|
|
|
///
|
|
|
|
/// assert!(elem.get_child("a", "ns").unwrap().is("a", "ns"));
|
|
|
|
/// assert!(elem.get_child("a", "other_ns").unwrap().is("a", "other_ns"));
|
|
|
|
/// assert!(elem.get_child("b", "ns").unwrap().is("b", "ns"));
|
|
|
|
/// assert_eq!(elem.get_child("c", "ns"), None);
|
|
|
|
/// assert_eq!(elem.get_child("b", "other_ns"), None);
|
|
|
|
/// assert_eq!(elem.get_child("a", "inexistent_ns"), None);
|
|
|
|
/// ```
|
|
|
|
pub fn get_child<N: AsRef<str>, NS: AsRef<str>>(&self, name: N, namespace: NS) -> Option<&Element> {
|
|
|
|
for fork in &self.children {
|
|
|
|
if let Node::Element(ref e) = *fork {
|
|
|
|
if e.is(name.as_ref(), namespace.as_ref()) {
|
|
|
|
return Some(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a mutable reference to the first child element with the specific name and namespace,
|
|
|
|
/// if it exists in the direct descendants of this `Element`, else returns `None`.
|
|
|
|
pub fn get_child_mut<N: AsRef<str>, NS: AsRef<str>>(&mut self, name: N, namespace: NS) -> Option<&mut Element> {
|
|
|
|
for fork in &mut self.children {
|
|
|
|
if let Node::Element(ref mut e) = *fork {
|
|
|
|
if e.is(name.as_ref(), namespace.as_ref()) {
|
|
|
|
return Some(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether a specific child with this name and namespace exists in the direct
|
|
|
|
/// descendants of the `Element`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-05-14 14:38:56 +00:00
|
|
|
/// ```rust
|
2017-03-08 19:34:17 +00:00
|
|
|
/// use minidom::Element;
|
|
|
|
///
|
|
|
|
/// let elem: Element = r#"<node xmlns="ns"><a /><a xmlns="other_ns" /><b /></node>"#.parse().unwrap();
|
|
|
|
///
|
|
|
|
/// assert_eq!(elem.has_child("a", "other_ns"), true);
|
|
|
|
/// assert_eq!(elem.has_child("a", "ns"), true);
|
|
|
|
/// assert_eq!(elem.has_child("a", "inexistent_ns"), false);
|
|
|
|
/// assert_eq!(elem.has_child("b", "ns"), true);
|
|
|
|
/// assert_eq!(elem.has_child("b", "other_ns"), false);
|
|
|
|
/// assert_eq!(elem.has_child("b", "inexistent_ns"), false);
|
|
|
|
/// ```
|
|
|
|
pub fn has_child<N: AsRef<str>, NS: AsRef<str>>(&self, name: N, namespace: NS) -> bool {
|
|
|
|
self.get_child(name, namespace).is_some()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 21:58:03 +00:00
|
|
|
fn split_element_name<S: AsRef<str>>(s: S) -> Result<(Option<String>, String)> {
|
|
|
|
let name_parts = s.as_ref().split(':').collect::<Vec<&str>>();
|
|
|
|
match name_parts.len() {
|
|
|
|
2 => Ok((Some(name_parts[0].to_owned()), name_parts[1].to_owned())),
|
|
|
|
1 => Ok((None, name_parts[0].to_owned())),
|
|
|
|
_ => bail!(ErrorKind::InvalidElement),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 20:40:53 +00:00
|
|
|
fn build_element(event: &BytesStart) -> Result<Element> {
|
2017-07-28 21:58:03 +00:00
|
|
|
let mut namespaces = BTreeMap::new();
|
|
|
|
let attributes = event.attributes()
|
|
|
|
.map(|o| {
|
|
|
|
let o = o?;
|
|
|
|
let key = str::from_utf8(o.key)?.to_owned();
|
|
|
|
let value = str::from_utf8(o.value)?.to_owned();
|
|
|
|
Ok((key, value))
|
|
|
|
})
|
|
|
|
.filter(|o| {
|
|
|
|
match o {
|
|
|
|
&Ok((ref key, ref value)) if key == "xmlns" => {
|
|
|
|
namespaces.insert(None, value.to_owned());
|
|
|
|
false
|
|
|
|
},
|
|
|
|
&Ok((ref key, ref value)) if key.starts_with("xmlns:") => {
|
|
|
|
namespaces.insert(Some(key[6..].to_owned()), value.to_owned());
|
|
|
|
false
|
|
|
|
},
|
|
|
|
_ => true,
|
2017-06-07 20:40:53 +00:00
|
|
|
}
|
2017-07-28 21:58:03 +00:00
|
|
|
})
|
|
|
|
.collect::<Result<BTreeMap<String, String>>>()?;
|
2017-06-07 20:40:53 +00:00
|
|
|
|
2017-07-28 21:58:03 +00:00
|
|
|
let (prefix, name) = split_element_name(str::from_utf8(event.name())?)?;
|
|
|
|
let element = Element::new(name, prefix, namespaces, attributes, Vec::new());
|
|
|
|
Ok(element)
|
2017-06-07 20:40:53 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 14:38:56 +00:00
|
|
|
/// An iterator over references to child elements of an `Element`.
|
2017-03-08 19:34:17 +00:00
|
|
|
pub struct Children<'a> {
|
|
|
|
iter: slice::Iter<'a, Node>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for Children<'a> {
|
|
|
|
type Item = &'a Element;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<&'a Element> {
|
2017-05-22 16:49:24 +00:00
|
|
|
for item in &mut self.iter {
|
2017-03-08 19:34:17 +00:00
|
|
|
if let Node::Element(ref child) = *item {
|
|
|
|
return Some(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-14 14:38:56 +00:00
|
|
|
/// An iterator over mutable references to child elements of an `Element`.
|
2017-03-08 19:34:17 +00:00
|
|
|
pub struct ChildrenMut<'a> {
|
|
|
|
iter: slice::IterMut<'a, Node>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for ChildrenMut<'a> {
|
|
|
|
type Item = &'a mut Element;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<&'a mut Element> {
|
2017-05-22 16:49:24 +00:00
|
|
|
for item in &mut self.iter {
|
2017-03-08 19:34:17 +00:00
|
|
|
if let Node::Element(ref mut child) = *item {
|
|
|
|
return Some(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-14 14:38:56 +00:00
|
|
|
/// An iterator over references to child text nodes of an `Element`.
|
|
|
|
pub struct Texts<'a> {
|
|
|
|
iter: slice::Iter<'a, Node>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for Texts<'a> {
|
|
|
|
type Item = &'a str;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<&'a str> {
|
2017-05-22 16:49:24 +00:00
|
|
|
for item in &mut self.iter {
|
2017-05-14 14:38:56 +00:00
|
|
|
if let Node::Text(ref child) = *item {
|
|
|
|
return Some(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator over mutable references to child text nodes of an `Element`.
|
|
|
|
pub struct TextsMut<'a> {
|
|
|
|
iter: slice::IterMut<'a, Node>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for TextsMut<'a> {
|
|
|
|
type Item = &'a mut String;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<&'a mut String> {
|
2017-05-22 16:49:24 +00:00
|
|
|
for item in &mut self.iter {
|
2017-05-14 14:38:56 +00:00
|
|
|
if let Node::Text(ref mut child) = *item {
|
|
|
|
return Some(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator over references to all child nodes of an `Element`.
|
|
|
|
pub type Nodes<'a> = slice::Iter<'a, Node>;
|
|
|
|
|
|
|
|
/// An iterator over mutable references to all child nodes of an `Element`.
|
|
|
|
pub type NodesMut<'a> = slice::IterMut<'a, Node>;
|
|
|
|
|
|
|
|
/// An iterator over the attributes of an `Element`.
|
|
|
|
pub struct Attrs<'a> {
|
|
|
|
iter: btree_map::Iter<'a, String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for Attrs<'a> {
|
|
|
|
type Item = (&'a str, &'a str);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
self.iter.next().map(|(x, y)| (x.as_ref(), y.as_ref()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator over the attributes of an `Element`, with the values mutable.
|
|
|
|
pub struct AttrsMut<'a> {
|
|
|
|
iter: btree_map::IterMut<'a, String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for AttrsMut<'a> {
|
|
|
|
type Item = (&'a str, &'a mut String);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
self.iter.next().map(|(x, y)| (x.as_ref(), y))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:34:17 +00:00
|
|
|
/// A builder for `Element`s.
|
|
|
|
pub struct ElementBuilder {
|
|
|
|
root: Element,
|
2017-07-28 21:58:03 +00:00
|
|
|
namespaces: BTreeMap<Option<String>, String>,
|
2017-03-08 19:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ElementBuilder {
|
|
|
|
/// Sets the namespace.
|
|
|
|
pub fn ns<S: Into<String>>(mut self, namespace: S) -> ElementBuilder {
|
2017-07-28 21:58:03 +00:00
|
|
|
self.namespaces
|
|
|
|
.insert(self.root.prefix.clone(), namespace.into());
|
2017-03-08 19:34:17 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets an attribute.
|
|
|
|
pub fn attr<S: Into<String>, V: IntoAttributeValue>(mut self, name: S, value: V) -> ElementBuilder {
|
|
|
|
self.root.set_attr(name, value);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Appends anything implementing `IntoElements` into the tree.
|
|
|
|
pub fn append<T: IntoElements>(mut self, into: T) -> ElementBuilder {
|
|
|
|
{
|
|
|
|
let mut emitter = ElementEmitter::new(&mut self.root);
|
|
|
|
into.into_elements(&mut emitter);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds the `Element`.
|
|
|
|
pub fn build(self) -> Element {
|
2017-07-28 21:58:03 +00:00
|
|
|
let mut element = self.root;
|
|
|
|
// Set namespaces
|
|
|
|
element.namespaces = Rc::new(NamespaceSet::from(self.namespaces));
|
|
|
|
// Propagate namespaces
|
|
|
|
for node in &element.children {
|
|
|
|
if let Node::Element(ref e) = *node {
|
2017-08-13 00:28:44 +00:00
|
|
|
e.namespaces.set_parent(element.namespaces.clone());
|
2017-07-28 21:58:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
element
|
2017-03-08 19:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 21:58:03 +00:00
|
|
|
#[cfg(test)]
|
2017-03-08 19:34:17 +00:00
|
|
|
#[test]
|
|
|
|
fn test_element_new() {
|
2017-05-03 16:39:40 +00:00
|
|
|
use std::iter::FromIterator;
|
|
|
|
|
2017-03-08 19:34:17 +00:00
|
|
|
let elem = Element::new( "name".to_owned()
|
2017-07-28 21:58:03 +00:00
|
|
|
, None
|
2017-03-08 19:34:17 +00:00
|
|
|
, Some("namespace".to_owned())
|
2017-04-26 21:44:58 +00:00
|
|
|
, BTreeMap::from_iter(vec![ ("name".to_string(), "value".to_string()) ].into_iter() )
|
2017-03-08 19:34:17 +00:00
|
|
|
, Vec::new() );
|
|
|
|
|
|
|
|
assert_eq!(elem.name(), "name");
|
2017-07-28 21:58:03 +00:00
|
|
|
assert_eq!(elem.ns(), Some("namespace".to_owned()));
|
2017-03-08 19:34:17 +00:00
|
|
|
assert_eq!(elem.attr("name"), Some("value"));
|
|
|
|
assert_eq!(elem.attr("inexistent"), None);
|
|
|
|
}
|