Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * error.c: module displaying/handling XML parser errors |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 6 | * Daniel Veillard <daniel@veillard.com> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 9 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 12 | #include <string.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13 | #include <stdarg.h> |
| 14 | #include <libxml/parser.h> |
| 15 | #include <libxml/xmlerror.h> |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 16 | #include <libxml/xmlmemory.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 17 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 18 | |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 19 | void xmlGenericErrorDefaultFunc (void *ctx ATTRIBUTE_UNUSED, |
| 20 | const char *msg, |
| 21 | ...); |
| 22 | |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 23 | #define XML_GET_VAR_STR(msg, str) { \ |
| 24 | int size; \ |
| 25 | int chars; \ |
| 26 | char *larger; \ |
| 27 | va_list ap; \ |
| 28 | \ |
| 29 | str = (char *) xmlMalloc(150); \ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 30 | if (str != NULL) { \ |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 31 | \ |
| 32 | size = 150; \ |
| 33 | \ |
| 34 | while (1) { \ |
| 35 | va_start(ap, msg); \ |
| 36 | chars = vsnprintf(str, size, msg, ap); \ |
| 37 | va_end(ap); \ |
| 38 | if ((chars > -1) && (chars < size)) \ |
| 39 | break; \ |
| 40 | if (chars > -1) \ |
| 41 | size += chars + 1; \ |
| 42 | else \ |
| 43 | size += 100; \ |
| 44 | if ((larger = (char *) xmlRealloc(str, size)) == NULL) {\ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 45 | break; \ |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 46 | } \ |
| 47 | str = larger; \ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 48 | }} \ |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 49 | } |
Bjorn Reese | 570ff08 | 2001-06-05 12:45:55 +0000 | [diff] [blame] | 50 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 51 | /************************************************************************ |
| 52 | * * |
| 53 | * Handling of out of context errors * |
| 54 | * * |
| 55 | ************************************************************************/ |
| 56 | |
| 57 | /** |
| 58 | * xmlGenericErrorDefaultFunc: |
| 59 | * @ctx: an error context |
| 60 | * @msg: the message to display/transmit |
| 61 | * @...: extra parameters for the message display |
| 62 | * |
| 63 | * Default handler for out of context error messages. |
| 64 | */ |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 65 | void |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 66 | xmlGenericErrorDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 67 | va_list args; |
| 68 | |
| 69 | if (xmlGenericErrorContext == NULL) |
| 70 | xmlGenericErrorContext = (void *) stderr; |
| 71 | |
| 72 | va_start(args, msg); |
| 73 | vfprintf((FILE *)xmlGenericErrorContext, msg, args); |
| 74 | va_end(args); |
| 75 | } |
| 76 | |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 77 | /** |
| 78 | * initGenericErrorDefaultFunc: |
| 79 | * @handler: the handler |
| 80 | * |
| 81 | * Set or reset (if NULL) the default handler for generic errors |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 82 | * to the builtin error function. |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 83 | */ |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 84 | void |
Daniel Veillard | db5850a | 2002-01-18 11:49:26 +0000 | [diff] [blame] | 85 | initGenericErrorDefaultFunc(xmlGenericErrorFunc * handler) |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 86 | { |
Daniel Veillard | db5850a | 2002-01-18 11:49:26 +0000 | [diff] [blame] | 87 | if (handler == NULL) |
| 88 | xmlGenericError = xmlGenericErrorDefaultFunc; |
| 89 | else |
| 90 | (*handler) = xmlGenericErrorDefaultFunc; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 91 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * xmlSetGenericErrorFunc: |
| 95 | * @ctx: the new error handling context |
| 96 | * @handler: the new handler function |
| 97 | * |
| 98 | * Function to reset the handler and the error context for out of |
| 99 | * context error messages. |
| 100 | * This simply means that @handler will be called for subsequent |
| 101 | * error messages while not parsing nor validating. And @ctx will |
| 102 | * be passed as first argument to @handler |
| 103 | * One can simply force messages to be emitted to another FILE * than |
| 104 | * stderr by setting @ctx to this file handle and @handler to NULL. |
| 105 | */ |
| 106 | void |
| 107 | xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) { |
| 108 | xmlGenericErrorContext = ctx; |
| 109 | if (handler != NULL) |
| 110 | xmlGenericError = handler; |
| 111 | else |
| 112 | xmlGenericError = xmlGenericErrorDefaultFunc; |
| 113 | } |
| 114 | |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 115 | /** |
| 116 | * xmlSetStructuredErrorFunc: |
| 117 | * @ctx: the new error handling context |
| 118 | * @handler: the new handler function |
| 119 | * |
| 120 | * Function to reset the handler and the error context for out of |
| 121 | * context structured error messages. |
| 122 | * This simply means that @handler will be called for subsequent |
| 123 | * error messages while not parsing nor validating. And @ctx will |
| 124 | * be passed as first argument to @handler |
| 125 | */ |
| 126 | void |
| 127 | xmlSetStructuredErrorFunc(void *ctx, xmlStructuredErrorFunc handler) { |
| 128 | xmlGenericErrorContext = ctx; |
| 129 | xmlStructuredError = handler; |
| 130 | } |
| 131 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 132 | /************************************************************************ |
| 133 | * * |
| 134 | * Handling of parsing errors * |
| 135 | * * |
| 136 | ************************************************************************/ |
| 137 | |
| 138 | /** |
| 139 | * xmlParserPrintFileInfo: |
| 140 | * @input: an xmlParserInputPtr input |
| 141 | * |
| 142 | * Displays the associated file and line informations for the current input |
| 143 | */ |
| 144 | |
| 145 | void |
| 146 | xmlParserPrintFileInfo(xmlParserInputPtr input) { |
| 147 | if (input != NULL) { |
| 148 | if (input->filename) |
| 149 | xmlGenericError(xmlGenericErrorContext, |
| 150 | "%s:%d: ", input->filename, |
| 151 | input->line); |
| 152 | else |
| 153 | xmlGenericError(xmlGenericErrorContext, |
| 154 | "Entity: line %d: ", input->line); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * xmlParserPrintFileContext: |
| 160 | * @input: an xmlParserInputPtr input |
| 161 | * |
| 162 | * Displays current context within the input content for error tracking |
| 163 | */ |
| 164 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 165 | static void |
| 166 | xmlParserPrintFileContextInternal(xmlParserInputPtr input , |
| 167 | xmlGenericErrorFunc channel, void *data ) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 168 | const xmlChar *cur, *base; |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 169 | unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */ |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 170 | xmlChar content[81]; /* space for 80 chars + line terminator */ |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 171 | xmlChar *ctnt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 172 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 173 | if (input == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 174 | cur = input->cur; |
| 175 | base = input->base; |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 176 | /* skip backwards over any end-of-lines */ |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 177 | while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 178 | cur--; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 179 | } |
| 180 | n = 0; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 181 | /* search backwards for beginning-of-line (to max buff size) */ |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 182 | while ((n++ < (sizeof(content)-1)) && (cur > base) && |
| 183 | (*(cur) != '\n') && (*(cur) != '\r')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 184 | cur--; |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 185 | if ((*(cur) == '\n') || (*(cur) == '\r')) cur++; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 186 | /* calculate the error position in terms of the current position */ |
| 187 | col = input->cur - cur; |
| 188 | /* search forward for end-of-line (to max buff size) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 189 | n = 0; |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 190 | ctnt = content; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 191 | /* copy selected text to our buffer */ |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 192 | while ((*cur != 0) && (*(cur) != '\n') && |
| 193 | (*(cur) != '\r') && (n < sizeof(content)-1)) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 194 | *ctnt++ = *cur++; |
| 195 | n++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 196 | } |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 197 | *ctnt = 0; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 198 | /* print out the selected text */ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 199 | channel(data ,"%s\n", content); |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 200 | /* create blank line with problem pointer */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 201 | n = 0; |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 202 | ctnt = content; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 203 | /* (leave buffer space for pointer + line terminator) */ |
| 204 | while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) { |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 205 | if (*(ctnt) != '\t') |
| 206 | *(ctnt) = ' '; |
William M. Brack | 6984830 | 2003-09-22 00:24:51 +0000 | [diff] [blame] | 207 | ctnt++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 208 | } |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 209 | *ctnt++ = '^'; |
| 210 | *ctnt = 0; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 211 | channel(data ,"%s\n", content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 214 | /** |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 215 | * xmlParserPrintFileContext: |
| 216 | * @input: an xmlParserInputPtr input |
| 217 | * |
| 218 | * Displays current context within the input content for error tracking |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 219 | */ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 220 | void |
| 221 | xmlParserPrintFileContext(xmlParserInputPtr input) { |
| 222 | xmlParserPrintFileContextInternal(input, xmlGenericError, |
| 223 | xmlGenericErrorContext); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 224 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * xmlReportError: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 228 | * @err: the error |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 229 | * @ctx: the parser context or NULL |
| 230 | * @str: the formatted error message |
| 231 | * |
| 232 | * Report an erro with its context, replace the 4 old error/warning |
| 233 | * routines. |
| 234 | */ |
| 235 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 236 | xmlReportError(xmlErrorPtr err, xmlParserCtxtPtr ctxt, const char *str, |
| 237 | xmlGenericErrorFunc channel, void *data) |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 238 | { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 239 | char *file = NULL; |
| 240 | int line = 0; |
| 241 | int code = -1; |
| 242 | int domain; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 243 | const xmlChar *name = NULL; |
| 244 | xmlNodePtr node; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 245 | xmlErrorLevel level; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 246 | xmlParserInputPtr input = NULL; |
| 247 | xmlParserInputPtr cur = NULL; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 248 | |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 249 | if (err == NULL) |
| 250 | return; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 251 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 252 | if (channel == NULL) { |
| 253 | channel = xmlGenericError; |
| 254 | data = xmlGenericErrorContext; |
| 255 | } |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 256 | file = err->file; |
| 257 | line = err->line; |
| 258 | code = err->code; |
| 259 | domain = err->domain; |
| 260 | level = err->level; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 261 | node = err->node; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 262 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 263 | if (code == XML_ERR_OK) |
| 264 | return; |
| 265 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 266 | if ((node != NULL) && (node->type == XML_ELEMENT_NODE)) |
| 267 | name = node->name; |
| 268 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 269 | /* |
| 270 | * Maintain the compatibility with the legacy error handling |
| 271 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 272 | if (ctxt != NULL) { |
| 273 | input = ctxt->input; |
| 274 | if ((input != NULL) && (input->filename == NULL) && |
| 275 | (ctxt->inputNr > 1)) { |
| 276 | cur = input; |
| 277 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 278 | } |
| 279 | if (input != NULL) { |
| 280 | if (input->filename) |
| 281 | channel(data, "%s:%d: ", input->filename, input->line); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 282 | else if ((line != 0) && (domain == XML_FROM_PARSER)) |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 283 | channel(data, "Entity: line %d: ", input->line); |
| 284 | } |
| 285 | } else { |
| 286 | if (file != NULL) |
| 287 | channel(data, "%s:%d: ", file, line); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 288 | else if ((line != 0) && (domain == XML_FROM_PARSER)) |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 289 | channel(data, "Entity: line %d: ", line); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 290 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 291 | if (name != NULL) { |
| 292 | channel(data, "element %s: ", name); |
| 293 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 294 | if (code == XML_ERR_OK) |
| 295 | return; |
| 296 | switch (domain) { |
| 297 | case XML_FROM_PARSER: |
| 298 | channel(data, "parser "); |
| 299 | break; |
| 300 | case XML_FROM_NAMESPACE: |
| 301 | channel(data, "namespace "); |
| 302 | break; |
| 303 | case XML_FROM_DTD: |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 304 | case XML_FROM_VALID: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 305 | channel(data, "validity "); |
| 306 | break; |
| 307 | case XML_FROM_HTML: |
| 308 | channel(data, "HTML parser "); |
| 309 | break; |
| 310 | case XML_FROM_MEMORY: |
| 311 | channel(data, "memory "); |
| 312 | break; |
| 313 | case XML_FROM_OUTPUT: |
| 314 | channel(data, "output "); |
| 315 | break; |
| 316 | case XML_FROM_IO: |
| 317 | channel(data, "I/O "); |
| 318 | break; |
| 319 | case XML_FROM_XINCLUDE: |
| 320 | channel(data, "XInclude "); |
| 321 | break; |
| 322 | case XML_FROM_XPATH: |
| 323 | channel(data, "XPath "); |
| 324 | break; |
| 325 | case XML_FROM_XPOINTER: |
| 326 | channel(data, "parser "); |
| 327 | break; |
| 328 | case XML_FROM_REGEXP: |
| 329 | channel(data, "regexp "); |
| 330 | break; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 331 | case XML_FROM_SCHEMASV: |
Daniel Veillard | 87db3a8 | 2003-10-10 10:52:58 +0000 | [diff] [blame] | 332 | channel(data, "Schemas validity "); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 333 | break; |
| 334 | case XML_FROM_SCHEMASP: |
Daniel Veillard | 87db3a8 | 2003-10-10 10:52:58 +0000 | [diff] [blame] | 335 | channel(data, "Schemas parser "); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 336 | break; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 337 | case XML_FROM_RELAXNGP: |
| 338 | channel(data, "Relax-NG parser "); |
| 339 | break; |
| 340 | case XML_FROM_RELAXNGV: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 341 | channel(data, "Relax-NG validity "); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 342 | break; |
| 343 | case XML_FROM_CATALOG: |
| 344 | channel(data, "Catalog "); |
| 345 | break; |
| 346 | case XML_FROM_C14N: |
| 347 | channel(data, "C14N "); |
| 348 | break; |
| 349 | case XML_FROM_XSLT: |
| 350 | channel(data, "XSLT "); |
| 351 | break; |
| 352 | default: |
| 353 | break; |
| 354 | } |
| 355 | if (code == XML_ERR_OK) |
| 356 | return; |
| 357 | switch (level) { |
| 358 | case XML_ERR_NONE: |
| 359 | channel(data, ": "); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 360 | break; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 361 | case XML_ERR_WARNING: |
| 362 | channel(data, "warning : "); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 363 | break; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 364 | case XML_ERR_ERROR: |
| 365 | channel(data, "error : "); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 366 | break; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 367 | case XML_ERR_FATAL: |
| 368 | channel(data, "error : "); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 369 | break; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 370 | } |
| 371 | if (code == XML_ERR_OK) |
| 372 | return; |
| 373 | if (str != NULL) { |
Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 374 | int len; |
| 375 | len = xmlStrlen((const xmlChar *)str); |
| 376 | if ((len > 0) && (str[len - 1] != '\n')) |
Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 377 | channel(data, "%s\n", str); |
Daniel Veillard | a885622 | 2003-10-08 19:26:03 +0000 | [diff] [blame] | 378 | else |
| 379 | channel(data, "%s", str); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 380 | } else { |
Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 381 | channel(data, "%s\n", "out of memory error"); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 382 | } |
| 383 | if (code == XML_ERR_OK) |
| 384 | return; |
| 385 | |
| 386 | if (ctxt != NULL) { |
| 387 | xmlParserPrintFileContextInternal(input, channel, data); |
| 388 | if (cur != NULL) { |
| 389 | if (cur->filename) |
| 390 | channel(data, "%s:%d: \n", cur->filename, cur->line); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 391 | else if ((line != 0) && (domain == XML_FROM_PARSER)) |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 392 | channel(data, "Entity: line %d: \n", cur->line); |
| 393 | xmlParserPrintFileContextInternal(cur, channel, data); |
| 394 | } |
| 395 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 396 | if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) && |
| 397 | (err->int1 < 100) && |
| 398 | (err->int1 < xmlStrlen((const xmlChar *)err->str1))) { |
| 399 | xmlChar buf[150]; |
| 400 | int i; |
| 401 | |
| 402 | channel(data, "%s\n", err->str1); |
| 403 | for (i=0;i < err->int1;i++) |
| 404 | buf[i] = ' '; |
| 405 | buf[i++] = '^'; |
| 406 | buf[i] = 0; |
| 407 | channel(data, "%s\n", buf); |
| 408 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /** |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 412 | * __xmlRaiseError: |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 413 | * @channel: the structured callback channel |
| 414 | * @channel: the old callback channel |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 415 | * @data: the callback data |
| 416 | * @ctx: the parser context or NULL |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 417 | * @ctx: the parser context or NULL |
| 418 | * @domain: the domain for the error |
| 419 | * @code: the code for the error |
| 420 | * @level: the xmlErrorLevel for the error |
| 421 | * @file: the file source of the error (or NULL) |
| 422 | * @line: the line of the error or 0 if N/A |
| 423 | * @str1: extra string info |
| 424 | * @str2: extra string info |
| 425 | * @str3: extra string info |
| 426 | * @int1: extra int info |
| 427 | * @int2: extra int info |
| 428 | * @msg: the message to display/transmit |
| 429 | * @...: extra parameters for the message display |
| 430 | * |
| 431 | * Update teh appropriate global or contextual error structure, |
| 432 | * then forward the error message down the parser or generic |
| 433 | * error callback handler |
| 434 | */ |
| 435 | void |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 436 | __xmlRaiseError(xmlStructuredErrorFunc schannel, |
| 437 | xmlGenericErrorFunc channel, void *data, void *ctx, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 438 | void *nod, int domain, int code, xmlErrorLevel level, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 439 | const char *file, int line, const char *str1, |
| 440 | const char *str2, const char *str3, int int1, int int2, |
| 441 | const char *msg, ...) |
| 442 | { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 443 | xmlParserCtxtPtr ctxt = NULL; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 444 | xmlNodePtr node = (xmlNodePtr) nod; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 445 | char *str = NULL; |
| 446 | xmlParserInputPtr input = NULL; |
| 447 | xmlErrorPtr to = &xmlLastError; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 448 | xmlChar *base = NULL; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 449 | |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 450 | if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) || |
| 451 | (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) || |
| 452 | (domain == XML_FROM_IO)) { |
| 453 | ctxt = (xmlParserCtxtPtr) ctx; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 454 | if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) && |
| 455 | (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
| 456 | schannel = ctxt->sax->serror; |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 457 | } |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 458 | if ((domain == XML_FROM_VALID) && |
| 459 | ((channel == xmlParserValidityError) || |
| 460 | (channel == xmlParserValidityWarning))) { |
| 461 | ctxt = (xmlParserCtxtPtr) ctx; |
| 462 | if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) && |
| 463 | (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
| 464 | schannel = ctxt->sax->serror; |
| 465 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 466 | if (code == XML_ERR_OK) |
| 467 | return; |
| 468 | /* |
| 469 | * Formatting the message |
| 470 | */ |
| 471 | if (msg == NULL) { |
| 472 | str = (char *) xmlStrdup(BAD_CAST "No error message provided"); |
| 473 | } else { |
| 474 | XML_GET_VAR_STR(msg, str); |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * specific processing if a parser context is provided |
| 479 | */ |
| 480 | if (ctxt != NULL) { |
| 481 | if (file == NULL) { |
| 482 | input = ctxt->input; |
| 483 | if ((input != NULL) && (input->filename == NULL) && |
| 484 | (ctxt->inputNr > 1)) { |
| 485 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 486 | } |
| 487 | if (input != NULL) { |
| 488 | file = input->filename; |
| 489 | line = input->line; |
| 490 | } |
| 491 | } |
| 492 | to = &ctxt->lastError; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 493 | } else if ((node != NULL) && (file == NULL)) { |
| 494 | int i; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 495 | |
| 496 | if ((node->doc != NULL) && (node->doc->URL != NULL)) |
| 497 | base = xmlStrdup(node->doc->URL); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 498 | for (i = 0; |
| 499 | ((i < 10) && (node != NULL) && (node->type != XML_ELEMENT_NODE)); |
| 500 | i++) |
| 501 | node = node->parent; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 502 | if ((base == NULL) && (node != NULL) && |
| 503 | (node->doc != NULL) && (node->doc->URL != NULL)) |
| 504 | base = xmlStrdup(node->doc->URL); |
| 505 | |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 506 | if ((node != NULL) && (node->type == XML_ELEMENT_NODE)) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 507 | line = node->line; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Save the informations about the error |
| 512 | */ |
| 513 | xmlResetError(to); |
| 514 | to->domain = domain; |
| 515 | to->code = code; |
| 516 | to->message = str; |
| 517 | to->level = level; |
| 518 | if (file != NULL) |
| 519 | to->file = (char *) xmlStrdup((const xmlChar *) file); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 520 | else if (base != NULL) { |
| 521 | to->file = (char *) base; |
| 522 | file = (char *) base; |
| 523 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 524 | to->line = line; |
| 525 | if (str1 != NULL) |
| 526 | to->str1 = (char *) xmlStrdup((const xmlChar *) str1); |
| 527 | if (str2 != NULL) |
| 528 | to->str2 = (char *) xmlStrdup((const xmlChar *) str2); |
| 529 | if (str3 != NULL) |
| 530 | to->str3 = (char *) xmlStrdup((const xmlChar *) str3); |
| 531 | to->int1 = int1; |
| 532 | to->int2 = int2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 533 | to->node = node; |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 534 | to->ctxt = ctx; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 535 | |
| 536 | /* |
| 537 | * Find the callback channel. |
| 538 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 539 | if ((ctxt != NULL) && (channel == NULL)) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 540 | if (level == XML_ERR_WARNING) |
| 541 | channel = ctxt->sax->warning; |
| 542 | else |
| 543 | channel = ctxt->sax->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 544 | data = ctxt->userData; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 545 | } else if (channel == NULL) { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 546 | if (xmlStructuredError != NULL) |
| 547 | schannel = xmlStructuredError; |
| 548 | else |
| 549 | channel = xmlGenericError; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 550 | data = xmlGenericErrorContext; |
| 551 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 552 | if (schannel != NULL) { |
| 553 | schannel(data, to); |
| 554 | return; |
| 555 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 556 | if (channel == NULL) |
| 557 | return; |
| 558 | |
| 559 | if ((channel == xmlParserError) || |
| 560 | (channel == xmlParserWarning) || |
| 561 | (channel == xmlParserValidityError) || |
| 562 | (channel == xmlParserValidityWarning)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 563 | xmlReportError(to, ctxt, str, NULL, NULL); |
| 564 | else if ((channel == (xmlGenericErrorFunc) fprintf) || |
| 565 | (channel == xmlGenericErrorDefaultFunc)) |
| 566 | xmlReportError(to, ctxt, str, channel, data); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 567 | else |
| 568 | channel(data, "%s", str); |
| 569 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 570 | |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 571 | /** |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 572 | * __xmlSimpleError: |
| 573 | * @domain: where the error comes from |
| 574 | * @code: the error code |
| 575 | * @node: the context node |
| 576 | * @extra: extra informations |
| 577 | * |
| 578 | * Handle an out of memory condition |
| 579 | */ |
| 580 | void |
| 581 | __xmlSimpleError(int domain, int code, xmlNodePtr node, |
| 582 | const char *msg, const char *extra) |
| 583 | { |
| 584 | |
| 585 | if (code == XML_ERR_NO_MEMORY) { |
| 586 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 587 | __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain, |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 588 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 589 | NULL, NULL, 0, 0, |
| 590 | "Memory allocation failed : %s\n", extra); |
| 591 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 592 | __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain, |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 593 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 594 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 595 | } else { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 596 | __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain, |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 597 | code, XML_ERR_ERROR, NULL, 0, extra, |
| 598 | NULL, NULL, 0, 0, msg, extra); |
| 599 | } |
| 600 | } |
| 601 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 602 | * xmlParserError: |
| 603 | * @ctx: an XML parser context |
| 604 | * @msg: the message to display/transmit |
| 605 | * @...: extra parameters for the message display |
| 606 | * |
| 607 | * Display and format an error messages, gives file, line, position and |
| 608 | * extra parameters. |
| 609 | */ |
| 610 | void |
| 611 | xmlParserError(void *ctx, const char *msg, ...) |
| 612 | { |
| 613 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 614 | xmlParserInputPtr input = NULL; |
| 615 | xmlParserInputPtr cur = NULL; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 616 | char * str; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 617 | |
| 618 | if (ctxt != NULL) { |
| 619 | input = ctxt->input; |
| 620 | if ((input != NULL) && (input->filename == NULL) && |
| 621 | (ctxt->inputNr > 1)) { |
| 622 | cur = input; |
| 623 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 624 | } |
| 625 | xmlParserPrintFileInfo(input); |
| 626 | } |
| 627 | |
| 628 | xmlGenericError(xmlGenericErrorContext, "error: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 629 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 630 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 631 | if (str != NULL) |
| 632 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 633 | |
| 634 | if (ctxt != NULL) { |
| 635 | xmlParserPrintFileContext(input); |
| 636 | if (cur != NULL) { |
| 637 | xmlParserPrintFileInfo(cur); |
| 638 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 639 | xmlParserPrintFileContext(cur); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * xmlParserWarning: |
| 646 | * @ctx: an XML parser context |
| 647 | * @msg: the message to display/transmit |
| 648 | * @...: extra parameters for the message display |
| 649 | * |
| 650 | * Display and format a warning messages, gives file, line, position and |
| 651 | * extra parameters. |
| 652 | */ |
| 653 | void |
| 654 | xmlParserWarning(void *ctx, const char *msg, ...) |
| 655 | { |
| 656 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 657 | xmlParserInputPtr input = NULL; |
| 658 | xmlParserInputPtr cur = NULL; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 659 | char * str; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 660 | |
| 661 | if (ctxt != NULL) { |
| 662 | input = ctxt->input; |
| 663 | if ((input != NULL) && (input->filename == NULL) && |
| 664 | (ctxt->inputNr > 1)) { |
| 665 | cur = input; |
| 666 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 667 | } |
| 668 | xmlParserPrintFileInfo(input); |
| 669 | } |
| 670 | |
| 671 | xmlGenericError(xmlGenericErrorContext, "warning: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 672 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 673 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 674 | if (str != NULL) |
| 675 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 676 | |
| 677 | if (ctxt != NULL) { |
| 678 | xmlParserPrintFileContext(input); |
| 679 | if (cur != NULL) { |
| 680 | xmlParserPrintFileInfo(cur); |
| 681 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 682 | xmlParserPrintFileContext(cur); |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | /************************************************************************ |
| 688 | * * |
| 689 | * Handling of validation errors * |
| 690 | * * |
| 691 | ************************************************************************/ |
| 692 | |
| 693 | /** |
| 694 | * xmlParserValidityError: |
| 695 | * @ctx: an XML parser context |
| 696 | * @msg: the message to display/transmit |
| 697 | * @...: extra parameters for the message display |
| 698 | * |
| 699 | * Display and format an validity error messages, gives file, |
| 700 | * line, position and extra parameters. |
| 701 | */ |
| 702 | void |
| 703 | xmlParserValidityError(void *ctx, const char *msg, ...) |
| 704 | { |
| 705 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 706 | xmlParserInputPtr input = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 707 | char * str; |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 708 | int len = xmlStrlen((const xmlChar *) msg); |
| 709 | static int had_info = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 710 | |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 711 | if ((len > 1) && (msg[len - 2] != ':')) { |
| 712 | if (ctxt != NULL) { |
| 713 | input = ctxt->input; |
| 714 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 715 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 716 | |
| 717 | if (had_info == 0) { |
| 718 | xmlParserPrintFileInfo(input); |
| 719 | } |
| 720 | } |
| 721 | xmlGenericError(xmlGenericErrorContext, "validity error: "); |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 722 | had_info = 0; |
| 723 | } else { |
| 724 | had_info = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 727 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 728 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 729 | if (str != NULL) |
| 730 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 731 | |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 732 | if ((ctxt != NULL) && (input != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 733 | xmlParserPrintFileContext(input); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * xmlParserValidityWarning: |
| 739 | * @ctx: an XML parser context |
| 740 | * @msg: the message to display/transmit |
| 741 | * @...: extra parameters for the message display |
| 742 | * |
| 743 | * Display and format a validity warning messages, gives file, line, |
| 744 | * position and extra parameters. |
| 745 | */ |
| 746 | void |
| 747 | xmlParserValidityWarning(void *ctx, const char *msg, ...) |
| 748 | { |
| 749 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 750 | xmlParserInputPtr input = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 751 | char * str; |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 752 | int len = xmlStrlen((const xmlChar *) msg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 753 | |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 754 | if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 755 | input = ctxt->input; |
| 756 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 757 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 758 | |
| 759 | xmlParserPrintFileInfo(input); |
| 760 | } |
| 761 | |
| 762 | xmlGenericError(xmlGenericErrorContext, "validity warning: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 763 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 764 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 765 | if (str != NULL) |
| 766 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 767 | |
| 768 | if (ctxt != NULL) { |
| 769 | xmlParserPrintFileContext(input); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 774 | /************************************************************************ |
| 775 | * * |
| 776 | * Extended Error Handling * |
| 777 | * * |
| 778 | ************************************************************************/ |
| 779 | |
| 780 | /** |
| 781 | * xmlGetLastError: |
| 782 | * |
| 783 | * Get the last global error registered. This is per thread if compiled |
| 784 | * with thread support. |
| 785 | * |
| 786 | * Returns NULL if no error occured or a pointer to the error |
| 787 | */ |
| 788 | xmlErrorPtr |
| 789 | xmlGetLastError(void) |
| 790 | { |
| 791 | if (xmlLastError.code == XML_ERR_OK) |
| 792 | return (NULL); |
| 793 | return (&xmlLastError); |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * xmlResetError: |
| 798 | * @err: pointer to the error. |
| 799 | * |
| 800 | * Cleanup the error. |
| 801 | */ |
| 802 | void |
| 803 | xmlResetError(xmlErrorPtr err) |
| 804 | { |
| 805 | if (err == NULL) |
| 806 | return; |
| 807 | if (err->code == XML_ERR_OK) |
| 808 | return; |
| 809 | if (err->message != NULL) |
| 810 | xmlFree(err->message); |
| 811 | if (err->file != NULL) |
| 812 | xmlFree(err->file); |
| 813 | if (err->str1 != NULL) |
| 814 | xmlFree(err->str1); |
| 815 | if (err->str2 != NULL) |
| 816 | xmlFree(err->str2); |
| 817 | if (err->str3 != NULL) |
| 818 | xmlFree(err->str3); |
| 819 | memset(err, 0, sizeof(xmlError)); |
| 820 | err->code = XML_ERR_OK; |
| 821 | } |
| 822 | |
| 823 | /** |
| 824 | * xmlResetLastError: |
| 825 | * |
| 826 | * Cleanup the last global error registered. For parsing error |
| 827 | * this does not change the well-formedness result. |
| 828 | */ |
| 829 | void |
| 830 | xmlResetLastError(void) |
| 831 | { |
| 832 | if (xmlLastError.code == XML_ERR_OK) |
| 833 | return; |
| 834 | xmlResetError(&xmlLastError); |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * xmlCtxtGetLastError: |
| 839 | * @ctx: an XML parser context |
| 840 | * |
| 841 | * Get the last parsing error registered. |
| 842 | * |
| 843 | * Returns NULL if no error occured or a pointer to the error |
| 844 | */ |
| 845 | xmlErrorPtr |
| 846 | xmlCtxtGetLastError(void *ctx) |
| 847 | { |
| 848 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 849 | |
| 850 | if (ctxt == NULL) |
| 851 | return (NULL); |
| 852 | if (ctxt->lastError.code == XML_ERR_OK) |
| 853 | return (NULL); |
| 854 | return (&ctxt->lastError); |
| 855 | } |
| 856 | |
| 857 | /** |
| 858 | * xmlCtxtResetLastError: |
| 859 | * @ctx: an XML parser context |
| 860 | * |
| 861 | * Cleanup the last global error registered. For parsing error |
| 862 | * this does not change the well-formedness result. |
| 863 | */ |
| 864 | void |
| 865 | xmlCtxtResetLastError(void *ctx) |
| 866 | { |
| 867 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 868 | |
| 869 | if (ctxt == NULL) |
| 870 | return; |
| 871 | if (ctxt->lastError.code == XML_ERR_OK) |
| 872 | return; |
| 873 | xmlResetError(&ctxt->lastError); |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * xmlCopyError: |
| 878 | * @from: a source error |
| 879 | * @to: a target error |
| 880 | * |
| 881 | * Save the original error to the new place. |
| 882 | * |
| 883 | * Returns 0 in case of success and -1 in case of error. |
| 884 | */ |
| 885 | int |
| 886 | xmlCopyError(xmlErrorPtr from, xmlErrorPtr to) { |
| 887 | if ((from == NULL) || (to == NULL)) |
| 888 | return(-1); |
| 889 | if (to->message != NULL) |
| 890 | xmlFree(to->message); |
| 891 | if (to->file != NULL) |
| 892 | xmlFree(to->file); |
| 893 | if (to->str1 != NULL) |
| 894 | xmlFree(to->str1); |
| 895 | if (to->str2 != NULL) |
| 896 | xmlFree(to->str2); |
| 897 | if (to->str3 != NULL) |
| 898 | xmlFree(to->str3); |
| 899 | to->domain = from->domain; |
| 900 | to->code = from->code; |
| 901 | to->level = from->level; |
| 902 | to->line = from->line; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 903 | to->node = from->node; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 904 | to->int1 = from->int1; |
| 905 | to->int2 = from->int2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 906 | to->node = from->node; |
| 907 | to->ctxt = from->ctxt; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 908 | if (from->message != NULL) |
| 909 | to->message = (char *) xmlStrdup((xmlChar *) from->message); |
| 910 | else |
| 911 | to->message = NULL; |
| 912 | if (from->file != NULL) |
| 913 | to->file = (char *) xmlStrdup((xmlChar *) from->file); |
| 914 | else |
| 915 | to->file = NULL; |
| 916 | if (from->str1 != NULL) |
| 917 | to->str1 = (char *) xmlStrdup((xmlChar *) from->str1); |
| 918 | else |
| 919 | to->str1 = NULL; |
| 920 | if (from->str2 != NULL) |
| 921 | to->str2 = (char *) xmlStrdup((xmlChar *) from->str2); |
| 922 | else |
| 923 | to->str2 = NULL; |
| 924 | if (from->str3 != NULL) |
| 925 | to->str3 = (char *) xmlStrdup((xmlChar *) from->str3); |
| 926 | else |
| 927 | to->str3 = NULL; |
| 928 | return(0); |
| 929 | } |
| 930 | |