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 |
Daniel Veillard | da0ff5d | 2004-04-20 09:45:26 +0000 | [diff] [blame] | 90 | xmlGenericError = (*handler); |
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: |
William M. Brack | d233e39 | 2004-05-16 03:12:08 +0000 | [diff] [blame] | 413 | * @schannel: the structured callback channel |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 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 | * |
William M. Brack | d233e39 | 2004-05-16 03:12:08 +0000 | [diff] [blame] | 431 | * Update the appropriate global or contextual error structure, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 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 | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 450 | if ((xmlGetWarningsDefaultValue == 0) && (level == XML_ERR_WARNING)) |
| 451 | return; |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 452 | if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) || |
| 453 | (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) || |
| 454 | (domain == XML_FROM_IO)) { |
| 455 | ctxt = (xmlParserCtxtPtr) ctx; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 456 | if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) && |
| 457 | (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
| 458 | schannel = ctxt->sax->serror; |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 459 | } |
William M. Brack | cd3628b | 2004-07-25 21:07:29 +0000 | [diff] [blame] | 460 | /* |
| 461 | * Check if structured error handler set |
| 462 | */ |
| 463 | if (schannel == NULL) { |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 464 | schannel = xmlStructuredError; |
William M. Brack | cd3628b | 2004-07-25 21:07:29 +0000 | [diff] [blame] | 465 | /* |
| 466 | * if user has defined handler, change data ptr to user's choice |
| 467 | */ |
| 468 | if (schannel != NULL) |
| 469 | data = xmlGenericErrorContext; |
| 470 | } |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 471 | if ((domain == XML_FROM_VALID) && |
| 472 | ((channel == xmlParserValidityError) || |
| 473 | (channel == xmlParserValidityWarning))) { |
| 474 | ctxt = (xmlParserCtxtPtr) ctx; |
| 475 | if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) && |
| 476 | (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
| 477 | schannel = ctxt->sax->serror; |
| 478 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 479 | if (code == XML_ERR_OK) |
| 480 | return; |
| 481 | /* |
| 482 | * Formatting the message |
| 483 | */ |
| 484 | if (msg == NULL) { |
| 485 | str = (char *) xmlStrdup(BAD_CAST "No error message provided"); |
| 486 | } else { |
| 487 | XML_GET_VAR_STR(msg, str); |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * specific processing if a parser context is provided |
| 492 | */ |
| 493 | if (ctxt != NULL) { |
| 494 | if (file == NULL) { |
| 495 | input = ctxt->input; |
| 496 | if ((input != NULL) && (input->filename == NULL) && |
| 497 | (ctxt->inputNr > 1)) { |
| 498 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 499 | } |
| 500 | if (input != NULL) { |
| 501 | file = input->filename; |
| 502 | line = input->line; |
| 503 | } |
| 504 | } |
| 505 | to = &ctxt->lastError; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 506 | } else if ((node != NULL) && (file == NULL)) { |
| 507 | int i; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 508 | |
| 509 | if ((node->doc != NULL) && (node->doc->URL != NULL)) |
| 510 | base = xmlStrdup(node->doc->URL); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 511 | for (i = 0; |
| 512 | ((i < 10) && (node != NULL) && (node->type != XML_ELEMENT_NODE)); |
| 513 | i++) |
| 514 | node = node->parent; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 515 | if ((base == NULL) && (node != NULL) && |
| 516 | (node->doc != NULL) && (node->doc->URL != NULL)) |
| 517 | base = xmlStrdup(node->doc->URL); |
| 518 | |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 519 | if ((node != NULL) && (node->type == XML_ELEMENT_NODE)) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 520 | line = node->line; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /* |
William M. Brack | d233e39 | 2004-05-16 03:12:08 +0000 | [diff] [blame] | 524 | * Save the information about the error |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 525 | */ |
| 526 | xmlResetError(to); |
| 527 | to->domain = domain; |
| 528 | to->code = code; |
| 529 | to->message = str; |
| 530 | to->level = level; |
| 531 | if (file != NULL) |
| 532 | to->file = (char *) xmlStrdup((const xmlChar *) file); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 533 | else if (base != NULL) { |
| 534 | to->file = (char *) base; |
| 535 | file = (char *) base; |
| 536 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 537 | to->line = line; |
| 538 | if (str1 != NULL) |
| 539 | to->str1 = (char *) xmlStrdup((const xmlChar *) str1); |
| 540 | if (str2 != NULL) |
| 541 | to->str2 = (char *) xmlStrdup((const xmlChar *) str2); |
| 542 | if (str3 != NULL) |
| 543 | to->str3 = (char *) xmlStrdup((const xmlChar *) str3); |
| 544 | to->int1 = int1; |
| 545 | to->int2 = int2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 546 | to->node = node; |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 547 | to->ctxt = ctx; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 548 | |
Daniel Veillard | d34b0b8 | 2004-01-02 20:26:01 +0000 | [diff] [blame] | 549 | if (to != &xmlLastError) |
| 550 | xmlCopyError(to,&xmlLastError); |
| 551 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 552 | /* |
William M. Brack | cd3628b | 2004-07-25 21:07:29 +0000 | [diff] [blame] | 553 | * Find the callback channel if channel param is NULL |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 554 | */ |
William M. Brack | 9f797ab | 2004-07-28 07:40:12 +0000 | [diff] [blame] | 555 | if ((ctxt != NULL) && (channel == NULL) && (xmlStructuredError == NULL) && (ctxt->sax != NULL)) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 556 | if (level == XML_ERR_WARNING) |
| 557 | channel = ctxt->sax->warning; |
| 558 | else |
| 559 | channel = ctxt->sax->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 560 | data = ctxt->userData; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 561 | } else if (channel == NULL) { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 562 | if (xmlStructuredError != NULL) |
| 563 | schannel = xmlStructuredError; |
| 564 | else |
| 565 | channel = xmlGenericError; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 566 | data = xmlGenericErrorContext; |
| 567 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 568 | if (schannel != NULL) { |
| 569 | schannel(data, to); |
| 570 | return; |
| 571 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 572 | if (channel == NULL) |
| 573 | return; |
| 574 | |
| 575 | if ((channel == xmlParserError) || |
| 576 | (channel == xmlParserWarning) || |
| 577 | (channel == xmlParserValidityError) || |
| 578 | (channel == xmlParserValidityWarning)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 579 | xmlReportError(to, ctxt, str, NULL, NULL); |
| 580 | else if ((channel == (xmlGenericErrorFunc) fprintf) || |
| 581 | (channel == xmlGenericErrorDefaultFunc)) |
| 582 | xmlReportError(to, ctxt, str, channel, data); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 583 | else |
| 584 | channel(data, "%s", str); |
| 585 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 586 | |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 587 | /** |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 588 | * __xmlSimpleError: |
| 589 | * @domain: where the error comes from |
| 590 | * @code: the error code |
| 591 | * @node: the context node |
| 592 | * @extra: extra informations |
| 593 | * |
| 594 | * Handle an out of memory condition |
| 595 | */ |
| 596 | void |
| 597 | __xmlSimpleError(int domain, int code, xmlNodePtr node, |
| 598 | const char *msg, const char *extra) |
| 599 | { |
| 600 | |
| 601 | if (code == XML_ERR_NO_MEMORY) { |
| 602 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 603 | __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain, |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 604 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 605 | NULL, NULL, 0, 0, |
| 606 | "Memory allocation failed : %s\n", extra); |
| 607 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 608 | __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain, |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 609 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 610 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 611 | } else { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 612 | __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain, |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 613 | code, XML_ERR_ERROR, NULL, 0, extra, |
| 614 | NULL, NULL, 0, 0, msg, extra); |
| 615 | } |
| 616 | } |
| 617 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 618 | * xmlParserError: |
| 619 | * @ctx: an XML parser context |
| 620 | * @msg: the message to display/transmit |
| 621 | * @...: extra parameters for the message display |
| 622 | * |
| 623 | * Display and format an error messages, gives file, line, position and |
| 624 | * extra parameters. |
| 625 | */ |
| 626 | void |
| 627 | xmlParserError(void *ctx, const char *msg, ...) |
| 628 | { |
| 629 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 630 | xmlParserInputPtr input = NULL; |
| 631 | xmlParserInputPtr cur = NULL; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 632 | char * str; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 633 | |
| 634 | if (ctxt != NULL) { |
| 635 | input = ctxt->input; |
| 636 | if ((input != NULL) && (input->filename == NULL) && |
| 637 | (ctxt->inputNr > 1)) { |
| 638 | cur = input; |
| 639 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 640 | } |
| 641 | xmlParserPrintFileInfo(input); |
| 642 | } |
| 643 | |
| 644 | xmlGenericError(xmlGenericErrorContext, "error: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 645 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 646 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 647 | if (str != NULL) |
| 648 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 649 | |
| 650 | if (ctxt != NULL) { |
| 651 | xmlParserPrintFileContext(input); |
| 652 | if (cur != NULL) { |
| 653 | xmlParserPrintFileInfo(cur); |
| 654 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 655 | xmlParserPrintFileContext(cur); |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * xmlParserWarning: |
| 662 | * @ctx: an XML parser context |
| 663 | * @msg: the message to display/transmit |
| 664 | * @...: extra parameters for the message display |
| 665 | * |
| 666 | * Display and format a warning messages, gives file, line, position and |
| 667 | * extra parameters. |
| 668 | */ |
| 669 | void |
| 670 | xmlParserWarning(void *ctx, const char *msg, ...) |
| 671 | { |
| 672 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 673 | xmlParserInputPtr input = NULL; |
| 674 | xmlParserInputPtr cur = NULL; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 675 | char * str; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 676 | |
| 677 | if (ctxt != NULL) { |
| 678 | input = ctxt->input; |
| 679 | if ((input != NULL) && (input->filename == NULL) && |
| 680 | (ctxt->inputNr > 1)) { |
| 681 | cur = input; |
| 682 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 683 | } |
| 684 | xmlParserPrintFileInfo(input); |
| 685 | } |
| 686 | |
| 687 | xmlGenericError(xmlGenericErrorContext, "warning: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 688 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 689 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 690 | if (str != NULL) |
| 691 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 692 | |
| 693 | if (ctxt != NULL) { |
| 694 | xmlParserPrintFileContext(input); |
| 695 | if (cur != NULL) { |
| 696 | xmlParserPrintFileInfo(cur); |
| 697 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 698 | xmlParserPrintFileContext(cur); |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | /************************************************************************ |
| 704 | * * |
| 705 | * Handling of validation errors * |
| 706 | * * |
| 707 | ************************************************************************/ |
| 708 | |
| 709 | /** |
| 710 | * xmlParserValidityError: |
| 711 | * @ctx: an XML parser context |
| 712 | * @msg: the message to display/transmit |
| 713 | * @...: extra parameters for the message display |
| 714 | * |
| 715 | * Display and format an validity error messages, gives file, |
| 716 | * line, position and extra parameters. |
| 717 | */ |
| 718 | void |
| 719 | xmlParserValidityError(void *ctx, const char *msg, ...) |
| 720 | { |
| 721 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 722 | xmlParserInputPtr input = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 723 | char * str; |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 724 | int len = xmlStrlen((const xmlChar *) msg); |
| 725 | static int had_info = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 726 | |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 727 | if ((len > 1) && (msg[len - 2] != ':')) { |
| 728 | if (ctxt != NULL) { |
| 729 | input = ctxt->input; |
| 730 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 731 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 732 | |
| 733 | if (had_info == 0) { |
| 734 | xmlParserPrintFileInfo(input); |
| 735 | } |
| 736 | } |
| 737 | xmlGenericError(xmlGenericErrorContext, "validity error: "); |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 738 | had_info = 0; |
| 739 | } else { |
| 740 | had_info = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 743 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 744 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 745 | if (str != NULL) |
| 746 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 747 | |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 748 | if ((ctxt != NULL) && (input != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 749 | xmlParserPrintFileContext(input); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * xmlParserValidityWarning: |
| 755 | * @ctx: an XML parser context |
| 756 | * @msg: the message to display/transmit |
| 757 | * @...: extra parameters for the message display |
| 758 | * |
| 759 | * Display and format a validity warning messages, gives file, line, |
| 760 | * position and extra parameters. |
| 761 | */ |
| 762 | void |
| 763 | xmlParserValidityWarning(void *ctx, const char *msg, ...) |
| 764 | { |
| 765 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 766 | xmlParserInputPtr input = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 767 | char * str; |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 768 | int len = xmlStrlen((const xmlChar *) msg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 769 | |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 770 | if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 771 | input = ctxt->input; |
| 772 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 773 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 774 | |
| 775 | xmlParserPrintFileInfo(input); |
| 776 | } |
| 777 | |
| 778 | xmlGenericError(xmlGenericErrorContext, "validity warning: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 779 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 780 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 781 | if (str != NULL) |
| 782 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 783 | |
| 784 | if (ctxt != NULL) { |
| 785 | xmlParserPrintFileContext(input); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 790 | /************************************************************************ |
| 791 | * * |
| 792 | * Extended Error Handling * |
| 793 | * * |
| 794 | ************************************************************************/ |
| 795 | |
| 796 | /** |
| 797 | * xmlGetLastError: |
| 798 | * |
| 799 | * Get the last global error registered. This is per thread if compiled |
| 800 | * with thread support. |
| 801 | * |
| 802 | * Returns NULL if no error occured or a pointer to the error |
| 803 | */ |
| 804 | xmlErrorPtr |
| 805 | xmlGetLastError(void) |
| 806 | { |
| 807 | if (xmlLastError.code == XML_ERR_OK) |
| 808 | return (NULL); |
| 809 | return (&xmlLastError); |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * xmlResetError: |
| 814 | * @err: pointer to the error. |
| 815 | * |
| 816 | * Cleanup the error. |
| 817 | */ |
| 818 | void |
| 819 | xmlResetError(xmlErrorPtr err) |
| 820 | { |
| 821 | if (err == NULL) |
| 822 | return; |
| 823 | if (err->code == XML_ERR_OK) |
| 824 | return; |
| 825 | if (err->message != NULL) |
| 826 | xmlFree(err->message); |
| 827 | if (err->file != NULL) |
| 828 | xmlFree(err->file); |
| 829 | if (err->str1 != NULL) |
| 830 | xmlFree(err->str1); |
| 831 | if (err->str2 != NULL) |
| 832 | xmlFree(err->str2); |
| 833 | if (err->str3 != NULL) |
| 834 | xmlFree(err->str3); |
| 835 | memset(err, 0, sizeof(xmlError)); |
| 836 | err->code = XML_ERR_OK; |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * xmlResetLastError: |
| 841 | * |
| 842 | * Cleanup the last global error registered. For parsing error |
| 843 | * this does not change the well-formedness result. |
| 844 | */ |
| 845 | void |
| 846 | xmlResetLastError(void) |
| 847 | { |
| 848 | if (xmlLastError.code == XML_ERR_OK) |
| 849 | return; |
| 850 | xmlResetError(&xmlLastError); |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * xmlCtxtGetLastError: |
| 855 | * @ctx: an XML parser context |
| 856 | * |
| 857 | * Get the last parsing error registered. |
| 858 | * |
| 859 | * Returns NULL if no error occured or a pointer to the error |
| 860 | */ |
| 861 | xmlErrorPtr |
| 862 | xmlCtxtGetLastError(void *ctx) |
| 863 | { |
| 864 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 865 | |
| 866 | if (ctxt == NULL) |
| 867 | return (NULL); |
| 868 | if (ctxt->lastError.code == XML_ERR_OK) |
| 869 | return (NULL); |
| 870 | return (&ctxt->lastError); |
| 871 | } |
| 872 | |
| 873 | /** |
| 874 | * xmlCtxtResetLastError: |
| 875 | * @ctx: an XML parser context |
| 876 | * |
| 877 | * Cleanup the last global error registered. For parsing error |
| 878 | * this does not change the well-formedness result. |
| 879 | */ |
| 880 | void |
| 881 | xmlCtxtResetLastError(void *ctx) |
| 882 | { |
| 883 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 884 | |
| 885 | if (ctxt == NULL) |
| 886 | return; |
| 887 | if (ctxt->lastError.code == XML_ERR_OK) |
| 888 | return; |
| 889 | xmlResetError(&ctxt->lastError); |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * xmlCopyError: |
| 894 | * @from: a source error |
| 895 | * @to: a target error |
| 896 | * |
| 897 | * Save the original error to the new place. |
| 898 | * |
| 899 | * Returns 0 in case of success and -1 in case of error. |
| 900 | */ |
| 901 | int |
| 902 | xmlCopyError(xmlErrorPtr from, xmlErrorPtr to) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 903 | char *message, *file, *str1, *str2, *str3; |
| 904 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 905 | if ((from == NULL) || (to == NULL)) |
| 906 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 907 | |
| 908 | message = (char *) xmlStrdup((xmlChar *) from->message); |
| 909 | file = (char *) xmlStrdup ((xmlChar *) from->file); |
| 910 | str1 = (char *) xmlStrdup ((xmlChar *) from->str1); |
| 911 | str2 = (char *) xmlStrdup ((xmlChar *) from->str2); |
| 912 | str3 = (char *) xmlStrdup ((xmlChar *) from->str3); |
| 913 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 914 | if (to->message != NULL) |
| 915 | xmlFree(to->message); |
| 916 | if (to->file != NULL) |
| 917 | xmlFree(to->file); |
| 918 | if (to->str1 != NULL) |
| 919 | xmlFree(to->str1); |
| 920 | if (to->str2 != NULL) |
| 921 | xmlFree(to->str2); |
| 922 | if (to->str3 != NULL) |
| 923 | xmlFree(to->str3); |
| 924 | to->domain = from->domain; |
| 925 | to->code = from->code; |
| 926 | to->level = from->level; |
| 927 | to->line = from->line; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 928 | to->node = from->node; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 929 | to->int1 = from->int1; |
| 930 | to->int2 = from->int2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 931 | to->node = from->node; |
| 932 | to->ctxt = from->ctxt; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 933 | to->message = message; |
| 934 | to->file = file; |
| 935 | to->str1 = str1; |
| 936 | to->str2 = str2; |
| 937 | to->str3 = str3; |
| 938 | |
| 939 | return 0; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 940 | } |
| 941 | |