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