From e810683e092989877d735765eb79026ce91b992f Mon Sep 17 00:00:00 2001 From: aheinri5 <Anna@netzkritzler.de> Date: Fri, 25 Dec 2020 21:10:41 +0100 Subject: [PATCH] [base] Changed return of analyse_xml for improved class generation --- R/analyse_xml.R | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/R/analyse_xml.R b/R/analyse_xml.R index c110d85..32d30a6 100644 --- a/R/analyse_xml.R +++ b/R/analyse_xml.R @@ -13,19 +13,19 @@ #' summary of its findings at the end. #'@param path string: A path #'@param pattern string: A regex pattern -#'@param element_name string: The name of the XML element to look for +#'@param tag_name string: The name of the XML element to look for #'@param xpath_prefix string: Optional: The XPath prefix to use #' (defaults to "//") #'@param print_findings Optional: Should the results be printed to the console? analyse_xml <- function(path, pattern, - element_name, + tag_name, xpath_prefix = "//", print_findings = TRUE) { - assertthat::assert_that(assertthat::is.string(path)) + path <- validate_is_dir_path(path) assertthat::assert_that(assertthat::is.string(pattern)) - assertthat::assert_that(assertthat::is.string(element_name)) + assertthat::assert_that(assertthat::is.string(tag_name)) assertthat::assert_that(assertthat::is.string(xpath_prefix)) xml_files <- list.files(path = path, pattern = pattern, recursive = TRUE) @@ -79,7 +79,7 @@ analyse_xml <- function(path, valid_files_count <- valid_files_count + 1 valid_files_names <- c(valid_files_names, basename(xml_files[[i]])) - xpath_exp <- paste0(xpath_prefix, element_name) + xpath_exp <- paste0(xpath_prefix, tag_name) doc_matches <- xml2::xml_find_all(xml_doc, xpath_exp) total_matches <- total_matches + length(doc_matches) @@ -185,7 +185,7 @@ analyse_xml <- function(path, invalid_files_count, invalid_files_names, valid_files_count, - element_name, + tag_name, element_found_files_names, total_matches, attr_ex_counts, @@ -194,12 +194,15 @@ analyse_xml <- function(path, child_df) } + both_flags <- sort(c(attr_flags, child_flags), decreasing = TRUE) + #Return attributes and children (if found) return(invisible( list( - tag_name = element_name, + tag_name = tag_name, children = child_flags, - attributes = attr_flags + attributes = attr_flags, + both_sorted = both_flags ) )) } @@ -211,7 +214,7 @@ analyse_xml <- function(path, print_analysis_findings <- function(invalid_files_count, invalid_files_names, valid_files_count, - element_name, + tag_name, element_found_files_names, total_matches, attr_ex_counts, @@ -232,7 +235,7 @@ print_analysis_findings <- function(invalid_files_count, if(length(element_found_files_names) > 0){ cat("\nI found at least one element named ", - element_name, " in the following file(s):\n", sep = "") + tag_name, " in the following file(s):\n", sep = "") for(i in seq_len(length(element_found_files_names))){ cat(element_found_files_names[[i]], "\n") @@ -240,7 +243,7 @@ print_analysis_findings <- function(invalid_files_count, } cat("\nIn total, I found ", total_matches, - " element(s) named ", element_name, ".\n", sep = "") + " element(s) named ", tag_name, ".\n", sep = "") if(length(attr_ex_counts) > 0) { cat("\nThese are the attributes I found:\n") -- GitLab