File: RspString.R

package info (click to toggle)
r-cran-r.rsp 0.46.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,472 kB
  • sloc: javascript: 612; tcl: 304; sh: 18; makefile: 16
file content (189 lines) | stat: -rw-r--r-- 4,315 bytes parent folder | download | duplicates (2)
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
###########################################################################/**
# @RdocClass RspString
#
# @title "The RspString class"
#
# \description{
#  @classhierarchy
#
#  An RspString is a @character @vector with RSP markup.
# }
#
# @synopsis
#
# \arguments{
#   \item{s}{A @character @vector.}
#   \item{...}{Arguments passed to @see "RspObject".}
# }
#
# \section{Fields and Methods}{
#  @allmethods
# }
#
# @author
#
# @keyword internal
#*/###########################################################################
setConstructorS3("RspString", function(s=character(), ...) {
  # Argument 's':
  s <- paste(s, collapse="\n")

  extend(RspObject(s, ...), "RspString")
})


setMethodS3("print", "RspString", function(x, ...) {
  s <- sprintf("%s:", class(x)[1L])
  s <- c(s, sprintf("Content type: %s", getAttribute(x, "type", NA)))
  s <- c(s, sprintf("Language: %s", getAttribute(x, "language", NA)))
  metadata <- getMetadata(x, local=FALSE)
  if (length(metadata) > 0L) {
    metadata <- unlist(metadata, use.names=TRUE)
    s <- c(s, sprintf("Metadata '%s': %s", names(metadata), metadata))
  } else {
    s <- c(s, "Metadata to available.")
  }
  s <- c(s, sprintf("Number of characters: %s", nchar(x)))
  s <- c(s, sprintf("Number of lines: %s", nbrOfLines(x)))
  ruler <- paste(rep("#", times=getOption("width")-2L), collapse="")
  s <- c(s, ruler, x)
  s <- paste(s, collapse="\n")
  cat(s, "\n", sep="")
}, protected=TRUE)


#########################################################################/**
# @RdocMethod nbrOfLines
#
# @title "Gets the number of lines in an RSP string"
#
# \description{
#  @get "title".
# }
#
# @synopsis
#
# \arguments{
#   \item{...}{Not used.}
# }
#
# \value{
#  Returns a non-negative @integer.
# }
#
# @author
#
# \seealso{
#   @seeclass
# }
#*/#########################################################################
setMethodS3("nbrOfLines", "RspString", function(object, ...) {
  length(unlist(strsplit(object, split="\n", fixed=TRUE), use.names=FALSE))
})



#########################################################################/**
# @RdocMethod getType
#
# @title "Gets the type of an RSP string"
#
# \description{
#  @get "title".
# }
#
# @synopsis
#
# \arguments{
#   \item{default}{If unknown/not set, the default content type to return.}
#   \item{...}{Not used.}
# }
#
# \value{
#  Returns a @character string.
# }
#
# @author
#
# \seealso{
#   @seeclass
# }
#*/#########################################################################
setMethodS3("getType", "RspString", function(object, default=NA, as=c("text", "IMT"), ...) {
  as <- match.arg(as)
  res <- getAttribute(object, "type", default=as.character(default))
  res <- tolower(res)
  if (as == "IMT" && !is.na(res)) {
    res <- parseInternetMediaType(res)
  }
  res
}, protected=TRUE)



#########################################################################/**
# @RdocMethod getSource
#
# @title "Gets the source reference of an RSP string"
#
# \description{
#  @get "title".
# }
#
# @synopsis
#
# \arguments{
#   \item{...}{Not used.}
# }
#
# \value{
#  Returns a @character string.
# }
#
# @author
#
# \seealso{
#   @seeclass
# }
#*/#########################################################################
setMethodS3("getSource", "RspString", function(object, ...) {
  getAttribute(object, "source", default=NA_character_)
}, protected=TRUE, createGeneric=FALSE)




#########################################################################/**
# @RdocMethod parseDocument
#
# @title "Parses an RSP string into a RSP document"
#
# \description{
#  @get "title".
# }
#
# @synopsis
#
# \arguments{
#   \item{...}{Additional arguments passed to the RSP parser.}
#   \item{envir}{The @environment where the RSP document is parsed.}
#   \item{parser}{An @see "RspParser".}
# }
#
# \value{
#  Returns a @see "RspDocument" (unless \code{until != "*"} in case it
#  returns a deparsed @see "RspString".)
# }
#
# @author
#
# \seealso{
#   @seeclass
# }
#*/#########################################################################
setMethodS3("parseDocument", "RspString", function(object, ..., envir=parent.frame(), parser=RspParser()) {
  # Argument 'parser':
  parser <- Arguments$getInstanceOf(parser, "RspParser")

  parseDocument(parser, object, ..., envir=envir)
}, protected=TRUE)