Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * tree.c : implemetation of access function for an XML tree. |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * Daniel.Veillard@w3.org |
| 7 | * |
| 8 | * 14 Nov 2000 ht - Changed the name of function xmlBufferWriteChar under VMS |
| 9 | * as it was similar to xmlBufferWriteCHAR when compiling without case |
| 10 | * sensitivity. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #ifdef WIN32 |
| 15 | #include "win32config.h" |
| 16 | #else |
| 17 | #include "config.h" |
| 18 | #endif |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> /* for memset() only ! */ |
| 22 | |
| 23 | #ifdef HAVE_CTYPE_H |
| 24 | #include <ctype.h> |
| 25 | #endif |
| 26 | #ifdef HAVE_STDLIB_H |
| 27 | #include <stdlib.h> |
| 28 | #endif |
| 29 | #ifdef HAVE_ZLIB_H |
| 30 | #include <zlib.h> |
| 31 | #endif |
| 32 | |
| 33 | #include <libxml/xmlmemory.h> |
| 34 | #include <libxml/tree.h> |
| 35 | #include <libxml/parser.h> |
| 36 | #include <libxml/entities.h> |
| 37 | #include <libxml/valid.h> |
| 38 | #include <libxml/xmlerror.h> |
| 39 | |
| 40 | xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
| 41 | xmlChar xmlStringTextNoenc[] = |
| 42 | { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 }; |
| 43 | xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; |
| 44 | int oldXMLWDcompatibility = 0; |
| 45 | int xmlIndentTreeOutput = 0; |
| 46 | xmlBufferAllocationScheme xmlBufferAllocScheme = XML_BUFFER_ALLOC_EXACT; |
| 47 | |
| 48 | static int xmlCompressMode = 0; |
| 49 | static int xmlCheckDTD = 1; |
| 50 | int xmlSaveNoEmptyTags = 0; |
| 51 | |
| 52 | #define IS_BLANK(c) \ |
| 53 | (((c) == '\n') || ((c) == '\r') || ((c) == '\t') || ((c) == ' ')) |
| 54 | |
| 55 | #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \ |
| 56 | xmlNodePtr ulccur = (n)->children; \ |
| 57 | if (ulccur == NULL) { \ |
| 58 | (n)->last = NULL; \ |
| 59 | } else { \ |
| 60 | while (ulccur->next != NULL) { \ |
| 61 | ulccur->parent = (n); \ |
| 62 | ulccur = ulccur->next; \ |
| 63 | } \ |
| 64 | ulccur->parent = (n); \ |
| 65 | (n)->last = ulccur; \ |
| 66 | }} |
| 67 | |
| 68 | /* #define DEBUG_BUFFER */ |
| 69 | /* #define DEBUG_TREE */ |
| 70 | |
| 71 | /************************************************************************ |
| 72 | * * |
| 73 | * Allocation and deallocation of basic structures * |
| 74 | * * |
| 75 | ************************************************************************/ |
| 76 | |
| 77 | /** |
| 78 | * xmlSetBufferAllocationScheme: |
| 79 | * @scheme: allocation method to use |
| 80 | * |
| 81 | * Set the buffer allocation method. Types are |
| 82 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 83 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 84 | * improves performance |
| 85 | */ |
| 86 | void |
| 87 | xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { |
| 88 | xmlBufferAllocScheme = scheme; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * xmlGetBufferAllocationScheme: |
| 93 | * |
| 94 | * Types are |
| 95 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 96 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 97 | * improves performance |
| 98 | * |
| 99 | * Returns the current allocation scheme |
| 100 | */ |
| 101 | xmlBufferAllocationScheme |
| 102 | xmlGetBufferAllocationScheme() { |
| 103 | return xmlBufferAllocScheme; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * xmlNewNs: |
| 108 | * @node: the element carrying the namespace |
| 109 | * @href: the URI associated |
| 110 | * @prefix: the prefix for the namespace |
| 111 | * |
| 112 | * Creation of a new Namespace. This function will refuse to create |
| 113 | * a namespace with a similar prefix than an existing one present on this |
| 114 | * node. |
| 115 | * We use href==NULL in the case of an element creation where the namespace |
| 116 | * was not defined. |
| 117 | * Returns returns a new namespace pointer or NULL |
| 118 | */ |
| 119 | xmlNsPtr |
| 120 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
| 121 | xmlNsPtr cur; |
| 122 | |
| 123 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
| 124 | return(NULL); |
| 125 | |
| 126 | /* |
| 127 | * Allocate a new Namespace and fill the fields. |
| 128 | */ |
| 129 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 130 | if (cur == NULL) { |
| 131 | xmlGenericError(xmlGenericErrorContext, |
| 132 | "xmlNewNs : malloc failed\n"); |
| 133 | return(NULL); |
| 134 | } |
| 135 | memset(cur, 0, sizeof(xmlNs)); |
| 136 | cur->type = XML_LOCAL_NAMESPACE; |
| 137 | |
| 138 | if (href != NULL) |
| 139 | cur->href = xmlStrdup(href); |
| 140 | if (prefix != NULL) |
| 141 | cur->prefix = xmlStrdup(prefix); |
| 142 | |
| 143 | /* |
| 144 | * Add it at the end to preserve parsing order ... |
| 145 | * and checks for existing use of the prefix |
| 146 | */ |
| 147 | if (node != NULL) { |
| 148 | if (node->nsDef == NULL) { |
| 149 | node->nsDef = cur; |
| 150 | } else { |
| 151 | xmlNsPtr prev = node->nsDef; |
| 152 | |
| 153 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 154 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 155 | xmlFreeNs(cur); |
| 156 | return(NULL); |
| 157 | } |
| 158 | while (prev->next != NULL) { |
| 159 | prev = prev->next; |
| 160 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 161 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 162 | xmlFreeNs(cur); |
| 163 | return(NULL); |
| 164 | } |
| 165 | } |
| 166 | prev->next = cur; |
| 167 | } |
| 168 | } |
| 169 | return(cur); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * xmlSetNs: |
| 174 | * @node: a node in the document |
| 175 | * @ns: a namespace pointer |
| 176 | * |
| 177 | * Associate a namespace to a node, a posteriori. |
| 178 | */ |
| 179 | void |
| 180 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
| 181 | if (node == NULL) { |
| 182 | #ifdef DEBUG_TREE |
| 183 | xmlGenericError(xmlGenericErrorContext, |
| 184 | "xmlSetNs: node == NULL\n"); |
| 185 | #endif |
| 186 | return; |
| 187 | } |
| 188 | node->ns = ns; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * xmlFreeNs: |
| 193 | * @cur: the namespace pointer |
| 194 | * |
| 195 | * Free up the structures associated to a namespace |
| 196 | */ |
| 197 | void |
| 198 | xmlFreeNs(xmlNsPtr cur) { |
| 199 | if (cur == NULL) { |
| 200 | #ifdef DEBUG_TREE |
| 201 | xmlGenericError(xmlGenericErrorContext, |
| 202 | "xmlFreeNs : ns == NULL\n"); |
| 203 | #endif |
| 204 | return; |
| 205 | } |
| 206 | if (cur->href != NULL) xmlFree((char *) cur->href); |
| 207 | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 208 | MEM_CLEANUP(cur, sizeof(xmlNs)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 209 | xmlFree(cur); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * xmlFreeNsList: |
| 214 | * @cur: the first namespace pointer |
| 215 | * |
| 216 | * Free up all the structures associated to the chained namespaces. |
| 217 | */ |
| 218 | void |
| 219 | xmlFreeNsList(xmlNsPtr cur) { |
| 220 | xmlNsPtr next; |
| 221 | if (cur == NULL) { |
| 222 | #ifdef DEBUG_TREE |
| 223 | xmlGenericError(xmlGenericErrorContext, |
| 224 | "xmlFreeNsList : ns == NULL\n"); |
| 225 | #endif |
| 226 | return; |
| 227 | } |
| 228 | while (cur != NULL) { |
| 229 | next = cur->next; |
| 230 | xmlFreeNs(cur); |
| 231 | cur = next; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * xmlNewDtd: |
| 237 | * @doc: the document pointer |
| 238 | * @name: the DTD name |
| 239 | * @ExternalID: the external ID |
| 240 | * @SystemID: the system ID |
| 241 | * |
| 242 | * Creation of a new DTD for the external subset. To create an |
| 243 | * internal subset, use xmlCreateIntSubset(). |
| 244 | * |
| 245 | * Returns a pointer to the new DTD structure |
| 246 | */ |
| 247 | xmlDtdPtr |
| 248 | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
| 249 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 250 | xmlDtdPtr cur; |
| 251 | |
| 252 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
| 253 | #ifdef DEBUG_TREE |
| 254 | xmlGenericError(xmlGenericErrorContext, |
| 255 | "xmlNewDtd(%s): document %s already have a DTD %s\n", |
| 256 | /* !!! */ (char *) name, doc->name, |
| 257 | /* !!! */ (char *)doc->extSubset->name); |
| 258 | #endif |
| 259 | return(NULL); |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Allocate a new DTD and fill the fields. |
| 264 | */ |
| 265 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 266 | if (cur == NULL) { |
| 267 | xmlGenericError(xmlGenericErrorContext, |
| 268 | "xmlNewDtd : malloc failed\n"); |
| 269 | return(NULL); |
| 270 | } |
| 271 | memset(cur, 0 , sizeof(xmlDtd)); |
| 272 | cur->type = XML_DTD_NODE; |
| 273 | |
| 274 | if (name != NULL) |
| 275 | cur->name = xmlStrdup(name); |
| 276 | if (ExternalID != NULL) |
| 277 | cur->ExternalID = xmlStrdup(ExternalID); |
| 278 | if (SystemID != NULL) |
| 279 | cur->SystemID = xmlStrdup(SystemID); |
| 280 | if (doc != NULL) |
| 281 | doc->extSubset = cur; |
| 282 | cur->doc = doc; |
| 283 | |
| 284 | return(cur); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * xmlGetIntSubset: |
| 289 | * @doc: the document pointer |
| 290 | * |
| 291 | * Get the internal subset of a document |
| 292 | * Returns a pointer to the DTD structure or NULL if not found |
| 293 | */ |
| 294 | |
| 295 | xmlDtdPtr |
| 296 | xmlGetIntSubset(xmlDocPtr doc) { |
| 297 | xmlNodePtr cur; |
| 298 | |
| 299 | if (doc == NULL) |
| 300 | return(NULL); |
| 301 | cur = doc->children; |
| 302 | while (cur != NULL) { |
| 303 | if (cur->type == XML_DTD_NODE) |
| 304 | return((xmlDtdPtr) cur); |
| 305 | cur = cur->next; |
| 306 | } |
| 307 | return((xmlDtdPtr) doc->intSubset); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * xmlCreateIntSubset: |
| 312 | * @doc: the document pointer |
| 313 | * @name: the DTD name |
| 314 | * @ExternalID: the external ID |
| 315 | * @SystemID: the system ID |
| 316 | * |
| 317 | * Create the internal subset of a document |
| 318 | * Returns a pointer to the new DTD structure |
| 319 | */ |
| 320 | xmlDtdPtr |
| 321 | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
| 322 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 323 | xmlDtdPtr cur; |
| 324 | |
| 325 | if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) { |
| 326 | #ifdef DEBUG_TREE |
| 327 | xmlGenericError(xmlGenericErrorContext, |
| 328 | |
| 329 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 330 | doc->name); |
| 331 | #endif |
| 332 | return(NULL); |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Allocate a new DTD and fill the fields. |
| 337 | */ |
| 338 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 339 | if (cur == NULL) { |
| 340 | xmlGenericError(xmlGenericErrorContext, |
| 341 | "xmlNewDtd : malloc failed\n"); |
| 342 | return(NULL); |
| 343 | } |
| 344 | memset(cur, 0, sizeof(xmlDtd)); |
| 345 | cur->type = XML_DTD_NODE; |
| 346 | |
| 347 | if (name != NULL) |
| 348 | cur->name = xmlStrdup(name); |
| 349 | if (ExternalID != NULL) |
| 350 | cur->ExternalID = xmlStrdup(ExternalID); |
| 351 | if (SystemID != NULL) |
| 352 | cur->SystemID = xmlStrdup(SystemID); |
| 353 | if (doc != NULL) { |
| 354 | doc->intSubset = cur; |
| 355 | cur->parent = doc; |
| 356 | cur->doc = doc; |
| 357 | if (doc->children == NULL) { |
| 358 | doc->children = (xmlNodePtr) cur; |
| 359 | doc->last = (xmlNodePtr) cur; |
| 360 | } else { |
| 361 | xmlNodePtr prev; |
| 362 | |
| 363 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 364 | prev = doc->children; |
| 365 | prev->prev = (xmlNodePtr) cur; |
| 366 | cur->next = prev; |
| 367 | doc->children = (xmlNodePtr) cur; |
| 368 | } else { |
| 369 | prev = doc->last; |
| 370 | prev->next = (xmlNodePtr) cur; |
| 371 | cur->prev = prev; |
| 372 | doc->last = (xmlNodePtr) cur; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | return(cur); |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * xmlFreeDtd: |
| 381 | * @cur: the DTD structure to free up |
| 382 | * |
| 383 | * Free a DTD structure. |
| 384 | */ |
| 385 | void |
| 386 | xmlFreeDtd(xmlDtdPtr cur) { |
| 387 | if (cur == NULL) { |
| 388 | #ifdef DEBUG_TREE |
| 389 | xmlGenericError(xmlGenericErrorContext, |
| 390 | "xmlFreeDtd : DTD == NULL\n"); |
| 391 | #endif |
| 392 | return; |
| 393 | } |
| 394 | if (cur->children != NULL) { |
| 395 | xmlNodePtr next, c = cur->children; |
| 396 | |
| 397 | /* |
| 398 | * Cleanup all the DTD comments they are not in the Dtd |
| 399 | * indexes. |
| 400 | */ |
| 401 | while (c != NULL) { |
| 402 | next = c->next; |
| 403 | if (c->type == XML_COMMENT_NODE) { |
| 404 | xmlUnlinkNode(c); |
| 405 | xmlFreeNode(c); |
| 406 | } |
| 407 | c = next; |
| 408 | } |
| 409 | } |
| 410 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 411 | if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID); |
| 412 | if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID); |
| 413 | /* TODO !!! */ |
| 414 | if (cur->notations != NULL) |
| 415 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
| 416 | |
| 417 | if (cur->elements != NULL) |
| 418 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
| 419 | if (cur->attributes != NULL) |
| 420 | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
| 421 | if (cur->entities != NULL) |
| 422 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
| 423 | if (cur->pentities != NULL) |
| 424 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities); |
| 425 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 426 | MEM_CLEANUP(cur, sizeof(xmlDtd)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 427 | xmlFree(cur); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * xmlNewDoc: |
| 432 | * @version: xmlChar string giving the version of XML "1.0" |
| 433 | * |
| 434 | * Returns a new document |
| 435 | */ |
| 436 | xmlDocPtr |
| 437 | xmlNewDoc(const xmlChar *version) { |
| 438 | xmlDocPtr cur; |
| 439 | |
| 440 | if (version == NULL) |
| 441 | version = (const xmlChar *) "1.0"; |
| 442 | |
| 443 | /* |
| 444 | * Allocate a new document and fill the fields. |
| 445 | */ |
| 446 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
| 447 | if (cur == NULL) { |
| 448 | xmlGenericError(xmlGenericErrorContext, |
| 449 | "xmlNewDoc : malloc failed\n"); |
| 450 | return(NULL); |
| 451 | } |
| 452 | memset(cur, 0, sizeof(xmlDoc)); |
| 453 | cur->type = XML_DOCUMENT_NODE; |
| 454 | |
| 455 | cur->version = xmlStrdup(version); |
| 456 | cur->standalone = -1; |
| 457 | cur->compression = -1; /* not initialized */ |
| 458 | cur->doc = cur; |
| 459 | return(cur); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * xmlFreeDoc: |
| 464 | * @cur: pointer to the document |
| 465 | * @: |
| 466 | * |
| 467 | * Free up all the structures used by a document, tree included. |
| 468 | */ |
| 469 | void |
| 470 | xmlFreeDoc(xmlDocPtr cur) { |
| 471 | if (cur == NULL) { |
| 472 | #ifdef DEBUG_TREE |
| 473 | xmlGenericError(xmlGenericErrorContext, |
| 474 | "xmlFreeDoc : document == NULL\n"); |
| 475 | #endif |
| 476 | return; |
| 477 | } |
| 478 | if (cur->version != NULL) xmlFree((char *) cur->version); |
| 479 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 480 | if (cur->encoding != NULL) xmlFree((char *) cur->encoding); |
| 481 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 482 | if (cur->intSubset != NULL) xmlFreeDtd(cur->intSubset); |
| 483 | if (cur->extSubset != NULL) xmlFreeDtd(cur->extSubset); |
| 484 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
| 485 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
| 486 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
| 487 | if (cur->URL != NULL) xmlFree((char *) cur->URL); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 488 | MEM_CLEANUP(cur, sizeof(xmlDoc)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 489 | xmlFree(cur); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * xmlStringLenGetNodeList: |
| 494 | * @doc: the document |
| 495 | * @value: the value of the text |
| 496 | * @len: the length of the string value |
| 497 | * |
| 498 | * Parse the value string and build the node list associated. Should |
| 499 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 500 | * Returns a pointer to the first child |
| 501 | */ |
| 502 | xmlNodePtr |
| 503 | xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) { |
| 504 | xmlNodePtr ret = NULL, last = NULL; |
| 505 | xmlNodePtr node; |
| 506 | xmlChar *val; |
| 507 | const xmlChar *cur = value; |
| 508 | const xmlChar *q; |
| 509 | xmlEntityPtr ent; |
| 510 | |
| 511 | if (value == NULL) return(NULL); |
| 512 | |
| 513 | q = cur; |
| 514 | while ((*cur != 0) && (cur - value < len)) { |
| 515 | if (*cur == '&') { |
| 516 | /* |
| 517 | * Save the current text. |
| 518 | */ |
| 519 | if (cur != q) { |
| 520 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 521 | xmlNodeAddContentLen(last, q, cur - q); |
| 522 | } else { |
| 523 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 524 | if (node == NULL) return(ret); |
| 525 | if (last == NULL) |
| 526 | last = ret = node; |
| 527 | else { |
| 528 | last->next = node; |
| 529 | node->prev = last; |
| 530 | last = node; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | /* |
| 535 | * Read the entity string |
| 536 | */ |
| 537 | cur++; |
| 538 | q = cur; |
| 539 | while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++; |
| 540 | if ((*cur == 0) || (cur - value >= len)) { |
| 541 | #ifdef DEBUG_TREE |
| 542 | xmlGenericError(xmlGenericErrorContext, |
| 543 | "xmlStringLenGetNodeList: unterminated entity %30s\n", q); |
| 544 | #endif |
| 545 | return(ret); |
| 546 | } |
| 547 | if (cur != q) { |
| 548 | /* |
| 549 | * Predefined entities don't generate nodes |
| 550 | */ |
| 551 | val = xmlStrndup(q, cur - q); |
| 552 | ent = xmlGetDocEntity(doc, val); |
| 553 | if ((ent != NULL) && |
| 554 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 555 | if (last == NULL) { |
| 556 | node = xmlNewDocText(doc, ent->content); |
| 557 | last = ret = node; |
| 558 | } else |
| 559 | xmlNodeAddContent(last, ent->content); |
| 560 | |
| 561 | } else { |
| 562 | /* |
| 563 | * Create a new REFERENCE_REF node |
| 564 | */ |
| 565 | node = xmlNewReference(doc, val); |
| 566 | if (node == NULL) { |
| 567 | if (val != NULL) xmlFree(val); |
| 568 | return(ret); |
| 569 | } |
| 570 | if (last == NULL) |
| 571 | last = ret = node; |
| 572 | else { |
| 573 | last->next = node; |
| 574 | node->prev = last; |
| 575 | last = node; |
| 576 | } |
| 577 | } |
| 578 | xmlFree(val); |
| 579 | } |
| 580 | cur++; |
| 581 | q = cur; |
| 582 | } else |
| 583 | cur++; |
| 584 | } |
| 585 | if (cur != q) { |
| 586 | /* |
| 587 | * Handle the last piece of text. |
| 588 | */ |
| 589 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 590 | xmlNodeAddContentLen(last, q, cur - q); |
| 591 | } else { |
| 592 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 593 | if (node == NULL) return(ret); |
| 594 | if (last == NULL) |
| 595 | last = ret = node; |
| 596 | else { |
| 597 | last->next = node; |
| 598 | node->prev = last; |
| 599 | last = node; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | return(ret); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * xmlStringGetNodeList: |
| 608 | * @doc: the document |
| 609 | * @value: the value of the attribute |
| 610 | * |
| 611 | * Parse the value string and build the node list associated. Should |
| 612 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 613 | * Returns a pointer to the first child |
| 614 | */ |
| 615 | xmlNodePtr |
| 616 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
| 617 | xmlNodePtr ret = NULL, last = NULL; |
| 618 | xmlNodePtr node; |
| 619 | xmlChar *val; |
| 620 | const xmlChar *cur = value; |
| 621 | const xmlChar *q; |
| 622 | xmlEntityPtr ent; |
| 623 | |
| 624 | if (value == NULL) return(NULL); |
| 625 | |
| 626 | q = cur; |
| 627 | while (*cur != 0) { |
| 628 | if (*cur == '&') { |
| 629 | /* |
| 630 | * Save the current text. |
| 631 | */ |
| 632 | if (cur != q) { |
| 633 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 634 | xmlNodeAddContentLen(last, q, cur - q); |
| 635 | } else { |
| 636 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 637 | if (node == NULL) return(ret); |
| 638 | if (last == NULL) |
| 639 | last = ret = node; |
| 640 | else { |
| 641 | last->next = node; |
| 642 | node->prev = last; |
| 643 | last = node; |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | /* |
| 648 | * Read the entity string |
| 649 | */ |
| 650 | cur++; |
| 651 | q = cur; |
| 652 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 653 | if (*cur == 0) { |
| 654 | #ifdef DEBUG_TREE |
| 655 | xmlGenericError(xmlGenericErrorContext, |
| 656 | "xmlStringGetNodeList: unterminated entity %30s\n", q); |
| 657 | #endif |
| 658 | return(ret); |
| 659 | } |
| 660 | if (cur != q) { |
| 661 | /* |
| 662 | * Predefined entities don't generate nodes |
| 663 | */ |
| 664 | val = xmlStrndup(q, cur - q); |
| 665 | ent = xmlGetDocEntity(doc, val); |
| 666 | if ((ent != NULL) && |
| 667 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 668 | if (last == NULL) { |
| 669 | node = xmlNewDocText(doc, ent->content); |
| 670 | last = ret = node; |
| 671 | } else |
| 672 | xmlNodeAddContent(last, ent->content); |
| 673 | |
| 674 | } else { |
| 675 | /* |
| 676 | * Create a new REFERENCE_REF node |
| 677 | */ |
| 678 | node = xmlNewReference(doc, val); |
| 679 | if (node == NULL) { |
| 680 | if (val != NULL) xmlFree(val); |
| 681 | return(ret); |
| 682 | } |
| 683 | if (last == NULL) |
| 684 | last = ret = node; |
| 685 | else { |
| 686 | last->next = node; |
| 687 | node->prev = last; |
| 688 | last = node; |
| 689 | } |
| 690 | } |
| 691 | xmlFree(val); |
| 692 | } |
| 693 | cur++; |
| 694 | q = cur; |
| 695 | } else |
| 696 | cur++; |
| 697 | } |
| 698 | if (cur != q) { |
| 699 | /* |
| 700 | * Handle the last piece of text. |
| 701 | */ |
| 702 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 703 | xmlNodeAddContentLen(last, q, cur - q); |
| 704 | } else { |
| 705 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 706 | if (node == NULL) return(ret); |
| 707 | if (last == NULL) |
| 708 | last = ret = node; |
| 709 | else { |
| 710 | last->next = node; |
| 711 | node->prev = last; |
| 712 | last = node; |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | return(ret); |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * xmlNodeListGetString: |
| 721 | * @doc: the document |
| 722 | * @list: a Node list |
| 723 | * @inLine: should we replace entity contents or show their external form |
| 724 | * |
| 725 | * Returns the string equivalent to the text contained in the Node list |
| 726 | * made of TEXTs and ENTITY_REFs |
| 727 | * Returns a pointer to the string copy, the calller must free it. |
| 728 | */ |
| 729 | xmlChar * |
| 730 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) { |
| 731 | xmlNodePtr node = list; |
| 732 | xmlChar *ret = NULL; |
| 733 | xmlEntityPtr ent; |
| 734 | |
| 735 | if (list == NULL) return(NULL); |
| 736 | |
| 737 | while (node != NULL) { |
| 738 | if ((node->type == XML_TEXT_NODE) || |
| 739 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 740 | if (inLine) { |
| 741 | #ifndef XML_USE_BUFFER_CONTENT |
| 742 | ret = xmlStrcat(ret, node->content); |
| 743 | #else |
| 744 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 745 | #endif |
| 746 | } else { |
| 747 | xmlChar *buffer; |
| 748 | |
| 749 | #ifndef XML_USE_BUFFER_CONTENT |
| 750 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
| 751 | #else |
| 752 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 753 | xmlBufferContent(node->content)); |
| 754 | #endif |
| 755 | if (buffer != NULL) { |
| 756 | ret = xmlStrcat(ret, buffer); |
| 757 | xmlFree(buffer); |
| 758 | } |
| 759 | } |
| 760 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 761 | if (inLine) { |
| 762 | ent = xmlGetDocEntity(doc, node->name); |
| 763 | if (ent != NULL) |
| 764 | ret = xmlStrcat(ret, ent->content); |
| 765 | else { |
| 766 | #ifndef XML_USE_BUFFER_CONTENT |
| 767 | ret = xmlStrcat(ret, node->content); |
| 768 | #else |
| 769 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 770 | #endif |
| 771 | } |
| 772 | } else { |
| 773 | xmlChar buf[2]; |
| 774 | buf[0] = '&'; buf[1] = 0; |
| 775 | ret = xmlStrncat(ret, buf, 1); |
| 776 | ret = xmlStrcat(ret, node->name); |
| 777 | buf[0] = ';'; buf[1] = 0; |
| 778 | ret = xmlStrncat(ret, buf, 1); |
| 779 | } |
| 780 | } |
| 781 | #if 0 |
| 782 | else { |
| 783 | xmlGenericError(xmlGenericErrorContext, |
| 784 | "xmlGetNodeListString : invalide node type %d\n", |
| 785 | node->type); |
| 786 | } |
| 787 | #endif |
| 788 | node = node->next; |
| 789 | } |
| 790 | return(ret); |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * xmlNodeListGetRawString: |
| 795 | * @doc: the document |
| 796 | * @list: a Node list |
| 797 | * @inLine: should we replace entity contents or show their external form |
| 798 | * |
| 799 | * Returns the string equivalent to the text contained in the Node list |
| 800 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 801 | * this function doesn't do any character encoding handling. |
| 802 | * |
| 803 | * Returns a pointer to the string copy, the calller must free it. |
| 804 | */ |
| 805 | xmlChar * |
| 806 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) { |
| 807 | xmlNodePtr node = list; |
| 808 | xmlChar *ret = NULL; |
| 809 | xmlEntityPtr ent; |
| 810 | |
| 811 | if (list == NULL) return(NULL); |
| 812 | |
| 813 | while (node != NULL) { |
| 814 | if (node->type == XML_TEXT_NODE) { |
| 815 | if (inLine) { |
| 816 | #ifndef XML_USE_BUFFER_CONTENT |
| 817 | ret = xmlStrcat(ret, node->content); |
| 818 | #else |
| 819 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 820 | #endif |
| 821 | } else { |
| 822 | xmlChar *buffer; |
| 823 | |
| 824 | #ifndef XML_USE_BUFFER_CONTENT |
| 825 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 826 | #else |
| 827 | buffer = xmlEncodeSpecialChars(doc, |
| 828 | xmlBufferContent(node->content)); |
| 829 | #endif |
| 830 | if (buffer != NULL) { |
| 831 | ret = xmlStrcat(ret, buffer); |
| 832 | xmlFree(buffer); |
| 833 | } |
| 834 | } |
| 835 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 836 | if (inLine) { |
| 837 | ent = xmlGetDocEntity(doc, node->name); |
| 838 | if (ent != NULL) |
| 839 | ret = xmlStrcat(ret, ent->content); |
| 840 | else { |
| 841 | #ifndef XML_USE_BUFFER_CONTENT |
| 842 | ret = xmlStrcat(ret, node->content); |
| 843 | #else |
| 844 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 845 | #endif |
| 846 | } |
| 847 | } else { |
| 848 | xmlChar buf[2]; |
| 849 | buf[0] = '&'; buf[1] = 0; |
| 850 | ret = xmlStrncat(ret, buf, 1); |
| 851 | ret = xmlStrcat(ret, node->name); |
| 852 | buf[0] = ';'; buf[1] = 0; |
| 853 | ret = xmlStrncat(ret, buf, 1); |
| 854 | } |
| 855 | } |
| 856 | #if 0 |
| 857 | else { |
| 858 | xmlGenericError(xmlGenericErrorContext, |
| 859 | "xmlGetNodeListString : invalide node type %d\n", |
| 860 | node->type); |
| 861 | } |
| 862 | #endif |
| 863 | node = node->next; |
| 864 | } |
| 865 | return(ret); |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * xmlNewProp: |
| 870 | * @node: the holding node |
| 871 | * @name: the name of the attribute |
| 872 | * @value: the value of the attribute |
| 873 | * |
| 874 | * Create a new property carried by a node. |
| 875 | * Returns a pointer to the attribute |
| 876 | */ |
| 877 | xmlAttrPtr |
| 878 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
| 879 | xmlAttrPtr cur; |
| 880 | xmlDocPtr doc = NULL; |
| 881 | |
| 882 | if (name == NULL) { |
| 883 | #ifdef DEBUG_TREE |
| 884 | xmlGenericError(xmlGenericErrorContext, |
| 885 | "xmlNewProp : name == NULL\n"); |
| 886 | #endif |
| 887 | return(NULL); |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * Allocate a new property and fill the fields. |
| 892 | */ |
| 893 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 894 | if (cur == NULL) { |
| 895 | xmlGenericError(xmlGenericErrorContext, |
| 896 | "xmlNewProp : malloc failed\n"); |
| 897 | return(NULL); |
| 898 | } |
| 899 | memset(cur, 0, sizeof(xmlAttr)); |
| 900 | cur->type = XML_ATTRIBUTE_NODE; |
| 901 | |
| 902 | cur->parent = node; |
| 903 | if (node != NULL) { |
| 904 | doc = node->doc; |
| 905 | cur->doc = doc; |
| 906 | } |
| 907 | cur->name = xmlStrdup(name); |
| 908 | if (value != NULL) { |
| 909 | xmlChar *buffer; |
| 910 | xmlNodePtr tmp; |
| 911 | |
| 912 | buffer = xmlEncodeEntitiesReentrant(doc, value); |
| 913 | cur->children = xmlStringGetNodeList(doc, buffer); |
| 914 | cur->last = NULL; |
| 915 | tmp = cur->children; |
| 916 | while (tmp != NULL) { |
| 917 | tmp->parent = (xmlNodePtr) cur; |
| 918 | tmp->doc = doc; |
| 919 | if (tmp->next == NULL) |
| 920 | cur->last = tmp; |
| 921 | tmp = tmp->next; |
| 922 | } |
| 923 | xmlFree(buffer); |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Add it at the end to preserve parsing order ... |
| 928 | */ |
| 929 | if (node != NULL) { |
| 930 | if (node->properties == NULL) { |
| 931 | node->properties = cur; |
| 932 | } else { |
| 933 | xmlAttrPtr prev = node->properties; |
| 934 | |
| 935 | while (prev->next != NULL) prev = prev->next; |
| 936 | prev->next = cur; |
| 937 | cur->prev = prev; |
| 938 | } |
| 939 | } |
| 940 | return(cur); |
| 941 | } |
| 942 | |
| 943 | /** |
| 944 | * xmlNewNsProp: |
| 945 | * @node: the holding node |
| 946 | * @ns: the namespace |
| 947 | * @name: the name of the attribute |
| 948 | * @value: the value of the attribute |
| 949 | * |
| 950 | * Create a new property tagged with a namespace and carried by a node. |
| 951 | * Returns a pointer to the attribute |
| 952 | */ |
| 953 | xmlAttrPtr |
| 954 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 955 | const xmlChar *value) { |
| 956 | xmlAttrPtr cur; |
| 957 | |
| 958 | if (name == NULL) { |
| 959 | #ifdef DEBUG_TREE |
| 960 | xmlGenericError(xmlGenericErrorContext, |
| 961 | "xmlNewProp : name == NULL\n"); |
| 962 | #endif |
| 963 | return(NULL); |
| 964 | } |
| 965 | |
| 966 | /* |
| 967 | * Allocate a new property and fill the fields. |
| 968 | */ |
| 969 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 970 | if (cur == NULL) { |
| 971 | xmlGenericError(xmlGenericErrorContext, |
| 972 | "xmlNewProp : malloc failed\n"); |
| 973 | return(NULL); |
| 974 | } |
| 975 | memset(cur, 0, sizeof(xmlAttr)); |
| 976 | cur->type = XML_ATTRIBUTE_NODE; |
| 977 | |
| 978 | cur->parent = node; |
| 979 | if (node != NULL) |
| 980 | cur->doc = node->doc; |
| 981 | cur->ns = ns; |
| 982 | cur->name = xmlStrdup(name); |
| 983 | if (value != NULL) { |
| 984 | xmlChar *buffer; |
| 985 | xmlNodePtr tmp; |
| 986 | |
| 987 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 988 | cur->children = xmlStringGetNodeList(node->doc, buffer); |
| 989 | cur->last = NULL; |
| 990 | tmp = cur->children; |
| 991 | while (tmp != NULL) { |
| 992 | tmp->parent = (xmlNodePtr) cur; |
| 993 | if (tmp->next == NULL) |
| 994 | cur->last = tmp; |
| 995 | tmp = tmp->next; |
| 996 | } |
| 997 | xmlFree(buffer); |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | * Add it at the end to preserve parsing order ... |
| 1002 | */ |
| 1003 | if (node != NULL) { |
| 1004 | if (node->properties == NULL) { |
| 1005 | node->properties = cur; |
| 1006 | } else { |
| 1007 | xmlAttrPtr prev = node->properties; |
| 1008 | |
| 1009 | while (prev->next != NULL) prev = prev->next; |
| 1010 | prev->next = cur; |
| 1011 | cur->prev = prev; |
| 1012 | } |
| 1013 | } |
| 1014 | return(cur); |
| 1015 | } |
| 1016 | |
| 1017 | /** |
| 1018 | * xmlNewDocProp: |
| 1019 | * @doc: the document |
| 1020 | * @name: the name of the attribute |
| 1021 | * @value: the value of the attribute |
| 1022 | * |
| 1023 | * Create a new property carried by a document. |
| 1024 | * Returns a pointer to the attribute |
| 1025 | */ |
| 1026 | xmlAttrPtr |
| 1027 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
| 1028 | xmlAttrPtr cur; |
| 1029 | |
| 1030 | if (name == NULL) { |
| 1031 | #ifdef DEBUG_TREE |
| 1032 | xmlGenericError(xmlGenericErrorContext, |
| 1033 | "xmlNewProp : name == NULL\n"); |
| 1034 | #endif |
| 1035 | return(NULL); |
| 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | * Allocate a new property and fill the fields. |
| 1040 | */ |
| 1041 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1042 | if (cur == NULL) { |
| 1043 | xmlGenericError(xmlGenericErrorContext, |
| 1044 | "xmlNewProp : malloc failed\n"); |
| 1045 | return(NULL); |
| 1046 | } |
| 1047 | memset(cur, 0, sizeof(xmlAttr)); |
| 1048 | cur->type = XML_ATTRIBUTE_NODE; |
| 1049 | |
| 1050 | cur->name = xmlStrdup(name); |
| 1051 | cur->doc = doc; |
| 1052 | if (value != NULL) { |
| 1053 | xmlNodePtr tmp; |
| 1054 | |
| 1055 | cur->children = xmlStringGetNodeList(doc, value); |
| 1056 | cur->last = NULL; |
| 1057 | |
| 1058 | tmp = cur->children; |
| 1059 | while (tmp != NULL) { |
| 1060 | tmp->parent = (xmlNodePtr) cur; |
| 1061 | if (tmp->next == NULL) |
| 1062 | cur->last = tmp; |
| 1063 | tmp = tmp->next; |
| 1064 | } |
| 1065 | } |
| 1066 | return(cur); |
| 1067 | } |
| 1068 | |
| 1069 | /** |
| 1070 | * xmlFreePropList: |
| 1071 | * @cur: the first property in the list |
| 1072 | * |
| 1073 | * Free a property and all its siblings, all the children are freed too. |
| 1074 | */ |
| 1075 | void |
| 1076 | xmlFreePropList(xmlAttrPtr cur) { |
| 1077 | xmlAttrPtr next; |
| 1078 | if (cur == NULL) { |
| 1079 | #ifdef DEBUG_TREE |
| 1080 | xmlGenericError(xmlGenericErrorContext, |
| 1081 | "xmlFreePropList : property == NULL\n"); |
| 1082 | #endif |
| 1083 | return; |
| 1084 | } |
| 1085 | while (cur != NULL) { |
| 1086 | next = cur->next; |
| 1087 | xmlFreeProp(cur); |
| 1088 | cur = next; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | /** |
| 1093 | * xmlFreeProp: |
| 1094 | * @cur: an attribute |
| 1095 | * |
| 1096 | * Free one attribute, all the content is freed too |
| 1097 | */ |
| 1098 | void |
| 1099 | xmlFreeProp(xmlAttrPtr cur) { |
| 1100 | if (cur == NULL) { |
| 1101 | #ifdef DEBUG_TREE |
| 1102 | xmlGenericError(xmlGenericErrorContext, |
| 1103 | "xmlFreeProp : property == NULL\n"); |
| 1104 | #endif |
| 1105 | return; |
| 1106 | } |
| 1107 | /* Check for ID removal -> leading to invalid references ! */ |
| 1108 | if ((cur->parent != NULL) && |
| 1109 | (xmlIsID(cur->parent->doc, cur->parent, cur))) |
| 1110 | xmlRemoveID(cur->parent->doc, cur); |
| 1111 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 1112 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1113 | MEM_CLEANUP(cur, sizeof(xmlAttr)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1114 | xmlFree(cur); |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * xmlRemoveProp: |
| 1119 | * @cur: an attribute |
| 1120 | * |
| 1121 | * Unlink and free one attribute, all the content is freed too |
| 1122 | * Note this doesn't work for namespace definition attributes |
| 1123 | * |
| 1124 | * Returns 0 if success and -1 in case of error. |
| 1125 | */ |
| 1126 | int |
| 1127 | xmlRemoveProp(xmlAttrPtr cur) { |
| 1128 | xmlAttrPtr tmp; |
| 1129 | if (cur == NULL) { |
| 1130 | #ifdef DEBUG_TREE |
| 1131 | xmlGenericError(xmlGenericErrorContext, |
| 1132 | "xmlRemoveProp : cur == NULL\n"); |
| 1133 | #endif |
| 1134 | return(-1); |
| 1135 | } |
| 1136 | if (cur->parent == NULL) { |
| 1137 | #ifdef DEBUG_TREE |
| 1138 | xmlGenericError(xmlGenericErrorContext, |
| 1139 | "xmlRemoveProp : cur->parent == NULL\n"); |
| 1140 | #endif |
| 1141 | return(-1); |
| 1142 | } |
| 1143 | tmp = cur->parent->properties; |
| 1144 | if (tmp == cur) { |
| 1145 | cur->parent->properties = cur->next; |
| 1146 | xmlFreeProp(cur); |
| 1147 | return(0); |
| 1148 | } |
| 1149 | while (tmp != NULL) { |
| 1150 | if (tmp->next == cur) { |
| 1151 | tmp->next = cur->next; |
| 1152 | if (tmp->next != NULL) |
| 1153 | tmp->next->prev = tmp; |
| 1154 | xmlFreeProp(cur); |
| 1155 | return(0); |
| 1156 | } |
| 1157 | tmp = tmp->next; |
| 1158 | } |
| 1159 | #ifdef DEBUG_TREE |
| 1160 | xmlGenericError(xmlGenericErrorContext, |
| 1161 | "xmlRemoveProp : attribute not owned by its node\n"); |
| 1162 | #endif |
| 1163 | return(-1); |
| 1164 | } |
| 1165 | |
| 1166 | /** |
| 1167 | * xmlNewPI: |
| 1168 | * @name: the processing instruction name |
| 1169 | * @content: the PI content |
| 1170 | * |
| 1171 | * Creation of a processing instruction element. |
| 1172 | * Returns a pointer to the new node object. |
| 1173 | */ |
| 1174 | xmlNodePtr |
| 1175 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
| 1176 | xmlNodePtr cur; |
| 1177 | |
| 1178 | if (name == NULL) { |
| 1179 | #ifdef DEBUG_TREE |
| 1180 | xmlGenericError(xmlGenericErrorContext, |
| 1181 | "xmlNewPI : name == NULL\n"); |
| 1182 | #endif |
| 1183 | return(NULL); |
| 1184 | } |
| 1185 | |
| 1186 | /* |
| 1187 | * Allocate a new node and fill the fields. |
| 1188 | */ |
| 1189 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1190 | if (cur == NULL) { |
| 1191 | xmlGenericError(xmlGenericErrorContext, |
| 1192 | "xmlNewPI : malloc failed\n"); |
| 1193 | return(NULL); |
| 1194 | } |
| 1195 | memset(cur, 0, sizeof(xmlNode)); |
| 1196 | cur->type = XML_PI_NODE; |
| 1197 | |
| 1198 | cur->name = xmlStrdup(name); |
| 1199 | if (content != NULL) { |
| 1200 | #ifndef XML_USE_BUFFER_CONTENT |
| 1201 | cur->content = xmlStrdup(content); |
| 1202 | #else |
| 1203 | cur->content = xmlBufferCreateSize(0); |
| 1204 | xmlBufferSetAllocationScheme(cur->content, |
| 1205 | xmlGetBufferAllocationScheme()); |
| 1206 | xmlBufferAdd(cur->content, content, -1); |
| 1207 | #endif |
| 1208 | } |
| 1209 | return(cur); |
| 1210 | } |
| 1211 | |
| 1212 | /** |
| 1213 | * xmlNewNode: |
| 1214 | * @ns: namespace if any |
| 1215 | * @name: the node name |
| 1216 | * |
| 1217 | * Creation of a new node element. @ns is optionnal (NULL). |
| 1218 | * |
| 1219 | * Returns a pointer to the new node object. |
| 1220 | */ |
| 1221 | xmlNodePtr |
| 1222 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
| 1223 | xmlNodePtr cur; |
| 1224 | |
| 1225 | if (name == NULL) { |
| 1226 | #ifdef DEBUG_TREE |
| 1227 | xmlGenericError(xmlGenericErrorContext, |
| 1228 | "xmlNewNode : name == NULL\n"); |
| 1229 | #endif |
| 1230 | return(NULL); |
| 1231 | } |
| 1232 | |
| 1233 | /* |
| 1234 | * Allocate a new node and fill the fields. |
| 1235 | */ |
| 1236 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1237 | if (cur == NULL) { |
| 1238 | xmlGenericError(xmlGenericErrorContext, |
| 1239 | "xmlNewNode : malloc failed\n"); |
| 1240 | return(NULL); |
| 1241 | } |
| 1242 | memset(cur, 0, sizeof(xmlNode)); |
| 1243 | cur->type = XML_ELEMENT_NODE; |
| 1244 | |
| 1245 | cur->name = xmlStrdup(name); |
| 1246 | cur->ns = ns; |
| 1247 | return(cur); |
| 1248 | } |
| 1249 | |
| 1250 | /** |
| 1251 | * xmlNewDocNode: |
| 1252 | * @doc: the document |
| 1253 | * @ns: namespace if any |
| 1254 | * @name: the node name |
| 1255 | * @content: the XML text content if any |
| 1256 | * |
| 1257 | * Creation of a new node element within a document. @ns and @content |
| 1258 | * are optionnal (NULL). |
| 1259 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 1260 | * references, but XML special chars need to be escaped first by using |
| 1261 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 1262 | * need entities support. |
| 1263 | * |
| 1264 | * Returns a pointer to the new node object. |
| 1265 | */ |
| 1266 | xmlNodePtr |
| 1267 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
| 1268 | const xmlChar *name, const xmlChar *content) { |
| 1269 | xmlNodePtr cur; |
| 1270 | |
| 1271 | cur = xmlNewNode(ns, name); |
| 1272 | if (cur != NULL) { |
| 1273 | cur->doc = doc; |
| 1274 | if (content != NULL) { |
| 1275 | cur->children = xmlStringGetNodeList(doc, content); |
| 1276 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 1277 | } |
| 1278 | } |
| 1279 | return(cur); |
| 1280 | } |
| 1281 | |
| 1282 | |
| 1283 | /** |
| 1284 | * xmlNewDocRawNode: |
| 1285 | * @doc: the document |
| 1286 | * @ns: namespace if any |
| 1287 | * @name: the node name |
| 1288 | * @content: the text content if any |
| 1289 | * |
| 1290 | * Creation of a new node element within a document. @ns and @content |
| 1291 | * are optionnal (NULL). |
| 1292 | * |
| 1293 | * Returns a pointer to the new node object. |
| 1294 | */ |
| 1295 | xmlNodePtr |
| 1296 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 1297 | const xmlChar *name, const xmlChar *content) { |
| 1298 | xmlNodePtr cur; |
| 1299 | |
| 1300 | cur = xmlNewNode(ns, name); |
| 1301 | if (cur != NULL) { |
| 1302 | cur->doc = doc; |
| 1303 | if (content != NULL) { |
| 1304 | cur->children = xmlNewDocText(doc, content); |
| 1305 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 1306 | } |
| 1307 | } |
| 1308 | return(cur); |
| 1309 | } |
| 1310 | |
| 1311 | /** |
| 1312 | * xmlNewDocFragment: |
| 1313 | * @doc: the document owning the fragment |
| 1314 | * |
| 1315 | * Creation of a new Fragment node. |
| 1316 | * Returns a pointer to the new node object. |
| 1317 | */ |
| 1318 | xmlNodePtr |
| 1319 | xmlNewDocFragment(xmlDocPtr doc) { |
| 1320 | xmlNodePtr cur; |
| 1321 | |
| 1322 | /* |
| 1323 | * Allocate a new DocumentFragment node and fill the fields. |
| 1324 | */ |
| 1325 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1326 | if (cur == NULL) { |
| 1327 | xmlGenericError(xmlGenericErrorContext, |
| 1328 | "xmlNewDocFragment : malloc failed\n"); |
| 1329 | return(NULL); |
| 1330 | } |
| 1331 | memset(cur, 0, sizeof(xmlNode)); |
| 1332 | cur->type = XML_DOCUMENT_FRAG_NODE; |
| 1333 | |
| 1334 | cur->doc = doc; |
| 1335 | return(cur); |
| 1336 | } |
| 1337 | |
| 1338 | /** |
| 1339 | * xmlNewText: |
| 1340 | * @content: the text content |
| 1341 | * |
| 1342 | * Creation of a new text node. |
| 1343 | * Returns a pointer to the new node object. |
| 1344 | */ |
| 1345 | xmlNodePtr |
| 1346 | xmlNewText(const xmlChar *content) { |
| 1347 | xmlNodePtr cur; |
| 1348 | |
| 1349 | /* |
| 1350 | * Allocate a new node and fill the fields. |
| 1351 | */ |
| 1352 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1353 | if (cur == NULL) { |
| 1354 | xmlGenericError(xmlGenericErrorContext, |
| 1355 | "xmlNewText : malloc failed\n"); |
| 1356 | return(NULL); |
| 1357 | } |
| 1358 | memset(cur, 0, sizeof(xmlNode)); |
| 1359 | cur->type = XML_TEXT_NODE; |
| 1360 | |
| 1361 | cur->name = xmlStringText; |
| 1362 | if (content != NULL) { |
| 1363 | #ifndef XML_USE_BUFFER_CONTENT |
| 1364 | cur->content = xmlStrdup(content); |
| 1365 | #else |
| 1366 | cur->content = xmlBufferCreateSize(0); |
| 1367 | xmlBufferSetAllocationScheme(cur->content, |
| 1368 | xmlGetBufferAllocationScheme()); |
| 1369 | xmlBufferAdd(cur->content, content, -1); |
| 1370 | #endif |
| 1371 | } |
| 1372 | return(cur); |
| 1373 | } |
| 1374 | |
| 1375 | /** |
| 1376 | * xmlNewTextChild: |
| 1377 | * @parent: the parent node |
| 1378 | * @ns: a namespace if any |
| 1379 | * @name: the name of the child |
| 1380 | * @content: the text content of the child if any. |
| 1381 | * |
| 1382 | * Creation of a new child element, added at the end of @parent children list. |
| 1383 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1384 | * a child TEXT node will be created containing the string content. |
| 1385 | * |
| 1386 | * Returns a pointer to the new node object. |
| 1387 | */ |
| 1388 | xmlNodePtr |
| 1389 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 1390 | const xmlChar *name, const xmlChar *content) { |
| 1391 | xmlNodePtr cur, prev; |
| 1392 | |
| 1393 | if (parent == NULL) { |
| 1394 | #ifdef DEBUG_TREE |
| 1395 | xmlGenericError(xmlGenericErrorContext, |
| 1396 | "xmlNewTextChild : parent == NULL\n"); |
| 1397 | #endif |
| 1398 | return(NULL); |
| 1399 | } |
| 1400 | |
| 1401 | if (name == NULL) { |
| 1402 | #ifdef DEBUG_TREE |
| 1403 | xmlGenericError(xmlGenericErrorContext, |
| 1404 | "xmlNewTextChild : name == NULL\n"); |
| 1405 | #endif |
| 1406 | return(NULL); |
| 1407 | } |
| 1408 | |
| 1409 | /* |
| 1410 | * Allocate a new node |
| 1411 | */ |
| 1412 | if (ns == NULL) |
| 1413 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 1414 | else |
| 1415 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 1416 | if (cur == NULL) return(NULL); |
| 1417 | |
| 1418 | /* |
| 1419 | * add the new element at the end of the children list. |
| 1420 | */ |
| 1421 | cur->type = XML_ELEMENT_NODE; |
| 1422 | cur->parent = parent; |
| 1423 | cur->doc = parent->doc; |
| 1424 | if (parent->children == NULL) { |
| 1425 | parent->children = cur; |
| 1426 | parent->last = cur; |
| 1427 | } else { |
| 1428 | prev = parent->last; |
| 1429 | prev->next = cur; |
| 1430 | cur->prev = prev; |
| 1431 | parent->last = cur; |
| 1432 | } |
| 1433 | |
| 1434 | return(cur); |
| 1435 | } |
| 1436 | |
| 1437 | /** |
| 1438 | * xmlNewCharRef: |
| 1439 | * @doc: the document |
| 1440 | * @name: the char ref string, starting with # or "&# ... ;" |
| 1441 | * |
| 1442 | * Creation of a new character reference node. |
| 1443 | * Returns a pointer to the new node object. |
| 1444 | */ |
| 1445 | xmlNodePtr |
| 1446 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 1447 | xmlNodePtr cur; |
| 1448 | |
| 1449 | /* |
| 1450 | * Allocate a new node and fill the fields. |
| 1451 | */ |
| 1452 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1453 | if (cur == NULL) { |
| 1454 | xmlGenericError(xmlGenericErrorContext, |
| 1455 | "xmlNewText : malloc failed\n"); |
| 1456 | return(NULL); |
| 1457 | } |
| 1458 | memset(cur, 0, sizeof(xmlNode)); |
| 1459 | cur->type = XML_ENTITY_REF_NODE; |
| 1460 | |
| 1461 | cur->doc = doc; |
| 1462 | if (name[0] == '&') { |
| 1463 | int len; |
| 1464 | name++; |
| 1465 | len = xmlStrlen(name); |
| 1466 | if (name[len - 1] == ';') |
| 1467 | cur->name = xmlStrndup(name, len - 1); |
| 1468 | else |
| 1469 | cur->name = xmlStrndup(name, len); |
| 1470 | } else |
| 1471 | cur->name = xmlStrdup(name); |
| 1472 | return(cur); |
| 1473 | } |
| 1474 | |
| 1475 | /** |
| 1476 | * xmlNewReference: |
| 1477 | * @doc: the document |
| 1478 | * @name: the reference name, or the reference string with & and ; |
| 1479 | * |
| 1480 | * Creation of a new reference node. |
| 1481 | * Returns a pointer to the new node object. |
| 1482 | */ |
| 1483 | xmlNodePtr |
| 1484 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
| 1485 | xmlNodePtr cur; |
| 1486 | xmlEntityPtr ent; |
| 1487 | |
| 1488 | /* |
| 1489 | * Allocate a new node and fill the fields. |
| 1490 | */ |
| 1491 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1492 | if (cur == NULL) { |
| 1493 | xmlGenericError(xmlGenericErrorContext, |
| 1494 | "xmlNewText : malloc failed\n"); |
| 1495 | return(NULL); |
| 1496 | } |
| 1497 | memset(cur, 0, sizeof(xmlNode)); |
| 1498 | cur->type = XML_ENTITY_REF_NODE; |
| 1499 | |
| 1500 | cur->doc = doc; |
| 1501 | if (name[0] == '&') { |
| 1502 | int len; |
| 1503 | name++; |
| 1504 | len = xmlStrlen(name); |
| 1505 | if (name[len - 1] == ';') |
| 1506 | cur->name = xmlStrndup(name, len - 1); |
| 1507 | else |
| 1508 | cur->name = xmlStrndup(name, len); |
| 1509 | } else |
| 1510 | cur->name = xmlStrdup(name); |
| 1511 | |
| 1512 | ent = xmlGetDocEntity(doc, cur->name); |
| 1513 | if (ent != NULL) { |
| 1514 | #ifndef XML_USE_BUFFER_CONTENT |
| 1515 | cur->content = ent->content; |
| 1516 | #else |
| 1517 | /* |
| 1518 | * CJN 11.18.99 this might be a problem, since the xmlBuffer gets |
| 1519 | * a copy of this pointer. Let's hope we don't manipulate it |
| 1520 | * later |
| 1521 | */ |
| 1522 | cur->content = xmlBufferCreateSize(0); |
| 1523 | xmlBufferSetAllocationScheme(cur->content, |
| 1524 | xmlGetBufferAllocationScheme()); |
| 1525 | if (ent->content != NULL) |
| 1526 | xmlBufferAdd(cur->content, ent->content, -1); |
| 1527 | #endif |
| 1528 | /* |
| 1529 | * The parent pointer in entity is a Dtd pointer and thus is NOT |
| 1530 | * updated. Not sure if this is 100% correct. |
| 1531 | * -George |
| 1532 | */ |
| 1533 | cur->children = (xmlNodePtr) ent; |
| 1534 | cur->last = (xmlNodePtr) ent; |
| 1535 | } |
| 1536 | return(cur); |
| 1537 | } |
| 1538 | |
| 1539 | /** |
| 1540 | * xmlNewDocText: |
| 1541 | * @doc: the document |
| 1542 | * @content: the text content |
| 1543 | * |
| 1544 | * Creation of a new text node within a document. |
| 1545 | * Returns a pointer to the new node object. |
| 1546 | */ |
| 1547 | xmlNodePtr |
| 1548 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
| 1549 | xmlNodePtr cur; |
| 1550 | |
| 1551 | cur = xmlNewText(content); |
| 1552 | if (cur != NULL) cur->doc = doc; |
| 1553 | return(cur); |
| 1554 | } |
| 1555 | |
| 1556 | /** |
| 1557 | * xmlNewTextLen: |
| 1558 | * @content: the text content |
| 1559 | * @len: the text len. |
| 1560 | * |
| 1561 | * Creation of a new text node with an extra parameter for the content's lenght |
| 1562 | * Returns a pointer to the new node object. |
| 1563 | */ |
| 1564 | xmlNodePtr |
| 1565 | xmlNewTextLen(const xmlChar *content, int len) { |
| 1566 | xmlNodePtr cur; |
| 1567 | |
| 1568 | /* |
| 1569 | * Allocate a new node and fill the fields. |
| 1570 | */ |
| 1571 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1572 | if (cur == NULL) { |
| 1573 | xmlGenericError(xmlGenericErrorContext, |
| 1574 | "xmlNewText : malloc failed\n"); |
| 1575 | return(NULL); |
| 1576 | } |
| 1577 | memset(cur, 0, sizeof(xmlNode)); |
| 1578 | cur->type = XML_TEXT_NODE; |
| 1579 | |
| 1580 | cur->name = xmlStringText; |
| 1581 | if (content != NULL) { |
| 1582 | #ifndef XML_USE_BUFFER_CONTENT |
| 1583 | cur->content = xmlStrndup(content, len); |
| 1584 | #else |
| 1585 | cur->content = xmlBufferCreateSize(len); |
| 1586 | xmlBufferSetAllocationScheme(cur->content, |
| 1587 | xmlGetBufferAllocationScheme()); |
| 1588 | xmlBufferAdd(cur->content, content, len); |
| 1589 | #endif |
| 1590 | } |
| 1591 | return(cur); |
| 1592 | } |
| 1593 | |
| 1594 | /** |
| 1595 | * xmlNewDocTextLen: |
| 1596 | * @doc: the document |
| 1597 | * @content: the text content |
| 1598 | * @len: the text len. |
| 1599 | * |
| 1600 | * Creation of a new text node with an extra content lenght parameter. The |
| 1601 | * text node pertain to a given document. |
| 1602 | * Returns a pointer to the new node object. |
| 1603 | */ |
| 1604 | xmlNodePtr |
| 1605 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
| 1606 | xmlNodePtr cur; |
| 1607 | |
| 1608 | cur = xmlNewTextLen(content, len); |
| 1609 | if (cur != NULL) cur->doc = doc; |
| 1610 | return(cur); |
| 1611 | } |
| 1612 | |
| 1613 | /** |
| 1614 | * xmlNewComment: |
| 1615 | * @content: the comment content |
| 1616 | * |
| 1617 | * Creation of a new node containing a comment. |
| 1618 | * Returns a pointer to the new node object. |
| 1619 | */ |
| 1620 | xmlNodePtr |
| 1621 | xmlNewComment(const xmlChar *content) { |
| 1622 | xmlNodePtr cur; |
| 1623 | |
| 1624 | /* |
| 1625 | * Allocate a new node and fill the fields. |
| 1626 | */ |
| 1627 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1628 | if (cur == NULL) { |
| 1629 | xmlGenericError(xmlGenericErrorContext, |
| 1630 | "xmlNewComment : malloc failed\n"); |
| 1631 | return(NULL); |
| 1632 | } |
| 1633 | memset(cur, 0, sizeof(xmlNode)); |
| 1634 | cur->type = XML_COMMENT_NODE; |
| 1635 | |
| 1636 | cur->name = xmlStringComment; |
| 1637 | if (content != NULL) { |
| 1638 | #ifndef XML_USE_BUFFER_CONTENT |
| 1639 | cur->content = xmlStrdup(content); |
| 1640 | #else |
| 1641 | cur->content = xmlBufferCreateSize(0); |
| 1642 | xmlBufferSetAllocationScheme(cur->content, |
| 1643 | xmlGetBufferAllocationScheme()); |
| 1644 | xmlBufferAdd(cur->content, content, -1); |
| 1645 | #endif |
| 1646 | } |
| 1647 | return(cur); |
| 1648 | } |
| 1649 | |
| 1650 | /** |
| 1651 | * xmlNewCDataBlock: |
| 1652 | * @doc: the document |
| 1653 | * @content: the CData block content content |
| 1654 | * @len: the length of the block |
| 1655 | * |
| 1656 | * Creation of a new node containing a CData block. |
| 1657 | * Returns a pointer to the new node object. |
| 1658 | */ |
| 1659 | xmlNodePtr |
| 1660 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
| 1661 | xmlNodePtr cur; |
| 1662 | |
| 1663 | /* |
| 1664 | * Allocate a new node and fill the fields. |
| 1665 | */ |
| 1666 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1667 | if (cur == NULL) { |
| 1668 | xmlGenericError(xmlGenericErrorContext, |
| 1669 | "xmlNewCDataBlock : malloc failed\n"); |
| 1670 | return(NULL); |
| 1671 | } |
| 1672 | memset(cur, 0, sizeof(xmlNode)); |
| 1673 | cur->type = XML_CDATA_SECTION_NODE; |
| 1674 | |
| 1675 | if (content != NULL) { |
| 1676 | #ifndef XML_USE_BUFFER_CONTENT |
| 1677 | cur->content = xmlStrndup(content, len); |
| 1678 | #else |
| 1679 | cur->content = xmlBufferCreateSize(len); |
| 1680 | xmlBufferSetAllocationScheme(cur->content, |
| 1681 | xmlGetBufferAllocationScheme()); |
| 1682 | xmlBufferAdd(cur->content, content, len); |
| 1683 | #endif |
| 1684 | } |
| 1685 | return(cur); |
| 1686 | } |
| 1687 | |
| 1688 | /** |
| 1689 | * xmlNewDocComment: |
| 1690 | * @doc: the document |
| 1691 | * @content: the comment content |
| 1692 | * |
| 1693 | * Creation of a new node containing a commentwithin a document. |
| 1694 | * Returns a pointer to the new node object. |
| 1695 | */ |
| 1696 | xmlNodePtr |
| 1697 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
| 1698 | xmlNodePtr cur; |
| 1699 | |
| 1700 | cur = xmlNewComment(content); |
| 1701 | if (cur != NULL) cur->doc = doc; |
| 1702 | return(cur); |
| 1703 | } |
| 1704 | |
| 1705 | /** |
| 1706 | * xmlSetTreeDoc: |
| 1707 | * @tree: the top element |
| 1708 | * @doc: the document |
| 1709 | * |
| 1710 | * update all nodes under the tree to point to the right document |
| 1711 | */ |
| 1712 | void |
| 1713 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
| 1714 | if (tree == NULL) |
| 1715 | return; |
| 1716 | if (tree->type == XML_ENTITY_DECL) |
| 1717 | return; |
| 1718 | if (tree->doc != doc) { |
| 1719 | if (tree->children != NULL) |
| 1720 | xmlSetListDoc(tree->children, doc); |
| 1721 | tree->doc = doc; |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | /** |
| 1726 | * xmlSetListDoc: |
| 1727 | * @tree: the first element |
| 1728 | * @doc: the document |
| 1729 | * |
| 1730 | * update all nodes in the list to point to the right document |
| 1731 | */ |
| 1732 | void |
| 1733 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 1734 | xmlNodePtr cur; |
| 1735 | |
| 1736 | if (list == NULL) |
| 1737 | return; |
| 1738 | cur = list; |
| 1739 | while (cur != NULL) { |
| 1740 | if (cur->doc != doc) |
| 1741 | xmlSetTreeDoc(cur, doc); |
| 1742 | cur = cur->next; |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | |
| 1747 | /** |
| 1748 | * xmlNewChild: |
| 1749 | * @parent: the parent node |
| 1750 | * @ns: a namespace if any |
| 1751 | * @name: the name of the child |
| 1752 | * @content: the XML content of the child if any. |
| 1753 | * |
| 1754 | * Creation of a new child element, added at the end of @parent children list. |
| 1755 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1756 | * a child list containing the TEXTs and ENTITY_REFs node will be created. |
| 1757 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 1758 | * references, but XML special chars need to be escaped first by using |
| 1759 | * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities |
| 1760 | * support is not needed. |
| 1761 | * |
| 1762 | * Returns a pointer to the new node object. |
| 1763 | */ |
| 1764 | xmlNodePtr |
| 1765 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
| 1766 | const xmlChar *name, const xmlChar *content) { |
| 1767 | xmlNodePtr cur, prev; |
| 1768 | |
| 1769 | if (parent == NULL) { |
| 1770 | #ifdef DEBUG_TREE |
| 1771 | xmlGenericError(xmlGenericErrorContext, |
| 1772 | "xmlNewChild : parent == NULL\n"); |
| 1773 | #endif |
| 1774 | return(NULL); |
| 1775 | } |
| 1776 | |
| 1777 | if (name == NULL) { |
| 1778 | #ifdef DEBUG_TREE |
| 1779 | xmlGenericError(xmlGenericErrorContext, |
| 1780 | "xmlNewChild : name == NULL\n"); |
| 1781 | #endif |
| 1782 | return(NULL); |
| 1783 | } |
| 1784 | |
| 1785 | /* |
| 1786 | * Allocate a new node |
| 1787 | */ |
| 1788 | if (ns == NULL) |
| 1789 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
| 1790 | else |
| 1791 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
| 1792 | if (cur == NULL) return(NULL); |
| 1793 | |
| 1794 | /* |
| 1795 | * add the new element at the end of the children list. |
| 1796 | */ |
| 1797 | cur->type = XML_ELEMENT_NODE; |
| 1798 | cur->parent = parent; |
| 1799 | cur->doc = parent->doc; |
| 1800 | if (parent->children == NULL) { |
| 1801 | parent->children = cur; |
| 1802 | parent->last = cur; |
| 1803 | } else { |
| 1804 | prev = parent->last; |
| 1805 | prev->next = cur; |
| 1806 | cur->prev = prev; |
| 1807 | parent->last = cur; |
| 1808 | } |
| 1809 | |
| 1810 | return(cur); |
| 1811 | } |
| 1812 | |
| 1813 | /** |
| 1814 | * xmlAddNextSibling: |
| 1815 | * @cur: the child node |
| 1816 | * @elem: the new node |
| 1817 | * |
| 1818 | * Add a new element @elem as the next siblings of @cur |
| 1819 | * If the new element was already inserted in a document it is |
| 1820 | * first unlinked from its existing context. |
| 1821 | * As a result of text merging @elem may be freed. |
| 1822 | * |
| 1823 | * Returns the new element or NULL in case of error. |
| 1824 | */ |
| 1825 | xmlNodePtr |
| 1826 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1827 | if (cur == NULL) { |
| 1828 | #ifdef DEBUG_TREE |
| 1829 | xmlGenericError(xmlGenericErrorContext, |
| 1830 | "xmlAddNextSibling : cur == NULL\n"); |
| 1831 | #endif |
| 1832 | return(NULL); |
| 1833 | } |
| 1834 | if (elem == NULL) { |
| 1835 | #ifdef DEBUG_TREE |
| 1836 | xmlGenericError(xmlGenericErrorContext, |
| 1837 | "xmlAddNextSibling : elem == NULL\n"); |
| 1838 | #endif |
| 1839 | return(NULL); |
| 1840 | } |
| 1841 | |
| 1842 | xmlUnlinkNode(elem); |
| 1843 | |
| 1844 | if (elem->type == XML_TEXT_NODE) { |
| 1845 | if (cur->type == XML_TEXT_NODE) { |
| 1846 | #ifndef XML_USE_BUFFER_CONTENT |
| 1847 | xmlNodeAddContent(cur, elem->content); |
| 1848 | #else |
| 1849 | xmlNodeAddContent(cur, xmlBufferContent(elem->content)); |
| 1850 | #endif |
| 1851 | xmlFreeNode(elem); |
| 1852 | return(cur); |
| 1853 | } |
| 1854 | if ((cur->next != NULL) && (cur->type == XML_TEXT_NODE)) { |
| 1855 | #ifndef XML_USE_BUFFER_CONTENT |
| 1856 | xmlChar *tmp; |
| 1857 | |
| 1858 | tmp = xmlStrdup(elem->content); |
| 1859 | tmp = xmlStrcat(tmp, cur->next->content); |
| 1860 | xmlNodeSetContent(cur->next, tmp); |
| 1861 | xmlFree(tmp); |
| 1862 | #else |
| 1863 | xmlBufferAddHead(cur->next->content, |
| 1864 | xmlBufferContent(elem->content), |
| 1865 | xmlBufferLength(elem->content)); |
| 1866 | #endif |
| 1867 | xmlFreeNode(elem); |
| 1868 | return(cur->next); |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | if (elem->doc != cur->doc) { |
| 1873 | xmlSetTreeDoc(elem, cur->doc); |
| 1874 | } |
| 1875 | elem->parent = cur->parent; |
| 1876 | elem->prev = cur; |
| 1877 | elem->next = cur->next; |
| 1878 | cur->next = elem; |
| 1879 | if (elem->next != NULL) |
| 1880 | elem->next->prev = elem; |
| 1881 | if ((elem->parent != NULL) && (elem->parent->last == cur)) |
| 1882 | elem->parent->last = elem; |
| 1883 | return(elem); |
| 1884 | } |
| 1885 | |
| 1886 | /** |
| 1887 | * xmlAddPrevSibling: |
| 1888 | * @cur: the child node |
| 1889 | * @elem: the new node |
| 1890 | * |
| 1891 | * Add a new element @elem as the previous siblings of @cur |
| 1892 | * merging adjacent TEXT nodes (@elem may be freed) |
| 1893 | * If the new element was already inserted in a document it is |
| 1894 | * first unlinked from its existing context. |
| 1895 | * |
| 1896 | * Returns the new element or NULL in case of error. |
| 1897 | */ |
| 1898 | xmlNodePtr |
| 1899 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1900 | if (cur == NULL) { |
| 1901 | #ifdef DEBUG_TREE |
| 1902 | xmlGenericError(xmlGenericErrorContext, |
| 1903 | "xmlAddPrevSibling : cur == NULL\n"); |
| 1904 | #endif |
| 1905 | return(NULL); |
| 1906 | } |
| 1907 | if (elem == NULL) { |
| 1908 | #ifdef DEBUG_TREE |
| 1909 | xmlGenericError(xmlGenericErrorContext, |
| 1910 | "xmlAddPrevSibling : elem == NULL\n"); |
| 1911 | #endif |
| 1912 | return(NULL); |
| 1913 | } |
| 1914 | |
| 1915 | xmlUnlinkNode(elem); |
| 1916 | |
| 1917 | if (elem->type == XML_TEXT_NODE) { |
| 1918 | if (cur->type == XML_TEXT_NODE) { |
| 1919 | #ifndef XML_USE_BUFFER_CONTENT |
| 1920 | xmlChar *tmp; |
| 1921 | |
| 1922 | tmp = xmlStrdup(elem->content); |
| 1923 | tmp = xmlStrcat(tmp, cur->content); |
| 1924 | xmlNodeSetContent(cur, tmp); |
| 1925 | xmlFree(tmp); |
| 1926 | #else |
| 1927 | xmlBufferAddHead(cur->content, xmlBufferContent(elem->content), |
| 1928 | xmlBufferLength(elem->content)); |
| 1929 | #endif |
| 1930 | xmlFreeNode(elem); |
| 1931 | return(cur); |
| 1932 | } |
| 1933 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE)) { |
| 1934 | #ifndef XML_USE_BUFFER_CONTENT |
| 1935 | xmlNodeAddContent(cur->prev, elem->content); |
| 1936 | #else |
| 1937 | xmlNodeAddContent(cur->prev, xmlBufferContent(elem->content)); |
| 1938 | #endif |
| 1939 | xmlFreeNode(elem); |
| 1940 | return(cur->prev); |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | if (elem->doc != cur->doc) { |
| 1945 | xmlSetTreeDoc(elem, cur->doc); |
| 1946 | } |
| 1947 | elem->parent = cur->parent; |
| 1948 | elem->next = cur; |
| 1949 | elem->prev = cur->prev; |
| 1950 | cur->prev = elem; |
| 1951 | if (elem->prev != NULL) |
| 1952 | elem->prev->next = elem; |
| 1953 | if ((elem->parent != NULL) && (elem->parent->children == cur)) |
| 1954 | elem->parent->children = elem; |
| 1955 | return(elem); |
| 1956 | } |
| 1957 | |
| 1958 | /** |
| 1959 | * xmlAddSibling: |
| 1960 | * @cur: the child node |
| 1961 | * @elem: the new node |
| 1962 | * |
| 1963 | * Add a new element @elem to the list of siblings of @cur |
| 1964 | * merging adjacent TEXT nodes (@elem may be freed) |
| 1965 | * If the new element was already inserted in a document it is |
| 1966 | * first unlinked from its existing context. |
| 1967 | * |
| 1968 | * Returns the new element or NULL in case of error. |
| 1969 | */ |
| 1970 | xmlNodePtr |
| 1971 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1972 | xmlNodePtr parent; |
| 1973 | |
| 1974 | if (cur == NULL) { |
| 1975 | #ifdef DEBUG_TREE |
| 1976 | xmlGenericError(xmlGenericErrorContext, |
| 1977 | "xmlAddSibling : cur == NULL\n"); |
| 1978 | #endif |
| 1979 | return(NULL); |
| 1980 | } |
| 1981 | |
| 1982 | if (elem == NULL) { |
| 1983 | #ifdef DEBUG_TREE |
| 1984 | xmlGenericError(xmlGenericErrorContext, |
| 1985 | "xmlAddSibling : elem == NULL\n"); |
| 1986 | #endif |
| 1987 | return(NULL); |
| 1988 | } |
| 1989 | |
| 1990 | /* |
| 1991 | * Constant time is we can rely on the ->parent->last to find |
| 1992 | * the last sibling. |
| 1993 | */ |
| 1994 | if ((cur->parent != NULL) && |
| 1995 | (cur->parent->children != NULL) && |
| 1996 | (cur->parent->last != NULL) && |
| 1997 | (cur->parent->last->next == NULL)) { |
| 1998 | cur = cur->parent->last; |
| 1999 | } else { |
| 2000 | while (cur->next != NULL) cur = cur->next; |
| 2001 | } |
| 2002 | |
| 2003 | xmlUnlinkNode(elem); |
| 2004 | |
| 2005 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE)) { |
| 2006 | #ifndef XML_USE_BUFFER_CONTENT |
| 2007 | xmlNodeAddContent(cur, elem->content); |
| 2008 | #else |
| 2009 | xmlNodeAddContent(cur, xmlBufferContent(elem->content)); |
| 2010 | #endif |
| 2011 | xmlFreeNode(elem); |
| 2012 | return(cur); |
| 2013 | } |
| 2014 | |
| 2015 | if (elem->doc != cur->doc) { |
| 2016 | xmlSetTreeDoc(elem, cur->doc); |
| 2017 | } |
| 2018 | parent = cur->parent; |
| 2019 | elem->prev = cur; |
| 2020 | elem->next = NULL; |
| 2021 | elem->parent = parent; |
| 2022 | cur->next = elem; |
| 2023 | if (parent != NULL) |
| 2024 | parent->last = elem; |
| 2025 | |
| 2026 | return(elem); |
| 2027 | } |
| 2028 | |
| 2029 | /** |
| 2030 | * xmlAddChildList: |
| 2031 | * @parent: the parent node |
| 2032 | * @cur: the first node in the list |
| 2033 | * |
| 2034 | * Add a list of node at the end of the child list of the parent |
| 2035 | * merging adjacent TEXT nodes (@cur may be freed) |
| 2036 | * |
| 2037 | * Returns the last child or NULL in case of error. |
| 2038 | */ |
| 2039 | xmlNodePtr |
| 2040 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 2041 | xmlNodePtr prev; |
| 2042 | |
| 2043 | if (parent == NULL) { |
| 2044 | #ifdef DEBUG_TREE |
| 2045 | xmlGenericError(xmlGenericErrorContext, |
| 2046 | "xmlAddChild : parent == NULL\n"); |
| 2047 | #endif |
| 2048 | return(NULL); |
| 2049 | } |
| 2050 | |
| 2051 | if (cur == NULL) { |
| 2052 | #ifdef DEBUG_TREE |
| 2053 | xmlGenericError(xmlGenericErrorContext, |
| 2054 | "xmlAddChild : child == NULL\n"); |
| 2055 | #endif |
| 2056 | return(NULL); |
| 2057 | } |
| 2058 | |
| 2059 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 2060 | (cur->doc != parent->doc)) { |
| 2061 | #ifdef DEBUG_TREE |
| 2062 | xmlGenericError(xmlGenericErrorContext, |
| 2063 | "Elements moved to a different document\n"); |
| 2064 | #endif |
| 2065 | } |
| 2066 | |
| 2067 | /* |
| 2068 | * add the first element at the end of the children list. |
| 2069 | */ |
| 2070 | if (parent->children == NULL) { |
| 2071 | parent->children = cur; |
| 2072 | } else { |
| 2073 | /* |
| 2074 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 2075 | */ |
| 2076 | if ((cur->type == XML_TEXT_NODE) && |
| 2077 | (parent->last->type == XML_TEXT_NODE) && |
| 2078 | (cur->name == parent->last->name)) { |
| 2079 | #ifndef XML_USE_BUFFER_CONTENT |
| 2080 | xmlNodeAddContent(parent->last, cur->content); |
| 2081 | #else |
| 2082 | xmlNodeAddContent(parent->last, xmlBufferContent(cur->content)); |
| 2083 | #endif |
| 2084 | /* |
| 2085 | * if it's the only child, nothing more to be done. |
| 2086 | */ |
| 2087 | if (cur->next == NULL) { |
| 2088 | xmlFreeNode(cur); |
| 2089 | return(parent->last); |
| 2090 | } |
| 2091 | prev = cur; |
| 2092 | cur = cur->next; |
| 2093 | xmlFreeNode(prev); |
| 2094 | } |
| 2095 | prev = parent->last; |
| 2096 | prev->next = cur; |
| 2097 | cur->prev = prev; |
| 2098 | } |
| 2099 | while (cur->next != NULL) { |
| 2100 | cur->parent = parent; |
| 2101 | if (cur->doc != parent->doc) { |
| 2102 | xmlSetTreeDoc(cur, parent->doc); |
| 2103 | } |
| 2104 | cur = cur->next; |
| 2105 | } |
| 2106 | cur->parent = parent; |
| 2107 | cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ |
| 2108 | parent->last = cur; |
| 2109 | |
| 2110 | return(cur); |
| 2111 | } |
| 2112 | |
| 2113 | /** |
| 2114 | * xmlAddChild: |
| 2115 | * @parent: the parent node |
| 2116 | * @cur: the child node |
| 2117 | * |
| 2118 | * Add a new child element, to @parent, at the end of the child list |
| 2119 | * merging adjacent TEXT nodes (in which case @cur is freed) |
| 2120 | * Returns the child or NULL in case of error. |
| 2121 | */ |
| 2122 | xmlNodePtr |
| 2123 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
| 2124 | xmlNodePtr prev; |
| 2125 | |
| 2126 | if (parent == NULL) { |
| 2127 | #ifdef DEBUG_TREE |
| 2128 | xmlGenericError(xmlGenericErrorContext, |
| 2129 | "xmlAddChild : parent == NULL\n"); |
| 2130 | #endif |
| 2131 | return(NULL); |
| 2132 | } |
| 2133 | |
| 2134 | if (cur == NULL) { |
| 2135 | #ifdef DEBUG_TREE |
| 2136 | xmlGenericError(xmlGenericErrorContext, |
| 2137 | "xmlAddChild : child == NULL\n"); |
| 2138 | #endif |
| 2139 | return(NULL); |
| 2140 | } |
| 2141 | |
| 2142 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 2143 | (cur->doc != parent->doc)) { |
| 2144 | #ifdef DEBUG_TREE |
| 2145 | xmlGenericError(xmlGenericErrorContext, |
| 2146 | "Elements moved to a different document\n"); |
| 2147 | #endif |
| 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | * If cur is a TEXT node, merge its content with adjacent TEXT nodes |
| 2152 | * or with parent->content if parent->content != NULL. |
| 2153 | * cur is then freed. |
| 2154 | */ |
| 2155 | if (cur->type == XML_TEXT_NODE) { |
| 2156 | if (((parent->type == XML_ELEMENT_NODE) || |
| 2157 | (parent->type == XML_TEXT_NODE)) && |
| 2158 | (parent->content != NULL)) { |
| 2159 | #ifndef XML_USE_BUFFER_CONTENT |
| 2160 | xmlNodeAddContent(parent, cur->content); |
| 2161 | #else |
| 2162 | xmlNodeAddContent(parent, xmlBufferContent(cur->content)); |
| 2163 | #endif |
| 2164 | xmlFreeNode(cur); |
| 2165 | return(parent); |
| 2166 | } |
| 2167 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) && |
| 2168 | (parent->last->name == cur->name)) { |
| 2169 | #ifndef XML_USE_BUFFER_CONTENT |
| 2170 | xmlNodeAddContent(parent->last, cur->content); |
| 2171 | #else |
| 2172 | xmlNodeAddContent(parent->last, xmlBufferContent(cur->content)); |
| 2173 | #endif |
| 2174 | xmlFreeNode(cur); |
| 2175 | return(parent->last); |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | /* |
| 2180 | * add the new element at the end of the children list. |
| 2181 | */ |
| 2182 | cur->parent = parent; |
| 2183 | if (cur->doc != parent->doc) { |
| 2184 | xmlSetTreeDoc(cur, parent->doc); |
| 2185 | } |
| 2186 | |
| 2187 | /* |
| 2188 | * Handle the case where parent->content != NULL, in that case it will |
| 2189 | * create a intermediate TEXT node. |
| 2190 | */ |
| 2191 | if (((parent->type == XML_ELEMENT_NODE) || (parent->type == XML_TEXT_NODE)) && |
| 2192 | (parent->content != NULL)) { |
| 2193 | xmlNodePtr text; |
| 2194 | |
| 2195 | #ifndef XML_USE_BUFFER_CONTENT |
| 2196 | text = xmlNewDocText(parent->doc, parent->content); |
| 2197 | #else |
| 2198 | text = xmlNewDocText(parent->doc, xmlBufferContent(parent->content)); |
| 2199 | #endif |
| 2200 | if (text != NULL) { |
| 2201 | text->next = parent->children; |
| 2202 | if (text->next != NULL) |
| 2203 | text->next->prev = text; |
| 2204 | parent->children = text; |
| 2205 | UPDATE_LAST_CHILD_AND_PARENT(parent) |
| 2206 | #ifndef XML_USE_BUFFER_CONTENT |
| 2207 | xmlFree(parent->content); |
| 2208 | #else |
| 2209 | xmlBufferFree(parent->content); |
| 2210 | #endif |
| 2211 | parent->content = NULL; |
| 2212 | } |
| 2213 | } |
| 2214 | if (parent->children == NULL) { |
| 2215 | parent->children = cur; |
| 2216 | parent->last = cur; |
| 2217 | } else { |
| 2218 | prev = parent->last; |
| 2219 | prev->next = cur; |
| 2220 | cur->prev = prev; |
| 2221 | parent->last = cur; |
| 2222 | } |
| 2223 | |
| 2224 | return(cur); |
| 2225 | } |
| 2226 | |
| 2227 | /** |
| 2228 | * xmlGetLastChild: |
| 2229 | * @parent: the parent node |
| 2230 | * |
| 2231 | * Search the last child of a node. |
| 2232 | * Returns the last child or NULL if none. |
| 2233 | */ |
| 2234 | xmlNodePtr |
| 2235 | xmlGetLastChild(xmlNodePtr parent) { |
| 2236 | if (parent == NULL) { |
| 2237 | #ifdef DEBUG_TREE |
| 2238 | xmlGenericError(xmlGenericErrorContext, |
| 2239 | "xmlGetLastChild : parent == NULL\n"); |
| 2240 | #endif |
| 2241 | return(NULL); |
| 2242 | } |
| 2243 | return(parent->last); |
| 2244 | } |
| 2245 | |
| 2246 | /** |
| 2247 | * xmlFreeNodeList: |
| 2248 | * @cur: the first node in the list |
| 2249 | * |
| 2250 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 2251 | * the children are freed too. |
| 2252 | */ |
| 2253 | void |
| 2254 | xmlFreeNodeList(xmlNodePtr cur) { |
| 2255 | xmlNodePtr next; |
| 2256 | if (cur == NULL) { |
| 2257 | #ifdef DEBUG_TREE |
| 2258 | xmlGenericError(xmlGenericErrorContext, |
| 2259 | "xmlFreeNodeList : node == NULL\n"); |
| 2260 | #endif |
| 2261 | return; |
| 2262 | } |
| 2263 | while (cur != NULL) { |
| 2264 | next = cur->next; |
| 2265 | xmlFreeNode(cur); |
| 2266 | cur = next; |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | /** |
| 2271 | * xmlFreeNode: |
| 2272 | * @cur: the node |
| 2273 | * |
| 2274 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 2275 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
| 2276 | */ |
| 2277 | void |
| 2278 | xmlFreeNode(xmlNodePtr cur) { |
| 2279 | if (cur == NULL) { |
| 2280 | #ifdef DEBUG_TREE |
| 2281 | xmlGenericError(xmlGenericErrorContext, |
| 2282 | "xmlFreeNode : node == NULL\n"); |
| 2283 | #endif |
| 2284 | return; |
| 2285 | } |
| 2286 | if (cur->type == XML_DTD_NODE) |
| 2287 | return; |
| 2288 | cur->doc = NULL; |
| 2289 | cur->parent = NULL; |
| 2290 | cur->next = NULL; |
| 2291 | cur->prev = NULL; |
| 2292 | if ((cur->children != NULL) && |
| 2293 | (cur->type != XML_ENTITY_REF_NODE)) |
| 2294 | xmlFreeNodeList(cur->children); |
| 2295 | if (cur->properties != NULL) xmlFreePropList(cur->properties); |
| 2296 | if (cur->type != XML_ENTITY_REF_NODE) |
| 2297 | #ifndef XML_USE_BUFFER_CONTENT |
| 2298 | if (cur->content != NULL) xmlFree(cur->content); |
| 2299 | #else |
| 2300 | if (cur->content != NULL) xmlBufferFree(cur->content); |
| 2301 | #endif |
| 2302 | if ((cur->name != NULL) && |
| 2303 | (cur->name != xmlStringText) && |
| 2304 | (cur->name != xmlStringTextNoenc) && |
| 2305 | (cur->name != xmlStringComment)) |
| 2306 | xmlFree((char *) cur->name); |
| 2307 | if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2308 | MEM_CLEANUP(cur, sizeof(xmlNode)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2309 | xmlFree(cur); |
| 2310 | } |
| 2311 | |
| 2312 | /** |
| 2313 | * xmlUnlinkNode: |
| 2314 | * @cur: the node |
| 2315 | * |
| 2316 | * Unlink a node from it's current context, the node is not freed |
| 2317 | */ |
| 2318 | void |
| 2319 | xmlUnlinkNode(xmlNodePtr cur) { |
| 2320 | if (cur == NULL) { |
| 2321 | #ifdef DEBUG_TREE |
| 2322 | xmlGenericError(xmlGenericErrorContext, |
| 2323 | "xmlUnlinkNode : node == NULL\n"); |
| 2324 | #endif |
| 2325 | return; |
| 2326 | } |
| 2327 | if ((cur->parent != NULL) && (cur->parent->children == cur)) |
| 2328 | cur->parent->children = cur->next; |
| 2329 | if ((cur->parent != NULL) && (cur->parent->last == cur)) |
| 2330 | cur->parent->last = cur->prev; |
| 2331 | if (cur->next != NULL) |
| 2332 | cur->next->prev = cur->prev; |
| 2333 | if (cur->prev != NULL) |
| 2334 | cur->prev->next = cur->next; |
| 2335 | cur->next = cur->prev = NULL; |
| 2336 | cur->parent = NULL; |
| 2337 | } |
| 2338 | |
| 2339 | /** |
| 2340 | * xmlReplaceNode: |
| 2341 | * @old: the old node |
| 2342 | * @cur: the node |
| 2343 | * |
| 2344 | * Unlink the old node from it's current context, prune the new one |
| 2345 | * at the same place. If cur was already inserted in a document it is |
| 2346 | * first unlinked from its existing context. |
| 2347 | * |
| 2348 | * Returns the old node |
| 2349 | */ |
| 2350 | xmlNodePtr |
| 2351 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
| 2352 | if (old == NULL) { |
| 2353 | #ifdef DEBUG_TREE |
| 2354 | xmlGenericError(xmlGenericErrorContext, |
| 2355 | "xmlReplaceNode : old == NULL\n"); |
| 2356 | #endif |
| 2357 | return(NULL); |
| 2358 | } |
| 2359 | if (cur == NULL) { |
| 2360 | xmlUnlinkNode(old); |
| 2361 | return(old); |
| 2362 | } |
| 2363 | if (cur == old) { |
| 2364 | return(old); |
| 2365 | } |
| 2366 | xmlUnlinkNode(cur); |
| 2367 | cur->doc = old->doc; |
| 2368 | cur->parent = old->parent; |
| 2369 | cur->next = old->next; |
| 2370 | if (cur->next != NULL) |
| 2371 | cur->next->prev = cur; |
| 2372 | cur->prev = old->prev; |
| 2373 | if (cur->prev != NULL) |
| 2374 | cur->prev->next = cur; |
| 2375 | if (cur->parent != NULL) { |
| 2376 | if (cur->parent->children == old) |
| 2377 | cur->parent->children = cur; |
| 2378 | if (cur->parent->last == old) |
| 2379 | cur->parent->last = cur; |
| 2380 | } |
| 2381 | old->next = old->prev = NULL; |
| 2382 | old->parent = NULL; |
| 2383 | return(old); |
| 2384 | } |
| 2385 | |
| 2386 | /************************************************************************ |
| 2387 | * * |
| 2388 | * Copy operations * |
| 2389 | * * |
| 2390 | ************************************************************************/ |
| 2391 | |
| 2392 | /** |
| 2393 | * xmlCopyNamespace: |
| 2394 | * @cur: the namespace |
| 2395 | * |
| 2396 | * Do a copy of the namespace. |
| 2397 | * |
| 2398 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 2399 | */ |
| 2400 | xmlNsPtr |
| 2401 | xmlCopyNamespace(xmlNsPtr cur) { |
| 2402 | xmlNsPtr ret; |
| 2403 | |
| 2404 | if (cur == NULL) return(NULL); |
| 2405 | switch (cur->type) { |
| 2406 | case XML_LOCAL_NAMESPACE: |
| 2407 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 2408 | break; |
| 2409 | default: |
| 2410 | #ifdef DEBUG_TREE |
| 2411 | xmlGenericError(xmlGenericErrorContext, |
| 2412 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
| 2413 | #endif |
| 2414 | return(NULL); |
| 2415 | } |
| 2416 | return(ret); |
| 2417 | } |
| 2418 | |
| 2419 | /** |
| 2420 | * xmlCopyNamespaceList: |
| 2421 | * @cur: the first namespace |
| 2422 | * |
| 2423 | * Do a copy of an namespace list. |
| 2424 | * |
| 2425 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 2426 | */ |
| 2427 | xmlNsPtr |
| 2428 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 2429 | xmlNsPtr ret = NULL; |
| 2430 | xmlNsPtr p = NULL,q; |
| 2431 | |
| 2432 | while (cur != NULL) { |
| 2433 | q = xmlCopyNamespace(cur); |
| 2434 | if (p == NULL) { |
| 2435 | ret = p = q; |
| 2436 | } else { |
| 2437 | p->next = q; |
| 2438 | p = q; |
| 2439 | } |
| 2440 | cur = cur->next; |
| 2441 | } |
| 2442 | return(ret); |
| 2443 | } |
| 2444 | |
| 2445 | static xmlNodePtr |
| 2446 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
| 2447 | /** |
| 2448 | * xmlCopyProp: |
| 2449 | * @target: the element where the attribute will be grafted |
| 2450 | * @cur: the attribute |
| 2451 | * |
| 2452 | * Do a copy of the attribute. |
| 2453 | * |
| 2454 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 2455 | */ |
| 2456 | xmlAttrPtr |
| 2457 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
| 2458 | xmlAttrPtr ret; |
| 2459 | |
| 2460 | if (cur == NULL) return(NULL); |
| 2461 | if (target != NULL) |
| 2462 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
| 2463 | else if (cur->parent != NULL) |
| 2464 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 2465 | else if (cur->children != NULL) |
| 2466 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
| 2467 | else |
| 2468 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 2469 | if (ret == NULL) return(NULL); |
| 2470 | ret->parent = target; |
| 2471 | |
| 2472 | if ((cur->ns != NULL) && (target != NULL)) { |
| 2473 | xmlNsPtr ns; |
| 2474 | |
| 2475 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 2476 | ret->ns = ns; |
| 2477 | } else |
| 2478 | ret->ns = NULL; |
| 2479 | |
| 2480 | if (cur->children != NULL) { |
| 2481 | xmlNodePtr tmp; |
| 2482 | |
| 2483 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
| 2484 | ret->last = NULL; |
| 2485 | tmp = ret->children; |
| 2486 | while (tmp != NULL) { |
| 2487 | /* tmp->parent = (xmlNodePtr)ret; */ |
| 2488 | if (tmp->next == NULL) |
| 2489 | ret->last = tmp; |
| 2490 | tmp = tmp->next; |
| 2491 | } |
| 2492 | } |
| 2493 | return(ret); |
| 2494 | } |
| 2495 | |
| 2496 | /** |
| 2497 | * xmlCopyPropList: |
| 2498 | * @target: the element where the attributes will be grafted |
| 2499 | * @cur: the first attribute |
| 2500 | * |
| 2501 | * Do a copy of an attribute list. |
| 2502 | * |
| 2503 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 2504 | */ |
| 2505 | xmlAttrPtr |
| 2506 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
| 2507 | xmlAttrPtr ret = NULL; |
| 2508 | xmlAttrPtr p = NULL,q; |
| 2509 | |
| 2510 | while (cur != NULL) { |
| 2511 | q = xmlCopyProp(target, cur); |
| 2512 | if (p == NULL) { |
| 2513 | ret = p = q; |
| 2514 | } else { |
| 2515 | p->next = q; |
| 2516 | q->prev = p; |
| 2517 | p = q; |
| 2518 | } |
| 2519 | cur = cur->next; |
| 2520 | } |
| 2521 | return(ret); |
| 2522 | } |
| 2523 | |
| 2524 | /* |
| 2525 | * NOTE abeut the CopyNode operations ! |
| 2526 | * |
| 2527 | * They are splitted into external and internal parts for one |
| 2528 | * tricky reason: namespaces. Doing a direct copy of a node |
| 2529 | * say RPM:Copyright without changing the namespace pointer to |
| 2530 | * something else can produce stale links. One way to do it is |
| 2531 | * to keep a reference counter but this doesn't work as soon |
| 2532 | * as one move the element or the subtree out of the scope of |
| 2533 | * the existing namespace. The actual solution seems to add |
| 2534 | * a copy of the namespace at the top of the copied tree if |
| 2535 | * not available in the subtree. |
| 2536 | * Hence two functions, the public front-end call the inner ones |
| 2537 | */ |
| 2538 | |
| 2539 | static xmlNodePtr |
| 2540 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
| 2541 | |
| 2542 | static xmlNodePtr |
| 2543 | xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
| 2544 | int recursive) { |
| 2545 | xmlNodePtr ret; |
| 2546 | |
| 2547 | if (node == NULL) return(NULL); |
| 2548 | /* |
| 2549 | * Allocate a new node and fill the fields. |
| 2550 | */ |
| 2551 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2552 | if (ret == NULL) { |
| 2553 | xmlGenericError(xmlGenericErrorContext, |
| 2554 | "xmlStaticCopyNode : malloc failed\n"); |
| 2555 | return(NULL); |
| 2556 | } |
| 2557 | memset(ret, 0, sizeof(xmlNode)); |
| 2558 | ret->type = node->type; |
| 2559 | |
| 2560 | ret->doc = doc; |
| 2561 | ret->parent = parent; |
| 2562 | if (node->name == xmlStringText) |
| 2563 | ret->name = xmlStringText; |
| 2564 | else if (node->name == xmlStringTextNoenc) |
| 2565 | ret->name = xmlStringTextNoenc; |
| 2566 | else if (node->name == xmlStringComment) |
| 2567 | ret->name = xmlStringComment; |
| 2568 | else if (node->name != NULL) |
| 2569 | ret->name = xmlStrdup(node->name); |
| 2570 | if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) { |
| 2571 | #ifndef XML_USE_BUFFER_CONTENT |
| 2572 | ret->content = xmlStrdup(node->content); |
| 2573 | #else |
| 2574 | ret->content = xmlBufferCreateSize(xmlBufferLength(node->content)); |
| 2575 | xmlBufferSetAllocationScheme(ret->content, |
| 2576 | xmlGetBufferAllocationScheme()); |
| 2577 | xmlBufferAdd(ret->content, |
| 2578 | xmlBufferContent(node->content), |
| 2579 | xmlBufferLength(node->content)); |
| 2580 | #endif |
| 2581 | } |
| 2582 | if (parent != NULL) |
| 2583 | xmlAddChild(parent, ret); |
| 2584 | |
| 2585 | if (!recursive) return(ret); |
| 2586 | if (node->nsDef != NULL) |
| 2587 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 2588 | |
| 2589 | if (node->ns != NULL) { |
| 2590 | xmlNsPtr ns; |
| 2591 | |
| 2592 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 2593 | if (ns == NULL) { |
| 2594 | /* |
| 2595 | * Humm, we are copying an element whose namespace is defined |
| 2596 | * out of the new tree scope. Search it in the original tree |
| 2597 | * and add it at the top of the new tree |
| 2598 | */ |
| 2599 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 2600 | if (ns != NULL) { |
| 2601 | xmlNodePtr root = ret; |
| 2602 | |
| 2603 | while (root->parent != NULL) root = root->parent; |
| 2604 | xmlNewNs(root, ns->href, ns->prefix); |
| 2605 | } |
| 2606 | } else { |
| 2607 | /* |
| 2608 | * reference the existing namespace definition in our own tree. |
| 2609 | */ |
| 2610 | ret->ns = ns; |
| 2611 | } |
| 2612 | } |
| 2613 | if (node->properties != NULL) |
| 2614 | ret->properties = xmlCopyPropList(ret, node->properties); |
| 2615 | if (node->children != NULL) |
| 2616 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
| 2617 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
| 2618 | return(ret); |
| 2619 | } |
| 2620 | |
| 2621 | static xmlNodePtr |
| 2622 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 2623 | xmlNodePtr ret = NULL; |
| 2624 | xmlNodePtr p = NULL,q; |
| 2625 | |
| 2626 | while (node != NULL) { |
| 2627 | if( node->type == XML_DTD_NODE ) |
| 2628 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
| 2629 | else |
| 2630 | q = xmlStaticCopyNode(node, doc, parent, 1); |
| 2631 | if (ret == NULL) { |
| 2632 | q->prev = NULL; |
| 2633 | ret = p = q; |
| 2634 | } else { |
| 2635 | p->next = q; |
| 2636 | q->prev = p; |
| 2637 | p = q; |
| 2638 | } |
| 2639 | node = node->next; |
| 2640 | } |
| 2641 | return(ret); |
| 2642 | } |
| 2643 | |
| 2644 | /** |
| 2645 | * xmlCopyNode: |
| 2646 | * @node: the node |
| 2647 | * @recursive: if 1 do a recursive copy. |
| 2648 | * |
| 2649 | * Do a copy of the node. |
| 2650 | * |
| 2651 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 2652 | */ |
| 2653 | xmlNodePtr |
| 2654 | xmlCopyNode(xmlNodePtr node, int recursive) { |
| 2655 | xmlNodePtr ret; |
| 2656 | |
| 2657 | ret = xmlStaticCopyNode(node, NULL, NULL, recursive); |
| 2658 | return(ret); |
| 2659 | } |
| 2660 | |
| 2661 | /** |
| 2662 | * xmlCopyNodeList: |
| 2663 | * @node: the first node in the list. |
| 2664 | * |
| 2665 | * Do a recursive copy of the node list. |
| 2666 | * |
| 2667 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 2668 | */ |
| 2669 | xmlNodePtr xmlCopyNodeList(xmlNodePtr node) { |
| 2670 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 2671 | return(ret); |
| 2672 | } |
| 2673 | |
| 2674 | /** |
| 2675 | * xmlCopyElement: |
| 2676 | * @elem: the element |
| 2677 | * |
| 2678 | * Do a copy of the element definition. |
| 2679 | * |
| 2680 | * Returns: a new xmlElementPtr, or NULL in case of error. |
| 2681 | xmlElementPtr |
| 2682 | xmlCopyElement(xmlElementPtr elem) { |
| 2683 | xmlElementPtr ret; |
| 2684 | |
| 2685 | if (elem == NULL) return(NULL); |
| 2686 | ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content); |
| 2687 | if (ret == NULL) return(NULL); |
| 2688 | if (!recursive) return(ret); |
| 2689 | if (elem->properties != NULL) |
| 2690 | ret->properties = xmlCopyPropList(elem->properties); |
| 2691 | |
| 2692 | if (elem->nsDef != NULL) |
| 2693 | ret->nsDef = xmlCopyNamespaceList(elem->nsDef); |
| 2694 | if (elem->children != NULL) |
| 2695 | ret->children = xmlCopyElementList(elem->children); |
| 2696 | return(ret); |
| 2697 | } |
| 2698 | */ |
| 2699 | |
| 2700 | /** |
| 2701 | * xmlCopyDtd: |
| 2702 | * @dtd: the dtd |
| 2703 | * |
| 2704 | * Do a copy of the dtd. |
| 2705 | * |
| 2706 | * Returns: a new xmlDtdPtr, or NULL in case of error. |
| 2707 | */ |
| 2708 | xmlDtdPtr |
| 2709 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 2710 | xmlDtdPtr ret; |
| 2711 | |
| 2712 | if (dtd == NULL) return(NULL); |
| 2713 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 2714 | if (ret == NULL) return(NULL); |
| 2715 | if (dtd->entities != NULL) |
| 2716 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 2717 | (xmlEntitiesTablePtr) dtd->entities); |
| 2718 | if (dtd->notations != NULL) |
| 2719 | ret->notations = (void *) xmlCopyNotationTable( |
| 2720 | (xmlNotationTablePtr) dtd->notations); |
| 2721 | if (dtd->elements != NULL) |
| 2722 | ret->elements = (void *) xmlCopyElementTable( |
| 2723 | (xmlElementTablePtr) dtd->elements); |
| 2724 | if (dtd->attributes != NULL) |
| 2725 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 2726 | (xmlAttributeTablePtr) dtd->attributes); |
| 2727 | return(ret); |
| 2728 | } |
| 2729 | |
| 2730 | /** |
| 2731 | * xmlCopyDoc: |
| 2732 | * @doc: the document |
| 2733 | * @recursive: if 1 do a recursive copy. |
| 2734 | * |
| 2735 | * Do a copy of the document info. If recursive, the content tree will |
| 2736 | * be copied too as well as Dtd, namespaces and entities. |
| 2737 | * |
| 2738 | * Returns: a new xmlDocPtr, or NULL in case of error. |
| 2739 | */ |
| 2740 | xmlDocPtr |
| 2741 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 2742 | xmlDocPtr ret; |
| 2743 | |
| 2744 | if (doc == NULL) return(NULL); |
| 2745 | ret = xmlNewDoc(doc->version); |
| 2746 | if (ret == NULL) return(NULL); |
| 2747 | if (doc->name != NULL) |
| 2748 | ret->name = xmlMemStrdup(doc->name); |
| 2749 | if (doc->encoding != NULL) |
| 2750 | ret->encoding = xmlStrdup(doc->encoding); |
| 2751 | ret->charset = doc->charset; |
| 2752 | ret->compression = doc->compression; |
| 2753 | ret->standalone = doc->standalone; |
| 2754 | if (!recursive) return(ret); |
| 2755 | |
| 2756 | if (doc->intSubset != NULL) |
| 2757 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
| 2758 | if (doc->oldNs != NULL) |
| 2759 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 2760 | if (doc->children != NULL) { |
| 2761 | xmlNodePtr tmp; |
| 2762 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 2763 | (xmlNodePtr)ret); |
| 2764 | ret->last = NULL; |
| 2765 | tmp = ret->children; |
| 2766 | while (tmp != NULL) { |
| 2767 | if (tmp->next == NULL) |
| 2768 | ret->last = tmp; |
| 2769 | tmp = tmp->next; |
| 2770 | } |
| 2771 | } |
| 2772 | return(ret); |
| 2773 | } |
| 2774 | |
| 2775 | /************************************************************************ |
| 2776 | * * |
| 2777 | * Content access functions * |
| 2778 | * * |
| 2779 | ************************************************************************/ |
| 2780 | |
| 2781 | /** |
| 2782 | * xmlDocGetRootElement: |
| 2783 | * @doc: the document |
| 2784 | * |
| 2785 | * Get the root element of the document (doc->children is a list |
| 2786 | * containing possibly comments, PIs, etc ...). |
| 2787 | * |
| 2788 | * Returns the xmlNodePtr for the root or NULL |
| 2789 | */ |
| 2790 | xmlNodePtr |
| 2791 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 2792 | xmlNodePtr ret; |
| 2793 | |
| 2794 | if (doc == NULL) return(NULL); |
| 2795 | ret = doc->children; |
| 2796 | while (ret != NULL) { |
| 2797 | if (ret->type == XML_ELEMENT_NODE) |
| 2798 | return(ret); |
| 2799 | ret = ret->next; |
| 2800 | } |
| 2801 | return(ret); |
| 2802 | } |
| 2803 | |
| 2804 | /** |
| 2805 | * xmlDocSetRootElement: |
| 2806 | * @doc: the document |
| 2807 | * @root: the new document root element |
| 2808 | * |
| 2809 | * Set the root element of the document (doc->children is a list |
| 2810 | * containing possibly comments, PIs, etc ...). |
| 2811 | * |
| 2812 | * Returns the old root element if any was found |
| 2813 | */ |
| 2814 | xmlNodePtr |
| 2815 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 2816 | xmlNodePtr old = NULL; |
| 2817 | |
| 2818 | if (doc == NULL) return(NULL); |
| 2819 | old = doc->children; |
| 2820 | while (old != NULL) { |
| 2821 | if (old->type == XML_ELEMENT_NODE) |
| 2822 | break; |
| 2823 | old = old->next; |
| 2824 | } |
| 2825 | if (old == NULL) { |
| 2826 | if (doc->children == NULL) { |
| 2827 | doc->children = root; |
| 2828 | doc->last = root; |
| 2829 | } else { |
| 2830 | xmlAddSibling(doc->children, root); |
| 2831 | } |
| 2832 | } else { |
| 2833 | xmlReplaceNode(old, root); |
| 2834 | } |
| 2835 | return(old); |
| 2836 | } |
| 2837 | |
| 2838 | /** |
| 2839 | * xmlNodeSetLang: |
| 2840 | * @cur: the node being changed |
| 2841 | * @lang: the langage description |
| 2842 | * |
| 2843 | * Set the language of a node, i.e. the values of the xml:lang |
| 2844 | * attribute. |
| 2845 | */ |
| 2846 | void |
| 2847 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
| 2848 | if (cur == NULL) return; |
| 2849 | switch(cur->type) { |
| 2850 | case XML_TEXT_NODE: |
| 2851 | case XML_CDATA_SECTION_NODE: |
| 2852 | case XML_COMMENT_NODE: |
| 2853 | case XML_DOCUMENT_NODE: |
| 2854 | case XML_DOCUMENT_TYPE_NODE: |
| 2855 | case XML_DOCUMENT_FRAG_NODE: |
| 2856 | case XML_NOTATION_NODE: |
| 2857 | case XML_HTML_DOCUMENT_NODE: |
| 2858 | case XML_DTD_NODE: |
| 2859 | case XML_ELEMENT_DECL: |
| 2860 | case XML_ATTRIBUTE_DECL: |
| 2861 | case XML_ENTITY_DECL: |
| 2862 | case XML_PI_NODE: |
| 2863 | case XML_ENTITY_REF_NODE: |
| 2864 | case XML_ENTITY_NODE: |
| 2865 | case XML_NAMESPACE_DECL: |
| 2866 | #ifdef LIBXML_SGML_ENABLED |
| 2867 | case XML_SGML_DOCUMENT_NODE: |
| 2868 | #endif |
| 2869 | case XML_XINCLUDE_START: |
| 2870 | case XML_XINCLUDE_END: |
| 2871 | return; |
| 2872 | case XML_ELEMENT_NODE: |
| 2873 | case XML_ATTRIBUTE_NODE: |
| 2874 | break; |
| 2875 | } |
| 2876 | xmlSetProp(cur, BAD_CAST "xml:lang", lang); |
| 2877 | } |
| 2878 | |
| 2879 | /** |
| 2880 | * xmlNodeGetLang: |
| 2881 | * @cur: the node being checked |
| 2882 | * |
| 2883 | * Searches the language of a node, i.e. the values of the xml:lang |
| 2884 | * attribute or the one carried by the nearest ancestor. |
| 2885 | * |
| 2886 | * Returns a pointer to the lang value, or NULL if not found |
| 2887 | * It's up to the caller to free the memory. |
| 2888 | */ |
| 2889 | xmlChar * |
| 2890 | xmlNodeGetLang(xmlNodePtr cur) { |
| 2891 | xmlChar *lang; |
| 2892 | |
| 2893 | while (cur != NULL) { |
| 2894 | lang = xmlGetProp(cur, BAD_CAST "xml:lang"); |
| 2895 | if (lang != NULL) |
| 2896 | return(lang); |
| 2897 | cur = cur->parent; |
| 2898 | } |
| 2899 | return(NULL); |
| 2900 | } |
| 2901 | |
| 2902 | |
| 2903 | /** |
| 2904 | * xmlNodeSetSpacePreserve: |
| 2905 | * @cur: the node being changed |
| 2906 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 2907 | * |
| 2908 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 2909 | * value of the xml:space attribute. |
| 2910 | */ |
| 2911 | void |
| 2912 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
| 2913 | if (cur == NULL) return; |
| 2914 | switch(cur->type) { |
| 2915 | case XML_TEXT_NODE: |
| 2916 | case XML_CDATA_SECTION_NODE: |
| 2917 | case XML_COMMENT_NODE: |
| 2918 | case XML_DOCUMENT_NODE: |
| 2919 | case XML_DOCUMENT_TYPE_NODE: |
| 2920 | case XML_DOCUMENT_FRAG_NODE: |
| 2921 | case XML_NOTATION_NODE: |
| 2922 | case XML_HTML_DOCUMENT_NODE: |
| 2923 | case XML_DTD_NODE: |
| 2924 | case XML_ELEMENT_DECL: |
| 2925 | case XML_ATTRIBUTE_DECL: |
| 2926 | case XML_ENTITY_DECL: |
| 2927 | case XML_PI_NODE: |
| 2928 | case XML_ENTITY_REF_NODE: |
| 2929 | case XML_ENTITY_NODE: |
| 2930 | case XML_NAMESPACE_DECL: |
| 2931 | case XML_XINCLUDE_START: |
| 2932 | case XML_XINCLUDE_END: |
| 2933 | #ifdef LIBXML_SGML_ENABLED |
| 2934 | case XML_SGML_DOCUMENT_NODE: |
| 2935 | #endif |
| 2936 | return; |
| 2937 | case XML_ELEMENT_NODE: |
| 2938 | case XML_ATTRIBUTE_NODE: |
| 2939 | break; |
| 2940 | } |
| 2941 | switch (val) { |
| 2942 | case 0: |
| 2943 | xmlSetProp(cur, BAD_CAST "xml:space", BAD_CAST "default"); |
| 2944 | break; |
| 2945 | case 1: |
| 2946 | xmlSetProp(cur, BAD_CAST "xml:space", |
| 2947 | BAD_CAST "preserve"); |
| 2948 | break; |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | /** |
| 2953 | * xmlNodeGetSpacePreserve: |
| 2954 | * @cur: the node being checked |
| 2955 | * |
| 2956 | * Searches the space preserving behaviour of a node, i.e. the values |
| 2957 | * of the xml:space attribute or the one carried by the nearest |
| 2958 | * ancestor. |
| 2959 | * |
| 2960 | * Returns -1 if xml:space is not inheried, 0 if "default", 1 if "preserve" |
| 2961 | */ |
| 2962 | int |
| 2963 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 2964 | xmlChar *space; |
| 2965 | |
| 2966 | while (cur != NULL) { |
| 2967 | space = xmlGetProp(cur, BAD_CAST "xml:space"); |
| 2968 | if (space != NULL) { |
| 2969 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
| 2970 | xmlFree(space); |
| 2971 | return(1); |
| 2972 | } |
| 2973 | if (xmlStrEqual(space, BAD_CAST "default")) { |
| 2974 | xmlFree(space); |
| 2975 | return(0); |
| 2976 | } |
| 2977 | xmlFree(space); |
| 2978 | } |
| 2979 | cur = cur->parent; |
| 2980 | } |
| 2981 | return(-1); |
| 2982 | } |
| 2983 | |
| 2984 | /** |
| 2985 | * xmlNodeSetName: |
| 2986 | * @cur: the node being changed |
| 2987 | * @name: the new tag name |
| 2988 | * |
| 2989 | * Set (or reset) the name of a node. |
| 2990 | */ |
| 2991 | void |
| 2992 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
| 2993 | if (cur == NULL) return; |
| 2994 | if (name == NULL) return; |
| 2995 | switch(cur->type) { |
| 2996 | case XML_TEXT_NODE: |
| 2997 | case XML_CDATA_SECTION_NODE: |
| 2998 | case XML_COMMENT_NODE: |
| 2999 | case XML_DOCUMENT_TYPE_NODE: |
| 3000 | case XML_DOCUMENT_FRAG_NODE: |
| 3001 | case XML_NOTATION_NODE: |
| 3002 | case XML_HTML_DOCUMENT_NODE: |
| 3003 | case XML_NAMESPACE_DECL: |
| 3004 | case XML_XINCLUDE_START: |
| 3005 | case XML_XINCLUDE_END: |
| 3006 | #ifdef LIBXML_SGML_ENABLED |
| 3007 | case XML_SGML_DOCUMENT_NODE: |
| 3008 | #endif |
| 3009 | return; |
| 3010 | case XML_ELEMENT_NODE: |
| 3011 | case XML_ATTRIBUTE_NODE: |
| 3012 | case XML_PI_NODE: |
| 3013 | case XML_ENTITY_REF_NODE: |
| 3014 | case XML_ENTITY_NODE: |
| 3015 | case XML_DTD_NODE: |
| 3016 | case XML_DOCUMENT_NODE: |
| 3017 | case XML_ELEMENT_DECL: |
| 3018 | case XML_ATTRIBUTE_DECL: |
| 3019 | case XML_ENTITY_DECL: |
| 3020 | break; |
| 3021 | } |
| 3022 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 3023 | cur->name = xmlStrdup(name); |
| 3024 | } |
| 3025 | |
| 3026 | /** |
| 3027 | * xmlNodeSetBase: |
| 3028 | * @cur: the node being changed |
| 3029 | * @uri: the new base URI |
| 3030 | * |
| 3031 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 3032 | * xml:base attribute. |
| 3033 | */ |
| 3034 | void |
| 3035 | xmlNodeSetBase(xmlNodePtr cur, xmlChar* uri) { |
| 3036 | if (cur == NULL) return; |
| 3037 | switch(cur->type) { |
| 3038 | case XML_TEXT_NODE: |
| 3039 | case XML_CDATA_SECTION_NODE: |
| 3040 | case XML_COMMENT_NODE: |
| 3041 | case XML_DOCUMENT_NODE: |
| 3042 | case XML_DOCUMENT_TYPE_NODE: |
| 3043 | case XML_DOCUMENT_FRAG_NODE: |
| 3044 | case XML_NOTATION_NODE: |
| 3045 | case XML_HTML_DOCUMENT_NODE: |
| 3046 | case XML_DTD_NODE: |
| 3047 | case XML_ELEMENT_DECL: |
| 3048 | case XML_ATTRIBUTE_DECL: |
| 3049 | case XML_ENTITY_DECL: |
| 3050 | case XML_PI_NODE: |
| 3051 | case XML_ENTITY_REF_NODE: |
| 3052 | case XML_ENTITY_NODE: |
| 3053 | case XML_NAMESPACE_DECL: |
| 3054 | case XML_XINCLUDE_START: |
| 3055 | case XML_XINCLUDE_END: |
| 3056 | #ifdef LIBXML_SGML_ENABLED |
| 3057 | case XML_SGML_DOCUMENT_NODE: |
| 3058 | #endif |
| 3059 | return; |
| 3060 | case XML_ELEMENT_NODE: |
| 3061 | case XML_ATTRIBUTE_NODE: |
| 3062 | break; |
| 3063 | } |
| 3064 | xmlSetProp(cur, BAD_CAST "xml:base", uri); |
| 3065 | } |
| 3066 | |
| 3067 | /** |
| 3068 | * xmlDocumentGetBase: |
| 3069 | * @doc: the document |
| 3070 | * |
| 3071 | * Searches for the Document BASE URL. The code should work on both XML |
| 3072 | * and HTML document. |
| 3073 | * It returns the base as defined in RFC 2396 section |
| 3074 | * 5.1.3. Base URI from the Retrieval URI |
| 3075 | * However it does not return the computed base (5.1.1 and 5.1.2), use |
| 3076 | * xmlNodeGetBase() for this |
| 3077 | * |
| 3078 | * Returns a pointer to the base URL, or NULL if not found |
| 3079 | * It's up to the caller to free the memory. |
| 3080 | */ |
| 3081 | xmlChar * |
| 3082 | xmlDocumentGetBase(xmlDocPtr doc) { |
| 3083 | if (doc == NULL) |
| 3084 | return(NULL); |
| 3085 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 3086 | if (doc->URL != NULL) |
| 3087 | return(xmlStrdup(doc->URL)); |
| 3088 | return(NULL); |
| 3089 | } |
| 3090 | if (doc->URL != NULL) |
| 3091 | return(xmlStrdup(doc->URL)); |
| 3092 | return(NULL); |
| 3093 | } |
| 3094 | |
| 3095 | /** |
| 3096 | * xmlNodeGetBase: |
| 3097 | * @doc: the document the node pertains to |
| 3098 | * @cur: the node being checked |
| 3099 | * |
| 3100 | * Searches for the BASE URL. The code should work on both XML |
| 3101 | * and HTML document even if base mechanisms are completely different. |
| 3102 | * It returns the base as defined in RFC 2396 sections |
| 3103 | * 5.1.1. Base URI within Document Content |
| 3104 | * and |
| 3105 | * 5.1.2. Base URI from the Encapsulating Entity |
| 3106 | * However it does not return the document base (5.1.3), use |
| 3107 | * xmlDocumentGetBase() for this |
| 3108 | * |
| 3109 | * Returns a pointer to the base URL, or NULL if not found |
| 3110 | * It's up to the caller to free the memory. |
| 3111 | */ |
| 3112 | xmlChar * |
| 3113 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
| 3114 | xmlChar *base; |
| 3115 | |
| 3116 | if ((cur == NULL) && (doc == NULL)) |
| 3117 | return(NULL); |
| 3118 | if (doc == NULL) doc = cur->doc; |
| 3119 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 3120 | cur = doc->children; |
| 3121 | while ((cur != NULL) && (cur->name != NULL)) { |
| 3122 | if (cur->type != XML_ELEMENT_NODE) { |
| 3123 | cur = cur->next; |
| 3124 | continue; |
| 3125 | } |
| 3126 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
| 3127 | cur = cur->children; |
| 3128 | continue; |
| 3129 | } |
| 3130 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
| 3131 | cur = cur->children; |
| 3132 | continue; |
| 3133 | } |
| 3134 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 3135 | return(xmlGetProp(cur, BAD_CAST "href")); |
| 3136 | } |
| 3137 | cur = cur->next; |
| 3138 | } |
| 3139 | return(NULL); |
| 3140 | } |
| 3141 | while (cur != NULL) { |
| 3142 | if (cur->type == XML_ENTITY_DECL) { |
| 3143 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 3144 | return(xmlStrdup(ent->URI)); |
| 3145 | } |
| 3146 | base = xmlGetProp(cur, BAD_CAST "xml:base"); |
| 3147 | if (base != NULL) |
| 3148 | return(base); |
| 3149 | cur = cur->parent; |
| 3150 | } |
| 3151 | if ((doc != NULL) && (doc->URL != NULL)) |
| 3152 | return(xmlStrdup(doc->URL)); |
| 3153 | return(NULL); |
| 3154 | } |
| 3155 | |
| 3156 | /** |
| 3157 | * xmlNodeGetContent: |
| 3158 | * @cur: the node being read |
| 3159 | * |
| 3160 | * Read the value of a node, this can be either the text carried |
| 3161 | * directly by this node if it's a TEXT node or the aggregate string |
| 3162 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 3163 | * Entity references are substitued. |
| 3164 | * Returns a new xmlChar * or NULL if no content is available. |
| 3165 | * It's up to the caller to free the memory. |
| 3166 | */ |
| 3167 | xmlChar * |
| 3168 | xmlNodeGetContent(xmlNodePtr cur) { |
| 3169 | if (cur == NULL) return(NULL); |
| 3170 | switch (cur->type) { |
| 3171 | case XML_DOCUMENT_FRAG_NODE: |
| 3172 | case XML_ELEMENT_NODE: { |
| 3173 | xmlNodePtr tmp = cur; |
| 3174 | xmlBufferPtr buffer; |
| 3175 | xmlChar *ret; |
| 3176 | |
| 3177 | buffer = xmlBufferCreate(); |
| 3178 | if (buffer == NULL) |
| 3179 | return(NULL); |
| 3180 | while (tmp != NULL) { |
| 3181 | switch (tmp->type) { |
| 3182 | case XML_ELEMENT_NODE: |
| 3183 | case XML_TEXT_NODE: |
| 3184 | if (tmp->content != NULL) |
| 3185 | #ifndef XML_USE_BUFFER_CONTENT |
| 3186 | xmlBufferCat(buffer, tmp->content); |
| 3187 | #else |
| 3188 | xmlBufferCat(buffer, |
| 3189 | xmlBufferContent(tmp->content)); |
| 3190 | #endif |
| 3191 | break; |
| 3192 | case XML_ENTITY_REF_NODE: { |
| 3193 | xmlEntityPtr ent; |
| 3194 | |
| 3195 | ent = xmlGetDocEntity(cur->doc, tmp->name); |
| 3196 | if (ent != NULL) |
| 3197 | xmlBufferCat(buffer, ent->content); |
| 3198 | } |
| 3199 | default: |
| 3200 | break; |
| 3201 | } |
| 3202 | /* |
| 3203 | * Skip to next node |
| 3204 | */ |
| 3205 | if (tmp->children != NULL) { |
| 3206 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 3207 | tmp = tmp->children; |
| 3208 | continue; |
| 3209 | } |
| 3210 | } |
| 3211 | if (tmp->next != NULL) { |
| 3212 | tmp = tmp->next; |
| 3213 | continue; |
| 3214 | } |
| 3215 | |
| 3216 | do { |
| 3217 | tmp = tmp->parent; |
| 3218 | if (tmp == NULL) |
| 3219 | break; |
| 3220 | if (tmp == (xmlNodePtr) cur) { |
| 3221 | tmp = NULL; |
| 3222 | break; |
| 3223 | } |
| 3224 | if (tmp->next != NULL) { |
| 3225 | tmp = tmp->next; |
| 3226 | break; |
| 3227 | } |
| 3228 | } while (tmp != NULL); |
| 3229 | } |
| 3230 | ret = buffer->content; |
| 3231 | buffer->content = NULL; |
| 3232 | xmlBufferFree(buffer); |
| 3233 | return(ret); |
| 3234 | } |
| 3235 | case XML_ATTRIBUTE_NODE: { |
| 3236 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 3237 | if (attr->parent != NULL) |
| 3238 | return(xmlNodeListGetString(attr->parent->doc, attr->children, 1)); |
| 3239 | else |
| 3240 | return(xmlNodeListGetString(NULL, attr->children, 1)); |
| 3241 | break; |
| 3242 | } |
| 3243 | case XML_COMMENT_NODE: |
| 3244 | case XML_PI_NODE: |
| 3245 | if (cur->content != NULL) |
| 3246 | #ifndef XML_USE_BUFFER_CONTENT |
| 3247 | return(xmlStrdup(cur->content)); |
| 3248 | #else |
| 3249 | return(xmlStrdup(xmlBufferContent(cur->content))); |
| 3250 | #endif |
| 3251 | return(NULL); |
| 3252 | case XML_ENTITY_REF_NODE: |
| 3253 | /* |
| 3254 | * Locate the entity, and get it's content |
| 3255 | * @@@ |
| 3256 | */ |
| 3257 | return(NULL); |
| 3258 | case XML_ENTITY_NODE: |
| 3259 | case XML_DOCUMENT_NODE: |
| 3260 | case XML_HTML_DOCUMENT_NODE: |
| 3261 | case XML_DOCUMENT_TYPE_NODE: |
| 3262 | case XML_NOTATION_NODE: |
| 3263 | case XML_DTD_NODE: |
| 3264 | case XML_XINCLUDE_START: |
| 3265 | case XML_XINCLUDE_END: |
| 3266 | #ifdef LIBXML_SGML_ENABLED |
| 3267 | case XML_SGML_DOCUMENT_NODE: |
| 3268 | #endif |
| 3269 | return(NULL); |
| 3270 | case XML_NAMESPACE_DECL: |
| 3271 | return(xmlStrdup(((xmlNsPtr)cur)->href)); |
| 3272 | case XML_ELEMENT_DECL: |
| 3273 | /* TODO !!! */ |
| 3274 | return(NULL); |
| 3275 | case XML_ATTRIBUTE_DECL: |
| 3276 | /* TODO !!! */ |
| 3277 | return(NULL); |
| 3278 | case XML_ENTITY_DECL: |
| 3279 | /* TODO !!! */ |
| 3280 | return(NULL); |
| 3281 | case XML_CDATA_SECTION_NODE: |
| 3282 | case XML_TEXT_NODE: |
| 3283 | if (cur->content != NULL) |
| 3284 | #ifndef XML_USE_BUFFER_CONTENT |
| 3285 | return(xmlStrdup(cur->content)); |
| 3286 | #else |
| 3287 | return(xmlStrdup(xmlBufferContent(cur->content))); |
| 3288 | #endif |
| 3289 | return(NULL); |
| 3290 | } |
| 3291 | return(NULL); |
| 3292 | } |
| 3293 | |
| 3294 | /** |
| 3295 | * xmlNodeSetContent: |
| 3296 | * @cur: the node being modified |
| 3297 | * @content: the new value of the content |
| 3298 | * |
| 3299 | * Replace the content of a node. |
| 3300 | */ |
| 3301 | void |
| 3302 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
| 3303 | if (cur == NULL) { |
| 3304 | #ifdef DEBUG_TREE |
| 3305 | xmlGenericError(xmlGenericErrorContext, |
| 3306 | "xmlNodeSetContent : node == NULL\n"); |
| 3307 | #endif |
| 3308 | return; |
| 3309 | } |
| 3310 | switch (cur->type) { |
| 3311 | case XML_DOCUMENT_FRAG_NODE: |
| 3312 | case XML_ELEMENT_NODE: |
| 3313 | if (cur->content != NULL) { |
| 3314 | #ifndef XML_USE_BUFFER_CONTENT |
| 3315 | xmlFree(cur->content); |
| 3316 | #else |
| 3317 | xmlBufferFree(cur->content); |
| 3318 | #endif |
| 3319 | cur->content = NULL; |
| 3320 | } |
| 3321 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3322 | cur->children = xmlStringGetNodeList(cur->doc, content); |
| 3323 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 3324 | break; |
| 3325 | case XML_ATTRIBUTE_NODE: |
| 3326 | break; |
| 3327 | case XML_TEXT_NODE: |
| 3328 | case XML_CDATA_SECTION_NODE: |
| 3329 | case XML_ENTITY_REF_NODE: |
| 3330 | case XML_ENTITY_NODE: |
| 3331 | case XML_PI_NODE: |
| 3332 | case XML_COMMENT_NODE: |
| 3333 | if (cur->content != NULL) { |
| 3334 | #ifndef XML_USE_BUFFER_CONTENT |
| 3335 | xmlFree(cur->content); |
| 3336 | #else |
| 3337 | xmlBufferFree(cur->content); |
| 3338 | #endif |
| 3339 | } |
| 3340 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3341 | cur->last = cur->children = NULL; |
| 3342 | if (content != NULL) { |
| 3343 | #ifndef XML_USE_BUFFER_CONTENT |
| 3344 | cur->content = xmlStrdup(content); |
| 3345 | #else |
| 3346 | cur->content = xmlBufferCreateSize(0); |
| 3347 | xmlBufferSetAllocationScheme(cur->content, |
| 3348 | xmlGetBufferAllocationScheme()); |
| 3349 | xmlBufferAdd(cur->content, content, -1); |
| 3350 | #endif |
| 3351 | } else |
| 3352 | cur->content = NULL; |
| 3353 | break; |
| 3354 | case XML_DOCUMENT_NODE: |
| 3355 | case XML_HTML_DOCUMENT_NODE: |
| 3356 | case XML_DOCUMENT_TYPE_NODE: |
| 3357 | case XML_XINCLUDE_START: |
| 3358 | case XML_XINCLUDE_END: |
| 3359 | #ifdef LIBXML_SGML_ENABLED |
| 3360 | case XML_SGML_DOCUMENT_NODE: |
| 3361 | #endif |
| 3362 | break; |
| 3363 | case XML_NOTATION_NODE: |
| 3364 | break; |
| 3365 | case XML_DTD_NODE: |
| 3366 | break; |
| 3367 | case XML_NAMESPACE_DECL: |
| 3368 | break; |
| 3369 | case XML_ELEMENT_DECL: |
| 3370 | /* TODO !!! */ |
| 3371 | break; |
| 3372 | case XML_ATTRIBUTE_DECL: |
| 3373 | /* TODO !!! */ |
| 3374 | break; |
| 3375 | case XML_ENTITY_DECL: |
| 3376 | /* TODO !!! */ |
| 3377 | break; |
| 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | /** |
| 3382 | * xmlNodeSetContentLen: |
| 3383 | * @cur: the node being modified |
| 3384 | * @content: the new value of the content |
| 3385 | * @len: the size of @content |
| 3386 | * |
| 3387 | * Replace the content of a node. |
| 3388 | */ |
| 3389 | void |
| 3390 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 3391 | if (cur == NULL) { |
| 3392 | #ifdef DEBUG_TREE |
| 3393 | xmlGenericError(xmlGenericErrorContext, |
| 3394 | "xmlNodeSetContentLen : node == NULL\n"); |
| 3395 | #endif |
| 3396 | return; |
| 3397 | } |
| 3398 | switch (cur->type) { |
| 3399 | case XML_DOCUMENT_FRAG_NODE: |
| 3400 | case XML_ELEMENT_NODE: |
| 3401 | if (cur->content != NULL) { |
| 3402 | #ifndef XML_USE_BUFFER_CONTENT |
| 3403 | xmlFree(cur->content); |
| 3404 | #else |
| 3405 | xmlBufferFree(cur->content); |
| 3406 | #endif |
| 3407 | cur->content = NULL; |
| 3408 | } |
| 3409 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3410 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
| 3411 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 3412 | break; |
| 3413 | case XML_ATTRIBUTE_NODE: |
| 3414 | break; |
| 3415 | case XML_TEXT_NODE: |
| 3416 | case XML_CDATA_SECTION_NODE: |
| 3417 | case XML_ENTITY_REF_NODE: |
| 3418 | case XML_ENTITY_NODE: |
| 3419 | case XML_PI_NODE: |
| 3420 | case XML_COMMENT_NODE: |
| 3421 | case XML_NOTATION_NODE: |
| 3422 | if (cur->content != NULL) { |
| 3423 | #ifndef XML_USE_BUFFER_CONTENT |
| 3424 | xmlFree(cur->content); |
| 3425 | #else |
| 3426 | xmlBufferFree(cur->content); |
| 3427 | #endif |
| 3428 | } |
| 3429 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3430 | cur->children = cur->last = NULL; |
| 3431 | if (content != NULL) { |
| 3432 | #ifndef XML_USE_BUFFER_CONTENT |
| 3433 | cur->content = xmlStrndup(content, len); |
| 3434 | #else |
| 3435 | cur->content = xmlBufferCreateSize(len); |
| 3436 | xmlBufferSetAllocationScheme(cur->content, |
| 3437 | xmlGetBufferAllocationScheme()); |
| 3438 | xmlBufferAdd(cur->content, content, len); |
| 3439 | #endif |
| 3440 | } else |
| 3441 | cur->content = NULL; |
| 3442 | break; |
| 3443 | case XML_DOCUMENT_NODE: |
| 3444 | case XML_DTD_NODE: |
| 3445 | case XML_HTML_DOCUMENT_NODE: |
| 3446 | case XML_DOCUMENT_TYPE_NODE: |
| 3447 | case XML_NAMESPACE_DECL: |
| 3448 | case XML_XINCLUDE_START: |
| 3449 | case XML_XINCLUDE_END: |
| 3450 | #ifdef LIBXML_SGML_ENABLED |
| 3451 | case XML_SGML_DOCUMENT_NODE: |
| 3452 | #endif |
| 3453 | break; |
| 3454 | case XML_ELEMENT_DECL: |
| 3455 | /* TODO !!! */ |
| 3456 | break; |
| 3457 | case XML_ATTRIBUTE_DECL: |
| 3458 | /* TODO !!! */ |
| 3459 | break; |
| 3460 | case XML_ENTITY_DECL: |
| 3461 | /* TODO !!! */ |
| 3462 | break; |
| 3463 | } |
| 3464 | } |
| 3465 | |
| 3466 | /** |
| 3467 | * xmlNodeAddContentLen: |
| 3468 | * @cur: the node being modified |
| 3469 | * @content: extra content |
| 3470 | * @len: the size of @content |
| 3471 | * |
| 3472 | * Append the extra substring to the node content. |
| 3473 | */ |
| 3474 | void |
| 3475 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 3476 | if (cur == NULL) { |
| 3477 | #ifdef DEBUG_TREE |
| 3478 | xmlGenericError(xmlGenericErrorContext, |
| 3479 | "xmlNodeAddContentLen : node == NULL\n"); |
| 3480 | #endif |
| 3481 | return; |
| 3482 | } |
| 3483 | if (len <= 0) return; |
| 3484 | switch (cur->type) { |
| 3485 | case XML_DOCUMENT_FRAG_NODE: |
| 3486 | case XML_ELEMENT_NODE: { |
| 3487 | xmlNodePtr last = NULL, newNode; |
| 3488 | |
| 3489 | if (cur->children != NULL) { |
| 3490 | last = cur->last; |
| 3491 | } else { |
| 3492 | if (cur->content != NULL) { |
| 3493 | #ifndef XML_USE_BUFFER_CONTENT |
| 3494 | cur->children = xmlStringGetNodeList(cur->doc, cur->content); |
| 3495 | #else |
| 3496 | cur->children = xmlStringGetNodeList(cur->doc, |
| 3497 | xmlBufferContent(cur->content)); |
| 3498 | #endif |
| 3499 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 3500 | #ifndef XML_USE_BUFFER_CONTENT |
| 3501 | xmlFree(cur->content); |
| 3502 | #else |
| 3503 | xmlBufferFree(cur->content); |
| 3504 | #endif |
| 3505 | cur->content = NULL; |
| 3506 | last = cur->last; |
| 3507 | } |
| 3508 | } |
| 3509 | newNode = xmlNewTextLen(content, len); |
| 3510 | if (newNode != NULL) { |
| 3511 | xmlAddChild(cur, newNode); |
| 3512 | if ((last != NULL) && (last->next == newNode)) { |
| 3513 | xmlTextMerge(last, newNode); |
| 3514 | } |
| 3515 | } |
| 3516 | break; |
| 3517 | } |
| 3518 | case XML_ATTRIBUTE_NODE: |
| 3519 | break; |
| 3520 | case XML_TEXT_NODE: |
| 3521 | case XML_CDATA_SECTION_NODE: |
| 3522 | case XML_ENTITY_REF_NODE: |
| 3523 | case XML_ENTITY_NODE: |
| 3524 | case XML_PI_NODE: |
| 3525 | case XML_COMMENT_NODE: |
| 3526 | case XML_NOTATION_NODE: |
| 3527 | if (content != NULL) { |
| 3528 | #ifndef XML_USE_BUFFER_CONTENT |
| 3529 | cur->content = xmlStrncat(cur->content, content, len); |
| 3530 | #else |
| 3531 | xmlBufferAdd(cur->content, content, len); |
| 3532 | #endif |
| 3533 | } |
| 3534 | case XML_DOCUMENT_NODE: |
| 3535 | case XML_DTD_NODE: |
| 3536 | case XML_HTML_DOCUMENT_NODE: |
| 3537 | case XML_DOCUMENT_TYPE_NODE: |
| 3538 | case XML_NAMESPACE_DECL: |
| 3539 | case XML_XINCLUDE_START: |
| 3540 | case XML_XINCLUDE_END: |
| 3541 | #ifdef LIBXML_SGML_ENABLED |
| 3542 | case XML_SGML_DOCUMENT_NODE: |
| 3543 | #endif |
| 3544 | break; |
| 3545 | case XML_ELEMENT_DECL: |
| 3546 | case XML_ATTRIBUTE_DECL: |
| 3547 | case XML_ENTITY_DECL: |
| 3548 | break; |
| 3549 | } |
| 3550 | } |
| 3551 | |
| 3552 | /** |
| 3553 | * xmlNodeAddContent: |
| 3554 | * @cur: the node being modified |
| 3555 | * @content: extra content |
| 3556 | * |
| 3557 | * Append the extra substring to the node content. |
| 3558 | */ |
| 3559 | void |
| 3560 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
| 3561 | int len; |
| 3562 | |
| 3563 | if (cur == NULL) { |
| 3564 | #ifdef DEBUG_TREE |
| 3565 | xmlGenericError(xmlGenericErrorContext, |
| 3566 | "xmlNodeAddContent : node == NULL\n"); |
| 3567 | #endif |
| 3568 | return; |
| 3569 | } |
| 3570 | if (content == NULL) return; |
| 3571 | len = xmlStrlen(content); |
| 3572 | xmlNodeAddContentLen(cur, content, len); |
| 3573 | } |
| 3574 | |
| 3575 | /** |
| 3576 | * xmlTextMerge: |
| 3577 | * @first: the first text node |
| 3578 | * @second: the second text node being merged |
| 3579 | * |
| 3580 | * Merge two text nodes into one |
| 3581 | * Returns the first text node augmented |
| 3582 | */ |
| 3583 | xmlNodePtr |
| 3584 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 3585 | if (first == NULL) return(second); |
| 3586 | if (second == NULL) return(first); |
| 3587 | if (first->type != XML_TEXT_NODE) return(first); |
| 3588 | if (second->type != XML_TEXT_NODE) return(first); |
| 3589 | if (second->name != first->name) |
| 3590 | return(first); |
| 3591 | #ifndef XML_USE_BUFFER_CONTENT |
| 3592 | xmlNodeAddContent(first, second->content); |
| 3593 | #else |
| 3594 | xmlNodeAddContent(first, xmlBufferContent(second->content)); |
| 3595 | #endif |
| 3596 | xmlUnlinkNode(second); |
| 3597 | xmlFreeNode(second); |
| 3598 | return(first); |
| 3599 | } |
| 3600 | |
| 3601 | /** |
| 3602 | * xmlGetNsList: |
| 3603 | * @doc: the document |
| 3604 | * @node: the current node |
| 3605 | * |
| 3606 | * Search all the namespace applying to a given element. |
| 3607 | * Returns an NULL terminated array of all the xmlNsPtr found |
| 3608 | * that need to be freed by the caller or NULL if no |
| 3609 | * namespace if defined |
| 3610 | */ |
| 3611 | xmlNsPtr * |
| 3612 | xmlGetNsList(xmlDocPtr doc, xmlNodePtr node) { |
| 3613 | xmlNsPtr cur; |
| 3614 | xmlNsPtr *ret = NULL; |
| 3615 | int nbns = 0; |
| 3616 | int maxns = 10; |
| 3617 | int i; |
| 3618 | |
| 3619 | while (node != NULL) { |
| 3620 | cur = node->nsDef; |
| 3621 | while (cur != NULL) { |
| 3622 | if (ret == NULL) { |
| 3623 | ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr)); |
| 3624 | if (ret == NULL) { |
| 3625 | xmlGenericError(xmlGenericErrorContext, |
| 3626 | "xmlGetNsList : out of memory!\n"); |
| 3627 | return(NULL); |
| 3628 | } |
| 3629 | ret[nbns] = NULL; |
| 3630 | } |
| 3631 | for (i = 0;i < nbns;i++) { |
| 3632 | if ((cur->prefix == ret[i]->prefix) || |
| 3633 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) break; |
| 3634 | } |
| 3635 | if (i >= nbns) { |
| 3636 | if (nbns >= maxns) { |
| 3637 | maxns *= 2; |
| 3638 | ret = (xmlNsPtr *) xmlRealloc(ret, |
| 3639 | (maxns + 1) * sizeof(xmlNsPtr)); |
| 3640 | if (ret == NULL) { |
| 3641 | xmlGenericError(xmlGenericErrorContext, |
| 3642 | "xmlGetNsList : realloc failed!\n"); |
| 3643 | return(NULL); |
| 3644 | } |
| 3645 | } |
| 3646 | ret[nbns++] = cur; |
| 3647 | ret[nbns] = NULL; |
| 3648 | } |
| 3649 | |
| 3650 | cur = cur->next; |
| 3651 | } |
| 3652 | node = node->parent; |
| 3653 | } |
| 3654 | return(ret); |
| 3655 | } |
| 3656 | |
| 3657 | /** |
| 3658 | * xmlSearchNs: |
| 3659 | * @doc: the document |
| 3660 | * @node: the current node |
| 3661 | * @nameSpace: the namespace string |
| 3662 | * |
| 3663 | * Search a Ns registered under a given name space for a document. |
| 3664 | * recurse on the parents until it finds the defined namespace |
| 3665 | * or return NULL otherwise. |
| 3666 | * @nameSpace can be NULL, this is a search for the default namespace. |
| 3667 | * We don't allow to cross entities boundaries. If you don't declare |
| 3668 | * the namespace within those you will be in troubles !!! A warning |
| 3669 | * is generated to cover this case. |
| 3670 | * |
| 3671 | * Returns the namespace pointer or NULL. |
| 3672 | */ |
| 3673 | xmlNsPtr |
| 3674 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
| 3675 | xmlNsPtr cur; |
| 3676 | |
| 3677 | if (node == NULL) return(NULL); |
| 3678 | if ((nameSpace != NULL) && |
| 3679 | (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { |
| 3680 | if (doc->oldNs == NULL) { |
| 3681 | /* |
| 3682 | * Allocate a new Namespace and fill the fields. |
| 3683 | */ |
| 3684 | doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 3685 | if (doc->oldNs == NULL) { |
| 3686 | xmlGenericError(xmlGenericErrorContext, |
| 3687 | "xmlSearchNsByHref : malloc failed\n"); |
| 3688 | return(NULL); |
| 3689 | } |
| 3690 | memset(doc->oldNs, 0, sizeof(xmlNs)); |
| 3691 | doc->oldNs->type = XML_LOCAL_NAMESPACE; |
| 3692 | |
| 3693 | doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE); |
| 3694 | doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 3695 | } |
| 3696 | return(doc->oldNs); |
| 3697 | } |
| 3698 | while (node != NULL) { |
| 3699 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 3700 | (node->type == XML_ENTITY_NODE) || |
| 3701 | (node->type == XML_ENTITY_DECL)) |
| 3702 | return(NULL); |
| 3703 | if (node->type == XML_ELEMENT_NODE) { |
| 3704 | cur = node->nsDef; |
| 3705 | while (cur != NULL) { |
| 3706 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 3707 | (cur->href != NULL)) |
| 3708 | return(cur); |
| 3709 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 3710 | (cur->href != NULL) && |
| 3711 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 3712 | return(cur); |
| 3713 | cur = cur->next; |
| 3714 | } |
| 3715 | } |
| 3716 | node = node->parent; |
| 3717 | } |
| 3718 | return(NULL); |
| 3719 | } |
| 3720 | |
| 3721 | /** |
| 3722 | * xmlSearchNsByHref: |
| 3723 | * @doc: the document |
| 3724 | * @node: the current node |
| 3725 | * @href: the namespace value |
| 3726 | * |
| 3727 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 3728 | * the defined namespace or return NULL otherwise. |
| 3729 | * Returns the namespace pointer or NULL. |
| 3730 | */ |
| 3731 | xmlNsPtr |
| 3732 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) { |
| 3733 | xmlNsPtr cur; |
| 3734 | xmlNodePtr orig = node; |
| 3735 | |
| 3736 | if ((node == NULL) || (href == NULL)) return(NULL); |
| 3737 | if (xmlStrEqual(href, XML_XML_NAMESPACE)) { |
| 3738 | if (doc->oldNs == NULL) { |
| 3739 | /* |
| 3740 | * Allocate a new Namespace and fill the fields. |
| 3741 | */ |
| 3742 | doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 3743 | if (doc->oldNs == NULL) { |
| 3744 | xmlGenericError(xmlGenericErrorContext, |
| 3745 | "xmlSearchNsByHref : malloc failed\n"); |
| 3746 | return(NULL); |
| 3747 | } |
| 3748 | memset(doc->oldNs, 0, sizeof(xmlNs)); |
| 3749 | doc->oldNs->type = XML_LOCAL_NAMESPACE; |
| 3750 | |
| 3751 | doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE); |
| 3752 | doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 3753 | } |
| 3754 | return(doc->oldNs); |
| 3755 | } |
| 3756 | while (node != NULL) { |
| 3757 | cur = node->nsDef; |
| 3758 | while (cur != NULL) { |
| 3759 | if ((cur->href != NULL) && (href != NULL) && |
| 3760 | (xmlStrEqual(cur->href, href))) { |
| 3761 | /* |
| 3762 | * Check that the prefix is not shadowed between orig and node |
| 3763 | */ |
| 3764 | xmlNodePtr check = orig; |
| 3765 | xmlNsPtr tst; |
| 3766 | |
| 3767 | while (check != node) { |
| 3768 | tst = check->nsDef; |
| 3769 | while (tst != NULL) { |
| 3770 | if ((tst->prefix == NULL) && (cur->prefix == NULL)) |
| 3771 | goto shadowed; |
| 3772 | if ((tst->prefix != NULL) && (cur->prefix != NULL) && |
| 3773 | (xmlStrEqual(tst->prefix, cur->prefix))) |
| 3774 | goto shadowed; |
| 3775 | tst = tst->next; |
| 3776 | } |
| 3777 | check = check->parent; |
| 3778 | } |
| 3779 | return(cur); |
| 3780 | } |
| 3781 | shadowed: |
| 3782 | cur = cur->next; |
| 3783 | } |
| 3784 | node = node->parent; |
| 3785 | } |
| 3786 | return(NULL); |
| 3787 | } |
| 3788 | |
| 3789 | /** |
| 3790 | * xmlNewReconciliedNs |
| 3791 | * @doc: the document |
| 3792 | * @tree: a node expected to hold the new namespace |
| 3793 | * @ns: the original namespace |
| 3794 | * |
| 3795 | * This function tries to locate a namespace definition in a tree |
| 3796 | * ancestors, or create a new namespace definition node similar to |
| 3797 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 3798 | * null (default namespace) or reused within the subtree defined by |
| 3799 | * @tree or on one of its ancestors then a new prefix is generated. |
| 3800 | * Returns the (new) namespace definition or NULL in case of error |
| 3801 | */ |
| 3802 | xmlNsPtr |
| 3803 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 3804 | xmlNsPtr def; |
| 3805 | xmlChar prefix[50]; |
| 3806 | int counter = 1; |
| 3807 | |
| 3808 | if (tree == NULL) { |
| 3809 | #ifdef DEBUG_TREE |
| 3810 | xmlGenericError(xmlGenericErrorContext, |
| 3811 | "xmlNewReconciliedNs : tree == NULL\n"); |
| 3812 | #endif |
| 3813 | return(NULL); |
| 3814 | } |
| 3815 | if (ns == NULL) { |
| 3816 | #ifdef DEBUG_TREE |
| 3817 | xmlGenericError(xmlGenericErrorContext, |
| 3818 | "xmlNewReconciliedNs : ns == NULL\n"); |
| 3819 | #endif |
| 3820 | return(NULL); |
| 3821 | } |
| 3822 | /* |
| 3823 | * Search an existing namespace definition inherited. |
| 3824 | */ |
| 3825 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 3826 | if (def != NULL) |
| 3827 | return(def); |
| 3828 | |
| 3829 | /* |
| 3830 | * Find a close prefix which is not already in use. |
| 3831 | * Let's strip namespace prefixes longer than 20 chars ! |
| 3832 | */ |
| 3833 | sprintf((char *) prefix, "%.20s", ns->prefix); |
| 3834 | def = xmlSearchNs(doc, tree, prefix); |
| 3835 | while (def != NULL) { |
| 3836 | if (counter > 1000) return(NULL); |
| 3837 | sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++); |
| 3838 | def = xmlSearchNs(doc, tree, prefix); |
| 3839 | } |
| 3840 | |
| 3841 | /* |
| 3842 | * Ok, now we are ready to create a new one. |
| 3843 | */ |
| 3844 | def = xmlNewNs(tree, ns->href, prefix); |
| 3845 | return(def); |
| 3846 | } |
| 3847 | |
| 3848 | /** |
| 3849 | * xmlReconciliateNs |
| 3850 | * @doc: the document |
| 3851 | * @tree: a node defining the subtree to reconciliate |
| 3852 | * |
| 3853 | * This function checks that all the namespaces declared within the given |
| 3854 | * tree are properly declared. This is needed for example after Copy or Cut |
| 3855 | * and then paste operations. The subtree may still hold pointers to |
| 3856 | * namespace declarations outside the subtree or invalid/masked. As much |
| 3857 | * as possible the function try tu reuse the existing namespaces found in |
| 3858 | * the new environment. If not possible the new namespaces are redeclared |
| 3859 | * on @tree at the top of the given subtree. |
| 3860 | * Returns the number of namespace declarations created or -1 in case of error. |
| 3861 | */ |
| 3862 | int |
| 3863 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 3864 | xmlNsPtr *oldNs = NULL; |
| 3865 | xmlNsPtr *newNs = NULL; |
| 3866 | int sizeCache = 0; |
| 3867 | int nbCache = 0; |
| 3868 | |
| 3869 | xmlNsPtr n; |
| 3870 | xmlNodePtr node = tree; |
| 3871 | xmlAttrPtr attr; |
| 3872 | int ret = 0, i; |
| 3873 | |
| 3874 | while (node != NULL) { |
| 3875 | /* |
| 3876 | * Reconciliate the node namespace |
| 3877 | */ |
| 3878 | if (node->ns != NULL) { |
| 3879 | /* |
| 3880 | * initialize the cache if needed |
| 3881 | */ |
| 3882 | if (sizeCache == 0) { |
| 3883 | sizeCache = 10; |
| 3884 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3885 | sizeof(xmlNsPtr)); |
| 3886 | if (oldNs == NULL) { |
| 3887 | xmlGenericError(xmlGenericErrorContext, |
| 3888 | "xmlReconciliateNs : memory pbm\n"); |
| 3889 | return(-1); |
| 3890 | } |
| 3891 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3892 | sizeof(xmlNsPtr)); |
| 3893 | if (newNs == NULL) { |
| 3894 | xmlGenericError(xmlGenericErrorContext, |
| 3895 | "xmlReconciliateNs : memory pbm\n"); |
| 3896 | xmlFree(oldNs); |
| 3897 | return(-1); |
| 3898 | } |
| 3899 | } |
| 3900 | for (i = 0;i < nbCache;i++) { |
| 3901 | if (oldNs[i] == node->ns) { |
| 3902 | node->ns = newNs[i]; |
| 3903 | break; |
| 3904 | } |
| 3905 | } |
| 3906 | if (i == nbCache) { |
| 3907 | /* |
| 3908 | * Ok we need to recreate a new namespace definition |
| 3909 | */ |
| 3910 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 3911 | if (n != NULL) { /* :-( what if else ??? */ |
| 3912 | /* |
| 3913 | * check if we need to grow the cache buffers. |
| 3914 | */ |
| 3915 | if (sizeCache <= nbCache) { |
| 3916 | sizeCache *= 2; |
| 3917 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 3918 | sizeof(xmlNsPtr)); |
| 3919 | if (oldNs == NULL) { |
| 3920 | xmlGenericError(xmlGenericErrorContext, |
| 3921 | "xmlReconciliateNs : memory pbm\n"); |
| 3922 | xmlFree(newNs); |
| 3923 | return(-1); |
| 3924 | } |
| 3925 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 3926 | sizeof(xmlNsPtr)); |
| 3927 | if (newNs == NULL) { |
| 3928 | xmlGenericError(xmlGenericErrorContext, |
| 3929 | "xmlReconciliateNs : memory pbm\n"); |
| 3930 | xmlFree(oldNs); |
| 3931 | return(-1); |
| 3932 | } |
| 3933 | } |
| 3934 | newNs[nbCache] = n; |
| 3935 | oldNs[nbCache++] = node->ns; |
| 3936 | node->ns = n; |
| 3937 | } |
| 3938 | } |
| 3939 | } |
| 3940 | /* |
| 3941 | * now check for namespace hold by attributes on the node. |
| 3942 | */ |
| 3943 | attr = node->properties; |
| 3944 | while (attr != NULL) { |
| 3945 | if (attr->ns != NULL) { |
| 3946 | /* |
| 3947 | * initialize the cache if needed |
| 3948 | */ |
| 3949 | if (sizeCache == 0) { |
| 3950 | sizeCache = 10; |
| 3951 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3952 | sizeof(xmlNsPtr)); |
| 3953 | if (oldNs == NULL) { |
| 3954 | xmlGenericError(xmlGenericErrorContext, |
| 3955 | "xmlReconciliateNs : memory pbm\n"); |
| 3956 | return(-1); |
| 3957 | } |
| 3958 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3959 | sizeof(xmlNsPtr)); |
| 3960 | if (newNs == NULL) { |
| 3961 | xmlGenericError(xmlGenericErrorContext, |
| 3962 | "xmlReconciliateNs : memory pbm\n"); |
| 3963 | xmlFree(oldNs); |
| 3964 | return(-1); |
| 3965 | } |
| 3966 | } |
| 3967 | for (i = 0;i < nbCache;i++) { |
| 3968 | if (oldNs[i] == attr->ns) { |
| 3969 | node->ns = newNs[i]; |
| 3970 | break; |
| 3971 | } |
| 3972 | } |
| 3973 | if (i == nbCache) { |
| 3974 | /* |
| 3975 | * Ok we need to recreate a new namespace definition |
| 3976 | */ |
| 3977 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 3978 | if (n != NULL) { /* :-( what if else ??? */ |
| 3979 | /* |
| 3980 | * check if we need to grow the cache buffers. |
| 3981 | */ |
| 3982 | if (sizeCache <= nbCache) { |
| 3983 | sizeCache *= 2; |
| 3984 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 3985 | sizeof(xmlNsPtr)); |
| 3986 | if (oldNs == NULL) { |
| 3987 | xmlGenericError(xmlGenericErrorContext, |
| 3988 | "xmlReconciliateNs : memory pbm\n"); |
| 3989 | xmlFree(newNs); |
| 3990 | return(-1); |
| 3991 | } |
| 3992 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 3993 | sizeof(xmlNsPtr)); |
| 3994 | if (newNs == NULL) { |
| 3995 | xmlGenericError(xmlGenericErrorContext, |
| 3996 | "xmlReconciliateNs : memory pbm\n"); |
| 3997 | xmlFree(oldNs); |
| 3998 | return(-1); |
| 3999 | } |
| 4000 | } |
| 4001 | newNs[nbCache] = n; |
| 4002 | oldNs[nbCache++] = attr->ns; |
| 4003 | attr->ns = n; |
| 4004 | } |
| 4005 | } |
| 4006 | } |
| 4007 | attr = attr->next; |
| 4008 | } |
| 4009 | |
| 4010 | /* |
| 4011 | * Browse the full subtree, deep first |
| 4012 | */ |
| 4013 | if (node->children != NULL) { |
| 4014 | /* deep first */ |
| 4015 | node = node->children; |
| 4016 | } else if ((node != tree) && (node->next != NULL)) { |
| 4017 | /* then siblings */ |
| 4018 | node = node->next; |
| 4019 | } else if (node != tree) { |
| 4020 | /* go up to parents->next if needed */ |
| 4021 | while (node != tree) { |
| 4022 | if (node->parent != NULL) |
| 4023 | node = node->parent; |
| 4024 | if ((node != tree) && (node->next != NULL)) { |
| 4025 | node = node->next; |
| 4026 | break; |
| 4027 | } |
| 4028 | if (node->parent == NULL) { |
| 4029 | node = NULL; |
| 4030 | break; |
| 4031 | } |
| 4032 | } |
| 4033 | /* exit condition */ |
| 4034 | if (node == tree) |
| 4035 | node = NULL; |
| 4036 | } |
| 4037 | } |
| 4038 | return(ret); |
| 4039 | } |
| 4040 | |
| 4041 | /** |
| 4042 | * xmlHasProp: |
| 4043 | * @node: the node |
| 4044 | * @name: the attribute name |
| 4045 | * |
| 4046 | * Search an attribute associated to a node |
| 4047 | * This function also looks in DTD attribute declaration for #FIXED or |
| 4048 | * default declaration values unless DTD use has been turned off. |
| 4049 | * |
| 4050 | * Returns the attribute or the attribute declaration or NULL if |
| 4051 | * neither was found. |
| 4052 | */ |
| 4053 | xmlAttrPtr |
| 4054 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 4055 | xmlAttrPtr prop; |
| 4056 | xmlDocPtr doc; |
| 4057 | |
| 4058 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 4059 | /* |
| 4060 | * Check on the properties attached to the node |
| 4061 | */ |
| 4062 | prop = node->properties; |
| 4063 | while (prop != NULL) { |
| 4064 | if (xmlStrEqual(prop->name, name)) { |
| 4065 | return(prop); |
| 4066 | } |
| 4067 | prop = prop->next; |
| 4068 | } |
| 4069 | if (!xmlCheckDTD) return(NULL); |
| 4070 | |
| 4071 | /* |
| 4072 | * Check if there is a default declaration in the internal |
| 4073 | * or external subsets |
| 4074 | */ |
| 4075 | doc = node->doc; |
| 4076 | if (doc != NULL) { |
| 4077 | xmlAttributePtr attrDecl; |
| 4078 | if (doc->intSubset != NULL) { |
| 4079 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 4080 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4081 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 4082 | if (attrDecl != NULL) |
| 4083 | return((xmlAttrPtr) attrDecl); |
| 4084 | } |
| 4085 | } |
| 4086 | return(NULL); |
| 4087 | } |
| 4088 | |
| 4089 | /** |
| 4090 | * xmlGetProp: |
| 4091 | * @node: the node |
| 4092 | * @name: the attribute name |
| 4093 | * |
| 4094 | * Search and get the value of an attribute associated to a node |
| 4095 | * This does the entity substitution. |
| 4096 | * This function looks in DTD attribute declaration for #FIXED or |
| 4097 | * default declaration values unless DTD use has been turned off. |
| 4098 | * |
| 4099 | * Returns the attribute value or NULL if not found. |
| 4100 | * It's up to the caller to free the memory. |
| 4101 | */ |
| 4102 | xmlChar * |
| 4103 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
| 4104 | xmlAttrPtr prop; |
| 4105 | xmlDocPtr doc; |
| 4106 | |
| 4107 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 4108 | /* |
| 4109 | * Check on the properties attached to the node |
| 4110 | */ |
| 4111 | prop = node->properties; |
| 4112 | while (prop != NULL) { |
| 4113 | if (xmlStrEqual(prop->name, name)) { |
| 4114 | xmlChar *ret; |
| 4115 | |
| 4116 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 4117 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 4118 | return(ret); |
| 4119 | } |
| 4120 | prop = prop->next; |
| 4121 | } |
| 4122 | if (!xmlCheckDTD) return(NULL); |
| 4123 | |
| 4124 | /* |
| 4125 | * Check if there is a default declaration in the internal |
| 4126 | * or external subsets |
| 4127 | */ |
| 4128 | doc = node->doc; |
| 4129 | if (doc != NULL) { |
| 4130 | xmlAttributePtr attrDecl; |
| 4131 | if (doc->intSubset != NULL) { |
| 4132 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 4133 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4134 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 4135 | if (attrDecl != NULL) |
| 4136 | return(xmlStrdup(attrDecl->defaultValue)); |
| 4137 | } |
| 4138 | } |
| 4139 | return(NULL); |
| 4140 | } |
| 4141 | |
| 4142 | /** |
| 4143 | * xmlGetNsProp: |
| 4144 | * @node: the node |
| 4145 | * @name: the attribute name |
| 4146 | * @namespace: the URI of the namespace |
| 4147 | * |
| 4148 | * Search and get the value of an attribute associated to a node |
| 4149 | * This attribute has to be anchored in the namespace specified. |
| 4150 | * This does the entity substitution. |
| 4151 | * This function looks in DTD attribute declaration for #FIXED or |
| 4152 | * default declaration values unless DTD use has been turned off. |
| 4153 | * |
| 4154 | * Returns the attribute value or NULL if not found. |
| 4155 | * It's up to the caller to free the memory. |
| 4156 | */ |
| 4157 | xmlChar * |
| 4158 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) { |
| 4159 | xmlAttrPtr prop; |
| 4160 | xmlDocPtr doc; |
| 4161 | xmlNsPtr ns; |
| 4162 | |
| 4163 | if (node == NULL) |
| 4164 | return(NULL); |
| 4165 | |
| 4166 | prop = node->properties; |
| 4167 | if (namespace == NULL) |
| 4168 | return(xmlGetProp(node, name)); |
| 4169 | while (prop != NULL) { |
| 4170 | /* |
| 4171 | * One need to have |
| 4172 | * - same attribute names |
| 4173 | * - and the attribute carrying that namespace |
| 4174 | * or |
| 4175 | * no namespace on the attribute and the element carrying it |
| 4176 | */ |
| 4177 | if ((xmlStrEqual(prop->name, name)) && |
| 4178 | (((prop->ns == NULL) && (node->ns != NULL) && |
| 4179 | (xmlStrEqual(node->ns->href, namespace))) || |
| 4180 | ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, namespace))))) { |
| 4181 | xmlChar *ret; |
| 4182 | |
| 4183 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 4184 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 4185 | return(ret); |
| 4186 | } |
| 4187 | prop = prop->next; |
| 4188 | } |
| 4189 | if (!xmlCheckDTD) return(NULL); |
| 4190 | |
| 4191 | /* |
| 4192 | * Check if there is a default declaration in the internal |
| 4193 | * or external subsets |
| 4194 | */ |
| 4195 | doc = node->doc; |
| 4196 | if (doc != NULL) { |
| 4197 | xmlAttributePtr attrDecl; |
| 4198 | if (doc->intSubset != NULL) { |
| 4199 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 4200 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4201 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 4202 | |
| 4203 | if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) { |
| 4204 | /* |
| 4205 | * The DTD declaration only allows a prefix search |
| 4206 | */ |
| 4207 | ns = xmlSearchNs(doc, node, attrDecl->prefix); |
| 4208 | if ((ns != NULL) && (xmlStrEqual(ns->href, namespace))) |
| 4209 | return(xmlStrdup(attrDecl->defaultValue)); |
| 4210 | } |
| 4211 | } |
| 4212 | } |
| 4213 | return(NULL); |
| 4214 | } |
| 4215 | |
| 4216 | /** |
| 4217 | * xmlSetProp: |
| 4218 | * @node: the node |
| 4219 | * @name: the attribute name |
| 4220 | * @value: the attribute value |
| 4221 | * |
| 4222 | * Set (or reset) an attribute carried by a node. |
| 4223 | * Returns the attribute pointer. |
| 4224 | */ |
| 4225 | xmlAttrPtr |
| 4226 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
| 4227 | xmlAttrPtr prop = node->properties; |
| 4228 | xmlDocPtr doc = NULL; |
| 4229 | |
| 4230 | if ((node == NULL) || (name == NULL)) |
| 4231 | return(NULL); |
| 4232 | doc = node->doc; |
| 4233 | while (prop != NULL) { |
| 4234 | if (xmlStrEqual(prop->name, name)) { |
| 4235 | if (prop->children != NULL) |
| 4236 | xmlFreeNodeList(prop->children); |
| 4237 | prop->children = NULL; |
| 4238 | prop->last = NULL; |
| 4239 | if (value != NULL) { |
| 4240 | xmlChar *buffer; |
| 4241 | xmlNodePtr tmp; |
| 4242 | |
| 4243 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 4244 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 4245 | prop->last = NULL; |
| 4246 | prop->doc = doc; |
| 4247 | tmp = prop->children; |
| 4248 | while (tmp != NULL) { |
| 4249 | tmp->parent = (xmlNodePtr) prop; |
| 4250 | tmp->doc = doc; |
| 4251 | if (tmp->next == NULL) |
| 4252 | prop->last = tmp; |
| 4253 | tmp = tmp->next; |
| 4254 | } |
| 4255 | xmlFree(buffer); |
| 4256 | } |
| 4257 | return(prop); |
| 4258 | } |
| 4259 | prop = prop->next; |
| 4260 | } |
| 4261 | prop = xmlNewProp(node, name, value); |
| 4262 | return(prop); |
| 4263 | } |
| 4264 | |
| 4265 | /** |
| 4266 | * xmlSetNsProp: |
| 4267 | * @node: the node |
| 4268 | * @ns: the namespace definition |
| 4269 | * @name: the attribute name |
| 4270 | * @value: the attribute value |
| 4271 | * |
| 4272 | * Set (or reset) an attribute carried by a node. |
| 4273 | * The ns structure must be in scope, this is not checked. |
| 4274 | * |
| 4275 | * Returns the attribute pointer. |
| 4276 | */ |
| 4277 | xmlAttrPtr |
| 4278 | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 4279 | const xmlChar *value) { |
| 4280 | xmlAttrPtr prop; |
| 4281 | |
| 4282 | if ((node == NULL) || (name == NULL)) |
| 4283 | return(NULL); |
| 4284 | |
| 4285 | if (ns == NULL) |
| 4286 | return(xmlSetProp(node, name, value)); |
| 4287 | if (ns->href == NULL) |
| 4288 | return(NULL); |
| 4289 | prop = node->properties; |
| 4290 | |
| 4291 | while (prop != NULL) { |
| 4292 | /* |
| 4293 | * One need to have |
| 4294 | * - same attribute names |
| 4295 | * - and the attribute carrying that namespace |
| 4296 | * or |
| 4297 | * no namespace on the attribute and the element carrying it |
| 4298 | */ |
| 4299 | if ((xmlStrEqual(prop->name, name)) && |
| 4300 | (((prop->ns == NULL) && (node->ns != NULL) && |
| 4301 | (xmlStrEqual(node->ns->href, ns->href))) || |
| 4302 | ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))))) { |
| 4303 | if (prop->children != NULL) |
| 4304 | xmlFreeNodeList(prop->children); |
| 4305 | prop->children = NULL; |
| 4306 | prop->last = NULL; |
| 4307 | prop->ns = ns; |
| 4308 | if (value != NULL) { |
| 4309 | xmlChar *buffer; |
| 4310 | xmlNodePtr tmp; |
| 4311 | |
| 4312 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 4313 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 4314 | prop->last = NULL; |
| 4315 | tmp = prop->children; |
| 4316 | while (tmp != NULL) { |
| 4317 | tmp->parent = (xmlNodePtr) prop; |
| 4318 | if (tmp->next == NULL) |
| 4319 | prop->last = tmp; |
| 4320 | tmp = tmp->next; |
| 4321 | } |
| 4322 | xmlFree(buffer); |
| 4323 | } |
| 4324 | return(prop); |
| 4325 | } |
| 4326 | prop = prop->next; |
| 4327 | } |
| 4328 | prop = xmlNewNsProp(node, ns, name, value); |
| 4329 | return(prop); |
| 4330 | } |
| 4331 | |
| 4332 | /** |
| 4333 | * xmlNodeIsText: |
| 4334 | * @node: the node |
| 4335 | * |
| 4336 | * Is this node a Text node ? |
| 4337 | * Returns 1 yes, 0 no |
| 4338 | */ |
| 4339 | int |
| 4340 | xmlNodeIsText(xmlNodePtr node) { |
| 4341 | if (node == NULL) return(0); |
| 4342 | |
| 4343 | if (node->type == XML_TEXT_NODE) return(1); |
| 4344 | return(0); |
| 4345 | } |
| 4346 | |
| 4347 | /** |
| 4348 | * xmlIsBlankNode: |
| 4349 | * @node: the node |
| 4350 | * |
| 4351 | * Checks whether this node is an empty or whitespace only |
| 4352 | * (and possibly ignorable) text-node. |
| 4353 | * |
| 4354 | * Returns 1 yes, 0 no |
| 4355 | */ |
| 4356 | int |
| 4357 | xmlIsBlankNode(xmlNodePtr node) { |
| 4358 | const xmlChar *cur; |
| 4359 | if (node == NULL) return(0); |
| 4360 | |
| 4361 | if (node->type != XML_TEXT_NODE) return(0); |
| 4362 | if (node->content == NULL) return(1); |
| 4363 | #ifndef XML_USE_BUFFER_CONTENT |
| 4364 | cur = node->content; |
| 4365 | #else |
| 4366 | cur = xmlBufferContent(node->content); |
| 4367 | #endif |
| 4368 | while (*cur != 0) { |
| 4369 | if (!IS_BLANK(*cur)) return(0); |
| 4370 | cur++; |
| 4371 | } |
| 4372 | |
| 4373 | return(1); |
| 4374 | } |
| 4375 | |
| 4376 | /** |
| 4377 | * xmlTextConcat: |
| 4378 | * @node: the node |
| 4379 | * @content: the content |
| 4380 | * @len: @content lenght |
| 4381 | * |
| 4382 | * Concat the given string at the end of the existing node content |
| 4383 | */ |
| 4384 | |
| 4385 | void |
| 4386 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
| 4387 | if (node == NULL) return; |
| 4388 | |
| 4389 | if ((node->type != XML_TEXT_NODE) && |
| 4390 | (node->type != XML_CDATA_SECTION_NODE)) { |
| 4391 | #ifdef DEBUG_TREE |
| 4392 | xmlGenericError(xmlGenericErrorContext, |
| 4393 | "xmlTextConcat: node is not text nor cdata\n"); |
| 4394 | #endif |
| 4395 | return; |
| 4396 | } |
| 4397 | #ifndef XML_USE_BUFFER_CONTENT |
| 4398 | node->content = xmlStrncat(node->content, content, len); |
| 4399 | #else |
| 4400 | xmlBufferAdd(node->content, content, len); |
| 4401 | #endif |
| 4402 | } |
| 4403 | |
| 4404 | /************************************************************************ |
| 4405 | * * |
| 4406 | * Output : to a FILE or in memory * |
| 4407 | * * |
| 4408 | ************************************************************************/ |
| 4409 | |
| 4410 | #define BASE_BUFFER_SIZE 4000 |
| 4411 | |
| 4412 | /** |
| 4413 | * xmlBufferCreate: |
| 4414 | * |
| 4415 | * routine to create an XML buffer. |
| 4416 | * returns the new structure. |
| 4417 | */ |
| 4418 | xmlBufferPtr |
| 4419 | xmlBufferCreate(void) { |
| 4420 | xmlBufferPtr ret; |
| 4421 | |
| 4422 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 4423 | if (ret == NULL) { |
| 4424 | xmlGenericError(xmlGenericErrorContext, |
| 4425 | "xmlBufferCreate : out of memory!\n"); |
| 4426 | return(NULL); |
| 4427 | } |
| 4428 | ret->use = 0; |
| 4429 | ret->size = BASE_BUFFER_SIZE; |
| 4430 | ret->alloc = xmlBufferAllocScheme; |
| 4431 | ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar)); |
| 4432 | if (ret->content == NULL) { |
| 4433 | xmlGenericError(xmlGenericErrorContext, |
| 4434 | "xmlBufferCreate : out of memory!\n"); |
| 4435 | xmlFree(ret); |
| 4436 | return(NULL); |
| 4437 | } |
| 4438 | ret->content[0] = 0; |
| 4439 | return(ret); |
| 4440 | } |
| 4441 | |
| 4442 | /** |
| 4443 | * xmlBufferCreateSize: |
| 4444 | * @size: initial size of buffer |
| 4445 | * |
| 4446 | * routine to create an XML buffer. |
| 4447 | * returns the new structure. |
| 4448 | */ |
| 4449 | xmlBufferPtr |
| 4450 | xmlBufferCreateSize(size_t size) { |
| 4451 | xmlBufferPtr ret; |
| 4452 | |
| 4453 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 4454 | if (ret == NULL) { |
| 4455 | xmlGenericError(xmlGenericErrorContext, |
| 4456 | "xmlBufferCreate : out of memory!\n"); |
| 4457 | return(NULL); |
| 4458 | } |
| 4459 | ret->use = 0; |
| 4460 | ret->alloc = xmlBufferAllocScheme; |
| 4461 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
| 4462 | if (ret->size){ |
| 4463 | ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar)); |
| 4464 | if (ret->content == NULL) { |
| 4465 | xmlGenericError(xmlGenericErrorContext, |
| 4466 | "xmlBufferCreate : out of memory!\n"); |
| 4467 | xmlFree(ret); |
| 4468 | return(NULL); |
| 4469 | } |
| 4470 | ret->content[0] = 0; |
| 4471 | } else |
| 4472 | ret->content = NULL; |
| 4473 | return(ret); |
| 4474 | } |
| 4475 | |
| 4476 | /** |
| 4477 | * xmlBufferSetAllocationScheme: |
| 4478 | * @buf: the buffer to free |
| 4479 | * @scheme: allocation scheme to use |
| 4480 | * |
| 4481 | * Sets the allocation scheme for this buffer |
| 4482 | */ |
| 4483 | void |
| 4484 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
| 4485 | xmlBufferAllocationScheme scheme) { |
| 4486 | if (buf == NULL) { |
| 4487 | #ifdef DEBUG_BUFFER |
| 4488 | xmlGenericError(xmlGenericErrorContext, |
| 4489 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
| 4490 | #endif |
| 4491 | return; |
| 4492 | } |
| 4493 | |
| 4494 | buf->alloc = scheme; |
| 4495 | } |
| 4496 | |
| 4497 | /** |
| 4498 | * xmlBufferFree: |
| 4499 | * @buf: the buffer to free |
| 4500 | * |
| 4501 | * Frees an XML buffer. |
| 4502 | */ |
| 4503 | void |
| 4504 | xmlBufferFree(xmlBufferPtr buf) { |
| 4505 | if (buf == NULL) { |
| 4506 | #ifdef DEBUG_BUFFER |
| 4507 | xmlGenericError(xmlGenericErrorContext, |
| 4508 | "xmlBufferFree: buf == NULL\n"); |
| 4509 | #endif |
| 4510 | return; |
| 4511 | } |
| 4512 | if (buf->content != NULL) { |
| 4513 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4514 | MEM_CLEANUP(buf->content, BASE_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4515 | #else |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4516 | MEM_CLEANUP(buf->content, buf->size); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4517 | #endif |
| 4518 | xmlFree(buf->content); |
| 4519 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4520 | MEM_CLEANUP(buf, sizeof(xmlBuffer)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4521 | xmlFree(buf); |
| 4522 | } |
| 4523 | |
| 4524 | /** |
| 4525 | * xmlBufferEmpty: |
| 4526 | * @buf: the buffer |
| 4527 | * |
| 4528 | * empty a buffer. |
| 4529 | */ |
| 4530 | void |
| 4531 | xmlBufferEmpty(xmlBufferPtr buf) { |
| 4532 | if (buf->content == NULL) return; |
| 4533 | buf->use = 0; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4534 | MEM_CLEANUP(buf->content, buf->size); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
| 4537 | /** |
| 4538 | * xmlBufferShrink: |
| 4539 | * @buf: the buffer to dump |
| 4540 | * @len: the number of xmlChar to remove |
| 4541 | * |
| 4542 | * Remove the beginning of an XML buffer. |
| 4543 | * |
| 4544 | * Returns the number of xmlChar removed, or -1 in case of failure. |
| 4545 | */ |
| 4546 | int |
| 4547 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
| 4548 | if (len == 0) return(0); |
| 4549 | if (len > buf->use) return(-1); |
| 4550 | |
| 4551 | buf->use -= len; |
| 4552 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
| 4553 | |
| 4554 | buf->content[buf->use] = 0; |
| 4555 | return(len); |
| 4556 | } |
| 4557 | |
| 4558 | /** |
| 4559 | * xmlBufferGrow: |
| 4560 | * @buf: the buffer |
| 4561 | * @len: the minimum free size to allocate |
| 4562 | * |
| 4563 | * Grow the available space of an XML buffer. |
| 4564 | * |
| 4565 | * Returns the new available space or -1 in case of error |
| 4566 | */ |
| 4567 | int |
| 4568 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
| 4569 | int size; |
| 4570 | xmlChar *newbuf; |
| 4571 | |
| 4572 | if (len + buf->use < buf->size) return(0); |
| 4573 | |
| 4574 | size = buf->use + len + 100; |
| 4575 | |
| 4576 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
| 4577 | if (newbuf == NULL) return(-1); |
| 4578 | buf->content = newbuf; |
| 4579 | buf->size = size; |
| 4580 | return(buf->size - buf->use); |
| 4581 | } |
| 4582 | |
| 4583 | /** |
| 4584 | * xmlBufferDump: |
| 4585 | * @file: the file output |
| 4586 | * @buf: the buffer to dump |
| 4587 | * |
| 4588 | * Dumps an XML buffer to a FILE *. |
| 4589 | * Returns the number of xmlChar written |
| 4590 | */ |
| 4591 | int |
| 4592 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 4593 | int ret; |
| 4594 | |
| 4595 | if (buf == NULL) { |
| 4596 | #ifdef DEBUG_BUFFER |
| 4597 | xmlGenericError(xmlGenericErrorContext, |
| 4598 | "xmlBufferDump: buf == NULL\n"); |
| 4599 | #endif |
| 4600 | return(0); |
| 4601 | } |
| 4602 | if (buf->content == NULL) { |
| 4603 | #ifdef DEBUG_BUFFER |
| 4604 | xmlGenericError(xmlGenericErrorContext, |
| 4605 | "xmlBufferDump: buf->content == NULL\n"); |
| 4606 | #endif |
| 4607 | return(0); |
| 4608 | } |
| 4609 | if (file == NULL) file = stdout; |
| 4610 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
| 4611 | return(ret); |
| 4612 | } |
| 4613 | |
| 4614 | /** |
| 4615 | * xmlBufferContent: |
| 4616 | * @buf: the buffer |
| 4617 | * |
| 4618 | * Returns the internal content |
| 4619 | */ |
| 4620 | |
| 4621 | const xmlChar* |
| 4622 | xmlBufferContent(const xmlBufferPtr buf) |
| 4623 | { |
| 4624 | if(!buf) |
| 4625 | return NULL; |
| 4626 | |
| 4627 | return buf->content; |
| 4628 | } |
| 4629 | |
| 4630 | /** |
| 4631 | * xmlBufferLength: |
| 4632 | * @buf: the buffer |
| 4633 | * |
| 4634 | * Returns the length of data in the internal content |
| 4635 | */ |
| 4636 | |
| 4637 | int |
| 4638 | xmlBufferLength(const xmlBufferPtr buf) |
| 4639 | { |
| 4640 | if(!buf) |
| 4641 | return 0; |
| 4642 | |
| 4643 | return buf->use; |
| 4644 | } |
| 4645 | |
| 4646 | /** |
| 4647 | * xmlBufferResize: |
| 4648 | * @buf: the buffer to resize |
| 4649 | * @size: the desired size |
| 4650 | * |
| 4651 | * Resize a buffer to accomodate minimum size of @size. |
| 4652 | * |
| 4653 | * Returns 0 in case of problems, 1 otherwise |
| 4654 | */ |
| 4655 | int |
| 4656 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
| 4657 | { |
| 4658 | unsigned int newSize; |
| 4659 | xmlChar* rebuf = NULL; |
| 4660 | |
| 4661 | /*take care of empty case*/ |
| 4662 | newSize = (buf->size ? buf->size*2 : size); |
| 4663 | |
| 4664 | /* Don't resize if we don't have to */ |
| 4665 | if (size < buf->size) |
| 4666 | return 1; |
| 4667 | |
| 4668 | /* figure out new size */ |
| 4669 | switch (buf->alloc){ |
| 4670 | case XML_BUFFER_ALLOC_DOUBLEIT: |
| 4671 | while (size > newSize) newSize *= 2; |
| 4672 | break; |
| 4673 | case XML_BUFFER_ALLOC_EXACT: |
| 4674 | newSize = size+10; |
| 4675 | break; |
| 4676 | default: |
| 4677 | newSize = size+10; |
| 4678 | break; |
| 4679 | } |
| 4680 | |
| 4681 | if (buf->content == NULL) |
| 4682 | rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar)); |
| 4683 | else |
| 4684 | rebuf = (xmlChar *) xmlRealloc(buf->content, |
| 4685 | newSize * sizeof(xmlChar)); |
| 4686 | if (rebuf == NULL) { |
| 4687 | xmlGenericError(xmlGenericErrorContext, |
| 4688 | "xmlBufferAdd : out of memory!\n"); |
| 4689 | return 0; |
| 4690 | } |
| 4691 | buf->content = rebuf; |
| 4692 | buf->size = newSize; |
| 4693 | |
| 4694 | return 1; |
| 4695 | } |
| 4696 | |
| 4697 | /** |
| 4698 | * xmlBufferAdd: |
| 4699 | * @buf: the buffer to dump |
| 4700 | * @str: the xmlChar string |
| 4701 | * @len: the number of xmlChar to add |
| 4702 | * |
| 4703 | * Add a string range to an XML buffer. if len == -1, the lenght of |
| 4704 | * str is recomputed. |
| 4705 | */ |
| 4706 | void |
| 4707 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 4708 | unsigned int needSize; |
| 4709 | |
| 4710 | if (str == NULL) { |
| 4711 | #ifdef DEBUG_BUFFER |
| 4712 | xmlGenericError(xmlGenericErrorContext, |
| 4713 | "xmlBufferAdd: str == NULL\n"); |
| 4714 | #endif |
| 4715 | return; |
| 4716 | } |
| 4717 | if (len < -1) { |
| 4718 | #ifdef DEBUG_BUFFER |
| 4719 | xmlGenericError(xmlGenericErrorContext, |
| 4720 | "xmlBufferAdd: len < 0\n"); |
| 4721 | #endif |
| 4722 | return; |
| 4723 | } |
| 4724 | if (len == 0) return; |
| 4725 | |
| 4726 | if (len < 0) |
| 4727 | len = xmlStrlen(str); |
| 4728 | |
| 4729 | if (len <= 0) return; |
| 4730 | |
| 4731 | needSize = buf->use + len + 2; |
| 4732 | if (needSize > buf->size){ |
| 4733 | if (!xmlBufferResize(buf, needSize)){ |
| 4734 | xmlGenericError(xmlGenericErrorContext, |
| 4735 | "xmlBufferAdd : out of memory!\n"); |
| 4736 | return; |
| 4737 | } |
| 4738 | } |
| 4739 | |
| 4740 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
| 4741 | buf->use += len; |
| 4742 | buf->content[buf->use] = 0; |
| 4743 | } |
| 4744 | |
| 4745 | /** |
| 4746 | * xmlBufferAddHead: |
| 4747 | * @buf: the buffer |
| 4748 | * @str: the xmlChar string |
| 4749 | * @len: the number of xmlChar to add |
| 4750 | * |
| 4751 | * Add a string range to the beginning of an XML buffer. |
| 4752 | * if len == -1, the lenght of @str is recomputed. |
| 4753 | */ |
| 4754 | void |
| 4755 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 4756 | unsigned int needSize; |
| 4757 | |
| 4758 | if (str == NULL) { |
| 4759 | #ifdef DEBUG_BUFFER |
| 4760 | xmlGenericError(xmlGenericErrorContext, |
| 4761 | "xmlBufferAdd: str == NULL\n"); |
| 4762 | #endif |
| 4763 | return; |
| 4764 | } |
| 4765 | if (len < -1) { |
| 4766 | #ifdef DEBUG_BUFFER |
| 4767 | xmlGenericError(xmlGenericErrorContext, |
| 4768 | "xmlBufferAdd: len < 0\n"); |
| 4769 | #endif |
| 4770 | return; |
| 4771 | } |
| 4772 | if (len == 0) return; |
| 4773 | |
| 4774 | if (len < 0) |
| 4775 | len = xmlStrlen(str); |
| 4776 | |
| 4777 | if (len <= 0) return; |
| 4778 | |
| 4779 | needSize = buf->use + len + 2; |
| 4780 | if (needSize > buf->size){ |
| 4781 | if (!xmlBufferResize(buf, needSize)){ |
| 4782 | xmlGenericError(xmlGenericErrorContext, |
| 4783 | "xmlBufferAddHead : out of memory!\n"); |
| 4784 | return; |
| 4785 | } |
| 4786 | } |
| 4787 | |
| 4788 | memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar)); |
| 4789 | memmove(&buf->content[0], str, len * sizeof(xmlChar)); |
| 4790 | buf->use += len; |
| 4791 | buf->content[buf->use] = 0; |
| 4792 | } |
| 4793 | |
| 4794 | /** |
| 4795 | * xmlBufferCat: |
| 4796 | * @buf: the buffer to dump |
| 4797 | * @str: the xmlChar string |
| 4798 | * |
| 4799 | * Append a zero terminated string to an XML buffer. |
| 4800 | */ |
| 4801 | void |
| 4802 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
| 4803 | if (str != NULL) |
| 4804 | xmlBufferAdd(buf, str, -1); |
| 4805 | } |
| 4806 | |
| 4807 | /** |
| 4808 | * xmlBufferCCat: |
| 4809 | * @buf: the buffer to dump |
| 4810 | * @str: the C char string |
| 4811 | * |
| 4812 | * Append a zero terminated C string to an XML buffer. |
| 4813 | */ |
| 4814 | void |
| 4815 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 4816 | const char *cur; |
| 4817 | |
| 4818 | if (str == NULL) { |
| 4819 | #ifdef DEBUG_BUFFER |
| 4820 | xmlGenericError(xmlGenericErrorContext, |
| 4821 | "xmlBufferAdd: str == NULL\n"); |
| 4822 | #endif |
| 4823 | return; |
| 4824 | } |
| 4825 | for (cur = str;*cur != 0;cur++) { |
| 4826 | if (buf->use + 10 >= buf->size) { |
| 4827 | if (!xmlBufferResize(buf, buf->use+10)){ |
| 4828 | xmlGenericError(xmlGenericErrorContext, |
| 4829 | "xmlBufferCCat : out of memory!\n"); |
| 4830 | return; |
| 4831 | } |
| 4832 | } |
| 4833 | buf->content[buf->use++] = *cur; |
| 4834 | } |
| 4835 | buf->content[buf->use] = 0; |
| 4836 | } |
| 4837 | |
| 4838 | /** |
| 4839 | * xmlBufferWriteCHAR: |
| 4840 | * @buf: the XML buffer |
| 4841 | * @string: the string to add |
| 4842 | * |
| 4843 | * routine which manages and grows an output buffer. This one adds |
| 4844 | * xmlChars at the end of the buffer. |
| 4845 | */ |
| 4846 | void |
| 4847 | #ifdef VMS |
| 4848 | xmlBufferWriteXmlCHAR |
| 4849 | #else |
| 4850 | xmlBufferWriteCHAR |
| 4851 | #endif |
| 4852 | (xmlBufferPtr buf, const xmlChar *string) { |
| 4853 | xmlBufferCat(buf, string); |
| 4854 | } |
| 4855 | |
| 4856 | /** |
| 4857 | * xmlBufferWriteChar: |
| 4858 | * @buf: the XML buffer output |
| 4859 | * @string: the string to add |
| 4860 | * |
| 4861 | * routine which manage and grows an output buffer. This one add |
| 4862 | * C chars at the end of the array. |
| 4863 | */ |
| 4864 | void |
| 4865 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
| 4866 | xmlBufferCCat(buf, string); |
| 4867 | } |
| 4868 | |
| 4869 | |
| 4870 | /** |
| 4871 | * xmlBufferWriteQuotedString: |
| 4872 | * @buf: the XML buffer output |
| 4873 | * @string: the string to add |
| 4874 | * |
| 4875 | * routine which manage and grows an output buffer. This one writes |
| 4876 | * a quoted or double quoted xmlChar string, checking first if it holds |
| 4877 | * quote or double-quotes internally |
| 4878 | */ |
| 4879 | void |
| 4880 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
| 4881 | if (xmlStrchr(string, '"')) { |
| 4882 | if (xmlStrchr(string, '\'')) { |
| 4883 | #ifdef DEBUG_BUFFER |
| 4884 | xmlGenericError(xmlGenericErrorContext, |
| 4885 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 4886 | #endif |
| 4887 | } |
| 4888 | xmlBufferCCat(buf, "'"); |
| 4889 | xmlBufferCat(buf, string); |
| 4890 | xmlBufferCCat(buf, "'"); |
| 4891 | } else { |
| 4892 | xmlBufferCCat(buf, "\""); |
| 4893 | xmlBufferCat(buf, string); |
| 4894 | xmlBufferCCat(buf, "\""); |
| 4895 | } |
| 4896 | } |
| 4897 | |
| 4898 | |
| 4899 | /************************************************************************ |
| 4900 | * * |
| 4901 | * Dumping XML tree content to a simple buffer * |
| 4902 | * * |
| 4903 | ************************************************************************/ |
| 4904 | |
| 4905 | void |
| 4906 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4907 | int format); |
| 4908 | static void |
| 4909 | xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4910 | int format); |
| 4911 | void |
| 4912 | htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur); |
| 4913 | |
| 4914 | /** |
| 4915 | * xmlNsDump: |
| 4916 | * @buf: the XML buffer output |
| 4917 | * @cur: a namespace |
| 4918 | * |
| 4919 | * Dump a local Namespace definition. |
| 4920 | * Should be called in the context of attributes dumps. |
| 4921 | */ |
| 4922 | static void |
| 4923 | xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) { |
| 4924 | if (cur == NULL) { |
| 4925 | #ifdef DEBUG_TREE |
| 4926 | xmlGenericError(xmlGenericErrorContext, |
| 4927 | "xmlNsDump : Ns == NULL\n"); |
| 4928 | #endif |
| 4929 | return; |
| 4930 | } |
| 4931 | if (cur->type == XML_LOCAL_NAMESPACE) { |
| 4932 | /* Within the context of an element attributes */ |
| 4933 | if (cur->prefix != NULL) { |
| 4934 | xmlBufferWriteChar(buf, " xmlns:"); |
| 4935 | xmlBufferWriteCHAR(buf, cur->prefix); |
| 4936 | } else |
| 4937 | xmlBufferWriteChar(buf, " xmlns"); |
| 4938 | xmlBufferWriteChar(buf, "="); |
| 4939 | xmlBufferWriteQuotedString(buf, cur->href); |
| 4940 | } |
| 4941 | } |
| 4942 | |
| 4943 | /** |
| 4944 | * xmlNsListDump: |
| 4945 | * @buf: the XML buffer output |
| 4946 | * @cur: the first namespace |
| 4947 | * |
| 4948 | * Dump a list of local Namespace definitions. |
| 4949 | * Should be called in the context of attributes dumps. |
| 4950 | */ |
| 4951 | static void |
| 4952 | xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) { |
| 4953 | while (cur != NULL) { |
| 4954 | xmlNsDump(buf, cur); |
| 4955 | cur = cur->next; |
| 4956 | } |
| 4957 | } |
| 4958 | |
| 4959 | /** |
| 4960 | * xmlDtdDump: |
| 4961 | * @buf: the XML buffer output |
| 4962 | * @doc: the document |
| 4963 | * |
| 4964 | * Dump the XML document DTD, if any. |
| 4965 | */ |
| 4966 | static void |
| 4967 | xmlDtdDump(xmlBufferPtr buf, xmlDtdPtr dtd) { |
| 4968 | if (dtd == NULL) { |
| 4969 | #ifdef DEBUG_TREE |
| 4970 | xmlGenericError(xmlGenericErrorContext, |
| 4971 | "xmlDtdDump : no internal subset\n"); |
| 4972 | #endif |
| 4973 | return; |
| 4974 | } |
| 4975 | xmlBufferWriteChar(buf, "<!DOCTYPE "); |
| 4976 | xmlBufferWriteCHAR(buf, dtd->name); |
| 4977 | if (dtd->ExternalID != NULL) { |
| 4978 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 4979 | xmlBufferWriteQuotedString(buf, dtd->ExternalID); |
| 4980 | xmlBufferWriteChar(buf, " "); |
| 4981 | xmlBufferWriteQuotedString(buf, dtd->SystemID); |
| 4982 | } else if (dtd->SystemID != NULL) { |
| 4983 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 4984 | xmlBufferWriteQuotedString(buf, dtd->SystemID); |
| 4985 | } |
| 4986 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
| 4987 | (dtd->attributes == NULL) && (dtd->notations == NULL)) { |
| 4988 | xmlBufferWriteChar(buf, ">"); |
| 4989 | return; |
| 4990 | } |
| 4991 | xmlBufferWriteChar(buf, " [\n"); |
| 4992 | xmlNodeListDump(buf, dtd->doc, dtd->children, -1, 0); |
| 4993 | #if 0 |
| 4994 | if (dtd->entities != NULL) |
| 4995 | xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) dtd->entities); |
| 4996 | if (dtd->notations != NULL) |
| 4997 | xmlDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations); |
| 4998 | if (dtd->elements != NULL) |
| 4999 | xmlDumpElementTable(buf, (xmlElementTablePtr) dtd->elements); |
| 5000 | if (dtd->attributes != NULL) |
| 5001 | xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) dtd->attributes); |
| 5002 | #endif |
| 5003 | xmlBufferWriteChar(buf, "]>"); |
| 5004 | } |
| 5005 | |
| 5006 | /** |
| 5007 | * xmlAttrDump: |
| 5008 | * @buf: the XML buffer output |
| 5009 | * @doc: the document |
| 5010 | * @cur: the attribute pointer |
| 5011 | * |
| 5012 | * Dump an XML attribute |
| 5013 | */ |
| 5014 | static void |
| 5015 | xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
| 5016 | xmlChar *value; |
| 5017 | |
| 5018 | if (cur == NULL) { |
| 5019 | #ifdef DEBUG_TREE |
| 5020 | xmlGenericError(xmlGenericErrorContext, |
| 5021 | "xmlAttrDump : property == NULL\n"); |
| 5022 | #endif |
| 5023 | return; |
| 5024 | } |
| 5025 | xmlBufferWriteChar(buf, " "); |
| 5026 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5027 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 5028 | xmlBufferWriteChar(buf, ":"); |
| 5029 | } |
| 5030 | xmlBufferWriteCHAR(buf, cur->name); |
| 5031 | value = xmlNodeListGetString(doc, cur->children, 0); |
| 5032 | if (value != NULL) { |
| 5033 | xmlBufferWriteChar(buf, "="); |
| 5034 | xmlBufferWriteQuotedString(buf, value); |
| 5035 | xmlFree(value); |
| 5036 | } else { |
| 5037 | xmlBufferWriteChar(buf, "=\"\""); |
| 5038 | } |
| 5039 | } |
| 5040 | |
| 5041 | /** |
| 5042 | * xmlAttrListDump: |
| 5043 | * @buf: the XML buffer output |
| 5044 | * @doc: the document |
| 5045 | * @cur: the first attribute pointer |
| 5046 | * |
| 5047 | * Dump a list of XML attributes |
| 5048 | */ |
| 5049 | static void |
| 5050 | xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
| 5051 | if (cur == NULL) { |
| 5052 | #ifdef DEBUG_TREE |
| 5053 | xmlGenericError(xmlGenericErrorContext, |
| 5054 | "xmlAttrListDump : property == NULL\n"); |
| 5055 | #endif |
| 5056 | return; |
| 5057 | } |
| 5058 | while (cur != NULL) { |
| 5059 | xmlAttrDump(buf, doc, cur); |
| 5060 | cur = cur->next; |
| 5061 | } |
| 5062 | } |
| 5063 | |
| 5064 | |
| 5065 | |
| 5066 | /** |
| 5067 | * xmlNodeListDump: |
| 5068 | * @buf: the XML buffer output |
| 5069 | * @doc: the document |
| 5070 | * @cur: the first node |
| 5071 | * @level: the imbrication level for indenting |
| 5072 | * @format: is formatting allowed |
| 5073 | * |
| 5074 | * Dump an XML node list, recursive behaviour,children are printed too. |
| 5075 | */ |
| 5076 | static void |
| 5077 | xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 5078 | int format) { |
| 5079 | int i; |
| 5080 | |
| 5081 | if (cur == NULL) { |
| 5082 | #ifdef DEBUG_TREE |
| 5083 | xmlGenericError(xmlGenericErrorContext, |
| 5084 | "xmlNodeListDump : node == NULL\n"); |
| 5085 | #endif |
| 5086 | return; |
| 5087 | } |
| 5088 | while (cur != NULL) { |
| 5089 | if ((format) && (xmlIndentTreeOutput) && |
| 5090 | (cur->type == XML_ELEMENT_NODE)) |
| 5091 | for (i = 0;i < level;i++) |
| 5092 | xmlBufferWriteChar(buf, " "); |
| 5093 | xmlNodeDump(buf, doc, cur, level, format); |
| 5094 | if (format) { |
| 5095 | xmlBufferWriteChar(buf, "\n"); |
| 5096 | } |
| 5097 | cur = cur->next; |
| 5098 | } |
| 5099 | } |
| 5100 | |
| 5101 | /** |
| 5102 | * xmlNodeDump: |
| 5103 | * @buf: the XML buffer output |
| 5104 | * @doc: the document |
| 5105 | * @cur: the current node |
| 5106 | * @level: the imbrication level for indenting |
| 5107 | * @format: is formatting allowed |
| 5108 | * |
| 5109 | * Dump an XML node, recursive behaviour,children are printed too. |
| 5110 | */ |
| 5111 | void |
| 5112 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 5113 | int format) { |
| 5114 | int i; |
| 5115 | xmlNodePtr tmp; |
| 5116 | |
| 5117 | if (cur == NULL) { |
| 5118 | #ifdef DEBUG_TREE |
| 5119 | xmlGenericError(xmlGenericErrorContext, |
| 5120 | "xmlNodeDump : node == NULL\n"); |
| 5121 | #endif |
| 5122 | return; |
| 5123 | } |
| 5124 | if (cur->type == XML_XINCLUDE_START) |
| 5125 | return; |
| 5126 | if (cur->type == XML_XINCLUDE_END) |
| 5127 | return; |
| 5128 | if (cur->type == XML_DTD_NODE) { |
| 5129 | xmlDtdDump(buf, (xmlDtdPtr) cur); |
| 5130 | return; |
| 5131 | } |
| 5132 | if (cur->type == XML_ELEMENT_DECL) { |
| 5133 | xmlDumpElementDecl(buf, (xmlElementPtr) cur); |
| 5134 | return; |
| 5135 | } |
| 5136 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 5137 | xmlDumpAttributeDecl(buf, (xmlAttributePtr) cur); |
| 5138 | return; |
| 5139 | } |
| 5140 | if (cur->type == XML_ENTITY_DECL) { |
| 5141 | xmlDumpEntityDecl(buf, (xmlEntityPtr) cur); |
| 5142 | return; |
| 5143 | } |
| 5144 | if (cur->type == XML_TEXT_NODE) { |
| 5145 | if (cur->content != NULL) { |
| 5146 | if ((cur->name == xmlStringText) || |
| 5147 | (cur->name != xmlStringTextNoenc)) { |
| 5148 | xmlChar *buffer; |
| 5149 | |
| 5150 | #ifndef XML_USE_BUFFER_CONTENT |
| 5151 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 5152 | #else |
| 5153 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5154 | xmlBufferContent(cur->content)); |
| 5155 | #endif |
| 5156 | if (buffer != NULL) { |
| 5157 | xmlBufferWriteCHAR(buf, buffer); |
| 5158 | xmlFree(buffer); |
| 5159 | } |
| 5160 | } else { |
| 5161 | /* |
| 5162 | * Disable escaping, needed for XSLT |
| 5163 | */ |
| 5164 | #ifndef XML_USE_BUFFER_CONTENT |
| 5165 | xmlBufferWriteCHAR(buf, cur->content); |
| 5166 | #else |
| 5167 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 5168 | #endif |
| 5169 | } |
| 5170 | } |
| 5171 | return; |
| 5172 | } |
| 5173 | if (cur->type == XML_PI_NODE) { |
| 5174 | if (cur->content != NULL) { |
| 5175 | xmlBufferWriteChar(buf, "<?"); |
| 5176 | xmlBufferWriteCHAR(buf, cur->name); |
| 5177 | if (cur->content != NULL) { |
| 5178 | xmlBufferWriteChar(buf, " "); |
| 5179 | #ifndef XML_USE_BUFFER_CONTENT |
| 5180 | xmlBufferWriteCHAR(buf, cur->content); |
| 5181 | #else |
| 5182 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 5183 | #endif |
| 5184 | } |
| 5185 | xmlBufferWriteChar(buf, "?>"); |
| 5186 | } else { |
| 5187 | xmlBufferWriteChar(buf, "<?"); |
| 5188 | xmlBufferWriteCHAR(buf, cur->name); |
| 5189 | xmlBufferWriteChar(buf, "?>"); |
| 5190 | } |
| 5191 | return; |
| 5192 | } |
| 5193 | if (cur->type == XML_COMMENT_NODE) { |
| 5194 | if (cur->content != NULL) { |
| 5195 | xmlBufferWriteChar(buf, "<!--"); |
| 5196 | #ifndef XML_USE_BUFFER_CONTENT |
| 5197 | xmlBufferWriteCHAR(buf, cur->content); |
| 5198 | #else |
| 5199 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 5200 | #endif |
| 5201 | xmlBufferWriteChar(buf, "-->"); |
| 5202 | } |
| 5203 | return; |
| 5204 | } |
| 5205 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 5206 | xmlBufferWriteChar(buf, "&"); |
| 5207 | xmlBufferWriteCHAR(buf, cur->name); |
| 5208 | xmlBufferWriteChar(buf, ";"); |
| 5209 | return; |
| 5210 | } |
| 5211 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 5212 | xmlBufferWriteChar(buf, "<![CDATA["); |
| 5213 | if (cur->content != NULL) |
| 5214 | #ifndef XML_USE_BUFFER_CONTENT |
| 5215 | xmlBufferWriteCHAR(buf, cur->content); |
| 5216 | #else |
| 5217 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 5218 | #endif |
| 5219 | xmlBufferWriteChar(buf, "]]>"); |
| 5220 | return; |
| 5221 | } |
| 5222 | |
| 5223 | if (format == 1) { |
| 5224 | tmp = cur->children; |
| 5225 | while (tmp != NULL) { |
| 5226 | if ((tmp->type == XML_TEXT_NODE) || |
| 5227 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 5228 | format = 0; |
| 5229 | break; |
| 5230 | } |
| 5231 | tmp = tmp->next; |
| 5232 | } |
| 5233 | } |
| 5234 | xmlBufferWriteChar(buf, "<"); |
| 5235 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5236 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 5237 | xmlBufferWriteChar(buf, ":"); |
| 5238 | } |
| 5239 | |
| 5240 | xmlBufferWriteCHAR(buf, cur->name); |
| 5241 | if (cur->nsDef) |
| 5242 | xmlNsListDump(buf, cur->nsDef); |
| 5243 | if (cur->properties != NULL) |
| 5244 | xmlAttrListDump(buf, doc, cur->properties); |
| 5245 | |
| 5246 | if ((cur->content == NULL) && (cur->children == NULL) && |
| 5247 | (!xmlSaveNoEmptyTags)) { |
| 5248 | xmlBufferWriteChar(buf, "/>"); |
| 5249 | return; |
| 5250 | } |
| 5251 | xmlBufferWriteChar(buf, ">"); |
| 5252 | if (cur->content != NULL) { |
| 5253 | xmlChar *buffer; |
| 5254 | |
| 5255 | #ifndef XML_USE_BUFFER_CONTENT |
| 5256 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 5257 | #else |
| 5258 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5259 | xmlBufferContent(cur->content)); |
| 5260 | #endif |
| 5261 | if (buffer != NULL) { |
| 5262 | xmlBufferWriteCHAR(buf, buffer); |
| 5263 | xmlFree(buffer); |
| 5264 | } |
| 5265 | } |
| 5266 | if (cur->children != NULL) { |
| 5267 | if (format) xmlBufferWriteChar(buf, "\n"); |
| 5268 | xmlNodeListDump(buf, doc, cur->children, |
| 5269 | (level >= 0?level+1:-1), format); |
| 5270 | if ((xmlIndentTreeOutput) && (format)) |
| 5271 | for (i = 0;i < level;i++) |
| 5272 | xmlBufferWriteChar(buf, " "); |
| 5273 | } |
| 5274 | xmlBufferWriteChar(buf, "</"); |
| 5275 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5276 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 5277 | xmlBufferWriteChar(buf, ":"); |
| 5278 | } |
| 5279 | |
| 5280 | xmlBufferWriteCHAR(buf, cur->name); |
| 5281 | xmlBufferWriteChar(buf, ">"); |
| 5282 | } |
| 5283 | |
| 5284 | /** |
| 5285 | * xmlElemDump: |
| 5286 | * @f: the FILE * for the output |
| 5287 | * @doc: the document |
| 5288 | * @cur: the current node |
| 5289 | * |
| 5290 | * Dump an XML/HTML node, recursive behaviour,children are printed too. |
| 5291 | */ |
| 5292 | void |
| 5293 | xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) { |
| 5294 | xmlBufferPtr buf; |
| 5295 | |
| 5296 | if (cur == NULL) { |
| 5297 | #ifdef DEBUG_TREE |
| 5298 | xmlGenericError(xmlGenericErrorContext, |
| 5299 | "xmlElemDump : cur == NULL\n"); |
| 5300 | #endif |
| 5301 | return; |
| 5302 | } |
| 5303 | if (doc == NULL) { |
| 5304 | #ifdef DEBUG_TREE |
| 5305 | xmlGenericError(xmlGenericErrorContext, |
| 5306 | "xmlElemDump : doc == NULL\n"); |
| 5307 | #endif |
| 5308 | } |
| 5309 | buf = xmlBufferCreate(); |
| 5310 | if (buf == NULL) return; |
| 5311 | if ((doc != NULL) && |
| 5312 | (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 5313 | #ifdef LIBXML_HTML_ENABLED |
| 5314 | htmlNodeDump(buf, doc, cur); |
| 5315 | #else |
| 5316 | xmlGenericError(xmlGenericErrorContext, |
| 5317 | "HTML support not compiled in\n"); |
| 5318 | #endif /* LIBXML_HTML_ENABLED */ |
| 5319 | } else |
| 5320 | xmlNodeDump(buf, doc, cur, 0, 1); |
| 5321 | xmlBufferDump(f, buf); |
| 5322 | xmlBufferFree(buf); |
| 5323 | } |
| 5324 | |
| 5325 | /************************************************************************ |
| 5326 | * * |
| 5327 | * Dumping XML tree content to an I/O output buffer * |
| 5328 | * * |
| 5329 | ************************************************************************/ |
| 5330 | |
| 5331 | void |
| 5332 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 5333 | int level, int format, const char *encoding); |
| 5334 | static void |
| 5335 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 5336 | int level, int format, const char *encoding); |
| 5337 | /** |
| 5338 | * xmlNsDumpOutput: |
| 5339 | * @buf: the XML buffer output |
| 5340 | * @cur: a namespace |
| 5341 | * |
| 5342 | * Dump a local Namespace definition. |
| 5343 | * Should be called in the context of attributes dumps. |
| 5344 | */ |
| 5345 | static void |
| 5346 | xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 5347 | if (cur == NULL) { |
| 5348 | #ifdef DEBUG_TREE |
| 5349 | xmlGenericError(xmlGenericErrorContext, |
| 5350 | "xmlNsDump : Ns == NULL\n"); |
| 5351 | #endif |
| 5352 | return; |
| 5353 | } |
| 5354 | if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) { |
| 5355 | /* Within the context of an element attributes */ |
| 5356 | if (cur->prefix != NULL) { |
| 5357 | xmlOutputBufferWriteString(buf, " xmlns:"); |
| 5358 | xmlOutputBufferWriteString(buf, (const char *)cur->prefix); |
| 5359 | } else |
| 5360 | xmlOutputBufferWriteString(buf, " xmlns"); |
| 5361 | xmlOutputBufferWriteString(buf, "="); |
| 5362 | xmlBufferWriteQuotedString(buf->buffer, cur->href); |
| 5363 | } |
| 5364 | } |
| 5365 | |
| 5366 | /** |
| 5367 | * xmlNsListDumpOutput: |
| 5368 | * @buf: the XML buffer output |
| 5369 | * @cur: the first namespace |
| 5370 | * |
| 5371 | * Dump a list of local Namespace definitions. |
| 5372 | * Should be called in the context of attributes dumps. |
| 5373 | */ |
| 5374 | static void |
| 5375 | xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 5376 | while (cur != NULL) { |
| 5377 | xmlNsDumpOutput(buf, cur); |
| 5378 | cur = cur->next; |
| 5379 | } |
| 5380 | } |
| 5381 | |
| 5382 | /** |
| 5383 | * xmlDtdDumpOutput: |
| 5384 | * @buf: the XML buffer output |
| 5385 | * @doc: the document |
| 5386 | * @encoding: an optional encoding string |
| 5387 | * |
| 5388 | * Dump the XML document DTD, if any. |
| 5389 | */ |
| 5390 | static void |
| 5391 | xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) { |
| 5392 | if (dtd == NULL) { |
| 5393 | #ifdef DEBUG_TREE |
| 5394 | xmlGenericError(xmlGenericErrorContext, |
| 5395 | "xmlDtdDump : no internal subset\n"); |
| 5396 | #endif |
| 5397 | return; |
| 5398 | } |
| 5399 | xmlOutputBufferWriteString(buf, "<!DOCTYPE "); |
| 5400 | xmlOutputBufferWriteString(buf, (const char *)dtd->name); |
| 5401 | if (dtd->ExternalID != NULL) { |
| 5402 | xmlOutputBufferWriteString(buf, " PUBLIC "); |
| 5403 | xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID); |
| 5404 | xmlOutputBufferWriteString(buf, " "); |
| 5405 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 5406 | } else if (dtd->SystemID != NULL) { |
| 5407 | xmlOutputBufferWriteString(buf, " SYSTEM "); |
| 5408 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 5409 | } |
| 5410 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
| 5411 | (dtd->attributes == NULL) && (dtd->notations == NULL)) { |
| 5412 | xmlOutputBufferWriteString(buf, ">"); |
| 5413 | return; |
| 5414 | } |
| 5415 | xmlOutputBufferWriteString(buf, " [\n"); |
| 5416 | xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding); |
| 5417 | xmlOutputBufferWriteString(buf, "]>"); |
| 5418 | } |
| 5419 | |
| 5420 | /** |
| 5421 | * xmlAttrDumpOutput: |
| 5422 | * @buf: the XML buffer output |
| 5423 | * @doc: the document |
| 5424 | * @cur: the attribute pointer |
| 5425 | * @encoding: an optional encoding string |
| 5426 | * |
| 5427 | * Dump an XML attribute |
| 5428 | */ |
| 5429 | static void |
| 5430 | xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, |
| 5431 | const char *encoding) { |
| 5432 | xmlChar *value; |
| 5433 | |
| 5434 | if (cur == NULL) { |
| 5435 | #ifdef DEBUG_TREE |
| 5436 | xmlGenericError(xmlGenericErrorContext, |
| 5437 | "xmlAttrDump : property == NULL\n"); |
| 5438 | #endif |
| 5439 | return; |
| 5440 | } |
| 5441 | xmlOutputBufferWriteString(buf, " "); |
| 5442 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5443 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 5444 | xmlOutputBufferWriteString(buf, ":"); |
| 5445 | } |
| 5446 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5447 | value = xmlNodeListGetString(doc, cur->children, 0); |
| 5448 | if (value) { |
| 5449 | xmlOutputBufferWriteString(buf, "="); |
| 5450 | xmlBufferWriteQuotedString(buf->buffer, value); |
| 5451 | xmlFree(value); |
| 5452 | } else { |
| 5453 | xmlOutputBufferWriteString(buf, "=\"\""); |
| 5454 | } |
| 5455 | } |
| 5456 | |
| 5457 | /** |
| 5458 | * xmlAttrListDumpOutput: |
| 5459 | * @buf: the XML buffer output |
| 5460 | * @doc: the document |
| 5461 | * @cur: the first attribute pointer |
| 5462 | * @encoding: an optional encoding string |
| 5463 | * |
| 5464 | * Dump a list of XML attributes |
| 5465 | */ |
| 5466 | static void |
| 5467 | xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 5468 | xmlAttrPtr cur, const char *encoding) { |
| 5469 | if (cur == NULL) { |
| 5470 | #ifdef DEBUG_TREE |
| 5471 | xmlGenericError(xmlGenericErrorContext, |
| 5472 | "xmlAttrListDump : property == NULL\n"); |
| 5473 | #endif |
| 5474 | return; |
| 5475 | } |
| 5476 | while (cur != NULL) { |
| 5477 | xmlAttrDumpOutput(buf, doc, cur, encoding); |
| 5478 | cur = cur->next; |
| 5479 | } |
| 5480 | } |
| 5481 | |
| 5482 | |
| 5483 | |
| 5484 | /** |
| 5485 | * xmlNodeListDumpOutput: |
| 5486 | * @buf: the XML buffer output |
| 5487 | * @doc: the document |
| 5488 | * @cur: the first node |
| 5489 | * @level: the imbrication level for indenting |
| 5490 | * @format: is formatting allowed |
| 5491 | * @encoding: an optional encoding string |
| 5492 | * |
| 5493 | * Dump an XML node list, recursive behaviour,children are printed too. |
| 5494 | */ |
| 5495 | static void |
| 5496 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 5497 | xmlNodePtr cur, int level, int format, const char *encoding) { |
| 5498 | int i; |
| 5499 | |
| 5500 | if (cur == NULL) { |
| 5501 | #ifdef DEBUG_TREE |
| 5502 | xmlGenericError(xmlGenericErrorContext, |
| 5503 | "xmlNodeListDump : node == NULL\n"); |
| 5504 | #endif |
| 5505 | return; |
| 5506 | } |
| 5507 | while (cur != NULL) { |
| 5508 | if ((format) && (xmlIndentTreeOutput) && |
| 5509 | (cur->type == XML_ELEMENT_NODE)) |
| 5510 | for (i = 0;i < level;i++) |
| 5511 | xmlOutputBufferWriteString(buf, " "); |
| 5512 | xmlNodeDumpOutput(buf, doc, cur, level, format, encoding); |
| 5513 | if (format) { |
| 5514 | xmlOutputBufferWriteString(buf, "\n"); |
| 5515 | } |
| 5516 | cur = cur->next; |
| 5517 | } |
| 5518 | } |
| 5519 | |
| 5520 | /** |
| 5521 | * xmlNodeDumpOutput: |
| 5522 | * @buf: the XML buffer output |
| 5523 | * @doc: the document |
| 5524 | * @cur: the current node |
| 5525 | * @level: the imbrication level for indenting |
| 5526 | * @format: is formatting allowed |
| 5527 | * @encoding: an optional encoding string |
| 5528 | * |
| 5529 | * Dump an XML node, recursive behaviour,children are printed too. |
| 5530 | */ |
| 5531 | void |
| 5532 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 5533 | int level, int format, const char *encoding) { |
| 5534 | int i; |
| 5535 | xmlNodePtr tmp; |
| 5536 | |
| 5537 | if (cur == NULL) { |
| 5538 | #ifdef DEBUG_TREE |
| 5539 | xmlGenericError(xmlGenericErrorContext, |
| 5540 | "xmlNodeDump : node == NULL\n"); |
| 5541 | #endif |
| 5542 | return; |
| 5543 | } |
| 5544 | if (cur->type == XML_XINCLUDE_START) |
| 5545 | return; |
| 5546 | if (cur->type == XML_XINCLUDE_END) |
| 5547 | return; |
| 5548 | if (cur->type == XML_DTD_NODE) { |
| 5549 | xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding); |
| 5550 | return; |
| 5551 | } |
| 5552 | if (cur->type == XML_ELEMENT_DECL) { |
| 5553 | xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
| 5554 | return; |
| 5555 | } |
| 5556 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 5557 | xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
| 5558 | return; |
| 5559 | } |
| 5560 | if (cur->type == XML_ENTITY_DECL) { |
| 5561 | xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
| 5562 | return; |
| 5563 | } |
| 5564 | if (cur->type == XML_TEXT_NODE) { |
| 5565 | if (cur->content != NULL) { |
| 5566 | if ((cur->name == xmlStringText) || |
| 5567 | (cur->name != xmlStringTextNoenc)) { |
| 5568 | xmlChar *buffer; |
| 5569 | |
| 5570 | #ifndef XML_USE_BUFFER_CONTENT |
| 5571 | if (encoding == NULL) |
| 5572 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 5573 | else |
| 5574 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 5575 | #else |
| 5576 | if (encoding == NULL) |
| 5577 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5578 | xmlBufferContent(cur->content)); |
| 5579 | else |
| 5580 | buffer = xmlEncodeSpecialChars(doc, |
| 5581 | xmlBufferContent(cur->content)); |
| 5582 | #endif |
| 5583 | if (buffer != NULL) { |
| 5584 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 5585 | xmlFree(buffer); |
| 5586 | } |
| 5587 | } else { |
| 5588 | /* |
| 5589 | * Disable escaping, needed for XSLT |
| 5590 | */ |
| 5591 | #ifndef XML_USE_BUFFER_CONTENT |
| 5592 | xmlOutputBufferWriteString(buf, (const char *) cur->content); |
| 5593 | #else |
| 5594 | xmlOutputBufferWriteString(buf, xmlBufferContent(cur->content)); |
| 5595 | #endif |
| 5596 | } |
| 5597 | } |
| 5598 | |
| 5599 | return; |
| 5600 | } |
| 5601 | if (cur->type == XML_PI_NODE) { |
| 5602 | if (cur->content != NULL) { |
| 5603 | xmlOutputBufferWriteString(buf, "<?"); |
| 5604 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5605 | if (cur->content != NULL) { |
| 5606 | xmlOutputBufferWriteString(buf, " "); |
| 5607 | #ifndef XML_USE_BUFFER_CONTENT |
| 5608 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 5609 | #else |
| 5610 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 5611 | #endif |
| 5612 | } |
| 5613 | xmlOutputBufferWriteString(buf, "?>"); |
| 5614 | } else { |
| 5615 | xmlOutputBufferWriteString(buf, "<?"); |
| 5616 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5617 | xmlOutputBufferWriteString(buf, "?>"); |
| 5618 | } |
| 5619 | return; |
| 5620 | } |
| 5621 | if (cur->type == XML_COMMENT_NODE) { |
| 5622 | if (cur->content != NULL) { |
| 5623 | xmlOutputBufferWriteString(buf, "<!--"); |
| 5624 | #ifndef XML_USE_BUFFER_CONTENT |
| 5625 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 5626 | #else |
| 5627 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 5628 | #endif |
| 5629 | xmlOutputBufferWriteString(buf, "-->"); |
| 5630 | } |
| 5631 | return; |
| 5632 | } |
| 5633 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 5634 | xmlOutputBufferWriteString(buf, "&"); |
| 5635 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5636 | xmlOutputBufferWriteString(buf, ";"); |
| 5637 | return; |
| 5638 | } |
| 5639 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 5640 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 5641 | if (cur->content != NULL) |
| 5642 | #ifndef XML_USE_BUFFER_CONTENT |
| 5643 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 5644 | #else |
| 5645 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 5646 | #endif |
| 5647 | xmlOutputBufferWriteString(buf, "]]>"); |
| 5648 | return; |
| 5649 | } |
| 5650 | |
| 5651 | if (format == 1) { |
| 5652 | tmp = cur->children; |
| 5653 | while (tmp != NULL) { |
| 5654 | if ((tmp->type == XML_TEXT_NODE) || |
| 5655 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 5656 | format = 0; |
| 5657 | break; |
| 5658 | } |
| 5659 | tmp = tmp->next; |
| 5660 | } |
| 5661 | } |
| 5662 | xmlOutputBufferWriteString(buf, "<"); |
| 5663 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5664 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 5665 | xmlOutputBufferWriteString(buf, ":"); |
| 5666 | } |
| 5667 | |
| 5668 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5669 | if (cur->nsDef) |
| 5670 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 5671 | if (cur->properties != NULL) |
| 5672 | xmlAttrListDumpOutput(buf, doc, cur->properties, encoding); |
| 5673 | |
| 5674 | if ((cur->content == NULL) && (cur->children == NULL) && |
| 5675 | (!xmlSaveNoEmptyTags)) { |
| 5676 | xmlOutputBufferWriteString(buf, "/>"); |
| 5677 | return; |
| 5678 | } |
| 5679 | xmlOutputBufferWriteString(buf, ">"); |
| 5680 | if (cur->content != NULL) { |
| 5681 | xmlChar *buffer; |
| 5682 | |
| 5683 | #ifndef XML_USE_BUFFER_CONTENT |
| 5684 | if (encoding == NULL) |
| 5685 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 5686 | else |
| 5687 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 5688 | #else |
| 5689 | if (encoding == NULL) |
| 5690 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5691 | xmlBufferContent(cur->content)); |
| 5692 | else |
| 5693 | buffer = xmlEncodeSpecialChars(doc, |
| 5694 | xmlBufferContent(cur->content)); |
| 5695 | #endif |
| 5696 | if (buffer != NULL) { |
| 5697 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 5698 | xmlFree(buffer); |
| 5699 | } |
| 5700 | } |
| 5701 | if (cur->children != NULL) { |
| 5702 | if (format) xmlOutputBufferWriteString(buf, "\n"); |
| 5703 | xmlNodeListDumpOutput(buf, doc, cur->children, |
| 5704 | (level >= 0?level+1:-1), format, encoding); |
| 5705 | if ((xmlIndentTreeOutput) && (format)) |
| 5706 | for (i = 0;i < level;i++) |
| 5707 | xmlOutputBufferWriteString(buf, " "); |
| 5708 | } |
| 5709 | xmlOutputBufferWriteString(buf, "</"); |
| 5710 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5711 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 5712 | xmlOutputBufferWriteString(buf, ":"); |
| 5713 | } |
| 5714 | |
| 5715 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5716 | xmlOutputBufferWriteString(buf, ">"); |
| 5717 | } |
| 5718 | |
| 5719 | /** |
| 5720 | * xmlDocContentDumpOutput: |
| 5721 | * @buf: the XML buffer output |
| 5722 | * @cur: the document |
| 5723 | * @encoding: an optional encoding string |
| 5724 | * @format: should formatting spaces been added |
| 5725 | * |
| 5726 | * Dump an XML document. |
| 5727 | */ |
| 5728 | static void |
| 5729 | xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur, |
| 5730 | const char *encoding, int format) { |
| 5731 | xmlOutputBufferWriteString(buf, "<?xml version="); |
| 5732 | if (cur->version != NULL) |
| 5733 | xmlBufferWriteQuotedString(buf->buffer, cur->version); |
| 5734 | else |
| 5735 | xmlOutputBufferWriteString(buf, "\"1.0\""); |
| 5736 | if (encoding == NULL) { |
| 5737 | if (cur->encoding != NULL) |
| 5738 | encoding = (const char *) cur->encoding; |
| 5739 | else if (cur->charset != XML_CHAR_ENCODING_UTF8) |
| 5740 | encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset); |
| 5741 | } |
| 5742 | if (encoding != NULL) { |
| 5743 | xmlOutputBufferWriteString(buf, " encoding="); |
| 5744 | xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding); |
| 5745 | } |
| 5746 | switch (cur->standalone) { |
| 5747 | case 0: |
| 5748 | xmlOutputBufferWriteString(buf, " standalone=\"no\""); |
| 5749 | break; |
| 5750 | case 1: |
| 5751 | xmlOutputBufferWriteString(buf, " standalone=\"yes\""); |
| 5752 | break; |
| 5753 | } |
| 5754 | xmlOutputBufferWriteString(buf, "?>\n"); |
| 5755 | if (cur->children != NULL) { |
| 5756 | xmlNodePtr child = cur->children; |
| 5757 | |
| 5758 | while (child != NULL) { |
| 5759 | xmlNodeDumpOutput(buf, cur, child, 0, format, encoding); |
| 5760 | xmlOutputBufferWriteString(buf, "\n"); |
| 5761 | child = child->next; |
| 5762 | } |
| 5763 | } |
| 5764 | } |
| 5765 | |
| 5766 | /************************************************************************ |
| 5767 | * * |
| 5768 | * Saving functions front-ends * |
| 5769 | * * |
| 5770 | ************************************************************************/ |
| 5771 | |
| 5772 | /** |
| 5773 | * xmlDocDumpMemoryEnc: |
| 5774 | * @out_doc: Document to generate XML text from |
| 5775 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 5776 | * @doc_txt_len: Length of the generated XML text |
| 5777 | * @txt_encoding: Character encoding to use when generating XML text |
| 5778 | * @format: should formatting spaces been added |
| 5779 | * |
| 5780 | * Dump the current DOM tree into memory using the character encoding specified |
| 5781 | * by the caller. Note it is up to the caller of this function to free the |
| 5782 | * allocated memory. |
| 5783 | */ |
| 5784 | |
| 5785 | void |
| 5786 | xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
| 5787 | int * doc_txt_len, const char * txt_encoding, int format) { |
| 5788 | int dummy = 0; |
| 5789 | |
| 5790 | xmlCharEncoding doc_charset; |
| 5791 | xmlOutputBufferPtr out_buff = NULL; |
| 5792 | xmlCharEncodingHandlerPtr conv_hdlr = NULL; |
| 5793 | |
| 5794 | if (doc_txt_len == NULL) { |
| 5795 | doc_txt_len = &dummy; /* Continue, caller just won't get length */ |
| 5796 | } |
| 5797 | |
| 5798 | if (doc_txt_ptr == NULL) { |
| 5799 | *doc_txt_len = 0; |
| 5800 | xmlGenericError(xmlGenericErrorContext, |
| 5801 | "xmlDocDumpFormatMemoryEnc: Null return buffer pointer."); |
| 5802 | return; |
| 5803 | } |
| 5804 | |
| 5805 | *doc_txt_ptr = NULL; |
| 5806 | *doc_txt_len = 0; |
| 5807 | |
| 5808 | if (out_doc == NULL) { |
| 5809 | /* No document, no output */ |
| 5810 | xmlGenericError(xmlGenericErrorContext, |
| 5811 | "xmlDocDumpFormatMemoryEnc: Null DOM tree document pointer.\n"); |
| 5812 | return; |
| 5813 | } |
| 5814 | |
| 5815 | /* |
| 5816 | * Validate the encoding value, if provided. |
| 5817 | * This logic is copied from xmlSaveFileEnc. |
| 5818 | */ |
| 5819 | |
| 5820 | if (txt_encoding == NULL) |
| 5821 | txt_encoding = (const char *) out_doc->encoding; |
| 5822 | if (txt_encoding != NULL) { |
| 5823 | doc_charset = xmlParseCharEncoding(txt_encoding); |
| 5824 | |
| 5825 | if (out_doc->charset != XML_CHAR_ENCODING_UTF8) { |
| 5826 | xmlGenericError(xmlGenericErrorContext, |
| 5827 | "xmlDocDumpFormatMemoryEnc: Source document not in UTF8\n"); |
| 5828 | return; |
| 5829 | |
| 5830 | } else if (doc_charset != XML_CHAR_ENCODING_UTF8) { |
| 5831 | conv_hdlr = xmlFindCharEncodingHandler(txt_encoding); |
| 5832 | if ( conv_hdlr == NULL ) { |
| 5833 | xmlGenericError(xmlGenericErrorContext, |
| 5834 | "%s: %s %s '%s'\n", |
| 5835 | "xmlDocDumpFormatMemoryEnc", |
| 5836 | "Failed to identify encoding handler for", |
| 5837 | "character set", |
| 5838 | txt_encoding); |
| 5839 | return; |
| 5840 | } |
| 5841 | } |
| 5842 | } |
| 5843 | |
| 5844 | if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) { |
| 5845 | xmlGenericError(xmlGenericErrorContext, |
| 5846 | "xmlDocDumpFormatMemoryEnc: Failed to allocate output buffer.\n"); |
| 5847 | return; |
| 5848 | } |
| 5849 | |
| 5850 | xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, 1); |
| 5851 | xmlOutputBufferFlush(out_buff); |
| 5852 | if (out_buff->conv != NULL) { |
| 5853 | *doc_txt_len = out_buff->conv->use; |
| 5854 | *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len); |
| 5855 | } else { |
| 5856 | *doc_txt_len = out_buff->buffer->use; |
| 5857 | *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len); |
| 5858 | } |
| 5859 | (void)xmlOutputBufferClose(out_buff); |
| 5860 | |
| 5861 | if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) { |
| 5862 | *doc_txt_len = 0; |
| 5863 | xmlGenericError(xmlGenericErrorContext, |
| 5864 | "xmlDocDumpFormatMemoryEnc: %s\n", |
| 5865 | "Failed to allocate memory for document text representation."); |
| 5866 | } |
| 5867 | |
| 5868 | return; |
| 5869 | } |
| 5870 | |
| 5871 | /** |
| 5872 | * xmlDocDumpMemory: |
| 5873 | * @cur: the document |
| 5874 | * @mem: OUT: the memory pointer |
| 5875 | * @size: OUT: the memory lenght |
| 5876 | * |
| 5877 | * Dump an XML document in memory and return the xmlChar * and it's size. |
| 5878 | * It's up to the caller to free the memory. |
| 5879 | */ |
| 5880 | void |
| 5881 | xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { |
| 5882 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0); |
| 5883 | } |
| 5884 | |
| 5885 | /** |
| 5886 | * xmlDocDumpFormatMemory: |
| 5887 | * @cur: the document |
| 5888 | * @mem: OUT: the memory pointer |
| 5889 | * @size: OUT: the memory lenght |
| 5890 | * @format: should formatting spaces been added |
| 5891 | * |
| 5892 | * |
| 5893 | * Dump an XML document in memory and return the xmlChar * and it's size. |
| 5894 | * It's up to the caller to free the memory. |
| 5895 | */ |
| 5896 | void |
| 5897 | xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) { |
| 5898 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format); |
| 5899 | } |
| 5900 | |
| 5901 | /** |
| 5902 | * xmlDocDumpMemoryEnc: |
| 5903 | * @out_doc: Document to generate XML text from |
| 5904 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 5905 | * @doc_txt_len: Length of the generated XML text |
| 5906 | * @txt_encoding: Character encoding to use when generating XML text |
| 5907 | * |
| 5908 | * Dump the current DOM tree into memory using the character encoding specified |
| 5909 | * by the caller. Note it is up to the caller of this function to free the |
| 5910 | * allocated memory. |
| 5911 | */ |
| 5912 | |
| 5913 | void |
| 5914 | xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
| 5915 | int * doc_txt_len, const char * txt_encoding) { |
| 5916 | xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len, |
| 5917 | txt_encoding, 1); |
| 5918 | } |
| 5919 | |
| 5920 | /** |
| 5921 | * xmlGetDocCompressMode: |
| 5922 | * @doc: the document |
| 5923 | * |
| 5924 | * get the compression ratio for a document, ZLIB based |
| 5925 | * Returns 0 (uncompressed) to 9 (max compression) |
| 5926 | */ |
| 5927 | int |
| 5928 | xmlGetDocCompressMode (xmlDocPtr doc) { |
| 5929 | if (doc == NULL) return(-1); |
| 5930 | return(doc->compression); |
| 5931 | } |
| 5932 | |
| 5933 | /** |
| 5934 | * xmlSetDocCompressMode: |
| 5935 | * @doc: the document |
| 5936 | * @mode: the compression ratio |
| 5937 | * |
| 5938 | * set the compression ratio for a document, ZLIB based |
| 5939 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 5940 | */ |
| 5941 | void |
| 5942 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
| 5943 | if (doc == NULL) return; |
| 5944 | if (mode < 0) doc->compression = 0; |
| 5945 | else if (mode > 9) doc->compression = 9; |
| 5946 | else doc->compression = mode; |
| 5947 | } |
| 5948 | |
| 5949 | /** |
| 5950 | * xmlGetCompressMode: |
| 5951 | * |
| 5952 | * get the default compression mode used, ZLIB based. |
| 5953 | * Returns 0 (uncompressed) to 9 (max compression) |
| 5954 | */ |
| 5955 | int |
| 5956 | xmlGetCompressMode(void) { |
| 5957 | return(xmlCompressMode); |
| 5958 | } |
| 5959 | |
| 5960 | /** |
| 5961 | * xmlSetCompressMode: |
| 5962 | * @mode: the compression ratio |
| 5963 | * |
| 5964 | * set the default compression mode used, ZLIB based |
| 5965 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 5966 | */ |
| 5967 | void |
| 5968 | xmlSetCompressMode(int mode) { |
| 5969 | if (mode < 0) xmlCompressMode = 0; |
| 5970 | else if (mode > 9) xmlCompressMode = 9; |
| 5971 | else xmlCompressMode = mode; |
| 5972 | } |
| 5973 | |
| 5974 | /** |
| 5975 | * xmlDocDump: |
| 5976 | * @f: the FILE* |
| 5977 | * @cur: the document |
| 5978 | * |
| 5979 | * Dump an XML document to an open FILE. |
| 5980 | * |
| 5981 | * returns: the number of byte written or -1 in case of failure. |
| 5982 | */ |
| 5983 | int |
| 5984 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
| 5985 | xmlOutputBufferPtr buf; |
| 5986 | const char * encoding; |
| 5987 | xmlCharEncodingHandlerPtr handler = NULL; |
| 5988 | int ret; |
| 5989 | |
| 5990 | if (cur == NULL) { |
| 5991 | #ifdef DEBUG_TREE |
| 5992 | xmlGenericError(xmlGenericErrorContext, |
| 5993 | "xmlDocDump : document == NULL\n"); |
| 5994 | #endif |
| 5995 | return(-1); |
| 5996 | } |
| 5997 | encoding = (const char *) cur->encoding; |
| 5998 | |
| 5999 | if (encoding != NULL) { |
| 6000 | xmlCharEncoding enc; |
| 6001 | |
| 6002 | enc = xmlParseCharEncoding(encoding); |
| 6003 | |
| 6004 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
| 6005 | xmlGenericError(xmlGenericErrorContext, |
| 6006 | "xmlDocDump: document not in UTF8\n"); |
| 6007 | return(-1); |
| 6008 | } |
| 6009 | if (enc != XML_CHAR_ENCODING_UTF8) { |
| 6010 | handler = xmlFindCharEncodingHandler(encoding); |
| 6011 | if (handler == NULL) { |
| 6012 | xmlFree((char *) cur->encoding); |
| 6013 | cur->encoding = NULL; |
| 6014 | } |
| 6015 | } |
| 6016 | } |
| 6017 | buf = xmlOutputBufferCreateFile(f, handler); |
| 6018 | if (buf == NULL) return(-1); |
| 6019 | xmlDocContentDumpOutput(buf, cur, NULL, 1); |
| 6020 | |
| 6021 | ret = xmlOutputBufferClose(buf); |
| 6022 | return(ret); |
| 6023 | } |
| 6024 | |
| 6025 | /** |
| 6026 | * xmlSaveFileTo: |
| 6027 | * @buf: an output I/O buffer |
| 6028 | * @cur: the document |
| 6029 | * @encoding: the encoding if any assuming the i/O layer handles the trancoding |
| 6030 | * |
| 6031 | * Dump an XML document to an I/O buffer. |
| 6032 | * |
| 6033 | * returns: the number of byte written or -1 in case of failure. |
| 6034 | */ |
| 6035 | int |
| 6036 | xmlSaveFileTo(xmlOutputBuffer *buf, xmlDocPtr cur, const char *encoding) { |
| 6037 | int ret; |
| 6038 | |
| 6039 | if (buf == NULL) return(0); |
| 6040 | xmlDocContentDumpOutput(buf, cur, encoding, 1); |
| 6041 | ret = xmlOutputBufferClose(buf); |
| 6042 | return(ret); |
| 6043 | } |
| 6044 | |
| 6045 | /** |
| 6046 | * xmlSaveFileEnc: |
| 6047 | * @filename: the filename (or URL) |
| 6048 | * @cur: the document |
| 6049 | * @encoding: the name of an encoding (or NULL) |
| 6050 | * |
| 6051 | * Dump an XML document, converting it to the given encoding |
| 6052 | * |
| 6053 | * returns: the number of byte written or -1 in case of failure. |
| 6054 | */ |
| 6055 | int |
| 6056 | xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) { |
| 6057 | xmlOutputBufferPtr buf; |
| 6058 | xmlCharEncodingHandlerPtr handler = NULL; |
| 6059 | int ret; |
| 6060 | |
| 6061 | if (encoding != NULL) { |
| 6062 | xmlCharEncoding enc; |
| 6063 | |
| 6064 | enc = xmlParseCharEncoding(encoding); |
| 6065 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
| 6066 | xmlGenericError(xmlGenericErrorContext, |
| 6067 | "xmlSaveFileEnc: document not in UTF8\n"); |
| 6068 | return(-1); |
| 6069 | } |
| 6070 | if (enc != XML_CHAR_ENCODING_UTF8) { |
| 6071 | handler = xmlFindCharEncodingHandler(encoding); |
| 6072 | if (handler == NULL) { |
| 6073 | return(-1); |
| 6074 | } |
| 6075 | } |
| 6076 | } |
| 6077 | |
| 6078 | /* |
| 6079 | * save the content to a temp buffer. |
| 6080 | */ |
| 6081 | buf = xmlOutputBufferCreateFilename(filename, handler, 0); |
| 6082 | if (buf == NULL) return(-1); |
| 6083 | |
| 6084 | xmlDocContentDumpOutput(buf, cur, encoding, 1); |
| 6085 | |
| 6086 | ret = xmlOutputBufferClose(buf); |
| 6087 | return(ret); |
| 6088 | } |
| 6089 | |
| 6090 | /** |
| 6091 | * xmlSaveFile: |
| 6092 | * @filename: the filename (or URL) |
| 6093 | * @cur: the document |
| 6094 | * |
| 6095 | * Dump an XML document to a file. Will use compression if |
| 6096 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 6097 | * used. |
| 6098 | * returns: the number of byte written or -1 in case of failure. |
| 6099 | */ |
| 6100 | int |
| 6101 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
| 6102 | xmlOutputBufferPtr buf; |
| 6103 | const char *encoding; |
| 6104 | xmlCharEncodingHandlerPtr handler = NULL; |
| 6105 | int ret; |
| 6106 | |
| 6107 | if (cur == NULL) |
| 6108 | return(-1); |
| 6109 | encoding = (const char *) cur->encoding; |
| 6110 | |
| 6111 | /* |
| 6112 | * save the content to a temp buffer. |
| 6113 | */ |
| 6114 | #ifdef HAVE_ZLIB_H |
| 6115 | if (cur->compression < 0) cur->compression = xmlCompressMode; |
| 6116 | #endif |
| 6117 | if (encoding != NULL) { |
| 6118 | xmlCharEncoding enc; |
| 6119 | |
| 6120 | enc = xmlParseCharEncoding(encoding); |
| 6121 | |
| 6122 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
| 6123 | xmlGenericError(xmlGenericErrorContext, |
| 6124 | "xmlSaveFile: document not in UTF8\n"); |
| 6125 | return(-1); |
| 6126 | } |
| 6127 | if (enc != XML_CHAR_ENCODING_UTF8) { |
| 6128 | handler = xmlFindCharEncodingHandler(encoding); |
| 6129 | if (handler == NULL) { |
| 6130 | xmlFree((char *) cur->encoding); |
| 6131 | cur->encoding = NULL; |
| 6132 | } |
| 6133 | } |
| 6134 | } |
| 6135 | |
| 6136 | buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression); |
| 6137 | if (buf == NULL) return(-1); |
| 6138 | |
| 6139 | xmlDocContentDumpOutput(buf, cur, NULL, 1); |
| 6140 | |
| 6141 | ret = xmlOutputBufferClose(buf); |
| 6142 | return(ret); |
| 6143 | } |
| 6144 | |