@@ -144,3 +144,99 @@ drop_templatenode_from_app <- function(package_dir) {
144144 write_xml(doc , file )
145145 }
146146}
147+
148+
149+ # app.xml properties ----
150+
151+ read_app_properties <- function (package_dir ) {
152+ file <- file.path(package_dir , " docProps" , " app.xml" )
153+ if (! file.exists(file )) {
154+ return (NULL )
155+ }
156+ doc <- read_xml(file )
157+ ns <- xml_ns(doc )
158+
159+ hyperlink_base_node <- xml_find_first(doc , " //d1:HyperlinkBase" , ns = ns )
160+ hyperlink_base <- if (inherits(hyperlink_base_node , " xml_missing" )) {
161+ NA_character_
162+ } else {
163+ xml_text(hyperlink_base_node )
164+ }
165+
166+ company_node <- xml_find_first(doc , " //d1:Company" , ns = ns )
167+ company <- if (inherits(company_node , " xml_missing" )) {
168+ NA_character_
169+ } else {
170+ xml_text(company_node )
171+ }
172+
173+ z <- list (
174+ data = data.frame (
175+ name = c(" HyperlinkBase" , " Company" ),
176+ value = c(hyperlink_base , company ),
177+ stringsAsFactors = FALSE
178+ ),
179+ file = file
180+ )
181+ class(z ) <- " app_properties"
182+ z
183+ }
184+
185+
186+ # ' @export
187+ `[<-.app_properties` <- function (x , i , j , value ) {
188+ if (! i %in% x $ data $ name ) {
189+ new_row <- data.frame (name = i , value = value , stringsAsFactors = FALSE )
190+ x $ data <- rbind(x $ data , new_row )
191+ } else {
192+ x $ data [x $ data $ name == i , j ] <- value
193+ }
194+ x
195+ }
196+
197+
198+ # ' @export
199+ `[.app_properties` <- function (x , i , j ) {
200+ x $ data [x $ data $ name == i , j ]
201+ }
202+
203+ # ' @importFrom xml2 xml_set_text
204+ write_app_properties <- function (app_props , package_dir ) {
205+ if (is.null(app_props )) {
206+ return (invisible ())
207+ }
208+
209+ file <- file.path(package_dir , " docProps" , " app.xml" )
210+ if (! file.exists(file )) {
211+ return (invisible ())
212+ }
213+
214+ doc <- read_xml(file )
215+ ns <- xml_ns(doc )
216+
217+ for (i in seq_len(nrow(app_props $ data ))) {
218+ prop_name <- app_props $ data $ name [i ]
219+ prop_value <- app_props $ data $ value [i ]
220+
221+ if (is.na(prop_value ) || prop_value == " " ) {
222+ next
223+ }
224+
225+ xpath <- paste0(" //d1:" , prop_name )
226+ node <- xml_find_first(doc , xpath , ns = ns )
227+ if (inherits(node , " xml_missing" )) {
228+ new_node <- read_xml(sprintf(
229+ " <%s>%s</%s>" ,
230+ prop_name ,
231+ htmlEscapeCopy(prop_value ),
232+ prop_name
233+ ))
234+ xml_add_child(doc , new_node )
235+ } else {
236+ xml_set_text(node , prop_value )
237+ }
238+ }
239+
240+ write_xml(doc , file )
241+ invisible ()
242+ }
0 commit comments