Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SAX.c : Default SAX handler to build a tree. |
| 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 | |
| 9 | |
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 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <libxml/xmlmemory.h> |
| 14 | #include <libxml/tree.h> |
| 15 | #include <libxml/parser.h> |
| 16 | #include <libxml/parserInternals.h> |
| 17 | #include <libxml/valid.h> |
| 18 | #include <libxml/entities.h> |
| 19 | #include <libxml/xmlerror.h> |
| 20 | #include <libxml/debugXML.h> |
| 21 | #include <libxml/xmlIO.h> |
| 22 | #include <libxml/SAX.h> |
| 23 | #include <libxml/uri.h> |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 24 | #include <libxml/valid.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 25 | #include <libxml/HTMLtree.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 26 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 27 | |
| 28 | /* #define DEBUG_SAX */ |
| 29 | /* #define DEBUG_SAX_TREE */ |
| 30 | |
| 31 | /** |
| 32 | * getPublicId: |
| 33 | * @ctx: the user data (XML parser context) |
| 34 | * |
| 35 | * Return the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN" |
| 36 | * |
| 37 | * Returns a xmlChar * |
| 38 | */ |
| 39 | const xmlChar * |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 40 | getPublicId(void *ctx ATTRIBUTE_UNUSED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 41 | { |
| 42 | /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */ |
| 43 | return(NULL); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * getSystemId: |
| 48 | * @ctx: the user data (XML parser context) |
| 49 | * |
| 50 | * Return the system ID, basically URL or filename e.g. |
| 51 | * http://www.sgmlsource.com/dtds/memo.dtd |
| 52 | * |
| 53 | * Returns a xmlChar * |
| 54 | */ |
| 55 | const xmlChar * |
| 56 | getSystemId(void *ctx) |
| 57 | { |
| 58 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 59 | return((const xmlChar *) ctxt->input->filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * getLineNumber: |
| 64 | * @ctx: the user data (XML parser context) |
| 65 | * |
| 66 | * Return the line number of the current parsing point. |
| 67 | * |
| 68 | * Returns an int |
| 69 | */ |
| 70 | int |
| 71 | getLineNumber(void *ctx) |
| 72 | { |
| 73 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 74 | return(ctxt->input->line); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * getColumnNumber: |
| 79 | * @ctx: the user data (XML parser context) |
| 80 | * |
| 81 | * Return the column number of the current parsing point. |
| 82 | * |
| 83 | * Returns an int |
| 84 | */ |
| 85 | int |
| 86 | getColumnNumber(void *ctx) |
| 87 | { |
| 88 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 89 | return(ctxt->input->col); |
| 90 | } |
| 91 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 92 | /** |
| 93 | * isStandalone: |
| 94 | * @ctx: the user data (XML parser context) |
| 95 | * |
| 96 | * Is this document tagged standalone ? |
| 97 | * |
| 98 | * Returns 1 if true |
| 99 | */ |
| 100 | int |
| 101 | isStandalone(void *ctx) |
| 102 | { |
| 103 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 104 | return(ctxt->myDoc->standalone == 1); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * hasInternalSubset: |
| 109 | * @ctx: the user data (XML parser context) |
| 110 | * |
| 111 | * Does this document has an internal subset |
| 112 | * |
| 113 | * Returns 1 if true |
| 114 | */ |
| 115 | int |
| 116 | hasInternalSubset(void *ctx) |
| 117 | { |
| 118 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 119 | return(ctxt->myDoc->intSubset != NULL); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * hasExternalSubset: |
| 124 | * @ctx: the user data (XML parser context) |
| 125 | * |
| 126 | * Does this document has an external subset |
| 127 | * |
| 128 | * Returns 1 if true |
| 129 | */ |
| 130 | int |
| 131 | hasExternalSubset(void *ctx) |
| 132 | { |
| 133 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 134 | return(ctxt->myDoc->extSubset != NULL); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * internalSubset: |
| 139 | * @ctx: the user data (XML parser context) |
| 140 | * @name: the root element name |
| 141 | * @ExternalID: the external ID |
| 142 | * @SystemID: the SYSTEM ID (e.g. filename or URL) |
| 143 | * |
| 144 | * Callback on internal subset declaration. |
| 145 | */ |
| 146 | void |
| 147 | internalSubset(void *ctx, const xmlChar *name, |
| 148 | const xmlChar *ExternalID, const xmlChar *SystemID) |
| 149 | { |
| 150 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 151 | xmlDtdPtr dtd; |
| 152 | #ifdef DEBUG_SAX |
| 153 | xmlGenericError(xmlGenericErrorContext, |
| 154 | "SAX.internalSubset(%s, %s, %s)\n", |
| 155 | name, ExternalID, SystemID); |
| 156 | #endif |
| 157 | |
| 158 | if (ctxt->myDoc == NULL) |
| 159 | return; |
| 160 | dtd = xmlGetIntSubset(ctxt->myDoc); |
| 161 | if (dtd != NULL) { |
| 162 | if (ctxt->html) |
| 163 | return; |
| 164 | xmlUnlinkNode((xmlNodePtr) dtd); |
| 165 | xmlFreeDtd(dtd); |
| 166 | ctxt->myDoc->intSubset = NULL; |
| 167 | } |
| 168 | ctxt->myDoc->intSubset = |
| 169 | xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * externalSubset: |
| 174 | * @ctx: the user data (XML parser context) |
| 175 | * @name: the root element name |
| 176 | * @ExternalID: the external ID |
| 177 | * @SystemID: the SYSTEM ID (e.g. filename or URL) |
| 178 | * |
| 179 | * Callback on external subset declaration. |
| 180 | */ |
| 181 | void |
| 182 | externalSubset(void *ctx, const xmlChar *name, |
| 183 | const xmlChar *ExternalID, const xmlChar *SystemID) |
| 184 | { |
| 185 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 186 | #ifdef DEBUG_SAX |
| 187 | xmlGenericError(xmlGenericErrorContext, |
| 188 | "SAX.externalSubset(%s, %s, %s)\n", |
| 189 | name, ExternalID, SystemID); |
| 190 | #endif |
| 191 | if (((ExternalID != NULL) || (SystemID != NULL)) && |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 192 | (((ctxt->validate) || (ctxt->loadsubset != 0)) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 193 | (ctxt->wellFormed && ctxt->myDoc))) { |
| 194 | /* |
| 195 | * Try to fetch and parse the external subset. |
| 196 | */ |
| 197 | xmlParserInputPtr oldinput; |
| 198 | int oldinputNr; |
| 199 | int oldinputMax; |
| 200 | xmlParserInputPtr *oldinputTab; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 201 | xmlParserInputPtr input = NULL; |
| 202 | xmlCharEncoding enc; |
| 203 | int oldcharset; |
| 204 | |
| 205 | /* |
| 206 | * Ask the Entity resolver to load the damn thing |
| 207 | */ |
| 208 | if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL)) |
| 209 | input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID, |
| 210 | SystemID); |
| 211 | if (input == NULL) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID); |
| 216 | |
| 217 | /* |
| 218 | * make sure we won't destroy the main document context |
| 219 | */ |
| 220 | oldinput = ctxt->input; |
| 221 | oldinputNr = ctxt->inputNr; |
| 222 | oldinputMax = ctxt->inputMax; |
| 223 | oldinputTab = ctxt->inputTab; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 224 | oldcharset = ctxt->charset; |
| 225 | |
| 226 | ctxt->inputTab = (xmlParserInputPtr *) |
| 227 | xmlMalloc(5 * sizeof(xmlParserInputPtr)); |
| 228 | if (ctxt->inputTab == NULL) { |
| 229 | ctxt->errNo = XML_ERR_NO_MEMORY; |
| 230 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 231 | ctxt->sax->error(ctxt->userData, |
| 232 | "externalSubset: out of memory\n"); |
| 233 | ctxt->errNo = XML_ERR_NO_MEMORY; |
| 234 | ctxt->input = oldinput; |
| 235 | ctxt->inputNr = oldinputNr; |
| 236 | ctxt->inputMax = oldinputMax; |
| 237 | ctxt->inputTab = oldinputTab; |
| 238 | ctxt->charset = oldcharset; |
| 239 | return; |
| 240 | } |
| 241 | ctxt->inputNr = 0; |
| 242 | ctxt->inputMax = 5; |
| 243 | ctxt->input = NULL; |
| 244 | xmlPushInput(ctxt, input); |
| 245 | |
| 246 | /* |
| 247 | * On the fly encoding conversion if needed |
| 248 | */ |
| 249 | enc = xmlDetectCharEncoding(ctxt->input->cur, 4); |
| 250 | xmlSwitchEncoding(ctxt, enc); |
| 251 | |
| 252 | if (input->filename == NULL) |
| 253 | input->filename = (char *) xmlStrdup(SystemID); |
| 254 | input->line = 1; |
| 255 | input->col = 1; |
| 256 | input->base = ctxt->input->cur; |
| 257 | input->cur = ctxt->input->cur; |
| 258 | input->free = NULL; |
| 259 | |
| 260 | /* |
| 261 | * let's parse that entity knowing it's an external subset. |
| 262 | */ |
| 263 | xmlParseExternalSubset(ctxt, ExternalID, SystemID); |
| 264 | |
| 265 | /* |
| 266 | * Free up the external entities |
| 267 | */ |
| 268 | |
| 269 | while (ctxt->inputNr > 1) |
| 270 | xmlPopInput(ctxt); |
| 271 | xmlFreeInputStream(ctxt->input); |
| 272 | xmlFree(ctxt->inputTab); |
| 273 | |
| 274 | /* |
| 275 | * Restore the parsing context of the main entity |
| 276 | */ |
| 277 | ctxt->input = oldinput; |
| 278 | ctxt->inputNr = oldinputNr; |
| 279 | ctxt->inputMax = oldinputMax; |
| 280 | ctxt->inputTab = oldinputTab; |
| 281 | ctxt->charset = oldcharset; |
| 282 | /* ctxt->wellFormed = oldwellFormed; */ |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * resolveEntity: |
| 288 | * @ctx: the user data (XML parser context) |
| 289 | * @publicId: The public ID of the entity |
| 290 | * @systemId: The system ID of the entity |
| 291 | * |
| 292 | * The entity loader, to control the loading of external entities, |
| 293 | * the application can either: |
| 294 | * - override this resolveEntity() callback in the SAX block |
| 295 | * - or better use the xmlSetExternalEntityLoader() function to |
| 296 | * set up it's own entity resolution routine |
| 297 | * |
| 298 | * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour. |
| 299 | */ |
| 300 | xmlParserInputPtr |
| 301 | resolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId) |
| 302 | { |
| 303 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 304 | xmlParserInputPtr ret; |
| 305 | xmlChar *URI; |
| 306 | const char *base = NULL; |
| 307 | |
| 308 | if (ctxt->input != NULL) |
| 309 | base = ctxt->input->filename; |
| 310 | if (base == NULL) |
| 311 | base = ctxt->directory; |
| 312 | |
| 313 | URI = xmlBuildURI(systemId, (const xmlChar *) base); |
| 314 | |
| 315 | #ifdef DEBUG_SAX |
| 316 | xmlGenericError(xmlGenericErrorContext, |
| 317 | "SAX.resolveEntity(%s, %s)\n", publicId, systemId); |
| 318 | #endif |
| 319 | |
| 320 | ret = xmlLoadExternalEntity((const char *) URI, |
| 321 | (const char *) publicId, ctxt); |
| 322 | if (URI != NULL) |
| 323 | xmlFree(URI); |
| 324 | return(ret); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * getEntity: |
| 329 | * @ctx: the user data (XML parser context) |
| 330 | * @name: The entity name |
| 331 | * |
| 332 | * Get an entity by name |
| 333 | * |
| 334 | * Returns the xmlEntityPtr if found. |
| 335 | */ |
| 336 | xmlEntityPtr |
| 337 | getEntity(void *ctx, const xmlChar *name) |
| 338 | { |
| 339 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 340 | xmlEntityPtr ret; |
| 341 | |
| 342 | #ifdef DEBUG_SAX |
| 343 | xmlGenericError(xmlGenericErrorContext, |
| 344 | "SAX.getEntity(%s)\n", name); |
| 345 | #endif |
| 346 | |
Daniel Veillard | 2875770 | 2002-02-18 11:19:30 +0000 | [diff] [blame] | 347 | if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) { |
| 348 | if (ctxt->inSubset == 2) { |
| 349 | ctxt->myDoc->standalone = 0; |
| 350 | ret = xmlGetDocEntity(ctxt->myDoc, name); |
| 351 | ctxt->myDoc->standalone = 1; |
| 352 | } else { |
| 353 | ret = xmlGetDocEntity(ctxt->myDoc, name); |
| 354 | if (ret == NULL) { |
| 355 | ctxt->myDoc->standalone = 0; |
| 356 | ret = xmlGetDocEntity(ctxt->myDoc, name); |
| 357 | if (ret != NULL) { |
| 358 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 359 | ctxt->sax->error(ctxt, |
| 360 | "Entity(%s) document marked standalone but require external subset\n", |
| 361 | name); |
| 362 | ctxt->valid = 0; |
| 363 | ctxt->wellFormed = 0; |
| 364 | } |
| 365 | ctxt->myDoc->standalone = 1; |
| 366 | } |
| 367 | } |
| 368 | } else { |
| 369 | ret = xmlGetDocEntity(ctxt->myDoc, name); |
| 370 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 371 | if ((ret != NULL) && (ctxt->validate) && (ret->children == NULL) && |
| 372 | (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 373 | /* |
| 374 | * for validation purposes we really need to fetch and |
| 375 | * parse the external entity |
| 376 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 377 | xmlNodePtr children; |
| 378 | |
Daniel Veillard | 5015b71 | 2001-08-17 09:37:52 +0000 | [diff] [blame] | 379 | xmlParseCtxtExternalEntity(ctxt, ret->URI, ret->ExternalID, &children); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 380 | xmlAddChildList((xmlNodePtr) ret, children); |
| 381 | } |
| 382 | return(ret); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * getParameterEntity: |
| 387 | * @ctx: the user data (XML parser context) |
| 388 | * @name: The entity name |
| 389 | * |
| 390 | * Get a parameter entity by name |
| 391 | * |
| 392 | * Returns the xmlEntityPtr if found. |
| 393 | */ |
| 394 | xmlEntityPtr |
| 395 | getParameterEntity(void *ctx, const xmlChar *name) |
| 396 | { |
| 397 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 398 | xmlEntityPtr ret; |
| 399 | |
| 400 | #ifdef DEBUG_SAX |
| 401 | xmlGenericError(xmlGenericErrorContext, |
| 402 | "SAX.getParameterEntity(%s)\n", name); |
| 403 | #endif |
| 404 | |
| 405 | ret = xmlGetParameterEntity(ctxt->myDoc, name); |
| 406 | return(ret); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /** |
| 411 | * entityDecl: |
| 412 | * @ctx: the user data (XML parser context) |
| 413 | * @name: the entity name |
| 414 | * @type: the entity type |
| 415 | * @publicId: The public ID of the entity |
| 416 | * @systemId: The system ID of the entity |
| 417 | * @content: the entity value (without processing). |
| 418 | * |
| 419 | * An entity definition has been parsed |
| 420 | */ |
| 421 | void |
| 422 | entityDecl(void *ctx, const xmlChar *name, int type, |
| 423 | const xmlChar *publicId, const xmlChar *systemId, xmlChar *content) |
| 424 | { |
| 425 | xmlEntityPtr ent; |
| 426 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 427 | |
| 428 | #ifdef DEBUG_SAX |
| 429 | xmlGenericError(xmlGenericErrorContext, |
| 430 | "SAX.entityDecl(%s, %d, %s, %s, %s)\n", |
| 431 | name, type, publicId, systemId, content); |
| 432 | #endif |
| 433 | if (ctxt->inSubset == 1) { |
| 434 | ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId, |
| 435 | systemId, content); |
| 436 | if ((ent == NULL) && (ctxt->pedantic) && |
| 437 | (ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 438 | ctxt->sax->warning(ctxt, |
| 439 | "Entity(%s) already defined in the internal subset\n", name); |
| 440 | if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) { |
| 441 | xmlChar *URI; |
| 442 | const char *base = NULL; |
| 443 | |
| 444 | if (ctxt->input != NULL) |
| 445 | base = ctxt->input->filename; |
| 446 | if (base == NULL) |
| 447 | base = ctxt->directory; |
| 448 | |
| 449 | URI = xmlBuildURI(systemId, (const xmlChar *) base); |
| 450 | ent->URI = URI; |
| 451 | } |
| 452 | } else if (ctxt->inSubset == 2) { |
| 453 | ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId, |
| 454 | systemId, content); |
| 455 | if ((ent == NULL) && (ctxt->pedantic) && |
| 456 | (ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 457 | ctxt->sax->warning(ctxt, |
| 458 | "Entity(%s) already defined in the external subset\n", name); |
| 459 | if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) { |
| 460 | xmlChar *URI; |
| 461 | const char *base = NULL; |
| 462 | |
| 463 | if (ctxt->input != NULL) |
| 464 | base = ctxt->input->filename; |
| 465 | if (base == NULL) |
| 466 | base = ctxt->directory; |
| 467 | |
| 468 | URI = xmlBuildURI(systemId, (const xmlChar *) base); |
| 469 | ent->URI = URI; |
| 470 | } |
| 471 | } else { |
| 472 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 473 | ctxt->sax->error(ctxt, |
| 474 | "SAX.entityDecl(%s) called while not in subset\n", name); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * attributeDecl: |
| 480 | * @ctx: the user data (XML parser context) |
| 481 | * @elem: the name of the element |
| 482 | * @fullname: the attribute name |
| 483 | * @type: the attribute type |
| 484 | * @def: the type of default value |
| 485 | * @defaultValue: the attribute default value |
| 486 | * @tree: the tree of enumerated value set |
| 487 | * |
| 488 | * An attribute definition has been parsed |
| 489 | */ |
| 490 | void |
| 491 | attributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname, |
| 492 | int type, int def, const xmlChar *defaultValue, |
| 493 | xmlEnumerationPtr tree) |
| 494 | { |
| 495 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 496 | xmlAttributePtr attr; |
| 497 | xmlChar *name = NULL, *prefix = NULL; |
| 498 | |
| 499 | #ifdef DEBUG_SAX |
| 500 | xmlGenericError(xmlGenericErrorContext, |
| 501 | "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n", |
| 502 | elem, fullname, type, def, defaultValue); |
| 503 | #endif |
| 504 | name = xmlSplitQName(ctxt, fullname, &prefix); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 505 | ctxt->vctxt.valid = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 506 | if (ctxt->inSubset == 1) |
| 507 | attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem, |
| 508 | name, prefix, (xmlAttributeType) type, |
| 509 | (xmlAttributeDefault) def, defaultValue, tree); |
| 510 | else if (ctxt->inSubset == 2) |
| 511 | attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem, |
| 512 | name, prefix, (xmlAttributeType) type, |
| 513 | (xmlAttributeDefault) def, defaultValue, tree); |
| 514 | else { |
| 515 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 516 | ctxt->sax->error(ctxt, |
| 517 | "SAX.attributeDecl(%s) called while not in subset\n", name); |
| 518 | return; |
| 519 | } |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 520 | if (ctxt->vctxt.valid == 0) |
| 521 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 522 | if (ctxt->validate && ctxt->wellFormed && |
| 523 | ctxt->myDoc && ctxt->myDoc->intSubset) |
| 524 | ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc, |
| 525 | attr); |
| 526 | if (prefix != NULL) |
| 527 | xmlFree(prefix); |
| 528 | if (name != NULL) |
| 529 | xmlFree(name); |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * elementDecl: |
| 534 | * @ctx: the user data (XML parser context) |
| 535 | * @name: the element name |
| 536 | * @type: the element type |
| 537 | * @content: the element value tree |
| 538 | * |
| 539 | * An element definition has been parsed |
| 540 | */ |
| 541 | void |
Daniel Veillard | 1fd36d2 | 2001-07-04 22:54:28 +0000 | [diff] [blame] | 542 | elementDecl(void *ctx, const xmlChar * name, int type, |
| 543 | xmlElementContentPtr content) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 544 | { |
| 545 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 546 | xmlElementPtr elem = NULL; |
| 547 | |
| 548 | #ifdef DEBUG_SAX |
| 549 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 1fd36d2 | 2001-07-04 22:54:28 +0000 | [diff] [blame] | 550 | "SAX.elementDecl(%s, %d, ...)\n", name, type); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 551 | #endif |
Daniel Veillard | 1fd36d2 | 2001-07-04 22:54:28 +0000 | [diff] [blame] | 552 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 553 | if (ctxt->inSubset == 1) |
Daniel Veillard | 1fd36d2 | 2001-07-04 22:54:28 +0000 | [diff] [blame] | 554 | elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, |
| 555 | name, (xmlElementTypeVal) type, content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 556 | else if (ctxt->inSubset == 2) |
Daniel Veillard | 1fd36d2 | 2001-07-04 22:54:28 +0000 | [diff] [blame] | 557 | elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, |
| 558 | name, (xmlElementTypeVal) type, content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 559 | else { |
Daniel Veillard | 1fd36d2 | 2001-07-04 22:54:28 +0000 | [diff] [blame] | 560 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 561 | ctxt->sax->error(ctxt, |
| 562 | "SAX.elementDecl(%s) called while not in subset\n", |
| 563 | name); |
| 564 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 565 | } |
Daniel Veillard | 1fd36d2 | 2001-07-04 22:54:28 +0000 | [diff] [blame] | 566 | if (elem == NULL) |
| 567 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 568 | if (ctxt->validate && ctxt->wellFormed && |
| 569 | ctxt->myDoc && ctxt->myDoc->intSubset) |
Daniel Veillard | 1fd36d2 | 2001-07-04 22:54:28 +0000 | [diff] [blame] | 570 | ctxt->valid &= |
| 571 | xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /** |
| 575 | * notationDecl: |
| 576 | * @ctx: the user data (XML parser context) |
| 577 | * @name: The name of the notation |
| 578 | * @publicId: The public ID of the entity |
| 579 | * @systemId: The system ID of the entity |
| 580 | * |
| 581 | * What to do when a notation declaration has been parsed. |
| 582 | */ |
| 583 | void |
| 584 | notationDecl(void *ctx, const xmlChar *name, |
| 585 | const xmlChar *publicId, const xmlChar *systemId) |
| 586 | { |
| 587 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 588 | xmlNotationPtr nota = NULL; |
| 589 | |
| 590 | #ifdef DEBUG_SAX |
| 591 | xmlGenericError(xmlGenericErrorContext, |
| 592 | "SAX.notationDecl(%s, %s, %s)\n", name, publicId, systemId); |
| 593 | #endif |
| 594 | |
Daniel Veillard | 7aea52d | 2002-02-17 23:07:47 +0000 | [diff] [blame] | 595 | if ((publicId == NULL) && (systemId == NULL)) { |
| 596 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 597 | ctxt->sax->error(ctxt, |
| 598 | "SAX.notationDecl(%s) externalID or PublicID missing\n", name); |
| 599 | ctxt->valid = 0; |
| 600 | ctxt->wellFormed = 0; |
| 601 | return; |
| 602 | } else if (ctxt->inSubset == 1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 603 | nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name, |
| 604 | publicId, systemId); |
| 605 | else if (ctxt->inSubset == 2) |
Daniel Veillard | 25239c1 | 2001-03-14 13:56:48 +0000 | [diff] [blame] | 606 | nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 607 | publicId, systemId); |
| 608 | else { |
| 609 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 610 | ctxt->sax->error(ctxt, |
| 611 | "SAX.notationDecl(%s) called while not in subset\n", name); |
| 612 | return; |
| 613 | } |
| 614 | if (nota == NULL) ctxt->valid = 0; |
| 615 | if (ctxt->validate && ctxt->wellFormed && |
| 616 | ctxt->myDoc && ctxt->myDoc->intSubset) |
| 617 | ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc, |
| 618 | nota); |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * unparsedEntityDecl: |
| 623 | * @ctx: the user data (XML parser context) |
| 624 | * @name: The name of the entity |
| 625 | * @publicId: The public ID of the entity |
| 626 | * @systemId: The system ID of the entity |
| 627 | * @notationName: the name of the notation |
| 628 | * |
| 629 | * What to do when an unparsed entity declaration is parsed |
| 630 | */ |
| 631 | void |
| 632 | unparsedEntityDecl(void *ctx, const xmlChar *name, |
| 633 | const xmlChar *publicId, const xmlChar *systemId, |
| 634 | const xmlChar *notationName) |
| 635 | { |
Daniel Veillard | 9f4eb91 | 2001-08-01 21:22:27 +0000 | [diff] [blame] | 636 | xmlEntityPtr ent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 637 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 638 | #ifdef DEBUG_SAX |
| 639 | xmlGenericError(xmlGenericErrorContext, |
| 640 | "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n", |
| 641 | name, publicId, systemId, notationName); |
| 642 | #endif |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 643 | #if 0 |
| 644 | Done in xmlValidateDtdFinal now. |
Daniel Veillard | 2875770 | 2002-02-18 11:19:30 +0000 | [diff] [blame] | 645 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc) { |
| 646 | int ret; |
| 647 | ret = xmlValidateNotationUse(&ctxt->vctxt, ctxt->myDoc, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 648 | notationName); |
Daniel Veillard | 2875770 | 2002-02-18 11:19:30 +0000 | [diff] [blame] | 649 | if (ret == 0) { |
| 650 | ctxt->wellFormed = 0; |
| 651 | ctxt->valid = 0; |
| 652 | } |
| 653 | } |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 654 | #endif |
Daniel Veillard | 9f4eb91 | 2001-08-01 21:22:27 +0000 | [diff] [blame] | 655 | if (ctxt->inSubset == 1) { |
| 656 | ent = xmlAddDocEntity(ctxt->myDoc, name, |
Daniel Veillard | e020c3a | 2001-03-21 18:06:15 +0000 | [diff] [blame] | 657 | XML_EXTERNAL_GENERAL_UNPARSED_ENTITY, |
| 658 | publicId, systemId, notationName); |
Daniel Veillard | 9f4eb91 | 2001-08-01 21:22:27 +0000 | [diff] [blame] | 659 | if ((ent == NULL) && (ctxt->pedantic) && |
| 660 | (ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 661 | ctxt->sax->warning(ctxt, |
| 662 | "Entity(%s) already defined in the internal subset\n", name); |
| 663 | if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) { |
| 664 | xmlChar *URI; |
| 665 | const char *base = NULL; |
| 666 | |
| 667 | if (ctxt->input != NULL) |
| 668 | base = ctxt->input->filename; |
| 669 | if (base == NULL) |
| 670 | base = ctxt->directory; |
| 671 | |
| 672 | URI = xmlBuildURI(systemId, (const xmlChar *) base); |
| 673 | ent->URI = URI; |
| 674 | } |
| 675 | } else if (ctxt->inSubset == 2) { |
| 676 | ent = xmlAddDtdEntity(ctxt->myDoc, name, |
Daniel Veillard | e020c3a | 2001-03-21 18:06:15 +0000 | [diff] [blame] | 677 | XML_EXTERNAL_GENERAL_UNPARSED_ENTITY, |
| 678 | publicId, systemId, notationName); |
Daniel Veillard | 9f4eb91 | 2001-08-01 21:22:27 +0000 | [diff] [blame] | 679 | if ((ent == NULL) && (ctxt->pedantic) && |
| 680 | (ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 681 | ctxt->sax->warning(ctxt, |
| 682 | "Entity(%s) already defined in the external subset\n", name); |
| 683 | if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) { |
| 684 | xmlChar *URI; |
| 685 | const char *base = NULL; |
| 686 | |
| 687 | if (ctxt->input != NULL) |
| 688 | base = ctxt->input->filename; |
| 689 | if (base == NULL) |
| 690 | base = ctxt->directory; |
| 691 | |
| 692 | URI = xmlBuildURI(systemId, (const xmlChar *) base); |
| 693 | ent->URI = URI; |
| 694 | } |
| 695 | } else { |
Daniel Veillard | e020c3a | 2001-03-21 18:06:15 +0000 | [diff] [blame] | 696 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 697 | ctxt->sax->error(ctxt, |
| 698 | "SAX.unparsedEntityDecl(%s) called while not in subset\n", name); |
| 699 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /** |
| 703 | * setDocumentLocator: |
| 704 | * @ctx: the user data (XML parser context) |
| 705 | * @loc: A SAX Locator |
| 706 | * |
| 707 | * Receive the document locator at startup, actually xmlDefaultSAXLocator |
| 708 | * Everything is available on the context, so this is useless in our case. |
| 709 | */ |
| 710 | void |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 711 | setDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 712 | { |
| 713 | /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */ |
| 714 | #ifdef DEBUG_SAX |
| 715 | xmlGenericError(xmlGenericErrorContext, |
| 716 | "SAX.setDocumentLocator()\n"); |
| 717 | #endif |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * startDocument: |
| 722 | * @ctx: the user data (XML parser context) |
| 723 | * |
| 724 | * called when the document start being processed. |
| 725 | */ |
| 726 | void |
| 727 | startDocument(void *ctx) |
| 728 | { |
| 729 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 730 | xmlDocPtr doc; |
| 731 | |
| 732 | #ifdef DEBUG_SAX |
| 733 | xmlGenericError(xmlGenericErrorContext, |
| 734 | "SAX.startDocument()\n"); |
| 735 | #endif |
| 736 | if (ctxt->html) { |
| 737 | if (ctxt->myDoc == NULL) |
| 738 | #ifdef LIBXML_HTML_ENABLED |
| 739 | ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL); |
| 740 | #else |
| 741 | xmlGenericError(xmlGenericErrorContext, |
| 742 | "libxml2 built without HTML support\n"); |
| 743 | #endif |
| 744 | } else { |
| 745 | doc = ctxt->myDoc = xmlNewDoc(ctxt->version); |
| 746 | if (doc != NULL) { |
| 747 | if (ctxt->encoding != NULL) |
| 748 | doc->encoding = xmlStrdup(ctxt->encoding); |
| 749 | else |
| 750 | doc->encoding = NULL; |
| 751 | doc->standalone = ctxt->standalone; |
| 752 | } |
| 753 | } |
| 754 | if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) && |
| 755 | (ctxt->input != NULL) && (ctxt->input->filename != NULL)) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 756 | ctxt->myDoc->URL = xmlStrdup((const xmlChar *) ctxt->input->filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * endDocument: |
| 762 | * @ctx: the user data (XML parser context) |
| 763 | * |
| 764 | * called when the document end has been detected. |
| 765 | */ |
| 766 | void |
| 767 | endDocument(void *ctx) |
| 768 | { |
| 769 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 770 | #ifdef DEBUG_SAX |
| 771 | xmlGenericError(xmlGenericErrorContext, |
| 772 | "SAX.endDocument()\n"); |
| 773 | #endif |
| 774 | if (ctxt->validate && ctxt->wellFormed && |
| 775 | ctxt->myDoc && ctxt->myDoc->intSubset) |
| 776 | ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc); |
| 777 | |
| 778 | /* |
| 779 | * Grab the encoding if it was added on-the-fly |
| 780 | */ |
| 781 | if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) && |
| 782 | (ctxt->myDoc->encoding == NULL)) { |
| 783 | ctxt->myDoc->encoding = ctxt->encoding; |
| 784 | ctxt->encoding = NULL; |
| 785 | } |
| 786 | if ((ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) && |
| 787 | (ctxt->myDoc->encoding == NULL)) { |
| 788 | ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding); |
| 789 | } |
| 790 | if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) && |
| 791 | (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) { |
| 792 | ctxt->myDoc->charset = ctxt->charset; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * attribute: |
| 798 | * @ctx: the user data (XML parser context) |
| 799 | * @fullname: The attribute name, including namespace prefix |
| 800 | * @value: The attribute value |
| 801 | * |
| 802 | * Handle an attribute that has been read by the parser. |
| 803 | * The default handling is to convert the attribute into an |
| 804 | * DOM subtree and past it in a new xmlAttr element added to |
| 805 | * the element. |
| 806 | */ |
| 807 | void |
| 808 | attribute(void *ctx, const xmlChar *fullname, const xmlChar *value) |
| 809 | { |
| 810 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 811 | xmlAttrPtr ret; |
| 812 | xmlChar *name; |
| 813 | xmlChar *ns; |
| 814 | xmlChar *nval; |
| 815 | xmlNsPtr namespace; |
| 816 | |
| 817 | /**************** |
| 818 | #ifdef DEBUG_SAX |
| 819 | xmlGenericError(xmlGenericErrorContext, |
| 820 | "SAX.attribute(%s, %s)\n", fullname, value); |
| 821 | #endif |
| 822 | ****************/ |
| 823 | /* |
| 824 | * Split the full name into a namespace prefix and the tag name |
| 825 | */ |
| 826 | name = xmlSplitQName(ctxt, fullname, &ns); |
| 827 | |
| 828 | /* |
| 829 | * Do the last stage of the attribute normalization |
| 830 | * Needed for HTML too: |
| 831 | * http://www.w3.org/TR/html4/types.html#h-6.2 |
| 832 | */ |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 833 | ctxt->vctxt.valid = 1; |
| 834 | nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt, |
| 835 | ctxt->myDoc, ctxt->node, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 836 | fullname, value); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 837 | if (ctxt->vctxt.valid != 1) { |
| 838 | ctxt->valid = 0; |
| 839 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 840 | if (nval != NULL) |
| 841 | value = nval; |
| 842 | |
| 843 | /* |
| 844 | * Check whether it's a namespace definition |
| 845 | */ |
| 846 | if ((!ctxt->html) && (ns == NULL) && |
| 847 | (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') && |
| 848 | (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) { |
| 849 | if (value[0] != 0) { |
| 850 | xmlURIPtr uri; |
| 851 | |
| 852 | uri = xmlParseURI((const char *)value); |
| 853 | if (uri == NULL) { |
| 854 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 855 | ctxt->sax->warning(ctxt->userData, |
| 856 | "nmlns: %s not a valid URI\n", value); |
| 857 | } else { |
| 858 | if (uri->scheme == NULL) { |
| 859 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 860 | ctxt->sax->warning(ctxt->userData, |
| 861 | "nmlns: URI %s is not absolute\n", value); |
| 862 | } |
| 863 | xmlFreeURI(uri); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | /* a default namespace definition */ |
| 868 | xmlNewNs(ctxt->node, value, NULL); |
| 869 | if (name != NULL) |
| 870 | xmlFree(name); |
| 871 | if (nval != NULL) |
| 872 | xmlFree(nval); |
| 873 | return; |
| 874 | } |
| 875 | if ((!ctxt->html) && |
| 876 | (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') && |
| 877 | (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) { |
| 878 | /* |
| 879 | * Validate also for namespace decls, they are attributes from |
| 880 | * an XML-1.0 perspective |
| 881 | TODO ... doesn't map well with current API |
| 882 | if (ctxt->validate && ctxt->wellFormed && |
| 883 | ctxt->myDoc && ctxt->myDoc->intSubset) |
| 884 | ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc, |
| 885 | ctxt->node, ret, value); |
| 886 | */ |
| 887 | /* a standard namespace definition */ |
| 888 | xmlNewNs(ctxt->node, value, name); |
| 889 | xmlFree(ns); |
| 890 | if (name != NULL) |
| 891 | xmlFree(name); |
| 892 | if (nval != NULL) |
| 893 | xmlFree(nval); |
| 894 | return; |
| 895 | } |
| 896 | |
| 897 | if (ns != NULL) |
| 898 | namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns); |
| 899 | else { |
| 900 | namespace = NULL; |
| 901 | } |
| 902 | |
| 903 | /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */ |
| 904 | ret = xmlNewNsProp(ctxt->node, namespace, name, NULL); |
| 905 | |
| 906 | if (ret != NULL) { |
| 907 | if ((ctxt->replaceEntities == 0) && (!ctxt->html)) { |
| 908 | xmlNodePtr tmp; |
| 909 | |
| 910 | ret->children = xmlStringGetNodeList(ctxt->myDoc, value); |
| 911 | tmp = ret->children; |
| 912 | while (tmp != NULL) { |
| 913 | tmp->parent = (xmlNodePtr) ret; |
| 914 | if (tmp->next == NULL) |
| 915 | ret->last = tmp; |
| 916 | tmp = tmp->next; |
| 917 | } |
| 918 | } else if (value != NULL) { |
| 919 | ret->children = xmlNewDocText(ctxt->myDoc, value); |
| 920 | ret->last = ret->children; |
| 921 | if (ret->children != NULL) |
| 922 | ret->children->parent = (xmlNodePtr) ret; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed && |
| 927 | ctxt->myDoc && ctxt->myDoc->intSubset) { |
| 928 | |
| 929 | /* |
| 930 | * If we don't substitute entities, the validation should be |
| 931 | * done on a value with replaced entities anyway. |
| 932 | */ |
| 933 | if (!ctxt->replaceEntities) { |
| 934 | xmlChar *val; |
| 935 | |
| 936 | ctxt->depth++; |
| 937 | val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF, |
| 938 | 0,0,0); |
| 939 | ctxt->depth--; |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 940 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 941 | if (val == NULL) |
| 942 | ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, |
| 943 | ctxt->myDoc, ctxt->node, ret, value); |
| 944 | else { |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 945 | xmlChar *nvalnorm; |
| 946 | |
| 947 | /* |
| 948 | * Do the last stage of the attribute normalization |
| 949 | * It need to be done twice ... it's an extra burden related |
| 950 | * to the ability to keep references in attributes |
| 951 | */ |
| 952 | nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc, |
| 953 | ctxt->node, fullname, val); |
| 954 | if (nvalnorm != NULL) { |
| 955 | xmlFree(val); |
| 956 | val = nvalnorm; |
| 957 | } |
| 958 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 959 | ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, |
| 960 | ctxt->myDoc, ctxt->node, ret, val); |
| 961 | xmlFree(val); |
| 962 | } |
| 963 | } else { |
| 964 | ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc, |
| 965 | ctxt->node, ret, value); |
| 966 | } |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 967 | } else if (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) || |
| 968 | ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 969 | /* |
| 970 | * when validating, the ID registration is done at the attribute |
| 971 | * validation level. Otherwise we have to do specific handling here. |
| 972 | */ |
| 973 | if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) |
| 974 | xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret); |
| 975 | else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) |
| 976 | xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret); |
| 977 | } |
| 978 | |
| 979 | if (nval != NULL) |
| 980 | xmlFree(nval); |
| 981 | if (name != NULL) |
| 982 | xmlFree(name); |
| 983 | if (ns != NULL) |
| 984 | xmlFree(ns); |
| 985 | } |
| 986 | |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 987 | /* |
| 988 | * xmlCheckDefaultedAttributes: |
| 989 | * |
| 990 | * Check defaulted attributes from the DTD |
| 991 | */ |
| 992 | static void |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 993 | xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name, |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 994 | const xmlChar *prefix, const xmlChar **atts) { |
| 995 | xmlElementPtr elemDecl; |
| 996 | const xmlChar *att; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 997 | int internal = 1; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 998 | int i; |
| 999 | |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 1000 | elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix); |
| 1001 | if (elemDecl == NULL) { |
| 1002 | elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix); |
| 1003 | internal = 0; |
| 1004 | } |
| 1005 | |
| 1006 | process_external_subset: |
| 1007 | |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1008 | if (elemDecl != NULL) { |
| 1009 | xmlAttributePtr attr = elemDecl->attributes; |
| 1010 | /* |
| 1011 | * Check against defaulted attributes from the external subset |
| 1012 | * if the document is stamped as standalone |
| 1013 | */ |
| 1014 | if ((ctxt->myDoc->standalone == 1) && |
| 1015 | (ctxt->myDoc->extSubset != NULL) && |
| 1016 | (ctxt->validate)) { |
| 1017 | while (attr != NULL) { |
| 1018 | if ((attr->defaultValue != NULL) && |
| 1019 | (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset, |
| 1020 | attr->elem, attr->name, |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 1021 | attr->prefix) == attr) && |
| 1022 | (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset, |
| 1023 | attr->elem, attr->name, |
| 1024 | attr->prefix) == NULL)) { |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1025 | xmlChar *fulln; |
| 1026 | |
| 1027 | if (attr->prefix != NULL) { |
| 1028 | fulln = xmlStrdup(attr->prefix); |
| 1029 | fulln = xmlStrcat(fulln, BAD_CAST ":"); |
| 1030 | fulln = xmlStrcat(fulln, attr->name); |
| 1031 | } else { |
| 1032 | fulln = xmlStrdup(attr->name); |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * Check that the attribute is not declared in the |
| 1037 | * serialization |
| 1038 | */ |
| 1039 | att = NULL; |
| 1040 | if (atts != NULL) { |
| 1041 | i = 0; |
| 1042 | att = atts[i]; |
| 1043 | while (att != NULL) { |
| 1044 | if (xmlStrEqual(att, fulln)) |
| 1045 | break; |
| 1046 | i += 2; |
| 1047 | att = atts[i]; |
| 1048 | } |
| 1049 | } |
| 1050 | if (att == NULL) { |
| 1051 | if (ctxt->vctxt.error != NULL) |
| 1052 | ctxt->vctxt.error(ctxt->vctxt.userData, |
| 1053 | "standalone: attribute %s on %s defaulted from external subset\n", |
| 1054 | fulln, attr->elem); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1055 | ctxt->valid = 0; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1056 | } |
| 1057 | } |
| 1058 | attr = attr->nexth; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | * Actually insert defaulted values when needed |
| 1064 | */ |
| 1065 | attr = elemDecl->attributes; |
| 1066 | while (attr != NULL) { |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 1067 | /* |
| 1068 | * Make sure that attributes redefinition occuring in the |
| 1069 | * internal subset are not overriden by definitions in the |
| 1070 | * external subset. |
| 1071 | */ |
Daniel Veillard | 8aff247 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1072 | if (attr->defaultValue != NULL) { |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1073 | /* |
| 1074 | * the element should be instantiated in the tree if: |
| 1075 | * - this is a namespace prefix |
| 1076 | * - the user required for completion in the tree |
| 1077 | * like XSLT |
Daniel Veillard | 8aff247 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1078 | * - there isn't already an attribute definition |
| 1079 | * in the internal subset overriding it. |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1080 | */ |
| 1081 | if (((attr->prefix != NULL) && |
| 1082 | (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) || |
| 1083 | ((attr->prefix == NULL) && |
| 1084 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) || |
| 1085 | (ctxt->loadsubset & XML_COMPLETE_ATTRS)) { |
Daniel Veillard | 8aff247 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1086 | xmlAttributePtr tst; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1087 | |
Daniel Veillard | 8aff247 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1088 | tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset, |
| 1089 | attr->elem, attr->name, |
| 1090 | attr->prefix); |
| 1091 | if ((tst == attr) || (tst == NULL)) { |
| 1092 | xmlChar *fulln; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1093 | |
Daniel Veillard | 8aff247 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1094 | if (attr->prefix != NULL) { |
| 1095 | fulln = xmlStrdup(attr->prefix); |
| 1096 | fulln = xmlStrcat(fulln, BAD_CAST ":"); |
| 1097 | fulln = xmlStrcat(fulln, attr->name); |
| 1098 | } else { |
| 1099 | fulln = xmlStrdup(attr->name); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1100 | } |
Daniel Veillard | 8aff247 | 2002-02-19 21:50:43 +0000 | [diff] [blame] | 1101 | |
| 1102 | /* |
| 1103 | * Check that the attribute is not declared in the |
| 1104 | * serialization |
| 1105 | */ |
| 1106 | att = NULL; |
| 1107 | if (atts != NULL) { |
| 1108 | i = 0; |
| 1109 | att = atts[i]; |
| 1110 | while (att != NULL) { |
| 1111 | if (xmlStrEqual(att, fulln)) |
| 1112 | break; |
| 1113 | i += 2; |
| 1114 | att = atts[i]; |
| 1115 | } |
| 1116 | } |
| 1117 | if (att == NULL) { |
| 1118 | attribute(ctxt, fulln, attr->defaultValue); |
| 1119 | } |
| 1120 | xmlFree(fulln); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1121 | } |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | attr = attr->nexth; |
| 1125 | } |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 1126 | if (internal == 1) { |
| 1127 | elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, |
| 1128 | name, prefix); |
| 1129 | internal = 0; |
| 1130 | goto process_external_subset; |
| 1131 | } |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1135 | /** |
| 1136 | * startElement: |
| 1137 | * @ctx: the user data (XML parser context) |
| 1138 | * @fullname: The element name, including namespace prefix |
| 1139 | * @atts: An array of name/value attributes pairs, NULL terminated |
| 1140 | * |
| 1141 | * called when an opening tag has been processed. |
| 1142 | */ |
| 1143 | void |
| 1144 | startElement(void *ctx, const xmlChar *fullname, const xmlChar **atts) |
| 1145 | { |
| 1146 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 1147 | xmlNodePtr ret; |
| 1148 | xmlNodePtr parent = ctxt->node; |
| 1149 | xmlNsPtr ns; |
| 1150 | xmlChar *name; |
| 1151 | xmlChar *prefix; |
| 1152 | const xmlChar *att; |
| 1153 | const xmlChar *value; |
| 1154 | int i; |
| 1155 | |
| 1156 | #ifdef DEBUG_SAX |
| 1157 | xmlGenericError(xmlGenericErrorContext, |
| 1158 | "SAX.startElement(%s)\n", fullname); |
| 1159 | #endif |
| 1160 | |
| 1161 | /* |
| 1162 | * First check on validity: |
| 1163 | */ |
| 1164 | if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) && |
| 1165 | ((ctxt->myDoc->intSubset == NULL) || |
| 1166 | ((ctxt->myDoc->intSubset->notations == NULL) && |
| 1167 | (ctxt->myDoc->intSubset->elements == NULL) && |
| 1168 | (ctxt->myDoc->intSubset->attributes == NULL) && |
| 1169 | (ctxt->myDoc->intSubset->entities == NULL)))) { |
| 1170 | if (ctxt->vctxt.error != NULL) { |
| 1171 | ctxt->vctxt.error(ctxt->vctxt.userData, |
| 1172 | "Validation failed: no DTD found !\n"); |
| 1173 | } |
| 1174 | ctxt->validate = 0; |
Daniel Veillard | 7a51d6d | 2001-09-10 14:40:43 +0000 | [diff] [blame] | 1175 | ctxt->valid = 0; |
| 1176 | ctxt->errNo = XML_ERR_NO_DTD; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | |
| 1180 | /* |
| 1181 | * Split the full name into a namespace prefix and the tag name |
| 1182 | */ |
| 1183 | name = xmlSplitQName(ctxt, fullname, &prefix); |
| 1184 | |
| 1185 | |
| 1186 | /* |
| 1187 | * Note : the namespace resolution is deferred until the end of the |
| 1188 | * attributes parsing, since local namespace can be defined as |
| 1189 | * an attribute at this level. |
| 1190 | */ |
| 1191 | ret = xmlNewDocNode(ctxt->myDoc, NULL, name, NULL); |
| 1192 | if (ret == NULL) return; |
| 1193 | if (ctxt->myDoc->children == NULL) { |
| 1194 | #ifdef DEBUG_SAX_TREE |
| 1195 | xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name); |
| 1196 | #endif |
| 1197 | xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret); |
| 1198 | } else if (parent == NULL) { |
| 1199 | parent = ctxt->myDoc->children; |
| 1200 | } |
| 1201 | ctxt->nodemem = -1; |
Daniel Veillard | d9bad13 | 2001-07-23 19:39:43 +0000 | [diff] [blame] | 1202 | if (ctxt->linenumbers) { |
| 1203 | if (ctxt->input != NULL) |
| 1204 | ret->content = (void *) (long) ctxt->input->line; |
| 1205 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1206 | |
| 1207 | /* |
| 1208 | * We are parsing a new node. |
| 1209 | */ |
| 1210 | #ifdef DEBUG_SAX_TREE |
| 1211 | xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name); |
| 1212 | #endif |
| 1213 | nodePush(ctxt, ret); |
| 1214 | |
| 1215 | /* |
| 1216 | * Link the child element |
| 1217 | */ |
| 1218 | if (parent != NULL) { |
| 1219 | if (parent->type == XML_ELEMENT_NODE) { |
| 1220 | #ifdef DEBUG_SAX_TREE |
| 1221 | xmlGenericError(xmlGenericErrorContext, |
| 1222 | "adding child %s to %s\n", name, parent->name); |
| 1223 | #endif |
| 1224 | xmlAddChild(parent, ret); |
| 1225 | } else { |
| 1226 | #ifdef DEBUG_SAX_TREE |
| 1227 | xmlGenericError(xmlGenericErrorContext, |
| 1228 | "adding sibling %s to ", name); |
| 1229 | xmlDebugDumpOneNode(stderr, parent, 0); |
| 1230 | #endif |
| 1231 | xmlAddSibling(parent, ret); |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | /* |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1236 | * Insert all the defaulted attributes from the DTD especially namespaces |
| 1237 | */ |
| 1238 | if ((!ctxt->html) && |
| 1239 | ((ctxt->myDoc->intSubset != NULL) || |
| 1240 | (ctxt->myDoc->extSubset != NULL))) { |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 1241 | xmlCheckDefaultedAttributes(ctxt, name, prefix, atts); |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1245 | * process all the attributes whose name start with "xml" |
| 1246 | */ |
| 1247 | if (atts != NULL) { |
| 1248 | i = 0; |
| 1249 | att = atts[i++]; |
| 1250 | value = atts[i++]; |
| 1251 | if (!ctxt->html) { |
| 1252 | while ((att != NULL) && (value != NULL)) { |
| 1253 | if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l')) |
| 1254 | attribute(ctxt, att, value); |
| 1255 | |
| 1256 | att = atts[i++]; |
| 1257 | value = atts[i++]; |
| 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | * Search the namespace, note that since the attributes have been |
| 1264 | * processed, the local namespaces are available. |
| 1265 | */ |
| 1266 | ns = xmlSearchNs(ctxt->myDoc, ret, prefix); |
| 1267 | if ((ns == NULL) && (parent != NULL)) |
| 1268 | ns = xmlSearchNs(ctxt->myDoc, parent, prefix); |
| 1269 | if ((prefix != NULL) && (ns == NULL)) { |
| 1270 | ns = xmlNewNs(ret, NULL, prefix); |
| 1271 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 1272 | ctxt->sax->warning(ctxt->userData, |
| 1273 | "Namespace prefix %s is not defined\n", prefix); |
| 1274 | } |
Daniel Veillard | 143b04f | 2001-09-10 18:14:14 +0000 | [diff] [blame] | 1275 | |
| 1276 | /* |
| 1277 | * set the namespace node, making sure that if the default namspace |
| 1278 | * is unbound on a parent we simply kee it NULL |
| 1279 | */ |
Daniel Veillard | 651f947 | 2001-10-04 14:51:06 +0000 | [diff] [blame] | 1280 | if ((ns != NULL) && (ns->href != NULL) && (ns->href[0] != 0)) |
Daniel Veillard | 143b04f | 2001-09-10 18:14:14 +0000 | [diff] [blame] | 1281 | xmlSetNs(ret, ns); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1282 | |
| 1283 | /* |
| 1284 | * process all the other attributes |
| 1285 | */ |
| 1286 | if (atts != NULL) { |
| 1287 | i = 0; |
| 1288 | att = atts[i++]; |
| 1289 | value = atts[i++]; |
| 1290 | if (ctxt->html) { |
| 1291 | while (att != NULL) { |
| 1292 | attribute(ctxt, att, value); |
| 1293 | att = atts[i++]; |
| 1294 | value = atts[i++]; |
| 1295 | } |
| 1296 | } else { |
| 1297 | while ((att != NULL) && (value != NULL)) { |
| 1298 | if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l')) |
| 1299 | attribute(ctxt, att, value); |
| 1300 | |
| 1301 | /* |
| 1302 | * Next ones |
| 1303 | */ |
| 1304 | att = atts[i++]; |
| 1305 | value = atts[i++]; |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1311 | * If it's the Document root, finish the DTD validation and |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1312 | * check the document root element for validity |
| 1313 | */ |
| 1314 | if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 1315 | int chk; |
| 1316 | |
| 1317 | chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc); |
| 1318 | if (chk <= 0) |
| 1319 | ctxt->valid = 0; |
| 1320 | if (chk < 0) |
| 1321 | ctxt->wellFormed = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1322 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
| 1323 | ctxt->vctxt.finishDtd = 1; |
| 1324 | } |
| 1325 | |
| 1326 | if (prefix != NULL) |
| 1327 | xmlFree(prefix); |
| 1328 | if (name != NULL) |
| 1329 | xmlFree(name); |
| 1330 | |
| 1331 | } |
| 1332 | |
| 1333 | /** |
| 1334 | * endElement: |
| 1335 | * @ctx: the user data (XML parser context) |
| 1336 | * @name: The element name |
| 1337 | * |
| 1338 | * called when the end of an element has been detected. |
| 1339 | */ |
| 1340 | void |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 1341 | endElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1342 | { |
| 1343 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 1344 | xmlParserNodeInfo node_info; |
| 1345 | xmlNodePtr cur = ctxt->node; |
| 1346 | |
| 1347 | #ifdef DEBUG_SAX |
| 1348 | if (name == NULL) |
| 1349 | xmlGenericError(xmlGenericErrorContext, "SAX.endElement(NULL)\n"); |
| 1350 | else |
| 1351 | xmlGenericError(xmlGenericErrorContext, "SAX.endElement(%s)\n", name); |
| 1352 | #endif |
| 1353 | |
| 1354 | /* Capture end position and add node */ |
| 1355 | if (cur != NULL && ctxt->record_info) { |
| 1356 | node_info.end_pos = ctxt->input->cur - ctxt->input->base; |
| 1357 | node_info.end_line = ctxt->input->line; |
| 1358 | node_info.node = cur; |
| 1359 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 1360 | } |
| 1361 | ctxt->nodemem = -1; |
| 1362 | |
| 1363 | if (ctxt->validate && ctxt->wellFormed && |
| 1364 | ctxt->myDoc && ctxt->myDoc->intSubset) |
| 1365 | ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, |
| 1366 | cur); |
| 1367 | |
| 1368 | |
| 1369 | /* |
| 1370 | * end of parsing of this node. |
| 1371 | */ |
| 1372 | #ifdef DEBUG_SAX_TREE |
| 1373 | xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name); |
| 1374 | #endif |
| 1375 | nodePop(ctxt); |
| 1376 | } |
| 1377 | |
| 1378 | /** |
| 1379 | * reference: |
| 1380 | * @ctx: the user data (XML parser context) |
| 1381 | * @name: The entity name |
| 1382 | * |
| 1383 | * called when an entity reference is detected. |
| 1384 | */ |
| 1385 | void |
| 1386 | reference(void *ctx, const xmlChar *name) |
| 1387 | { |
| 1388 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 1389 | xmlNodePtr ret; |
| 1390 | |
| 1391 | #ifdef DEBUG_SAX |
| 1392 | xmlGenericError(xmlGenericErrorContext, |
| 1393 | "SAX.reference(%s)\n", name); |
| 1394 | #endif |
| 1395 | if (name[0] == '#') |
| 1396 | ret = xmlNewCharRef(ctxt->myDoc, name); |
| 1397 | else |
| 1398 | ret = xmlNewReference(ctxt->myDoc, name); |
| 1399 | #ifdef DEBUG_SAX_TREE |
| 1400 | xmlGenericError(xmlGenericErrorContext, |
| 1401 | "add reference %s to %s \n", name, ctxt->node->name); |
| 1402 | #endif |
| 1403 | xmlAddChild(ctxt->node, ret); |
| 1404 | } |
| 1405 | |
| 1406 | /** |
| 1407 | * characters: |
| 1408 | * @ctx: the user data (XML parser context) |
| 1409 | * @ch: a xmlChar string |
| 1410 | * @len: the number of xmlChar |
| 1411 | * |
| 1412 | * receiving some chars from the parser. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1413 | */ |
| 1414 | void |
| 1415 | characters(void *ctx, const xmlChar *ch, int len) |
| 1416 | { |
| 1417 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 1418 | xmlNodePtr lastChild; |
| 1419 | |
| 1420 | #ifdef DEBUG_SAX |
| 1421 | xmlGenericError(xmlGenericErrorContext, |
| 1422 | "SAX.characters(%.30s, %d)\n", ch, len); |
| 1423 | #endif |
| 1424 | /* |
| 1425 | * Handle the data if any. If there is no child |
| 1426 | * add it as content, otherwise if the last child is text, |
| 1427 | * concatenate it, else create a new node of type text. |
| 1428 | */ |
| 1429 | |
| 1430 | if (ctxt->node == NULL) { |
| 1431 | #ifdef DEBUG_SAX_TREE |
| 1432 | xmlGenericError(xmlGenericErrorContext, |
| 1433 | "add chars: ctxt->node == NULL !\n"); |
| 1434 | #endif |
| 1435 | return; |
| 1436 | } |
| 1437 | lastChild = xmlGetLastChild(ctxt->node); |
| 1438 | #ifdef DEBUG_SAX_TREE |
| 1439 | xmlGenericError(xmlGenericErrorContext, |
| 1440 | "add chars to %s \n", ctxt->node->name); |
| 1441 | #endif |
| 1442 | |
| 1443 | /* |
| 1444 | * Here we needed an accelerator mechanism in case of very large |
| 1445 | * elements. Use an attribute in the structure !!! |
| 1446 | */ |
| 1447 | if (lastChild == NULL) { |
| 1448 | /* first node, first time */ |
| 1449 | xmlNodeAddContentLen(ctxt->node, ch, len); |
| 1450 | #ifndef XML_USE_BUFFER_CONTENT |
| 1451 | if (ctxt->node->children != NULL) { |
| 1452 | ctxt->nodelen = len; |
| 1453 | ctxt->nodemem = len + 1; |
| 1454 | } |
| 1455 | #endif |
| 1456 | } else { |
Daniel Veillard | f300b7e | 2001-08-13 10:43:15 +0000 | [diff] [blame] | 1457 | int coalesceText = (lastChild != NULL) && |
| 1458 | (lastChild->type == XML_TEXT_NODE) && |
| 1459 | (lastChild->name == xmlStringText); |
| 1460 | if ((coalesceText) && (ctxt->nodemem != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1461 | #ifndef XML_USE_BUFFER_CONTENT |
| 1462 | /* |
| 1463 | * The whole point of maintaining nodelen and nodemem, |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 1464 | * xmlTextConcat is too costly, i.e. compute length, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1465 | * reallocate a new buffer, move data, append ch. Here |
| 1466 | * We try to minimaze realloc() uses and avoid copying |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 1467 | * and recomputing length over and over. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1468 | */ |
| 1469 | if (ctxt->nodelen + len >= ctxt->nodemem) { |
| 1470 | xmlChar *newbuf; |
| 1471 | int size; |
| 1472 | |
| 1473 | size = ctxt->nodemem + len; |
| 1474 | size *= 2; |
| 1475 | newbuf = (xmlChar *) xmlRealloc(lastChild->content,size); |
| 1476 | if (newbuf == NULL) { |
| 1477 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 1478 | ctxt->sax->error(ctxt->userData, |
| 1479 | "SAX.characters(): out of memory\n"); |
| 1480 | return; |
| 1481 | } |
| 1482 | ctxt->nodemem = size; |
| 1483 | lastChild->content = newbuf; |
| 1484 | } |
| 1485 | memcpy(&lastChild->content[ctxt->nodelen], ch, len); |
| 1486 | ctxt->nodelen += len; |
| 1487 | lastChild->content[ctxt->nodelen] = 0; |
| 1488 | #else |
| 1489 | xmlTextConcat(lastChild, ch, len); |
| 1490 | #endif |
Daniel Veillard | f300b7e | 2001-08-13 10:43:15 +0000 | [diff] [blame] | 1491 | } else if (coalesceText) { |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 1492 | xmlTextConcat(lastChild, ch, len); |
| 1493 | if (ctxt->node->children != NULL) { |
| 1494 | ctxt->nodelen = xmlStrlen(lastChild->content); |
| 1495 | ctxt->nodemem = ctxt->nodelen + 1; |
| 1496 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1497 | } else { |
| 1498 | /* Mixed content, first time */ |
| 1499 | lastChild = xmlNewTextLen(ch, len); |
| 1500 | xmlAddChild(ctxt->node, lastChild); |
| 1501 | #ifndef XML_USE_BUFFER_CONTENT |
| 1502 | if (ctxt->node->children != NULL) { |
| 1503 | ctxt->nodelen = len; |
| 1504 | ctxt->nodemem = len + 1; |
| 1505 | } |
| 1506 | #endif |
| 1507 | } |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | /** |
| 1512 | * ignorableWhitespace: |
| 1513 | * @ctx: the user data (XML parser context) |
| 1514 | * @ch: a xmlChar string |
| 1515 | * @len: the number of xmlChar |
| 1516 | * |
| 1517 | * receiving some ignorable whitespaces from the parser. |
Daniel Veillard | 05c13a2 | 2001-09-09 08:38:09 +0000 | [diff] [blame] | 1518 | * UNUSED: by default the DOM building will use characters |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1519 | */ |
| 1520 | void |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 1521 | ignorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1522 | { |
| 1523 | /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */ |
| 1524 | #ifdef DEBUG_SAX |
| 1525 | xmlGenericError(xmlGenericErrorContext, |
| 1526 | "SAX.ignorableWhitespace(%.30s, %d)\n", ch, len); |
| 1527 | #endif |
| 1528 | } |
| 1529 | |
| 1530 | /** |
| 1531 | * processingInstruction: |
| 1532 | * @ctx: the user data (XML parser context) |
| 1533 | * @target: the target name |
| 1534 | * @data: the PI data's |
| 1535 | * |
| 1536 | * A processing instruction has been parsed. |
| 1537 | */ |
| 1538 | void |
| 1539 | processingInstruction(void *ctx, const xmlChar *target, |
| 1540 | const xmlChar *data) |
| 1541 | { |
| 1542 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 1543 | xmlNodePtr ret; |
| 1544 | xmlNodePtr parent = ctxt->node; |
| 1545 | |
| 1546 | #ifdef DEBUG_SAX |
| 1547 | xmlGenericError(xmlGenericErrorContext, |
| 1548 | "SAX.processingInstruction(%s, %s)\n", target, data); |
| 1549 | #endif |
| 1550 | |
| 1551 | ret = xmlNewPI(target, data); |
| 1552 | if (ret == NULL) return; |
| 1553 | parent = ctxt->node; |
| 1554 | |
| 1555 | if (ctxt->inSubset == 1) { |
| 1556 | xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret); |
| 1557 | return; |
| 1558 | } else if (ctxt->inSubset == 2) { |
| 1559 | xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret); |
| 1560 | return; |
| 1561 | } |
| 1562 | if ((ctxt->myDoc->children == NULL) || (parent == NULL)) { |
| 1563 | #ifdef DEBUG_SAX_TREE |
| 1564 | xmlGenericError(xmlGenericErrorContext, |
| 1565 | "Setting PI %s as root\n", target); |
| 1566 | #endif |
| 1567 | xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret); |
| 1568 | return; |
| 1569 | } |
| 1570 | if (parent->type == XML_ELEMENT_NODE) { |
| 1571 | #ifdef DEBUG_SAX_TREE |
| 1572 | xmlGenericError(xmlGenericErrorContext, |
| 1573 | "adding PI %s child to %s\n", target, parent->name); |
| 1574 | #endif |
| 1575 | xmlAddChild(parent, ret); |
| 1576 | } else { |
| 1577 | #ifdef DEBUG_SAX_TREE |
| 1578 | xmlGenericError(xmlGenericErrorContext, |
| 1579 | "adding PI %s sibling to ", target); |
| 1580 | xmlDebugDumpOneNode(stderr, parent, 0); |
| 1581 | #endif |
| 1582 | xmlAddSibling(parent, ret); |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | /** |
| 1587 | * globalNamespace: |
| 1588 | * @ctx: the user data (XML parser context) |
| 1589 | * @href: the namespace associated URN |
| 1590 | * @prefix: the namespace prefix |
| 1591 | * |
| 1592 | * An old global namespace has been parsed. |
| 1593 | */ |
| 1594 | void |
| 1595 | globalNamespace(void *ctx, const xmlChar *href, const xmlChar *prefix) |
| 1596 | { |
| 1597 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 1598 | #ifdef DEBUG_SAX |
| 1599 | xmlGenericError(xmlGenericErrorContext, |
| 1600 | "SAX.globalNamespace(%s, %s)\n", href, prefix); |
| 1601 | #endif |
| 1602 | xmlNewGlobalNs(ctxt->myDoc, href, prefix); |
| 1603 | } |
| 1604 | |
| 1605 | /** |
| 1606 | * setNamespace: |
| 1607 | * @ctx: the user data (XML parser context) |
| 1608 | * @name: the namespace prefix |
| 1609 | * |
| 1610 | * Set the current element namespace. |
| 1611 | */ |
| 1612 | |
| 1613 | void |
| 1614 | setNamespace(void *ctx, const xmlChar *name) |
| 1615 | { |
| 1616 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 1617 | xmlNsPtr ns; |
| 1618 | xmlNodePtr parent; |
| 1619 | |
| 1620 | #ifdef DEBUG_SAX |
| 1621 | xmlGenericError(xmlGenericErrorContext, "SAX.setNamespace(%s)\n", name); |
| 1622 | #endif |
| 1623 | ns = xmlSearchNs(ctxt->myDoc, ctxt->node, name); |
| 1624 | if (ns == NULL) { /* ctxt->node may not have a parent yet ! */ |
| 1625 | if (ctxt->nodeNr >= 2) { |
| 1626 | parent = ctxt->nodeTab[ctxt->nodeNr - 2]; |
| 1627 | if (parent != NULL) |
| 1628 | ns = xmlSearchNs(ctxt->myDoc, parent, name); |
| 1629 | } |
| 1630 | } |
| 1631 | xmlSetNs(ctxt->node, ns); |
| 1632 | } |
| 1633 | |
| 1634 | /** |
| 1635 | * getNamespace: |
| 1636 | * @ctx: the user data (XML parser context) |
| 1637 | * |
| 1638 | * Get the current element namespace. |
| 1639 | * |
| 1640 | * Returns the xmlNsPtr or NULL if none |
| 1641 | */ |
| 1642 | |
| 1643 | xmlNsPtr |
| 1644 | getNamespace(void *ctx) |
| 1645 | { |
| 1646 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 1647 | xmlNsPtr ret; |
| 1648 | |
| 1649 | #ifdef DEBUG_SAX |
| 1650 | xmlGenericError(xmlGenericErrorContext, "SAX.getNamespace()\n"); |
| 1651 | #endif |
| 1652 | ret = ctxt->node->ns; |
| 1653 | return(ret); |
| 1654 | } |
| 1655 | |
| 1656 | /** |
| 1657 | * checkNamespace: |
| 1658 | * @ctx: the user data (XML parser context) |
| 1659 | * @namespace: the namespace to check against |
| 1660 | * |
| 1661 | * Check that the current element namespace is the same as the |
| 1662 | * one read upon parsing. |
| 1663 | * |
| 1664 | * Returns 1 if true 0 otherwise |
| 1665 | */ |
| 1666 | |
| 1667 | int |
| 1668 | checkNamespace(void *ctx, xmlChar *namespace) |
| 1669 | { |
| 1670 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 1671 | xmlNodePtr cur = ctxt->node; |
| 1672 | |
| 1673 | #ifdef DEBUG_SAX |
| 1674 | xmlGenericError(xmlGenericErrorContext, |
| 1675 | "SAX.checkNamespace(%s)\n", namespace); |
| 1676 | #endif |
| 1677 | |
| 1678 | /* |
| 1679 | * Check that the Name in the ETag is the same as in the STag. |
| 1680 | */ |
| 1681 | if (namespace == NULL) { |
| 1682 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 1683 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 1684 | ctxt->sax->error(ctxt, |
| 1685 | "End tags for %s don't hold the namespace %s\n", |
| 1686 | cur->name, cur->ns->prefix); |
| 1687 | ctxt->wellFormed = 0; |
| 1688 | } |
| 1689 | } else { |
| 1690 | if ((cur->ns == NULL) || (cur->ns->prefix == NULL)) { |
| 1691 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 1692 | ctxt->sax->error(ctxt, |
| 1693 | "End tags %s holds a prefix %s not used by the open tag\n", |
| 1694 | cur->name, namespace); |
| 1695 | ctxt->wellFormed = 0; |
| 1696 | } else if (!xmlStrEqual(namespace, cur->ns->prefix)) { |
| 1697 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 1698 | ctxt->sax->error(ctxt, |
| 1699 | "Start and End tags for %s don't use the same namespaces: %s and %s\n", |
| 1700 | cur->name, cur->ns->prefix, namespace); |
| 1701 | ctxt->wellFormed = 0; |
| 1702 | } else |
| 1703 | return(1); |
| 1704 | } |
| 1705 | return(0); |
| 1706 | } |
| 1707 | |
| 1708 | /** |
| 1709 | * namespaceDecl: |
| 1710 | * @ctx: the user data (XML parser context) |
| 1711 | * @href: the namespace associated URN |
| 1712 | * @prefix: the namespace prefix |
| 1713 | * |
| 1714 | * A namespace has been parsed. |
| 1715 | */ |
| 1716 | void |
| 1717 | namespaceDecl(void *ctx, const xmlChar *href, const xmlChar *prefix) |
| 1718 | { |
| 1719 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 1720 | #ifdef DEBUG_SAX |
| 1721 | if (prefix == NULL) |
| 1722 | xmlGenericError(xmlGenericErrorContext, |
| 1723 | "SAX.namespaceDecl(%s, NULL)\n", href); |
| 1724 | else |
| 1725 | xmlGenericError(xmlGenericErrorContext, |
| 1726 | "SAX.namespaceDecl(%s, %s)\n", href, prefix); |
| 1727 | #endif |
| 1728 | xmlNewNs(ctxt->node, href, prefix); |
| 1729 | } |
| 1730 | |
| 1731 | /** |
| 1732 | * comment: |
| 1733 | * @ctx: the user data (XML parser context) |
| 1734 | * @value: the comment content |
| 1735 | * |
| 1736 | * A comment has been parsed. |
| 1737 | */ |
| 1738 | void |
| 1739 | comment(void *ctx, const xmlChar *value) |
| 1740 | { |
| 1741 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 1742 | xmlNodePtr ret; |
| 1743 | xmlNodePtr parent = ctxt->node; |
| 1744 | |
| 1745 | #ifdef DEBUG_SAX |
| 1746 | xmlGenericError(xmlGenericErrorContext, "SAX.comment(%s)\n", value); |
| 1747 | #endif |
| 1748 | ret = xmlNewDocComment(ctxt->myDoc, value); |
| 1749 | if (ret == NULL) return; |
| 1750 | |
| 1751 | if (ctxt->inSubset == 1) { |
| 1752 | xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret); |
| 1753 | return; |
| 1754 | } else if (ctxt->inSubset == 2) { |
| 1755 | xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret); |
| 1756 | return; |
| 1757 | } |
| 1758 | if ((ctxt->myDoc->children == NULL) || (parent == NULL)) { |
| 1759 | #ifdef DEBUG_SAX_TREE |
| 1760 | xmlGenericError(xmlGenericErrorContext, |
| 1761 | "Setting comment as root\n"); |
| 1762 | #endif |
| 1763 | xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret); |
| 1764 | return; |
| 1765 | } |
| 1766 | if (parent->type == XML_ELEMENT_NODE) { |
| 1767 | #ifdef DEBUG_SAX_TREE |
| 1768 | xmlGenericError(xmlGenericErrorContext, |
| 1769 | "adding comment child to %s\n", parent->name); |
| 1770 | #endif |
| 1771 | xmlAddChild(parent, ret); |
| 1772 | } else { |
| 1773 | #ifdef DEBUG_SAX_TREE |
| 1774 | xmlGenericError(xmlGenericErrorContext, |
| 1775 | "adding comment sibling to "); |
| 1776 | xmlDebugDumpOneNode(stderr, parent, 0); |
| 1777 | #endif |
| 1778 | xmlAddSibling(parent, ret); |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | /** |
| 1783 | * cdataBlock: |
| 1784 | * @ctx: the user data (XML parser context) |
| 1785 | * @value: The pcdata content |
| 1786 | * @len: the block length |
| 1787 | * |
| 1788 | * called when a pcdata block has been parsed |
| 1789 | */ |
| 1790 | void |
| 1791 | cdataBlock(void *ctx, const xmlChar *value, int len) |
| 1792 | { |
| 1793 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 1794 | xmlNodePtr ret, lastChild; |
| 1795 | |
| 1796 | #ifdef DEBUG_SAX |
| 1797 | xmlGenericError(xmlGenericErrorContext, |
| 1798 | "SAX.pcdata(%.10s, %d)\n", value, len); |
| 1799 | #endif |
| 1800 | lastChild = xmlGetLastChild(ctxt->node); |
| 1801 | #ifdef DEBUG_SAX_TREE |
| 1802 | xmlGenericError(xmlGenericErrorContext, |
| 1803 | "add chars to %s \n", ctxt->node->name); |
| 1804 | #endif |
| 1805 | if ((lastChild != NULL) && |
| 1806 | (lastChild->type == XML_CDATA_SECTION_NODE)) { |
| 1807 | xmlTextConcat(lastChild, value, len); |
| 1808 | } else { |
| 1809 | ret = xmlNewCDataBlock(ctxt->myDoc, value, len); |
| 1810 | xmlAddChild(ctxt->node, ret); |
| 1811 | } |
| 1812 | } |
| 1813 | |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 1814 | /** |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1815 | * initxmlDefaultSAXHandler: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1816 | * @hdlr: the SAX handler |
| 1817 | * @warning: flag if non-zero sets the handler warning procedure |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 1818 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1819 | * Initialize the default XML SAX handler |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1820 | */ |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 1821 | void |
| 1822 | initxmlDefaultSAXHandler(xmlSAXHandler *hdlr, int warning) |
| 1823 | { |
| 1824 | if(hdlr->initialized == 1) |
| 1825 | return; |
| 1826 | |
| 1827 | hdlr->internalSubset = internalSubset; |
| 1828 | hdlr->externalSubset = externalSubset; |
| 1829 | hdlr->isStandalone = isStandalone; |
| 1830 | hdlr->hasInternalSubset = hasInternalSubset; |
| 1831 | hdlr->hasExternalSubset = hasExternalSubset; |
| 1832 | hdlr->resolveEntity = resolveEntity; |
| 1833 | hdlr->getEntity = getEntity; |
| 1834 | hdlr->getParameterEntity = getParameterEntity; |
| 1835 | hdlr->entityDecl = entityDecl; |
| 1836 | hdlr->attributeDecl = attributeDecl; |
| 1837 | hdlr->elementDecl = elementDecl; |
| 1838 | hdlr->notationDecl = notationDecl; |
| 1839 | hdlr->unparsedEntityDecl = unparsedEntityDecl; |
| 1840 | hdlr->setDocumentLocator = setDocumentLocator; |
| 1841 | hdlr->startDocument = startDocument; |
| 1842 | hdlr->endDocument = endDocument; |
| 1843 | hdlr->startElement = startElement; |
| 1844 | hdlr->endElement = endElement; |
| 1845 | hdlr->reference = reference; |
| 1846 | hdlr->characters = characters; |
| 1847 | hdlr->cdataBlock = cdataBlock; |
| 1848 | hdlr->ignorableWhitespace = characters; |
| 1849 | hdlr->processingInstruction = processingInstruction; |
| 1850 | hdlr->comment = comment; |
| 1851 | /* if (xmlGetWarningsDefaultValue == 0) */ |
| 1852 | if (warning == 0) |
| 1853 | hdlr->warning = NULL; |
| 1854 | else |
| 1855 | hdlr->warning = xmlParserWarning; |
| 1856 | hdlr->error = xmlParserError; |
| 1857 | hdlr->fatalError = xmlParserError; |
| 1858 | |
| 1859 | hdlr->initialized = 1; |
| 1860 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1861 | |
| 1862 | /** |
| 1863 | * xmlDefaultSAXHandlerInit: |
| 1864 | * |
| 1865 | * Initialize the default SAX handler |
| 1866 | */ |
| 1867 | void |
| 1868 | xmlDefaultSAXHandlerInit(void) |
| 1869 | { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 1870 | initxmlDefaultSAXHandler(&xmlDefaultSAXHandler, xmlGetWarningsDefaultValue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 1873 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 1874 | |
| 1875 | /** |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1876 | * inithtmlDefaultSAXHandler: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1877 | * @hdlr: the SAX handler |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 1878 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1879 | * Initialize the default HTML SAX handler |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1880 | */ |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 1881 | void |
| 1882 | inithtmlDefaultSAXHandler(xmlSAXHandler *hdlr) |
| 1883 | { |
| 1884 | if(hdlr->initialized == 1) |
| 1885 | return; |
| 1886 | |
| 1887 | hdlr->internalSubset = internalSubset; |
| 1888 | hdlr->externalSubset = NULL; |
| 1889 | hdlr->isStandalone = NULL; |
| 1890 | hdlr->hasInternalSubset = NULL; |
| 1891 | hdlr->hasExternalSubset = NULL; |
| 1892 | hdlr->resolveEntity = NULL; |
| 1893 | hdlr->getEntity = getEntity; |
| 1894 | hdlr->getParameterEntity = NULL; |
| 1895 | hdlr->entityDecl = NULL; |
| 1896 | hdlr->attributeDecl = NULL; |
| 1897 | hdlr->elementDecl = NULL; |
| 1898 | hdlr->notationDecl = NULL; |
| 1899 | hdlr->unparsedEntityDecl = NULL; |
| 1900 | hdlr->setDocumentLocator = setDocumentLocator; |
| 1901 | hdlr->startDocument = startDocument; |
| 1902 | hdlr->endDocument = endDocument; |
| 1903 | hdlr->startElement = startElement; |
| 1904 | hdlr->endElement = endElement; |
| 1905 | hdlr->reference = NULL; |
| 1906 | hdlr->characters = characters; |
| 1907 | hdlr->cdataBlock = cdataBlock; |
| 1908 | hdlr->ignorableWhitespace = ignorableWhitespace; |
| 1909 | hdlr->processingInstruction = NULL; |
| 1910 | hdlr->comment = comment; |
| 1911 | hdlr->warning = xmlParserWarning; |
| 1912 | hdlr->error = xmlParserError; |
| 1913 | hdlr->fatalError = xmlParserError; |
| 1914 | |
| 1915 | hdlr->initialized = 1; |
| 1916 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1917 | |
| 1918 | /** |
| 1919 | * htmlDefaultSAXHandlerInit: |
| 1920 | * |
| 1921 | * Initialize the default SAX handler |
| 1922 | */ |
| 1923 | void |
| 1924 | htmlDefaultSAXHandlerInit(void) |
| 1925 | { |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 1926 | inithtmlDefaultSAXHandler(&htmlDefaultSAXHandler); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1927 | } |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 1928 | |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 1929 | #endif /* LIBXML_HTML_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1930 | |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 1931 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 1932 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1933 | /** |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1934 | * initdocbDefaultSAXHandler: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1935 | * @hdlr: the SAX handler |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1936 | * |
| 1937 | * Initialize the default DocBook SAX handler |
| 1938 | */ |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 1939 | void |
| 1940 | initdocbDefaultSAXHandler(xmlSAXHandler *hdlr) |
| 1941 | { |
| 1942 | if(hdlr->initialized == 1) |
| 1943 | return; |
| 1944 | |
| 1945 | hdlr->internalSubset = internalSubset; |
| 1946 | hdlr->externalSubset = NULL; |
| 1947 | hdlr->isStandalone = isStandalone; |
| 1948 | hdlr->hasInternalSubset = hasInternalSubset; |
| 1949 | hdlr->hasExternalSubset = hasExternalSubset; |
| 1950 | hdlr->resolveEntity = resolveEntity; |
| 1951 | hdlr->getEntity = getEntity; |
| 1952 | hdlr->getParameterEntity = NULL; |
| 1953 | hdlr->entityDecl = entityDecl; |
| 1954 | hdlr->attributeDecl = NULL; |
| 1955 | hdlr->elementDecl = NULL; |
| 1956 | hdlr->notationDecl = NULL; |
| 1957 | hdlr->unparsedEntityDecl = NULL; |
| 1958 | hdlr->setDocumentLocator = setDocumentLocator; |
| 1959 | hdlr->startDocument = startDocument; |
| 1960 | hdlr->endDocument = endDocument; |
| 1961 | hdlr->startElement = startElement; |
| 1962 | hdlr->endElement = endElement; |
| 1963 | hdlr->reference = reference; |
| 1964 | hdlr->characters = characters; |
| 1965 | hdlr->cdataBlock = NULL; |
| 1966 | hdlr->ignorableWhitespace = ignorableWhitespace; |
| 1967 | hdlr->processingInstruction = NULL; |
| 1968 | hdlr->comment = comment; |
| 1969 | hdlr->warning = xmlParserWarning; |
| 1970 | hdlr->error = xmlParserError; |
| 1971 | hdlr->fatalError = xmlParserError; |
| 1972 | |
| 1973 | hdlr->initialized = 1; |
| 1974 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1975 | |
| 1976 | /** |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 1977 | * docbDefaultSAXHandlerInit: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1978 | * |
| 1979 | * Initialize the default SAX handler |
| 1980 | */ |
| 1981 | void |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 1982 | docbDefaultSAXHandlerInit(void) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1983 | { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 1984 | initdocbDefaultSAXHandler(&docbDefaultSAXHandler); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1985 | } |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 1986 | |
| 1987 | #endif /* LIBXML_DOCB_ENABLED */ |