1use super::*;
2use crate::reader::{FromXML, ReaderError};
3use std::str::FromStr;
4use std::{
5 collections::{BTreeMap, BTreeSet},
6 io::Read,
7 path::{Path, PathBuf},
8};
9pub type ReadRels = BTreeMap<String, BTreeSet<(RId, PathBuf, Option<String>)>>;
10
11impl FromXML for Rels {
12 fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
13 let parser = EventReader::new(reader);
14 let mut s = Self::default();
15 let mut depth = 0;
16 for e in parser {
17 match e {
18 Ok(XmlEvent::StartElement { attributes, .. }) => {
19 if depth == 1 {
20 let mut rel_type = "".to_owned();
21 let mut target = "".to_owned();
22 let mut id = "".to_owned();
23 for attr in attributes {
24 let name: &str = &attr.name.local_name;
25 if name == "Type" {
26 rel_type = attr.value.clone();
27 } else if name == "Target" {
28 target = attr.value.clone();
29 } else if name == "Id" {
30 id = attr.value.clone();
31 }
32 }
33 s.rels.push((rel_type, id, target));
35 }
36 depth += 1;
37 }
38 Ok(XmlEvent::EndElement { .. }) => {
39 depth -= 1;
40 }
41 Err(_) => return Err(ReaderError::XMLReadError),
42 _ => {}
43 }
44 }
45 Ok(s)
46 }
47}
48
49pub fn find_rels_filename(main_path: impl AsRef<Path>) -> Result<PathBuf, ReaderError> {
50 let path = main_path.as_ref();
51 let dir = path
52 .parent()
53 .ok_or(ReaderError::DocumentRelsNotFoundError)?;
54 let base = path
55 .file_stem()
56 .ok_or(ReaderError::DocumentRelsNotFoundError)?;
57 Ok(Path::new(dir)
58 .join("_rels")
59 .join(base)
60 .with_extension("xml.rels"))
61}
62
63pub fn read_rels_xml<R: Read>(reader: R, dir: impl AsRef<Path>) -> Result<ReadRels, ReaderError> {
64 let mut parser = EventReader::new(reader);
65 let mut rels: BTreeMap<String, BTreeSet<(RId, PathBuf, Option<String>)>> = BTreeMap::new();
66
67 loop {
68 let e = parser.next();
69 match e {
70 Ok(XmlEvent::StartElement {
71 attributes, name, ..
72 }) => {
73 let e = XMLElement::from_str(&name.local_name).unwrap();
74 if let XMLElement::Relationship = e {
75 let mut rel_type = "".to_owned();
76 let mut rid = "".to_owned();
77 let mut target_mode = None;
78 let mut target_string = "".to_owned();
79 for a in attributes {
80 let local_name = &a.name.local_name;
81 if local_name == "Type" {
82 rel_type = a.value.to_owned();
83 } else if local_name == "Target" {
84 target_string = a.value.to_owned();
86 } else if local_name == "Id" {
87 rid = a.value.to_owned();
88 } else if local_name == "TargetMode" {
89 target_mode = Some(a.value.to_owned());
90 }
91 }
92
93 let target = if !rel_type.ends_with("hyperlink") {
94 Path::new(dir.as_ref()).join(target_string)
95 } else {
96 Path::new("").join(target_string)
97 };
98
99 let current = rels.remove(&rel_type);
100 if let Some(mut paths) = current {
101 paths.insert((rid, target, target_mode));
102 rels.insert(rel_type, paths);
103 } else {
104 let s: BTreeSet<(RId, PathBuf, Option<String>)> =
105 vec![(rid, target, target_mode)].into_iter().collect();
106 rels.insert(rel_type, s);
107 }
108 continue;
109 }
110 }
111 Ok(XmlEvent::EndElement { name, .. }) => {
112 let e = XMLElement::from_str(&name.local_name).unwrap();
113 if let XMLElement::Relationships = e {
114 break;
115 }
116 }
117 Err(_) => return Err(ReaderError::XMLReadError),
118 _ => {}
119 }
120 }
121 Ok(rels)
122}
123
124#[cfg(test)]
125mod tests {
126
127 use super::*;
128 #[cfg(test)]
129 use pretty_assertions::assert_eq;
130
131 #[test]
132 fn test_from_xml() {
133 let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
134<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
135 <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml" />
136</Relationships>"#;
137 let c = Rels::from_xml(xml.as_bytes()).unwrap();
138 let rels =
139 vec![
140 (
141 "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"
142 .to_owned(),
143 "rId1".to_owned(),
144 "docProps/core.xml".to_owned(),
145 )];
146 assert_eq!(Rels { rels }, c);
147 }
148}