-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathxml_modify.R
338 lines (287 loc) · 9.92 KB
/
xml_modify.R
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#' Modify a tree by inserting, replacing or removing nodes
#'
#' `xml_add_sibling()` and `xml_add_child()` are used to insert a node
#' as a sibling or a child. `xml_add_parent()` adds a new parent in
#' between the input node and the current parent. `xml_replace()`
#' replaces an existing node with a new node. `xml_remove()` removes a
#' node from the tree.
#'
#' @details Care needs to be taken when using `xml_remove()`,
#' @param .x a document, node or nodeset.
#' @param .copy whether to copy the `.value` before replacing. If this is `FALSE`
#' then the node will be moved from it's current location.
#' @param .where to add the new node, for `xml_add_child` the position
#' after which to add, use `0` for the first child. For
#' `xml_add_sibling` either \sQuote{"before"} or \sQuote{"after"}
#' indicating if the new node should be before or after `.x`.
#' @param ... If named attributes or namespaces to set on the node, if unnamed
#' text to assign to the node.
#' @param .value node to insert.
#' @param free When removing the node also free the memory used for that node.
#' Note if you use this option you cannot use any existing objects pointing to
#' the node or its children, it is likely to crash R or return garbage.
#' @export
xml_replace <- function(.x, .value, ..., .copy = TRUE) {
UseMethod("xml_replace")
}
#' @export
xml_replace.xml_node <- function(.x, .value, ..., .copy = TRUE) {
node <- create_node(.value, .parent = .x, .copy = .copy, ...)
.x$node <- .Call(node_replace, .x$node, node$node)
node
}
#' @export
xml_replace.xml_nodeset <- function(.x, .value, ..., .copy = TRUE) {
if (length(.x) == 0) {
return(.x)
}
# Need to wrap this in a list if a bare xml_node so it is recycled properly
if (inherits(.value, "xml_node")) {
.value <- list(.value)
}
Map(xml_replace, .x, .value, ..., .copy = .copy)
}
#' @export
xml_replace.xml_missing <- function(.x, .value, ..., .copy = TRUE) {
.x
}
#' @rdname xml_replace
#' @export
xml_add_sibling <- function(.x, .value, ..., .where = c("after", "before"), .copy = TRUE) {
UseMethod("xml_add_sibling")
}
#' @export
xml_add_sibling.xml_node <- function(.x, .value, ..., .where = c("after", "before"), .copy = inherits(.value, "xml_node")) {
.where <- match.arg(.where)
node <- create_node(.value, .parent = .x, .copy = .copy, ...)
.x$node <- switch(.where,
before = .Call(node_prepend_sibling, .x$node, node$node),
after = .Call(node_append_sibling, .x$node, node$node)
)
invisible(.x)
}
#' @export
xml_add_sibling.xml_nodeset <- function(.x, .value, ..., .where = c("after", "before"), .copy = TRUE) {
if (length(.x) == 0) {
return(.x)
}
.where <- match.arg(.where)
# Need to wrap this in a list if a bare xml_node so it is recycled properly
if (inherits(.value, "xml_node")) {
.value <- list(.value)
}
invisible(Map(xml_add_sibling, rev(.x), rev(.value), ..., .where = .where, .copy = .copy))
}
#' @export
xml_add_sibling.xml_missing <- function(.x, .value, ..., .where = c("after", "before"), .copy = TRUE) {
.x
}
# Helper function used in the xml_add* methods
create_node <- function(.value, ..., .parent, .copy) {
if (inherits(.value, "xml_node")) {
if (isTRUE(.copy)) {
.value$node <- .Call(node_copy, .value$node)
}
return(.value)
}
if (inherits(.value, "xml_cdata")) {
return(xml_node(.Call(node_cdata_new, .parent$doc, .value), doc = .parent$doc))
}
if (inherits(.value, "xml_comment")) {
return(xml_node(.Call(node_comment_new, .value), doc = .parent$doc))
}
if (inherits(.value, "xml_dtd")) {
.Call(node_new_dtd, .parent$doc, .value$name, .value$external_id, .value$system_id)
return()
}
check_character(.value)
parts <- strsplit(.value, ":")[[1]]
if (length(parts) == 2 && !is.null(.parent$node)) {
namespace <- .Call(ns_lookup, .parent$doc, .parent$node, parts[[1]])
node <- list(node = .Call(node_new_ns, parts[[2]], namespace), doc = .parent$doc)
} else {
node <- list(node = .Call(node_new, .value), doc = .parent$doc)
}
class(node) <- "xml_node"
args <- list(...)
named <- has_names(args)
xml_attrs(node) <- args[named]
xml_text(node) <- paste(args[!named], collapse = "")
node
}
#' @rdname xml_replace
#' @export
xml_add_child <- function(.x, .value, ..., .where = length(xml_children(.x)), .copy = TRUE) {
UseMethod("xml_add_child")
}
#' @export
xml_add_child.xml_node <- function(.x, .value, ..., .where = length(xml_children(.x)), .copy = inherits(.value, "xml_node")) {
node <- create_node(.value, .parent = .x, .copy = .copy, ...)
if (.where == 0L) {
if (.Call(node_has_children, .x$node, TRUE)) {
.Call(node_prepend_child, .x$node, node$node)
} else {
.Call(node_append_child, .x$node, node$node)
}
} else {
num_children <- length(xml_children(.x))
if (.where >= num_children) {
.Call(node_append_child, .x$node, node$node)
} else {
.Call(node_append_sibling, xml_child(.x, search = .where)$node, node$node)
}
}
invisible(node)
}
#' @export
xml_add_child.xml_document <- function(.x, .value, ..., .where = length(xml_children(.x)), .copy = inherits(.value, "xml_node")) {
if (inherits(.x, "xml_node")) {
NextMethod("xml_add_child")
} else {
node <- create_node(.value, .parent = .x, .copy = .copy, ...)
if (!is.null(node)) {
if (!.Call(doc_has_root, .x$doc)) {
.Call(doc_set_root, .x$doc, node$node)
}
.Call(node_append_child, .Call(doc_root, .x$doc), node$node)
}
invisible(xml_document(.x$doc))
}
}
#' @export
xml_add_child.xml_nodeset <- function(.x, .value, ..., .where = length(xml_children(.x)), .copy = TRUE) {
if (length(.x) == 0) {
return(.x)
}
# Need to wrap this in a list if a bare xml_node so it is recycled properly
if (inherits(.value, "xml_node")) {
.value <- list(.value)
}
res <- Map(xml_add_child, .x, .value, ..., .where = .where, .copy = .copy)
invisible(make_nodeset(res, res[[1]]$doc))
}
#' @export
xml_add_child.xml_missing <- function(.x, .value, ..., .copy = TRUE) {
.x
}
#' @rdname xml_replace
#' @export
xml_add_parent <- function(.x, .value, ...) {
UseMethod("xml_add_parent")
}
#' @export
xml_add_parent.xml_node <- function(.x, .value, ...) {
new_parent <- xml_replace(.x, .value = .value, ..., .copy = FALSE)
node <- xml_add_child(new_parent, .value = .x, .copy = FALSE)
invisible(node)
}
#' @export
xml_add_parent.xml_nodeset <- function(.x, .value, ...) {
if (length(.x) == 0) {
return(.x)
}
# Need to wrap this in a list if a bare xml_node so it is recycled properly
if (inherits(.value, "xml_node")) {
.value <- list(.value)
}
res <- Map(xml_add_parent, .x, .value, ...)
invisible(make_nodeset(res, res[[1]]$doc))
}
#' @export
xml_add_parent.xml_missing <- function(.x, .value, ..., .copy = TRUE) {
invisible(.x)
}
#' @rdname xml_replace
#' @export
xml_remove <- function(.x, free = FALSE) {
UseMethod("xml_remove")
}
#' @export
xml_remove.xml_node <- function(.x, free = FALSE) {
.Call(node_remove, .x$node, free)
invisible(.x)
}
#' @export
xml_remove.xml_nodeset <- function(.x, free = FALSE) {
if (length(.x) == 0) {
return(invisible(.x))
}
invisible(Map(xml_remove, rev(.x), free = free))
}
#' @export
xml_remove.xml_missing <- function(.x, free = FALSE) {
invisible(.x)
}
#' Set the node's namespace
#'
#' The namespace to be set must be already defined in one of the node's
#' ancestors.
#' @param .x a node
#' @param prefix The namespace prefix to use
#' @param uri The namespace URI to use
#' @return the node (invisibly)
#' @export
xml_set_namespace <- function(.x, prefix = "", uri = "") {
stopifnot(inherits(.x, "xml_node"))
if (nzchar(uri)) {
.Call(node_set_namespace_uri, .x$doc, .x$node, uri)
} else {
.Call(node_set_namespace_prefix, .x$doc, .x$node, prefix)
}
invisible(.x)
}
#' Create a new document, possibly with a root node
#'
#' `xml_new_document` creates only a new document without a root node. In
#' most cases you should instead use `xml_new_root`, which creates a new
#' document and assigns the root node in one step.
#' @param version The version number of the document.
#' @param encoding The character encoding to use in the document. The default
#' encoding is \sQuote{UTF-8}. Available encodings are specified at
#' <http://xmlsoft.org/html/libxml-encoding.html#xmlCharEncoding>.
#' @return A `xml_document` object.
#' @export
# TODO: jimhester 2016-12-16 Deprecate this in the future?
xml_new_document <- function(version = "1.0", encoding = "UTF-8") {
doc <- .Call(doc_new, version, encoding)
out <- list(doc = doc)
class(out) <- "xml_document"
out
}
#' @param .version The version number of the document, passed to `xml_new_document(version)`.
#' @param .encoding The encoding of the document, passed to `xml_new_document(encoding)`.
#' @inheritParams xml_add_child
#' @rdname xml_new_document
#' @export
xml_new_root <- function(.value, ..., .copy = inherits(.value, "xml_node"), .version = "1.0", .encoding = "UTF-8") {
xml_add_child(xml_new_document(version = .version, encoding = .encoding), .value = .value, ... = ..., .copy = .copy)
}
#' Strip the default namespaces from a document
#'
#' @inheritParams xml_name
#' @examples
#' x <- read_xml(
#' "<foo xmlns = 'http://foo.com'>
#' <baz/>
#' <bar xmlns = 'http://bar.com'>
#' <baz/>
#' </bar>
#' </foo>"
#' )
#' # Need to specify the default namespaces to find the baz nodes
#' xml_find_all(x, "//d1:baz")
#' xml_find_all(x, "//d2:baz")
#'
#' # After stripping the default namespaces you can find both baz nodes directly
#' xml_ns_strip(x)
#' xml_find_all(x, "//baz")
#' @export
xml_ns_strip <- function(x) {
# //namespace::*[name()=''] finds all the namespace definition nodes with no
# prefix (default namespaces).
# What we actually want is the element node the definitions are contained in
# so return the parent (/parent::*)
namespace_element_nodes <- xml_find_all(x, "//namespace::*[name()='']/parent::*")
xml_attr(namespace_element_nodes, "xmlns") <- NULL
invisible(x)
}