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
use std::io::Read;
use std::str::FromStr;
use crate::reader::*;
use xml::reader::{EventReader, XmlEvent};
use super::{Paragraph, Table};
impl FromXML for Document {
fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
let mut parser = EventReader::new(reader);
let mut doc = Self::default();
loop {
let e = parser.next();
match e {
Ok(XmlEvent::StartElement {
attributes, name, ..
}) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
match e {
XMLElement::Paragraph => {
let p = Paragraph::read(&mut parser, &attributes)?;
doc = doc.add_paragraph(p);
continue;
}
XMLElement::Table => {
let t = Table::read(&mut parser, &attributes)?;
doc = doc.add_table(t);
continue;
}
XMLElement::BookmarkStart => {
let s = BookmarkStart::read(&mut parser, &attributes)?;
doc = doc.add_bookmark_start(s.id, s.name);
continue;
}
XMLElement::BookmarkEnd => {
let e = BookmarkEnd::read(&mut parser, &attributes)?;
doc = doc.add_bookmark_end(e.id);
continue;
}
XMLElement::CommentRangeStart => {
if let Some(id) = read(&attributes, "id") {
if let Ok(id) = usize::from_str(&id) {
let comment = Comment::new(id);
doc = doc.add_comment_start(comment);
}
}
continue;
}
XMLElement::CommentRangeEnd => {
if let Some(id) = read(&attributes, "id") {
if let Ok(id) = usize::from_str(&id) {
doc = doc.add_comment_end(id);
}
}
continue;
}
XMLElement::SectionProperty => {
let e = SectionProperty::read(&mut parser, &attributes)?;
doc = doc.default_section_property(e);
continue;
}
XMLElement::StructuredDataTag => {
if let Ok(tag) = StructuredDataTag::read(&mut parser, &attributes) {
doc = doc.add_structured_data_tag(tag);
}
continue;
}
_ => {}
}
}
Ok(XmlEvent::EndDocument) => break,
Err(_) => return Err(ReaderError::XMLReadError),
_ => {}
}
}
Ok(doc)
}
}