1
###########################################################################/**
2
# @RdocClass RspString
3
#
4
# @title "The RspString class"
5
#
6
# \description{
7
#  @classhierarchy
8
#
9
#  An RspString is a @character @vector with RSP markup.
10
# }
11
#
12
# @synopsis
13
#
14
# \arguments{
15
#   \item{s}{A @character @vector.}
16
#   \item{...}{Arguments passed to @see "RspObject".}
17
# }
18
#
19
# \section{Fields and Methods}{
20
#  @allmethods
21
# }
22
#
23
# @author
24
#
25
# @keyword internal
26
#*/###########################################################################
27
setConstructorS3("RspString", function(s=character(), ...) {
28
  # Argument 's':
29 1
  s <- paste(s, collapse="\n")
30

31 1
  extend(RspObject(s, ...), "RspString")
32
})
33

34

35
setMethodS3("print", "RspString", function(x, ...) {
36 1
  s <- sprintf("%s:", class(x)[1L])
37 1
  s <- c(s, sprintf("Content type: %s", getAttribute(x, "type", NA)))
38 1
  s <- c(s, sprintf("Language: %s", getAttribute(x, "language", NA)))
39 1
  metadata <- getMetadata(x, local=FALSE)
40 1
  if (length(metadata) > 0L) {
41 0
    metadata <- unlist(metadata, use.names=TRUE)
42 0
    s <- c(s, sprintf("Metadata '%s': %s", names(metadata), metadata))
43
  } else {
44 1
    s <- c(s, "Metadata to available.")
45
  }
46 1
  s <- c(s, sprintf("Number of characters: %s", nchar(x)))
47 1
  s <- c(s, sprintf("Number of lines: %s", nbrOfLines(x)))
48 1
  ruler <- paste(rep("#", times=getOption("width")-2L), collapse="")
49 1
  s <- c(s, ruler, x)
50 1
  s <- paste(s, collapse="\n")
51 1
  cat(s, "\n", sep="")
52
}, protected=TRUE)
53

54

55
#########################################################################/**
56
# @RdocMethod nbrOfLines
57
#
58
# @title "Gets the number of lines in an RSP string"
59
#
60
# \description{
61
#  @get "title".
62
# }
63
#
64
# @synopsis
65
#
66
# \arguments{
67
#   \item{...}{Not used.}
68
# }
69
#
70
# \value{
71
#  Returns a non-negative @integer.
72
# }
73
#
74
# @author
75
#
76
# \seealso{
77
#   @seeclass
78
# }
79
#*/#########################################################################
80
setMethodS3("nbrOfLines", "RspString", function(object, ...) {
81 1
  length(unlist(strsplit(object, split="\n", fixed=TRUE), use.names=FALSE))
82
})
83

84

85

86
#########################################################################/**
87
# @RdocMethod getType
88
#
89
# @title "Gets the type of an RSP string"
90
#
91
# \description{
92
#  @get "title".
93
# }
94
#
95
# @synopsis
96
#
97
# \arguments{
98
#   \item{default}{If unknown/not set, the default content type to return.}
99
#   \item{...}{Not used.}
100
# }
101
#
102
# \value{
103
#  Returns a @character string.
104
# }
105
#
106
# @author
107
#
108
# \seealso{
109
#   @seeclass
110
# }
111
#*/#########################################################################
112
setMethodS3("getType", "RspString", function(object, default=NA, as=c("text", "IMT"), ...) {
113 0
  as <- match.arg(as)
114 0
  res <- getAttribute(object, "type", default=as.character(default))
115 0
  res <- tolower(res)
116 0
  if (as == "IMT" && !is.na(res)) {
117 0
    res <- parseInternetMediaType(res)
118
  }
119 0
  res
120
}, protected=TRUE)
121

122

123

124
#########################################################################/**
125
# @RdocMethod getSource
126
#
127
# @title "Gets the source reference of an RSP string"
128
#
129
# \description{
130
#  @get "title".
131
# }
132
#
133
# @synopsis
134
#
135
# \arguments{
136
#   \item{...}{Not used.}
137
# }
138
#
139
# \value{
140
#  Returns a @character string.
141
# }
142
#
143
# @author
144
#
145
# \seealso{
146
#   @seeclass
147
# }
148
#*/#########################################################################
149
setMethodS3("getSource", "RspString", function(object, ...) {
150 0
  getAttribute(object, "source", default=NA_character_)
151
}, protected=TRUE, createGeneric=FALSE)
152

153

154

155

156
#########################################################################/**
157
# @RdocMethod parseDocument
158
#
159
# @title "Parses an RSP string into a RSP document"
160
#
161
# \description{
162
#  @get "title".
163
# }
164
#
165
# @synopsis
166
#
167
# \arguments{
168
#   \item{...}{Additional arguments passed to the RSP parser.}
169
#   \item{envir}{The @environment where the RSP document is parsed.}
170
#   \item{parser}{An @see "RspParser".}
171
# }
172
#
173
# \value{
174
#  Returns a @see "RspDocument" (unless \code{until != "*"} in case it
175
#  returns a deparsed @see "RspString".)
176
# }
177
#
178
# @author
179
#
180
# \seealso{
181
#   @seeclass
182
# }
183
#*/#########################################################################
184
setMethodS3("parseDocument", "RspString", function(object, ..., envir=parent.frame(), parser=RspParser()) {
185
  # Argument 'parser':
186 1
  parser <- Arguments$getInstanceOf(parser, "RspParser")
187

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

Read our documentation on viewing source code .

Loading