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