use std::{collections::HashSet, io::Cursor};
use crate::xmlwriter::{
xml_data_element, xml_declaration, xml_empty_tag, xml_end_tag, xml_start_tag,
};
pub struct FeaturePropertyBag {
pub(crate) writer: Cursor<Vec<u8>>,
pub(crate) feature_property_bags: HashSet<FeaturePropertyBagTypes>,
}
impl FeaturePropertyBag {
pub(crate) fn new() -> FeaturePropertyBag {
let writer = Cursor::new(Vec::with_capacity(2048));
FeaturePropertyBag {
writer,
feature_property_bags: HashSet::new(),
}
}
pub(crate) fn assemble_xml_file(&mut self) {
xml_declaration(&mut self.writer);
self.write_feature_property_bags();
self.write_checkbox_bag();
self.write_xf_controls_bag();
self.write_xf_compliment_bag();
self.write_xf_compliments_bag();
if self
.feature_property_bags
.contains(&FeaturePropertyBagTypes::DXFComplements)
{
self.write_dxf_compliments_bag();
}
xml_end_tag(&mut self.writer, "FeaturePropertyBags");
}
fn write_feature_property_bags(&mut self) {
let attributes = [(
"xmlns",
"http://schemas.microsoft.com/office/spreadsheetml/2022/featurepropertybag",
)];
xml_start_tag(&mut self.writer, "FeaturePropertyBags", &attributes);
}
fn write_checkbox_bag(&mut self) {
let attributes = [("type", "Checkbox")];
xml_empty_tag(&mut self.writer, "bag", &attributes);
}
fn write_xf_controls_bag(&mut self) {
let attributes = [("type", "XFControls")];
xml_start_tag(&mut self.writer, "bag", &attributes);
self.write_bag_id("CellControl", "0");
xml_end_tag(&mut self.writer, "bag");
}
fn write_xf_compliment_bag(&mut self) {
let attributes = [("type", "XFComplement")];
xml_start_tag(&mut self.writer, "bag", &attributes);
self.write_bag_id("XFControls", "1");
xml_end_tag(&mut self.writer, "bag");
}
fn write_xf_compliments_bag(&mut self) {
let attributes = [
("type", "XFComplements"),
("extRef", "XFComplementsMapperExtRef"),
];
xml_start_tag(&mut self.writer, "bag", &attributes);
xml_start_tag(&mut self.writer, "a", &[("k", "MappedFeaturePropertyBags")]);
self.write_bag_id("", "2");
xml_end_tag(&mut self.writer, "a");
xml_end_tag(&mut self.writer, "bag");
}
fn write_dxf_compliments_bag(&mut self) {
let attributes = [
("type", "DXFComplements"),
("extRef", "DXFComplementsMapperExtRef"),
];
xml_start_tag(&mut self.writer, "bag", &attributes);
xml_start_tag(&mut self.writer, "a", &[("k", "MappedFeaturePropertyBags")]);
self.write_bag_id("", "2");
xml_end_tag(&mut self.writer, "a");
xml_end_tag(&mut self.writer, "bag");
}
fn write_bag_id(&mut self, key: &str, id: &str) {
let mut attributes = vec![];
if !key.is_empty() {
attributes.push(("k", key.to_string()));
}
xml_data_element(&mut self.writer, "bagId", id, &attributes);
}
}
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub(crate) enum FeaturePropertyBagTypes {
XFComplements,
DXFComplements,
}