From 99a484a5bd4b7d83d7c4ca60465463b30b5931d1 Mon Sep 17 00:00:00 2001 From: Jaby Date: Fri, 4 Nov 2022 15:10:26 +0100 Subject: [PATCH] Parse XML --- src/Tools/Tests/ISO_Planschbecken.xml | 2 +- .../psxcdgen_ex/src/config_reader/mod.rs | 50 ++++++++++++- .../psxcdgen_ex/src/config_reader/xml.rs | 71 +++++++++++++++++-- src/Tools/psxcdgen_ex/src/main.rs | 6 +- 4 files changed, 121 insertions(+), 8 deletions(-) diff --git a/src/Tools/Tests/ISO_Planschbecken.xml b/src/Tools/Tests/ISO_Planschbecken.xml index 30e9bfeb..8da7a166 100644 --- a/src/Tools/Tests/ISO_Planschbecken.xml +++ b/src/Tools/Tests/ISO_Planschbecken.xml @@ -3,7 +3,7 @@ Jaby Wuff C:/../ - + C:/../ C:/../ diff --git a/src/Tools/psxcdgen_ex/src/config_reader/mod.rs b/src/Tools/psxcdgen_ex/src/config_reader/mod.rs index b84ee1e3..e322a240 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/mod.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/mod.rs @@ -1,6 +1,54 @@ use super::{Error, ErrorString}; +use std::path::PathBuf; mod xml; -pub fn parse_xml(xml: String) -> Result<(), Error> { +pub struct Configuration { + pub publisher: String, + pub license_path: PathBuf, + pub root: Directory, +} + +impl Configuration { + pub fn new() -> Configuration { + Configuration{publisher: String::new(), license_path: PathBuf::new(), root: Directory::new("root")} + } +} + +pub struct File { + pub name: String, + pub path: PathBuf, +} + +impl File { + pub fn new() -> File { + File{name: String::new(), path: PathBuf::new()} + } +} + +pub struct Directory { + pub name: String, + member: Vec, +} + +impl Directory { + pub fn new(name: &str) -> Directory { + Directory{name: String::from(name), member: Vec::new()} + } + + pub fn add_file(&mut self, file: File) { + self.member.push(DirMember::File(file)); + } + + pub fn add_dir(&mut self, dir: Directory) { + self.member.push(DirMember::Directory(dir)); + } +} + +enum DirMember { + File(File), + Directory(Directory), +} + +pub fn parse_xml(xml: String) -> Result { Ok(xml::parse(xml)?) } \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs index 7d53cea5..04b9b1d4 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs @@ -1,9 +1,72 @@ -use super::ErrorString; +use std::path::PathBuf; +use crate::config_reader::Directory; -pub fn parse(xml: String) -> Result<(), Error> { - roxmltree::Document::parse(xml.as_str())?; +use super::{Configuration, ErrorString, File}; - Ok(()) +pub fn parse(xml: String) -> Result { + let mut config = Configuration::new(); + let parser = roxmltree::Document::parse(xml.as_str())?; + let children = parser.root().children(); + + for node in children { + if node.is_element() && node.tag_name().name() == "ISO_Project" { + parse_iso_project(node, &mut config); + } + } + + Ok(config) +} + +fn parse_iso_project(iso_project: roxmltree::Node, config: &mut Configuration) { + for node in iso_project.children() { + if node.is_element() { + match node.tag_name().name() { + "Description" => parse_description(node, config), + "Track" => parse_track(node, config), + _ => () + } + } + } +} + +fn parse_description(description: roxmltree::Node, config: &mut Configuration) { + for node in description.descendants() { + if node.is_element() { + match node.tag_name().name() { + "Publisher" => config.publisher = String::from(node.text().unwrap_or_default()), + "License" => config.license_path = PathBuf::from(node.text().unwrap_or_default()), + _ => () + } + } + } +} + +fn parse_track(track: roxmltree::Node, config: &mut Configuration) { + fn parse_file(file: roxmltree::Node) -> File { + File{ + name: String::from(file.attribute("name").unwrap_or_default()), + path: PathBuf::from(file.text().unwrap_or_default()) + } + } + + fn parse_file_system(cur_node: roxmltree::Node, root: &mut Directory) { + for node in cur_node.children() { + if node.is_element() { + match node.tag_name().name() { + "File" => root.add_file(parse_file(node)), + "Directory" => { + let mut new_dir = Directory::new(node.attribute("name").unwrap_or_default()); + + parse_file_system(node, &mut new_dir); + root.add_dir(new_dir); + }, + _ => (), + } + } + } + } + + parse_file_system(track, &mut config.root); } pub struct Error(roxmltree::Error); diff --git a/src/Tools/psxcdgen_ex/src/main.rs b/src/Tools/psxcdgen_ex/src/main.rs index 489de4db..eecb1186 100644 --- a/src/Tools/psxcdgen_ex/src/main.rs +++ b/src/Tools/psxcdgen_ex/src/main.rs @@ -36,7 +36,7 @@ fn populate() -> Result { Ok(desc) } -fn run_main() -> Result<(), Error> { +fn _run_main() -> Result<(), Error> { let desc = populate()?; println!("\n<== Planschbecken ==>"); @@ -59,7 +59,9 @@ fn run_main() -> Result<(), Error> { } fn run_main_xml() -> Result<(), Error> { - config_reader::parse_xml(std::fs::read_to_string("../Tests/ISO_Planschbecken.xml")?) + let _ = config_reader::parse_xml(std::fs::read_to_string("../Tests/ISO_Planschbecken.xml")?); + + Ok(()) } fn main() {