Skip to main content

docx_rs/reader/
header.rs

1use std::io::Read;
2use std::str::FromStr;
3
4use crate::reader::*;
5
6use super::{Paragraph, Table};
7
8impl FromXML for Header {
9    fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
10        let mut parser = EventReader::new(reader);
11        let mut header = Self::default();
12        loop {
13            let e = parser.next();
14            match e {
15                Ok(XmlEvent::StartElement {
16                    attributes, name, ..
17                }) => {
18                    let e = XMLElement::from_str(&name.local_name).unwrap();
19                    match e {
20                        XMLElement::Paragraph => {
21                            if let Ok(p) = Paragraph::read(&mut parser, &attributes) {
22                                header = header.add_paragraph(p);
23                            }
24                            continue;
25                        }
26                        XMLElement::Table => {
27                            if let Ok(t) = Table::read(&mut parser, &attributes) {
28                                header = header.add_table(t);
29                            }
30                            continue;
31                        }
32                        XMLElement::StructuredDataTag => {
33                            if let Ok(tag) = StructuredDataTag::read(&mut parser, &attributes) {
34                                header = header.add_structured_data_tag(tag);
35                            }
36                            continue;
37                        }
38                        _ => {}
39                    }
40                }
41                Ok(XmlEvent::EndDocument) => break,
42                Err(_) => return Err(ReaderError::XMLReadError),
43                _ => {}
44            }
45        }
46        Ok(header)
47    }
48}
49
50#[test]
51fn test_header_from_xml() {
52    let xml = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
53<w:hdr xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
54    xmlns:o="urn:schemas-microsoft-com:office:office"
55    xmlns:v="urn:schemas-microsoft-com:vml"
56    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
57    xmlns:w10="urn:schemas-microsoft-com:office:word"
58    xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
59    xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
60    xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
61    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
62    xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
63    xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14 wp14">
64    <w:p w14:paraId="12345678">
65        <w:pPr>
66            <w:rPr />
67        </w:pPr>
68        <w:r>
69            <w:rPr />
70            <w:t xml:space="preserve">Hello Header</w:t>
71        </w:r>
72    </w:p>
73</w:hdr>"#;
74    let h = Header::from_xml(xml.as_bytes()).unwrap();
75    let expected =
76        Header::new().add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello Header")));
77    assert_eq!(h, expected)
78}