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