1//! Contains high-level interface for a pull-based XML parser.
2//!
3//! The most important type in this module is `EventReader`, which provides an iterator
4//! view for events in XML document.
56use std::io::{Read};
7use std::result;
89use common::{Position, TextPosition};
1011pub use self::config::ParserConfig;
12pub use self::events::XmlEvent;
1314use self::parser::PullParser;
1516mod lexer;
17mod parser;
18mod config;
19mod events;
2021mod error;
22pub use self::error::{Error, ErrorKind};
2324/// A result type yielded by `XmlReader`.
25pub type Result<T> = result::Result<T, Error>;
2627/// A wrapper around an `std::io::Read` instance which provides pull-based XML parsing.
28pub struct EventReader<R: Read> {
29 source: R,
30 parser: PullParser
31}
3233impl<R: Read> EventReader<R> {
34/// Creates a new reader, consuming the given stream.
35#[inline]
36pub fn new(source: R) -> EventReader<R> {
37 EventReader::new_with_config(source, ParserConfig::new())
38 }
3940/// Creates a new reader with the provded configuration, consuming the given stream.
41#[inline]
42pub fn new_with_config(source: R, config: ParserConfig) -> EventReader<R> {
43 EventReader { source: source, parser: PullParser::new(config) }
44 }
4546/// Pulls and returns next XML event from the stream.
47 ///
48 /// If returned event is `XmlEvent::Error` or `XmlEvent::EndDocument`, then
49 /// further calls to this method will return this event again.
50#[inline]
51pub fn next(&mut self) -> Result<XmlEvent> {
52self.parser.next(&mut self.source)
53 }
5455pub fn source(&self) -> &R { &self.source }
56pub fn source_mut(&mut self) -> &mut R { &mut self.source }
5758/// Unwraps this `EventReader`, returning the underlying reader.
59 ///
60 /// Note that this operation is destructive; unwrapping the reader and wrapping it
61 /// again with `EventReader::new()` will create a fresh reader which will attempt
62 /// to parse an XML document from the beginning.
63pub fn into_inner(self) -> R {
64self.source
65 }
66}
6768impl<B: Read> Position for EventReader<B> {
69/// Returns the position of the last event produced by the reader.
70#[inline]
71fn position(&self) -> TextPosition {
72self.parser.position()
73 }
74}
7576impl<R: Read> IntoIterator for EventReader<R> {
77type Item = Result<XmlEvent>;
78type IntoIter = Events<R>;
7980fn into_iter(self) -> Events<R> {
81 Events { reader: self, finished: false }
82 }
83}
8485/// An iterator over XML events created from some type implementing `Read`.
86///
87/// When the next event is `xml::event::Error` or `xml::event::EndDocument`, then
88/// it will be returned by the iterator once, and then it will stop producing events.
89pub struct Events<R: Read> {
90 reader: EventReader<R>,
91 finished: bool
92}
9394impl<R: Read> Events<R> {
95/// Unwraps the iterator, returning the internal `EventReader`.
96#[inline]
97pub fn into_inner(self) -> EventReader<R> {
98self.reader
99 }
100101pub fn source(&self) -> &R { &self.reader.source }
102pub fn source_mut(&mut self) -> &mut R { &mut self.reader.source }
103104}
105106impl<R: Read> Iterator for Events<R> {
107type Item = Result<XmlEvent>;
108109#[inline]
110fn next(&mut self) -> Option<Result<XmlEvent>> {
111if self.finished && !self.reader.parser.is_ignoring_end_of_stream() { None }
112else {
113let ev = self.reader.next();
114match ev {
115Ok(XmlEvent::EndDocument) | Err(_) => self.finished = true,
116_ => {}
117 }
118Some(ev)
119 }
120 }
121}
122123impl<'r> EventReader<&'r [u8]> {
124/// A convenience method to create an `XmlReader` from a string slice.
125#[inline]
126pub fn from_str(source: &'r str) -> EventReader<&'r [u8]> {
127 EventReader::new(source.as_bytes())
128 }
129}