use quick_xml::events::Event;
use quick_xml::Reader;
use super::HwpxError;
#[derive(Debug, Clone)]
pub struct PackageItem {
pub href: String,
pub media_type: String,
pub id: String,
pub is_embedded: bool,
}
#[derive(Debug, Default)]
pub struct PackageInfo {
pub section_files: Vec<String>,
pub section_master_page_files: Vec<Vec<String>>,
pub master_page_items: Vec<PackageItem>,
pub bin_data_items: Vec<PackageItem>,
}
pub fn parse_content_hpf(xml: &str) -> Result<PackageInfo, HwpxError> {
let mut reader = Reader::from_str(xml);
let mut info = PackageInfo::default();
let mut buf = Vec::new();
let mut all_items: Vec<(String, String, String, bool)> = Vec::new(); let mut spine_order: Vec<String> = Vec::new();
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Empty(ref e)) | Ok(Event::Start(ref e)) => {
let ename = e.name();
let local_name = local_tag_name(ename.as_ref());
match local_name {
b"item" => {
let mut id = String::new();
let mut href = String::new();
let mut media_type = String::new();
let mut is_embedded = true;
for attr in e.attributes().flatten() {
match attr.key.as_ref().as_bytes() {
b"id" => id = attr_value(&attr),
b"href" => href = attr_value(&attr),
b"media-type" => media_type = attr_value(&attr),
b"isEmbeded" => {
is_embedded = attr_value(&attr) != "0";
}
_ => {}
}
}
if !id.is_empty() && !href.is_empty() {
all_items.push((id, href, media_type, is_embedded));
}
}
b"itemref" => {
for attr in e.attributes().flatten() {
if attr.key.as_ref().as_bytes() == b"idref" {
spine_order.push(attr_value(&attr));
}
}
}
_ => {}
}
}
Ok(Event::Eof) => break,
Err(e) => return Err(HwpxError::XmlError(format!("content.hpf: {}", e))),
_ => {}
}
buf.clear();
}
for idref in &spine_order {
if let Some((_, href, media_type, _)) = all_items.iter().find(|(id, _, _, _)| id == idref) {
if media_type == "application/xml" && href.contains("section") {
info.section_files.push(href.clone());
}
}
}
if info.section_files.is_empty() {
let mut section_items: Vec<_> = all_items
.iter()
.filter(|(_, href, mt, _)| mt == "application/xml" && href.contains("section"))
.collect();
section_items.sort_by(|a, b| a.1.cmp(&b.1));
info.section_files = section_items
.into_iter()
.map(|(_, href, _, _)| href.clone())
.collect();
}
info.section_master_page_files = collect_section_master_pages(&all_items, &info.section_files);
info.master_page_items = collect_master_page_items(&all_items);
for (id, href, media_type, is_embedded) in &all_items {
let is_image = media_type.starts_with("image/");
let is_bin_data_path = href.starts_with("BinData/") || href.contains("/BinData/");
let is_external_link = !*is_embedded && media_type != "application/xml";
if is_image || is_bin_data_path || is_external_link {
info.bin_data_items.push(PackageItem {
href: href.clone(),
media_type: media_type.clone(),
id: id.clone(),
is_embedded: *is_embedded,
});
}
}
Ok(info)
}
fn collect_master_page_items(all_items: &[(String, String, String, bool)]) -> Vec<PackageItem> {
all_items
.iter()
.filter(|(_, href, media_type, _)| {
media_type == "application/xml" && href.to_ascii_lowercase().contains("masterpage")
})
.map(|(id, href, media_type, is_embedded)| PackageItem {
href: href.clone(),
media_type: media_type.clone(),
id: id.clone(),
is_embedded: *is_embedded,
})
.collect()
}
fn collect_section_master_pages(
all_items: &[(String, String, String, bool)],
section_files: &[String],
) -> Vec<Vec<String>> {
let mut groups = vec![Vec::new(); section_files.len()];
if section_files.is_empty() {
return groups;
}
let mut pending: Vec<String> = Vec::new();
for (_, href, media_type, _) in all_items {
if media_type != "application/xml" {
continue;
}
let lower_href = href.to_ascii_lowercase();
if lower_href.contains("masterpage") {
pending.push(href.clone());
continue;
}
if let Some(section_idx) = section_files.iter().position(|section| section == href) {
if !pending.is_empty() {
groups[section_idx].append(&mut pending);
}
}
}
if groups.iter().all(|group| group.is_empty()) {
let mut master_pages: Vec<String> = all_items
.iter()
.filter(|(_, href, media_type, _)| {
media_type == "application/xml" && href.to_ascii_lowercase().contains("masterpage")
})
.map(|(_, href, _, _)| href.clone())
.collect();
master_pages.sort();
if !master_pages.is_empty() {
let chunk = master_pages.len().div_ceil(section_files.len());
for (section_idx, group) in groups.iter_mut().enumerate() {
let start = section_idx * chunk;
let end = ((section_idx + 1) * chunk).min(master_pages.len());
if start < end {
group.extend_from_slice(&master_pages[start..end]);
}
}
}
}
groups
}
fn attr_value(attr: &quick_xml::events::attributes::Attribute) -> String {
attr.value.as_ref().to_owned()
}
fn local_tag_name(name: &str) -> &[u8] {
name.rsplit(':').next().unwrap_or(name).as_bytes()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_content_hpf() {
let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<opf:package xmlns:opf="http://www.idpf.org/2007/opf/" version="" unique-identifier="" id="">
<opf:manifest>
<opf:item id="header" href="Contents/header.xml" media-type="application/xml"/>
<opf:item id="image1" href="BinData/image1.png" media-type="image/png" isEmbeded="1"/>
<opf:item id="image2" href="BinData/image2.jpg" media-type="image/jpeg" isEmbeded="1"/>
<opf:item id="section0" href="Contents/section0.xml" media-type="application/xml"/>
<opf:item id="section1" href="Contents/section1.xml" media-type="application/xml"/>
</opf:manifest>
<opf:spine>
<opf:itemref idref="header" linear="yes"/>
<opf:itemref idref="section0" linear="yes"/>
<opf:itemref idref="section1" linear="yes"/>
</opf:spine>
</opf:package>"#;
let info = parse_content_hpf(xml).unwrap();
assert_eq!(info.section_files.len(), 2);
assert_eq!(info.section_files[0], "Contents/section0.xml");
assert_eq!(info.section_files[1], "Contents/section1.xml");
assert_eq!(info.section_master_page_files.len(), 2);
assert_eq!(info.bin_data_items.len(), 2);
assert_eq!(info.bin_data_items[0].href, "BinData/image1.png");
assert_eq!(info.bin_data_items[1].id, "image2");
assert!(info.bin_data_items[0].is_embedded);
assert!(info.bin_data_items[1].is_embedded);
}
#[test]
fn test_parse_content_hpf_external_image() {
let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<opf:package xmlns:opf="http://www.idpf.org/2007/opf/">
<opf:manifest>
<opf:item id="header" href="Contents/header.xml" media-type="application/xml"/>
<opf:item id="image1" href="D:\Work\images\oracle.gif" media-type="image/gif" isEmbeded="0"/>
<opf:item id="image2" href="BinData/image2.jpg" media-type="image/jpeg" isEmbeded="1"/>
<opf:item id="section0" href="Contents/section0.xml" media-type="application/xml"/>
</opf:manifest>
<opf:spine><opf:itemref idref="section0"/></opf:spine>
</opf:package>"#;
let info = parse_content_hpf(xml).unwrap();
assert_eq!(info.bin_data_items.len(), 2);
assert_eq!(info.bin_data_items[0].href, "D:\\Work\\images\\oracle.gif");
assert!(!info.bin_data_items[0].is_embedded);
assert_eq!(info.bin_data_items[1].href, "BinData/image2.jpg");
assert!(info.bin_data_items[1].is_embedded);
}
#[test]
fn test_parse_empty_content() {
let xml = r#"<?xml version="1.0"?><opf:package xmlns:opf="http://www.idpf.org/2007/opf/"><opf:manifest/><opf:spine/></opf:package>"#;
let info = parse_content_hpf(xml).unwrap();
assert!(info.section_files.is_empty());
assert!(info.bin_data_items.is_empty());
}
#[test]
fn test_parse_content_hpf_master_pages_by_manifest_order() {
let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<opf:package xmlns:opf="http://www.idpf.org/2007/opf/">
<opf:manifest>
<opf:item id="masterpage0" href="Contents/masterpage0.xml" media-type="application/xml"/>
<opf:item id="masterpage1" href="Contents/masterpage1.xml" media-type="application/xml"/>
<opf:item id="section0" href="Contents/section0.xml" media-type="application/xml"/>
<opf:item id="masterpage2" href="Contents/masterpage2.xml" media-type="application/xml"/>
<opf:item id="section1" href="Contents/section1.xml" media-type="application/xml"/>
</opf:manifest>
<opf:spine>
<opf:itemref idref="section0"/>
<opf:itemref idref="section1"/>
</opf:spine>
</opf:package>"#;
let info = parse_content_hpf(xml).unwrap();
assert_eq!(info.master_page_items.len(), 3);
assert_eq!(info.master_page_items[0].id, "masterpage0");
assert_eq!(info.master_page_items[0].href, "Contents/masterpage0.xml");
assert_eq!(info.master_page_items[1].id, "masterpage1");
assert_eq!(info.master_page_items[1].href, "Contents/masterpage1.xml");
assert_eq!(info.master_page_items[2].id, "masterpage2");
assert_eq!(info.master_page_items[2].href, "Contents/masterpage2.xml");
assert_eq!(
info.section_master_page_files,
vec![
vec![
"Contents/masterpage0.xml".to_string(),
"Contents/masterpage1.xml".to_string()
],
vec!["Contents/masterpage2.xml".to_string()]
]
);
}
}