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 | |
| 34 | static CHAR xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
| 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 |
| 92 | xmlNewNs(xmlNodePtr node, const CHAR *href, const CHAR *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 |
| 147 | xmlNewGlobalNs(xmlDocPtr doc, const CHAR *href, const CHAR *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 |
| 252 | xmlNewDtd(xmlDocPtr doc, const CHAR *name, |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 253 | const CHAR *ExternalID, const CHAR *SystemID) { |
| 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 |
| 304 | xmlCreateIntSubset(xmlDocPtr doc, const CHAR *name, |
| 305 | const CHAR *ExternalID, const CHAR *SystemID) { |
| 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: |
| 375 | * @version: CHAR string giving the version of XML "1.0" |
| 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 |
| 380 | xmlNewDoc(const CHAR *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 |
| 455 | xmlStringLenGetNodeList(xmlDocPtr doc, const CHAR *value, int len) { |
| 456 | xmlNodePtr ret = NULL, last = NULL; |
| 457 | xmlNodePtr node; |
| 458 | CHAR *val; |
| 459 | const CHAR *cur = value; |
| 460 | const CHAR *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 |
| 566 | xmlStringGetNodeList(xmlDocPtr doc, const CHAR *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 567 | xmlNodePtr ret = NULL, last = NULL; |
| 568 | xmlNodePtr node; |
| 569 | CHAR *val; |
| 570 | const CHAR *cur = value; |
| 571 | const CHAR *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 | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 677 | CHAR * |
| 678 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 679 | xmlNodePtr node = list; |
| 680 | CHAR *ret = NULL; |
| 681 | xmlEntityPtr ent; |
| 682 | |
| 683 | if (list == NULL) return(NULL); |
| 684 | |
| 685 | while (node != NULL) { |
| 686 | if (node->type == XML_TEXT_NODE) { |
| 687 | if (inLine) |
| 688 | ret = xmlStrcat(ret, node->content); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 689 | else { |
| 690 | CHAR *buffer; |
| 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 { |
| 706 | CHAR buf[2]; |
| 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 |
| 735 | xmlNewProp(xmlNodePtr node, const CHAR *name, const CHAR *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 | } |
| 779 | return(cur); |
| 780 | } |
| 781 | |
| 782 | /** |
| 783 | * xmlNewNsProp: |
| 784 | * @node: the holding node |
| 785 | * @ns: the namespace |
| 786 | * @name: the name of the attribute |
| 787 | * @value: the value of the attribute |
| 788 | * |
| 789 | * Create a new property tagged with a namespace and carried by a node. |
| 790 | * Returns a pointer to the attribute |
| 791 | */ |
| 792 | xmlAttrPtr |
| 793 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const CHAR *name, |
| 794 | const CHAR *value) { |
| 795 | xmlAttrPtr cur; |
| 796 | |
| 797 | if (name == NULL) { |
| 798 | fprintf(stderr, "xmlNewProp : name == NULL\n"); |
| 799 | return(NULL); |
| 800 | } |
| 801 | |
| 802 | /* |
| 803 | * Allocate a new property and fill the fields. |
| 804 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 805 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 806 | if (cur == NULL) { |
| 807 | fprintf(stderr, "xmlNewProp : malloc failed\n"); |
| 808 | return(NULL); |
| 809 | } |
| 810 | |
| 811 | cur->type = XML_ATTRIBUTE_NODE; |
| 812 | cur->node = node; |
| 813 | cur->ns = ns; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 814 | cur->name = xmlStrdup(name); |
| 815 | if (value != NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 816 | cur->val = xmlStringGetNodeList(node->doc, value); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 817 | else |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 818 | cur->val = NULL; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 819 | #ifndef XML_WITHOUT_CORBA |
Daniel Veillard | 27fb075 | 1998-10-17 06:47:46 +0000 | [diff] [blame] | 820 | cur->_private = NULL; |
| 821 | cur->vepv = NULL; |
| 822 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 823 | |
| 824 | /* |
| 825 | * Add it at the end to preserve parsing order ... |
| 826 | */ |
| 827 | cur->next = NULL; |
| 828 | if (node != NULL) { |
| 829 | if (node->properties == NULL) { |
| 830 | node->properties = cur; |
| 831 | } else { |
| 832 | xmlAttrPtr prev = node->properties; |
| 833 | |
| 834 | while (prev->next != NULL) prev = prev->next; |
| 835 | prev->next = cur; |
| 836 | } |
| 837 | } |
| 838 | return(cur); |
| 839 | } |
| 840 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 841 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 842 | * xmlNewDocProp: |
| 843 | * @doc: the document |
| 844 | * @name: the name of the attribute |
| 845 | * @value: the value of the attribute |
| 846 | * |
| 847 | * Create a new property carried by a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 848 | * Returns a pointer to the attribute |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 849 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 850 | xmlAttrPtr |
| 851 | xmlNewDocProp(xmlDocPtr doc, const CHAR *name, const CHAR *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 852 | xmlAttrPtr cur; |
| 853 | |
| 854 | if (name == NULL) { |
| 855 | fprintf(stderr, "xmlNewProp : name == NULL\n"); |
| 856 | return(NULL); |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | * Allocate a new property and fill the fields. |
| 861 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 862 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 863 | if (cur == NULL) { |
| 864 | fprintf(stderr, "xmlNewProp : malloc failed\n"); |
| 865 | return(NULL); |
| 866 | } |
| 867 | |
| 868 | cur->type = XML_ATTRIBUTE_NODE; |
| 869 | cur->node = NULL; |
| 870 | cur->name = xmlStrdup(name); |
| 871 | if (value != NULL) |
| 872 | cur->val = xmlStringGetNodeList(doc, value); |
| 873 | else |
| 874 | cur->val = NULL; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 875 | #ifndef XML_WITHOUT_CORBA |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 876 | cur->_private = NULL; |
| 877 | cur->vepv = NULL; |
| 878 | #endif |
| 879 | |
| 880 | cur->next = NULL; |
| 881 | return(cur); |
| 882 | } |
| 883 | |
| 884 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 885 | * xmlFreePropList: |
| 886 | * @cur: the first property in the list |
| 887 | * |
| 888 | * 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] | 889 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 890 | void |
| 891 | xmlFreePropList(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 892 | xmlAttrPtr next; |
| 893 | if (cur == NULL) { |
| 894 | fprintf(stderr, "xmlFreePropList : property == NULL\n"); |
| 895 | return; |
| 896 | } |
| 897 | while (cur != NULL) { |
| 898 | next = cur->next; |
| 899 | xmlFreeProp(cur); |
| 900 | cur = next; |
| 901 | } |
| 902 | } |
| 903 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 904 | /** |
| 905 | * xmlFreeProp: |
| 906 | * @cur: the first property in the list |
| 907 | * |
| 908 | * Free one property, all the childs are freed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 909 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 910 | void |
| 911 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 912 | if (cur == NULL) { |
| 913 | fprintf(stderr, "xmlFreeProp : property == NULL\n"); |
| 914 | return; |
| 915 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 916 | if (cur->name != NULL) xmlFree((char *) cur->name); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 917 | if (cur->val != NULL) xmlFreeNodeList(cur->val); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 918 | memset(cur, -1, sizeof(xmlAttr)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 919 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 922 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 923 | * xmlNewPI: |
| 924 | * @name: the processing instruction name |
| 925 | * @content: the PI content |
| 926 | * |
| 927 | * Creation of a processing instruction element. |
| 928 | * Returns a pointer to the new node object. |
| 929 | */ |
| 930 | xmlNodePtr |
| 931 | xmlNewPI(const CHAR *name, const CHAR *content) { |
| 932 | xmlNodePtr cur; |
| 933 | |
| 934 | if (name == NULL) { |
| 935 | fprintf(stderr, "xmlNewPI : name == NULL\n"); |
| 936 | return(NULL); |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * Allocate a new node and fill the fields. |
| 941 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 942 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 943 | if (cur == NULL) { |
| 944 | fprintf(stderr, "xmlNewPI : malloc failed\n"); |
| 945 | return(NULL); |
| 946 | } |
| 947 | |
| 948 | cur->type = XML_PI_NODE; |
| 949 | cur->doc = NULL; |
| 950 | cur->parent = NULL; |
| 951 | cur->next = NULL; |
| 952 | cur->prev = NULL; |
| 953 | cur->childs = NULL; |
| 954 | cur->last = NULL; |
| 955 | cur->properties = NULL; |
| 956 | cur->name = xmlStrdup(name); |
| 957 | cur->ns = NULL; |
| 958 | cur->nsDef = NULL; |
| 959 | if (content != NULL) |
| 960 | cur->content = xmlStrdup(content); |
| 961 | else |
| 962 | cur->content = NULL; |
| 963 | #ifndef XML_WITHOUT_CORBA |
| 964 | cur->_private = NULL; |
| 965 | cur->vepv = NULL; |
| 966 | #endif |
| 967 | return(cur); |
| 968 | } |
| 969 | |
| 970 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 971 | * xmlNewNode: |
| 972 | * @ns: namespace if any |
| 973 | * @name: the node name |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 974 | * |
| 975 | * Creation of a new node element. @ns and @content are optionnal (NULL). |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 976 | * If content is non NULL, a child list containing the TEXTs and |
| 977 | * ENTITY_REFs node will be created. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 978 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 979 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 980 | xmlNodePtr |
| 981 | xmlNewNode(xmlNsPtr ns, const CHAR *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 982 | xmlNodePtr cur; |
| 983 | |
| 984 | if (name == NULL) { |
| 985 | fprintf(stderr, "xmlNewNode : name == NULL\n"); |
| 986 | return(NULL); |
| 987 | } |
| 988 | |
| 989 | /* |
| 990 | * Allocate a new node and fill the fields. |
| 991 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 992 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 993 | if (cur == NULL) { |
| 994 | fprintf(stderr, "xmlNewNode : malloc failed\n"); |
| 995 | return(NULL); |
| 996 | } |
| 997 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 998 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 999 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1000 | cur->parent = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1001 | cur->next = NULL; |
| 1002 | cur->prev = NULL; |
| 1003 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1004 | cur->last = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1005 | cur->properties = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1006 | cur->name = xmlStrdup(name); |
| 1007 | cur->ns = ns; |
| 1008 | cur->nsDef = NULL; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1009 | cur->content = NULL; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 1010 | #ifndef XML_WITHOUT_CORBA |
Daniel Veillard | 27fb075 | 1998-10-17 06:47:46 +0000 | [diff] [blame] | 1011 | cur->_private = NULL; |
| 1012 | cur->vepv = NULL; |
| 1013 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1014 | return(cur); |
| 1015 | } |
| 1016 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1017 | /** |
| 1018 | * xmlNewDocNode: |
| 1019 | * @doc: the document |
| 1020 | * @ns: namespace if any |
| 1021 | * @name: the node name |
| 1022 | * @content: the text content if any |
| 1023 | * |
| 1024 | * Creation of a new node element within a document. @ns and @content |
| 1025 | * are optionnal (NULL). |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1026 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1027 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1028 | xmlNodePtr |
| 1029 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
Daniel Veillard | 15b75af | 1999-07-26 16:42:37 +0000 | [diff] [blame] | 1030 | const CHAR *name, const CHAR *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1031 | xmlNodePtr cur; |
| 1032 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1033 | cur = xmlNewNode(ns, name); |
| 1034 | if (cur != NULL) { |
| 1035 | cur->doc = doc; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1036 | if (content != NULL) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1037 | cur->childs = xmlStringGetNodeList(doc, content); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1038 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1039 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1040 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1041 | return(cur); |
| 1042 | } |
| 1043 | |
| 1044 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1045 | /** |
| 1046 | * xmlNewText: |
| 1047 | * @content: the text content |
| 1048 | * |
| 1049 | * Creation of a new text node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1050 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1051 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1052 | xmlNodePtr |
| 1053 | xmlNewText(const CHAR *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1054 | xmlNodePtr cur; |
| 1055 | |
| 1056 | /* |
| 1057 | * Allocate a new node and fill the fields. |
| 1058 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1059 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1060 | if (cur == NULL) { |
| 1061 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 1062 | return(NULL); |
| 1063 | } |
| 1064 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1065 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1066 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1067 | cur->parent = NULL; |
| 1068 | cur->next = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1069 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1070 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1071 | cur->last = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1072 | cur->properties = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1073 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1074 | cur->name = xmlStrdup(xmlStringText); |
| 1075 | cur->ns = NULL; |
| 1076 | cur->nsDef = NULL; |
| 1077 | if (content != NULL) |
| 1078 | cur->content = xmlStrdup(content); |
| 1079 | else |
| 1080 | cur->content = NULL; |
| 1081 | return(cur); |
| 1082 | } |
| 1083 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1084 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1085 | * xmlNewReference: |
| 1086 | * @doc: the document |
| 1087 | * @name: the reference name, or the reference string with & and ; |
| 1088 | * |
| 1089 | * Creation of a new reference node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1090 | * Returns a pointer to the new node object. |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1091 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1092 | xmlNodePtr |
| 1093 | xmlNewReference(xmlDocPtr doc, const CHAR *name) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1094 | xmlNodePtr cur; |
| 1095 | xmlEntityPtr ent; |
| 1096 | |
| 1097 | /* |
| 1098 | * Allocate a new node and fill the fields. |
| 1099 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1100 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1101 | if (cur == NULL) { |
| 1102 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 1103 | return(NULL); |
| 1104 | } |
| 1105 | |
| 1106 | cur->type = XML_ENTITY_REF_NODE; |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 1107 | cur->doc = doc; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1108 | cur->parent = NULL; |
| 1109 | cur->next = NULL; |
| 1110 | cur->prev = NULL; |
| 1111 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1112 | cur->last = NULL; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1113 | cur->properties = NULL; |
| 1114 | if (name[0] == '&') { |
| 1115 | int len; |
| 1116 | name++; |
| 1117 | len = xmlStrlen(name); |
| 1118 | if (name[len - 1] == ';') |
| 1119 | cur->name = xmlStrndup(name, len - 1); |
| 1120 | else |
| 1121 | cur->name = xmlStrndup(name, len); |
| 1122 | } else |
| 1123 | cur->name = xmlStrdup(name); |
| 1124 | cur->ns = NULL; |
| 1125 | cur->nsDef = NULL; |
| 1126 | |
| 1127 | ent = xmlGetDocEntity(doc, cur->name); |
| 1128 | if (ent != NULL) |
| 1129 | cur->content = ent->content; |
| 1130 | else |
| 1131 | cur->content = NULL; |
| 1132 | return(cur); |
| 1133 | } |
| 1134 | |
| 1135 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1136 | * xmlNewDocText: |
| 1137 | * @doc: the document |
| 1138 | * @content: the text content |
| 1139 | * |
| 1140 | * Creation of a new text node within a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1141 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1142 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1143 | xmlNodePtr |
| 1144 | xmlNewDocText(xmlDocPtr doc, const CHAR *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1145 | xmlNodePtr cur; |
| 1146 | |
| 1147 | cur = xmlNewText(content); |
| 1148 | if (cur != NULL) cur->doc = doc; |
| 1149 | return(cur); |
| 1150 | } |
| 1151 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1152 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1153 | * xmlNewTextLen: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1154 | * @content: the text content |
| 1155 | * @len: the text len. |
| 1156 | * |
| 1157 | * 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] | 1158 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1159 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1160 | xmlNodePtr |
| 1161 | xmlNewTextLen(const CHAR *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1162 | xmlNodePtr cur; |
| 1163 | |
| 1164 | /* |
| 1165 | * Allocate a new node and fill the fields. |
| 1166 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1167 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1168 | if (cur == NULL) { |
| 1169 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 1170 | return(NULL); |
| 1171 | } |
| 1172 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1173 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1174 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1175 | cur->parent = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1176 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1177 | cur->next = NULL; |
| 1178 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1179 | cur->last = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1180 | cur->properties = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1181 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1182 | cur->name = xmlStrdup(xmlStringText); |
| 1183 | cur->ns = NULL; |
| 1184 | cur->nsDef = NULL; |
| 1185 | if (content != NULL) |
| 1186 | cur->content = xmlStrndup(content, len); |
| 1187 | else |
| 1188 | cur->content = NULL; |
| 1189 | return(cur); |
| 1190 | } |
| 1191 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1192 | /** |
| 1193 | * xmlNewDocTextLen: |
| 1194 | * @doc: the document |
| 1195 | * @content: the text content |
| 1196 | * @len: the text len. |
| 1197 | * |
| 1198 | * Creation of a new text node with an extra content lenght parameter. The |
| 1199 | * text node pertain to a given document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1200 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1201 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1202 | xmlNodePtr |
| 1203 | xmlNewDocTextLen(xmlDocPtr doc, const CHAR *content, int len) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1204 | xmlNodePtr cur; |
| 1205 | |
| 1206 | cur = xmlNewTextLen(content, len); |
| 1207 | if (cur != NULL) cur->doc = doc; |
| 1208 | return(cur); |
| 1209 | } |
| 1210 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1211 | /** |
| 1212 | * xmlNewComment: |
| 1213 | * @content: the comment content |
| 1214 | * |
| 1215 | * Creation of a new node containing a comment. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1216 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1217 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1218 | xmlNodePtr |
Daniel Veillard | 517752b | 1999-04-05 12:20:10 +0000 | [diff] [blame] | 1219 | xmlNewComment(const CHAR *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1220 | xmlNodePtr cur; |
| 1221 | |
| 1222 | /* |
| 1223 | * Allocate a new node and fill the fields. |
| 1224 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1225 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1226 | if (cur == NULL) { |
| 1227 | fprintf(stderr, "xmlNewComment : malloc failed\n"); |
| 1228 | return(NULL); |
| 1229 | } |
| 1230 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1231 | cur->type = XML_COMMENT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1232 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1233 | cur->parent = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1234 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1235 | cur->next = NULL; |
| 1236 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1237 | cur->last = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1238 | cur->properties = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1239 | cur->type = XML_COMMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1240 | cur->name = xmlStrdup(xmlStringText); |
| 1241 | cur->ns = NULL; |
| 1242 | cur->nsDef = NULL; |
| 1243 | if (content != NULL) |
| 1244 | cur->content = xmlStrdup(content); |
| 1245 | else |
| 1246 | cur->content = NULL; |
| 1247 | return(cur); |
| 1248 | } |
| 1249 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1250 | /** |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1251 | * xmlNewCDataBlock: |
| 1252 | * @doc: the document |
| 1253 | * @content: the CData block content content |
| 1254 | * @len: the length of the block |
| 1255 | * |
| 1256 | * Creation of a new node containing a CData block. |
| 1257 | * Returns a pointer to the new node object. |
| 1258 | */ |
| 1259 | xmlNodePtr |
| 1260 | xmlNewCDataBlock(xmlDocPtr doc, const CHAR *content, int len) { |
| 1261 | xmlNodePtr cur; |
| 1262 | |
| 1263 | /* |
| 1264 | * Allocate a new node and fill the fields. |
| 1265 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1266 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1267 | if (cur == NULL) { |
| 1268 | fprintf(stderr, "xmlNewCDataBlock : malloc failed\n"); |
| 1269 | return(NULL); |
| 1270 | } |
| 1271 | |
| 1272 | cur->type = XML_CDATA_SECTION_NODE; |
| 1273 | cur->doc = NULL; |
| 1274 | cur->parent = NULL; |
| 1275 | cur->prev = NULL; |
| 1276 | cur->next = NULL; |
| 1277 | cur->childs = NULL; |
| 1278 | cur->last = NULL; |
| 1279 | cur->properties = NULL; |
| 1280 | cur->name = xmlStrdup(xmlStringText); |
| 1281 | cur->ns = NULL; |
| 1282 | cur->nsDef = NULL; |
| 1283 | if ((content != NULL) && (len > 0)) { |
| 1284 | cur->content = xmlStrndup(content, len); |
| 1285 | } else |
| 1286 | cur->content = NULL; |
| 1287 | return(cur); |
| 1288 | } |
| 1289 | |
| 1290 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1291 | * xmlNewDocComment: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1292 | * @doc: the document |
| 1293 | * @content: the comment content |
| 1294 | * |
| 1295 | * Creation of a new node containing a commentwithin a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1296 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1297 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1298 | xmlNodePtr |
Daniel Veillard | 517752b | 1999-04-05 12:20:10 +0000 | [diff] [blame] | 1299 | xmlNewDocComment(xmlDocPtr doc, const CHAR *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1300 | xmlNodePtr cur; |
| 1301 | |
| 1302 | cur = xmlNewComment(content); |
| 1303 | if (cur != NULL) cur->doc = doc; |
| 1304 | return(cur); |
| 1305 | } |
| 1306 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1307 | /** |
| 1308 | * xmlNewChild: |
| 1309 | * @parent: the parent node |
| 1310 | * @ns: a namespace if any |
| 1311 | * @name: the name of the child |
| 1312 | * @content: the content of the child if any. |
| 1313 | * |
| 1314 | * |
| 1315 | * 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] | 1316 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1317 | * 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] | 1318 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1319 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1320 | xmlNodePtr |
| 1321 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
Daniel Veillard | 15b75af | 1999-07-26 16:42:37 +0000 | [diff] [blame] | 1322 | const CHAR *name, const CHAR *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1323 | xmlNodePtr cur, prev; |
| 1324 | |
| 1325 | if (parent == NULL) { |
| 1326 | fprintf(stderr, "xmlNewChild : parent == NULL\n"); |
| 1327 | return(NULL); |
| 1328 | } |
| 1329 | |
| 1330 | if (name == NULL) { |
| 1331 | fprintf(stderr, "xmlNewChild : name == NULL\n"); |
| 1332 | return(NULL); |
| 1333 | } |
| 1334 | |
| 1335 | /* |
| 1336 | * Allocate a new node |
| 1337 | */ |
| 1338 | if (ns == NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1339 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1340 | else |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1341 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1342 | if (cur == NULL) return(NULL); |
| 1343 | |
| 1344 | /* |
| 1345 | * add the new element at the end of the childs list. |
| 1346 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1347 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1348 | cur->parent = parent; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1349 | cur->doc = parent->doc; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1350 | if (parent->childs == NULL) { |
| 1351 | parent->childs = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1352 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1353 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1354 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1355 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1356 | cur->prev = prev; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1357 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | return(cur); |
| 1361 | } |
| 1362 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1363 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1364 | * xmlAddSibling: |
| 1365 | * @cur: the child node |
| 1366 | * @elem: the new node |
| 1367 | * |
| 1368 | * Add a new element to the list of siblings of @cur |
| 1369 | * Returns the element or NULL in case of error. |
| 1370 | */ |
| 1371 | xmlNodePtr |
| 1372 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1373 | xmlNodePtr parent; |
| 1374 | |
| 1375 | if (cur == NULL) { |
| 1376 | fprintf(stderr, "xmlAddSibling : cur == NULL\n"); |
| 1377 | return(NULL); |
| 1378 | } |
| 1379 | |
| 1380 | if (elem == NULL) { |
| 1381 | fprintf(stderr, "xmlAddSibling : elem == NULL\n"); |
| 1382 | return(NULL); |
| 1383 | } |
| 1384 | |
| 1385 | if ((cur->doc != NULL) && (elem->doc != NULL) && |
| 1386 | (cur->doc != elem->doc)) { |
| 1387 | fprintf(stderr, |
| 1388 | "xmlAddSibling: Elements moved to a different document\n"); |
| 1389 | } |
| 1390 | |
| 1391 | while (cur->next != NULL) cur = cur->next; |
| 1392 | |
| 1393 | if (elem->doc == NULL) |
| 1394 | elem->doc = cur->doc; /* the parent may not be linked to a doc ! */ |
| 1395 | |
| 1396 | parent = cur->parent; |
| 1397 | elem->prev = cur; |
| 1398 | elem->next = NULL; |
| 1399 | elem->parent = parent; |
| 1400 | cur->next = elem; |
| 1401 | if (parent != NULL) |
| 1402 | parent->last = elem; |
| 1403 | |
| 1404 | return(elem); |
| 1405 | } |
| 1406 | |
| 1407 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1408 | * xmlAddChild: |
| 1409 | * @parent: the parent node |
| 1410 | * @cur: the child node |
| 1411 | * |
| 1412 | * 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] | 1413 | * Returns the child or NULL in case of error. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1414 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1415 | xmlNodePtr |
| 1416 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1417 | xmlNodePtr prev; |
| 1418 | |
| 1419 | if (parent == NULL) { |
| 1420 | fprintf(stderr, "xmladdChild : parent == NULL\n"); |
| 1421 | return(NULL); |
| 1422 | } |
| 1423 | |
| 1424 | if (cur == NULL) { |
| 1425 | fprintf(stderr, "xmladdChild : child == NULL\n"); |
| 1426 | return(NULL); |
| 1427 | } |
| 1428 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1429 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 1430 | (cur->doc != parent->doc)) { |
| 1431 | fprintf(stderr, "Elements moved to a different document\n"); |
| 1432 | } |
| 1433 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1434 | /* |
| 1435 | * add the new element at the end of the childs list. |
| 1436 | */ |
| 1437 | cur->parent = parent; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1438 | 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] | 1439 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1440 | /* |
| 1441 | * Handle the case where parent->content != NULL, in that case it will |
| 1442 | * create a intermediate TEXT node. |
| 1443 | */ |
| 1444 | if (parent->content != NULL) { |
| 1445 | xmlNodePtr text; |
| 1446 | |
| 1447 | text = xmlNewDocText(parent->doc, parent->content); |
| 1448 | if (text != NULL) { |
| 1449 | text->next = parent->childs; |
| 1450 | if (text->next != NULL) |
| 1451 | text->next->prev = text; |
| 1452 | parent->childs = text; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1453 | UPDATE_LAST_CHILD(parent) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1454 | xmlFree(parent->content); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1455 | parent->content = NULL; |
| 1456 | } |
| 1457 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1458 | if (parent->childs == NULL) { |
| 1459 | parent->childs = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1460 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1461 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1462 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1463 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1464 | cur->prev = prev; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1465 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | return(cur); |
| 1469 | } |
| 1470 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1471 | /** |
| 1472 | * xmlGetLastChild: |
| 1473 | * @parent: the parent node |
| 1474 | * |
| 1475 | * Search the last child of a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1476 | * Returns the last child or NULL if none. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1477 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1478 | xmlNodePtr |
| 1479 | xmlGetLastChild(xmlNodePtr parent) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1480 | if (parent == NULL) { |
| 1481 | fprintf(stderr, "xmlGetLastChild : parent == NULL\n"); |
| 1482 | return(NULL); |
| 1483 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1484 | return(parent->last); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1487 | /** |
| 1488 | * xmlFreeNodeList: |
| 1489 | * @cur: the first node in the list |
| 1490 | * |
| 1491 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 1492 | * the childs are freed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1493 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1494 | void |
| 1495 | xmlFreeNodeList(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1496 | xmlNodePtr next; |
| 1497 | if (cur == NULL) { |
| 1498 | fprintf(stderr, "xmlFreeNodeList : node == NULL\n"); |
| 1499 | return; |
| 1500 | } |
| 1501 | while (cur != NULL) { |
| 1502 | next = cur->next; |
| 1503 | xmlFreeNode(cur); |
| 1504 | cur = next; |
| 1505 | } |
| 1506 | } |
| 1507 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1508 | /** |
| 1509 | * xmlFreeNode: |
| 1510 | * @cur: the node |
| 1511 | * |
| 1512 | * 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] | 1513 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1514 | void |
| 1515 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1516 | if (cur == NULL) { |
| 1517 | fprintf(stderr, "xmlFreeNode : node == NULL\n"); |
| 1518 | return; |
| 1519 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1520 | cur->doc = NULL; |
| 1521 | cur->parent = NULL; |
| 1522 | cur->next = NULL; |
| 1523 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1524 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1525 | if (cur->properties != NULL) xmlFreePropList(cur->properties); |
| 1526 | if (cur->type != XML_ENTITY_REF_NODE) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1527 | if (cur->content != NULL) xmlFree(cur->content); |
| 1528 | if (cur->name != NULL) xmlFree((char *) cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1529 | if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef); |
| 1530 | memset(cur, -1, sizeof(xmlNode)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1531 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1534 | /** |
| 1535 | * xmlUnlinkNode: |
| 1536 | * @cur: the node |
| 1537 | * |
| 1538 | * Unlink a node from it's current context, the node is not freed |
| 1539 | */ |
| 1540 | void |
| 1541 | xmlUnlinkNode(xmlNodePtr cur) { |
| 1542 | if (cur == NULL) { |
| 1543 | fprintf(stderr, "xmlUnlinkNode : node == NULL\n"); |
| 1544 | return; |
| 1545 | } |
| 1546 | if ((cur->parent != NULL) && (cur->parent->childs == cur)) |
| 1547 | cur->parent->childs = cur->next; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1548 | if ((cur->parent != NULL) && (cur->parent->last == cur)) |
| 1549 | cur->parent->last = cur->prev; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1550 | if (cur->next != NULL) |
| 1551 | cur->next->prev = cur->prev; |
| 1552 | if (cur->prev != NULL) |
| 1553 | cur->prev->next = cur->next; |
| 1554 | cur->next = cur->prev = NULL; |
| 1555 | cur->parent = NULL; |
| 1556 | } |
| 1557 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1558 | /************************************************************************ |
| 1559 | * * |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1560 | * Copy operations * |
| 1561 | * * |
| 1562 | ************************************************************************/ |
| 1563 | |
| 1564 | /** |
| 1565 | * xmlCopyNamespace: |
| 1566 | * @cur: the namespace |
| 1567 | * |
| 1568 | * Do a copy of the namespace. |
| 1569 | * |
| 1570 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 1571 | */ |
| 1572 | xmlNsPtr |
| 1573 | xmlCopyNamespace(xmlNsPtr cur) { |
| 1574 | xmlNsPtr ret; |
| 1575 | |
| 1576 | if (cur == NULL) return(NULL); |
| 1577 | switch (cur->type) { |
| 1578 | case XML_GLOBAL_NAMESPACE: |
| 1579 | ret = xmlNewGlobalNs(NULL, cur->href, cur->prefix); |
| 1580 | break; |
| 1581 | case XML_LOCAL_NAMESPACE: |
| 1582 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 1583 | break; |
| 1584 | default: |
| 1585 | fprintf(stderr, "xmlCopyNamespace: unknown type %d\n", cur->type); |
| 1586 | return(NULL); |
| 1587 | } |
| 1588 | return(ret); |
| 1589 | } |
| 1590 | |
| 1591 | /** |
| 1592 | * xmlCopyNamespaceList: |
| 1593 | * @cur: the first namespace |
| 1594 | * |
| 1595 | * Do a copy of an namespace list. |
| 1596 | * |
| 1597 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 1598 | */ |
| 1599 | xmlNsPtr |
| 1600 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 1601 | xmlNsPtr ret = NULL; |
| 1602 | xmlNsPtr p = NULL,q; |
| 1603 | |
| 1604 | while (cur != NULL) { |
| 1605 | q = xmlCopyNamespace(cur); |
| 1606 | if (p == NULL) { |
| 1607 | ret = p = q; |
| 1608 | } else { |
| 1609 | p->next = q; |
| 1610 | p = q; |
| 1611 | } |
| 1612 | cur = cur->next; |
| 1613 | } |
| 1614 | return(ret); |
| 1615 | } |
| 1616 | |
| 1617 | /** |
| 1618 | * xmlCopyProp: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1619 | * @target: the element where the attribute will be grafted |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1620 | * @cur: the attribute |
| 1621 | * |
| 1622 | * Do a copy of the attribute. |
| 1623 | * |
| 1624 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 1625 | */ |
| 1626 | xmlAttrPtr |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1627 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1628 | xmlAttrPtr ret; |
| 1629 | |
| 1630 | if (cur == NULL) return(NULL); |
| 1631 | if (cur->val != NULL) |
| 1632 | ret = xmlNewDocProp(cur->val->doc, cur->name, NULL); |
| 1633 | else |
| 1634 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 1635 | if (ret == NULL) return(NULL); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1636 | |
| 1637 | if ((cur->ns != NULL) && (target != NULL)) { |
| 1638 | xmlNsPtr ns; |
| 1639 | |
| 1640 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 1641 | ret->ns = ns; |
| 1642 | } else |
| 1643 | ret->ns = NULL; |
| 1644 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1645 | if (cur->val != NULL) |
| 1646 | ret->val = xmlCopyNodeList(cur->val); |
| 1647 | return(ret); |
| 1648 | } |
| 1649 | |
| 1650 | /** |
| 1651 | * xmlCopyPropList: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1652 | * @target: the element where the attributes will be grafted |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1653 | * @cur: the first attribute |
| 1654 | * |
| 1655 | * Do a copy of an attribute list. |
| 1656 | * |
| 1657 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 1658 | */ |
| 1659 | xmlAttrPtr |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1660 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1661 | xmlAttrPtr ret = NULL; |
| 1662 | xmlAttrPtr p = NULL,q; |
| 1663 | |
| 1664 | while (cur != NULL) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1665 | q = xmlCopyProp(target, cur); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1666 | if (p == NULL) { |
| 1667 | ret = p = q; |
| 1668 | } else { |
| 1669 | p->next = q; |
| 1670 | p = q; |
| 1671 | } |
| 1672 | cur = cur->next; |
| 1673 | } |
| 1674 | return(ret); |
| 1675 | } |
| 1676 | |
| 1677 | /* |
| 1678 | * NOTE about the CopyNode operations ! |
| 1679 | * |
| 1680 | * They are splitted into external and internal parts for one |
| 1681 | * tricky reason: namespaces. Doing a direct copy of a node |
| 1682 | * say RPM:Copyright without changing the namespace pointer to |
| 1683 | * something else can produce stale links. One way to do it is |
| 1684 | * to keep a reference counter but this doesn't work as soon |
| 1685 | * as one move the element or the subtree out of the scope of |
| 1686 | * the existing namespace. The actual solution seems to add |
| 1687 | * a copy of the namespace at the top of the copied tree if |
| 1688 | * not available in the subtree. |
| 1689 | * Hence two functions, the public front-end call the inner ones |
| 1690 | */ |
| 1691 | |
| 1692 | static xmlNodePtr |
| 1693 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
| 1694 | |
| 1695 | static xmlNodePtr |
| 1696 | xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
| 1697 | int recursive) { |
| 1698 | xmlNodePtr ret; |
| 1699 | |
| 1700 | if (node == NULL) return(NULL); |
| 1701 | /* |
| 1702 | * Allocate a new node and fill the fields. |
| 1703 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1704 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1705 | if (ret == NULL) { |
| 1706 | fprintf(stderr, "xmlStaticCopyNode : malloc failed\n"); |
| 1707 | return(NULL); |
| 1708 | } |
| 1709 | |
| 1710 | ret->type = node->type; |
| 1711 | ret->doc = doc; |
| 1712 | ret->parent = parent; |
| 1713 | ret->next = NULL; |
| 1714 | ret->prev = NULL; |
| 1715 | ret->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1716 | ret->last = NULL; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1717 | ret->properties = NULL; |
| 1718 | if (node->name != NULL) |
| 1719 | ret->name = xmlStrdup(node->name); |
| 1720 | else |
| 1721 | ret->name = NULL; |
| 1722 | ret->ns = NULL; |
| 1723 | ret->nsDef = NULL; |
| 1724 | if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) |
| 1725 | ret->content = xmlStrdup(node->content); |
| 1726 | else |
| 1727 | ret->content = NULL; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 1728 | #ifndef XML_WITHOUT_CORBA |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1729 | ret->_private = NULL; |
| 1730 | ret->vepv = NULL; |
| 1731 | #endif |
| 1732 | if (parent != NULL) |
| 1733 | xmlAddChild(parent, ret); |
| 1734 | |
| 1735 | if (!recursive) return(ret); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1736 | if (node->nsDef != NULL) |
| 1737 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 1738 | |
| 1739 | if (node->ns != NULL) { |
| 1740 | xmlNsPtr ns; |
| 1741 | |
| 1742 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 1743 | if (ns == NULL) { |
| 1744 | /* |
| 1745 | * Humm, we are copying an element whose namespace is defined |
| 1746 | * out of the new tree scope. Search it in the original tree |
| 1747 | * and add it at the top of the new tree |
| 1748 | */ |
| 1749 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 1750 | if (ns != NULL) { |
| 1751 | xmlNodePtr root = ret; |
| 1752 | |
| 1753 | while (root->parent != NULL) root = root->parent; |
| 1754 | xmlNewNs(root, ns->href, ns->prefix); |
| 1755 | } |
| 1756 | } else { |
| 1757 | /* |
| 1758 | * reference the existing namespace definition in our own tree. |
| 1759 | */ |
| 1760 | ret->ns = ns; |
| 1761 | } |
| 1762 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1763 | if (node->properties != NULL) |
| 1764 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1765 | if (node->childs != NULL) |
| 1766 | ret->childs = xmlStaticCopyNodeList(node->childs, doc, ret); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1767 | UPDATE_LAST_CHILD(ret) |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1768 | return(ret); |
| 1769 | } |
| 1770 | |
| 1771 | static xmlNodePtr |
| 1772 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 1773 | xmlNodePtr ret = NULL; |
| 1774 | xmlNodePtr p = NULL,q; |
| 1775 | |
| 1776 | while (node != NULL) { |
| 1777 | q = xmlStaticCopyNode(node, doc, parent, 1); |
| 1778 | if (parent == NULL) { |
| 1779 | if (ret == NULL) ret = q; |
| 1780 | } else { |
| 1781 | if (ret == NULL) { |
| 1782 | q->prev = NULL; |
| 1783 | ret = p = q; |
| 1784 | } else { |
| 1785 | p->next = q; |
| 1786 | q->prev = p; |
| 1787 | p = q; |
| 1788 | } |
| 1789 | } |
| 1790 | node = node->next; |
| 1791 | } |
| 1792 | return(ret); |
| 1793 | } |
| 1794 | |
| 1795 | /** |
| 1796 | * xmlCopyNode: |
| 1797 | * @node: the node |
| 1798 | * @recursive: if 1 do a recursive copy. |
| 1799 | * |
| 1800 | * Do a copy of the node. |
| 1801 | * |
| 1802 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 1803 | */ |
| 1804 | xmlNodePtr |
| 1805 | xmlCopyNode(xmlNodePtr node, int recursive) { |
| 1806 | xmlNodePtr ret; |
| 1807 | |
| 1808 | ret = xmlStaticCopyNode(node, NULL, NULL, recursive); |
| 1809 | return(ret); |
| 1810 | } |
| 1811 | |
| 1812 | /** |
| 1813 | * xmlCopyNodeList: |
| 1814 | * @node: the first node in the list. |
| 1815 | * |
| 1816 | * Do a recursive copy of the node list. |
| 1817 | * |
| 1818 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 1819 | */ |
| 1820 | xmlNodePtr xmlCopyNodeList(xmlNodePtr node) { |
| 1821 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 1822 | return(ret); |
| 1823 | } |
| 1824 | |
| 1825 | /** |
| 1826 | * xmlCopyElement: |
| 1827 | * @elem: the element |
| 1828 | * |
| 1829 | * Do a copy of the element definition. |
| 1830 | * |
| 1831 | * Returns: a new xmlElementPtr, or NULL in case of error. |
| 1832 | xmlElementPtr |
| 1833 | xmlCopyElement(xmlElementPtr elem) { |
| 1834 | xmlElementPtr ret; |
| 1835 | |
| 1836 | if (elem == NULL) return(NULL); |
| 1837 | ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content); |
| 1838 | if (ret == NULL) return(NULL); |
| 1839 | if (!recursive) return(ret); |
| 1840 | if (elem->properties != NULL) |
| 1841 | ret->properties = xmlCopyPropList(elem->properties); |
| 1842 | |
| 1843 | if (elem->nsDef != NULL) |
| 1844 | ret->nsDef = xmlCopyNamespaceList(elem->nsDef); |
| 1845 | if (elem->childs != NULL) |
| 1846 | ret->childs = xmlCopyElementList(elem->childs); |
| 1847 | return(ret); |
| 1848 | } |
| 1849 | */ |
| 1850 | |
| 1851 | /** |
| 1852 | * xmlCopyDtd: |
| 1853 | * @dtd: the dtd |
| 1854 | * |
| 1855 | * Do a copy of the dtd. |
| 1856 | * |
| 1857 | * Returns: a new xmlDtdPtr, or NULL in case of error. |
| 1858 | */ |
| 1859 | xmlDtdPtr |
| 1860 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 1861 | xmlDtdPtr ret; |
| 1862 | |
| 1863 | if (dtd == NULL) return(NULL); |
| 1864 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 1865 | if (ret == NULL) return(NULL); |
| 1866 | if (dtd->entities != NULL) |
| 1867 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 1868 | (xmlEntitiesTablePtr) dtd->entities); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1869 | if (dtd->notations != NULL) |
| 1870 | ret->notations = (void *) xmlCopyNotationTable( |
| 1871 | (xmlNotationTablePtr) dtd->notations); |
| 1872 | if (dtd->elements != NULL) |
| 1873 | ret->elements = (void *) xmlCopyElementTable( |
| 1874 | (xmlElementTablePtr) dtd->elements); |
| 1875 | if (dtd->attributes != NULL) |
| 1876 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 1877 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1878 | return(ret); |
| 1879 | } |
| 1880 | |
| 1881 | /** |
| 1882 | * xmlCopyDoc: |
| 1883 | * @doc: the document |
| 1884 | * @recursive: if 1 do a recursive copy. |
| 1885 | * |
| 1886 | * Do a copy of the document info. If recursive, the content tree will |
| 1887 | * be copied too as well as Dtd, namespaces and entities. |
| 1888 | * |
| 1889 | * Returns: a new xmlDocPtr, or NULL in case of error. |
| 1890 | */ |
| 1891 | xmlDocPtr |
| 1892 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 1893 | xmlDocPtr ret; |
| 1894 | |
| 1895 | if (doc == NULL) return(NULL); |
| 1896 | ret = xmlNewDoc(doc->version); |
| 1897 | if (ret == NULL) return(NULL); |
| 1898 | if (doc->name != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1899 | ret->name = xmlMemStrdup(doc->name); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1900 | if (doc->encoding != NULL) |
| 1901 | ret->encoding = xmlStrdup(doc->encoding); |
| 1902 | ret->compression = doc->compression; |
| 1903 | ret->standalone = doc->standalone; |
| 1904 | if (!recursive) return(ret); |
| 1905 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1906 | if (doc->intSubset != NULL) |
| 1907 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1908 | if (doc->oldNs != NULL) |
| 1909 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 1910 | if (doc->root != NULL) |
| 1911 | ret->root = xmlStaticCopyNodeList(doc->root, ret, NULL); |
| 1912 | return(ret); |
| 1913 | } |
| 1914 | |
| 1915 | /************************************************************************ |
| 1916 | * * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1917 | * Content access functions * |
| 1918 | * * |
| 1919 | ************************************************************************/ |
| 1920 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1921 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1922 | * xmlNodeSetLang: |
| 1923 | * @cur: the node being changed |
| 1924 | * @lang: the langage description |
| 1925 | * |
| 1926 | * Searches the language of a node, i.e. the values of the xml:lang |
| 1927 | * attribute or the one carried by the nearest ancestor. |
| 1928 | * |
| 1929 | * Returns a pointer to the lang value, or NULL if not found |
| 1930 | */ |
| 1931 | void |
| 1932 | xmlNodeSetLang(xmlNodePtr cur, const CHAR *lang) { |
| 1933 | /* TODO xmlNodeSetLang check against the production [33] LanguageID */ |
| 1934 | xmlSetProp(cur, BAD_CAST "xml:lang", lang); |
| 1935 | } |
| 1936 | |
| 1937 | /** |
| 1938 | * xmlNodeGetLang: |
| 1939 | * @cur: the node being checked |
| 1940 | * |
| 1941 | * Searches the language of a node, i.e. the values of the xml:lang |
| 1942 | * attribute or the one carried by the nearest ancestor. |
| 1943 | * |
| 1944 | * Returns a pointer to the lang value, or NULL if not found |
| 1945 | */ |
| 1946 | const CHAR * |
| 1947 | xmlNodeGetLang(xmlNodePtr cur) { |
| 1948 | const CHAR *lang; |
| 1949 | |
| 1950 | while (cur != NULL) { |
| 1951 | lang = xmlGetProp(cur, BAD_CAST "xml:lang"); |
| 1952 | if (lang != NULL) |
| 1953 | return(lang); |
| 1954 | cur = cur->parent; |
| 1955 | } |
| 1956 | return(NULL); |
| 1957 | } |
| 1958 | |
| 1959 | /** |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1960 | * xmlNodeGetContent: |
| 1961 | * @cur: the node being read |
| 1962 | * |
| 1963 | * Read the value of a node, this can be either the text carried |
| 1964 | * directly by this node if it's a TEXT node or the aggregate string |
| 1965 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 1966 | * Entity references are substitued. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1967 | * Returns a new CHAR * or NULL if no content is available. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1968 | * It's up to the caller to free the memory. |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1969 | */ |
| 1970 | CHAR * |
| 1971 | xmlNodeGetContent(xmlNodePtr cur) { |
| 1972 | if (cur == NULL) return(NULL); |
| 1973 | switch (cur->type) { |
| 1974 | case XML_DOCUMENT_FRAG_NODE: |
| 1975 | case XML_ELEMENT_NODE: |
| 1976 | return(xmlNodeListGetString(cur->doc, cur->childs, 1)); |
| 1977 | break; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1978 | case XML_ATTRIBUTE_NODE: { |
| 1979 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 1980 | if (attr->node != NULL) |
| 1981 | return(xmlNodeListGetString(attr->node->doc, attr->val, 1)); |
| 1982 | else |
| 1983 | return(xmlNodeListGetString(NULL, attr->val, 1)); |
| 1984 | break; |
| 1985 | } |
| 1986 | case XML_PI_NODE: |
| 1987 | if (cur->content != NULL) |
| 1988 | return(xmlStrdup(cur->content)); |
| 1989 | return(NULL); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1990 | case XML_ENTITY_REF_NODE: |
| 1991 | case XML_ENTITY_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1992 | case XML_COMMENT_NODE: |
| 1993 | case XML_DOCUMENT_NODE: |
| 1994 | case XML_DOCUMENT_TYPE_NODE: |
| 1995 | case XML_NOTATION_NODE: |
| 1996 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1997 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1998 | case XML_TEXT_NODE: |
| 1999 | if (cur->content != NULL) |
| 2000 | return(xmlStrdup(cur->content)); |
| 2001 | return(NULL); |
| 2002 | } |
| 2003 | return(NULL); |
| 2004 | } |
| 2005 | |
| 2006 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2007 | * xmlNodeSetContent: |
| 2008 | * @cur: the node being modified |
| 2009 | * @content: the new value of the content |
| 2010 | * |
| 2011 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2012 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2013 | void |
| 2014 | xmlNodeSetContent(xmlNodePtr cur, const CHAR *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2015 | if (cur == NULL) { |
| 2016 | fprintf(stderr, "xmlNodeSetContent : node == NULL\n"); |
| 2017 | return; |
| 2018 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2019 | switch (cur->type) { |
| 2020 | case XML_DOCUMENT_FRAG_NODE: |
| 2021 | case XML_ELEMENT_NODE: |
| 2022 | if (cur->content != NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2023 | xmlFree(cur->content); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2024 | cur->content = NULL; |
| 2025 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2026 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2027 | cur->childs = xmlStringGetNodeList(cur->doc, content); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2028 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2029 | break; |
| 2030 | case XML_ATTRIBUTE_NODE: |
| 2031 | break; |
| 2032 | case XML_TEXT_NODE: |
| 2033 | case XML_CDATA_SECTION_NODE: |
| 2034 | case XML_ENTITY_REF_NODE: |
| 2035 | case XML_ENTITY_NODE: |
| 2036 | case XML_PI_NODE: |
| 2037 | case XML_COMMENT_NODE: |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2038 | if (cur->content != NULL) xmlFree(cur->content); |
| 2039 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2040 | cur->last = cur->childs = NULL; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2041 | if (content != NULL) |
| 2042 | cur->content = xmlStrdup(content); |
| 2043 | else |
| 2044 | cur->content = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2045 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2046 | case XML_DOCUMENT_NODE: |
| 2047 | case XML_DOCUMENT_TYPE_NODE: |
| 2048 | break; |
| 2049 | case XML_NOTATION_NODE: |
| 2050 | break; |
| 2051 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2054 | /** |
| 2055 | * xmlNodeSetContentLen: |
| 2056 | * @cur: the node being modified |
| 2057 | * @content: the new value of the content |
| 2058 | * @len: the size of @content |
| 2059 | * |
| 2060 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2061 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2062 | void |
| 2063 | xmlNodeSetContentLen(xmlNodePtr cur, const CHAR *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2064 | if (cur == NULL) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2065 | fprintf(stderr, "xmlNodeSetContentLen : node == NULL\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2066 | return; |
| 2067 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2068 | switch (cur->type) { |
| 2069 | case XML_DOCUMENT_FRAG_NODE: |
| 2070 | case XML_ELEMENT_NODE: |
| 2071 | if (cur->content != NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2072 | xmlFree(cur->content); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2073 | cur->content = NULL; |
| 2074 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2075 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2076 | cur->childs = xmlStringLenGetNodeList(cur->doc, content, len); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2077 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2078 | break; |
| 2079 | case XML_ATTRIBUTE_NODE: |
| 2080 | break; |
| 2081 | case XML_TEXT_NODE: |
| 2082 | case XML_CDATA_SECTION_NODE: |
| 2083 | case XML_ENTITY_REF_NODE: |
| 2084 | case XML_ENTITY_NODE: |
| 2085 | case XML_PI_NODE: |
| 2086 | case XML_COMMENT_NODE: |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2087 | if (cur->content != NULL) xmlFree(cur->content); |
| 2088 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2089 | cur->childs = cur->last = NULL; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2090 | if (content != NULL) |
| 2091 | cur->content = xmlStrndup(content, len); |
| 2092 | else |
| 2093 | cur->content = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2094 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2095 | case XML_DOCUMENT_NODE: |
| 2096 | case XML_DOCUMENT_TYPE_NODE: |
| 2097 | break; |
| 2098 | case XML_NOTATION_NODE: |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2099 | if (cur->content != NULL) xmlFree(cur->content); |
| 2100 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2101 | cur->childs = cur->last = NULL; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2102 | if (content != NULL) |
| 2103 | cur->content = xmlStrndup(content, len); |
| 2104 | else |
| 2105 | cur->content = NULL; |
| 2106 | break; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2107 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2110 | /** |
| 2111 | * xmlNodeAddContentLen: |
| 2112 | * @cur: the node being modified |
| 2113 | * @content: extra content |
| 2114 | * @len: the size of @content |
| 2115 | * |
| 2116 | * Append the extra substring to the node content. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2117 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2118 | void |
| 2119 | xmlNodeAddContentLen(xmlNodePtr cur, const CHAR *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2120 | if (cur == NULL) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2121 | fprintf(stderr, "xmlNodeAddContentLen : node == NULL\n"); |
| 2122 | return; |
| 2123 | } |
| 2124 | if (len <= 0) return; |
| 2125 | switch (cur->type) { |
| 2126 | case XML_DOCUMENT_FRAG_NODE: |
| 2127 | case XML_ELEMENT_NODE: { |
| 2128 | xmlNodePtr last = NULL, new; |
| 2129 | |
| 2130 | if (cur->childs != NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2131 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2132 | } else { |
| 2133 | if (cur->content != NULL) { |
| 2134 | cur->childs = xmlStringGetNodeList(cur->doc, cur->content); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2135 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2136 | xmlFree(cur->content); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2137 | cur->content = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2138 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2139 | } |
| 2140 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2141 | new = xmlNewTextLen(content, len); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2142 | if (new != NULL) { |
| 2143 | xmlAddChild(cur, new); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2144 | if ((last != NULL) && (last->next == new)) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2145 | xmlTextMerge(last, new); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2146 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2147 | } |
| 2148 | break; |
| 2149 | } |
| 2150 | case XML_ATTRIBUTE_NODE: |
| 2151 | break; |
| 2152 | case XML_TEXT_NODE: |
| 2153 | case XML_CDATA_SECTION_NODE: |
| 2154 | case XML_ENTITY_REF_NODE: |
| 2155 | case XML_ENTITY_NODE: |
| 2156 | case XML_PI_NODE: |
| 2157 | case XML_COMMENT_NODE: |
| 2158 | if (content != NULL) |
| 2159 | cur->content = xmlStrncat(cur->content, content, len); |
| 2160 | case XML_DOCUMENT_NODE: |
| 2161 | case XML_DOCUMENT_TYPE_NODE: |
| 2162 | break; |
| 2163 | case XML_NOTATION_NODE: |
| 2164 | if (content != NULL) |
| 2165 | cur->content = xmlStrncat(cur->content, content, len); |
| 2166 | break; |
| 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | /** |
| 2171 | * xmlNodeAddContent: |
| 2172 | * @cur: the node being modified |
| 2173 | * @content: extra content |
| 2174 | * |
| 2175 | * Append the extra substring to the node content. |
| 2176 | */ |
| 2177 | void |
| 2178 | xmlNodeAddContent(xmlNodePtr cur, const CHAR *content) { |
| 2179 | int len; |
| 2180 | |
| 2181 | if (cur == NULL) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2182 | fprintf(stderr, "xmlNodeAddContent : node == NULL\n"); |
| 2183 | return; |
| 2184 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2185 | if (content == NULL) return; |
| 2186 | len = xmlStrlen(content); |
| 2187 | xmlNodeAddContentLen(cur, content, len); |
| 2188 | } |
| 2189 | |
| 2190 | /** |
| 2191 | * xmlTextMerge: |
| 2192 | * @first: the first text node |
| 2193 | * @second: the second text node being merged |
| 2194 | * |
| 2195 | * Merge two text nodes into one |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2196 | * Returns the first text node augmented |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2197 | */ |
| 2198 | xmlNodePtr |
| 2199 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 2200 | if (first == NULL) return(second); |
| 2201 | if (second == NULL) return(first); |
| 2202 | if (first->type != XML_TEXT_NODE) return(first); |
| 2203 | if (second->type != XML_TEXT_NODE) return(first); |
| 2204 | xmlNodeAddContent(first, second->content); |
| 2205 | xmlUnlinkNode(second); |
| 2206 | xmlFreeNode(second); |
| 2207 | return(first); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2210 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2211 | * xmlGetNsList: |
| 2212 | * @doc: the document |
| 2213 | * @node: the current node |
| 2214 | * |
| 2215 | * Search all the namespace applying to a given element. |
| 2216 | * Returns an NULL terminated array of all the xmlNsPtr found |
| 2217 | * that need to be freed by the caller or NULL if no |
| 2218 | * namespace if defined |
| 2219 | */ |
| 2220 | xmlNsPtr * |
| 2221 | xmlGetNsList(xmlDocPtr doc, xmlNodePtr node) { |
| 2222 | xmlNsPtr cur; |
| 2223 | xmlNsPtr *ret = NULL; |
| 2224 | int nbns = 0; |
| 2225 | int maxns = 10; |
| 2226 | int i; |
| 2227 | |
| 2228 | while (node != NULL) { |
| 2229 | cur = node->nsDef; |
| 2230 | while (cur != NULL) { |
| 2231 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2232 | ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2233 | if (ret == NULL) { |
| 2234 | fprintf(stderr, "xmlGetNsList : out of memory!\n"); |
| 2235 | return(NULL); |
| 2236 | } |
| 2237 | ret[nbns] = NULL; |
| 2238 | } |
| 2239 | for (i = 0;i < nbns;i++) { |
| 2240 | if ((cur->prefix == ret[i]->prefix) || |
| 2241 | (!xmlStrcmp(cur->prefix, ret[i]->prefix))) break; |
| 2242 | } |
| 2243 | if (i >= nbns) { |
| 2244 | if (nbns >= maxns) { |
| 2245 | maxns *= 2; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2246 | ret = (xmlNsPtr *) xmlRealloc(ret, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2247 | (maxns + 1) * sizeof(xmlNsPtr)); |
| 2248 | if (ret == NULL) { |
| 2249 | fprintf(stderr, "xmlGetNsList : realloc failed!\n"); |
| 2250 | return(NULL); |
| 2251 | } |
| 2252 | } |
| 2253 | ret[nbns++] = cur; |
| 2254 | ret[nbns] = NULL; |
| 2255 | } |
| 2256 | |
| 2257 | cur = cur->next; |
| 2258 | } |
| 2259 | node = node->parent; |
| 2260 | } |
| 2261 | return(ret); |
| 2262 | } |
| 2263 | |
| 2264 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2265 | * xmlSearchNs: |
| 2266 | * @doc: the document |
| 2267 | * @node: the current node |
| 2268 | * @nameSpace: the namespace string |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2269 | * |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2270 | * Search a Ns registered under a given name space for a document. |
| 2271 | * recurse on the parents until it finds the defined namespace |
| 2272 | * or return NULL otherwise. |
| 2273 | * @nameSpace can be NULL, this is a search for the default namespace. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2274 | * Returns the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2275 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2276 | xmlNsPtr |
| 2277 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const CHAR *nameSpace) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2278 | xmlNsPtr cur; |
| 2279 | |
| 2280 | while (node != NULL) { |
| 2281 | cur = node->nsDef; |
| 2282 | while (cur != NULL) { |
| 2283 | if ((cur->prefix == NULL) && (nameSpace == NULL)) |
| 2284 | return(cur); |
| 2285 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 2286 | (!xmlStrcmp(cur->prefix, nameSpace))) |
| 2287 | return(cur); |
| 2288 | cur = cur->next; |
| 2289 | } |
| 2290 | node = node->parent; |
| 2291 | } |
| 2292 | if (doc != NULL) { |
| 2293 | cur = doc->oldNs; |
| 2294 | while (cur != NULL) { |
| 2295 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 2296 | (!xmlStrcmp(cur->prefix, nameSpace))) |
| 2297 | return(cur); |
| 2298 | cur = cur->next; |
| 2299 | } |
| 2300 | } |
| 2301 | return(NULL); |
| 2302 | } |
| 2303 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2304 | /** |
| 2305 | * xmlSearchNsByHref: |
| 2306 | * @doc: the document |
| 2307 | * @node: the current node |
| 2308 | * @href: the namespace value |
| 2309 | * |
| 2310 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 2311 | * the defined namespace or return NULL otherwise. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2312 | * Returns the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2313 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2314 | xmlNsPtr |
| 2315 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const CHAR *href) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2316 | xmlNsPtr cur; |
| 2317 | |
| 2318 | while (node != NULL) { |
| 2319 | cur = node->nsDef; |
| 2320 | while (cur != NULL) { |
| 2321 | if ((cur->href != NULL) && (href != NULL) && |
| 2322 | (!xmlStrcmp(cur->href, href))) |
| 2323 | return(cur); |
| 2324 | cur = cur->next; |
| 2325 | } |
| 2326 | node = node->parent; |
| 2327 | } |
| 2328 | if (doc != NULL) { |
| 2329 | cur = doc->oldNs; |
| 2330 | while (cur != NULL) { |
| 2331 | if ((cur->href != NULL) && (href != NULL) && |
| 2332 | (!xmlStrcmp(cur->href, href))) |
| 2333 | return(cur); |
| 2334 | cur = cur->next; |
| 2335 | } |
| 2336 | } |
| 2337 | return(NULL); |
| 2338 | } |
| 2339 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2340 | /** |
| 2341 | * xmlGetProp: |
| 2342 | * @node: the node |
| 2343 | * @name: the attribute name |
| 2344 | * |
| 2345 | * Search and get the value of an attribute associated to a node |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2346 | * This does the entity substitution. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2347 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2348 | */ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2349 | CHAR *xmlGetProp(xmlNodePtr node, const CHAR *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2350 | xmlAttrPtr prop = node->properties; |
| 2351 | |
| 2352 | while (prop != NULL) { |
Daniel Veillard | 6817893 | 1999-02-08 18:34:36 +0000 | [diff] [blame] | 2353 | if (!xmlStrcmp(prop->name, name)) { |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame] | 2354 | CHAR *ret; |
| 2355 | |
| 2356 | ret = xmlNodeListGetString(node->doc, prop->val, 1); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2357 | if (ret == NULL) return(xmlStrdup((CHAR *)"")); |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame] | 2358 | return(ret); |
Daniel Veillard | 6817893 | 1999-02-08 18:34:36 +0000 | [diff] [blame] | 2359 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2360 | prop = prop->next; |
| 2361 | } |
| 2362 | return(NULL); |
| 2363 | } |
| 2364 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2365 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2366 | * xmlSetProp: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2367 | * @node: the node |
| 2368 | * @name: the attribute name |
| 2369 | * @value: the attribute value |
| 2370 | * |
| 2371 | * Set (or reset) an attribute carried by a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2372 | * Returns the attribute pointer. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2373 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2374 | xmlAttrPtr |
| 2375 | xmlSetProp(xmlNodePtr node, const CHAR *name, const CHAR *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2376 | xmlAttrPtr prop = node->properties; |
| 2377 | |
| 2378 | while (prop != NULL) { |
| 2379 | if (!xmlStrcmp(prop->name, name)) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2380 | if (prop->val != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2381 | xmlFreeNodeList(prop->val); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2382 | prop->val = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2383 | if (value != NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2384 | prop->val = xmlStringGetNodeList(node->doc, value); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2385 | return(prop); |
| 2386 | } |
| 2387 | prop = prop->next; |
| 2388 | } |
| 2389 | prop = xmlNewProp(node, name, value); |
| 2390 | return(prop); |
| 2391 | } |
| 2392 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2393 | /** |
| 2394 | * xmlNodeIsText: |
| 2395 | * @node: the node |
| 2396 | * |
| 2397 | * Is this node a Text node ? |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2398 | * Returns 1 yes, 0 no |
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 | int |
| 2401 | xmlNodeIsText(xmlNodePtr node) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2402 | if (node == NULL) return(0); |
| 2403 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2404 | if (node->type == XML_TEXT_NODE) return(1); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2405 | return(0); |
| 2406 | } |
| 2407 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2408 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2409 | * xmlTextConcat: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2410 | * @node: the node |
| 2411 | * @content: the content |
| 2412 | * @len: @content lenght |
| 2413 | * |
| 2414 | * Concat the given string at the end of the existing node content |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2415 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2416 | |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2417 | void |
| 2418 | xmlTextConcat(xmlNodePtr node, const CHAR *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2419 | if (node == NULL) return; |
| 2420 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2421 | if (node->type != XML_TEXT_NODE) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2422 | fprintf(stderr, "xmlTextConcat: node is not text\n"); |
| 2423 | return; |
| 2424 | } |
| 2425 | node->content = xmlStrncat(node->content, content, len); |
| 2426 | } |
| 2427 | |
| 2428 | /************************************************************************ |
| 2429 | * * |
| 2430 | * Output : to a FILE or in memory * |
| 2431 | * * |
| 2432 | ************************************************************************/ |
| 2433 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2434 | #define BASE_BUFFER_SIZE 4000 |
| 2435 | |
| 2436 | /** |
| 2437 | * xmlBufferCreate: |
| 2438 | * |
| 2439 | * routine to create an XML buffer. |
| 2440 | * returns the new structure. |
| 2441 | */ |
| 2442 | xmlBufferPtr |
| 2443 | xmlBufferCreate(void) { |
| 2444 | xmlBufferPtr ret; |
| 2445 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2446 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2447 | if (ret == NULL) { |
| 2448 | fprintf(stderr, "xmlBufferCreate : out of memory!\n"); |
| 2449 | return(NULL); |
| 2450 | } |
| 2451 | ret->use = 0; |
| 2452 | ret->size = BASE_BUFFER_SIZE; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2453 | ret->content = (CHAR *) xmlMalloc(ret->size * sizeof(CHAR)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2454 | if (ret->content == NULL) { |
| 2455 | fprintf(stderr, "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2456 | xmlFree(ret); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2457 | return(NULL); |
| 2458 | } |
| 2459 | ret->content[0] = 0; |
| 2460 | return(ret); |
| 2461 | } |
| 2462 | |
| 2463 | /** |
| 2464 | * xmlBufferFree: |
| 2465 | * @buf: the buffer to free |
| 2466 | * |
| 2467 | * Frees an XML buffer. |
| 2468 | */ |
| 2469 | void |
| 2470 | xmlBufferFree(xmlBufferPtr buf) { |
| 2471 | if (buf == NULL) { |
| 2472 | fprintf(stderr, "xmlBufferFree: buf == NULL\n"); |
| 2473 | return; |
| 2474 | } |
| 2475 | if (buf->content == NULL) { |
| 2476 | fprintf(stderr, "xmlBufferFree: buf->content == NULL\n"); |
| 2477 | } else { |
| 2478 | memset(buf->content, -1, BASE_BUFFER_SIZE); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2479 | xmlFree(buf->content); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2480 | } |
| 2481 | memset(buf, -1, sizeof(xmlBuffer)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2482 | xmlFree(buf); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
| 2485 | /** |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2486 | * xmlBufferEmpty: |
| 2487 | * @buf: the buffer |
| 2488 | * |
| 2489 | * empty a buffer. |
| 2490 | */ |
| 2491 | void |
| 2492 | xmlBufferEmpty(xmlBufferPtr buf) { |
| 2493 | buf->use = 0; |
| 2494 | memset(buf->content, -1, buf->size);/* just for debug */ |
| 2495 | } |
| 2496 | |
| 2497 | /** |
| 2498 | * xmlBufferShrink: |
| 2499 | * @buf: the buffer to dump |
| 2500 | * @len: the number of CHAR to remove |
| 2501 | * |
| 2502 | * Remove the beginning of an XML buffer. |
| 2503 | * |
| 2504 | * Returns the number of CHAR removed, or -1 in case of failure. |
| 2505 | */ |
| 2506 | int |
| 2507 | xmlBufferShrink(xmlBufferPtr buf, int len) { |
| 2508 | if (len == 0) return(0); |
| 2509 | if (len > buf->use) return(-1); |
| 2510 | |
| 2511 | buf->use -= len; |
| 2512 | memmove(buf->content, &buf->content[len], buf->use * sizeof(CHAR)); |
| 2513 | |
| 2514 | buf->content[buf->use] = 0; |
| 2515 | return(len); |
| 2516 | } |
| 2517 | |
| 2518 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2519 | * xmlBufferDump: |
| 2520 | * @file: the file output |
| 2521 | * @buf: the buffer to dump |
| 2522 | * |
| 2523 | * Dumps an XML buffer to a FILE *. |
| 2524 | * Returns the number of CHAR written |
| 2525 | */ |
| 2526 | int |
| 2527 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 2528 | int ret; |
| 2529 | |
| 2530 | if (buf == NULL) { |
| 2531 | fprintf(stderr, "xmlBufferDump: buf == NULL\n"); |
| 2532 | return(0); |
| 2533 | } |
| 2534 | if (buf->content == NULL) { |
| 2535 | fprintf(stderr, "xmlBufferDump: buf->content == NULL\n"); |
| 2536 | return(0); |
| 2537 | } |
| 2538 | if (file == NULL) file = stdout; |
| 2539 | ret = fwrite(buf->content, sizeof(CHAR), buf->use, file); |
| 2540 | return(ret); |
| 2541 | } |
| 2542 | |
| 2543 | /** |
| 2544 | * xmlBufferAdd: |
| 2545 | * @buf: the buffer to dump |
| 2546 | * @str: the CHAR string |
| 2547 | * @len: the number of CHAR to add |
| 2548 | * |
| 2549 | * Add a string range to an XML buffer. |
| 2550 | */ |
| 2551 | void |
| 2552 | xmlBufferAdd(xmlBufferPtr buf, const CHAR *str, int len) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2553 | int l; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2554 | |
| 2555 | if (str == NULL) { |
| 2556 | fprintf(stderr, "xmlBufferAdd: str == NULL\n"); |
| 2557 | return; |
| 2558 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2559 | l = xmlStrlen(str); |
| 2560 | if (l < len) len = l; |
| 2561 | if (len <= 0) return; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2562 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2563 | if (buf->use + len + 10 >= buf->size) { |
| 2564 | CHAR *rebuf; |
| 2565 | |
| 2566 | buf->size *= 2; |
| 2567 | if (buf->use + len + 10 > buf->size) |
| 2568 | buf->size = buf->use + len + 10; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2569 | rebuf = (CHAR *) xmlRealloc(buf->content, buf->size * sizeof(CHAR)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2570 | if (rebuf == NULL) { |
| 2571 | fprintf(stderr, "xmlBufferAdd : out of memory!\n"); |
| 2572 | return; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2573 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2574 | buf->content = rebuf; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2575 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2576 | memmove(&buf->content[buf->use], str, len); |
| 2577 | buf->use += len; |
| 2578 | buf->content[buf->use] = 0; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | /** |
| 2582 | * xmlBufferCat: |
| 2583 | * @buf: the buffer to dump |
| 2584 | * @str: the CHAR string |
| 2585 | * |
| 2586 | * Append a zero terminated string to an XML buffer. |
| 2587 | */ |
| 2588 | void |
| 2589 | xmlBufferCat(xmlBufferPtr buf, const CHAR *str) { |
| 2590 | const CHAR *cur; |
| 2591 | |
| 2592 | if (str == NULL) { |
| 2593 | fprintf(stderr, "xmlBufferAdd: str == NULL\n"); |
| 2594 | return; |
| 2595 | } |
| 2596 | for (cur = str;*cur != 0;cur++) { |
| 2597 | if (buf->use + 10 >= buf->size) { |
| 2598 | CHAR *rebuf; |
| 2599 | |
| 2600 | buf->size *= 2; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2601 | rebuf = (CHAR *) xmlRealloc(buf->content, buf->size * sizeof(CHAR)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2602 | if (rebuf == NULL) { |
| 2603 | fprintf(stderr, "xmlBufferAdd : out of memory!\n"); |
| 2604 | return; |
| 2605 | } |
| 2606 | buf->content = rebuf; |
| 2607 | } |
| 2608 | buf->content[buf->use++] = *cur; |
| 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | /** |
| 2613 | * xmlBufferCCat: |
| 2614 | * @buf: the buffer to dump |
| 2615 | * @str: the C char string |
| 2616 | * |
| 2617 | * Append a zero terminated C string to an XML buffer. |
| 2618 | */ |
| 2619 | void |
| 2620 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 2621 | const char *cur; |
| 2622 | |
| 2623 | if (str == NULL) { |
| 2624 | fprintf(stderr, "xmlBufferAdd: str == NULL\n"); |
| 2625 | return; |
| 2626 | } |
| 2627 | for (cur = str;*cur != 0;cur++) { |
| 2628 | if (buf->use + 10 >= buf->size) { |
| 2629 | CHAR *rebuf; |
| 2630 | |
| 2631 | buf->size *= 2; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2632 | rebuf = (CHAR *) xmlRealloc(buf->content, buf->size * sizeof(CHAR)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2633 | if (rebuf == NULL) { |
| 2634 | fprintf(stderr, "xmlBufferAdd : out of memory!\n"); |
| 2635 | return; |
| 2636 | } |
| 2637 | buf->content = rebuf; |
| 2638 | } |
| 2639 | buf->content[buf->use++] = *cur; |
| 2640 | } |
| 2641 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2642 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2643 | /** |
| 2644 | * xmlBufferWriteCHAR: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2645 | * @buf: the XML buffer |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2646 | * @string: the string to add |
| 2647 | * |
| 2648 | * routine which manage and grows an output buffer. This one add |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2649 | * CHARs at the end of the buffer. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2650 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2651 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2652 | xmlBufferWriteCHAR(xmlBufferPtr buf, const CHAR *string) { |
| 2653 | xmlBufferCat(buf, string); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2656 | /** |
| 2657 | * xmlBufferWriteChar: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2658 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2659 | * @string: the string to add |
| 2660 | * |
| 2661 | * routine which manage and grows an output buffer. This one add |
| 2662 | * C chars at the end of the array. |
| 2663 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2664 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2665 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
| 2666 | xmlBufferCCat(buf, string); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2669 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2670 | /** |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2671 | * xmlBufferWriteQuotedString: |
| 2672 | * @buf: the XML buffer output |
| 2673 | * @string: the string to add |
| 2674 | * |
| 2675 | * routine which manage and grows an output buffer. This one writes |
| 2676 | * a quoted or double quoted CHAR string, checking first if it holds |
| 2677 | * quote or double-quotes internally |
| 2678 | */ |
| 2679 | void |
| 2680 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const CHAR *string) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2681 | if (xmlStrchr(string, '"')) { |
| 2682 | if (xmlStrchr(string, '\'')) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2683 | fprintf(stderr, |
| 2684 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 2685 | } |
| 2686 | xmlBufferCCat(buf, "'"); |
| 2687 | xmlBufferCat(buf, string); |
| 2688 | xmlBufferCCat(buf, "'"); |
| 2689 | } else { |
| 2690 | xmlBufferCCat(buf, "\""); |
| 2691 | xmlBufferCat(buf, string); |
| 2692 | xmlBufferCCat(buf, "\""); |
| 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | |
| 2697 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2698 | * xmlGlobalNsDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2699 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2700 | * @cur: a namespace |
| 2701 | * |
| 2702 | * Dump a global Namespace, this is the old version based on PIs. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2703 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2704 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2705 | xmlGlobalNsDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2706 | if (cur == NULL) { |
| 2707 | fprintf(stderr, "xmlGlobalNsDump : Ns == NULL\n"); |
| 2708 | return; |
| 2709 | } |
| 2710 | if (cur->type == XML_GLOBAL_NAMESPACE) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2711 | xmlBufferWriteChar(buf, "<?namespace"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2712 | if (cur->href != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2713 | xmlBufferWriteChar(buf, " href="); |
| 2714 | xmlBufferWriteQuotedString(buf, cur->href); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2715 | } |
| 2716 | if (cur->prefix != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2717 | xmlBufferWriteChar(buf, " AS="); |
| 2718 | xmlBufferWriteQuotedString(buf, cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2719 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2720 | xmlBufferWriteChar(buf, "?>\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2721 | } |
| 2722 | } |
| 2723 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2724 | /** |
| 2725 | * xmlGlobalNsListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2726 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2727 | * @cur: the first namespace |
| 2728 | * |
| 2729 | * 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] | 2730 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2731 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2732 | xmlGlobalNsListDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2733 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2734 | xmlGlobalNsDump(buf, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2735 | cur = cur->next; |
| 2736 | } |
| 2737 | } |
| 2738 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2739 | /** |
| 2740 | * xmlNsDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2741 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2742 | * @cur: a namespace |
| 2743 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2744 | * Dump a local Namespace definition. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2745 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2746 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2747 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2748 | xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2749 | if (cur == NULL) { |
| 2750 | fprintf(stderr, "xmlNsDump : Ns == NULL\n"); |
| 2751 | return; |
| 2752 | } |
| 2753 | if (cur->type == XML_LOCAL_NAMESPACE) { |
| 2754 | /* Within the context of an element attributes */ |
| 2755 | if (cur->prefix != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2756 | xmlBufferWriteChar(buf, " xmlns:"); |
| 2757 | xmlBufferWriteCHAR(buf, cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2758 | } else |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2759 | xmlBufferWriteChar(buf, " xmlns"); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2760 | xmlBufferWriteChar(buf, "="); |
| 2761 | xmlBufferWriteQuotedString(buf, cur->href); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2762 | } |
| 2763 | } |
| 2764 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2765 | /** |
| 2766 | * xmlNsListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2767 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2768 | * @cur: the first namespace |
| 2769 | * |
| 2770 | * Dump a list of local Namespace definitions. |
| 2771 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2772 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2773 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2774 | xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2775 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2776 | xmlNsDump(buf, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2777 | cur = cur->next; |
| 2778 | } |
| 2779 | } |
| 2780 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2781 | /** |
| 2782 | * xmlDtdDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2783 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2784 | * @doc: the document |
| 2785 | * |
| 2786 | * Dump the XML document DTD, if any. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2787 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2788 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2789 | xmlDtdDump(xmlBufferPtr buf, xmlDocPtr doc) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2790 | xmlDtdPtr cur = doc->intSubset; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2791 | |
| 2792 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2793 | fprintf(stderr, "xmlDtdDump : no internal subset\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2794 | return; |
| 2795 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2796 | xmlBufferWriteChar(buf, "<!DOCTYPE "); |
| 2797 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2798 | if (cur->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2799 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 2800 | xmlBufferWriteQuotedString(buf, cur->ExternalID); |
| 2801 | xmlBufferWriteChar(buf, " "); |
| 2802 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2803 | } else if (cur->SystemID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2804 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 2805 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2806 | } |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2807 | if ((cur->entities == NULL) && (cur->elements == NULL) && |
| 2808 | (cur->attributes == NULL) && (cur->notations == NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2809 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2810 | return; |
| 2811 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2812 | xmlBufferWriteChar(buf, " [\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2813 | if (cur->entities != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2814 | xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) cur->entities); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2815 | if (cur->notations != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2816 | xmlDumpNotationTable(buf, (xmlNotationTablePtr) cur->notations); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 2817 | if (cur->elements != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2818 | xmlDumpElementTable(buf, (xmlElementTablePtr) cur->elements); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2819 | if (cur->attributes != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2820 | xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) cur->attributes); |
| 2821 | xmlBufferWriteChar(buf, "]"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2822 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2823 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2826 | /** |
| 2827 | * xmlAttrDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2828 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2829 | * @doc: the document |
| 2830 | * @cur: the attribute pointer |
| 2831 | * |
| 2832 | * Dump an XML attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2833 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2834 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2835 | xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2836 | CHAR *value; |
| 2837 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2838 | if (cur == NULL) { |
| 2839 | fprintf(stderr, "xmlAttrDump : property == NULL\n"); |
| 2840 | return; |
| 2841 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2842 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2843 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 2844 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 2845 | xmlBufferWriteChar(buf, ":"); |
| 2846 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2847 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2848 | value = xmlNodeListGetString(doc, cur->val, 0); |
| 2849 | if (value) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2850 | xmlBufferWriteChar(buf, "="); |
| 2851 | xmlBufferWriteQuotedString(buf, value); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2852 | xmlFree(value); |
Daniel Veillard | 726c7e3 | 1999-02-08 15:13:10 +0000 | [diff] [blame] | 2853 | } else { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2854 | xmlBufferWriteChar(buf, "=\"\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2855 | } |
| 2856 | } |
| 2857 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2858 | /** |
| 2859 | * xmlAttrListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2860 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2861 | * @doc: the document |
| 2862 | * @cur: the first attribute pointer |
| 2863 | * |
| 2864 | * Dump a list of XML attributes |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2865 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2866 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2867 | xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2868 | if (cur == NULL) { |
| 2869 | fprintf(stderr, "xmlAttrListDump : property == NULL\n"); |
| 2870 | return; |
| 2871 | } |
| 2872 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2873 | xmlAttrDump(buf, doc, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2874 | cur = cur->next; |
| 2875 | } |
| 2876 | } |
| 2877 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2878 | |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2879 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2880 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level); |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2881 | /** |
| 2882 | * xmlNodeListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2883 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2884 | * @doc: the document |
| 2885 | * @cur: the first node |
| 2886 | * @level: the imbrication level for indenting |
| 2887 | * |
| 2888 | * Dump an XML node list, recursive behaviour,children are printed too. |
| 2889 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2890 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2891 | xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2892 | int needIndent = 0, i; |
| 2893 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2894 | if (cur == NULL) { |
| 2895 | fprintf(stderr, "xmlNodeListDump : node == NULL\n"); |
| 2896 | return; |
| 2897 | } |
| 2898 | while (cur != NULL) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2899 | if ((cur->type != XML_TEXT_NODE) && |
| 2900 | (cur->type != XML_ENTITY_REF_NODE)) { |
| 2901 | if (!needIndent) { |
| 2902 | needIndent = 1; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2903 | xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2904 | } |
| 2905 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2906 | xmlNodeDump(buf, doc, cur, level); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2907 | cur = cur->next; |
| 2908 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2909 | if ((xmlIndentTreeOutput) && (needIndent)) |
| 2910 | for (i = 1;i < level;i++) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2911 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2914 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2915 | * xmlNodeDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 2916 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2917 | * @doc: the document |
| 2918 | * @cur: the current node |
| 2919 | * @level: the imbrication level for indenting |
| 2920 | * |
| 2921 | * Dump an XML node, recursive behaviour,children are printed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2922 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2923 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2924 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2925 | int i; |
| 2926 | |
| 2927 | if (cur == NULL) { |
| 2928 | fprintf(stderr, "xmlNodeDump : node == NULL\n"); |
| 2929 | return; |
| 2930 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2931 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 2932 | if (cur->content != NULL) { |
| 2933 | CHAR *buffer; |
| 2934 | |
| 2935 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 2936 | if (buffer != NULL) { |
| 2937 | xmlBufferWriteCHAR(buf, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2938 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 2939 | } |
| 2940 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2941 | return; |
| 2942 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2943 | if (cur->type == XML_PI_NODE) { |
| 2944 | if (cur->content != NULL) { |
| 2945 | xmlBufferWriteChar(buf, "<?"); |
| 2946 | xmlBufferWriteCHAR(buf, cur->name); |
| 2947 | if (cur->content != NULL) { |
| 2948 | xmlBufferWriteChar(buf, " "); |
| 2949 | xmlBufferWriteCHAR(buf, cur->content); |
| 2950 | } |
| 2951 | xmlBufferWriteChar(buf, "?>\n"); |
| 2952 | } |
| 2953 | return; |
| 2954 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2955 | if (cur->type == XML_COMMENT_NODE) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2956 | if (cur->content != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2957 | xmlBufferWriteChar(buf, "<!--"); |
| 2958 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2959 | xmlBufferWriteChar(buf, "-->\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2960 | } |
| 2961 | return; |
| 2962 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2963 | if (cur->type == XML_ENTITY_REF_NODE) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2964 | xmlBufferWriteChar(buf, "&"); |
| 2965 | xmlBufferWriteCHAR(buf, cur->name); |
| 2966 | xmlBufferWriteChar(buf, ";"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2967 | return; |
| 2968 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2969 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 2970 | xmlBufferWriteChar(buf, "<![CDATA["); |
| 2971 | if (cur->content != NULL) |
| 2972 | xmlBufferWriteCHAR(buf, cur->content); |
| 2973 | xmlBufferWriteChar(buf, "]]>"); |
| 2974 | return; |
| 2975 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2976 | if (xmlIndentTreeOutput) |
| 2977 | for (i = 0;i < level;i++) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2978 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2979 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2980 | xmlBufferWriteChar(buf, "<"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2981 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2982 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 2983 | xmlBufferWriteChar(buf, ":"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2986 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2987 | if (cur->nsDef) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2988 | xmlNsListDump(buf, cur->nsDef); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2989 | if (cur->properties != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2990 | xmlAttrListDump(buf, doc, cur->properties); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2991 | |
| 2992 | if ((cur->content == NULL) && (cur->childs == NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2993 | xmlBufferWriteChar(buf, "/>\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2994 | return; |
| 2995 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2996 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 2997 | if (cur->content != NULL) { |
| 2998 | CHAR *buffer; |
| 2999 | |
| 3000 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 3001 | if (buffer != NULL) { |
| 3002 | xmlBufferWriteCHAR(buf, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3003 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 3004 | } |
| 3005 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3006 | if (cur->childs != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3007 | xmlNodeListDump(buf, doc, cur->childs, level + 1); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3008 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3009 | xmlBufferWriteChar(buf, "</"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3010 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3011 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 3012 | xmlBufferWriteChar(buf, ":"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3015 | xmlBufferWriteCHAR(buf, cur->name); |
| 3016 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3019 | /** |
| 3020 | * xmlDocContentDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3021 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3022 | * @cur: the document |
| 3023 | * |
| 3024 | * Dump an XML document. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3025 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3026 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3027 | xmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur) { |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 3028 | xmlBufferWriteChar(buf, "<?xml version="); |
| 3029 | if (cur->version != NULL) |
| 3030 | xmlBufferWriteQuotedString(buf, cur->version); |
| 3031 | else |
| 3032 | xmlBufferWriteChar(buf, "\"1.0\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3033 | if (cur->encoding != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3034 | xmlBufferWriteChar(buf, " encoding="); |
| 3035 | xmlBufferWriteQuotedString(buf, cur->encoding); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3036 | } |
| 3037 | switch (cur->standalone) { |
| 3038 | case 0: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3039 | xmlBufferWriteChar(buf, " standalone=\"no\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3040 | break; |
| 3041 | case 1: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3042 | xmlBufferWriteChar(buf, " standalone=\"yes\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3043 | break; |
| 3044 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3045 | xmlBufferWriteChar(buf, "?>\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3046 | if (cur->intSubset != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3047 | xmlDtdDump(buf, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3048 | if (cur->root != NULL) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3049 | xmlNodePtr child = cur->root; |
| 3050 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3051 | /* global namespace definitions, the old way */ |
| 3052 | if (oldXMLWDcompatibility) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3053 | xmlGlobalNsListDump(buf, cur->oldNs); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3054 | else |
| 3055 | xmlUpgradeOldNs(cur); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3056 | |
| 3057 | while (child != NULL) { |
| 3058 | xmlNodeDump(buf, cur, child, 0); |
| 3059 | child = child->next; |
| 3060 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3061 | } |
| 3062 | } |
| 3063 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3064 | /** |
| 3065 | * xmlDocDumpMemory: |
| 3066 | * @cur: the document |
| 3067 | * @mem: OUT: the memory pointer |
| 3068 | * @size: OUT: the memory lenght |
| 3069 | * |
| 3070 | * Dump an XML document in memory and return the CHAR * and it's size. |
| 3071 | * It's up to the caller to free the memory. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3072 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3073 | void |
| 3074 | xmlDocDumpMemory(xmlDocPtr cur, CHAR**mem, int *size) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3075 | xmlBufferPtr buf; |
| 3076 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3077 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3078 | #ifdef DEBUG_TREE |
| 3079 | fprintf(stderr, "xmlDocDumpMemory : document == NULL\n"); |
| 3080 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3081 | *mem = NULL; |
| 3082 | *size = 0; |
| 3083 | return; |
| 3084 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3085 | buf = xmlBufferCreate(); |
| 3086 | if (buf == NULL) { |
| 3087 | *mem = NULL; |
| 3088 | *size = 0; |
| 3089 | return; |
| 3090 | } |
| 3091 | xmlDocContentDump(buf, cur); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3092 | *mem = xmlStrndup(buf->content, buf->use); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3093 | *size = buf->use; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3094 | xmlBufferFree(buf); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3097 | /** |
| 3098 | * xmlGetDocCompressMode: |
| 3099 | * @doc: the document |
| 3100 | * |
| 3101 | * get the compression ratio for a document, ZLIB based |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3102 | * Returns 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3103 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3104 | int |
| 3105 | xmlGetDocCompressMode (xmlDocPtr doc) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 3106 | if (doc == NULL) return(-1); |
| 3107 | return(doc->compression); |
| 3108 | } |
| 3109 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3110 | /** |
| 3111 | * xmlSetDocCompressMode: |
| 3112 | * @doc: the document |
| 3113 | * @mode: the compression ratio |
| 3114 | * |
| 3115 | * set the compression ratio for a document, ZLIB based |
| 3116 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 3117 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3118 | void |
| 3119 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 3120 | if (doc == NULL) return; |
| 3121 | if (mode < 0) doc->compression = 0; |
| 3122 | else if (mode > 9) doc->compression = 9; |
| 3123 | else doc->compression = mode; |
| 3124 | } |
| 3125 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3126 | /** |
| 3127 | * xmlGetCompressMode: |
| 3128 | * |
| 3129 | * get the default compression mode used, ZLIB based. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3130 | * Returns 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 3131 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3132 | int |
| 3133 | xmlGetCompressMode(void) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3134 | return(xmlCompressMode); |
| 3135 | } |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3136 | |
| 3137 | /** |
| 3138 | * xmlSetCompressMode: |
| 3139 | * @mode: the compression ratio |
| 3140 | * |
| 3141 | * set the default compression mode used, ZLIB based |
| 3142 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 3143 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3144 | void |
| 3145 | xmlSetCompressMode(int mode) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3146 | if (mode < 0) xmlCompressMode = 0; |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 3147 | else if (mode > 9) xmlCompressMode = 9; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3148 | else xmlCompressMode = mode; |
| 3149 | } |
| 3150 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3151 | /** |
| 3152 | * xmlDocDump: |
| 3153 | * @f: the FILE* |
| 3154 | * @cur: the document |
| 3155 | * |
| 3156 | * Dump an XML document to an open FILE. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3157 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3158 | void |
| 3159 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3160 | xmlBufferPtr buf; |
| 3161 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3162 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3163 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3164 | fprintf(stderr, "xmlDocDump : document == NULL\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3165 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3166 | return; |
| 3167 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3168 | buf = xmlBufferCreate(); |
| 3169 | if (buf == NULL) return; |
| 3170 | xmlDocContentDump(buf, cur); |
| 3171 | xmlBufferDump(f, buf); |
| 3172 | xmlBufferFree(buf); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3175 | /** |
| 3176 | * xmlSaveFile: |
| 3177 | * @filename: the filename |
| 3178 | * @cur: the document |
| 3179 | * |
| 3180 | * Dump an XML document to a file. Will use compression if |
| 3181 | * compiled in and enabled. |
| 3182 | * returns: the number of file written or -1 in case of failure. |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3183 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3184 | int |
| 3185 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3186 | xmlBufferPtr buf; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3187 | #ifdef HAVE_ZLIB_H |
| 3188 | gzFile zoutput = NULL; |
| 3189 | char mode[15]; |
| 3190 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3191 | FILE *output = NULL; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3192 | int ret; |
| 3193 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3194 | /* |
| 3195 | * save the content to a temp buffer. |
| 3196 | */ |
| 3197 | buf = xmlBufferCreate(); |
| 3198 | if (buf == NULL) return(0); |
| 3199 | xmlDocContentDump(buf, cur); |
| 3200 | |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3201 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | dc3dd9d | 1998-09-24 19:25:54 +0000 | [diff] [blame] | 3202 | if ((cur->compression > 0) && (cur->compression <= 9)) { |
| 3203 | sprintf(mode, "w%d", cur->compression); |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3204 | zoutput = gzopen(filename, mode); |
| 3205 | } |
| 3206 | if (zoutput == NULL) { |
| 3207 | #endif |
| 3208 | output = fopen(filename, "w"); |
| 3209 | if (output == NULL) return(-1); |
| 3210 | #ifdef HAVE_ZLIB_H |
| 3211 | } |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3212 | |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3213 | if (zoutput != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3214 | ret = gzwrite(zoutput, buf->content, sizeof(CHAR) * buf->use); |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3215 | gzclose(zoutput); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3216 | } else { |
| 3217 | #endif |
| 3218 | ret = xmlBufferDump(output, buf); |
| 3219 | fclose(output); |
| 3220 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3221 | } |
| 3222 | #endif |
Manish Vachharajani | 5e60f5a | 1999-05-29 03:04:30 +0000 | [diff] [blame] | 3223 | xmlBufferFree(buf); |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 3224 | return(ret * sizeof(CHAR)); |
| 3225 | } |
| 3226 | |