From 504f22cf2afc453dc0dd469577d643462cc9d73d Mon Sep 17 00:00:00 2001 From: Malte Bastian Date: Thu, 6 Jun 2024 15:55:13 +0200 Subject: [PATCH] feat: support fm21 --- src/main.rs | 18 +++++++++++++++++- src/utils/mod.rs | 43 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9032d6c..c790909 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,7 @@ use crate::table_catalog::xml_explode_table_catalog; use crate::table_occurrence_catalog::xml_explode_table_occurrence_catalog; use crate::theme_catalog::xml_explode_theme_catalog; use crate::utils::attributes::get_attribute; +use crate::utils::xml_utils::skip_element; use crate::value_list_catalog::xml_explode_value_list_catalog; mod base_table_catalog; @@ -113,6 +114,7 @@ fn explode_xml(fm_export_file_path: &PathBuf, out_dir_path: &Path) -> Result<(), .with_context(|| format!("Error opening file {}", fm_export_file_path.display(),))?; // Initialize variables + let mut fm_version = String::new(); let mut fm_file_name = String::new(); let mut depth = 0; let mut script_id_path_map: HashMap> = HashMap::new(); @@ -137,7 +139,8 @@ fn explode_xml(fm_export_file_path: &PathBuf, out_dir_path: &Path) -> Result<(), .unwrap() .strip_suffix(".fmp12") .unwrap() - .to_string() + .to_string(); + fm_version = get_attribute(&e, "Source").unwrap().to_string(); } _ => { bail!("Unsupported XML-format"); @@ -204,7 +207,20 @@ fn explode_xml(fm_export_file_path: &PathBuf, out_dir_path: &Path) -> Result<(), ); continue; } + b"OptionsForValueLists" => { + xml_explode_value_list_catalog( + &mut reader, + &e, + out_dir_path, + &fm_file_name, + ); + continue; + } b"ValueListCatalog" => { + if !fm_version.starts_with("18.") && !fm_version.starts_with("19.") { + skip_element(&mut reader, &e); + continue; + } xml_explode_value_list_catalog( &mut reader, &e, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 2564024..235f8c5 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -3,12 +3,14 @@ use std::fs::File; use std::io::{BufRead, BufReader, Read, Write}; use std::path::Path; -use quick_xml::events::BytesStart; -use quick_xml::Reader; +use quick_xml::events::{BytesStart, Event}; +use quick_xml::reader::Reader; use regex::Regex; use crate::utils::attributes::get_attributes; -use crate::utils::xml_utils::element_to_string; +use crate::utils::xml_utils::{ + element_to_string, end_element_to_string, start_element_to_string, text_element_to_string, +}; use crate::{escape_filename, join_scope_id_and_name, should_skip_line}; pub(crate) mod attributes; @@ -28,8 +30,7 @@ impl Entity { self.content.clear(); } - pub fn read_xml_element(&mut self, reader: &mut Reader, e: &BytesStart) { - self.clear(); + fn parse_xml_attributes(&mut self, e: &BytesStart) { for attr in get_attributes(e).unwrap() { match attr.0.as_str() { "id" => self.id = attr.1.to_string(), @@ -42,7 +43,37 @@ impl Entity { _ => {} } } - self.content = element_to_string(reader, e); + } + + pub fn read_xml_element(&mut self, reader: &mut Reader, e: &BytesStart) { + // self.clear(); + self.parse_xml_attributes(e); + + if self.id.is_empty() { + self.content = start_element_to_string(e); + let mut buf = Vec::new(); + loop { + match reader.read_event_into(&mut buf) { + Ok(Event::Start(e)) => { + self.read_xml_element(reader, &e); + } + Ok(Event::Text(e)) => { + self.content += text_element_to_string(&e, false).as_str(); + } + Ok(Event::End(e)) => { + self.content += end_element_to_string(&e).as_str(); + break; + } + Ok(Event::Eof) => break, + unknown_event => { + panic!("Wrong read event: {:?}", unknown_event); + } + }; + buf.clear(); + } + } else { + self.content += element_to_string(reader, e).as_str(); + } } }