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
|
xmlErrorCumulator =
function(class = "XMLParserErrorList", immediate = TRUE)
{
messages = character()
function(msg, ...) {
# curently discards all the extra information.
if(length(grep("\\\n$", msg)) == 0)
paste(msg, "\n", sep = "")
if(immediate)
cat(msg)
if(length(msg) == 0) {
# collapse into string. Probably want to leave as separate elements of a character vector.
# Make into real objects with the ... information.
e = simpleError(paste(1:length(messages), messages, sep = ": ",collapse = ""))
class(e) = c(class, class(e))
stop(e)
}
messages <<- c(messages, msg)
}
}
xmlStop =
#
# Never used anymore.
# Related to the non-structed error handling.
function(msg, class = "XMLParserError")
{
err = simpleError(msg)
class(err) = c(class , class(err))
stop(err)
}
makeXMLError =
function(msg, code, domain, line, col, level, filename, class = "XMLError")
{
err = simpleError(msg)
err$code = getEnumValue(code, xmlParserErrors)
err$domain = getEnumValue(domain, xmlErrorDomain)
err$line = line
err$col = col
err$level = getEnumValue(level, xmlErrorLevel)
err$filename = filename
class(err) = c(class, class(err))
err
}
htmlErrorHandler =
function(msg, code, domain, line, col, level, filename, class = "XMLError")
{
e = makeXMLError(msg, code, domain, line, col, level, filename, class)
dom = names(e$domain)
class(e) = c(names(e$code),
sprintf("%s_Error", gsub("_FROM_", "_", dom)),
class(e))
if(e$code == xmlParserErrors["XML_IO_LOAD_ERROR"])
stop(e)
}
xmlStructuredStop =
function(msg, code, domain, line, col, level, filename, class = "XMLError")
{
err = makeXMLError(msg, code, domain, line, col, level, filename, class)
stop(err)
}
xmlErrorFun =
function()
{
errors = list()
h = function(msg, code, domain, line, col, level, filename) {
if(length(msg) == 0)
return(TRUE)
err = list(msg = msg, code = code,
domain = domain, line = line,
col = col, level = level, filename = filename)
err = fixXMLError(err)
errors[[length(errors) + 1]] <<- err
}
structure(list(handler = h, errors = function() structure(errors, class = "XMLStructuredErrorList"), reset = function() errors <<- list),
class = "XMLStructuredErrorCumulator")
}
setOldClass("XMLStructuredErrorList")
print.XMLStructuredErrorList =
function(x, ...) {
if(length(x) == 0)
print(NULL)
else
print(t(sapply(x, function(x) unlist(x[c("line", "msg")]))))
}
getXMLErrors=
#
# This attempts to read the specified file using the function given in parse
# and then returns a list of the errors in the document.
# This a somewhat convenient mechanism for fixing up, e.g., malformed HTML
# pages or other XML documents.
function(filename, parse = xmlParse, ...)
{
f = xmlErrorFun()
opts = options()
options(error = NULL)
on.exit(options(opts))
tryCatch(parse(filename, ..., error = f$handler), error = function(e){})
f$errors()
}
# Low level error handler
setXMLErrorHandler =
function(fun)
{
prev = .Call("RS_XML_getStructuredErrorHandler", PACKAGE = "XML")
sym = getNativeSymbolInfo("R_xmlStructuredErrorHandler", "XML")$address
.Call("RS_XML_setStructuredErrorHandler", list(fun, sym), PACKAGE = "XML")
prev
}
fixXMLError =
function(err)
{
err$domain = getEnumValue(err$domain, xmlErrorDomain)
err$code = getEnumValue(err$code, xmlParserErrors)
err$level = getEnumValue(err$level, xmlErrorLevel)
class(err) = "XMLError"
err
}
getEnumValue =
function(value, defs)
{
# might use for the class.
name = substitute(defs)
i = which(value == defs)
defs[i]
}
|