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