/
GSRESTResource.R
240 lines (223 loc) · 9 KB
/
GSRESTResource.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
#' Geoserver REST API REST Resource interface
#'
#' @docType class
#' @importFrom R6 R6Class
#' @export
#' @keywords geoserver rest api
#' @return Object of \code{\link{R6Class}} for modelling a GeoServer REST resource interface
#' @format \code{\link{R6Class}} object.
#'
#' @author Emmanuel Blondel <emmanuel.blondel1@@gmail.com>
#'
GSRESTResource <- R6Class("GSRESTResource",
public = list(
#'@field rootName root name
rootName = NA,
#'@description Initializes an object of class \link{GSRESTResource}
#'@param xml object of class \link{xml_node-class}
#'@param rootName root name
initialize = function(xml, rootName){
if(missing(rootName) | is.null(rootName)){
stop("No root name specified for GSRESTResource")
}
self$rootName = rootName
},
#'@description Decodes from XML. Abstract method to be implemented by sub-classes
#'@param xml object of class \link{xml_node-class}
decode = function(xml){
stop("Unimplemented XML 'decode' method")
},
#'@description Encodes as XML
#'@return an object of class \link{xml_node-class}
encode = function(){
#Generic XML encoder
rootXML <- xml2::xml_new_root(self$rootName)
if(is(self, "GSPublishable")){
xml2::xml_attrs(rootXML) <- c(type = self$attr_type)
}
#list of fields to encode as XML
fields <- rev(names(self))
fields <- fields[!sapply(fields, function(x){
(class(self[[x]])[1] %in% c("environment", "function")) ||
(x %in% c("rootName", "full", "attr_type"))
})]
if(is(self, "GSRESTEntrySet")){
items <- self$entryset
itemNb <- length(items)
if(itemNb > 0){
for(i in 1:itemNb){
itemName <- names(items)[i]
item <- xml2::xml_new_root("entry")
xml2::xml_attrs(item) = c(key = itemName)
itemValue <- items[[itemName]]
if(is.logical(itemValue)){
itemValue <- tolower(as.character(itemValue))
}
if(is(itemValue, "GSRESTResource")){
itemValue <- itemValue$encode()
item %>% xml2::xml_add_child(itemValue)
}else{
xml2::xml_text(item) <- itemValue
}
rootXML %>% xml2::xml_add_child(item)
}
}
}else{
for(field in fields){
fieldObj <- self[[field]]
if(!is.null(fieldObj)){
if(is.logical(fieldObj)){
fieldObj <- tolower(as.character(fieldObj))
}
if(any(class(fieldObj) == "list")){
if(is(self, "GSVirtualTable")){
itemsXML <- rootXML
}else{
itemsXML <- xml2::xml_new_root(field)
}
items <- fieldObj
itemNames <- names(items)
if(length(items) > 0){
for(i in 1:length(items)){
item <- items[[i]]
itemName <- itemNames[i]
if(any(class(item) == "R6")){
itemXML <- item$encode()
itemsXML %>% xml2::xml_add_child(itemXML)
}else{
if(is.null(itemNames)){
itemXML <- xml2::xml_new_root("string")
if(!is.na(item)) xml2::xml_text(itemXML) <- as.character(item)
itemsXML %>% xml2::xml_add_child(itemXML)
}else{
itemXML <- xml2::xml_new_root(itemName)
if(!is.na(item)) xml2::xml_text(itemXML) <- as.character(item)
itemsXML %>% xml2::xml_add_child(itemXML)
}
}
}
}
if(!is(self, "GSVirtualTable")) rootXML %>% xml2::xml_add_child(itemsXML)
}else{
if(any(class(fieldObj) == "R6")){
rootXML %>% xml2::xml_add_child(fieldObj$encode())
}else{
itemXML <- xml2::xml_new_root(field)
if(!is.na(fieldObj)) xml2::xml_text(itemXML) <- as.character(fieldObj)
rootXML %>% xml2::xml_add_child(itemXML)
}
}
}
}
}
return(rootXML)
},
#'@description Provides a custom print output (as tree) of the current class
#'@param ... args
#'@param depth class nesting depth
print = function(..., depth = 1){
#list of fields to encode as XML
fields <- rev(names(self))
#fields
fields <- fields[!sapply(fields, function(x){
(class(self[[x]])[1] %in% c("environment", "function")) ||
(x %in% c("rootName", "full", "attr_type"))
})]
cat(crayon::white(paste0("<", crayon::underline(self$getClassName()), ">")))
for(field in fields){
fieldObj <- self[[field]]
print_attrs <- function(obj){
paste(
sapply(names(obj$attrs), function(attrName){
paste0( crayon::magenta(attrName,"=",sep=""), crayon::green(obj$attrs[[attrName]]))
}
),
collapse=",")
}
#user values management
shift <- "...."
if(!is.null(fieldObj)){
if(is(fieldObj, "GSRESTResource")){
attrs_str <- ""
if(length(fieldObj$attrs)>0){
attrs <- print_attrs(fieldObj)
attrs_str <- paste0(" [",attrs,"] ")
}
cat(paste0("\n", paste(rep(shift, depth), collapse=""),"|-- ", crayon::italic(field), " ", attrs_str))
fieldObj$print(depth = depth+1)
}else if(is(fieldObj, "list")){
if(is.null(names(fieldObj))){
for(item in fieldObj){
if(is(item, "GSRESTResource")){
attrs_str <- ""
if(length(item$attrs)>0){
attrs <- print_attrs(item)
attrs_str <- paste0(" [",attrs,"] ")
}
cat(paste0("\n", paste(rep(shift, depth), collapse=""),"|-- ", crayon::italic(field), " ", attrs_str))
item$print(depth = depth+1)
}else if(is(item, "matrix")){
m <- paste(apply(item, 1L, function(x){
x <- lapply(x, function(el){
if(is.na(suppressWarnings(as.numeric(el))) & !all(sapply(item,class)=="character")){
el <- paste0("\"",el,"\"")
}else{
if(!is.na(suppressWarnings(as.numeric(el)))){
el <- as.numeric(el)
}
}
return(el)
})
return(paste(x, collapse = " "))
}), collapse = " ")
cat(paste0("\n",paste(rep(shift, depth), collapse=""),"|-- ", crayon::italic(field), ": ", crayon::bgWhite(m)))
}else{
cat(paste0("\n", paste(rep(shift, depth), collapse=""),"|-- ", crayon::italic(field), ": ", crayon::bgWhite(item)))
}
}
}else{
cat(paste0("\n", paste(rep(shift, depth), collapse=""),"|-- ", crayon::italic(field)))
for(itemName in names(fieldObj)){
item = fieldObj[[itemName]]
if(is(item, "GSRESTResource")){
attrs_str <- ""
if(length(item$attrs)>0){
attrs <- print_attrs(item)
attrs_str <- paste0(" [",attrs,"] ")
}
cat(paste0("\n", paste(rep(shift, depth), collapse=""),"|-- ", crayon::italic(field), " ", attrs_str))
item$print(depth = depth+1)
}else{
cat(paste0("\n", paste(rep(shift, depth+1), collapse=""),"|-- ", crayon::italic(itemName), ": ", crayon::bgWhite(fieldObj[[itemName]])))
}
}
}
}else if (is(fieldObj,"matrix")){
m <- paste(apply(fieldObj, 1L, function(x){
x <- lapply(x, function(el){
if(is.na(suppressWarnings(as.numeric(el)))& !all(sapply(fieldObj,class)=="character")){
el <- paste0("\"",el,"\"")
}else{
if(!is.na(suppressWarnings(as.numeric(el)))){
el <- as.numeric(el)
}
}
return(el)
})
return(paste(x, collapse = " "))
}), collapse = " ")
cat(paste0("\n",paste(rep(shift, depth), collapse=""),"|-- ", crayon::italic(field), ": ", crayon::bgWhite(m)))
}else{
cat(paste0("\n",paste(rep(shift, depth), collapse=""),"|-- ", crayon::italic(field), ": ", crayon::bgWhite(fieldObj)))
}
}
}
invisible(self)
},
#'@description Get class name
#'@return an object of class \code{character}
getClassName = function(){
return(class(self)[1])
}
)
)