diff --git a/R/read_in_utils.R b/R/read_in_utils.R index b4a62fe5121800c7e6f3f299f2346079cf4ab7db..d50db7bb7772caf53e37a8031a88fe6308037a02 100644 --- a/R/read_in_utils.R +++ b/R/read_in_utils.R @@ -170,24 +170,34 @@ node_to_object <- function(xml_node, node_name <- xml2::xml_name(xml_node) + # joboog: I think this if statements should be combined to if-else + # statements to be more explicit and to catch errors easier. #Node is leaf if(length(xml2::xml_children(xml_node)) == 0){ + xml_attrs <- xml2::xml_attrs(xml_node) + xml_text <- xml2::xml_text(xml_node) + xml_text_clean <- - stringr::str_remove_all(xml2::xml_text(xml_node), - "[\n|[:space:]]") + stringr::str_remove_all(xml_text, "[\n|[:space:]]") - if(xml_text_clean != "" && - length(xml2::xml_attrs(xml_node)) != 0){ - return(invisible(c(xml2::xml_attrs(xml_node), - xml_text = xml2::xml_text(xml_node)))) + if(xml_text_clean != "" && length(xml_attrs) != 0){ + return(invisible(c(xml_attrs, xml_text = xml_text))) } - - if(xml_text_clean != ""){ - return(invisible(xml2::xml_text(xml_node))) + else if(xml_text_clean != ""){ + return(invisible(xml_text)) + } + else if(length(xml_attrs) != 0){ + return(invisible(xml_attrs)) + } + else if(xml_text_clean == "" && length(xml_attrs) == 0){ + warning(paste(c("Tag '", xpath, "' was found empty."))) + return(NULL) + } + else{ + stop(paste0("Unusual case for importing tag: '", xpath, "'."), + call. = F) } - - return(invisible(xml2::xml_attrs(xml_node))) } #Node is represented by subclass diff --git a/inst/extdata/benchmarks/Elliptic/circle_radius_1/circle_1e1_axi.prj b/inst/extdata/benchmarks/Elliptic/circle_radius_1/circle_1e1_axi.prj index d4e89a5f689d3c97d8951a0c2ef8b72475b1a280..2cd04dc7fe09453ea74c88ef412e3d13013855c5 100644 --- a/inst/extdata/benchmarks/Elliptic/circle_radius_1/circle_1e1_axi.prj +++ b/inst/extdata/benchmarks/Elliptic/circle_radius_1/circle_1e1_axi.prj @@ -7,7 +7,6 @@ </processes> <media> <medium id="0"> - <phases/> <properties> <property> <name>diffusion</name> diff --git a/inst/extdata/benchmarks/square_1x1_SteadyStateDiffusion_Python/square_1e3_laplace_eq.prj b/inst/extdata/benchmarks/square_1x1_SteadyStateDiffusion_Python/square_1e3_laplace_eq.prj index 44af39f05769c662c9ad99544dd5c870fa7b377f..28afceaebed568737ad8d021251323ddde3b4c3e 100644 --- a/inst/extdata/benchmarks/square_1x1_SteadyStateDiffusion_Python/square_1e3_laplace_eq.prj +++ b/inst/extdata/benchmarks/square_1x1_SteadyStateDiffusion_Python/square_1e3_laplace_eq.prj @@ -5,7 +5,7 @@ <python_script>bcs_laplace_eq.py</python_script> <media> <medium id="0"> - <phases/> + <!--phases/--> <properties> <property> <name>diffusion</name> diff --git a/tests/testthat/test-read_in_utils.R b/tests/testthat/test-read_in_utils.R index 8fe34b17cf3c992fe951533bb4bb9f85e76ca176..8b2e143a6b1e8a0978a081f36b4bdc94a9829eca 100644 --- a/tests/testthat/test-read_in_utils.R +++ b/tests/testthat/test-read_in_utils.R @@ -45,6 +45,12 @@ test_that("node_to_object works for simple lists", { expect_equal(my_list, list(b = "1", b = "2")) }) +test_that("node_to_object prints warning for empty tags", { + + test_node <- xml2::read_xml("<test> </test>") + + expect_warning(node_to_object(test_node, xpath = "/test")) +}) test_that("order_parameters works for classes with Ellipsis argument", {