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
// RustyXML
// Copyright 2013-2016 RustyXML developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![crate_name = "xml"]
#![crate_type = "lib"]
#![forbid(non_camel_case_types)]
#![warn(missing_docs)]
// Required for benchmarks
#![cfg_attr(feature = "bench", feature(test))]

/*!
 * An XML parsing library
 */

pub use crate::element::ChildElements;
pub use crate::element::Element;
pub use crate::element_builder::BuilderError;
pub use crate::element_builder::ElementBuilder;
pub use crate::parser::Event;
pub use crate::parser::Parser;
pub use crate::parser::ParserError;

use std::char;
use std::collections::HashMap;
use std::fmt;

mod element;
mod element_builder;
mod parser;

// General functions

#[inline]
/// Escapes ', ", &, <, and > with the appropriate XML entities.
pub fn escape(input: &str) -> String {
    let mut result = String::with_capacity(input.len());

    for c in input.chars() {
        match c {
            '&' => result.push_str("&amp;"),
            '<' => result.push_str("&lt;"),
            '>' => result.push_str("&gt;"),
            '\'' => result.push_str("&apos;"),
            '"' => result.push_str("&quot;"),
            o => result.push(o),
        }
    }
    result
}

#[inline]
/// Unescapes all valid XML entities in a string.
/// Returns the first invalid entity on failure.
pub fn unescape(input: &str) -> Result<String, String> {
    let mut result = String::with_capacity(input.len());

    let mut it = input.split('&');

    // Push everything before the first '&'
    if let Some(sub) = it.next() {
        result.push_str(sub);
    }

    for sub in it {
        match sub.find(';') {
            Some(idx) => {
                let ent = &sub[..idx];
                match ent {
                    "quot" => result.push('"'),
                    "apos" => result.push('\''),
                    "gt" => result.push('>'),
                    "lt" => result.push('<'),
                    "amp" => result.push('&'),
                    ent => {
                        let val = if ent.starts_with("#x") {
                            u32::from_str_radix(&ent[2..], 16).ok()
                        } else if ent.starts_with('#') {
                            u32::from_str_radix(&ent[1..], 10).ok()
                        } else {
                            None
                        };
                        match val.and_then(char::from_u32) {
                            Some(c) => result.push(c),
                            None => return Err(format!("&{};", ent)),
                        }
                    }
                }
                result.push_str(&sub[idx + 1..]);
            }
            None => return Err("&".to_owned() + sub),
        }
    }
    Ok(result)
}

// General types
#[derive(Clone, PartialEq, Debug)]
/// An Enum describing a XML Node
pub enum Xml {
    /// An XML Element
    ElementNode(Element),
    /// Character Data
    CharacterNode(String),
    /// CDATA
    CDATANode(String),
    /// A XML Comment
    CommentNode(String),
    /// Processing Information
    PINode(String),
}

#[derive(PartialEq, Eq, Debug)]
/// Structure describing an opening tag
pub struct StartTag {
    /// The tag's name
    pub name: String,
    /// The tag's namespace
    pub ns: Option<String>,
    /// The tag's prefix
    pub prefix: Option<String>,
    /// The tag's attributes
    pub attributes: HashMap<(String, Option<String>), String>,
}

#[derive(PartialEq, Eq, Debug)]
/// Structure describing a closing tag
pub struct EndTag {
    /// The tag's name
    pub name: String,
    /// The tag's namespace
    pub ns: Option<String>,
    /// The tag's prefix
    pub prefix: Option<String>,
}

impl fmt::Display for Xml {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Xml::ElementNode(ref elem) => elem.fmt(f),
            Xml::CharacterNode(ref data) => write!(f, "{}", escape(&data)),
            Xml::CDATANode(ref data) => write!(f, "<![CDATA[{}]]>", &data),
            Xml::CommentNode(ref data) => write!(f, "<!--{}-->", &data),
            Xml::PINode(ref data) => write!(f, "<?{}?>", &data),
        }
    }
}

#[cfg(test)]
mod lib_tests {
    use super::{escape, unescape, Element, Xml};

    #[test]
    fn test_escape() {
        let esc = escape("&<>'\"");
        assert_eq!(esc, "&amp;&lt;&gt;&apos;&quot;");
    }

    #[test]
    fn test_unescape() {
        let unesc = unescape("&amp;lt;&lt;&gt;&apos;&quot;&#x201c;&#x201d;&#38;&#34;");
        assert_eq!(
            unesc.as_ref().map(|x| &x[..]),
            Ok("&lt;<>'\"\u{201c}\u{201d}&\""),
        );
    }

    #[test]
    fn test_unescape_invalid() {
        let unesc = unescape("&amp;&nbsp;");
        assert_eq!(unesc.as_ref().map_err(|x| &x[..]), Err("&nbsp;"));
    }

    #[test]
    fn test_show_element() {
        let elem = Element::new("a".to_owned(), None, vec![]);
        assert_eq!(format!("{}", elem), "<a/>");

        let elem = Element::new(
            "a".to_owned(),
            None,
            vec![("href".to_owned(), None, "http://rust-lang.org".to_owned())],
        );
        assert_eq!(format!("{}", elem), "<a href='http://rust-lang.org'/>");

        let mut elem = Element::new("a".to_owned(), None, vec![]);
        elem.tag(Element::new("b".to_owned(), None, vec![]));
        assert_eq!(format!("{}", elem), "<a><b/></a>");

        let mut elem = Element::new(
            "a".to_owned(),
            None,
            vec![("href".to_owned(), None, "http://rust-lang.org".to_owned())],
        );
        elem.tag(Element::new("b".to_owned(), None, vec![]));
        assert_eq!(
            format!("{}", elem),
            "<a href='http://rust-lang.org'><b/></a>",
        );
    }

    #[test]
    fn test_show_element_xmlns() {
        let elem: Element = "<a xmlns='urn:test'/>".parse().unwrap();
        assert_eq!(format!("{}", elem), "<a xmlns='urn:test'/>");

        let elem: Element = "<a xmlns='urn:test'><b xmlns='urn:toast'/></a>"
            .parse()
            .unwrap();
        assert_eq!(
            format!("{}", elem),
            "<a xmlns='urn:test'><b xmlns='urn:toast'/></a>",
        );

        let elem = Element::new(
            "a".to_owned(),
            Some("urn:test".to_owned()),
            vec![("href".to_owned(), None, "http://rust-lang.org".to_owned())],
        );
        assert_eq!(
            format!("{}", elem),
            "<a xmlns='urn:test' href='http://rust-lang.org'/>",
        );
    }

    #[test]
    fn test_show_characters() {
        let chars = Xml::CharacterNode("some text".to_owned());
        assert_eq!(format!("{}", chars), "some text");
    }

    #[test]
    fn test_show_cdata() {
        let chars = Xml::CDATANode("some text".to_owned());
        assert_eq!(format!("{}", chars), "<![CDATA[some text]]>");
    }

    #[test]
    fn test_show_comment() {
        let chars = Xml::CommentNode("some text".to_owned());
        assert_eq!(format!("{}", chars), "<!--some text-->");
    }

    #[test]
    fn test_show_pi() {
        let chars = Xml::PINode("xml version='1.0'".to_owned());
        assert_eq!(format!("{}", chars), "<?xml version='1.0'?>");
    }

    #[test]
    fn test_content_str() {
        let mut elem = Element::new("a".to_owned(), None, vec![]);
        elem.pi("processing information".to_owned())
            .cdata("<hello/>".to_owned())
            .tag_stay(Element::new("b".to_owned(), None, vec![]))
            .text("World".to_owned())
            .comment("Nothing to see".to_owned());
        assert_eq!(elem.content_str(), "<hello/>World");
    }
}

#[cfg(test)]
#[cfg(feature = "bench")]
mod lib_bench {
    extern crate test;

    use self::test::Bencher;
    use super::{escape, unescape};
    use std::iter::repeat;

    #[bench]
    fn bench_escape(bh: &mut Bencher) {
        let input: String = repeat("&<>'\"").take(100).collect();
        bh.iter(|| escape(&input));
        bh.bytes = input.len() as u64;
    }

    #[bench]
    fn bench_unescape(bh: &mut Bencher) {
        let input: String = repeat("&amp;&lt;&gt;&apos;&quot;").take(50).collect();
        bh.iter(|| unescape(&input));
        bh.bytes = input.len() as u64;
    }
}