Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
default_xml_version <- "1.0"
default_xml_encoding <- "ISO-8859-1"
#============================== GENERAL UTILITY (copy playground) ================================
###Stub
# Stub <- R6::R6Class("Stub",
# public = list(
#
# initialize = function(argument) {
# #stopifnot(is.character(name), length(name) == 1)
# #...
# }
# ),
#
# active = list(
#
# ),
#
# private = list(
#
# )
# )
#============================== XML UTILITY FUNCTIONS ================================
#' Reads the encoding of an XML file
#'
#' @param input_file_name An XML (.xml or, in the case of OpenGeoSys, .prj) file
#' @return The encoding of the input file
# @examples
# input_file_encoding <- get_xml_encoding("file.xml")
# input_file_encoding <- get_xml_encoding("file.prj")
get_xml_encoding <- function(input_file_name) {
input_file <- file(input_file_name, "r")
first_line <- readLines(input_file, n=1)
close(input_file)
decl <- stringr::str_split(first_line, '"', simplify = TRUE)
xml_encoding <- default_xml_encoding
if(length(decl) == 5 && decl[1] == "<?xml version=" && decl[3] == " encoding=" && decl[5]=="?>"){
xml_encoding <- decl[4]
cat(paste("Valid XML declaration in first line of input file found.\nDetected encoding ", xml_encoding))
}else{
cat("XML declaration not as expected. Example for valid declaration:\n")
cat('<?xml version="1.0" encoding="UTF-8"?>\n')
cat(paste("I will use my default value of XML encoding ", default_xml_encoding, ".\n"))
}
return(xml_encoding)
}
#' Creates a template project file based on the XML structure and attributes of an existing project file.
#' @param existing_prj An existing .prj file to use as a template for the structure and attributes
#' @param template_prj The path where the template should be saved
#' @return The newly created template project file
# @examples
# blank_prj_file <- create_template_prj("file.prj")
create_template_prj <- function(existing_prj, template_prj) {
xml_input <- xml2::read_xml(existing_prj)
#Empty text nodes
text_nodes<-xml2::xml_find_all(xml_input, "//text()")
xml2::xml_set_text(text_nodes, "")
xml2::write_xml(xml_input, template_prj, options = "format", encoding=get_xml_encoding(existing_prj))
}