docx_rs/reader/
document.rs

1use std::io::Read;
2use std::str::FromStr;
3
4use crate::reader::*;
5use xml::reader::{EventReader, XmlEvent};
6
7use super::{Paragraph, Table};
8
9impl FromXML for Document {
10    fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
11        let mut parser = EventReader::new(reader);
12        let mut doc = Self::default();
13        loop {
14            let e = parser.next();
15            match e {
16                Ok(XmlEvent::StartElement {
17                    attributes, name, ..
18                }) => {
19                    let e = XMLElement::from_str(&name.local_name).unwrap();
20                    match e {
21                        XMLElement::Paragraph => {
22                            let p = Paragraph::read(&mut parser, &attributes)?;
23                            doc = doc.add_paragraph(p);
24                            continue;
25                        }
26                        XMLElement::Table => {
27                            let t = Table::read(&mut parser, &attributes)?;
28                            doc = doc.add_table(t);
29                            continue;
30                        }
31                        XMLElement::BookmarkStart => {
32                            let s = BookmarkStart::read(&mut parser, &attributes)?;
33                            doc = doc.add_bookmark_start(s.id, s.name);
34                            continue;
35                        }
36                        XMLElement::BookmarkEnd => {
37                            let e = BookmarkEnd::read(&mut parser, &attributes)?;
38                            doc = doc.add_bookmark_end(e.id);
39                            continue;
40                        }
41                        XMLElement::CommentRangeStart => {
42                            if let Some(id) = read(&attributes, "id") {
43                                if let Ok(id) = usize::from_str(&id) {
44                                    let comment = Comment::new(id);
45                                    doc = doc.add_comment_start(comment);
46                                }
47                            }
48                            continue;
49                        }
50                        XMLElement::CommentRangeEnd => {
51                            if let Some(id) = read(&attributes, "id") {
52                                if let Ok(id) = usize::from_str(&id) {
53                                    doc = doc.add_comment_end(id);
54                                }
55                            }
56                            continue;
57                        }
58                        XMLElement::SectionProperty => {
59                            let e = SectionProperty::read(&mut parser, &attributes)?;
60                            doc = doc.default_section_property(e);
61                            continue;
62                        }
63                        XMLElement::StructuredDataTag => {
64                            if let Ok(tag) = StructuredDataTag::read(&mut parser, &attributes) {
65                                doc = doc.add_structured_data_tag(tag);
66                            }
67                            continue;
68                        }
69                        _ => {}
70                    }
71                }
72                Ok(XmlEvent::EndDocument) => break,
73                Err(_) => return Err(ReaderError::XMLReadError),
74                _ => {}
75            }
76        }
77        Ok(doc)
78    }
79}