-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathrelationship.R
155 lines (141 loc) · 5.64 KB
/
relationship.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
#' @importFrom R6 R6Class
relationship <- R6Class(
"relationship",
public = list(
initialize = function(id = character(0), type = character(0), target = character(0)) {
private$id <- id
private$type <- type
private$target <- target
private$target_mode <- as.character( rep(NA, length(target)) )
private$ext_src <- character(length(id))
},
feed_from_xml = function(path) {
doc <- read_xml( x = path )
children <- xml_children( doc )
ns <- xml_ns( doc )
private$id <- c( private$id, sapply(children, xml_attr, attr = "Id", ns) )
private$type <- c( private$type, sapply( children, xml_attr, attr = "Type", ns) )
private$target <- c( private$target, sapply( children, xml_attr, attr = "Target", ns) )
private$target_mode <- c( private$target_mode, sapply( children, xml_attr, attr = "TargetMode", ns) )
private$ext_src <- c( private$ext_src, character(length(children)) )
self
},
write = function(path) {
if( length(private$id) )
str <- paste0("<Relationship Id=\"", private$id,
"\" Type=\"", private$type,
"\" Target=\"", htmlEscapeCopy(private$target),
ifelse(
is.na(private$target_mode),
"",
"\" TargetMode=\"External" ),
"\"/>")
else str <- character(length = 0)
str <- c("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>",
"\n<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">", str, "</Relationships>")
dir.create(dirname(path), showWarnings = FALSE, recursive = FALSE)
writeLines(str, con = path, useBytes = TRUE)
self
},
get_next_id = function(){
max(c(0, private$get_int_id() ), na.rm = TRUE ) + 1
},
get_data = function() {
if( length(private$id) ){
data <- data.frame(id = private$id,
int_id = as.integer(gsub("rId([0-9]+)", "\\1", private$id)),
type = private$type,
target = private$target,
target_mode = private$target_mode,
ext_src = private$ext_src,
stringsAsFactors = FALSE )
data[order(data$id),]
} else {
data <- data.frame(id = character(0),
int_id = integer(0),
type = character(0),
target = character(0),
target_mode = character(0),
ext_src = character(0),
stringsAsFactors = FALSE )
}
data
},
get_images_path = function() {
is_img <- basename( private$type ) %in% "image"
targets <- private$target[is_img]
names( targets ) <- private$id[is_img]
targets
},
add_img = function( src, root_target ) {
src <- setdiff(src, private$ext_src)
if( !length(src) ) return(self)
if( any( grepl(" ", basename(src) ) ) ){
stop(paste(src, collapse = ", "),
": images with blanks in their basenames are not supported, please rename the file(s).",
call. = FALSE)
}
last_id <- max( c(0, private$get_int_id() ), na.rm = TRUE )
id <- paste0("rId", seq_along(src) + last_id)
type <- rep("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
length(src))
target <- file.path(root_target, basename(src) )
private$id <- c( private$id, id )
private$type <- c( private$type, type )
private$target <- c( private$target, target )
private$target_mode <- c( private$target_mode, rep(NA, length(id) ) )
private$ext_src <- c( private$ext_src, src )
self
},
add_drawing = function( src, root_target ) {
src <- setdiff(src, private$ext_src)
if( !length(src) ) return(self)
last_id <- max( c(0, private$get_int_id()), na.rm = TRUE )
id <- paste0("rId", seq_along(src) + last_id)
type <- rep("http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing",
length(src))
target <- file.path(root_target, basename(src) )
private$id <- unlist( c( private$id, id ) )
private$type <- unlist( c( private$type, type ) )
private$target <- unlist( c( private$target, target ) )
private$target_mode <- unlist( c( private$target_mode, rep(NA, length(id) ) ) )
private$ext_src <- unlist( c( private$ext_src, rep(NA, length(id) ) ) )
self
},
add = function(id, type, target, target_mode = NA ) {
Encoding(target) <- "UTF-8"
if( !target %in% private$target ){
private$id <- c( private$id, id )
private$type <- c( private$type, type )
private$target <- c( private$target, target )
private$target_mode <- c( private$target_mode, target_mode )
private$ext_src <- c( private$ext_src, "" )
}
self
},
remove = function( target ) {
id <- which( basename(private$target) %in% basename(target) )
private$id <- private$id[-id]
private$type <- private$type[-id]
private$target <- private$target[-id]
private$target_mode <- private$target_mode[-id]
private$ext_src <- private$ext_src[-id]
self
},
show = function() {
print(self$get_data())
}
),
private = list(
id = NA,
type = NA,
target = NA,
target_mode = NA,
ext_src = NA,
get_int_id = function(){
if( length(private$id) > 0 )
as.integer(gsub("rId([0-9]+)", "\\1", private$id))
else integer(0)
}
)
)