Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +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 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 6 | * TODO Cleanup the Dump mechanism. |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 7 | * |
| 8 | * Daniel.Veillard@w3.org |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 11 | #include "config.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <ctype.h> |
Seth Alves | e7f12e6 | 1998-10-01 20:51:15 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 15 | #include <string.h> /* for memset() only ! */ |
| 16 | |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 17 | #ifdef HAVE_ZLIB_H |
| 18 | #include <zlib.h> |
| 19 | #endif |
| 20 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 21 | #include "tree.h" |
| 22 | #include "entities.h" |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 23 | #include "valid.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 24 | |
| 25 | static CHAR xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
| 26 | int oldXMLWDcompatibility = 0; |
| 27 | int xmlIndentTreeOutput = 1; |
| 28 | |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 29 | static int xmlCompressMode = 0; |
| 30 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 31 | #define UPDATE_LAST_CHILD(n) if ((n) != NULL) { \ |
| 32 | xmlNodePtr ulccur = (n)->childs; \ |
| 33 | if (ulccur == NULL) { \ |
| 34 | (n)->last = NULL; \ |
| 35 | } else { \ |
| 36 | while (ulccur->next != NULL) ulccur = ulccur->next; \ |
| 37 | (n)->last = ulccur; \ |
| 38 | } } |
| 39 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 40 | /************************************************************************ |
| 41 | * * |
| 42 | * Allocation and deallocation of basic structures * |
| 43 | * * |
| 44 | ************************************************************************/ |
| 45 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 46 | /** |
| 47 | * xmlUpgradeOldNs: |
| 48 | * @doc: a document pointer |
| 49 | * |
| 50 | * Upgrade old style Namespaces (PI) and move them to the root of the document. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 51 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 52 | void |
| 53 | xmlUpgradeOldNs(xmlDocPtr doc) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 54 | xmlNsPtr cur; |
| 55 | |
| 56 | if ((doc == NULL) || (doc->oldNs == NULL)) return; |
| 57 | if (doc->root == NULL) { |
| 58 | fprintf(stderr, "xmlUpgradeOldNs: failed no root !\n"); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | cur = doc->oldNs; |
| 63 | while (cur->next != NULL) { |
| 64 | cur->type = XML_LOCAL_NAMESPACE; |
| 65 | cur = cur->next; |
| 66 | } |
| 67 | cur->type = XML_LOCAL_NAMESPACE; |
| 68 | cur->next = doc->root->nsDef; |
| 69 | doc->root->nsDef = doc->oldNs; |
| 70 | doc->oldNs = NULL; |
| 71 | } |
| 72 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 73 | /** |
| 74 | * xmlNewNs: |
| 75 | * @node: the element carrying the namespace |
| 76 | * @href: the URI associated |
| 77 | * @prefix: the prefix for the namespace |
| 78 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 79 | * Creation of a new Namespace. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 80 | * return values: returns a new namespace pointer |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 81 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 82 | xmlNsPtr |
| 83 | xmlNewNs(xmlNodePtr node, const CHAR *href, const CHAR *prefix) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 84 | xmlNsPtr cur; |
| 85 | |
| 86 | if (href == NULL) { |
| 87 | fprintf(stderr, "xmlNewNs: href == NULL !\n"); |
| 88 | return(NULL); |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Allocate a new DTD and fill the fields. |
| 93 | */ |
| 94 | cur = (xmlNsPtr) malloc(sizeof(xmlNs)); |
| 95 | if (cur == NULL) { |
| 96 | fprintf(stderr, "xmlNewNs : malloc failed\n"); |
| 97 | return(NULL); |
| 98 | } |
| 99 | |
| 100 | cur->type = XML_LOCAL_NAMESPACE; |
| 101 | if (href != NULL) |
| 102 | cur->href = xmlStrdup(href); |
| 103 | else |
| 104 | cur->href = NULL; |
| 105 | if (prefix != NULL) |
| 106 | cur->prefix = xmlStrdup(prefix); |
| 107 | else |
| 108 | cur->prefix = NULL; |
| 109 | |
| 110 | /* |
| 111 | * Add it at the end to preserve parsing order ... |
| 112 | */ |
| 113 | cur->next = NULL; |
| 114 | if (node != NULL) { |
| 115 | if (node->nsDef == NULL) { |
| 116 | node->nsDef = cur; |
| 117 | } else { |
| 118 | xmlNsPtr prev = node->nsDef; |
| 119 | |
| 120 | while (prev->next != NULL) prev = prev->next; |
| 121 | prev->next = cur; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return(cur); |
| 126 | } |
| 127 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 128 | /** |
| 129 | * xmlNewGlobalNs: |
| 130 | * @doc: the document carrying the namespace |
| 131 | * @href: the URI associated |
| 132 | * @prefix: the prefix for the namespace |
| 133 | * |
| 134 | * Creation of a Namespace, the old way using PI and without scoping, to AVOID. |
| 135 | * return values: returns a new namespace pointer |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 136 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 137 | xmlNsPtr |
| 138 | xmlNewGlobalNs(xmlDocPtr doc, const CHAR *href, const CHAR *prefix) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 139 | xmlNsPtr cur; |
| 140 | |
| 141 | /* |
| 142 | * Allocate a new DTD and fill the fields. |
| 143 | */ |
| 144 | cur = (xmlNsPtr) malloc(sizeof(xmlNs)); |
| 145 | if (cur == NULL) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 146 | fprintf(stderr, "xmlNewGlobalNs : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 147 | return(NULL); |
| 148 | } |
| 149 | |
| 150 | cur->type = XML_GLOBAL_NAMESPACE; |
| 151 | if (href != NULL) |
| 152 | cur->href = xmlStrdup(href); |
| 153 | else |
| 154 | cur->href = NULL; |
| 155 | if (prefix != NULL) |
| 156 | cur->prefix = xmlStrdup(prefix); |
| 157 | else |
| 158 | cur->prefix = NULL; |
| 159 | |
| 160 | /* |
| 161 | * Add it at the end to preserve parsing order ... |
| 162 | */ |
| 163 | cur->next = NULL; |
| 164 | if (doc != NULL) { |
| 165 | if (doc->oldNs == NULL) { |
| 166 | doc->oldNs = cur; |
| 167 | } else { |
| 168 | xmlNsPtr prev = doc->oldNs; |
| 169 | |
| 170 | while (prev->next != NULL) prev = prev->next; |
| 171 | prev->next = cur; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return(cur); |
| 176 | } |
| 177 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 178 | /** |
| 179 | * xmlSetNs: |
| 180 | * @node: a node in the document |
| 181 | * @ns: a namespace pointer |
| 182 | * |
| 183 | * Associate a namespace to a node, a posteriori. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 184 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 185 | void |
| 186 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 187 | if (node == NULL) { |
| 188 | fprintf(stderr, "xmlSetNs: node == NULL\n"); |
| 189 | return; |
| 190 | } |
| 191 | node->ns = ns; |
| 192 | } |
| 193 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 194 | /** |
| 195 | * xmlFreeNs: |
| 196 | * @cur: the namespace pointer |
| 197 | * |
| 198 | * Free up the structures associated to a namespace |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 199 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 200 | void |
| 201 | xmlFreeNs(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 202 | if (cur == NULL) { |
| 203 | fprintf(stderr, "xmlFreeNs : ns == NULL\n"); |
| 204 | return; |
| 205 | } |
| 206 | if (cur->href != NULL) free((char *) cur->href); |
| 207 | if (cur->prefix != NULL) free((char *) cur->prefix); |
| 208 | memset(cur, -1, sizeof(xmlNs)); |
| 209 | free(cur); |
| 210 | } |
| 211 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 212 | /** |
| 213 | * xmlFreeNsList: |
| 214 | * @cur: the first namespace pointer |
| 215 | * |
| 216 | * Free up all the structures associated to the chained namespaces. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 217 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 218 | void |
| 219 | xmlFreeNsList(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 220 | xmlNsPtr next; |
| 221 | if (cur == NULL) { |
| 222 | fprintf(stderr, "xmlFreeNsList : ns == NULL\n"); |
| 223 | return; |
| 224 | } |
| 225 | while (cur != NULL) { |
| 226 | next = cur->next; |
| 227 | xmlFreeNs(cur); |
| 228 | cur = next; |
| 229 | } |
| 230 | } |
| 231 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 232 | /** |
| 233 | * xmlNewDtd: |
| 234 | * @doc: the document pointer |
| 235 | * @name: the DTD name |
| 236 | * @ExternalID: the external ID |
| 237 | * @SystemID: the system ID |
| 238 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 239 | * Creation of a new DTD. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 240 | * return values: a pointer to the new DTD structure |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 241 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 242 | xmlDtdPtr |
| 243 | xmlNewDtd(xmlDocPtr doc, const CHAR *name, |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 244 | const CHAR *ExternalID, const CHAR *SystemID) { |
| 245 | xmlDtdPtr cur; |
| 246 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 247 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 248 | fprintf(stderr, "xmlNewDtd(%s): document %s already have a DTD %s\n", |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 249 | /* !!! */ (char *) name, doc->name, |
| 250 | /* !!! */ (char *)doc->extSubset->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Allocate a new DTD and fill the fields. |
| 255 | */ |
| 256 | cur = (xmlDtdPtr) malloc(sizeof(xmlDtd)); |
| 257 | if (cur == NULL) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 258 | fprintf(stderr, "xmlNewDtd : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 259 | return(NULL); |
| 260 | } |
| 261 | |
| 262 | if (name != NULL) |
| 263 | cur->name = xmlStrdup(name); |
| 264 | else |
| 265 | cur->name = NULL; |
| 266 | if (ExternalID != NULL) |
| 267 | cur->ExternalID = xmlStrdup(ExternalID); |
| 268 | else |
| 269 | cur->ExternalID = NULL; |
| 270 | if (SystemID != NULL) |
| 271 | cur->SystemID = xmlStrdup(SystemID); |
| 272 | else |
| 273 | cur->SystemID = NULL; |
| 274 | cur->elements = NULL; |
| 275 | cur->entities = NULL; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 276 | if (doc != NULL) |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 277 | doc->extSubset = cur; |
| 278 | |
| 279 | return(cur); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * xmlCreateIntSubset: |
| 284 | * @doc: the document pointer |
| 285 | * @name: the DTD name |
| 286 | * @ExternalID: the external ID |
| 287 | * @SystemID: the system ID |
| 288 | * |
| 289 | * Creatte the internal subset of a document |
| 290 | * return values: a pointer to the new DTD structure |
| 291 | */ |
| 292 | xmlDtdPtr |
| 293 | xmlCreateIntSubset(xmlDocPtr doc, const CHAR *name, |
| 294 | const CHAR *ExternalID, const CHAR *SystemID) { |
| 295 | xmlDtdPtr cur; |
| 296 | |
| 297 | if ((doc != NULL) && (doc->intSubset != NULL)) { |
| 298 | fprintf(stderr, |
| 299 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 300 | doc->name); |
| 301 | return(NULL); |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Allocate a new DTD and fill the fields. |
| 306 | */ |
| 307 | cur = (xmlDtdPtr) malloc(sizeof(xmlDtd)); |
| 308 | if (cur == NULL) { |
| 309 | fprintf(stderr, "xmlNewDtd : malloc failed\n"); |
| 310 | return(NULL); |
| 311 | } |
| 312 | |
| 313 | if (name != NULL) |
| 314 | cur->name = xmlStrdup(name); |
| 315 | else |
| 316 | cur->name = NULL; |
| 317 | if (ExternalID != NULL) |
| 318 | cur->ExternalID = xmlStrdup(ExternalID); |
| 319 | else |
| 320 | cur->ExternalID = NULL; |
| 321 | if (SystemID != NULL) |
| 322 | cur->SystemID = xmlStrdup(SystemID); |
| 323 | else |
| 324 | cur->SystemID = NULL; |
| 325 | cur->elements = NULL; |
| 326 | cur->entities = NULL; |
| 327 | if (doc != NULL) |
| 328 | doc->intSubset = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 329 | |
| 330 | return(cur); |
| 331 | } |
| 332 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 333 | /** |
| 334 | * xmlFreeDtd: |
| 335 | * @cur: the DTD structure to free up |
| 336 | * |
| 337 | * Free a DTD structure. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 338 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 339 | void |
| 340 | xmlFreeDtd(xmlDtdPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 341 | if (cur == NULL) { |
| 342 | fprintf(stderr, "xmlFreeDtd : DTD == NULL\n"); |
| 343 | return; |
| 344 | } |
| 345 | if (cur->name != NULL) free((char *) cur->name); |
| 346 | if (cur->SystemID != NULL) free((char *) cur->SystemID); |
| 347 | if (cur->ExternalID != NULL) free((char *) cur->ExternalID); |
| 348 | if (cur->elements != NULL) |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 349 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 350 | if (cur->entities != NULL) |
| 351 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
| 352 | memset(cur, -1, sizeof(xmlDtd)); |
| 353 | free(cur); |
| 354 | } |
| 355 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 356 | /** |
| 357 | * xmlNewDoc: |
| 358 | * @version: CHAR string giving the version of XML "1.0" |
| 359 | * |
| 360 | * Create a new document |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 361 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 362 | xmlDocPtr |
| 363 | xmlNewDoc(const CHAR *version) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 364 | xmlDocPtr cur; |
| 365 | |
| 366 | if (version == NULL) { |
| 367 | fprintf(stderr, "xmlNewDoc : version == NULL\n"); |
| 368 | return(NULL); |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Allocate a new document and fill the fields. |
| 373 | */ |
| 374 | cur = (xmlDocPtr) malloc(sizeof(xmlDoc)); |
| 375 | if (cur == NULL) { |
| 376 | fprintf(stderr, "xmlNewDoc : malloc failed\n"); |
| 377 | return(NULL); |
| 378 | } |
| 379 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 380 | cur->type = XML_DOCUMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 381 | cur->version = xmlStrdup(version); |
| 382 | cur->name = NULL; |
| 383 | cur->root = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 384 | cur->intSubset = NULL; |
| 385 | cur->extSubset = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 386 | cur->oldNs = NULL; |
| 387 | cur->encoding = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 388 | cur->standalone = -1; |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 389 | cur->compression = xmlCompressMode; |
Daniel Veillard | 27fb075 | 1998-10-17 06:47:46 +0000 | [diff] [blame] | 390 | #ifndef WITHOUT_CORBA |
| 391 | cur->_private = NULL; |
| 392 | cur->vepv = NULL; |
| 393 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 394 | return(cur); |
| 395 | } |
| 396 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 397 | /** |
| 398 | * xmlFreeDoc: |
| 399 | * @cur: pointer to the document |
| 400 | * @: |
| 401 | * |
| 402 | * Free up all the structures used by a document, tree included. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 403 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 404 | void |
| 405 | xmlFreeDoc(xmlDocPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 406 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 407 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 408 | fprintf(stderr, "xmlFreeDoc : document == NULL\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 409 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 410 | return; |
| 411 | } |
| 412 | free((char *) cur->version); |
| 413 | if (cur->name != NULL) free((char *) cur->name); |
| 414 | if (cur->encoding != NULL) free((char *) cur->encoding); |
| 415 | if (cur->root != NULL) xmlFreeNode(cur->root); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 416 | if (cur->intSubset != NULL) xmlFreeDtd(cur->intSubset); |
| 417 | if (cur->extSubset != NULL) xmlFreeDtd(cur->extSubset); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 418 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 419 | memset(cur, -1, sizeof(xmlDoc)); |
| 420 | free(cur); |
| 421 | } |
| 422 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 423 | /** |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 424 | * xmlStringLenGetNodeList: |
| 425 | * @doc: the document |
| 426 | * @value: the value of the text |
| 427 | * @int len: the length of the string value |
| 428 | * |
| 429 | * Parse the value string and build the node list associated. Should |
| 430 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 431 | * return values: a pointer to the first child |
| 432 | */ |
| 433 | xmlNodePtr |
| 434 | xmlStringLenGetNodeList(xmlDocPtr doc, const CHAR *value, int len) { |
| 435 | xmlNodePtr ret = NULL, last = NULL; |
| 436 | xmlNodePtr node; |
| 437 | CHAR *val; |
| 438 | const CHAR *cur = value; |
| 439 | const CHAR *q; |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 440 | xmlEntityPtr ent; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 441 | |
| 442 | if (value == NULL) return(NULL); |
| 443 | |
| 444 | q = cur; |
| 445 | while ((*cur != 0) && (cur - value < len)) { |
| 446 | if (*cur == '&') { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 447 | /* |
| 448 | * Save the current text. |
| 449 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 450 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 451 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 452 | xmlNodeAddContentLen(last, q, cur - q); |
| 453 | } else { |
| 454 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 455 | if (node == NULL) return(ret); |
| 456 | if (last == NULL) |
| 457 | last = ret = node; |
| 458 | else { |
| 459 | last->next = node; |
| 460 | node->prev = last; |
| 461 | last = node; |
| 462 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 465 | /* |
| 466 | * Read the entity string |
| 467 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 468 | cur++; |
| 469 | q = cur; |
| 470 | while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++; |
| 471 | if ((*cur == 0) || (cur - value >= len)) { |
| 472 | fprintf(stderr, |
| 473 | "xmlStringGetNodeList: unterminated entity %30s\n", q); |
| 474 | return(ret); |
| 475 | } |
| 476 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 477 | /* |
| 478 | * Predefined entities don't generate nodes |
| 479 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 480 | val = xmlStrndup(q, cur - q); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 481 | ent = xmlGetDocEntity(doc, val); |
| 482 | if ((ent != NULL) && |
| 483 | (ent->type == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 484 | if (last == NULL) { |
| 485 | node = xmlNewDocText(doc, ent->content); |
| 486 | last = ret = node; |
| 487 | } else |
| 488 | xmlNodeAddContent(last, ent->content); |
| 489 | |
| 490 | } else { |
| 491 | /* |
| 492 | * Create a new REFERENCE_REF node |
| 493 | */ |
| 494 | node = xmlNewReference(doc, val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 495 | if (node == NULL) { |
| 496 | if (val != NULL) free(val); |
| 497 | return(ret); |
| 498 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 499 | if (last == NULL) |
| 500 | last = ret = node; |
| 501 | else { |
| 502 | last->next = node; |
| 503 | node->prev = last; |
| 504 | last = node; |
| 505 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 506 | } |
| 507 | free(val); |
| 508 | } |
| 509 | cur++; |
| 510 | q = cur; |
| 511 | } else |
| 512 | cur++; |
| 513 | } |
| 514 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 515 | /* |
| 516 | * Handle the last piece of text. |
| 517 | */ |
| 518 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 519 | xmlNodeAddContentLen(last, q, cur - q); |
| 520 | } else { |
| 521 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 522 | if (node == NULL) return(ret); |
| 523 | if (last == NULL) |
| 524 | last = ret = node; |
| 525 | else { |
| 526 | last->next = node; |
| 527 | node->prev = last; |
| 528 | last = node; |
| 529 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | return(ret); |
| 533 | } |
| 534 | |
| 535 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 536 | * xmlStringGetNodeList: |
| 537 | * @doc: the document |
| 538 | * @value: the value of the attribute |
| 539 | * |
| 540 | * Parse the value string and build the node list associated. Should |
| 541 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 542 | * return values: a pointer to the first child |
| 543 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 544 | xmlNodePtr |
| 545 | xmlStringGetNodeList(xmlDocPtr doc, const CHAR *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 546 | xmlNodePtr ret = NULL, last = NULL; |
| 547 | xmlNodePtr node; |
| 548 | CHAR *val; |
| 549 | const CHAR *cur = value; |
| 550 | const CHAR *q; |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 551 | xmlEntityPtr ent; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 552 | |
| 553 | if (value == NULL) return(NULL); |
| 554 | |
| 555 | q = cur; |
| 556 | while (*cur != 0) { |
| 557 | if (*cur == '&') { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 558 | /* |
| 559 | * Save the current text. |
| 560 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 561 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 562 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 563 | xmlNodeAddContentLen(last, q, cur - q); |
| 564 | } else { |
| 565 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 566 | if (node == NULL) return(ret); |
| 567 | if (last == NULL) |
| 568 | last = ret = node; |
| 569 | else { |
| 570 | last->next = node; |
| 571 | node->prev = last; |
| 572 | last = node; |
| 573 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 576 | /* |
| 577 | * Read the entity string |
| 578 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 579 | cur++; |
| 580 | q = cur; |
| 581 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 582 | if (*cur == 0) { |
| 583 | fprintf(stderr, |
| 584 | "xmlStringGetNodeList: unterminated entity %30s\n", q); |
| 585 | return(ret); |
| 586 | } |
| 587 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 588 | /* |
| 589 | * Predefined entities don't generate nodes |
| 590 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 591 | val = xmlStrndup(q, cur - q); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 592 | ent = xmlGetDocEntity(doc, val); |
| 593 | if ((ent != NULL) && |
| 594 | (ent->type == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 595 | if (last == NULL) { |
| 596 | node = xmlNewDocText(doc, ent->content); |
| 597 | last = ret = node; |
| 598 | } else |
| 599 | xmlNodeAddContent(last, ent->content); |
| 600 | |
| 601 | } else { |
| 602 | /* |
| 603 | * Create a new REFERENCE_REF node |
| 604 | */ |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 605 | node = xmlNewReference(doc, val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 606 | if (node == NULL) { |
| 607 | if (val != NULL) free(val); |
| 608 | return(ret); |
| 609 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 610 | if (last == NULL) |
| 611 | last = ret = node; |
| 612 | else { |
| 613 | last->next = node; |
| 614 | node->prev = last; |
| 615 | last = node; |
| 616 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 617 | } |
| 618 | free(val); |
| 619 | } |
| 620 | cur++; |
| 621 | q = cur; |
| 622 | } else |
| 623 | cur++; |
| 624 | } |
| 625 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 626 | /* |
| 627 | * Handle the last piece of text. |
| 628 | */ |
| 629 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 630 | xmlNodeAddContentLen(last, q, cur - q); |
| 631 | } else { |
| 632 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 633 | if (node == NULL) return(ret); |
| 634 | if (last == NULL) |
| 635 | last = ret = node; |
| 636 | else { |
| 637 | last->next = node; |
| 638 | node->prev = last; |
| 639 | last = node; |
| 640 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | return(ret); |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * xmlNodeListGetString: |
| 648 | * @doc: the document |
| 649 | * @list: a Node list |
| 650 | * @inLine: should we replace entity contents or show their external form |
| 651 | * |
| 652 | * Returns the string equivalent to the text contained in the Node list |
| 653 | * made of TEXTs and ENTITY_REFs |
| 654 | * return values: a pointer to the string copy, the calller must free it. |
| 655 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 656 | CHAR * |
| 657 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 658 | xmlNodePtr node = list; |
| 659 | CHAR *ret = NULL; |
| 660 | xmlEntityPtr ent; |
| 661 | |
| 662 | if (list == NULL) return(NULL); |
| 663 | |
| 664 | while (node != NULL) { |
| 665 | if (node->type == XML_TEXT_NODE) { |
| 666 | if (inLine) |
| 667 | ret = xmlStrcat(ret, node->content); |
| 668 | else |
| 669 | ret = xmlStrcat(ret, xmlEncodeEntities(doc, node->content)); |
| 670 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 671 | if (inLine) { |
| 672 | ent = xmlGetDocEntity(doc, node->name); |
| 673 | if (ent != NULL) |
| 674 | ret = xmlStrcat(ret, ent->content); |
| 675 | else |
| 676 | ret = xmlStrcat(ret, node->content); |
| 677 | } else { |
| 678 | CHAR buf[2]; |
| 679 | buf[0] = '&'; buf[1] = 0; |
| 680 | ret = xmlStrncat(ret, buf, 1); |
| 681 | ret = xmlStrcat(ret, node->name); |
| 682 | buf[0] = ';'; buf[1] = 0; |
| 683 | ret = xmlStrncat(ret, buf, 1); |
| 684 | } |
| 685 | } |
| 686 | #if 0 |
| 687 | else { |
| 688 | fprintf(stderr, "xmlGetNodeListString : invalide node type %d\n", |
| 689 | node->type); |
| 690 | } |
| 691 | #endif |
| 692 | node = node->next; |
| 693 | } |
| 694 | return(ret); |
| 695 | } |
| 696 | |
| 697 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 698 | * xmlNewProp: |
| 699 | * @node: the holding node |
| 700 | * @name: the name of the attribute |
| 701 | * @value: the value of the attribute |
| 702 | * |
| 703 | * Create a new property carried by a node. |
| 704 | * return values: a pointer to the attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 705 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 706 | xmlAttrPtr |
| 707 | xmlNewProp(xmlNodePtr node, const CHAR *name, const CHAR *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 708 | xmlAttrPtr cur; |
| 709 | |
| 710 | if (name == NULL) { |
| 711 | fprintf(stderr, "xmlNewProp : name == NULL\n"); |
| 712 | return(NULL); |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Allocate a new property and fill the fields. |
| 717 | */ |
| 718 | cur = (xmlAttrPtr) malloc(sizeof(xmlAttr)); |
| 719 | if (cur == NULL) { |
| 720 | fprintf(stderr, "xmlNewProp : malloc failed\n"); |
| 721 | return(NULL); |
| 722 | } |
| 723 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 724 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 725 | cur->node = node; |
| 726 | cur->name = xmlStrdup(name); |
| 727 | if (value != NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 728 | cur->val = xmlStringGetNodeList(node->doc, value); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 729 | else |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 730 | cur->val = NULL; |
Daniel Veillard | 27fb075 | 1998-10-17 06:47:46 +0000 | [diff] [blame] | 731 | #ifndef WITHOUT_CORBA |
| 732 | cur->_private = NULL; |
| 733 | cur->vepv = NULL; |
| 734 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 735 | |
| 736 | /* |
| 737 | * Add it at the end to preserve parsing order ... |
| 738 | */ |
| 739 | cur->next = NULL; |
| 740 | if (node != NULL) { |
| 741 | if (node->properties == NULL) { |
| 742 | node->properties = cur; |
| 743 | } else { |
| 744 | xmlAttrPtr prev = node->properties; |
| 745 | |
| 746 | while (prev->next != NULL) prev = prev->next; |
| 747 | prev->next = cur; |
| 748 | } |
| 749 | } |
| 750 | return(cur); |
| 751 | } |
| 752 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 753 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 754 | * xmlNewDocProp: |
| 755 | * @doc: the document |
| 756 | * @name: the name of the attribute |
| 757 | * @value: the value of the attribute |
| 758 | * |
| 759 | * Create a new property carried by a document. |
| 760 | * return values: a pointer to the attribute |
| 761 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 762 | xmlAttrPtr |
| 763 | xmlNewDocProp(xmlDocPtr doc, const CHAR *name, const CHAR *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 764 | xmlAttrPtr cur; |
| 765 | |
| 766 | if (name == NULL) { |
| 767 | fprintf(stderr, "xmlNewProp : name == NULL\n"); |
| 768 | return(NULL); |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | * Allocate a new property and fill the fields. |
| 773 | */ |
| 774 | cur = (xmlAttrPtr) malloc(sizeof(xmlAttr)); |
| 775 | if (cur == NULL) { |
| 776 | fprintf(stderr, "xmlNewProp : malloc failed\n"); |
| 777 | return(NULL); |
| 778 | } |
| 779 | |
| 780 | cur->type = XML_ATTRIBUTE_NODE; |
| 781 | cur->node = NULL; |
| 782 | cur->name = xmlStrdup(name); |
| 783 | if (value != NULL) |
| 784 | cur->val = xmlStringGetNodeList(doc, value); |
| 785 | else |
| 786 | cur->val = NULL; |
| 787 | #ifndef WITHOUT_CORBA |
| 788 | cur->_private = NULL; |
| 789 | cur->vepv = NULL; |
| 790 | #endif |
| 791 | |
| 792 | cur->next = NULL; |
| 793 | return(cur); |
| 794 | } |
| 795 | |
| 796 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 797 | * xmlFreePropList: |
| 798 | * @cur: the first property in the list |
| 799 | * |
| 800 | * Free a property and all its siblings, all the childs are freed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 801 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 802 | void |
| 803 | xmlFreePropList(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 804 | xmlAttrPtr next; |
| 805 | if (cur == NULL) { |
| 806 | fprintf(stderr, "xmlFreePropList : property == NULL\n"); |
| 807 | return; |
| 808 | } |
| 809 | while (cur != NULL) { |
| 810 | next = cur->next; |
| 811 | xmlFreeProp(cur); |
| 812 | cur = next; |
| 813 | } |
| 814 | } |
| 815 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 816 | /** |
| 817 | * xmlFreeProp: |
| 818 | * @cur: the first property in the list |
| 819 | * |
| 820 | * Free one property, all the childs are freed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 821 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 822 | void |
| 823 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 824 | if (cur == NULL) { |
| 825 | fprintf(stderr, "xmlFreeProp : property == NULL\n"); |
| 826 | return; |
| 827 | } |
| 828 | if (cur->name != NULL) free((char *) cur->name); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 829 | if (cur->val != NULL) xmlFreeNodeList(cur->val); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 830 | memset(cur, -1, sizeof(xmlAttr)); |
| 831 | free(cur); |
| 832 | } |
| 833 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 834 | /** |
| 835 | * xmlNewNode: |
| 836 | * @ns: namespace if any |
| 837 | * @name: the node name |
| 838 | * @content: the text content if any |
| 839 | * |
| 840 | * Creation of a new node element. @ns and @content are optionnal (NULL). |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 841 | * If content is non NULL, a child list containing the TEXTs and |
| 842 | * ENTITY_REFs node will be created. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 843 | * return values: a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 844 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 845 | xmlNodePtr |
| 846 | xmlNewNode(xmlNsPtr ns, const CHAR *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 847 | xmlNodePtr cur; |
| 848 | |
| 849 | if (name == NULL) { |
| 850 | fprintf(stderr, "xmlNewNode : name == NULL\n"); |
| 851 | return(NULL); |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | * Allocate a new node and fill the fields. |
| 856 | */ |
| 857 | cur = (xmlNodePtr) malloc(sizeof(xmlNode)); |
| 858 | if (cur == NULL) { |
| 859 | fprintf(stderr, "xmlNewNode : malloc failed\n"); |
| 860 | return(NULL); |
| 861 | } |
| 862 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 863 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 864 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 865 | cur->parent = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 866 | cur->next = NULL; |
| 867 | cur->prev = NULL; |
| 868 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 869 | cur->last = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 870 | cur->properties = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 871 | cur->name = xmlStrdup(name); |
| 872 | cur->ns = ns; |
| 873 | cur->nsDef = NULL; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 874 | cur->content = NULL; |
Daniel Veillard | 27fb075 | 1998-10-17 06:47:46 +0000 | [diff] [blame] | 875 | #ifndef WITHOUT_CORBA |
| 876 | cur->_private = NULL; |
| 877 | cur->vepv = NULL; |
| 878 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 879 | return(cur); |
| 880 | } |
| 881 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 882 | /** |
| 883 | * xmlNewDocNode: |
| 884 | * @doc: the document |
| 885 | * @ns: namespace if any |
| 886 | * @name: the node name |
| 887 | * @content: the text content if any |
| 888 | * |
| 889 | * Creation of a new node element within a document. @ns and @content |
| 890 | * are optionnal (NULL). |
| 891 | * return values: a pointer to the new node object. |
| 892 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 893 | xmlNodePtr |
| 894 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 895 | const CHAR *name, CHAR *content) { |
| 896 | xmlNodePtr cur; |
| 897 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 898 | cur = xmlNewNode(ns, name); |
| 899 | if (cur != NULL) { |
| 900 | cur->doc = doc; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 901 | if (content != NULL) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 902 | cur->childs = xmlStringGetNodeList(doc, content); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 903 | UPDATE_LAST_CHILD(cur); |
| 904 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 905 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 906 | return(cur); |
| 907 | } |
| 908 | |
| 909 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 910 | /** |
| 911 | * xmlNewText: |
| 912 | * @content: the text content |
| 913 | * |
| 914 | * Creation of a new text node. |
| 915 | * return values: a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 916 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 917 | xmlNodePtr |
| 918 | xmlNewText(const CHAR *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 919 | xmlNodePtr cur; |
| 920 | |
| 921 | /* |
| 922 | * Allocate a new node and fill the fields. |
| 923 | */ |
| 924 | cur = (xmlNodePtr) malloc(sizeof(xmlNode)); |
| 925 | if (cur == NULL) { |
| 926 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 927 | return(NULL); |
| 928 | } |
| 929 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 930 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 931 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 932 | cur->parent = NULL; |
| 933 | cur->next = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 934 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 935 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 936 | cur->last = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 937 | cur->properties = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 938 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 939 | cur->name = xmlStrdup(xmlStringText); |
| 940 | cur->ns = NULL; |
| 941 | cur->nsDef = NULL; |
| 942 | if (content != NULL) |
| 943 | cur->content = xmlStrdup(content); |
| 944 | else |
| 945 | cur->content = NULL; |
| 946 | return(cur); |
| 947 | } |
| 948 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 949 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 950 | * xmlNewReference: |
| 951 | * @doc: the document |
| 952 | * @name: the reference name, or the reference string with & and ; |
| 953 | * |
| 954 | * Creation of a new reference node. |
| 955 | * return values: a pointer to the new node object. |
| 956 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 957 | xmlNodePtr |
| 958 | xmlNewReference(xmlDocPtr doc, const CHAR *name) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 959 | xmlNodePtr cur; |
| 960 | xmlEntityPtr ent; |
| 961 | |
| 962 | /* |
| 963 | * Allocate a new node and fill the fields. |
| 964 | */ |
| 965 | cur = (xmlNodePtr) malloc(sizeof(xmlNode)); |
| 966 | if (cur == NULL) { |
| 967 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 968 | return(NULL); |
| 969 | } |
| 970 | |
| 971 | cur->type = XML_ENTITY_REF_NODE; |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 972 | cur->doc = doc; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 973 | cur->parent = NULL; |
| 974 | cur->next = NULL; |
| 975 | cur->prev = NULL; |
| 976 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 977 | cur->last = NULL; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 978 | cur->properties = NULL; |
| 979 | if (name[0] == '&') { |
| 980 | int len; |
| 981 | name++; |
| 982 | len = xmlStrlen(name); |
| 983 | if (name[len - 1] == ';') |
| 984 | cur->name = xmlStrndup(name, len - 1); |
| 985 | else |
| 986 | cur->name = xmlStrndup(name, len); |
| 987 | } else |
| 988 | cur->name = xmlStrdup(name); |
| 989 | cur->ns = NULL; |
| 990 | cur->nsDef = NULL; |
| 991 | |
| 992 | ent = xmlGetDocEntity(doc, cur->name); |
| 993 | if (ent != NULL) |
| 994 | cur->content = ent->content; |
| 995 | else |
| 996 | cur->content = NULL; |
| 997 | return(cur); |
| 998 | } |
| 999 | |
| 1000 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1001 | * xmlNewDocText: |
| 1002 | * @doc: the document |
| 1003 | * @content: the text content |
| 1004 | * |
| 1005 | * Creation of a new text node within a document. |
| 1006 | * return values: a pointer to the new node object. |
| 1007 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1008 | xmlNodePtr |
| 1009 | xmlNewDocText(xmlDocPtr doc, const CHAR *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1010 | xmlNodePtr cur; |
| 1011 | |
| 1012 | cur = xmlNewText(content); |
| 1013 | if (cur != NULL) cur->doc = doc; |
| 1014 | return(cur); |
| 1015 | } |
| 1016 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1017 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1018 | * xmlNewTextLen: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1019 | * @content: the text content |
| 1020 | * @len: the text len. |
| 1021 | * |
| 1022 | * Creation of a new text node with an extra parameter for the content's lenght |
| 1023 | * return values: a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1024 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1025 | xmlNodePtr |
| 1026 | xmlNewTextLen(const CHAR *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1027 | xmlNodePtr cur; |
| 1028 | |
| 1029 | /* |
| 1030 | * Allocate a new node and fill the fields. |
| 1031 | */ |
| 1032 | cur = (xmlNodePtr) malloc(sizeof(xmlNode)); |
| 1033 | if (cur == NULL) { |
| 1034 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 1035 | return(NULL); |
| 1036 | } |
| 1037 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1038 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1039 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1040 | cur->parent = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1041 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1042 | cur->next = NULL; |
| 1043 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1044 | cur->last = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1045 | cur->properties = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1046 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1047 | cur->name = xmlStrdup(xmlStringText); |
| 1048 | cur->ns = NULL; |
| 1049 | cur->nsDef = NULL; |
| 1050 | if (content != NULL) |
| 1051 | cur->content = xmlStrndup(content, len); |
| 1052 | else |
| 1053 | cur->content = NULL; |
| 1054 | return(cur); |
| 1055 | } |
| 1056 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1057 | /** |
| 1058 | * xmlNewDocTextLen: |
| 1059 | * @doc: the document |
| 1060 | * @content: the text content |
| 1061 | * @len: the text len. |
| 1062 | * |
| 1063 | * Creation of a new text node with an extra content lenght parameter. The |
| 1064 | * text node pertain to a given document. |
| 1065 | * return values: a pointer to the new node object. |
| 1066 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1067 | xmlNodePtr |
| 1068 | xmlNewDocTextLen(xmlDocPtr doc, const CHAR *content, int len) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1069 | xmlNodePtr cur; |
| 1070 | |
| 1071 | cur = xmlNewTextLen(content, len); |
| 1072 | if (cur != NULL) cur->doc = doc; |
| 1073 | return(cur); |
| 1074 | } |
| 1075 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1076 | /** |
| 1077 | * xmlNewComment: |
| 1078 | * @content: the comment content |
| 1079 | * |
| 1080 | * Creation of a new node containing a comment. |
| 1081 | * return values: a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1082 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1083 | xmlNodePtr |
| 1084 | xmlNewComment(CHAR *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1085 | xmlNodePtr cur; |
| 1086 | |
| 1087 | /* |
| 1088 | * Allocate a new node and fill the fields. |
| 1089 | */ |
| 1090 | cur = (xmlNodePtr) malloc(sizeof(xmlNode)); |
| 1091 | if (cur == NULL) { |
| 1092 | fprintf(stderr, "xmlNewComment : malloc failed\n"); |
| 1093 | return(NULL); |
| 1094 | } |
| 1095 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1096 | cur->type = XML_COMMENT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1097 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1098 | cur->parent = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1099 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1100 | cur->next = NULL; |
| 1101 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1102 | cur->last = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1103 | cur->properties = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1104 | cur->type = XML_COMMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1105 | cur->name = xmlStrdup(xmlStringText); |
| 1106 | cur->ns = NULL; |
| 1107 | cur->nsDef = NULL; |
| 1108 | if (content != NULL) |
| 1109 | cur->content = xmlStrdup(content); |
| 1110 | else |
| 1111 | cur->content = NULL; |
| 1112 | return(cur); |
| 1113 | } |
| 1114 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1115 | /** |
| 1116 | * xmlNewComment: |
| 1117 | * @doc: the document |
| 1118 | * @content: the comment content |
| 1119 | * |
| 1120 | * Creation of a new node containing a commentwithin a document. |
| 1121 | * return values: a pointer to the new node object. |
| 1122 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1123 | xmlNodePtr |
| 1124 | xmlNewDocComment(xmlDocPtr doc, CHAR *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1125 | xmlNodePtr cur; |
| 1126 | |
| 1127 | cur = xmlNewComment(content); |
| 1128 | if (cur != NULL) cur->doc = doc; |
| 1129 | return(cur); |
| 1130 | } |
| 1131 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1132 | /** |
| 1133 | * xmlNewChild: |
| 1134 | * @parent: the parent node |
| 1135 | * @ns: a namespace if any |
| 1136 | * @name: the name of the child |
| 1137 | * @content: the content of the child if any. |
| 1138 | * |
| 1139 | * |
| 1140 | * Creation of a new child element, added at the end of @parent childs list. |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1141 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1142 | * a child list containing the TEXTs and ENTITY_REFs node will be created. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1143 | * return values: a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1144 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1145 | xmlNodePtr |
| 1146 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1147 | const CHAR *name, CHAR *content) { |
| 1148 | xmlNodePtr cur, prev; |
| 1149 | |
| 1150 | if (parent == NULL) { |
| 1151 | fprintf(stderr, "xmlNewChild : parent == NULL\n"); |
| 1152 | return(NULL); |
| 1153 | } |
| 1154 | |
| 1155 | if (name == NULL) { |
| 1156 | fprintf(stderr, "xmlNewChild : name == NULL\n"); |
| 1157 | return(NULL); |
| 1158 | } |
| 1159 | |
| 1160 | /* |
| 1161 | * Allocate a new node |
| 1162 | */ |
| 1163 | if (ns == NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1164 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1165 | else |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1166 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1167 | if (cur == NULL) return(NULL); |
| 1168 | |
| 1169 | /* |
| 1170 | * add the new element at the end of the childs list. |
| 1171 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1172 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1173 | cur->parent = parent; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1174 | cur->doc = parent->doc; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1175 | if (parent->childs == NULL) { |
| 1176 | parent->childs = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1177 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1178 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1179 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1180 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1181 | cur->prev = prev; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1182 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | return(cur); |
| 1186 | } |
| 1187 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1188 | /** |
| 1189 | * xmlAddChild: |
| 1190 | * @parent: the parent node |
| 1191 | * @cur: the child node |
| 1192 | * |
| 1193 | * Add a new child element, to @parent, at the end of the child list. |
| 1194 | * return values: the child or NULL in case of error. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1195 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1196 | xmlNodePtr |
| 1197 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1198 | xmlNodePtr prev; |
| 1199 | |
| 1200 | if (parent == NULL) { |
| 1201 | fprintf(stderr, "xmladdChild : parent == NULL\n"); |
| 1202 | return(NULL); |
| 1203 | } |
| 1204 | |
| 1205 | if (cur == NULL) { |
| 1206 | fprintf(stderr, "xmladdChild : child == NULL\n"); |
| 1207 | return(NULL); |
| 1208 | } |
| 1209 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1210 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 1211 | (cur->doc != parent->doc)) { |
| 1212 | fprintf(stderr, "Elements moved to a different document\n"); |
| 1213 | } |
| 1214 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1215 | /* |
| 1216 | * add the new element at the end of the childs list. |
| 1217 | */ |
| 1218 | cur->parent = parent; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1219 | cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1220 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1221 | /* |
| 1222 | * Handle the case where parent->content != NULL, in that case it will |
| 1223 | * create a intermediate TEXT node. |
| 1224 | */ |
| 1225 | if (parent->content != NULL) { |
| 1226 | xmlNodePtr text; |
| 1227 | |
| 1228 | text = xmlNewDocText(parent->doc, parent->content); |
| 1229 | if (text != NULL) { |
| 1230 | text->next = parent->childs; |
| 1231 | if (text->next != NULL) |
| 1232 | text->next->prev = text; |
| 1233 | parent->childs = text; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1234 | UPDATE_LAST_CHILD(parent); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1235 | free(parent->content); |
| 1236 | parent->content = NULL; |
| 1237 | } |
| 1238 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1239 | if (parent->childs == NULL) { |
| 1240 | parent->childs = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1241 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1242 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1243 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1244 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1245 | cur->prev = prev; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1246 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | return(cur); |
| 1250 | } |
| 1251 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1252 | /** |
| 1253 | * xmlGetLastChild: |
| 1254 | * @parent: the parent node |
| 1255 | * |
| 1256 | * Search the last child of a node. |
| 1257 | * return values: the last child or NULL if none. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1258 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1259 | xmlNodePtr |
| 1260 | xmlGetLastChild(xmlNodePtr parent) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1261 | if (parent == NULL) { |
| 1262 | fprintf(stderr, "xmlGetLastChild : parent == NULL\n"); |
| 1263 | return(NULL); |
| 1264 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1265 | return(parent->last); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1268 | /** |
| 1269 | * xmlFreeNodeList: |
| 1270 | * @cur: the first node in the list |
| 1271 | * |
| 1272 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 1273 | * the childs are freed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1274 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1275 | void |
| 1276 | xmlFreeNodeList(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1277 | xmlNodePtr next; |
| 1278 | if (cur == NULL) { |
| 1279 | fprintf(stderr, "xmlFreeNodeList : node == NULL\n"); |
| 1280 | return; |
| 1281 | } |
| 1282 | while (cur != NULL) { |
| 1283 | next = cur->next; |
| 1284 | xmlFreeNode(cur); |
| 1285 | cur = next; |
| 1286 | } |
| 1287 | } |
| 1288 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1289 | /** |
| 1290 | * xmlFreeNode: |
| 1291 | * @cur: the node |
| 1292 | * |
| 1293 | * Free a node, this is a recursive behaviour, all the childs are freed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1294 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1295 | void |
| 1296 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1297 | if (cur == NULL) { |
| 1298 | fprintf(stderr, "xmlFreeNode : node == NULL\n"); |
| 1299 | return; |
| 1300 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1301 | cur->doc = NULL; |
| 1302 | cur->parent = NULL; |
| 1303 | cur->next = NULL; |
| 1304 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1305 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1306 | if (cur->properties != NULL) xmlFreePropList(cur->properties); |
| 1307 | if (cur->type != XML_ENTITY_REF_NODE) |
| 1308 | if (cur->content != NULL) free(cur->content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1309 | if (cur->name != NULL) free((char *) cur->name); |
| 1310 | if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef); |
| 1311 | memset(cur, -1, sizeof(xmlNode)); |
| 1312 | free(cur); |
| 1313 | } |
| 1314 | |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1315 | /** |
| 1316 | * xmlUnlinkNode: |
| 1317 | * @cur: the node |
| 1318 | * |
| 1319 | * Unlink a node from it's current context, the node is not freed |
| 1320 | */ |
| 1321 | void |
| 1322 | xmlUnlinkNode(xmlNodePtr cur) { |
| 1323 | if (cur == NULL) { |
| 1324 | fprintf(stderr, "xmlUnlinkNode : node == NULL\n"); |
| 1325 | return; |
| 1326 | } |
| 1327 | if ((cur->parent != NULL) && (cur->parent->childs == cur)) |
| 1328 | cur->parent->childs = cur->next; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1329 | if ((cur->parent != NULL) && (cur->parent->last == cur)) |
| 1330 | cur->parent->last = cur->prev; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1331 | if (cur->next != NULL) |
| 1332 | cur->next->prev = cur->prev; |
| 1333 | if (cur->prev != NULL) |
| 1334 | cur->prev->next = cur->next; |
| 1335 | cur->next = cur->prev = NULL; |
| 1336 | cur->parent = NULL; |
| 1337 | } |
| 1338 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1339 | /************************************************************************ |
| 1340 | * * |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1341 | * Copy operations * |
| 1342 | * * |
| 1343 | ************************************************************************/ |
| 1344 | |
| 1345 | /** |
| 1346 | * xmlCopyNamespace: |
| 1347 | * @cur: the namespace |
| 1348 | * |
| 1349 | * Do a copy of the namespace. |
| 1350 | * |
| 1351 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 1352 | */ |
| 1353 | xmlNsPtr |
| 1354 | xmlCopyNamespace(xmlNsPtr cur) { |
| 1355 | xmlNsPtr ret; |
| 1356 | |
| 1357 | if (cur == NULL) return(NULL); |
| 1358 | switch (cur->type) { |
| 1359 | case XML_GLOBAL_NAMESPACE: |
| 1360 | ret = xmlNewGlobalNs(NULL, cur->href, cur->prefix); |
| 1361 | break; |
| 1362 | case XML_LOCAL_NAMESPACE: |
| 1363 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 1364 | break; |
| 1365 | default: |
| 1366 | fprintf(stderr, "xmlCopyNamespace: unknown type %d\n", cur->type); |
| 1367 | return(NULL); |
| 1368 | } |
| 1369 | return(ret); |
| 1370 | } |
| 1371 | |
| 1372 | /** |
| 1373 | * xmlCopyNamespaceList: |
| 1374 | * @cur: the first namespace |
| 1375 | * |
| 1376 | * Do a copy of an namespace list. |
| 1377 | * |
| 1378 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 1379 | */ |
| 1380 | xmlNsPtr |
| 1381 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 1382 | xmlNsPtr ret = NULL; |
| 1383 | xmlNsPtr p = NULL,q; |
| 1384 | |
| 1385 | while (cur != NULL) { |
| 1386 | q = xmlCopyNamespace(cur); |
| 1387 | if (p == NULL) { |
| 1388 | ret = p = q; |
| 1389 | } else { |
| 1390 | p->next = q; |
| 1391 | p = q; |
| 1392 | } |
| 1393 | cur = cur->next; |
| 1394 | } |
| 1395 | return(ret); |
| 1396 | } |
| 1397 | |
| 1398 | /** |
| 1399 | * xmlCopyProp: |
| 1400 | * @cur: the attribute |
| 1401 | * |
| 1402 | * Do a copy of the attribute. |
| 1403 | * |
| 1404 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 1405 | */ |
| 1406 | xmlAttrPtr |
| 1407 | xmlCopyProp(xmlAttrPtr cur) { |
| 1408 | xmlAttrPtr ret; |
| 1409 | |
| 1410 | if (cur == NULL) return(NULL); |
| 1411 | if (cur->val != NULL) |
| 1412 | ret = xmlNewDocProp(cur->val->doc, cur->name, NULL); |
| 1413 | else |
| 1414 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 1415 | if (ret == NULL) return(NULL); |
| 1416 | if (cur->val != NULL) |
| 1417 | ret->val = xmlCopyNodeList(cur->val); |
| 1418 | return(ret); |
| 1419 | } |
| 1420 | |
| 1421 | /** |
| 1422 | * xmlCopyPropList: |
| 1423 | * @cur: the first attribute |
| 1424 | * |
| 1425 | * Do a copy of an attribute list. |
| 1426 | * |
| 1427 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 1428 | */ |
| 1429 | xmlAttrPtr |
| 1430 | xmlCopyPropList(xmlAttrPtr cur) { |
| 1431 | xmlAttrPtr ret = NULL; |
| 1432 | xmlAttrPtr p = NULL,q; |
| 1433 | |
| 1434 | while (cur != NULL) { |
| 1435 | q = xmlCopyProp(cur); |
| 1436 | if (p == NULL) { |
| 1437 | ret = p = q; |
| 1438 | } else { |
| 1439 | p->next = q; |
| 1440 | p = q; |
| 1441 | } |
| 1442 | cur = cur->next; |
| 1443 | } |
| 1444 | return(ret); |
| 1445 | } |
| 1446 | |
| 1447 | /* |
| 1448 | * NOTE about the CopyNode operations ! |
| 1449 | * |
| 1450 | * They are splitted into external and internal parts for one |
| 1451 | * tricky reason: namespaces. Doing a direct copy of a node |
| 1452 | * say RPM:Copyright without changing the namespace pointer to |
| 1453 | * something else can produce stale links. One way to do it is |
| 1454 | * to keep a reference counter but this doesn't work as soon |
| 1455 | * as one move the element or the subtree out of the scope of |
| 1456 | * the existing namespace. The actual solution seems to add |
| 1457 | * a copy of the namespace at the top of the copied tree if |
| 1458 | * not available in the subtree. |
| 1459 | * Hence two functions, the public front-end call the inner ones |
| 1460 | */ |
| 1461 | |
| 1462 | static xmlNodePtr |
| 1463 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
| 1464 | |
| 1465 | static xmlNodePtr |
| 1466 | xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
| 1467 | int recursive) { |
| 1468 | xmlNodePtr ret; |
| 1469 | |
| 1470 | if (node == NULL) return(NULL); |
| 1471 | /* |
| 1472 | * Allocate a new node and fill the fields. |
| 1473 | */ |
| 1474 | ret = (xmlNodePtr) malloc(sizeof(xmlNode)); |
| 1475 | if (ret == NULL) { |
| 1476 | fprintf(stderr, "xmlStaticCopyNode : malloc failed\n"); |
| 1477 | return(NULL); |
| 1478 | } |
| 1479 | |
| 1480 | ret->type = node->type; |
| 1481 | ret->doc = doc; |
| 1482 | ret->parent = parent; |
| 1483 | ret->next = NULL; |
| 1484 | ret->prev = NULL; |
| 1485 | ret->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1486 | ret->last = NULL; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1487 | ret->properties = NULL; |
| 1488 | if (node->name != NULL) |
| 1489 | ret->name = xmlStrdup(node->name); |
| 1490 | else |
| 1491 | ret->name = NULL; |
| 1492 | ret->ns = NULL; |
| 1493 | ret->nsDef = NULL; |
| 1494 | if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) |
| 1495 | ret->content = xmlStrdup(node->content); |
| 1496 | else |
| 1497 | ret->content = NULL; |
| 1498 | #ifndef WITHOUT_CORBA |
| 1499 | ret->_private = NULL; |
| 1500 | ret->vepv = NULL; |
| 1501 | #endif |
| 1502 | if (parent != NULL) |
| 1503 | xmlAddChild(parent, ret); |
| 1504 | |
| 1505 | if (!recursive) return(ret); |
| 1506 | if (node->properties != NULL) |
| 1507 | ret->properties = xmlCopyPropList(node->properties); |
| 1508 | if (node->nsDef != NULL) |
| 1509 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 1510 | |
| 1511 | if (node->ns != NULL) { |
| 1512 | xmlNsPtr ns; |
| 1513 | |
| 1514 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 1515 | if (ns == NULL) { |
| 1516 | /* |
| 1517 | * Humm, we are copying an element whose namespace is defined |
| 1518 | * out of the new tree scope. Search it in the original tree |
| 1519 | * and add it at the top of the new tree |
| 1520 | */ |
| 1521 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 1522 | if (ns != NULL) { |
| 1523 | xmlNodePtr root = ret; |
| 1524 | |
| 1525 | while (root->parent != NULL) root = root->parent; |
| 1526 | xmlNewNs(root, ns->href, ns->prefix); |
| 1527 | } |
| 1528 | } else { |
| 1529 | /* |
| 1530 | * reference the existing namespace definition in our own tree. |
| 1531 | */ |
| 1532 | ret->ns = ns; |
| 1533 | } |
| 1534 | } |
| 1535 | if (node->childs != NULL) |
| 1536 | ret->childs = xmlStaticCopyNodeList(node->childs, doc, ret); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1537 | UPDATE_LAST_CHILD(ret); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1538 | return(ret); |
| 1539 | } |
| 1540 | |
| 1541 | static xmlNodePtr |
| 1542 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 1543 | xmlNodePtr ret = NULL; |
| 1544 | xmlNodePtr p = NULL,q; |
| 1545 | |
| 1546 | while (node != NULL) { |
| 1547 | q = xmlStaticCopyNode(node, doc, parent, 1); |
| 1548 | if (parent == NULL) { |
| 1549 | if (ret == NULL) ret = q; |
| 1550 | } else { |
| 1551 | if (ret == NULL) { |
| 1552 | q->prev = NULL; |
| 1553 | ret = p = q; |
| 1554 | } else { |
| 1555 | p->next = q; |
| 1556 | q->prev = p; |
| 1557 | p = q; |
| 1558 | } |
| 1559 | } |
| 1560 | node = node->next; |
| 1561 | } |
| 1562 | return(ret); |
| 1563 | } |
| 1564 | |
| 1565 | /** |
| 1566 | * xmlCopyNode: |
| 1567 | * @node: the node |
| 1568 | * @recursive: if 1 do a recursive copy. |
| 1569 | * |
| 1570 | * Do a copy of the node. |
| 1571 | * |
| 1572 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 1573 | */ |
| 1574 | xmlNodePtr |
| 1575 | xmlCopyNode(xmlNodePtr node, int recursive) { |
| 1576 | xmlNodePtr ret; |
| 1577 | |
| 1578 | ret = xmlStaticCopyNode(node, NULL, NULL, recursive); |
| 1579 | return(ret); |
| 1580 | } |
| 1581 | |
| 1582 | /** |
| 1583 | * xmlCopyNodeList: |
| 1584 | * @node: the first node in the list. |
| 1585 | * |
| 1586 | * Do a recursive copy of the node list. |
| 1587 | * |
| 1588 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 1589 | */ |
| 1590 | xmlNodePtr xmlCopyNodeList(xmlNodePtr node) { |
| 1591 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 1592 | return(ret); |
| 1593 | } |
| 1594 | |
| 1595 | /** |
| 1596 | * xmlCopyElement: |
| 1597 | * @elem: the element |
| 1598 | * |
| 1599 | * Do a copy of the element definition. |
| 1600 | * |
| 1601 | * Returns: a new xmlElementPtr, or NULL in case of error. |
| 1602 | xmlElementPtr |
| 1603 | xmlCopyElement(xmlElementPtr elem) { |
| 1604 | xmlElementPtr ret; |
| 1605 | |
| 1606 | if (elem == NULL) return(NULL); |
| 1607 | ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content); |
| 1608 | if (ret == NULL) return(NULL); |
| 1609 | if (!recursive) return(ret); |
| 1610 | if (elem->properties != NULL) |
| 1611 | ret->properties = xmlCopyPropList(elem->properties); |
| 1612 | |
| 1613 | if (elem->nsDef != NULL) |
| 1614 | ret->nsDef = xmlCopyNamespaceList(elem->nsDef); |
| 1615 | if (elem->childs != NULL) |
| 1616 | ret->childs = xmlCopyElementList(elem->childs); |
| 1617 | return(ret); |
| 1618 | } |
| 1619 | */ |
| 1620 | |
| 1621 | /** |
| 1622 | * xmlCopyDtd: |
| 1623 | * @dtd: the dtd |
| 1624 | * |
| 1625 | * Do a copy of the dtd. |
| 1626 | * |
| 1627 | * Returns: a new xmlDtdPtr, or NULL in case of error. |
| 1628 | */ |
| 1629 | xmlDtdPtr |
| 1630 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 1631 | xmlDtdPtr ret; |
| 1632 | |
| 1633 | if (dtd == NULL) return(NULL); |
| 1634 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 1635 | if (ret == NULL) return(NULL); |
| 1636 | if (dtd->entities != NULL) |
| 1637 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 1638 | (xmlEntitiesTablePtr) dtd->entities); |
| 1639 | /* |
| 1640 | * TODO: support for Element definitions. |
| 1641 | */ |
| 1642 | return(ret); |
| 1643 | } |
| 1644 | |
| 1645 | /** |
| 1646 | * xmlCopyDoc: |
| 1647 | * @doc: the document |
| 1648 | * @recursive: if 1 do a recursive copy. |
| 1649 | * |
| 1650 | * Do a copy of the document info. If recursive, the content tree will |
| 1651 | * be copied too as well as Dtd, namespaces and entities. |
| 1652 | * |
| 1653 | * Returns: a new xmlDocPtr, or NULL in case of error. |
| 1654 | */ |
| 1655 | xmlDocPtr |
| 1656 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 1657 | xmlDocPtr ret; |
| 1658 | |
| 1659 | if (doc == NULL) return(NULL); |
| 1660 | ret = xmlNewDoc(doc->version); |
| 1661 | if (ret == NULL) return(NULL); |
| 1662 | if (doc->name != NULL) |
| 1663 | ret->name = strdup(doc->name); |
| 1664 | if (doc->encoding != NULL) |
| 1665 | ret->encoding = xmlStrdup(doc->encoding); |
| 1666 | ret->compression = doc->compression; |
| 1667 | ret->standalone = doc->standalone; |
| 1668 | if (!recursive) return(ret); |
| 1669 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1670 | if (doc->intSubset != NULL) |
| 1671 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1672 | if (doc->oldNs != NULL) |
| 1673 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 1674 | if (doc->root != NULL) |
| 1675 | ret->root = xmlStaticCopyNodeList(doc->root, ret, NULL); |
| 1676 | return(ret); |
| 1677 | } |
| 1678 | |
| 1679 | /************************************************************************ |
| 1680 | * * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1681 | * Content access functions * |
| 1682 | * * |
| 1683 | ************************************************************************/ |
| 1684 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1685 | /** |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1686 | * xmlNodeGetContent: |
| 1687 | * @cur: the node being read |
| 1688 | * |
| 1689 | * Read the value of a node, this can be either the text carried |
| 1690 | * directly by this node if it's a TEXT node or the aggregate string |
| 1691 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 1692 | * Entity references are substitued. |
| 1693 | * Return value: a new CHAR * or NULL if no content is available. |
| 1694 | */ |
| 1695 | CHAR * |
| 1696 | xmlNodeGetContent(xmlNodePtr cur) { |
| 1697 | if (cur == NULL) return(NULL); |
| 1698 | switch (cur->type) { |
| 1699 | case XML_DOCUMENT_FRAG_NODE: |
| 1700 | case XML_ELEMENT_NODE: |
| 1701 | return(xmlNodeListGetString(cur->doc, cur->childs, 1)); |
| 1702 | break; |
| 1703 | case XML_ATTRIBUTE_NODE: |
| 1704 | case XML_CDATA_SECTION_NODE: |
| 1705 | case XML_ENTITY_REF_NODE: |
| 1706 | case XML_ENTITY_NODE: |
| 1707 | case XML_PI_NODE: |
| 1708 | case XML_COMMENT_NODE: |
| 1709 | case XML_DOCUMENT_NODE: |
| 1710 | case XML_DOCUMENT_TYPE_NODE: |
| 1711 | case XML_NOTATION_NODE: |
| 1712 | return(NULL); |
| 1713 | case XML_TEXT_NODE: |
| 1714 | if (cur->content != NULL) |
| 1715 | return(xmlStrdup(cur->content)); |
| 1716 | return(NULL); |
| 1717 | } |
| 1718 | return(NULL); |
| 1719 | } |
| 1720 | |
| 1721 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1722 | * xmlNodeSetContent: |
| 1723 | * @cur: the node being modified |
| 1724 | * @content: the new value of the content |
| 1725 | * |
| 1726 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1727 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1728 | void |
| 1729 | xmlNodeSetContent(xmlNodePtr cur, const CHAR *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1730 | if (cur == NULL) { |
| 1731 | fprintf(stderr, "xmlNodeSetContent : node == NULL\n"); |
| 1732 | return; |
| 1733 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1734 | switch (cur->type) { |
| 1735 | case XML_DOCUMENT_FRAG_NODE: |
| 1736 | case XML_ELEMENT_NODE: |
| 1737 | if (cur->content != NULL) { |
| 1738 | free(cur->content); |
| 1739 | cur->content = NULL; |
| 1740 | } |
| 1741 | if (cur->childs != NULL) xmlFreeNode(cur->childs); |
| 1742 | cur->childs = xmlStringGetNodeList(cur->doc, content); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1743 | UPDATE_LAST_CHILD(cur); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1744 | break; |
| 1745 | case XML_ATTRIBUTE_NODE: |
| 1746 | break; |
| 1747 | case XML_TEXT_NODE: |
| 1748 | case XML_CDATA_SECTION_NODE: |
| 1749 | case XML_ENTITY_REF_NODE: |
| 1750 | case XML_ENTITY_NODE: |
| 1751 | case XML_PI_NODE: |
| 1752 | case XML_COMMENT_NODE: |
| 1753 | if (cur->content != NULL) free(cur->content); |
| 1754 | if (cur->childs != NULL) xmlFreeNode(cur->childs); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1755 | cur->last = cur->childs = NULL; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1756 | if (content != NULL) |
| 1757 | cur->content = xmlStrdup(content); |
| 1758 | else |
| 1759 | cur->content = NULL; |
| 1760 | case XML_DOCUMENT_NODE: |
| 1761 | case XML_DOCUMENT_TYPE_NODE: |
| 1762 | break; |
| 1763 | case XML_NOTATION_NODE: |
| 1764 | break; |
| 1765 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1768 | /** |
| 1769 | * xmlNodeSetContentLen: |
| 1770 | * @cur: the node being modified |
| 1771 | * @content: the new value of the content |
| 1772 | * @len: the size of @content |
| 1773 | * |
| 1774 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1775 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1776 | void |
| 1777 | xmlNodeSetContentLen(xmlNodePtr cur, const CHAR *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1778 | if (cur == NULL) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1779 | fprintf(stderr, "xmlNodeSetContentLen : node == NULL\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1780 | return; |
| 1781 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1782 | switch (cur->type) { |
| 1783 | case XML_DOCUMENT_FRAG_NODE: |
| 1784 | case XML_ELEMENT_NODE: |
| 1785 | if (cur->content != NULL) { |
| 1786 | free(cur->content); |
| 1787 | cur->content = NULL; |
| 1788 | } |
| 1789 | if (cur->childs != NULL) xmlFreeNode(cur->childs); |
| 1790 | cur->childs = xmlStringLenGetNodeList(cur->doc, content, len); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1791 | UPDATE_LAST_CHILD(cur); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1792 | break; |
| 1793 | case XML_ATTRIBUTE_NODE: |
| 1794 | break; |
| 1795 | case XML_TEXT_NODE: |
| 1796 | case XML_CDATA_SECTION_NODE: |
| 1797 | case XML_ENTITY_REF_NODE: |
| 1798 | case XML_ENTITY_NODE: |
| 1799 | case XML_PI_NODE: |
| 1800 | case XML_COMMENT_NODE: |
| 1801 | if (cur->content != NULL) free(cur->content); |
| 1802 | if (cur->childs != NULL) xmlFreeNode(cur->childs); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1803 | cur->childs = cur->last = NULL; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1804 | if (content != NULL) |
| 1805 | cur->content = xmlStrndup(content, len); |
| 1806 | else |
| 1807 | cur->content = NULL; |
| 1808 | case XML_DOCUMENT_NODE: |
| 1809 | case XML_DOCUMENT_TYPE_NODE: |
| 1810 | break; |
| 1811 | case XML_NOTATION_NODE: |
| 1812 | if (cur->content != NULL) free(cur->content); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1813 | if (cur->childs != NULL) xmlFreeNode(cur->childs); |
| 1814 | cur->childs = cur->last = NULL; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1815 | if (content != NULL) |
| 1816 | cur->content = xmlStrndup(content, len); |
| 1817 | else |
| 1818 | cur->content = NULL; |
| 1819 | break; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1820 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1823 | /** |
| 1824 | * xmlNodeAddContentLen: |
| 1825 | * @cur: the node being modified |
| 1826 | * @content: extra content |
| 1827 | * @len: the size of @content |
| 1828 | * |
| 1829 | * Append the extra substring to the node content. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1830 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1831 | void |
| 1832 | xmlNodeAddContentLen(xmlNodePtr cur, const CHAR *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1833 | if (cur == NULL) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1834 | fprintf(stderr, "xmlNodeAddContentLen : node == NULL\n"); |
| 1835 | return; |
| 1836 | } |
| 1837 | if (len <= 0) return; |
| 1838 | switch (cur->type) { |
| 1839 | case XML_DOCUMENT_FRAG_NODE: |
| 1840 | case XML_ELEMENT_NODE: { |
| 1841 | xmlNodePtr last = NULL, new; |
| 1842 | |
| 1843 | if (cur->childs != NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1844 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1845 | } else { |
| 1846 | if (cur->content != NULL) { |
| 1847 | cur->childs = xmlStringGetNodeList(cur->doc, cur->content); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1848 | UPDATE_LAST_CHILD(cur); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1849 | free(cur->content); |
| 1850 | cur->content = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1851 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1852 | } |
| 1853 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1854 | new = xmlNewTextLen(content, len); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1855 | if (new != NULL) { |
| 1856 | xmlAddChild(cur, new); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1857 | if ((last != NULL) && (last->next == new)) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1858 | xmlTextMerge(last, new); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1859 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1860 | } |
| 1861 | break; |
| 1862 | } |
| 1863 | case XML_ATTRIBUTE_NODE: |
| 1864 | break; |
| 1865 | case XML_TEXT_NODE: |
| 1866 | case XML_CDATA_SECTION_NODE: |
| 1867 | case XML_ENTITY_REF_NODE: |
| 1868 | case XML_ENTITY_NODE: |
| 1869 | case XML_PI_NODE: |
| 1870 | case XML_COMMENT_NODE: |
| 1871 | if (content != NULL) |
| 1872 | cur->content = xmlStrncat(cur->content, content, len); |
| 1873 | case XML_DOCUMENT_NODE: |
| 1874 | case XML_DOCUMENT_TYPE_NODE: |
| 1875 | break; |
| 1876 | case XML_NOTATION_NODE: |
| 1877 | if (content != NULL) |
| 1878 | cur->content = xmlStrncat(cur->content, content, len); |
| 1879 | break; |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | /** |
| 1884 | * xmlNodeAddContent: |
| 1885 | * @cur: the node being modified |
| 1886 | * @content: extra content |
| 1887 | * |
| 1888 | * Append the extra substring to the node content. |
| 1889 | */ |
| 1890 | void |
| 1891 | xmlNodeAddContent(xmlNodePtr cur, const CHAR *content) { |
| 1892 | int len; |
| 1893 | |
| 1894 | if (cur == NULL) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1895 | fprintf(stderr, "xmlNodeAddContent : node == NULL\n"); |
| 1896 | return; |
| 1897 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1898 | if (content == NULL) return; |
| 1899 | len = xmlStrlen(content); |
| 1900 | xmlNodeAddContentLen(cur, content, len); |
| 1901 | } |
| 1902 | |
| 1903 | /** |
| 1904 | * xmlTextMerge: |
| 1905 | * @first: the first text node |
| 1906 | * @second: the second text node being merged |
| 1907 | * |
| 1908 | * Merge two text nodes into one |
| 1909 | * Return values: the first text node augmented |
| 1910 | */ |
| 1911 | xmlNodePtr |
| 1912 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 1913 | if (first == NULL) return(second); |
| 1914 | if (second == NULL) return(first); |
| 1915 | if (first->type != XML_TEXT_NODE) return(first); |
| 1916 | if (second->type != XML_TEXT_NODE) return(first); |
| 1917 | xmlNodeAddContent(first, second->content); |
| 1918 | xmlUnlinkNode(second); |
| 1919 | xmlFreeNode(second); |
| 1920 | return(first); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1923 | /** |
| 1924 | * xmlSearchNs: |
| 1925 | * @doc: the document |
| 1926 | * @node: the current node |
| 1927 | * @nameSpace: the namespace string |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1928 | * |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1929 | * Search a Ns registered under a given name space for a document. |
| 1930 | * recurse on the parents until it finds the defined namespace |
| 1931 | * or return NULL otherwise. |
| 1932 | * @nameSpace can be NULL, this is a search for the default namespace. |
| 1933 | * return values: the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1934 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1935 | xmlNsPtr |
| 1936 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const CHAR *nameSpace) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1937 | xmlNsPtr cur; |
| 1938 | |
| 1939 | while (node != NULL) { |
| 1940 | cur = node->nsDef; |
| 1941 | while (cur != NULL) { |
| 1942 | if ((cur->prefix == NULL) && (nameSpace == NULL)) |
| 1943 | return(cur); |
| 1944 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 1945 | (!xmlStrcmp(cur->prefix, nameSpace))) |
| 1946 | return(cur); |
| 1947 | cur = cur->next; |
| 1948 | } |
| 1949 | node = node->parent; |
| 1950 | } |
| 1951 | if (doc != NULL) { |
| 1952 | cur = doc->oldNs; |
| 1953 | while (cur != NULL) { |
| 1954 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 1955 | (!xmlStrcmp(cur->prefix, nameSpace))) |
| 1956 | return(cur); |
| 1957 | cur = cur->next; |
| 1958 | } |
| 1959 | } |
| 1960 | return(NULL); |
| 1961 | } |
| 1962 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1963 | /** |
| 1964 | * xmlSearchNsByHref: |
| 1965 | * @doc: the document |
| 1966 | * @node: the current node |
| 1967 | * @href: the namespace value |
| 1968 | * |
| 1969 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 1970 | * the defined namespace or return NULL otherwise. |
| 1971 | * return values: the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1972 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1973 | xmlNsPtr |
| 1974 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const CHAR *href) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1975 | xmlNsPtr cur; |
| 1976 | |
| 1977 | while (node != NULL) { |
| 1978 | cur = node->nsDef; |
| 1979 | while (cur != NULL) { |
| 1980 | if ((cur->href != NULL) && (href != NULL) && |
| 1981 | (!xmlStrcmp(cur->href, href))) |
| 1982 | return(cur); |
| 1983 | cur = cur->next; |
| 1984 | } |
| 1985 | node = node->parent; |
| 1986 | } |
| 1987 | if (doc != NULL) { |
| 1988 | cur = doc->oldNs; |
| 1989 | while (cur != NULL) { |
| 1990 | if ((cur->href != NULL) && (href != NULL) && |
| 1991 | (!xmlStrcmp(cur->href, href))) |
| 1992 | return(cur); |
| 1993 | cur = cur->next; |
| 1994 | } |
| 1995 | } |
| 1996 | return(NULL); |
| 1997 | } |
| 1998 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1999 | /** |
| 2000 | * xmlGetProp: |
| 2001 | * @node: the node |
| 2002 | * @name: the attribute name |
| 2003 | * |
| 2004 | * Search and get the value of an attribute associated to a node |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2005 | * This does the entity substitution. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2006 | * return values: the attribute value or NULL if not found. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2007 | */ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2008 | CHAR *xmlGetProp(xmlNodePtr node, const CHAR *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2009 | xmlAttrPtr prop = node->properties; |
| 2010 | |
| 2011 | while (prop != NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2012 | if (!xmlStrcmp(prop->name, name)) |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame^] | 2013 | CHAR *ret; |
| 2014 | |
| 2015 | ret = xmlNodeListGetString(node->doc, prop->val, 1); |
| 2016 | if (ret == NULL) return(xmlStrdup("")); |
| 2017 | return(ret); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2018 | prop = prop->next; |
| 2019 | } |
| 2020 | return(NULL); |
| 2021 | } |
| 2022 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2023 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2024 | * xmlSetProp: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2025 | * @node: the node |
| 2026 | * @name: the attribute name |
| 2027 | * @value: the attribute value |
| 2028 | * |
| 2029 | * Set (or reset) an attribute carried by a node. |
| 2030 | * return values: the attribute pointer. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2031 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2032 | xmlAttrPtr |
| 2033 | xmlSetProp(xmlNodePtr node, const CHAR *name, const CHAR *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2034 | xmlAttrPtr prop = node->properties; |
| 2035 | |
| 2036 | while (prop != NULL) { |
| 2037 | if (!xmlStrcmp(prop->name, name)) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2038 | if (prop->val != NULL) |
| 2039 | xmlFreeNode(prop->val); |
| 2040 | prop->val = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2041 | if (value != NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2042 | prop->val = xmlStringGetNodeList(node->doc, value); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2043 | return(prop); |
| 2044 | } |
| 2045 | prop = prop->next; |
| 2046 | } |
| 2047 | prop = xmlNewProp(node, name, value); |
| 2048 | return(prop); |
| 2049 | } |
| 2050 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2051 | /** |
| 2052 | * xmlNodeIsText: |
| 2053 | * @node: the node |
| 2054 | * |
| 2055 | * Is this node a Text node ? |
| 2056 | * return values: 1 yes, 0 no |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2057 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2058 | int |
| 2059 | xmlNodeIsText(xmlNodePtr node) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2060 | if (node == NULL) return(0); |
| 2061 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2062 | if (node->type == XML_TEXT_NODE) return(1); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2063 | return(0); |
| 2064 | } |
| 2065 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2066 | /** |
| 2067 | * xmlNodeIsText: |
| 2068 | * @node: the node |
| 2069 | * @content: the content |
| 2070 | * @len: @content lenght |
| 2071 | * |
| 2072 | * Concat the given string at the end of the existing node content |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2073 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2074 | |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2075 | void |
| 2076 | xmlTextConcat(xmlNodePtr node, const CHAR *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2077 | if (node == NULL) return; |
| 2078 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2079 | if (node->type != XML_TEXT_NODE) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2080 | fprintf(stderr, "xmlTextConcat: node is not text\n"); |
| 2081 | return; |
| 2082 | } |
| 2083 | node->content = xmlStrncat(node->content, content, len); |
| 2084 | } |
| 2085 | |
| 2086 | /************************************************************************ |
| 2087 | * * |
| 2088 | * Output : to a FILE or in memory * |
| 2089 | * * |
| 2090 | ************************************************************************/ |
| 2091 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2092 | static CHAR *buffer = NULL; |
| 2093 | static int buffer_index = 0; |
| 2094 | static int buffer_size = 0; |
| 2095 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2096 | /** |
| 2097 | * xmlBufferWriteCHAR: |
| 2098 | * @string: the string to add |
| 2099 | * |
| 2100 | * routine which manage and grows an output buffer. This one add |
| 2101 | * CHARs at the end of the array. |
| 2102 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2103 | void |
| 2104 | xmlBufferWriteCHAR(const CHAR *string) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2105 | const CHAR *cur; |
| 2106 | |
| 2107 | if (buffer == NULL) { |
| 2108 | buffer_size = 50000; |
| 2109 | buffer = (CHAR *) malloc(buffer_size * sizeof(CHAR)); |
| 2110 | if (buffer == NULL) { |
| 2111 | fprintf(stderr, "xmlBufferWrite : out of memory!\n"); |
| 2112 | exit(1); |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | if (string == NULL) return; |
| 2117 | for (cur = string;*cur != 0;cur++) { |
| 2118 | if (buffer_index + 10 >= buffer_size) { |
| 2119 | buffer_size *= 2; |
| 2120 | buffer = (CHAR *) realloc(buffer, buffer_size * sizeof(CHAR)); |
| 2121 | if (buffer == NULL) { |
| 2122 | fprintf(stderr, "xmlBufferWrite : out of memory!\n"); |
| 2123 | exit(1); |
| 2124 | } |
| 2125 | } |
| 2126 | buffer[buffer_index++] = *cur; |
| 2127 | } |
| 2128 | buffer[buffer_index] = 0; |
| 2129 | } |
| 2130 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2131 | /** |
| 2132 | * xmlBufferWriteChar: |
| 2133 | * @string: the string to add |
| 2134 | * |
| 2135 | * routine which manage and grows an output buffer. This one add |
| 2136 | * C chars at the end of the array. |
| 2137 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2138 | void |
| 2139 | xmlBufferWriteChar(const char *string) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2140 | const char *cur; |
| 2141 | |
| 2142 | if (buffer == NULL) { |
| 2143 | buffer_size = 50000; |
| 2144 | buffer = (CHAR *) malloc(buffer_size * sizeof(CHAR)); |
| 2145 | if (buffer == NULL) { |
| 2146 | fprintf(stderr, "xmlBufferWrite : out of memory!\n"); |
| 2147 | exit(1); |
| 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | if (string == NULL) return; |
| 2152 | for (cur = string;*cur != 0;cur++) { |
| 2153 | if (buffer_index + 10 >= buffer_size) { |
| 2154 | buffer_size *= 2; |
| 2155 | buffer = (CHAR *) realloc(buffer, buffer_size * sizeof(CHAR)); |
| 2156 | if (buffer == NULL) { |
| 2157 | fprintf(stderr, "xmlBufferWrite : out of memory!\n"); |
| 2158 | exit(1); |
| 2159 | } |
| 2160 | } |
| 2161 | buffer[buffer_index++] = *cur; |
| 2162 | } |
| 2163 | buffer[buffer_index] = 0; |
| 2164 | } |
| 2165 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2166 | /** |
| 2167 | * xmlGlobalNsDump: |
| 2168 | * @cur: a namespace |
| 2169 | * |
| 2170 | * Dump a global Namespace, this is the old version based on PIs. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2171 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2172 | static void |
| 2173 | xmlGlobalNsDump(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2174 | if (cur == NULL) { |
| 2175 | fprintf(stderr, "xmlGlobalNsDump : Ns == NULL\n"); |
| 2176 | return; |
| 2177 | } |
| 2178 | if (cur->type == XML_GLOBAL_NAMESPACE) { |
| 2179 | xmlBufferWriteChar("<?namespace"); |
| 2180 | if (cur->href != NULL) { |
| 2181 | xmlBufferWriteChar(" href=\""); |
| 2182 | xmlBufferWriteCHAR(cur->href); |
| 2183 | xmlBufferWriteChar("\""); |
| 2184 | } |
| 2185 | if (cur->prefix != NULL) { |
| 2186 | xmlBufferWriteChar(" AS=\""); |
| 2187 | xmlBufferWriteCHAR(cur->prefix); |
| 2188 | xmlBufferWriteChar("\""); |
| 2189 | } |
| 2190 | xmlBufferWriteChar("?>\n"); |
| 2191 | } |
| 2192 | } |
| 2193 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2194 | /** |
| 2195 | * xmlGlobalNsListDump: |
| 2196 | * @cur: the first namespace |
| 2197 | * |
| 2198 | * Dump a list of global Namespace, this is the old version based on PIs. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2199 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2200 | static void |
| 2201 | xmlGlobalNsListDump(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2202 | while (cur != NULL) { |
| 2203 | xmlGlobalNsDump(cur); |
| 2204 | cur = cur->next; |
| 2205 | } |
| 2206 | } |
| 2207 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2208 | /** |
| 2209 | * xmlNsDump: |
| 2210 | * @cur: a namespace |
| 2211 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2212 | * Dump a local Namespace definition. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2213 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2214 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2215 | static void |
| 2216 | xmlNsDump(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2217 | if (cur == NULL) { |
| 2218 | fprintf(stderr, "xmlNsDump : Ns == NULL\n"); |
| 2219 | return; |
| 2220 | } |
| 2221 | if (cur->type == XML_LOCAL_NAMESPACE) { |
| 2222 | /* Within the context of an element attributes */ |
| 2223 | if (cur->prefix != NULL) { |
| 2224 | xmlBufferWriteChar(" xmlns:"); |
| 2225 | xmlBufferWriteCHAR(cur->prefix); |
| 2226 | } else |
| 2227 | xmlBufferWriteChar(" xmlns"); |
| 2228 | xmlBufferWriteChar("=\""); |
| 2229 | xmlBufferWriteCHAR(cur->href); |
| 2230 | xmlBufferWriteChar("\""); |
| 2231 | } |
| 2232 | } |
| 2233 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2234 | /** |
| 2235 | * xmlNsListDump: |
| 2236 | * @cur: the first namespace |
| 2237 | * |
| 2238 | * Dump a list of local Namespace definitions. |
| 2239 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2240 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2241 | static void |
| 2242 | xmlNsListDump(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2243 | while (cur != NULL) { |
| 2244 | xmlNsDump(cur); |
| 2245 | cur = cur->next; |
| 2246 | } |
| 2247 | } |
| 2248 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2249 | /** |
| 2250 | * xmlDtdDump: |
| 2251 | * @doc: the document |
| 2252 | * |
| 2253 | * Dump the XML document DTD, if any. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2254 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2255 | static void |
| 2256 | xmlDtdDump(xmlDocPtr doc) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2257 | xmlDtdPtr cur = doc->intSubset; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2258 | |
| 2259 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2260 | fprintf(stderr, "xmlDtdDump : no internal subset\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2261 | return; |
| 2262 | } |
| 2263 | xmlBufferWriteChar("<!DOCTYPE "); |
| 2264 | xmlBufferWriteCHAR(cur->name); |
| 2265 | if (cur->ExternalID != NULL) { |
| 2266 | xmlBufferWriteChar(" PUBLIC \""); |
| 2267 | xmlBufferWriteCHAR(cur->ExternalID); |
| 2268 | xmlBufferWriteChar("\" \""); |
| 2269 | xmlBufferWriteCHAR(cur->SystemID); |
| 2270 | xmlBufferWriteChar("\""); |
| 2271 | } else if (cur->SystemID != NULL) { |
| 2272 | xmlBufferWriteChar(" SYSTEM \""); |
| 2273 | xmlBufferWriteCHAR(cur->SystemID); |
| 2274 | xmlBufferWriteChar("\""); |
| 2275 | } |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 2276 | if ((cur->entities == NULL) && (cur->elements == NULL)) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2277 | xmlBufferWriteChar(">\n"); |
| 2278 | return; |
| 2279 | } |
| 2280 | xmlBufferWriteChar(" [\n"); |
| 2281 | if (cur->entities != NULL) |
| 2282 | xmlDumpEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 2283 | if (cur->elements != NULL) |
| 2284 | xmlDumpElementTable((xmlElementTablePtr) cur->elements); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2285 | xmlBufferWriteChar("]"); |
| 2286 | |
| 2287 | /* TODO !!! a lot more things to dump ... */ |
| 2288 | xmlBufferWriteChar(">\n"); |
| 2289 | } |
| 2290 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2291 | /** |
| 2292 | * xmlAttrDump: |
| 2293 | * @doc: the document |
| 2294 | * @cur: the attribute pointer |
| 2295 | * |
| 2296 | * Dump an XML attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2297 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2298 | static void |
| 2299 | xmlAttrDump(xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2300 | CHAR *value; |
| 2301 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2302 | if (cur == NULL) { |
| 2303 | fprintf(stderr, "xmlAttrDump : property == NULL\n"); |
| 2304 | return; |
| 2305 | } |
| 2306 | xmlBufferWriteChar(" "); |
| 2307 | xmlBufferWriteCHAR(cur->name); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2308 | value = xmlNodeListGetString(doc, cur->val, 0); |
| 2309 | if (value) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2310 | xmlBufferWriteChar("=\""); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2311 | xmlBufferWriteCHAR(value); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2312 | xmlBufferWriteChar("\""); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2313 | free(value); |
Daniel Veillard | 726c7e3 | 1999-02-08 15:13:10 +0000 | [diff] [blame] | 2314 | } else { |
| 2315 | xmlBufferWriteChar("=\"\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2316 | } |
| 2317 | } |
| 2318 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2319 | /** |
| 2320 | * xmlAttrListDump: |
| 2321 | * @doc: the document |
| 2322 | * @cur: the first attribute pointer |
| 2323 | * |
| 2324 | * Dump a list of XML attributes |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2325 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2326 | static void |
| 2327 | xmlAttrListDump(xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2328 | if (cur == NULL) { |
| 2329 | fprintf(stderr, "xmlAttrListDump : property == NULL\n"); |
| 2330 | return; |
| 2331 | } |
| 2332 | while (cur != NULL) { |
| 2333 | xmlAttrDump(doc, cur); |
| 2334 | cur = cur->next; |
| 2335 | } |
| 2336 | } |
| 2337 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2338 | |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2339 | static void |
| 2340 | xmlNodeDump(xmlDocPtr doc, xmlNodePtr cur, int level); |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2341 | /** |
| 2342 | * xmlNodeListDump: |
| 2343 | * @doc: the document |
| 2344 | * @cur: the first node |
| 2345 | * @level: the imbrication level for indenting |
| 2346 | * |
| 2347 | * Dump an XML node list, recursive behaviour,children are printed too. |
| 2348 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2349 | static void |
| 2350 | xmlNodeListDump(xmlDocPtr doc, xmlNodePtr cur, int level) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2351 | int needIndent = 0, i; |
| 2352 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2353 | if (cur == NULL) { |
| 2354 | fprintf(stderr, "xmlNodeListDump : node == NULL\n"); |
| 2355 | return; |
| 2356 | } |
| 2357 | while (cur != NULL) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2358 | if ((cur->type != XML_TEXT_NODE) && |
| 2359 | (cur->type != XML_ENTITY_REF_NODE)) { |
| 2360 | if (!needIndent) { |
| 2361 | needIndent = 1; |
| 2362 | xmlBufferWriteChar("\n"); |
| 2363 | } |
| 2364 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2365 | xmlNodeDump(doc, cur, level); |
| 2366 | cur = cur->next; |
| 2367 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2368 | if ((xmlIndentTreeOutput) && (needIndent)) |
| 2369 | for (i = 1;i < level;i++) |
| 2370 | xmlBufferWriteChar(" "); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2373 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2374 | * xmlNodeDump: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2375 | * @doc: the document |
| 2376 | * @cur: the current node |
| 2377 | * @level: the imbrication level for indenting |
| 2378 | * |
| 2379 | * Dump an XML node, recursive behaviour,children are printed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2380 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2381 | static void |
| 2382 | xmlNodeDump(xmlDocPtr doc, xmlNodePtr cur, int level) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2383 | int i; |
| 2384 | |
| 2385 | if (cur == NULL) { |
| 2386 | fprintf(stderr, "xmlNodeDump : node == NULL\n"); |
| 2387 | return; |
| 2388 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2389 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2390 | if (cur->content != NULL) |
| 2391 | xmlBufferWriteCHAR(xmlEncodeEntities(doc, cur->content)); |
| 2392 | return; |
| 2393 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2394 | if (cur->type == XML_COMMENT_NODE) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2395 | if (cur->content != NULL) { |
| 2396 | xmlBufferWriteChar("<!--"); |
| 2397 | xmlBufferWriteCHAR(cur->content); |
| 2398 | xmlBufferWriteChar("-->"); |
| 2399 | } |
| 2400 | return; |
| 2401 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2402 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 2403 | xmlBufferWriteChar("&"); |
| 2404 | xmlBufferWriteCHAR(cur->name); |
| 2405 | xmlBufferWriteChar(";"); |
| 2406 | return; |
| 2407 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2408 | if (xmlIndentTreeOutput) |
| 2409 | for (i = 0;i < level;i++) |
| 2410 | xmlBufferWriteChar(" "); |
| 2411 | |
| 2412 | xmlBufferWriteChar("<"); |
| 2413 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 2414 | xmlBufferWriteCHAR(cur->ns->prefix); |
| 2415 | xmlBufferWriteChar(":"); |
| 2416 | } |
| 2417 | |
| 2418 | xmlBufferWriteCHAR(cur->name); |
| 2419 | if (cur->nsDef) |
| 2420 | xmlNsListDump(cur->nsDef); |
| 2421 | if (cur->properties != NULL) |
| 2422 | xmlAttrListDump(doc, cur->properties); |
| 2423 | |
| 2424 | if ((cur->content == NULL) && (cur->childs == NULL)) { |
| 2425 | xmlBufferWriteChar("/>\n"); |
| 2426 | return; |
| 2427 | } |
| 2428 | xmlBufferWriteChar(">"); |
| 2429 | if (cur->content != NULL) |
| 2430 | xmlBufferWriteCHAR(xmlEncodeEntities(doc, cur->content)); |
| 2431 | if (cur->childs != NULL) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2432 | xmlNodeListDump(doc, cur->childs, level + 1); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2433 | } |
| 2434 | xmlBufferWriteChar("</"); |
| 2435 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 2436 | xmlBufferWriteCHAR(cur->ns->prefix); |
| 2437 | xmlBufferWriteChar(":"); |
| 2438 | } |
| 2439 | |
| 2440 | xmlBufferWriteCHAR(cur->name); |
| 2441 | xmlBufferWriteChar(">\n"); |
| 2442 | } |
| 2443 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2444 | /** |
| 2445 | * xmlDocContentDump: |
| 2446 | * @cur: the document |
| 2447 | * |
| 2448 | * Dump an XML document. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2449 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2450 | static void |
| 2451 | xmlDocContentDump(xmlDocPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2452 | if (oldXMLWDcompatibility) |
| 2453 | xmlBufferWriteChar("<?XML version=\""); |
| 2454 | else |
| 2455 | xmlBufferWriteChar("<?xml version=\""); |
| 2456 | xmlBufferWriteCHAR(cur->version); |
| 2457 | xmlBufferWriteChar("\""); |
| 2458 | if (cur->encoding != NULL) { |
| 2459 | xmlBufferWriteChar(" encoding=\""); |
| 2460 | xmlBufferWriteCHAR(cur->encoding); |
| 2461 | xmlBufferWriteChar("\""); |
| 2462 | } |
| 2463 | switch (cur->standalone) { |
| 2464 | case 0: |
| 2465 | xmlBufferWriteChar(" standalone=\"no\""); |
| 2466 | break; |
| 2467 | case 1: |
| 2468 | xmlBufferWriteChar(" standalone=\"yes\""); |
| 2469 | break; |
| 2470 | } |
| 2471 | xmlBufferWriteChar("?>\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2472 | if (cur->intSubset != NULL) |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2473 | xmlDtdDump(cur); |
| 2474 | if (cur->root != NULL) { |
| 2475 | /* global namespace definitions, the old way */ |
| 2476 | if (oldXMLWDcompatibility) |
| 2477 | xmlGlobalNsListDump(cur->oldNs); |
| 2478 | else |
| 2479 | xmlUpgradeOldNs(cur); |
| 2480 | xmlNodeDump(cur, cur->root, 0); |
| 2481 | } |
| 2482 | } |
| 2483 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2484 | /** |
| 2485 | * xmlDocDumpMemory: |
| 2486 | * @cur: the document |
| 2487 | * @mem: OUT: the memory pointer |
| 2488 | * @size: OUT: the memory lenght |
| 2489 | * |
| 2490 | * Dump an XML document in memory and return the CHAR * and it's size. |
| 2491 | * It's up to the caller to free the memory. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2492 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2493 | void |
| 2494 | xmlDocDumpMemory(xmlDocPtr cur, CHAR**mem, int *size) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2495 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2496 | #ifdef DEBUG_TREE |
| 2497 | fprintf(stderr, "xmlDocDumpMemory : document == NULL\n"); |
| 2498 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2499 | *mem = NULL; |
| 2500 | *size = 0; |
| 2501 | return; |
| 2502 | } |
| 2503 | buffer_index = 0; |
| 2504 | xmlDocContentDump(cur); |
| 2505 | |
| 2506 | *mem = buffer; |
| 2507 | *size = buffer_index; |
| 2508 | } |
| 2509 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2510 | /** |
| 2511 | * xmlGetDocCompressMode: |
| 2512 | * @doc: the document |
| 2513 | * |
| 2514 | * get the compression ratio for a document, ZLIB based |
| 2515 | * return values: 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 2516 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2517 | int |
| 2518 | xmlGetDocCompressMode (xmlDocPtr doc) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 2519 | if (doc == NULL) return(-1); |
| 2520 | return(doc->compression); |
| 2521 | } |
| 2522 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2523 | /** |
| 2524 | * xmlSetDocCompressMode: |
| 2525 | * @doc: the document |
| 2526 | * @mode: the compression ratio |
| 2527 | * |
| 2528 | * set the compression ratio for a document, ZLIB based |
| 2529 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 2530 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2531 | void |
| 2532 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 2533 | if (doc == NULL) return; |
| 2534 | if (mode < 0) doc->compression = 0; |
| 2535 | else if (mode > 9) doc->compression = 9; |
| 2536 | else doc->compression = mode; |
| 2537 | } |
| 2538 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2539 | /** |
| 2540 | * xmlGetCompressMode: |
| 2541 | * |
| 2542 | * get the default compression mode used, ZLIB based. |
| 2543 | * return values: 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 2544 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2545 | int |
| 2546 | xmlGetCompressMode(void) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 2547 | return(xmlCompressMode); |
| 2548 | } |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2549 | |
| 2550 | /** |
| 2551 | * xmlSetCompressMode: |
| 2552 | * @mode: the compression ratio |
| 2553 | * |
| 2554 | * set the default compression mode used, ZLIB based |
| 2555 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 2556 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2557 | void |
| 2558 | xmlSetCompressMode(int mode) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 2559 | if (mode < 0) xmlCompressMode = 0; |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 2560 | else if (mode > 9) xmlCompressMode = 9; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 2561 | else xmlCompressMode = mode; |
| 2562 | } |
| 2563 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2564 | /** |
| 2565 | * xmlDocDump: |
| 2566 | * @f: the FILE* |
| 2567 | * @cur: the document |
| 2568 | * |
| 2569 | * Dump an XML document to an open FILE. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2570 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2571 | void |
| 2572 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2573 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2574 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2575 | fprintf(stderr, "xmlDocDump : document == NULL\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2576 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2577 | return; |
| 2578 | } |
| 2579 | buffer_index = 0; |
| 2580 | xmlDocContentDump(cur); |
| 2581 | |
| 2582 | fwrite(buffer, sizeof(CHAR), buffer_index, f); |
| 2583 | } |
| 2584 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2585 | /** |
| 2586 | * xmlSaveFile: |
| 2587 | * @filename: the filename |
| 2588 | * @cur: the document |
| 2589 | * |
| 2590 | * Dump an XML document to a file. Will use compression if |
| 2591 | * compiled in and enabled. |
| 2592 | * returns: the number of file written or -1 in case of failure. |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 2593 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2594 | int |
| 2595 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 2596 | #ifdef HAVE_ZLIB_H |
| 2597 | gzFile zoutput = NULL; |
| 2598 | char mode[15]; |
| 2599 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2600 | FILE *output = NULL; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 2601 | int ret; |
| 2602 | |
| 2603 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | dc3dd9d | 1998-09-24 19:25:54 +0000 | [diff] [blame] | 2604 | if ((cur->compression > 0) && (cur->compression <= 9)) { |
| 2605 | sprintf(mode, "w%d", cur->compression); |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 2606 | zoutput = gzopen(filename, mode); |
| 2607 | } |
| 2608 | if (zoutput == NULL) { |
| 2609 | #endif |
| 2610 | output = fopen(filename, "w"); |
| 2611 | if (output == NULL) return(-1); |
| 2612 | #ifdef HAVE_ZLIB_H |
| 2613 | } |
| 2614 | #endif |
| 2615 | |
| 2616 | /* |
| 2617 | * save the content to a temp buffer. |
| 2618 | */ |
| 2619 | buffer_index = 0; |
| 2620 | xmlDocContentDump(cur); |
| 2621 | |
| 2622 | #ifdef HAVE_ZLIB_H |
| 2623 | if (zoutput != NULL) { |
| 2624 | ret = gzwrite(zoutput, buffer, sizeof(CHAR) * buffer_index); |
| 2625 | gzclose(zoutput); |
| 2626 | return(ret); |
| 2627 | } |
| 2628 | #endif |
| 2629 | ret = fwrite(buffer, sizeof(CHAR), buffer_index, output); |
| 2630 | fclose(output); |
| 2631 | return(ret * sizeof(CHAR)); |
| 2632 | } |
| 2633 | |