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