docx_reader/reader/
document_rels.rs1use std::collections::BTreeMap;
2use std::collections::HashSet;
3use std::io::{Cursor, Read};
4use std::path::*;
5use std::str::FromStr;
6use xml::reader::{EventReader, XmlEvent};
7
8use super::errors::*;
9use super::*;
10
11pub type RId = String;
12
13#[derive(Debug, Clone, PartialEq)]
14pub struct ReadDocumentRels {
15 rels: BTreeMap<String, HashSet<(RId, PathBuf, Option<String>)>>,
16}
17
18impl ReadDocumentRels {
19 pub fn find_target_path(&self, target: &str) -> Option<Vec<(RId, PathBuf, Option<String>)>> {
20 self.rels
21 .get(target)
22 .map(|s| s.clone().into_iter().collect())
23 }
24}
25
26pub fn read_document_rels(
27 archive: &mut zip::read::ZipArchive<Cursor<&[u8]>>,
28 main_path: impl AsRef<Path>,
29) -> Result<ReadDocumentRels, ReaderError> {
30 let dir = &main_path
31 .as_ref()
32 .parent()
33 .ok_or(ReaderError::DocumentRelsNotFoundError)?;
34 let p = find_rels_filename(&main_path)?;
35 let p = p.to_str().ok_or(ReaderError::DocumentRelsNotFoundError)?;
36 let data = read_zip(archive, p)?;
37 let rels = read_rels_xml(&data[..], dir)?;
38 Ok(rels)
39}
40
41fn read_rels_xml<R: Read>(
42 reader: R,
43 dir: impl AsRef<Path>,
44) -> Result<ReadDocumentRels, ReaderError> {
45 let mut parser = EventReader::new(reader);
46 let mut rels = ReadDocumentRels {
47 rels: BTreeMap::new(),
48 };
49 loop {
50 let e = parser.next();
51 match e {
52 Ok(XmlEvent::StartElement {
53 attributes, name, ..
54 }) => {
55 let e = XMLElement::from_str(&name.local_name).unwrap();
56 if let XMLElement::Relationship = e {
57 let mut rel_type = "".to_owned();
58 let mut rid = "".to_owned();
59 let mut target_mode = None;
60 let mut target_string = "".to_owned();
61 for a in attributes {
62 let local_name = &a.name.local_name;
63 if local_name == "Type" {
64 rel_type = a.value.to_owned();
65 } else if local_name == "Target" {
66 target_string = a.value.to_owned();
68 } else if local_name == "Id" {
69 rid = a.value.to_owned();
70 } else if local_name == "TargetMode" {
71 target_mode = Some(a.value.to_owned());
72 }
73 }
74
75 let target = if !rel_type.ends_with("hyperlink") {
76 Path::new(dir.as_ref()).join(target_string)
77 } else {
78 Path::new("").join(target_string)
79 };
80
81 let current = rels.rels.remove(&rel_type);
82 if let Some(mut paths) = current {
83 paths.insert((rid, target, target_mode));
84 rels.rels.insert(rel_type, paths);
85 } else {
86 let s: HashSet<(RId, PathBuf, Option<String>)> =
87 vec![(rid, target, target_mode)].into_iter().collect();
88 rels.rels.insert(rel_type, s);
89 }
90 continue;
91 }
92 }
93 Ok(XmlEvent::EndElement { name, .. }) => {
94 let e = XMLElement::from_str(&name.local_name).unwrap();
95 if let XMLElement::Relationships = e {
96 break;
97 }
98 }
99 Err(_) => return Err(ReaderError::XMLReadError),
100 _ => {}
101 }
102 }
103 Ok(rels)
104}
105
106fn find_rels_filename(main_path: impl AsRef<Path>) -> Result<PathBuf, ReaderError> {
107 let path = main_path.as_ref();
108 let dir = path
109 .parent()
110 .ok_or(ReaderError::DocumentRelsNotFoundError)?;
111 let base = path
112 .file_stem()
113 .ok_or(ReaderError::DocumentRelsNotFoundError)?;
114 Ok(Path::new(dir)
115 .join("_rels")
116 .join(base)
117 .with_extension("xml.rels"))
118}