Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * tree.c : implemetation of access function for an XML tree. |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 6 | * Daniel.Veillard@w3.org |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 9 | #ifdef WIN32 |
| 10 | #define HAVE_FCNTL_H |
| 11 | #include <io.h> |
| 12 | #else |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 13 | #include "config.h" |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 14 | #endif |
| 15 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 16 | #include <stdio.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 17 | #include <string.h> /* for memset() only ! */ |
| 18 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 19 | #ifdef HAVE_CTYPE_H |
| 20 | #include <ctype.h> |
| 21 | #endif |
| 22 | #ifdef HAVE_STDLIB_H |
| 23 | #include <stdlib.h> |
| 24 | #endif |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 25 | #ifdef HAVE_ZLIB_H |
| 26 | #include <zlib.h> |
| 27 | #endif |
| 28 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 29 | #include "xmlmemory.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 30 | #include "tree.h" |
| 31 | #include "entities.h" |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 32 | #include "valid.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 33 | |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 34 | static xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 35 | int oldXMLWDcompatibility = 0; |
| 36 | int xmlIndentTreeOutput = 1; |
| 37 | |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 38 | static int xmlCompressMode = 0; |
| 39 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 40 | #define UPDATE_LAST_CHILD(n) if ((n) != NULL) { \ |
| 41 | xmlNodePtr ulccur = (n)->childs; \ |
| 42 | if (ulccur == NULL) { \ |
| 43 | (n)->last = NULL; \ |
| 44 | } else { \ |
| 45 | while (ulccur->next != NULL) ulccur = ulccur->next; \ |
| 46 | (n)->last = ulccur; \ |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 47 | }} |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 48 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 49 | /************************************************************************ |
| 50 | * * |
| 51 | * Allocation and deallocation of basic structures * |
| 52 | * * |
| 53 | ************************************************************************/ |
| 54 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 55 | /** |
| 56 | * xmlUpgradeOldNs: |
| 57 | * @doc: a document pointer |
| 58 | * |
| 59 | * 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] | 60 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 61 | void |
| 62 | xmlUpgradeOldNs(xmlDocPtr doc) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 63 | xmlNsPtr cur; |
| 64 | |
| 65 | if ((doc == NULL) || (doc->oldNs == NULL)) return; |
| 66 | if (doc->root == NULL) { |
| 67 | fprintf(stderr, "xmlUpgradeOldNs: failed no root !\n"); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | cur = doc->oldNs; |
| 72 | while (cur->next != NULL) { |
| 73 | cur->type = XML_LOCAL_NAMESPACE; |
| 74 | cur = cur->next; |
| 75 | } |
| 76 | cur->type = XML_LOCAL_NAMESPACE; |
| 77 | cur->next = doc->root->nsDef; |
| 78 | doc->root->nsDef = doc->oldNs; |
| 79 | doc->oldNs = NULL; |
| 80 | } |
| 81 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 82 | /** |
| 83 | * xmlNewNs: |
| 84 | * @node: the element carrying the namespace |
| 85 | * @href: the URI associated |
| 86 | * @prefix: the prefix for the namespace |
| 87 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 88 | * Creation of a new Namespace. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 89 | * Returns returns a new namespace pointer |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 90 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 91 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 92 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 93 | xmlNsPtr cur; |
| 94 | |
| 95 | if (href == NULL) { |
| 96 | fprintf(stderr, "xmlNewNs: href == NULL !\n"); |
| 97 | return(NULL); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Allocate a new DTD and fill the fields. |
| 102 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 103 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 104 | if (cur == NULL) { |
| 105 | fprintf(stderr, "xmlNewNs : malloc failed\n"); |
| 106 | return(NULL); |
| 107 | } |
| 108 | |
| 109 | cur->type = XML_LOCAL_NAMESPACE; |
| 110 | if (href != NULL) |
| 111 | cur->href = xmlStrdup(href); |
| 112 | else |
| 113 | cur->href = NULL; |
| 114 | if (prefix != NULL) |
| 115 | cur->prefix = xmlStrdup(prefix); |
| 116 | else |
| 117 | cur->prefix = NULL; |
| 118 | |
| 119 | /* |
| 120 | * Add it at the end to preserve parsing order ... |
| 121 | */ |
| 122 | cur->next = NULL; |
| 123 | if (node != NULL) { |
| 124 | if (node->nsDef == NULL) { |
| 125 | node->nsDef = cur; |
| 126 | } else { |
| 127 | xmlNsPtr prev = node->nsDef; |
| 128 | |
| 129 | while (prev->next != NULL) prev = prev->next; |
| 130 | prev->next = cur; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return(cur); |
| 135 | } |
| 136 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 137 | /** |
| 138 | * xmlNewGlobalNs: |
| 139 | * @doc: the document carrying the namespace |
| 140 | * @href: the URI associated |
| 141 | * @prefix: the prefix for the namespace |
| 142 | * |
| 143 | * Creation of a Namespace, the old way using PI and without scoping, to AVOID. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 144 | * Returns returns a new namespace pointer |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 145 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 146 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 147 | xmlNewGlobalNs(xmlDocPtr doc, const xmlChar *href, const xmlChar *prefix) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 148 | xmlNsPtr cur; |
| 149 | |
| 150 | /* |
| 151 | * Allocate a new DTD and fill the fields. |
| 152 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 153 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 154 | if (cur == NULL) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 155 | fprintf(stderr, "xmlNewGlobalNs : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 156 | return(NULL); |
| 157 | } |
| 158 | |
| 159 | cur->type = XML_GLOBAL_NAMESPACE; |
| 160 | if (href != NULL) |
| 161 | cur->href = xmlStrdup(href); |
| 162 | else |
| 163 | cur->href = NULL; |
| 164 | if (prefix != NULL) |
| 165 | cur->prefix = xmlStrdup(prefix); |
| 166 | else |
| 167 | cur->prefix = NULL; |
| 168 | |
| 169 | /* |
| 170 | * Add it at the end to preserve parsing order ... |
| 171 | */ |
| 172 | cur->next = NULL; |
| 173 | if (doc != NULL) { |
| 174 | if (doc->oldNs == NULL) { |
| 175 | doc->oldNs = cur; |
| 176 | } else { |
| 177 | xmlNsPtr prev = doc->oldNs; |
| 178 | |
| 179 | while (prev->next != NULL) prev = prev->next; |
| 180 | prev->next = cur; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return(cur); |
| 185 | } |
| 186 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 187 | /** |
| 188 | * xmlSetNs: |
| 189 | * @node: a node in the document |
| 190 | * @ns: a namespace pointer |
| 191 | * |
| 192 | * Associate a namespace to a node, a posteriori. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 193 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 194 | void |
| 195 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 196 | if (node == NULL) { |
| 197 | fprintf(stderr, "xmlSetNs: node == NULL\n"); |
| 198 | return; |
| 199 | } |
| 200 | node->ns = ns; |
| 201 | } |
| 202 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 203 | /** |
| 204 | * xmlFreeNs: |
| 205 | * @cur: the namespace pointer |
| 206 | * |
| 207 | * Free up the structures associated to a namespace |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 208 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 209 | void |
| 210 | xmlFreeNs(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 211 | if (cur == NULL) { |
| 212 | fprintf(stderr, "xmlFreeNs : ns == NULL\n"); |
| 213 | return; |
| 214 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 215 | if (cur->href != NULL) xmlFree((char *) cur->href); |
| 216 | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 217 | memset(cur, -1, sizeof(xmlNs)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 218 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 221 | /** |
| 222 | * xmlFreeNsList: |
| 223 | * @cur: the first namespace pointer |
| 224 | * |
| 225 | * Free up all the structures associated to the chained namespaces. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 226 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 227 | void |
| 228 | xmlFreeNsList(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 229 | xmlNsPtr next; |
| 230 | if (cur == NULL) { |
| 231 | fprintf(stderr, "xmlFreeNsList : ns == NULL\n"); |
| 232 | return; |
| 233 | } |
| 234 | while (cur != NULL) { |
| 235 | next = cur->next; |
| 236 | xmlFreeNs(cur); |
| 237 | cur = next; |
| 238 | } |
| 239 | } |
| 240 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 241 | /** |
| 242 | * xmlNewDtd: |
| 243 | * @doc: the document pointer |
| 244 | * @name: the DTD name |
| 245 | * @ExternalID: the external ID |
| 246 | * @SystemID: the system ID |
| 247 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 248 | * Creation of a new DTD. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 249 | * Returns a pointer to the new DTD structure |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 250 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 251 | xmlDtdPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 252 | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
| 253 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 254 | xmlDtdPtr cur; |
| 255 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 256 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 257 | fprintf(stderr, "xmlNewDtd(%s): document %s already have a DTD %s\n", |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 258 | /* !!! */ (char *) name, doc->name, |
| 259 | /* !!! */ (char *)doc->extSubset->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Allocate a new DTD and fill the fields. |
| 264 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 265 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 266 | if (cur == NULL) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 267 | fprintf(stderr, "xmlNewDtd : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 268 | return(NULL); |
| 269 | } |
| 270 | |
| 271 | if (name != NULL) |
| 272 | cur->name = xmlStrdup(name); |
| 273 | else |
| 274 | cur->name = NULL; |
| 275 | if (ExternalID != NULL) |
| 276 | cur->ExternalID = xmlStrdup(ExternalID); |
| 277 | else |
| 278 | cur->ExternalID = NULL; |
| 279 | if (SystemID != NULL) |
| 280 | cur->SystemID = xmlStrdup(SystemID); |
| 281 | else |
| 282 | cur->SystemID = NULL; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 283 | cur->notations = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 284 | cur->elements = NULL; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 285 | cur->attributes = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 286 | cur->entities = NULL; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 287 | if (doc != NULL) |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 288 | doc->extSubset = cur; |
| 289 | |
| 290 | return(cur); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * xmlCreateIntSubset: |
| 295 | * @doc: the document pointer |
| 296 | * @name: the DTD name |
| 297 | * @ExternalID: the external ID |
| 298 | * @SystemID: the system ID |
| 299 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 300 | * Create the internal subset of a document |
| 301 | * Returns a pointer to the new DTD structure |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 302 | */ |
| 303 | xmlDtdPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 304 | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
| 305 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 306 | xmlDtdPtr cur; |
| 307 | |
| 308 | if ((doc != NULL) && (doc->intSubset != NULL)) { |
| 309 | fprintf(stderr, |
| 310 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 311 | doc->name); |
| 312 | return(NULL); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Allocate a new DTD and fill the fields. |
| 317 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 318 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 319 | if (cur == NULL) { |
| 320 | fprintf(stderr, "xmlNewDtd : malloc failed\n"); |
| 321 | return(NULL); |
| 322 | } |
| 323 | |
| 324 | if (name != NULL) |
| 325 | cur->name = xmlStrdup(name); |
| 326 | else |
| 327 | cur->name = NULL; |
| 328 | if (ExternalID != NULL) |
| 329 | cur->ExternalID = xmlStrdup(ExternalID); |
| 330 | else |
| 331 | cur->ExternalID = NULL; |
| 332 | if (SystemID != NULL) |
| 333 | cur->SystemID = xmlStrdup(SystemID); |
| 334 | else |
| 335 | cur->SystemID = NULL; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 336 | cur->notations = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 337 | cur->elements = NULL; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 338 | cur->attributes = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 339 | cur->entities = NULL; |
| 340 | if (doc != NULL) |
| 341 | doc->intSubset = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 342 | |
| 343 | return(cur); |
| 344 | } |
| 345 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 346 | /** |
| 347 | * xmlFreeDtd: |
| 348 | * @cur: the DTD structure to free up |
| 349 | * |
| 350 | * Free a DTD structure. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 351 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 352 | void |
| 353 | xmlFreeDtd(xmlDtdPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 354 | if (cur == NULL) { |
| 355 | fprintf(stderr, "xmlFreeDtd : DTD == NULL\n"); |
| 356 | return; |
| 357 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 358 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 359 | if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID); |
| 360 | if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 361 | if (cur->notations != NULL) |
| 362 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 363 | if (cur->elements != NULL) |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 364 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 365 | if (cur->attributes != NULL) |
| 366 | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 367 | if (cur->entities != NULL) |
| 368 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
| 369 | memset(cur, -1, sizeof(xmlDtd)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 370 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 373 | /** |
| 374 | * xmlNewDoc: |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 375 | * @version: xmlChar string giving the version of XML "1.0" |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 376 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 377 | * Returns a new document |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 378 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 379 | xmlDocPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 380 | xmlNewDoc(const xmlChar *version) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 381 | xmlDocPtr cur; |
| 382 | |
| 383 | if (version == NULL) { |
| 384 | fprintf(stderr, "xmlNewDoc : version == NULL\n"); |
| 385 | return(NULL); |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * Allocate a new document and fill the fields. |
| 390 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 391 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 392 | if (cur == NULL) { |
| 393 | fprintf(stderr, "xmlNewDoc : malloc failed\n"); |
| 394 | return(NULL); |
| 395 | } |
| 396 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 397 | cur->type = XML_DOCUMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 398 | cur->version = xmlStrdup(version); |
| 399 | cur->name = NULL; |
| 400 | cur->root = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 401 | cur->intSubset = NULL; |
| 402 | cur->extSubset = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 403 | cur->oldNs = NULL; |
| 404 | cur->encoding = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 405 | cur->standalone = -1; |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 406 | cur->compression = xmlCompressMode; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 407 | cur->ids = NULL; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 408 | cur->refs = NULL; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 409 | #ifndef XML_WITHOUT_CORBA |
Daniel Veillard | 27fb075 | 1998-10-17 06:47:46 +0000 | [diff] [blame] | 410 | cur->_private = NULL; |
| 411 | cur->vepv = NULL; |
| 412 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 413 | return(cur); |
| 414 | } |
| 415 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 416 | /** |
| 417 | * xmlFreeDoc: |
| 418 | * @cur: pointer to the document |
| 419 | * @: |
| 420 | * |
| 421 | * Free up all the structures used by a document, tree included. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 422 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 423 | void |
| 424 | xmlFreeDoc(xmlDocPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 425 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 426 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 427 | fprintf(stderr, "xmlFreeDoc : document == NULL\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 428 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 429 | return; |
| 430 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 431 | if (cur->version != NULL) xmlFree((char *) cur->version); |
| 432 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 433 | if (cur->encoding != NULL) xmlFree((char *) cur->encoding); |
| 434 | if (cur->root != NULL) xmlFreeNodeList(cur->root); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 435 | if (cur->intSubset != NULL) xmlFreeDtd(cur->intSubset); |
| 436 | if (cur->extSubset != NULL) xmlFreeDtd(cur->extSubset); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 437 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 438 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 439 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 440 | memset(cur, -1, sizeof(xmlDoc)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 441 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 444 | /** |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 445 | * xmlStringLenGetNodeList: |
| 446 | * @doc: the document |
| 447 | * @value: the value of the text |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 448 | * @len: the length of the string value |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 449 | * |
| 450 | * Parse the value string and build the node list associated. Should |
| 451 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 452 | * Returns a pointer to the first child |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 453 | */ |
| 454 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 455 | xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 456 | xmlNodePtr ret = NULL, last = NULL; |
| 457 | xmlNodePtr node; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 458 | xmlChar *val; |
| 459 | const xmlChar *cur = value; |
| 460 | const xmlChar *q; |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 461 | xmlEntityPtr ent; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 462 | |
| 463 | if (value == NULL) return(NULL); |
| 464 | |
| 465 | q = cur; |
| 466 | while ((*cur != 0) && (cur - value < len)) { |
| 467 | if (*cur == '&') { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 468 | /* |
| 469 | * Save the current text. |
| 470 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 471 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 472 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 473 | xmlNodeAddContentLen(last, q, cur - q); |
| 474 | } else { |
| 475 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 476 | if (node == NULL) return(ret); |
| 477 | if (last == NULL) |
| 478 | last = ret = node; |
| 479 | else { |
| 480 | last->next = node; |
| 481 | node->prev = last; |
| 482 | last = node; |
| 483 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 486 | /* |
| 487 | * Read the entity string |
| 488 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 489 | cur++; |
| 490 | q = cur; |
| 491 | while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++; |
| 492 | if ((*cur == 0) || (cur - value >= len)) { |
| 493 | fprintf(stderr, |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 494 | "xmlStringLenGetNodeList: unterminated entity %30s\n", q); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 495 | return(ret); |
| 496 | } |
| 497 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 498 | /* |
| 499 | * Predefined entities don't generate nodes |
| 500 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 501 | val = xmlStrndup(q, cur - q); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 502 | ent = xmlGetDocEntity(doc, val); |
| 503 | if ((ent != NULL) && |
| 504 | (ent->type == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 505 | if (last == NULL) { |
| 506 | node = xmlNewDocText(doc, ent->content); |
| 507 | last = ret = node; |
| 508 | } else |
| 509 | xmlNodeAddContent(last, ent->content); |
| 510 | |
| 511 | } else { |
| 512 | /* |
| 513 | * Create a new REFERENCE_REF node |
| 514 | */ |
| 515 | node = xmlNewReference(doc, val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 516 | if (node == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 517 | if (val != NULL) xmlFree(val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 518 | return(ret); |
| 519 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 520 | if (last == NULL) |
| 521 | last = ret = node; |
| 522 | else { |
| 523 | last->next = node; |
| 524 | node->prev = last; |
| 525 | last = node; |
| 526 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 527 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 528 | xmlFree(val); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 529 | } |
| 530 | cur++; |
| 531 | q = cur; |
| 532 | } else |
| 533 | cur++; |
| 534 | } |
| 535 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 536 | /* |
| 537 | * Handle the last piece of text. |
| 538 | */ |
| 539 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 540 | xmlNodeAddContentLen(last, q, cur - q); |
| 541 | } else { |
| 542 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 543 | if (node == NULL) return(ret); |
| 544 | if (last == NULL) |
| 545 | last = ret = node; |
| 546 | else { |
| 547 | last->next = node; |
| 548 | node->prev = last; |
| 549 | last = node; |
| 550 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | return(ret); |
| 554 | } |
| 555 | |
| 556 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 557 | * xmlStringGetNodeList: |
| 558 | * @doc: the document |
| 559 | * @value: the value of the attribute |
| 560 | * |
| 561 | * Parse the value string and build the node list associated. Should |
| 562 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 563 | * Returns a pointer to the first child |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 564 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 565 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 566 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 567 | xmlNodePtr ret = NULL, last = NULL; |
| 568 | xmlNodePtr node; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 569 | xmlChar *val; |
| 570 | const xmlChar *cur = value; |
| 571 | const xmlChar *q; |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 572 | xmlEntityPtr ent; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 573 | |
| 574 | if (value == NULL) return(NULL); |
| 575 | |
| 576 | q = cur; |
| 577 | while (*cur != 0) { |
| 578 | if (*cur == '&') { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 579 | /* |
| 580 | * Save the current text. |
| 581 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 582 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 583 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 584 | xmlNodeAddContentLen(last, q, cur - q); |
| 585 | } else { |
| 586 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 587 | if (node == NULL) return(ret); |
| 588 | if (last == NULL) |
| 589 | last = ret = node; |
| 590 | else { |
| 591 | last->next = node; |
| 592 | node->prev = last; |
| 593 | last = node; |
| 594 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 597 | /* |
| 598 | * Read the entity string |
| 599 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 600 | cur++; |
| 601 | q = cur; |
| 602 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 603 | if (*cur == 0) { |
| 604 | fprintf(stderr, |
| 605 | "xmlStringGetNodeList: unterminated entity %30s\n", q); |
| 606 | return(ret); |
| 607 | } |
| 608 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 609 | /* |
| 610 | * Predefined entities don't generate nodes |
| 611 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 612 | val = xmlStrndup(q, cur - q); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 613 | ent = xmlGetDocEntity(doc, val); |
| 614 | if ((ent != NULL) && |
| 615 | (ent->type == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 616 | if (last == NULL) { |
| 617 | node = xmlNewDocText(doc, ent->content); |
| 618 | last = ret = node; |
| 619 | } else |
| 620 | xmlNodeAddContent(last, ent->content); |
| 621 | |
| 622 | } else { |
| 623 | /* |
| 624 | * Create a new REFERENCE_REF node |
| 625 | */ |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 626 | node = xmlNewReference(doc, val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 627 | if (node == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 628 | if (val != NULL) xmlFree(val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 629 | return(ret); |
| 630 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 631 | if (last == NULL) |
| 632 | last = ret = node; |
| 633 | else { |
| 634 | last->next = node; |
| 635 | node->prev = last; |
| 636 | last = node; |
| 637 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 638 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 639 | xmlFree(val); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 640 | } |
| 641 | cur++; |
| 642 | q = cur; |
| 643 | } else |
| 644 | cur++; |
| 645 | } |
| 646 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 647 | /* |
| 648 | * Handle the last piece of text. |
| 649 | */ |
| 650 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 651 | xmlNodeAddContentLen(last, q, cur - q); |
| 652 | } else { |
| 653 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 654 | if (node == NULL) return(ret); |
| 655 | if (last == NULL) |
| 656 | last = ret = node; |
| 657 | else { |
| 658 | last->next = node; |
| 659 | node->prev = last; |
| 660 | last = node; |
| 661 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | return(ret); |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * xmlNodeListGetString: |
| 669 | * @doc: the document |
| 670 | * @list: a Node list |
| 671 | * @inLine: should we replace entity contents or show their external form |
| 672 | * |
| 673 | * Returns the string equivalent to the text contained in the Node list |
| 674 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 675 | * Returns a pointer to the string copy, the calller must free it. |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 676 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 677 | xmlChar * |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 678 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 679 | xmlNodePtr node = list; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 680 | xmlChar *ret = NULL; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 681 | xmlEntityPtr ent; |
| 682 | |
| 683 | if (list == NULL) return(NULL); |
| 684 | |
| 685 | while (node != NULL) { |
| 686 | if (node->type == XML_TEXT_NODE) { |
Daniel Veillard | 3500838 | 1999-10-25 13:15:52 +0000 | [diff] [blame^] | 687 | if ((inLine) || (doc->type == XML_HTML_DOCUMENT_NODE)) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 688 | ret = xmlStrcat(ret, node->content); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 689 | else { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 690 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 691 | |
| 692 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
| 693 | if (buffer != NULL) { |
| 694 | ret = xmlStrcat(ret, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 695 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 698 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 699 | if (inLine) { |
| 700 | ent = xmlGetDocEntity(doc, node->name); |
| 701 | if (ent != NULL) |
| 702 | ret = xmlStrcat(ret, ent->content); |
| 703 | else |
| 704 | ret = xmlStrcat(ret, node->content); |
| 705 | } else { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 706 | xmlChar buf[2]; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 707 | buf[0] = '&'; buf[1] = 0; |
| 708 | ret = xmlStrncat(ret, buf, 1); |
| 709 | ret = xmlStrcat(ret, node->name); |
| 710 | buf[0] = ';'; buf[1] = 0; |
| 711 | ret = xmlStrncat(ret, buf, 1); |
| 712 | } |
| 713 | } |
| 714 | #if 0 |
| 715 | else { |
| 716 | fprintf(stderr, "xmlGetNodeListString : invalide node type %d\n", |
| 717 | node->type); |
| 718 | } |
| 719 | #endif |
| 720 | node = node->next; |
| 721 | } |
| 722 | return(ret); |
| 723 | } |
| 724 | |
| 725 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 726 | * xmlNewProp: |
| 727 | * @node: the holding node |
| 728 | * @name: the name of the attribute |
| 729 | * @value: the value of the attribute |
| 730 | * |
| 731 | * Create a new property carried by a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 732 | * Returns a pointer to the attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 733 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 734 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 735 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 736 | xmlAttrPtr cur; |
| 737 | |
| 738 | if (name == NULL) { |
| 739 | fprintf(stderr, "xmlNewProp : name == NULL\n"); |
| 740 | return(NULL); |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * Allocate a new property and fill the fields. |
| 745 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 746 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 747 | if (cur == NULL) { |
| 748 | fprintf(stderr, "xmlNewProp : malloc failed\n"); |
| 749 | return(NULL); |
| 750 | } |
| 751 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 752 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 753 | cur->node = node; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 754 | cur->ns = NULL; |
| 755 | cur->name = xmlStrdup(name); |
| 756 | if (value != NULL) |
| 757 | cur->val = xmlStringGetNodeList(node->doc, value); |
| 758 | else |
| 759 | cur->val = NULL; |
| 760 | #ifndef XML_WITHOUT_CORBA |
| 761 | cur->_private = NULL; |
| 762 | cur->vepv = NULL; |
| 763 | #endif |
| 764 | |
| 765 | /* |
| 766 | * Add it at the end to preserve parsing order ... |
| 767 | */ |
| 768 | cur->next = NULL; |
| 769 | if (node != NULL) { |
| 770 | if (node->properties == NULL) { |
| 771 | node->properties = cur; |
| 772 | } else { |
| 773 | xmlAttrPtr prev = node->properties; |
| 774 | |
| 775 | while (prev->next != NULL) prev = prev->next; |
| 776 | prev->next = cur; |
| 777 | } |
| 778 | } |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 779 | #ifndef XML_WITHOUT_CORBA |
| 780 | cur->_private = NULL; |
| 781 | cur->vepv = NULL; |
| 782 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 783 | return(cur); |
| 784 | } |
| 785 | |
| 786 | /** |
| 787 | * xmlNewNsProp: |
| 788 | * @node: the holding node |
| 789 | * @ns: the namespace |
| 790 | * @name: the name of the attribute |
| 791 | * @value: the value of the attribute |
| 792 | * |
| 793 | * Create a new property tagged with a namespace and carried by a node. |
| 794 | * Returns a pointer to the attribute |
| 795 | */ |
| 796 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 797 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 798 | const xmlChar *value) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 799 | xmlAttrPtr cur; |
| 800 | |
| 801 | if (name == NULL) { |
| 802 | fprintf(stderr, "xmlNewProp : name == NULL\n"); |
| 803 | return(NULL); |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * Allocate a new property and fill the fields. |
| 808 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 809 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 810 | if (cur == NULL) { |
| 811 | fprintf(stderr, "xmlNewProp : malloc failed\n"); |
| 812 | return(NULL); |
| 813 | } |
| 814 | |
| 815 | cur->type = XML_ATTRIBUTE_NODE; |
| 816 | cur->node = node; |
| 817 | cur->ns = ns; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 818 | cur->name = xmlStrdup(name); |
| 819 | if (value != NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 820 | cur->val = xmlStringGetNodeList(node->doc, value); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 821 | else |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 822 | cur->val = NULL; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 823 | #ifndef XML_WITHOUT_CORBA |
Daniel Veillard | 27fb075 | 1998-10-17 06:47:46 +0000 | [diff] [blame] | 824 | cur->_private = NULL; |
| 825 | cur->vepv = NULL; |
| 826 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 827 | |
| 828 | /* |
| 829 | * Add it at the end to preserve parsing order ... |
| 830 | */ |
| 831 | cur->next = NULL; |
| 832 | if (node != NULL) { |
| 833 | if (node->properties == NULL) { |
| 834 | node->properties = cur; |
| 835 | } else { |
| 836 | xmlAttrPtr prev = node->properties; |
| 837 | |
| 838 | while (prev->next != NULL) prev = prev->next; |
| 839 | prev->next = cur; |
| 840 | } |
| 841 | } |
| 842 | return(cur); |
| 843 | } |
| 844 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 845 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 846 | * xmlNewDocProp: |
| 847 | * @doc: the document |
| 848 | * @name: the name of the attribute |
| 849 | * @value: the value of the attribute |
| 850 | * |
| 851 | * Create a new property carried by a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 852 | * Returns a pointer to the attribute |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 853 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 854 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 855 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 856 | xmlAttrPtr cur; |
| 857 | |
| 858 | if (name == NULL) { |
| 859 | fprintf(stderr, "xmlNewProp : name == NULL\n"); |
| 860 | return(NULL); |
| 861 | } |
| 862 | |
| 863 | /* |
| 864 | * Allocate a new property and fill the fields. |
| 865 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 866 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 867 | if (cur == NULL) { |
| 868 | fprintf(stderr, "xmlNewProp : malloc failed\n"); |
| 869 | return(NULL); |
| 870 | } |
| 871 | |
| 872 | cur->type = XML_ATTRIBUTE_NODE; |
| 873 | cur->node = NULL; |
| 874 | cur->name = xmlStrdup(name); |
| 875 | if (value != NULL) |
| 876 | cur->val = xmlStringGetNodeList(doc, value); |
| 877 | else |
| 878 | cur->val = NULL; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 879 | #ifndef XML_WITHOUT_CORBA |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 880 | cur->_private = NULL; |
| 881 | cur->vepv = NULL; |
| 882 | #endif |
| 883 | |
| 884 | cur->next = NULL; |
| 885 | return(cur); |
| 886 | } |
| 887 | |
| 888 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 889 | * xmlFreePropList: |
| 890 | * @cur: the first property in the list |
| 891 | * |
| 892 | * 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] | 893 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 894 | void |
| 895 | xmlFreePropList(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 896 | xmlAttrPtr next; |
| 897 | if (cur == NULL) { |
| 898 | fprintf(stderr, "xmlFreePropList : property == NULL\n"); |
| 899 | return; |
| 900 | } |
| 901 | while (cur != NULL) { |
| 902 | next = cur->next; |
| 903 | xmlFreeProp(cur); |
| 904 | cur = next; |
| 905 | } |
| 906 | } |
| 907 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 908 | /** |
| 909 | * xmlFreeProp: |
| 910 | * @cur: the first property in the list |
| 911 | * |
| 912 | * Free one property, all the childs are freed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 913 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 914 | void |
| 915 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 916 | if (cur == NULL) { |
| 917 | fprintf(stderr, "xmlFreeProp : property == NULL\n"); |
| 918 | return; |
| 919 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 920 | if (cur->name != NULL) xmlFree((char *) cur->name); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 921 | if (cur->val != NULL) xmlFreeNodeList(cur->val); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 922 | memset(cur, -1, sizeof(xmlAttr)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 923 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 926 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 927 | * xmlNewPI: |
| 928 | * @name: the processing instruction name |
| 929 | * @content: the PI content |
| 930 | * |
| 931 | * Creation of a processing instruction element. |
| 932 | * Returns a pointer to the new node object. |
| 933 | */ |
| 934 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 935 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 936 | xmlNodePtr cur; |
| 937 | |
| 938 | if (name == NULL) { |
| 939 | fprintf(stderr, "xmlNewPI : name == NULL\n"); |
| 940 | return(NULL); |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * Allocate a new node and fill the fields. |
| 945 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 946 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 947 | if (cur == NULL) { |
| 948 | fprintf(stderr, "xmlNewPI : malloc failed\n"); |
| 949 | return(NULL); |
| 950 | } |
| 951 | |
| 952 | cur->type = XML_PI_NODE; |
| 953 | cur->doc = NULL; |
| 954 | cur->parent = NULL; |
| 955 | cur->next = NULL; |
| 956 | cur->prev = NULL; |
| 957 | cur->childs = NULL; |
| 958 | cur->last = NULL; |
| 959 | cur->properties = NULL; |
| 960 | cur->name = xmlStrdup(name); |
| 961 | cur->ns = NULL; |
| 962 | cur->nsDef = NULL; |
| 963 | if (content != NULL) |
| 964 | cur->content = xmlStrdup(content); |
| 965 | else |
| 966 | cur->content = NULL; |
| 967 | #ifndef XML_WITHOUT_CORBA |
| 968 | cur->_private = NULL; |
| 969 | cur->vepv = NULL; |
| 970 | #endif |
| 971 | return(cur); |
| 972 | } |
| 973 | |
| 974 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 975 | * xmlNewNode: |
| 976 | * @ns: namespace if any |
| 977 | * @name: the node name |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 978 | * |
| 979 | * Creation of a new node element. @ns and @content are optionnal (NULL). |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 980 | * If content is non NULL, a child list containing the TEXTs and |
| 981 | * ENTITY_REFs node will be created. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 982 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 983 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 984 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 985 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 986 | xmlNodePtr cur; |
| 987 | |
| 988 | if (name == NULL) { |
| 989 | fprintf(stderr, "xmlNewNode : name == NULL\n"); |
| 990 | return(NULL); |
| 991 | } |
| 992 | |
| 993 | /* |
| 994 | * Allocate a new node and fill the fields. |
| 995 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 996 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 997 | if (cur == NULL) { |
| 998 | fprintf(stderr, "xmlNewNode : malloc failed\n"); |
| 999 | return(NULL); |
| 1000 | } |
| 1001 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1002 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1003 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1004 | cur->parent = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1005 | cur->next = NULL; |
| 1006 | cur->prev = NULL; |
| 1007 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1008 | cur->last = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1009 | cur->properties = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1010 | cur->name = xmlStrdup(name); |
| 1011 | cur->ns = ns; |
| 1012 | cur->nsDef = NULL; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1013 | cur->content = NULL; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 1014 | #ifndef XML_WITHOUT_CORBA |
Daniel Veillard | 27fb075 | 1998-10-17 06:47:46 +0000 | [diff] [blame] | 1015 | cur->_private = NULL; |
| 1016 | cur->vepv = NULL; |
| 1017 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1018 | return(cur); |
| 1019 | } |
| 1020 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1021 | /** |
| 1022 | * xmlNewDocNode: |
| 1023 | * @doc: the document |
| 1024 | * @ns: namespace if any |
| 1025 | * @name: the node name |
| 1026 | * @content: the text content if any |
| 1027 | * |
| 1028 | * Creation of a new node element within a document. @ns and @content |
| 1029 | * are optionnal (NULL). |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1030 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1031 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1032 | xmlNodePtr |
| 1033 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1034 | const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1035 | xmlNodePtr cur; |
| 1036 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1037 | cur = xmlNewNode(ns, name); |
| 1038 | if (cur != NULL) { |
| 1039 | cur->doc = doc; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1040 | if (content != NULL) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1041 | cur->childs = xmlStringGetNodeList(doc, content); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1042 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1043 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1044 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1045 | return(cur); |
| 1046 | } |
| 1047 | |
| 1048 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1049 | /** |
| 1050 | * xmlNewText: |
| 1051 | * @content: the text content |
| 1052 | * |
| 1053 | * Creation of a new text node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1054 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1055 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1056 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1057 | xmlNewText(const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1058 | xmlNodePtr cur; |
| 1059 | |
| 1060 | /* |
| 1061 | * Allocate a new node and fill the fields. |
| 1062 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1063 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1064 | if (cur == NULL) { |
| 1065 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 1066 | return(NULL); |
| 1067 | } |
| 1068 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1069 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1070 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1071 | cur->parent = NULL; |
| 1072 | cur->next = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1073 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1074 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1075 | cur->last = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1076 | cur->properties = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1077 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1078 | cur->name = xmlStrdup(xmlStringText); |
| 1079 | cur->ns = NULL; |
| 1080 | cur->nsDef = NULL; |
| 1081 | if (content != NULL) |
| 1082 | cur->content = xmlStrdup(content); |
| 1083 | else |
| 1084 | cur->content = NULL; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 1085 | #ifndef XML_WITHOUT_CORBA |
| 1086 | cur->_private = NULL; |
| 1087 | cur->vepv = NULL; |
| 1088 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1089 | return(cur); |
| 1090 | } |
| 1091 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1092 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1093 | * xmlNewReference: |
| 1094 | * @doc: the document |
| 1095 | * @name: the reference name, or the reference string with & and ; |
| 1096 | * |
| 1097 | * Creation of a new reference node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1098 | * Returns a pointer to the new node object. |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1099 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1100 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1101 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1102 | xmlNodePtr cur; |
| 1103 | xmlEntityPtr ent; |
| 1104 | |
| 1105 | /* |
| 1106 | * Allocate a new node and fill the fields. |
| 1107 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1108 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1109 | if (cur == NULL) { |
| 1110 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 1111 | return(NULL); |
| 1112 | } |
| 1113 | |
| 1114 | cur->type = XML_ENTITY_REF_NODE; |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 1115 | cur->doc = doc; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1116 | cur->parent = NULL; |
| 1117 | cur->next = NULL; |
| 1118 | cur->prev = NULL; |
| 1119 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1120 | cur->last = NULL; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1121 | cur->properties = NULL; |
| 1122 | if (name[0] == '&') { |
| 1123 | int len; |
| 1124 | name++; |
| 1125 | len = xmlStrlen(name); |
| 1126 | if (name[len - 1] == ';') |
| 1127 | cur->name = xmlStrndup(name, len - 1); |
| 1128 | else |
| 1129 | cur->name = xmlStrndup(name, len); |
| 1130 | } else |
| 1131 | cur->name = xmlStrdup(name); |
| 1132 | cur->ns = NULL; |
| 1133 | cur->nsDef = NULL; |
| 1134 | |
| 1135 | ent = xmlGetDocEntity(doc, cur->name); |
| 1136 | if (ent != NULL) |
| 1137 | cur->content = ent->content; |
| 1138 | else |
| 1139 | cur->content = NULL; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 1140 | #ifndef XML_WITHOUT_CORBA |
| 1141 | cur->_private = NULL; |
| 1142 | cur->vepv = NULL; |
| 1143 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1144 | return(cur); |
| 1145 | } |
| 1146 | |
| 1147 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1148 | * xmlNewDocText: |
| 1149 | * @doc: the document |
| 1150 | * @content: the text content |
| 1151 | * |
| 1152 | * Creation of a new text node within a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1153 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1154 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1155 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1156 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1157 | xmlNodePtr cur; |
| 1158 | |
| 1159 | cur = xmlNewText(content); |
| 1160 | if (cur != NULL) cur->doc = doc; |
| 1161 | return(cur); |
| 1162 | } |
| 1163 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1164 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1165 | * xmlNewTextLen: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1166 | * @content: the text content |
| 1167 | * @len: the text len. |
| 1168 | * |
| 1169 | * Creation of a new text node with an extra parameter for the content's lenght |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1170 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1171 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1172 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1173 | xmlNewTextLen(const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1174 | xmlNodePtr cur; |
| 1175 | |
| 1176 | /* |
| 1177 | * Allocate a new node and fill the fields. |
| 1178 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1179 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1180 | if (cur == NULL) { |
| 1181 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 1182 | return(NULL); |
| 1183 | } |
| 1184 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1185 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1186 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1187 | cur->parent = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1188 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1189 | cur->next = NULL; |
| 1190 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1191 | cur->last = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1192 | cur->properties = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1193 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1194 | cur->name = xmlStrdup(xmlStringText); |
| 1195 | cur->ns = NULL; |
| 1196 | cur->nsDef = NULL; |
| 1197 | if (content != NULL) |
| 1198 | cur->content = xmlStrndup(content, len); |
| 1199 | else |
| 1200 | cur->content = NULL; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 1201 | #ifndef XML_WITHOUT_CORBA |
| 1202 | cur->_private = NULL; |
| 1203 | cur->vepv = NULL; |
| 1204 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1205 | return(cur); |
| 1206 | } |
| 1207 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1208 | /** |
| 1209 | * xmlNewDocTextLen: |
| 1210 | * @doc: the document |
| 1211 | * @content: the text content |
| 1212 | * @len: the text len. |
| 1213 | * |
| 1214 | * Creation of a new text node with an extra content lenght parameter. The |
| 1215 | * text node pertain to a given document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1216 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1217 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1218 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1219 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1220 | xmlNodePtr cur; |
| 1221 | |
| 1222 | cur = xmlNewTextLen(content, len); |
| 1223 | if (cur != NULL) cur->doc = doc; |
| 1224 | return(cur); |
| 1225 | } |
| 1226 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1227 | /** |
| 1228 | * xmlNewComment: |
| 1229 | * @content: the comment content |
| 1230 | * |
| 1231 | * Creation of a new node containing a comment. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1232 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1233 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1234 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1235 | xmlNewComment(const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1236 | xmlNodePtr cur; |
| 1237 | |
| 1238 | /* |
| 1239 | * Allocate a new node and fill the fields. |
| 1240 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1241 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1242 | if (cur == NULL) { |
| 1243 | fprintf(stderr, "xmlNewComment : malloc failed\n"); |
| 1244 | return(NULL); |
| 1245 | } |
| 1246 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1247 | cur->type = XML_COMMENT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1248 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1249 | cur->parent = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1250 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1251 | cur->next = NULL; |
| 1252 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1253 | cur->last = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1254 | cur->properties = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1255 | cur->type = XML_COMMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1256 | cur->name = xmlStrdup(xmlStringText); |
| 1257 | cur->ns = NULL; |
| 1258 | cur->nsDef = NULL; |
| 1259 | if (content != NULL) |
| 1260 | cur->content = xmlStrdup(content); |
| 1261 | else |
| 1262 | cur->content = NULL; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 1263 | #ifndef XML_WITHOUT_CORBA |
| 1264 | cur->_private = NULL; |
| 1265 | cur->vepv = NULL; |
| 1266 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1267 | return(cur); |
| 1268 | } |
| 1269 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1270 | /** |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1271 | * xmlNewCDataBlock: |
| 1272 | * @doc: the document |
| 1273 | * @content: the CData block content content |
| 1274 | * @len: the length of the block |
| 1275 | * |
| 1276 | * Creation of a new node containing a CData block. |
| 1277 | * Returns a pointer to the new node object. |
| 1278 | */ |
| 1279 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1280 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1281 | xmlNodePtr cur; |
| 1282 | |
| 1283 | /* |
| 1284 | * Allocate a new node and fill the fields. |
| 1285 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1286 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1287 | if (cur == NULL) { |
| 1288 | fprintf(stderr, "xmlNewCDataBlock : malloc failed\n"); |
| 1289 | return(NULL); |
| 1290 | } |
| 1291 | |
| 1292 | cur->type = XML_CDATA_SECTION_NODE; |
| 1293 | cur->doc = NULL; |
| 1294 | cur->parent = NULL; |
| 1295 | cur->prev = NULL; |
| 1296 | cur->next = NULL; |
| 1297 | cur->childs = NULL; |
| 1298 | cur->last = NULL; |
| 1299 | cur->properties = NULL; |
| 1300 | cur->name = xmlStrdup(xmlStringText); |
| 1301 | cur->ns = NULL; |
| 1302 | cur->nsDef = NULL; |
| 1303 | if ((content != NULL) && (len > 0)) { |
| 1304 | cur->content = xmlStrndup(content, len); |
| 1305 | } else |
| 1306 | cur->content = NULL; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 1307 | #ifndef XML_WITHOUT_CORBA |
| 1308 | cur->_private = NULL; |
| 1309 | cur->vepv = NULL; |
| 1310 | #endif |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1311 | return(cur); |
| 1312 | } |
| 1313 | |
| 1314 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1315 | * xmlNewDocComment: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1316 | * @doc: the document |
| 1317 | * @content: the comment content |
| 1318 | * |
| 1319 | * Creation of a new node containing a commentwithin a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1320 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1321 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1322 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1323 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1324 | xmlNodePtr cur; |
| 1325 | |
| 1326 | cur = xmlNewComment(content); |
| 1327 | if (cur != NULL) cur->doc = doc; |
| 1328 | return(cur); |
| 1329 | } |
| 1330 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1331 | /** |
| 1332 | * xmlNewChild: |
| 1333 | * @parent: the parent node |
| 1334 | * @ns: a namespace if any |
| 1335 | * @name: the name of the child |
| 1336 | * @content: the content of the child if any. |
| 1337 | * |
| 1338 | * |
| 1339 | * 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] | 1340 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1341 | * a child list containing the TEXTs and ENTITY_REFs node will be created. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1342 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1343 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1344 | xmlNodePtr |
| 1345 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1346 | const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1347 | xmlNodePtr cur, prev; |
| 1348 | |
| 1349 | if (parent == NULL) { |
| 1350 | fprintf(stderr, "xmlNewChild : parent == NULL\n"); |
| 1351 | return(NULL); |
| 1352 | } |
| 1353 | |
| 1354 | if (name == NULL) { |
| 1355 | fprintf(stderr, "xmlNewChild : name == NULL\n"); |
| 1356 | return(NULL); |
| 1357 | } |
| 1358 | |
| 1359 | /* |
| 1360 | * Allocate a new node |
| 1361 | */ |
| 1362 | if (ns == NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1363 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1364 | else |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1365 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1366 | if (cur == NULL) return(NULL); |
| 1367 | |
| 1368 | /* |
| 1369 | * add the new element at the end of the childs list. |
| 1370 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1371 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1372 | cur->parent = parent; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1373 | cur->doc = parent->doc; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1374 | if (parent->childs == NULL) { |
| 1375 | parent->childs = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1376 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1377 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1378 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1379 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1380 | cur->prev = prev; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1381 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | return(cur); |
| 1385 | } |
| 1386 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1387 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1388 | * xmlAddSibling: |
| 1389 | * @cur: the child node |
| 1390 | * @elem: the new node |
| 1391 | * |
| 1392 | * Add a new element to the list of siblings of @cur |
| 1393 | * Returns the element or NULL in case of error. |
| 1394 | */ |
| 1395 | xmlNodePtr |
| 1396 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1397 | xmlNodePtr parent; |
| 1398 | |
| 1399 | if (cur == NULL) { |
| 1400 | fprintf(stderr, "xmlAddSibling : cur == NULL\n"); |
| 1401 | return(NULL); |
| 1402 | } |
| 1403 | |
| 1404 | if (elem == NULL) { |
| 1405 | fprintf(stderr, "xmlAddSibling : elem == NULL\n"); |
| 1406 | return(NULL); |
| 1407 | } |
| 1408 | |
| 1409 | if ((cur->doc != NULL) && (elem->doc != NULL) && |
| 1410 | (cur->doc != elem->doc)) { |
| 1411 | fprintf(stderr, |
| 1412 | "xmlAddSibling: Elements moved to a different document\n"); |
| 1413 | } |
| 1414 | |
| 1415 | while (cur->next != NULL) cur = cur->next; |
| 1416 | |
| 1417 | if (elem->doc == NULL) |
| 1418 | elem->doc = cur->doc; /* the parent may not be linked to a doc ! */ |
| 1419 | |
| 1420 | parent = cur->parent; |
| 1421 | elem->prev = cur; |
| 1422 | elem->next = NULL; |
| 1423 | elem->parent = parent; |
| 1424 | cur->next = elem; |
| 1425 | if (parent != NULL) |
| 1426 | parent->last = elem; |
| 1427 | |
| 1428 | return(elem); |
| 1429 | } |
| 1430 | |
| 1431 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1432 | * xmlAddChild: |
| 1433 | * @parent: the parent node |
| 1434 | * @cur: the child node |
| 1435 | * |
| 1436 | * Add a new child element, to @parent, at the end of the child list. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1437 | * Returns the child or NULL in case of error. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1438 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1439 | xmlNodePtr |
| 1440 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1441 | xmlNodePtr prev; |
| 1442 | |
| 1443 | if (parent == NULL) { |
| 1444 | fprintf(stderr, "xmladdChild : parent == NULL\n"); |
| 1445 | return(NULL); |
| 1446 | } |
| 1447 | |
| 1448 | if (cur == NULL) { |
| 1449 | fprintf(stderr, "xmladdChild : child == NULL\n"); |
| 1450 | return(NULL); |
| 1451 | } |
| 1452 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1453 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 1454 | (cur->doc != parent->doc)) { |
| 1455 | fprintf(stderr, "Elements moved to a different document\n"); |
| 1456 | } |
| 1457 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1458 | /* |
| 1459 | * add the new element at the end of the childs list. |
| 1460 | */ |
| 1461 | cur->parent = parent; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1462 | cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1463 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1464 | /* |
| 1465 | * Handle the case where parent->content != NULL, in that case it will |
| 1466 | * create a intermediate TEXT node. |
| 1467 | */ |
| 1468 | if (parent->content != NULL) { |
| 1469 | xmlNodePtr text; |
| 1470 | |
| 1471 | text = xmlNewDocText(parent->doc, parent->content); |
| 1472 | if (text != NULL) { |
| 1473 | text->next = parent->childs; |
| 1474 | if (text->next != NULL) |
| 1475 | text->next->prev = text; |
| 1476 | parent->childs = text; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1477 | UPDATE_LAST_CHILD(parent) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1478 | xmlFree(parent->content); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1479 | parent->content = NULL; |
| 1480 | } |
| 1481 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1482 | if (parent->childs == NULL) { |
| 1483 | parent->childs = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1484 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1485 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1486 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1487 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1488 | cur->prev = prev; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1489 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | return(cur); |
| 1493 | } |
| 1494 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1495 | /** |
| 1496 | * xmlGetLastChild: |
| 1497 | * @parent: the parent node |
| 1498 | * |
| 1499 | * Search the last child of a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1500 | * Returns the last child or NULL if none. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1501 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1502 | xmlNodePtr |
| 1503 | xmlGetLastChild(xmlNodePtr parent) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1504 | if (parent == NULL) { |
| 1505 | fprintf(stderr, "xmlGetLastChild : parent == NULL\n"); |
| 1506 | return(NULL); |
| 1507 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1508 | return(parent->last); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1511 | /** |
| 1512 | * xmlFreeNodeList: |
| 1513 | * @cur: the first node in the list |
| 1514 | * |
| 1515 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 1516 | * the childs are freed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1517 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1518 | void |
| 1519 | xmlFreeNodeList(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1520 | xmlNodePtr next; |
| 1521 | if (cur == NULL) { |
| 1522 | fprintf(stderr, "xmlFreeNodeList : node == NULL\n"); |
| 1523 | return; |
| 1524 | } |
| 1525 | while (cur != NULL) { |
| 1526 | next = cur->next; |
| 1527 | xmlFreeNode(cur); |
| 1528 | cur = next; |
| 1529 | } |
| 1530 | } |
| 1531 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1532 | /** |
| 1533 | * xmlFreeNode: |
| 1534 | * @cur: the node |
| 1535 | * |
| 1536 | * 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] | 1537 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1538 | void |
| 1539 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1540 | if (cur == NULL) { |
| 1541 | fprintf(stderr, "xmlFreeNode : node == NULL\n"); |
| 1542 | return; |
| 1543 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1544 | cur->doc = NULL; |
| 1545 | cur->parent = NULL; |
| 1546 | cur->next = NULL; |
| 1547 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1548 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1549 | if (cur->properties != NULL) xmlFreePropList(cur->properties); |
| 1550 | if (cur->type != XML_ENTITY_REF_NODE) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1551 | if (cur->content != NULL) xmlFree(cur->content); |
| 1552 | if (cur->name != NULL) xmlFree((char *) cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1553 | if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef); |
| 1554 | memset(cur, -1, sizeof(xmlNode)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1555 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1558 | /** |
| 1559 | * xmlUnlinkNode: |
| 1560 | * @cur: the node |
| 1561 | * |
| 1562 | * Unlink a node from it's current context, the node is not freed |
| 1563 | */ |
| 1564 | void |
| 1565 | xmlUnlinkNode(xmlNodePtr cur) { |
| 1566 | if (cur == NULL) { |
| 1567 | fprintf(stderr, "xmlUnlinkNode : node == NULL\n"); |
| 1568 | return; |
| 1569 | } |
| 1570 | if ((cur->parent != NULL) && (cur->parent->childs == cur)) |
| 1571 | cur->parent->childs = cur->next; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1572 | if ((cur->parent != NULL) && (cur->parent->last == cur)) |
| 1573 | cur->parent->last = cur->prev; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1574 | if (cur->next != NULL) |
| 1575 | cur->next->prev = cur->prev; |
| 1576 | if (cur->prev != NULL) |
| 1577 | cur->prev->next = cur->next; |
| 1578 | cur->next = cur->prev = NULL; |
| 1579 | cur->parent = NULL; |
| 1580 | } |
| 1581 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1582 | /************************************************************************ |
| 1583 | * * |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1584 | * Copy operations * |
| 1585 | * * |
| 1586 | ************************************************************************/ |
| 1587 | |
| 1588 | /** |
| 1589 | * xmlCopyNamespace: |
| 1590 | * @cur: the namespace |
| 1591 | * |
| 1592 | * Do a copy of the namespace. |
| 1593 | * |
| 1594 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 1595 | */ |
| 1596 | xmlNsPtr |
| 1597 | xmlCopyNamespace(xmlNsPtr cur) { |
| 1598 | xmlNsPtr ret; |
| 1599 | |
| 1600 | if (cur == NULL) return(NULL); |
| 1601 | switch (cur->type) { |
| 1602 | case XML_GLOBAL_NAMESPACE: |
| 1603 | ret = xmlNewGlobalNs(NULL, cur->href, cur->prefix); |
| 1604 | break; |
| 1605 | case XML_LOCAL_NAMESPACE: |
| 1606 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 1607 | break; |
| 1608 | default: |
| 1609 | fprintf(stderr, "xmlCopyNamespace: unknown type %d\n", cur->type); |
| 1610 | return(NULL); |
| 1611 | } |
| 1612 | return(ret); |
| 1613 | } |
| 1614 | |
| 1615 | /** |
| 1616 | * xmlCopyNamespaceList: |
| 1617 | * @cur: the first namespace |
| 1618 | * |
| 1619 | * Do a copy of an namespace list. |
| 1620 | * |
| 1621 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 1622 | */ |
| 1623 | xmlNsPtr |
| 1624 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 1625 | xmlNsPtr ret = NULL; |
| 1626 | xmlNsPtr p = NULL,q; |
| 1627 | |
| 1628 | while (cur != NULL) { |
| 1629 | q = xmlCopyNamespace(cur); |
| 1630 | if (p == NULL) { |
| 1631 | ret = p = q; |
| 1632 | } else { |
| 1633 | p->next = q; |
| 1634 | p = q; |
| 1635 | } |
| 1636 | cur = cur->next; |
| 1637 | } |
| 1638 | return(ret); |
| 1639 | } |
| 1640 | |
| 1641 | /** |
| 1642 | * xmlCopyProp: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1643 | * @target: the element where the attribute will be grafted |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1644 | * @cur: the attribute |
| 1645 | * |
| 1646 | * Do a copy of the attribute. |
| 1647 | * |
| 1648 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 1649 | */ |
| 1650 | xmlAttrPtr |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1651 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1652 | xmlAttrPtr ret; |
| 1653 | |
| 1654 | if (cur == NULL) return(NULL); |
| 1655 | if (cur->val != NULL) |
| 1656 | ret = xmlNewDocProp(cur->val->doc, cur->name, NULL); |
| 1657 | else |
| 1658 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 1659 | if (ret == NULL) return(NULL); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1660 | |
| 1661 | if ((cur->ns != NULL) && (target != NULL)) { |
| 1662 | xmlNsPtr ns; |
| 1663 | |
| 1664 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 1665 | ret->ns = ns; |
| 1666 | } else |
| 1667 | ret->ns = NULL; |
| 1668 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1669 | if (cur->val != NULL) |
| 1670 | ret->val = xmlCopyNodeList(cur->val); |
| 1671 | return(ret); |
| 1672 | } |
| 1673 | |
| 1674 | /** |
| 1675 | * xmlCopyPropList: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1676 | * @target: the element where the attributes will be grafted |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1677 | * @cur: the first attribute |
| 1678 | * |
| 1679 | * Do a copy of an attribute list. |
| 1680 | * |
| 1681 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 1682 | */ |
| 1683 | xmlAttrPtr |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1684 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1685 | xmlAttrPtr ret = NULL; |
| 1686 | xmlAttrPtr p = NULL,q; |
| 1687 | |
| 1688 | while (cur != NULL) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1689 | q = xmlCopyProp(target, cur); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1690 | if (p == NULL) { |
| 1691 | ret = p = q; |
| 1692 | } else { |
| 1693 | p->next = q; |
| 1694 | p = q; |
| 1695 | } |
| 1696 | cur = cur->next; |
| 1697 | } |
| 1698 | return(ret); |
| 1699 | } |
| 1700 | |
| 1701 | /* |
| 1702 | * NOTE about the CopyNode operations ! |
| 1703 | * |
| 1704 | * They are splitted into external and internal parts for one |
| 1705 | * tricky reason: namespaces. Doing a direct copy of a node |
| 1706 | * say RPM:Copyright without changing the namespace pointer to |
| 1707 | * something else can produce stale links. One way to do it is |
| 1708 | * to keep a reference counter but this doesn't work as soon |
| 1709 | * as one move the element or the subtree out of the scope of |
| 1710 | * the existing namespace. The actual solution seems to add |
| 1711 | * a copy of the namespace at the top of the copied tree if |
| 1712 | * not available in the subtree. |
| 1713 | * Hence two functions, the public front-end call the inner ones |
| 1714 | */ |
| 1715 | |
| 1716 | static xmlNodePtr |
| 1717 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
| 1718 | |
| 1719 | static xmlNodePtr |
| 1720 | xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
| 1721 | int recursive) { |
| 1722 | xmlNodePtr ret; |
| 1723 | |
| 1724 | if (node == NULL) return(NULL); |
| 1725 | /* |
| 1726 | * Allocate a new node and fill the fields. |
| 1727 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1728 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1729 | if (ret == NULL) { |
| 1730 | fprintf(stderr, "xmlStaticCopyNode : malloc failed\n"); |
| 1731 | return(NULL); |
| 1732 | } |
| 1733 | |
| 1734 | ret->type = node->type; |
| 1735 | ret->doc = doc; |
| 1736 | ret->parent = parent; |
| 1737 | ret->next = NULL; |
| 1738 | ret->prev = NULL; |
| 1739 | ret->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1740 | ret->last = NULL; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1741 | ret->properties = NULL; |
| 1742 | if (node->name != NULL) |
| 1743 | ret->name = xmlStrdup(node->name); |
| 1744 | else |
| 1745 | ret->name = NULL; |
| 1746 | ret->ns = NULL; |
| 1747 | ret->nsDef = NULL; |
| 1748 | if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) |
| 1749 | ret->content = xmlStrdup(node->content); |
| 1750 | else |
| 1751 | ret->content = NULL; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 1752 | #ifndef XML_WITHOUT_CORBA |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1753 | ret->_private = NULL; |
| 1754 | ret->vepv = NULL; |
| 1755 | #endif |
| 1756 | if (parent != NULL) |
| 1757 | xmlAddChild(parent, ret); |
| 1758 | |
| 1759 | if (!recursive) return(ret); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1760 | if (node->nsDef != NULL) |
| 1761 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 1762 | |
| 1763 | if (node->ns != NULL) { |
| 1764 | xmlNsPtr ns; |
| 1765 | |
| 1766 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 1767 | if (ns == NULL) { |
| 1768 | /* |
| 1769 | * Humm, we are copying an element whose namespace is defined |
| 1770 | * out of the new tree scope. Search it in the original tree |
| 1771 | * and add it at the top of the new tree |
| 1772 | */ |
| 1773 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 1774 | if (ns != NULL) { |
| 1775 | xmlNodePtr root = ret; |
| 1776 | |
| 1777 | while (root->parent != NULL) root = root->parent; |
| 1778 | xmlNewNs(root, ns->href, ns->prefix); |
| 1779 | } |
| 1780 | } else { |
| 1781 | /* |
| 1782 | * reference the existing namespace definition in our own tree. |
| 1783 | */ |
| 1784 | ret->ns = ns; |
| 1785 | } |
| 1786 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1787 | if (node->properties != NULL) |
| 1788 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1789 | if (node->childs != NULL) |
| 1790 | ret->childs = xmlStaticCopyNodeList(node->childs, doc, ret); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1791 | UPDATE_LAST_CHILD(ret) |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1792 | return(ret); |
| 1793 | } |
| 1794 | |
| 1795 | static xmlNodePtr |
| 1796 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 1797 | xmlNodePtr ret = NULL; |
| 1798 | xmlNodePtr p = NULL,q; |
| 1799 | |
| 1800 | while (node != NULL) { |
| 1801 | q = xmlStaticCopyNode(node, doc, parent, 1); |
| 1802 | if (parent == NULL) { |
| 1803 | if (ret == NULL) ret = q; |
| 1804 | } else { |
| 1805 | if (ret == NULL) { |
| 1806 | q->prev = NULL; |
| 1807 | ret = p = q; |
| 1808 | } else { |
| 1809 | p->next = q; |
| 1810 | q->prev = p; |
| 1811 | p = q; |
| 1812 | } |
| 1813 | } |
| 1814 | node = node->next; |
| 1815 | } |
| 1816 | return(ret); |
| 1817 | } |
| 1818 | |
| 1819 | /** |
| 1820 | * xmlCopyNode: |
| 1821 | * @node: the node |
| 1822 | * @recursive: if 1 do a recursive copy. |
| 1823 | * |
| 1824 | * Do a copy of the node. |
| 1825 | * |
| 1826 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 1827 | */ |
| 1828 | xmlNodePtr |
| 1829 | xmlCopyNode(xmlNodePtr node, int recursive) { |
| 1830 | xmlNodePtr ret; |
| 1831 | |
| 1832 | ret = xmlStaticCopyNode(node, NULL, NULL, recursive); |
| 1833 | return(ret); |
| 1834 | } |
| 1835 | |
| 1836 | /** |
| 1837 | * xmlCopyNodeList: |
| 1838 | * @node: the first node in the list. |
| 1839 | * |
| 1840 | * Do a recursive copy of the node list. |
| 1841 | * |
| 1842 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 1843 | */ |
| 1844 | xmlNodePtr xmlCopyNodeList(xmlNodePtr node) { |
| 1845 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 1846 | return(ret); |
| 1847 | } |
| 1848 | |
| 1849 | /** |
| 1850 | * xmlCopyElement: |
| 1851 | * @elem: the element |
| 1852 | * |
| 1853 | * Do a copy of the element definition. |
| 1854 | * |
| 1855 | * Returns: a new xmlElementPtr, or NULL in case of error. |
| 1856 | xmlElementPtr |
| 1857 | xmlCopyElement(xmlElementPtr elem) { |
| 1858 | xmlElementPtr ret; |
| 1859 | |
| 1860 | if (elem == NULL) return(NULL); |
| 1861 | ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content); |
| 1862 | if (ret == NULL) return(NULL); |
| 1863 | if (!recursive) return(ret); |
| 1864 | if (elem->properties != NULL) |
| 1865 | ret->properties = xmlCopyPropList(elem->properties); |
| 1866 | |
| 1867 | if (elem->nsDef != NULL) |
| 1868 | ret->nsDef = xmlCopyNamespaceList(elem->nsDef); |
| 1869 | if (elem->childs != NULL) |
| 1870 | ret->childs = xmlCopyElementList(elem->childs); |
| 1871 | return(ret); |
| 1872 | } |
| 1873 | */ |
| 1874 | |
| 1875 | /** |
| 1876 | * xmlCopyDtd: |
| 1877 | * @dtd: the dtd |
| 1878 | * |
| 1879 | * Do a copy of the dtd. |
| 1880 | * |
| 1881 | * Returns: a new xmlDtdPtr, or NULL in case of error. |
| 1882 | */ |
| 1883 | xmlDtdPtr |
| 1884 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 1885 | xmlDtdPtr ret; |
| 1886 | |
| 1887 | if (dtd == NULL) return(NULL); |
| 1888 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 1889 | if (ret == NULL) return(NULL); |
| 1890 | if (dtd->entities != NULL) |
| 1891 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 1892 | (xmlEntitiesTablePtr) dtd->entities); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1893 | if (dtd->notations != NULL) |
| 1894 | ret->notations = (void *) xmlCopyNotationTable( |
| 1895 | (xmlNotationTablePtr) dtd->notations); |
| 1896 | if (dtd->elements != NULL) |
| 1897 | ret->elements = (void *) xmlCopyElementTable( |
| 1898 | (xmlElementTablePtr) dtd->elements); |
| 1899 | if (dtd->attributes != NULL) |
| 1900 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 1901 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1902 | return(ret); |
| 1903 | } |
| 1904 | |
| 1905 | /** |
| 1906 | * xmlCopyDoc: |
| 1907 | * @doc: the document |
| 1908 | * @recursive: if 1 do a recursive copy. |
| 1909 | * |
| 1910 | * Do a copy of the document info. If recursive, the content tree will |
| 1911 | * be copied too as well as Dtd, namespaces and entities. |
| 1912 | * |
| 1913 | * Returns: a new xmlDocPtr, or NULL in case of error. |
| 1914 | */ |
| 1915 | xmlDocPtr |
| 1916 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 1917 | xmlDocPtr ret; |
| 1918 | |
| 1919 | if (doc == NULL) return(NULL); |
| 1920 | ret = xmlNewDoc(doc->version); |
| 1921 | if (ret == NULL) return(NULL); |
| 1922 | if (doc->name != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1923 | ret->name = xmlMemStrdup(doc->name); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1924 | if (doc->encoding != NULL) |
| 1925 | ret->encoding = xmlStrdup(doc->encoding); |
| 1926 | ret->compression = doc->compression; |
| 1927 | ret->standalone = doc->standalone; |
| 1928 | if (!recursive) return(ret); |
| 1929 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1930 | if (doc->intSubset != NULL) |
| 1931 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1932 | if (doc->oldNs != NULL) |
| 1933 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 1934 | if (doc->root != NULL) |
| 1935 | ret->root = xmlStaticCopyNodeList(doc->root, ret, NULL); |
| 1936 | return(ret); |
| 1937 | } |
| 1938 | |
| 1939 | /************************************************************************ |
| 1940 | * * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1941 | * Content access functions * |
| 1942 | * * |
| 1943 | ************************************************************************/ |
| 1944 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1945 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1946 | * xmlNodeSetLang: |
| 1947 | * @cur: the node being changed |
| 1948 | * @lang: the langage description |
| 1949 | * |
| 1950 | * Searches the language of a node, i.e. the values of the xml:lang |
| 1951 | * attribute or the one carried by the nearest ancestor. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1952 | */ |
| 1953 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1954 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1955 | /* TODO xmlNodeSetLang check against the production [33] LanguageID */ |
| 1956 | xmlSetProp(cur, BAD_CAST "xml:lang", lang); |
| 1957 | } |
| 1958 | |
| 1959 | /** |
| 1960 | * xmlNodeGetLang: |
| 1961 | * @cur: the node being checked |
| 1962 | * |
| 1963 | * Searches the language of a node, i.e. the values of the xml:lang |
| 1964 | * attribute or the one carried by the nearest ancestor. |
| 1965 | * |
| 1966 | * Returns a pointer to the lang value, or NULL if not found |
| 1967 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1968 | const xmlChar * |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1969 | xmlNodeGetLang(xmlNodePtr cur) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1970 | const xmlChar *lang; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1971 | |
| 1972 | while (cur != NULL) { |
| 1973 | lang = xmlGetProp(cur, BAD_CAST "xml:lang"); |
| 1974 | if (lang != NULL) |
| 1975 | return(lang); |
| 1976 | cur = cur->parent; |
| 1977 | } |
| 1978 | return(NULL); |
| 1979 | } |
| 1980 | |
| 1981 | /** |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1982 | * xmlNodeGetContent: |
| 1983 | * @cur: the node being read |
| 1984 | * |
| 1985 | * Read the value of a node, this can be either the text carried |
| 1986 | * directly by this node if it's a TEXT node or the aggregate string |
| 1987 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 1988 | * Entity references are substitued. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1989 | * Returns a new xmlChar * or NULL if no content is available. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1990 | * It's up to the caller to free the memory. |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1991 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1992 | xmlChar * |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1993 | xmlNodeGetContent(xmlNodePtr cur) { |
| 1994 | if (cur == NULL) return(NULL); |
| 1995 | switch (cur->type) { |
| 1996 | case XML_DOCUMENT_FRAG_NODE: |
| 1997 | case XML_ELEMENT_NODE: |
| 1998 | return(xmlNodeListGetString(cur->doc, cur->childs, 1)); |
| 1999 | break; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2000 | case XML_ATTRIBUTE_NODE: { |
| 2001 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 2002 | if (attr->node != NULL) |
| 2003 | return(xmlNodeListGetString(attr->node->doc, attr->val, 1)); |
| 2004 | else |
| 2005 | return(xmlNodeListGetString(NULL, attr->val, 1)); |
| 2006 | break; |
| 2007 | } |
| 2008 | case XML_PI_NODE: |
| 2009 | if (cur->content != NULL) |
| 2010 | return(xmlStrdup(cur->content)); |
| 2011 | return(NULL); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2012 | case XML_ENTITY_REF_NODE: |
| 2013 | case XML_ENTITY_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2014 | case XML_COMMENT_NODE: |
| 2015 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 2016 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2017 | case XML_DOCUMENT_TYPE_NODE: |
| 2018 | case XML_NOTATION_NODE: |
| 2019 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2020 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2021 | case XML_TEXT_NODE: |
| 2022 | if (cur->content != NULL) |
| 2023 | return(xmlStrdup(cur->content)); |
| 2024 | return(NULL); |
| 2025 | } |
| 2026 | return(NULL); |
| 2027 | } |
| 2028 | |
| 2029 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2030 | * xmlNodeSetContent: |
| 2031 | * @cur: the node being modified |
| 2032 | * @content: the new value of the content |
| 2033 | * |
| 2034 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2035 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2036 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2037 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2038 | if (cur == NULL) { |
| 2039 | fprintf(stderr, "xmlNodeSetContent : node == NULL\n"); |
| 2040 | return; |
| 2041 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2042 | switch (cur->type) { |
| 2043 | case XML_DOCUMENT_FRAG_NODE: |
| 2044 | case XML_ELEMENT_NODE: |
| 2045 | if (cur->content != NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2046 | xmlFree(cur->content); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2047 | cur->content = NULL; |
| 2048 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2049 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2050 | cur->childs = xmlStringGetNodeList(cur->doc, content); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2051 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2052 | break; |
| 2053 | case XML_ATTRIBUTE_NODE: |
| 2054 | break; |
| 2055 | case XML_TEXT_NODE: |
| 2056 | case XML_CDATA_SECTION_NODE: |
| 2057 | case XML_ENTITY_REF_NODE: |
| 2058 | case XML_ENTITY_NODE: |
| 2059 | case XML_PI_NODE: |
| 2060 | case XML_COMMENT_NODE: |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2061 | if (cur->content != NULL) xmlFree(cur->content); |
| 2062 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2063 | cur->last = cur->childs = NULL; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2064 | if (content != NULL) |
| 2065 | cur->content = xmlStrdup(content); |
| 2066 | else |
| 2067 | cur->content = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2068 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2069 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 2070 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2071 | case XML_DOCUMENT_TYPE_NODE: |
| 2072 | break; |
| 2073 | case XML_NOTATION_NODE: |
| 2074 | break; |
| 2075 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2078 | /** |
| 2079 | * xmlNodeSetContentLen: |
| 2080 | * @cur: the node being modified |
| 2081 | * @content: the new value of the content |
| 2082 | * @len: the size of @content |
| 2083 | * |
| 2084 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2085 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2086 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2087 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2088 | if (cur == NULL) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2089 | fprintf(stderr, "xmlNodeSetContentLen : node == NULL\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2090 | return; |
| 2091 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2092 | switch (cur->type) { |
| 2093 | case XML_DOCUMENT_FRAG_NODE: |
| 2094 | case XML_ELEMENT_NODE: |
| 2095 | if (cur->content != NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2096 | xmlFree(cur->content); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2097 | cur->content = NULL; |
| 2098 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2099 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2100 | cur->childs = xmlStringLenGetNodeList(cur->doc, content, len); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2101 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2102 | break; |
| 2103 | case XML_ATTRIBUTE_NODE: |
| 2104 | break; |
| 2105 | case XML_TEXT_NODE: |
| 2106 | case XML_CDATA_SECTION_NODE: |
| 2107 | case XML_ENTITY_REF_NODE: |
| 2108 | case XML_ENTITY_NODE: |
| 2109 | case XML_PI_NODE: |
| 2110 | case XML_COMMENT_NODE: |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2111 | if (cur->content != NULL) xmlFree(cur->content); |
| 2112 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2113 | cur->childs = cur->last = NULL; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2114 | if (content != NULL) |
| 2115 | cur->content = xmlStrndup(content, len); |
| 2116 | else |
| 2117 | cur->content = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2118 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2119 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 2120 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2121 | case XML_DOCUMENT_TYPE_NODE: |
| 2122 | break; |
| 2123 | case XML_NOTATION_NODE: |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2124 | if (cur->content != NULL) xmlFree(cur->content); |
| 2125 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2126 | cur->childs = cur->last = NULL; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2127 | if (content != NULL) |
| 2128 | cur->content = xmlStrndup(content, len); |
| 2129 | else |
| 2130 | cur->content = NULL; |
| 2131 | break; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2132 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2135 | /** |
| 2136 | * xmlNodeAddContentLen: |
| 2137 | * @cur: the node being modified |
| 2138 | * @content: extra content |
| 2139 | * @len: the size of @content |
| 2140 | * |
| 2141 | * Append the extra substring to the node content. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2142 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2143 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2144 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2145 | if (cur == NULL) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2146 | fprintf(stderr, "xmlNodeAddContentLen : node == NULL\n"); |
| 2147 | return; |
| 2148 | } |
| 2149 | if (len <= 0) return; |
| 2150 | switch (cur->type) { |
| 2151 | case XML_DOCUMENT_FRAG_NODE: |
| 2152 | case XML_ELEMENT_NODE: { |
| 2153 | xmlNodePtr last = NULL, new; |
| 2154 | |
| 2155 | if (cur->childs != NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2156 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2157 | } else { |
| 2158 | if (cur->content != NULL) { |
| 2159 | cur->childs = xmlStringGetNodeList(cur->doc, cur->content); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2160 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2161 | xmlFree(cur->content); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2162 | cur->content = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2163 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2164 | } |
| 2165 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2166 | new = xmlNewTextLen(content, len); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2167 | if (new != NULL) { |
| 2168 | xmlAddChild(cur, new); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2169 | if ((last != NULL) && (last->next == new)) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2170 | xmlTextMerge(last, new); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2171 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2172 | } |
| 2173 | break; |
| 2174 | } |
| 2175 | case XML_ATTRIBUTE_NODE: |
| 2176 | break; |
| 2177 | case XML_TEXT_NODE: |
| 2178 | case XML_CDATA_SECTION_NODE: |
| 2179 | case XML_ENTITY_REF_NODE: |
| 2180 | case XML_ENTITY_NODE: |
| 2181 | case XML_PI_NODE: |
| 2182 | case XML_COMMENT_NODE: |
| 2183 | if (content != NULL) |
| 2184 | cur->content = xmlStrncat(cur->content, content, len); |
| 2185 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 2186 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2187 | case XML_DOCUMENT_TYPE_NODE: |
| 2188 | break; |
| 2189 | case XML_NOTATION_NODE: |
| 2190 | if (content != NULL) |
| 2191 | cur->content = xmlStrncat(cur->content, content, len); |
| 2192 | break; |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | /** |
| 2197 | * xmlNodeAddContent: |
| 2198 | * @cur: the node being modified |
| 2199 | * @content: extra content |
| 2200 | * |
| 2201 | * Append the extra substring to the node content. |
| 2202 | */ |
| 2203 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2204 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2205 | int len; |
| 2206 | |
| 2207 | if (cur == NULL) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2208 | fprintf(stderr, "xmlNodeAddContent : node == NULL\n"); |
| 2209 | return; |
| 2210 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2211 | if (content == NULL) return; |
| 2212 | len = xmlStrlen(content); |
| 2213 | xmlNodeAddContentLen(cur, content, len); |
| 2214 | } |
| 2215 | |
| 2216 | /** |
| 2217 | * xmlTextMerge: |
| 2218 | * @first: the first text node |
| 2219 | * @second: the second text node being merged |
| 2220 | * |
| 2221 | * Merge two text nodes into one |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2222 | * Returns the first text node augmented |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2223 | */ |
| 2224 | xmlNodePtr |
| 2225 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 2226 | if (first == NULL) return(second); |
| 2227 | if (second == NULL) return(first); |
| 2228 | if (first->type != XML_TEXT_NODE) return(first); |
| 2229 | if (second->type != XML_TEXT_NODE) return(first); |
| 2230 | xmlNodeAddContent(first, second->content); |
| 2231 | xmlUnlinkNode(second); |
| 2232 | xmlFreeNode(second); |
| 2233 | return(first); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2236 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2237 | * xmlGetNsList: |
| 2238 | * @doc: the document |
| 2239 | * @node: the current node |
| 2240 | * |
| 2241 | * Search all the namespace applying to a given element. |
| 2242 | * Returns an NULL terminated array of all the xmlNsPtr found |
| 2243 | * that need to be freed by the caller or NULL if no |
| 2244 | * namespace if defined |
| 2245 | */ |
| 2246 | xmlNsPtr * |
| 2247 | xmlGetNsList(xmlDocPtr doc, xmlNodePtr node) { |
| 2248 | xmlNsPtr cur; |
| 2249 | xmlNsPtr *ret = NULL; |
| 2250 | int nbns = 0; |
| 2251 | int maxns = 10; |
| 2252 | int i; |
| 2253 | |
| 2254 | while (node != NULL) { |
| 2255 | cur = node->nsDef; |
| 2256 | while (cur != NULL) { |
| 2257 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2258 | ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2259 | if (ret == NULL) { |
| 2260 | fprintf(stderr, "xmlGetNsList : out of memory!\n"); |
| 2261 | return(NULL); |
| 2262 | } |
| 2263 | ret[nbns] = NULL; |
| 2264 | } |
| 2265 | for (i = 0;i < nbns;i++) { |
| 2266 | if ((cur->prefix == ret[i]->prefix) || |
| 2267 | (!xmlStrcmp(cur->prefix, ret[i]->prefix))) break; |
| 2268 | } |
| 2269 | if (i >= nbns) { |
| 2270 | if (nbns >= maxns) { |
| 2271 | maxns *= 2; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2272 | ret = (xmlNsPtr *) xmlRealloc(ret, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2273 | (maxns + 1) * sizeof(xmlNsPtr)); |
| 2274 | if (ret == NULL) { |
| 2275 | fprintf(stderr, "xmlGetNsList : realloc failed!\n"); |
| 2276 | return(NULL); |
| 2277 | } |
| 2278 | } |
| 2279 | ret[nbns++] = cur; |
| 2280 | ret[nbns] = NULL; |
| 2281 | } |
| 2282 | |
| 2283 | cur = cur->next; |
| 2284 | } |
| 2285 | node = node->parent; |
| 2286 | } |
| 2287 | return(ret); |
| 2288 | } |
| 2289 | |
| 2290 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2291 | * xmlSearchNs: |
| 2292 | * @doc: the document |
| 2293 | * @node: the current node |
| 2294 | * @nameSpace: the namespace string |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2295 | * |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2296 | * Search a Ns registered under a given name space for a document. |
| 2297 | * recurse on the parents until it finds the defined namespace |
| 2298 | * or return NULL otherwise. |
| 2299 | * @nameSpace can be NULL, this is a search for the default namespace. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2300 | * Returns the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2301 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2302 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2303 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2304 | xmlNsPtr cur; |
| 2305 | |
| 2306 | while (node != NULL) { |
| 2307 | cur = node->nsDef; |
| 2308 | while (cur != NULL) { |
| 2309 | if ((cur->prefix == NULL) && (nameSpace == NULL)) |
| 2310 | return(cur); |
| 2311 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 2312 | (!xmlStrcmp(cur->prefix, nameSpace))) |
| 2313 | return(cur); |
| 2314 | cur = cur->next; |
| 2315 | } |
| 2316 | node = node->parent; |
| 2317 | } |
| 2318 | if (doc != NULL) { |
| 2319 | cur = doc->oldNs; |
| 2320 | while (cur != NULL) { |
| 2321 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 2322 | (!xmlStrcmp(cur->prefix, nameSpace))) |
| 2323 | return(cur); |
| 2324 | cur = cur->next; |
| 2325 | } |
| 2326 | } |
| 2327 | return(NULL); |
| 2328 | } |
| 2329 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2330 | /** |
| 2331 | * xmlSearchNsByHref: |
| 2332 | * @doc: the document |
| 2333 | * @node: the current node |
| 2334 | * @href: the namespace value |
| 2335 | * |
| 2336 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 2337 | * the defined namespace or return NULL otherwise. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2338 | * Returns the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2339 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2340 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2341 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2342 | xmlNsPtr cur; |
| 2343 | |
| 2344 | while (node != NULL) { |
| 2345 | cur = node->nsDef; |
| 2346 | while (cur != NULL) { |
| 2347 | if ((cur->href != NULL) && (href != NULL) && |
| 2348 | (!xmlStrcmp(cur->href, href))) |
| 2349 | return(cur); |
| 2350 | cur = cur->next; |
| 2351 | } |
| 2352 | node = node->parent; |
| 2353 | } |
| 2354 | if (doc != NULL) { |
| 2355 | cur = doc->oldNs; |
| 2356 | while (cur != NULL) { |
| 2357 | if ((cur->href != NULL) && (href != NULL) && |
| 2358 | (!xmlStrcmp(cur->href, href))) |
| 2359 | return(cur); |
| 2360 | cur = cur->next; |
| 2361 | } |
| 2362 | } |
| 2363 | return(NULL); |
| 2364 | } |
| 2365 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2366 | /** |
| 2367 | * xmlGetProp: |
| 2368 | * @node: the node |
| 2369 | * @name: the attribute name |
| 2370 | * |
| 2371 | * Search and get the value of an attribute associated to a node |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2372 | * This does the entity substitution. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2373 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2374 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2375 | xmlChar *xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2376 | xmlAttrPtr prop = node->properties; |
| 2377 | |
| 2378 | while (prop != NULL) { |
Daniel Veillard | 6817893 | 1999-02-08 18:34:36 +0000 | [diff] [blame] | 2379 | if (!xmlStrcmp(prop->name, name)) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2380 | xmlChar *ret; |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame] | 2381 | |
| 2382 | ret = xmlNodeListGetString(node->doc, prop->val, 1); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2383 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame] | 2384 | return(ret); |
Daniel Veillard | 6817893 | 1999-02-08 18:34:36 +0000 | [diff] [blame] | 2385 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2386 | prop = prop->next; |
| 2387 | } |
| 2388 | return(NULL); |
| 2389 | } |
| 2390 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2391 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2392 | * xmlSetProp: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2393 | * @node: the node |
| 2394 | * @name: the attribute name |
| 2395 | * @value: the attribute value |
| 2396 | * |
| 2397 | * Set (or reset) an attribute carried by a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2398 | * Returns the attribute pointer. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2399 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2400 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2401 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2402 | xmlAttrPtr prop = node->properties; |
| 2403 | |
| 2404 | while (prop != NULL) { |
| 2405 | if (!xmlStrcmp(prop->name, name)) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2406 | if (prop->val != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2407 | xmlFreeNodeList(prop->val); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2408 | prop->val = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2409 | if (value != NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2410 | prop->val = xmlStringGetNodeList(node->doc, value); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2411 | return(prop); |
| 2412 | } |
| 2413 | prop = prop->next; |
| 2414 | } |
| 2415 | prop = xmlNewProp(node, name, value); |
| 2416 | return(prop); |
| 2417 | } |
| 2418 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2419 | /** |
| 2420 | * xmlNodeIsText: |
| 2421 | * @node: the node |
| 2422 | * |
| 2423 | * Is this node a Text node ? |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2424 | * Returns 1 yes, 0 no |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2425 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2426 | int |
| 2427 | xmlNodeIsText(xmlNodePtr node) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2428 | if (node == NULL) return(0); |
| 2429 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2430 | if (node->type == XML_TEXT_NODE) return(1); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2431 | return(0); |
| 2432 | } |
| 2433 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2434 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2435 | * xmlTextConcat: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2436 | * @node: the node |
| 2437 | * @content: the content |
| 2438 | * @len: @content lenght |
| 2439 | * |
| 2440 | * Concat the given string at the end of the existing node content |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2441 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2442 | |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2443 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2444 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2445 | if (node == NULL) return; |
| 2446 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2447 | if (node->type != XML_TEXT_NODE) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2448 | fprintf(stderr, "xmlTextConcat: node is not text\n"); |
| 2449 | return; |
| 2450 | } |
| 2451 | node->content = xmlStrncat(node->content, content, len); |
| 2452 | } |
| 2453 | |
| 2454 | /************************************************************************ |
| 2455 | * * |
| 2456 | * Output : to a FILE or in memory * |
| 2457 | * * |
| 2458 | ************************************************************************/ |
| 2459 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2460 | #define BASE_BUFFER_SIZE 4000 |
| 2461 | |
| 2462 | /** |
| 2463 | * xmlBufferCreate: |
| 2464 | * |
| 2465 | * routine to create an XML buffer. |
| 2466 | * returns the new structure. |
| 2467 | */ |
| 2468 | xmlBufferPtr |
| 2469 | xmlBufferCreate(void) { |
| 2470 | xmlBufferPtr ret; |
| 2471 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2472 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2473 | if (ret == NULL) { |
| 2474 | fprintf(stderr, "xmlBufferCreate : out of memory!\n"); |
| 2475 | return(NULL); |
| 2476 | } |
| 2477 | ret->use = 0; |
| 2478 | ret->size = BASE_BUFFER_SIZE; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2479 | ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2480 | if (ret->content == NULL) { |
| 2481 | fprintf(stderr, "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2482 | xmlFree(ret); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2483 | return(NULL); |
| 2484 | } |
| 2485 | ret->content[0] = 0; |
| 2486 | return(ret); |
| 2487 | } |
| 2488 | |
| 2489 | /** |
| 2490 | * xmlBufferFree: |
| 2491 | * @buf: the buffer to free |
| 2492 | * |
| 2493 | * Frees an XML buffer. |
| 2494 | */ |
| 2495 | void |
| 2496 | xmlBufferFree(xmlBufferPtr buf) { |
| 2497 | if (buf == NULL) { |
| 2498 | fprintf(stderr, "xmlBufferFree: buf == NULL\n"); |
| 2499 | return; |
| 2500 | } |
| 2501 | if (buf->content == NULL) { |
| 2502 | fprintf(stderr, "xmlBufferFree: buf->content == NULL\n"); |
| 2503 | } else { |
| 2504 | memset(buf->content, -1, BASE_BUFFER_SIZE); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2505 | xmlFree(buf->content); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2506 | } |
| 2507 | memset(buf, -1, sizeof(xmlBuffer)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2508 | xmlFree(buf); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
| 2511 | /** |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2512 | * xmlBufferEmpty: |
| 2513 | * @buf: the buffer |
| 2514 | * |
| 2515 | * empty a buffer. |
| 2516 | */ |
| 2517 | void |
| 2518 | xmlBufferEmpty(xmlBufferPtr buf) { |
| 2519 | buf->use = 0; |
| 2520 | memset(buf->content, -1, buf->size);/* just for debug */ |
| 2521 | } |
| 2522 | |
| 2523 | /** |
| 2524 | * xmlBufferShrink: |
| 2525 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2526 | * @len: the number of xmlChar to remove |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2527 | * |
| 2528 | * Remove the beginning of an XML buffer. |
| 2529 | * |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2530 | * Returns the number of xmlChar removed, or -1 in case of failure. |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2531 | */ |
| 2532 | int |
| 2533 | xmlBufferShrink(xmlBufferPtr buf, int len) { |
| 2534 | if (len == 0) return(0); |
| 2535 | if (len > buf->use) return(-1); |
| 2536 | |
| 2537 | buf->use -= len; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2538 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2539 | |
| 2540 | buf->content[buf->use] = 0; |
| 2541 | return(len); |
| 2542 | } |
| 2543 | |
| 2544 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2545 | * xmlBufferDump: |
| 2546 | * @file: the file output |
| 2547 | * @buf: the buffer to dump |
| 2548 | * |
| 2549 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2550 | * Returns the number of xmlChar written |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2551 | */ |
| 2552 | int |
| 2553 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 2554 | int ret; |
| 2555 | |
| 2556 | if (buf == NULL) { |
| 2557 | fprintf(stderr, "xmlBufferDump: buf == NULL\n"); |
| 2558 | return(0); |
| 2559 | } |
| 2560 | if (buf->content == NULL) { |
| 2561 | fprintf(stderr, "xmlBufferDump: buf->content == NULL\n"); |
| 2562 | return(0); |
| 2563 | } |
| 2564 | if (file == NULL) file = stdout; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2565 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2566 | return(ret); |
| 2567 | } |
| 2568 | |
| 2569 | /** |
| 2570 | * xmlBufferAdd: |
| 2571 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2572 | * @str: the xmlChar string |
| 2573 | * @len: the number of xmlChar to add |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2574 | * |
| 2575 | * Add a string range to an XML buffer. |
| 2576 | */ |
| 2577 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2578 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2579 | int l; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2580 | |
| 2581 | if (str == NULL) { |
| 2582 | fprintf(stderr, "xmlBufferAdd: str == NULL\n"); |
| 2583 | return; |
| 2584 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2585 | l = xmlStrlen(str); |
| 2586 | if (l < len) len = l; |
| 2587 | if (len <= 0) return; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2588 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2589 | if (buf->use + len + 10 >= buf->size) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2590 | xmlChar *rebuf; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2591 | |
| 2592 | buf->size *= 2; |
| 2593 | if (buf->use + len + 10 > buf->size) |
| 2594 | buf->size = buf->use + len + 10; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2595 | rebuf = (xmlChar *) xmlRealloc(buf->content, buf->size * sizeof(xmlChar)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2596 | if (rebuf == NULL) { |
| 2597 | fprintf(stderr, "xmlBufferAdd : out of memory!\n"); |
| 2598 | return; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2599 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2600 | buf->content = rebuf; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2601 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2602 | memmove(&buf->content[buf->use], str, len); |
| 2603 | buf->use += len; |
| 2604 | buf->content[buf->use] = 0; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
| 2607 | /** |
| 2608 | * xmlBufferCat: |
| 2609 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2610 | * @str: the xmlChar string |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2611 | * |
| 2612 | * Append a zero terminated string to an XML buffer. |
| 2613 | */ |
| 2614 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2615 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
| 2616 | const xmlChar *cur; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2617 | |
| 2618 | if (str == NULL) { |
| 2619 | fprintf(stderr, "xmlBufferAdd: str == NULL\n"); |
| 2620 | return; |
| 2621 | } |
| 2622 | for (cur = str;*cur != 0;cur++) { |
| 2623 | if (buf->use + 10 >= buf->size) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2624 | xmlChar *rebuf; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2625 | |
| 2626 | buf->size *= 2; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2627 | rebuf = (xmlChar *) xmlRealloc(buf->content, buf->size * sizeof(xmlChar)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2628 | if (rebuf == NULL) { |
| 2629 | fprintf(stderr, "xmlBufferAdd : out of memory!\n"); |
| 2630 | return; |
| 2631 | } |
| 2632 | buf->content = rebuf; |
| 2633 | } |
| 2634 | buf->content[buf->use++] = *cur; |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | /** |
| 2639 | * xmlBufferCCat: |
| 2640 | * @buf: the buffer to dump |
| 2641 | * @str: the C char string |
| 2642 | * |
| 2643 | * Append a zero terminated C string to an XML buffer. |
| 2644 | */ |
| 2645 | void |
| 2646 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 2647 | const char *cur; |
| 2648 | |
| 2649 | if (str == NULL) { |
| 2650 | fprintf(stderr, "xmlBufferAdd: str == NULL\n"); |
| 2651 | return; |
| 2652 | } |
| 2653 | for (cur = str;*cur != 0;cur++) { |
| 2654 | if (buf->use + 10 >= buf->size) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2655 | xmlChar *rebuf; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2656 | |
| 2657 | buf->size *= 2; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2658 | rebuf = (xmlChar *) xmlRealloc(buf->content, buf->size * sizeof(xmlChar)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2659 | if (rebuf == NULL) { |
| 2660 | fprintf(stderr, "xmlBufferAdd : out of memory!\n"); |
| 2661 | return; |
| 2662 | } |
| 2663 | buf->content = rebuf; |
| 2664 | } |
| 2665 | buf->content[buf->use++] = *cur; |
| 2666 | } |
| 2667 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2668 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2669 | /** |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 2670 | * xmlBufferLastChar: |
| 2671 | * @buf: the buffer to dump |
| 2672 | * |
| 2673 | * Get the last char of the buffer |
| 2674 | * |
| 2675 | * Returns the last char from the buffer or 0 if empty |
| 2676 | */ |
| 2677 | xmlChar |
| 2678 | xmlBufferLastChar(xmlBufferPtr buf) { |
| 2679 | if ((buf == NULL) || (buf->use <= 0)) return(0); |
| 2680 | return(buf->content[buf->use - 1]); |
| 2681 | } |
| 2682 | |
| 2683 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2684 | * xmlBufferWriteCHAR: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2685 | * @buf: the XML buffer |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2686 | * @string: the string to add |
| 2687 | * |
| 2688 | * routine which manage and grows an output buffer. This one add |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2689 | * xmlChars at the end of the buffer. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2690 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2691 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2692 | xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2693 | xmlBufferCat(buf, string); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2696 | /** |
| 2697 | * xmlBufferWriteChar: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2698 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2699 | * @string: the string to add |
| 2700 | * |
| 2701 | * routine which manage and grows an output buffer. This one add |
| 2702 | * C chars at the end of the array. |
| 2703 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2704 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2705 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
| 2706 | xmlBufferCCat(buf, string); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2709 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2710 | /** |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2711 | * xmlBufferWriteQuotedString: |
| 2712 | * @buf: the XML buffer output |
| 2713 | * @string: the string to add |
| 2714 | * |
| 2715 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2716 | * a quoted or double quoted xmlChar string, checking first if it holds |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2717 | * quote or double-quotes internally |
| 2718 | */ |
| 2719 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2720 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2721 | if (xmlStrchr(string, '"')) { |
| 2722 | if (xmlStrchr(string, '\'')) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2723 | fprintf(stderr, |
| 2724 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 2725 | } |
| 2726 | xmlBufferCCat(buf, "'"); |
| 2727 | xmlBufferCat(buf, string); |
| 2728 | xmlBufferCCat(buf, "'"); |
| 2729 | } else { |
| 2730 | xmlBufferCCat(buf, "\""); |
| 2731 | xmlBufferCat(buf, string); |
| 2732 | xmlBufferCCat(buf, "\""); |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | |
| 2737 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2738 | * xmlGlobalNsDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2739 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2740 | * @cur: a namespace |
| 2741 | * |
| 2742 | * Dump a global Namespace, this is the old version based on PIs. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2743 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2744 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2745 | xmlGlobalNsDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2746 | if (cur == NULL) { |
| 2747 | fprintf(stderr, "xmlGlobalNsDump : Ns == NULL\n"); |
| 2748 | return; |
| 2749 | } |
| 2750 | if (cur->type == XML_GLOBAL_NAMESPACE) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2751 | xmlBufferWriteChar(buf, "<?namespace"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2752 | if (cur->href != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2753 | xmlBufferWriteChar(buf, " href="); |
| 2754 | xmlBufferWriteQuotedString(buf, cur->href); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2755 | } |
| 2756 | if (cur->prefix != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2757 | xmlBufferWriteChar(buf, " AS="); |
| 2758 | xmlBufferWriteQuotedString(buf, cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2759 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2760 | xmlBufferWriteChar(buf, "?>\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2761 | } |
| 2762 | } |
| 2763 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2764 | /** |
| 2765 | * xmlGlobalNsListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2766 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2767 | * @cur: the first namespace |
| 2768 | * |
| 2769 | * 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] | 2770 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2771 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2772 | xmlGlobalNsListDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2773 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2774 | xmlGlobalNsDump(buf, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2775 | cur = cur->next; |
| 2776 | } |
| 2777 | } |
| 2778 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2779 | /** |
| 2780 | * xmlNsDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2781 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2782 | * @cur: a namespace |
| 2783 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2784 | * Dump a local Namespace definition. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2785 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2786 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2787 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2788 | xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2789 | if (cur == NULL) { |
| 2790 | fprintf(stderr, "xmlNsDump : Ns == NULL\n"); |
| 2791 | return; |
| 2792 | } |
| 2793 | if (cur->type == XML_LOCAL_NAMESPACE) { |
| 2794 | /* Within the context of an element attributes */ |
| 2795 | if (cur->prefix != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2796 | xmlBufferWriteChar(buf, " xmlns:"); |
| 2797 | xmlBufferWriteCHAR(buf, cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2798 | } else |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2799 | xmlBufferWriteChar(buf, " xmlns"); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2800 | xmlBufferWriteChar(buf, "="); |
| 2801 | xmlBufferWriteQuotedString(buf, cur->href); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2802 | } |
| 2803 | } |
| 2804 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2805 | /** |
| 2806 | * xmlNsListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2807 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2808 | * @cur: the first namespace |
| 2809 | * |
| 2810 | * Dump a list of local Namespace definitions. |
| 2811 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2812 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2813 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2814 | xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2815 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2816 | xmlNsDump(buf, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2817 | cur = cur->next; |
| 2818 | } |
| 2819 | } |
| 2820 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2821 | /** |
| 2822 | * xmlDtdDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2823 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2824 | * @doc: the document |
| 2825 | * |
| 2826 | * Dump the XML document DTD, if any. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2827 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2828 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2829 | xmlDtdDump(xmlBufferPtr buf, xmlDocPtr doc) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2830 | xmlDtdPtr cur = doc->intSubset; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2831 | |
| 2832 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2833 | fprintf(stderr, "xmlDtdDump : no internal subset\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2834 | return; |
| 2835 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2836 | xmlBufferWriteChar(buf, "<!DOCTYPE "); |
| 2837 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2838 | if (cur->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2839 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 2840 | xmlBufferWriteQuotedString(buf, cur->ExternalID); |
| 2841 | xmlBufferWriteChar(buf, " "); |
| 2842 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2843 | } else if (cur->SystemID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2844 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 2845 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2846 | } |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2847 | if ((cur->entities == NULL) && (cur->elements == NULL) && |
| 2848 | (cur->attributes == NULL) && (cur->notations == NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2849 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2850 | return; |
| 2851 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2852 | xmlBufferWriteChar(buf, " [\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2853 | if (cur->entities != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2854 | xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) cur->entities); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2855 | if (cur->notations != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2856 | xmlDumpNotationTable(buf, (xmlNotationTablePtr) cur->notations); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 2857 | if (cur->elements != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2858 | xmlDumpElementTable(buf, (xmlElementTablePtr) cur->elements); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2859 | if (cur->attributes != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2860 | xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) cur->attributes); |
| 2861 | xmlBufferWriteChar(buf, "]"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2862 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2863 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2866 | /** |
| 2867 | * xmlAttrDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2868 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2869 | * @doc: the document |
| 2870 | * @cur: the attribute pointer |
| 2871 | * |
| 2872 | * Dump an XML attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2873 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2874 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2875 | xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2876 | xmlChar *value; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2877 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2878 | if (cur == NULL) { |
| 2879 | fprintf(stderr, "xmlAttrDump : property == NULL\n"); |
| 2880 | return; |
| 2881 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2882 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2883 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 2884 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 2885 | xmlBufferWriteChar(buf, ":"); |
| 2886 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2887 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2888 | value = xmlNodeListGetString(doc, cur->val, 0); |
| 2889 | if (value) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2890 | xmlBufferWriteChar(buf, "="); |
| 2891 | xmlBufferWriteQuotedString(buf, value); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2892 | xmlFree(value); |
Daniel Veillard | 726c7e3 | 1999-02-08 15:13:10 +0000 | [diff] [blame] | 2893 | } else { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2894 | xmlBufferWriteChar(buf, "=\"\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2895 | } |
| 2896 | } |
| 2897 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2898 | /** |
| 2899 | * xmlAttrListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2900 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2901 | * @doc: the document |
| 2902 | * @cur: the first attribute pointer |
| 2903 | * |
| 2904 | * Dump a list of XML attributes |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2905 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2906 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2907 | xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2908 | if (cur == NULL) { |
| 2909 | fprintf(stderr, "xmlAttrListDump : property == NULL\n"); |
| 2910 | return; |
| 2911 | } |
| 2912 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2913 | xmlAttrDump(buf, doc, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2914 | cur = cur->next; |
| 2915 | } |
| 2916 | } |
| 2917 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2918 | |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2919 | static void |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 2920 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 2921 | int format); |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2922 | /** |
| 2923 | * xmlNodeListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2924 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2925 | * @doc: the document |
| 2926 | * @cur: the first node |
| 2927 | * @level: the imbrication level for indenting |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 2928 | * @format: is formatting allowed |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2929 | * |
| 2930 | * Dump an XML node list, recursive behaviour,children are printed too. |
| 2931 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2932 | static void |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 2933 | xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 2934 | int format) { |
| 2935 | int i; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2936 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2937 | if (cur == NULL) { |
| 2938 | fprintf(stderr, "xmlNodeListDump : node == NULL\n"); |
| 2939 | return; |
| 2940 | } |
| 2941 | while (cur != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 2942 | if ((format) && (xmlIndentTreeOutput) && |
| 2943 | (cur->type == XML_ELEMENT_NODE)) |
| 2944 | for (i = 0;i < level;i++) |
| 2945 | xmlBufferWriteChar(buf, " "); |
| 2946 | xmlNodeDump(buf, doc, cur, level, format); |
| 2947 | if (format) { |
| 2948 | xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2949 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2950 | cur = cur->next; |
| 2951 | } |
| 2952 | } |
| 2953 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2954 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2955 | * xmlNodeDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2956 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2957 | * @doc: the document |
| 2958 | * @cur: the current node |
| 2959 | * @level: the imbrication level for indenting |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 2960 | * @format: is formatting allowed |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2961 | * |
| 2962 | * Dump an XML node, recursive behaviour,children are printed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2963 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2964 | static void |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 2965 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 2966 | int format) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2967 | int i; |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 2968 | xmlNodePtr tmp; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2969 | |
| 2970 | if (cur == NULL) { |
| 2971 | fprintf(stderr, "xmlNodeDump : node == NULL\n"); |
| 2972 | return; |
| 2973 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2974 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 2975 | if (cur->content != NULL) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2976 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 2977 | |
| 2978 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 2979 | if (buffer != NULL) { |
| 2980 | xmlBufferWriteCHAR(buf, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2981 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 2982 | } |
| 2983 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2984 | return; |
| 2985 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2986 | if (cur->type == XML_PI_NODE) { |
| 2987 | if (cur->content != NULL) { |
| 2988 | xmlBufferWriteChar(buf, "<?"); |
| 2989 | xmlBufferWriteCHAR(buf, cur->name); |
| 2990 | if (cur->content != NULL) { |
| 2991 | xmlBufferWriteChar(buf, " "); |
| 2992 | xmlBufferWriteCHAR(buf, cur->content); |
| 2993 | } |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 2994 | xmlBufferWriteChar(buf, "?>"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2995 | } |
| 2996 | return; |
| 2997 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2998 | if (cur->type == XML_COMMENT_NODE) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2999 | if (cur->content != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3000 | xmlBufferWriteChar(buf, "<!--"); |
| 3001 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 3002 | xmlBufferWriteChar(buf, "-->"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3003 | } |
| 3004 | return; |
| 3005 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3006 | if (cur->type == XML_ENTITY_REF_NODE) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3007 | xmlBufferWriteChar(buf, "&"); |
| 3008 | xmlBufferWriteCHAR(buf, cur->name); |
| 3009 | xmlBufferWriteChar(buf, ";"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3010 | return; |
| 3011 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3012 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 3013 | xmlBufferWriteChar(buf, "<![CDATA["); |
| 3014 | if (cur->content != NULL) |
| 3015 | xmlBufferWriteCHAR(buf, cur->content); |
| 3016 | xmlBufferWriteChar(buf, "]]>"); |
| 3017 | return; |
| 3018 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3019 | |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 3020 | if (format == 1) { |
| 3021 | tmp = cur->childs; |
| 3022 | while (tmp != NULL) { |
| 3023 | if ((tmp->type == XML_TEXT_NODE) || |
| 3024 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 3025 | format = 0; |
| 3026 | break; |
| 3027 | } |
| 3028 | tmp = tmp->next; |
| 3029 | } |
| 3030 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3031 | xmlBufferWriteChar(buf, "<"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3032 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3033 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 3034 | xmlBufferWriteChar(buf, ":"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3037 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3038 | if (cur->nsDef) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3039 | xmlNsListDump(buf, cur->nsDef); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3040 | if (cur->properties != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3041 | xmlAttrListDump(buf, doc, cur->properties); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3042 | |
| 3043 | if ((cur->content == NULL) && (cur->childs == NULL)) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 3044 | xmlBufferWriteChar(buf, "/>"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3045 | return; |
| 3046 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3047 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 3048 | if (cur->content != NULL) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3049 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 3050 | |
| 3051 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 3052 | if (buffer != NULL) { |
| 3053 | xmlBufferWriteCHAR(buf, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3054 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 3055 | } |
| 3056 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3057 | if (cur->childs != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 3058 | if (format) xmlBufferWriteChar(buf, "\n"); |
| 3059 | xmlNodeListDump(buf, doc, cur->childs, level + 1, format); |
| 3060 | if ((xmlIndentTreeOutput) && (format)) |
| 3061 | for (i = 0;i < level;i++) |
| 3062 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3063 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3064 | xmlBufferWriteChar(buf, "</"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3065 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3066 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 3067 | xmlBufferWriteChar(buf, ":"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3070 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 3071 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3074 | /** |
| 3075 | * xmlDocContentDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3076 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3077 | * @cur: the document |
| 3078 | * |
| 3079 | * Dump an XML document. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3080 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3081 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3082 | xmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur) { |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 3083 | xmlBufferWriteChar(buf, "<?xml version="); |
| 3084 | if (cur->version != NULL) |
| 3085 | xmlBufferWriteQuotedString(buf, cur->version); |
| 3086 | else |
| 3087 | xmlBufferWriteChar(buf, "\"1.0\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3088 | if (cur->encoding != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3089 | xmlBufferWriteChar(buf, " encoding="); |
| 3090 | xmlBufferWriteQuotedString(buf, cur->encoding); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3091 | } |
| 3092 | switch (cur->standalone) { |
| 3093 | case 0: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3094 | xmlBufferWriteChar(buf, " standalone=\"no\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3095 | break; |
| 3096 | case 1: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3097 | xmlBufferWriteChar(buf, " standalone=\"yes\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3098 | break; |
| 3099 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3100 | xmlBufferWriteChar(buf, "?>\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3101 | if (cur->intSubset != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3102 | xmlDtdDump(buf, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3103 | if (cur->root != NULL) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3104 | xmlNodePtr child = cur->root; |
| 3105 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3106 | /* global namespace definitions, the old way */ |
| 3107 | if (oldXMLWDcompatibility) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3108 | xmlGlobalNsListDump(buf, cur->oldNs); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3109 | else |
| 3110 | xmlUpgradeOldNs(cur); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3111 | |
| 3112 | while (child != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 3113 | xmlNodeDump(buf, cur, child, 0, 1); |
| 3114 | xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3115 | child = child->next; |
| 3116 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3117 | } |
| 3118 | } |
| 3119 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3120 | /** |
| 3121 | * xmlDocDumpMemory: |
| 3122 | * @cur: the document |
| 3123 | * @mem: OUT: the memory pointer |
| 3124 | * @size: OUT: the memory lenght |
| 3125 | * |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3126 | * Dump an XML document in memory and return the xmlChar * and it's size. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3127 | * It's up to the caller to free the memory. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3128 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3129 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3130 | xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3131 | xmlBufferPtr buf; |
| 3132 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3133 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3134 | #ifdef DEBUG_TREE |
| 3135 | fprintf(stderr, "xmlDocDumpMemory : document == NULL\n"); |
| 3136 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3137 | *mem = NULL; |
| 3138 | *size = 0; |
| 3139 | return; |
| 3140 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3141 | buf = xmlBufferCreate(); |
| 3142 | if (buf == NULL) { |
| 3143 | *mem = NULL; |
| 3144 | *size = 0; |
| 3145 | return; |
| 3146 | } |
| 3147 | xmlDocContentDump(buf, cur); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3148 | *mem = xmlStrndup(buf->content, buf->use); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3149 | *size = buf->use; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3150 | xmlBufferFree(buf); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3153 | /** |
| 3154 | * xmlGetDocCompressMode: |
| 3155 | * @doc: the document |
| 3156 | * |
| 3157 | * get the compression ratio for a document, ZLIB based |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3158 | * Returns 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3159 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3160 | int |
| 3161 | xmlGetDocCompressMode (xmlDocPtr doc) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 3162 | if (doc == NULL) return(-1); |
| 3163 | return(doc->compression); |
| 3164 | } |
| 3165 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3166 | /** |
| 3167 | * xmlSetDocCompressMode: |
| 3168 | * @doc: the document |
| 3169 | * @mode: the compression ratio |
| 3170 | * |
| 3171 | * set the compression ratio for a document, ZLIB based |
| 3172 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 3173 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3174 | void |
| 3175 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 3176 | if (doc == NULL) return; |
| 3177 | if (mode < 0) doc->compression = 0; |
| 3178 | else if (mode > 9) doc->compression = 9; |
| 3179 | else doc->compression = mode; |
| 3180 | } |
| 3181 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3182 | /** |
| 3183 | * xmlGetCompressMode: |
| 3184 | * |
| 3185 | * get the default compression mode used, ZLIB based. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3186 | * Returns 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 3187 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3188 | int |
| 3189 | xmlGetCompressMode(void) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3190 | return(xmlCompressMode); |
| 3191 | } |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3192 | |
| 3193 | /** |
| 3194 | * xmlSetCompressMode: |
| 3195 | * @mode: the compression ratio |
| 3196 | * |
| 3197 | * set the default compression mode used, ZLIB based |
| 3198 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 3199 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3200 | void |
| 3201 | xmlSetCompressMode(int mode) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3202 | if (mode < 0) xmlCompressMode = 0; |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 3203 | else if (mode > 9) xmlCompressMode = 9; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3204 | else xmlCompressMode = mode; |
| 3205 | } |
| 3206 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3207 | /** |
| 3208 | * xmlDocDump: |
| 3209 | * @f: the FILE* |
| 3210 | * @cur: the document |
| 3211 | * |
| 3212 | * Dump an XML document to an open FILE. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3213 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3214 | void |
| 3215 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3216 | xmlBufferPtr buf; |
| 3217 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3218 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3219 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3220 | fprintf(stderr, "xmlDocDump : document == NULL\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3221 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3222 | return; |
| 3223 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3224 | buf = xmlBufferCreate(); |
| 3225 | if (buf == NULL) return; |
| 3226 | xmlDocContentDump(buf, cur); |
| 3227 | xmlBufferDump(f, buf); |
| 3228 | xmlBufferFree(buf); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3231 | /** |
| 3232 | * xmlSaveFile: |
| 3233 | * @filename: the filename |
| 3234 | * @cur: the document |
| 3235 | * |
| 3236 | * Dump an XML document to a file. Will use compression if |
| 3237 | * compiled in and enabled. |
| 3238 | * returns: the number of file written or -1 in case of failure. |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3239 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3240 | int |
| 3241 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3242 | xmlBufferPtr buf; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3243 | #ifdef HAVE_ZLIB_H |
| 3244 | gzFile zoutput = NULL; |
| 3245 | char mode[15]; |
| 3246 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3247 | FILE *output = NULL; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3248 | int ret; |
| 3249 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3250 | /* |
| 3251 | * save the content to a temp buffer. |
| 3252 | */ |
| 3253 | buf = xmlBufferCreate(); |
| 3254 | if (buf == NULL) return(0); |
| 3255 | xmlDocContentDump(buf, cur); |
| 3256 | |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3257 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | dc3dd9d | 1998-09-24 19:25:54 +0000 | [diff] [blame] | 3258 | if ((cur->compression > 0) && (cur->compression <= 9)) { |
| 3259 | sprintf(mode, "w%d", cur->compression); |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3260 | zoutput = gzopen(filename, mode); |
| 3261 | } |
| 3262 | if (zoutput == NULL) { |
| 3263 | #endif |
| 3264 | output = fopen(filename, "w"); |
| 3265 | if (output == NULL) return(-1); |
| 3266 | #ifdef HAVE_ZLIB_H |
| 3267 | } |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3268 | |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3269 | if (zoutput != NULL) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3270 | ret = gzwrite(zoutput, buf->content, sizeof(xmlChar) * buf->use); |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3271 | gzclose(zoutput); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3272 | } else { |
| 3273 | #endif |
| 3274 | ret = xmlBufferDump(output, buf); |
| 3275 | fclose(output); |
| 3276 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3277 | } |
| 3278 | #endif |
Manish Vachharajani | 5e60f5a | 1999-05-29 03:04:30 +0000 | [diff] [blame] | 3279 | xmlBufferFree(buf); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3280 | return(ret * sizeof(xmlChar)); |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |