Skip to content

Commit

Permalink
feat: support fm21
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-m committed Jun 6, 2024
1 parent 1a429c0 commit 504f22c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Vec<String>> = HashMap::new();
Expand All @@ -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");
Expand Down Expand Up @@ -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,
Expand Down
43 changes: 37 additions & 6 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,8 +30,7 @@ impl Entity {
self.content.clear();
}

pub fn read_xml_element<R: Read + BufRead>(&mut self, reader: &mut Reader<R>, 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(),
Expand All @@ -42,7 +43,37 @@ impl Entity {
_ => {}
}
}
self.content = element_to_string(reader, e);
}

pub fn read_xml_element<R: Read + BufRead>(&mut self, reader: &mut Reader<R>, 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();
}
}
}

Expand Down

0 comments on commit 504f22c

Please sign in to comment.