Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * tree.c : implemetation of access function for an XML tree. |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 6 | * Daniel.Veillard@w3.org |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 9 | #ifdef WIN32 |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 10 | #include "win32config.h" |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 11 | #else |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 12 | #include "config.h" |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 13 | #endif |
| 14 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 16 | #include <string.h> /* for memset() only ! */ |
| 17 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 18 | #ifdef HAVE_CTYPE_H |
| 19 | #include <ctype.h> |
| 20 | #endif |
| 21 | #ifdef HAVE_STDLIB_H |
| 22 | #include <stdlib.h> |
| 23 | #endif |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 24 | #ifdef HAVE_ZLIB_H |
| 25 | #include <zlib.h> |
| 26 | #endif |
| 27 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 28 | #include "xmlmemory.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 29 | #include "tree.h" |
Daniel Veillard | 37846c6 | 1999-12-16 17:52:19 +0000 | [diff] [blame] | 30 | #include "parser.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 31 | #include "entities.h" |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 32 | #include "valid.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 33 | |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 34 | static xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 35 | int oldXMLWDcompatibility = 0; |
| 36 | int xmlIndentTreeOutput = 1; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 37 | xmlBufferAllocationScheme xmlBufferAllocScheme = XML_BUFFER_ALLOC_EXACT; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 38 | |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 39 | static int xmlCompressMode = 0; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 40 | static int xmlCheckDTD = 1; |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 41 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 42 | #define UPDATE_LAST_CHILD(n) if ((n) != NULL) { \ |
| 43 | xmlNodePtr ulccur = (n)->childs; \ |
| 44 | if (ulccur == NULL) { \ |
| 45 | (n)->last = NULL; \ |
| 46 | } else { \ |
| 47 | while (ulccur->next != NULL) ulccur = ulccur->next; \ |
| 48 | (n)->last = ulccur; \ |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 49 | }} |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 50 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 51 | /************************************************************************ |
| 52 | * * |
| 53 | * Allocation and deallocation of basic structures * |
| 54 | * * |
| 55 | ************************************************************************/ |
| 56 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 57 | /** |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 58 | * xmlSetBufferAllocationScheme: |
| 59 | * @scheme: allocation method to use |
| 60 | * |
| 61 | * Set the buffer allocation method. Types are |
| 62 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 63 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 64 | * improves performance |
| 65 | */ |
| 66 | void |
| 67 | xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { |
| 68 | xmlBufferAllocScheme = scheme; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * xmlGetBufferAllocationScheme: |
| 73 | * |
| 74 | * Types are |
| 75 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 76 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 77 | * improves performance |
| 78 | * |
| 79 | * Returns the current allocation scheme |
| 80 | */ |
| 81 | xmlBufferAllocationScheme |
| 82 | xmlGetBufferAllocationScheme() { |
| 83 | return xmlBufferAllocScheme; |
| 84 | } |
| 85 | |
| 86 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 87 | * xmlUpgradeOldNs: |
| 88 | * @doc: a document pointer |
| 89 | * |
| 90 | * 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] | 91 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 92 | void |
| 93 | xmlUpgradeOldNs(xmlDocPtr doc) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 94 | xmlNsPtr cur; |
| 95 | |
| 96 | if ((doc == NULL) || (doc->oldNs == NULL)) return; |
| 97 | if (doc->root == NULL) { |
| 98 | fprintf(stderr, "xmlUpgradeOldNs: failed no root !\n"); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | cur = doc->oldNs; |
| 103 | while (cur->next != NULL) { |
| 104 | cur->type = XML_LOCAL_NAMESPACE; |
| 105 | cur = cur->next; |
| 106 | } |
| 107 | cur->type = XML_LOCAL_NAMESPACE; |
| 108 | cur->next = doc->root->nsDef; |
| 109 | doc->root->nsDef = doc->oldNs; |
| 110 | doc->oldNs = NULL; |
| 111 | } |
| 112 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 113 | /** |
| 114 | * xmlNewNs: |
| 115 | * @node: the element carrying the namespace |
| 116 | * @href: the URI associated |
| 117 | * @prefix: the prefix for the namespace |
| 118 | * |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 119 | * Creation of a new Namespace. This function will refuse to create |
| 120 | * a namespace with a similar prefix than an existing one present on this |
| 121 | * node. |
| 122 | * Returns returns a new namespace pointer or NULL |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 123 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 124 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 125 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 126 | xmlNsPtr cur; |
| 127 | |
| 128 | if (href == NULL) { |
| 129 | fprintf(stderr, "xmlNewNs: href == NULL !\n"); |
| 130 | return(NULL); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Allocate a new DTD and fill the fields. |
| 135 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 136 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 137 | if (cur == NULL) { |
| 138 | fprintf(stderr, "xmlNewNs : malloc failed\n"); |
| 139 | return(NULL); |
| 140 | } |
| 141 | |
| 142 | cur->type = XML_LOCAL_NAMESPACE; |
| 143 | if (href != NULL) |
| 144 | cur->href = xmlStrdup(href); |
| 145 | else |
| 146 | cur->href = NULL; |
| 147 | if (prefix != NULL) |
| 148 | cur->prefix = xmlStrdup(prefix); |
| 149 | else |
| 150 | cur->prefix = NULL; |
| 151 | |
| 152 | /* |
| 153 | * Add it at the end to preserve parsing order ... |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 154 | * and checks for existing use of the prefix |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 155 | */ |
| 156 | cur->next = NULL; |
| 157 | if (node != NULL) { |
| 158 | if (node->nsDef == NULL) { |
| 159 | node->nsDef = cur; |
| 160 | } else { |
| 161 | xmlNsPtr prev = node->nsDef; |
| 162 | |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 163 | while (prev->next != NULL) { |
| 164 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 165 | (!xmlStrcmp(prev->prefix, cur->prefix))) { |
| 166 | xmlFreeNs(cur); |
| 167 | return(NULL); |
| 168 | } |
| 169 | prev = prev->next; |
| 170 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 171 | prev->next = cur; |
| 172 | } |
| 173 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 174 | return(cur); |
| 175 | } |
| 176 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 177 | /** |
| 178 | * xmlNewGlobalNs: |
| 179 | * @doc: the document carrying the namespace |
| 180 | * @href: the URI associated |
| 181 | * @prefix: the prefix for the namespace |
| 182 | * |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 183 | * Creation of a Namespace, the old way using PI and without scoping |
| 184 | * DEPRECATED !!! |
| 185 | * Will be removed at next major release ! |
| 186 | * Returns NULL this functionnality had been removed |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 187 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 188 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 189 | xmlNewGlobalNs(xmlDocPtr doc, const xmlChar *href, const xmlChar *prefix) { |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 190 | return(NULL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 193 | /** |
| 194 | * xmlSetNs: |
| 195 | * @node: a node in the document |
| 196 | * @ns: a namespace pointer |
| 197 | * |
| 198 | * Associate a namespace to a node, a posteriori. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 199 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 200 | void |
| 201 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 202 | if (node == NULL) { |
| 203 | fprintf(stderr, "xmlSetNs: node == NULL\n"); |
| 204 | return; |
| 205 | } |
| 206 | node->ns = ns; |
| 207 | } |
| 208 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 209 | /** |
| 210 | * xmlFreeNs: |
| 211 | * @cur: the namespace pointer |
| 212 | * |
| 213 | * Free up the structures associated to a namespace |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 214 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 215 | void |
| 216 | xmlFreeNs(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 217 | if (cur == NULL) { |
| 218 | fprintf(stderr, "xmlFreeNs : ns == NULL\n"); |
| 219 | return; |
| 220 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 221 | if (cur->href != NULL) xmlFree((char *) cur->href); |
| 222 | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 223 | memset(cur, -1, sizeof(xmlNs)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 224 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 227 | /** |
| 228 | * xmlFreeNsList: |
| 229 | * @cur: the first namespace pointer |
| 230 | * |
| 231 | * Free up all the structures associated to the chained namespaces. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 232 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 233 | void |
| 234 | xmlFreeNsList(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 235 | xmlNsPtr next; |
| 236 | if (cur == NULL) { |
| 237 | fprintf(stderr, "xmlFreeNsList : ns == NULL\n"); |
| 238 | return; |
| 239 | } |
| 240 | while (cur != NULL) { |
| 241 | next = cur->next; |
| 242 | xmlFreeNs(cur); |
| 243 | cur = next; |
| 244 | } |
| 245 | } |
| 246 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 247 | /** |
| 248 | * xmlNewDtd: |
| 249 | * @doc: the document pointer |
| 250 | * @name: the DTD name |
| 251 | * @ExternalID: the external ID |
| 252 | * @SystemID: the system ID |
| 253 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 254 | * Creation of a new DTD. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 255 | * Returns a pointer to the new DTD structure |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 256 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 257 | xmlDtdPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 258 | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
| 259 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 260 | xmlDtdPtr cur; |
| 261 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 262 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 263 | fprintf(stderr, "xmlNewDtd(%s): document %s already have a DTD %s\n", |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 264 | /* !!! */ (char *) name, doc->name, |
| 265 | /* !!! */ (char *)doc->extSubset->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Allocate a new DTD and fill the fields. |
| 270 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 271 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 272 | if (cur == NULL) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 273 | fprintf(stderr, "xmlNewDtd : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 274 | return(NULL); |
| 275 | } |
| 276 | |
| 277 | if (name != NULL) |
| 278 | cur->name = xmlStrdup(name); |
| 279 | else |
| 280 | cur->name = NULL; |
| 281 | if (ExternalID != NULL) |
| 282 | cur->ExternalID = xmlStrdup(ExternalID); |
| 283 | else |
| 284 | cur->ExternalID = NULL; |
| 285 | if (SystemID != NULL) |
| 286 | cur->SystemID = xmlStrdup(SystemID); |
| 287 | else |
| 288 | cur->SystemID = NULL; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 289 | cur->notations = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 290 | cur->elements = NULL; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 291 | cur->attributes = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 292 | cur->entities = NULL; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 293 | if (doc != NULL) |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 294 | doc->extSubset = cur; |
| 295 | |
| 296 | return(cur); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * xmlCreateIntSubset: |
| 301 | * @doc: the document pointer |
| 302 | * @name: the DTD name |
| 303 | * @ExternalID: the external ID |
| 304 | * @SystemID: the system ID |
| 305 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 306 | * Create the internal subset of a document |
| 307 | * Returns a pointer to the new DTD structure |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 308 | */ |
| 309 | xmlDtdPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 310 | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
| 311 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 312 | xmlDtdPtr cur; |
| 313 | |
| 314 | if ((doc != NULL) && (doc->intSubset != NULL)) { |
| 315 | fprintf(stderr, |
| 316 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 317 | doc->name); |
| 318 | return(NULL); |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Allocate a new DTD and fill the fields. |
| 323 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 324 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 325 | if (cur == NULL) { |
| 326 | fprintf(stderr, "xmlNewDtd : malloc failed\n"); |
| 327 | return(NULL); |
| 328 | } |
| 329 | |
| 330 | if (name != NULL) |
| 331 | cur->name = xmlStrdup(name); |
| 332 | else |
| 333 | cur->name = NULL; |
| 334 | if (ExternalID != NULL) |
| 335 | cur->ExternalID = xmlStrdup(ExternalID); |
| 336 | else |
| 337 | cur->ExternalID = NULL; |
| 338 | if (SystemID != NULL) |
| 339 | cur->SystemID = xmlStrdup(SystemID); |
| 340 | else |
| 341 | cur->SystemID = NULL; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 342 | cur->notations = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 343 | cur->elements = NULL; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 344 | cur->attributes = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 345 | cur->entities = NULL; |
| 346 | if (doc != NULL) |
| 347 | doc->intSubset = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 348 | |
| 349 | return(cur); |
| 350 | } |
| 351 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 352 | /** |
| 353 | * xmlFreeDtd: |
| 354 | * @cur: the DTD structure to free up |
| 355 | * |
| 356 | * Free a DTD structure. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 357 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 358 | void |
| 359 | xmlFreeDtd(xmlDtdPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 360 | if (cur == NULL) { |
| 361 | fprintf(stderr, "xmlFreeDtd : DTD == NULL\n"); |
| 362 | return; |
| 363 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 364 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 365 | if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID); |
| 366 | if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 367 | if (cur->notations != NULL) |
| 368 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 369 | if (cur->elements != NULL) |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 370 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 371 | if (cur->attributes != NULL) |
| 372 | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 373 | if (cur->entities != NULL) |
| 374 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
| 375 | memset(cur, -1, sizeof(xmlDtd)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 376 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 379 | /** |
| 380 | * xmlNewDoc: |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 381 | * @version: xmlChar string giving the version of XML "1.0" |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 382 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 383 | * Returns a new document |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 384 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 385 | xmlDocPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 386 | xmlNewDoc(const xmlChar *version) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 387 | xmlDocPtr cur; |
| 388 | |
| 389 | if (version == NULL) { |
| 390 | fprintf(stderr, "xmlNewDoc : version == NULL\n"); |
| 391 | return(NULL); |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Allocate a new document and fill the fields. |
| 396 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 397 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 398 | if (cur == NULL) { |
| 399 | fprintf(stderr, "xmlNewDoc : malloc failed\n"); |
| 400 | return(NULL); |
| 401 | } |
| 402 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 403 | cur->type = XML_DOCUMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 404 | cur->version = xmlStrdup(version); |
| 405 | cur->name = NULL; |
| 406 | cur->root = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 407 | cur->intSubset = NULL; |
| 408 | cur->extSubset = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 409 | cur->oldNs = NULL; |
| 410 | cur->encoding = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 411 | cur->standalone = -1; |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 412 | cur->compression = -1; /* not initialized */ |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 413 | cur->ids = NULL; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 414 | cur->refs = NULL; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 415 | #ifndef XML_WITHOUT_CORBA |
Daniel Veillard | 27fb075 | 1998-10-17 06:47:46 +0000 | [diff] [blame] | 416 | cur->_private = NULL; |
| 417 | cur->vepv = NULL; |
| 418 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 419 | return(cur); |
| 420 | } |
| 421 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 422 | /** |
| 423 | * xmlFreeDoc: |
| 424 | * @cur: pointer to the document |
| 425 | * @: |
| 426 | * |
| 427 | * Free up all the structures used by a document, tree included. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 428 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 429 | void |
| 430 | xmlFreeDoc(xmlDocPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 431 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 432 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 433 | fprintf(stderr, "xmlFreeDoc : document == NULL\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 434 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 435 | return; |
| 436 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 437 | if (cur->version != NULL) xmlFree((char *) cur->version); |
| 438 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 439 | if (cur->encoding != NULL) xmlFree((char *) cur->encoding); |
| 440 | if (cur->root != NULL) xmlFreeNodeList(cur->root); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 441 | if (cur->intSubset != NULL) xmlFreeDtd(cur->intSubset); |
| 442 | if (cur->extSubset != NULL) xmlFreeDtd(cur->extSubset); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 443 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 444 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 445 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 446 | memset(cur, -1, sizeof(xmlDoc)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 447 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 450 | /** |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 451 | * xmlStringLenGetNodeList: |
| 452 | * @doc: the document |
| 453 | * @value: the value of the text |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 454 | * @len: the length of the string value |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 455 | * |
| 456 | * Parse the value string and build the node list associated. Should |
| 457 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 458 | * Returns a pointer to the first child |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 459 | */ |
| 460 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 461 | xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 462 | xmlNodePtr ret = NULL, last = NULL; |
| 463 | xmlNodePtr node; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 464 | xmlChar *val; |
| 465 | const xmlChar *cur = value; |
| 466 | const xmlChar *q; |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 467 | xmlEntityPtr ent; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 468 | |
| 469 | if (value == NULL) return(NULL); |
| 470 | |
| 471 | q = cur; |
| 472 | while ((*cur != 0) && (cur - value < len)) { |
| 473 | if (*cur == '&') { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 474 | /* |
| 475 | * Save the current text. |
| 476 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 477 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 478 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 479 | xmlNodeAddContentLen(last, q, cur - q); |
| 480 | } else { |
| 481 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 482 | if (node == NULL) return(ret); |
| 483 | if (last == NULL) |
| 484 | last = ret = node; |
| 485 | else { |
| 486 | last->next = node; |
| 487 | node->prev = last; |
| 488 | last = node; |
| 489 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 492 | /* |
| 493 | * Read the entity string |
| 494 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 495 | cur++; |
| 496 | q = cur; |
| 497 | while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++; |
| 498 | if ((*cur == 0) || (cur - value >= len)) { |
| 499 | fprintf(stderr, |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 500 | "xmlStringLenGetNodeList: unterminated entity %30s\n", q); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 501 | return(ret); |
| 502 | } |
| 503 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 504 | /* |
| 505 | * Predefined entities don't generate nodes |
| 506 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 507 | val = xmlStrndup(q, cur - q); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 508 | ent = xmlGetDocEntity(doc, val); |
| 509 | if ((ent != NULL) && |
| 510 | (ent->type == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 511 | if (last == NULL) { |
| 512 | node = xmlNewDocText(doc, ent->content); |
| 513 | last = ret = node; |
| 514 | } else |
| 515 | xmlNodeAddContent(last, ent->content); |
| 516 | |
| 517 | } else { |
| 518 | /* |
| 519 | * Create a new REFERENCE_REF node |
| 520 | */ |
| 521 | node = xmlNewReference(doc, val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 522 | if (node == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 523 | if (val != NULL) xmlFree(val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 524 | return(ret); |
| 525 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 526 | if (last == NULL) |
| 527 | last = ret = node; |
| 528 | else { |
| 529 | last->next = node; |
| 530 | node->prev = last; |
| 531 | last = node; |
| 532 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 533 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 534 | xmlFree(val); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 535 | } |
| 536 | cur++; |
| 537 | q = cur; |
| 538 | } else |
| 539 | cur++; |
| 540 | } |
| 541 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 542 | /* |
| 543 | * Handle the last piece of text. |
| 544 | */ |
| 545 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 546 | xmlNodeAddContentLen(last, q, cur - q); |
| 547 | } else { |
| 548 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 549 | if (node == NULL) return(ret); |
| 550 | if (last == NULL) |
| 551 | last = ret = node; |
| 552 | else { |
| 553 | last->next = node; |
| 554 | node->prev = last; |
| 555 | last = node; |
| 556 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | return(ret); |
| 560 | } |
| 561 | |
| 562 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 563 | * xmlStringGetNodeList: |
| 564 | * @doc: the document |
| 565 | * @value: the value of the attribute |
| 566 | * |
| 567 | * Parse the value string and build the node list associated. Should |
| 568 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 569 | * Returns a pointer to the first child |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 570 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 571 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 572 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 573 | xmlNodePtr ret = NULL, last = NULL; |
| 574 | xmlNodePtr node; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 575 | xmlChar *val; |
| 576 | const xmlChar *cur = value; |
| 577 | const xmlChar *q; |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 578 | xmlEntityPtr ent; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 579 | |
| 580 | if (value == NULL) return(NULL); |
| 581 | |
| 582 | q = cur; |
| 583 | while (*cur != 0) { |
| 584 | if (*cur == '&') { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 585 | /* |
| 586 | * Save the current text. |
| 587 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 588 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 589 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 590 | xmlNodeAddContentLen(last, q, cur - q); |
| 591 | } else { |
| 592 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 593 | if (node == NULL) return(ret); |
| 594 | if (last == NULL) |
| 595 | last = ret = node; |
| 596 | else { |
| 597 | last->next = node; |
| 598 | node->prev = last; |
| 599 | last = node; |
| 600 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 603 | /* |
| 604 | * Read the entity string |
| 605 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 606 | cur++; |
| 607 | q = cur; |
| 608 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 609 | if (*cur == 0) { |
| 610 | fprintf(stderr, |
| 611 | "xmlStringGetNodeList: unterminated entity %30s\n", q); |
| 612 | return(ret); |
| 613 | } |
| 614 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 615 | /* |
| 616 | * Predefined entities don't generate nodes |
| 617 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 618 | val = xmlStrndup(q, cur - q); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 619 | ent = xmlGetDocEntity(doc, val); |
| 620 | if ((ent != NULL) && |
| 621 | (ent->type == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 622 | if (last == NULL) { |
| 623 | node = xmlNewDocText(doc, ent->content); |
| 624 | last = ret = node; |
| 625 | } else |
| 626 | xmlNodeAddContent(last, ent->content); |
| 627 | |
| 628 | } else { |
| 629 | /* |
| 630 | * Create a new REFERENCE_REF node |
| 631 | */ |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 632 | node = xmlNewReference(doc, val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 633 | if (node == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 634 | if (val != NULL) xmlFree(val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 635 | return(ret); |
| 636 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 637 | if (last == NULL) |
| 638 | last = ret = node; |
| 639 | else { |
| 640 | last->next = node; |
| 641 | node->prev = last; |
| 642 | last = node; |
| 643 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 644 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 645 | xmlFree(val); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 646 | } |
| 647 | cur++; |
| 648 | q = cur; |
| 649 | } else |
| 650 | cur++; |
| 651 | } |
| 652 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 653 | /* |
| 654 | * Handle the last piece of text. |
| 655 | */ |
| 656 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 657 | xmlNodeAddContentLen(last, q, cur - q); |
| 658 | } else { |
| 659 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 660 | if (node == NULL) return(ret); |
| 661 | if (last == NULL) |
| 662 | last = ret = node; |
| 663 | else { |
| 664 | last->next = node; |
| 665 | node->prev = last; |
| 666 | last = node; |
| 667 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | return(ret); |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * xmlNodeListGetString: |
| 675 | * @doc: the document |
| 676 | * @list: a Node list |
| 677 | * @inLine: should we replace entity contents or show their external form |
| 678 | * |
| 679 | * Returns the string equivalent to the text contained in the Node list |
| 680 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 681 | * Returns a pointer to the string copy, the calller must free it. |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 682 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 683 | xmlChar * |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 684 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 685 | xmlNodePtr node = list; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 686 | xmlChar *ret = NULL; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 687 | xmlEntityPtr ent; |
| 688 | |
| 689 | if (list == NULL) return(NULL); |
| 690 | |
| 691 | while (node != NULL) { |
| 692 | if (node->type == XML_TEXT_NODE) { |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 693 | if (inLine) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 694 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 695 | ret = xmlStrcat(ret, node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 696 | #else |
| 697 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 698 | #endif |
| 699 | } else { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 700 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 701 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 702 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 703 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 704 | #else |
| 705 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 706 | xmlBufferContent(node->content)); |
| 707 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 708 | if (buffer != NULL) { |
| 709 | ret = xmlStrcat(ret, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 710 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 713 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 714 | if (inLine) { |
| 715 | ent = xmlGetDocEntity(doc, node->name); |
| 716 | if (ent != NULL) |
| 717 | ret = xmlStrcat(ret, ent->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 718 | else { |
| 719 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 720 | ret = xmlStrcat(ret, node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 721 | #else |
| 722 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 723 | #endif |
| 724 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 725 | } else { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 726 | xmlChar buf[2]; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 727 | buf[0] = '&'; buf[1] = 0; |
| 728 | ret = xmlStrncat(ret, buf, 1); |
| 729 | ret = xmlStrcat(ret, node->name); |
| 730 | buf[0] = ';'; buf[1] = 0; |
| 731 | ret = xmlStrncat(ret, buf, 1); |
| 732 | } |
| 733 | } |
| 734 | #if 0 |
| 735 | else { |
| 736 | fprintf(stderr, "xmlGetNodeListString : invalide node type %d\n", |
| 737 | node->type); |
| 738 | } |
| 739 | #endif |
| 740 | node = node->next; |
| 741 | } |
| 742 | return(ret); |
| 743 | } |
| 744 | |
| 745 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 746 | * xmlNewProp: |
| 747 | * @node: the holding node |
| 748 | * @name: the name of the attribute |
| 749 | * @value: the value of the attribute |
| 750 | * |
| 751 | * Create a new property carried by a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 752 | * Returns a pointer to the attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 753 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 754 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 755 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 756 | xmlAttrPtr cur; |
| 757 | |
| 758 | if (name == NULL) { |
| 759 | fprintf(stderr, "xmlNewProp : name == NULL\n"); |
| 760 | return(NULL); |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * Allocate a new property and fill the fields. |
| 765 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 766 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 767 | if (cur == NULL) { |
| 768 | fprintf(stderr, "xmlNewProp : malloc failed\n"); |
| 769 | return(NULL); |
| 770 | } |
| 771 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 772 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 773 | cur->node = node; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 774 | cur->ns = NULL; |
| 775 | cur->name = xmlStrdup(name); |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 776 | if (value != NULL) { |
| 777 | xmlChar *buffer; |
| 778 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 779 | cur->val = xmlStringGetNodeList(node->doc, buffer); |
| 780 | xmlFree(buffer); |
| 781 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 782 | else |
| 783 | cur->val = NULL; |
| 784 | #ifndef XML_WITHOUT_CORBA |
| 785 | cur->_private = NULL; |
| 786 | cur->vepv = NULL; |
| 787 | #endif |
| 788 | |
| 789 | /* |
| 790 | * Add it at the end to preserve parsing order ... |
| 791 | */ |
| 792 | cur->next = NULL; |
| 793 | if (node != NULL) { |
| 794 | if (node->properties == NULL) { |
| 795 | node->properties = cur; |
| 796 | } else { |
| 797 | xmlAttrPtr prev = node->properties; |
| 798 | |
| 799 | while (prev->next != NULL) prev = prev->next; |
| 800 | prev->next = cur; |
| 801 | } |
| 802 | } |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 803 | #ifndef XML_WITHOUT_CORBA |
| 804 | cur->_private = NULL; |
| 805 | cur->vepv = NULL; |
| 806 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 807 | return(cur); |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * xmlNewNsProp: |
| 812 | * @node: the holding node |
| 813 | * @ns: the namespace |
| 814 | * @name: the name of the attribute |
| 815 | * @value: the value of the attribute |
| 816 | * |
| 817 | * Create a new property tagged with a namespace and carried by a node. |
| 818 | * Returns a pointer to the attribute |
| 819 | */ |
| 820 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 821 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 822 | const xmlChar *value) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 823 | xmlAttrPtr cur; |
| 824 | |
| 825 | if (name == NULL) { |
| 826 | fprintf(stderr, "xmlNewProp : name == NULL\n"); |
| 827 | return(NULL); |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * Allocate a new property and fill the fields. |
| 832 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 833 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 834 | if (cur == NULL) { |
| 835 | fprintf(stderr, "xmlNewProp : malloc failed\n"); |
| 836 | return(NULL); |
| 837 | } |
| 838 | |
| 839 | cur->type = XML_ATTRIBUTE_NODE; |
| 840 | cur->node = node; |
| 841 | cur->ns = ns; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 842 | cur->name = xmlStrdup(name); |
| 843 | if (value != NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 844 | cur->val = xmlStringGetNodeList(node->doc, value); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 845 | else |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 846 | cur->val = NULL; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 847 | #ifndef XML_WITHOUT_CORBA |
Daniel Veillard | 27fb075 | 1998-10-17 06:47:46 +0000 | [diff] [blame] | 848 | cur->_private = NULL; |
| 849 | cur->vepv = NULL; |
| 850 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 851 | |
| 852 | /* |
| 853 | * Add it at the end to preserve parsing order ... |
| 854 | */ |
| 855 | cur->next = NULL; |
| 856 | if (node != NULL) { |
| 857 | if (node->properties == NULL) { |
| 858 | node->properties = cur; |
| 859 | } else { |
| 860 | xmlAttrPtr prev = node->properties; |
| 861 | |
| 862 | while (prev->next != NULL) prev = prev->next; |
| 863 | prev->next = cur; |
| 864 | } |
| 865 | } |
| 866 | return(cur); |
| 867 | } |
| 868 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 869 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 870 | * xmlNewDocProp: |
| 871 | * @doc: the document |
| 872 | * @name: the name of the attribute |
| 873 | * @value: the value of the attribute |
| 874 | * |
| 875 | * Create a new property carried by a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 876 | * Returns a pointer to the attribute |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 877 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 878 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 879 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 880 | xmlAttrPtr cur; |
| 881 | |
| 882 | if (name == NULL) { |
| 883 | fprintf(stderr, "xmlNewProp : name == NULL\n"); |
| 884 | return(NULL); |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | * Allocate a new property and fill the fields. |
| 889 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 890 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 891 | if (cur == NULL) { |
| 892 | fprintf(stderr, "xmlNewProp : malloc failed\n"); |
| 893 | return(NULL); |
| 894 | } |
| 895 | |
| 896 | cur->type = XML_ATTRIBUTE_NODE; |
| 897 | cur->node = NULL; |
| 898 | cur->name = xmlStrdup(name); |
| 899 | if (value != NULL) |
| 900 | cur->val = xmlStringGetNodeList(doc, value); |
| 901 | else |
| 902 | cur->val = NULL; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 903 | #ifndef XML_WITHOUT_CORBA |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 904 | cur->_private = NULL; |
| 905 | cur->vepv = NULL; |
| 906 | #endif |
| 907 | |
| 908 | cur->next = NULL; |
| 909 | return(cur); |
| 910 | } |
| 911 | |
| 912 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 913 | * xmlFreePropList: |
| 914 | * @cur: the first property in the list |
| 915 | * |
| 916 | * 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] | 917 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 918 | void |
| 919 | xmlFreePropList(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 920 | xmlAttrPtr next; |
| 921 | if (cur == NULL) { |
| 922 | fprintf(stderr, "xmlFreePropList : property == NULL\n"); |
| 923 | return; |
| 924 | } |
| 925 | while (cur != NULL) { |
| 926 | next = cur->next; |
| 927 | xmlFreeProp(cur); |
| 928 | cur = next; |
| 929 | } |
| 930 | } |
| 931 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 932 | /** |
| 933 | * xmlFreeProp: |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 934 | * @cur: an attribute |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 935 | * |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 936 | * Free one attribute, all the content is freed too |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 937 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 938 | void |
| 939 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 940 | if (cur == NULL) { |
| 941 | fprintf(stderr, "xmlFreeProp : property == NULL\n"); |
| 942 | return; |
| 943 | } |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 944 | /* Check for ID removal -> leading to invalid references ! */ |
| 945 | if ((cur->node != NULL) && |
| 946 | (xmlIsID(cur->node->doc, cur->node, cur))) |
| 947 | xmlRemoveID(cur->node->doc, cur); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 948 | if (cur->name != NULL) xmlFree((char *) cur->name); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 949 | if (cur->val != NULL) xmlFreeNodeList(cur->val); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 950 | memset(cur, -1, sizeof(xmlAttr)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 951 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 954 | /** |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 955 | * xmlRemoveProp: |
| 956 | * @cur: an attribute |
| 957 | * |
| 958 | * Unlink and free one attribute, all the content is freed too |
| 959 | * Note this doesn't work for namespace definition attributes |
| 960 | * |
| 961 | * Returns 0 if success and -1 in case of error. |
| 962 | */ |
| 963 | int |
| 964 | xmlRemoveProp(xmlAttrPtr cur) { |
| 965 | xmlAttrPtr tmp; |
| 966 | if (cur == NULL) { |
| 967 | fprintf(stderr, "xmlRemoveProp : cur == NULL\n"); |
| 968 | return(-1); |
| 969 | } |
| 970 | if (cur->node == NULL) { |
| 971 | fprintf(stderr, "xmlRemoveProp : cur->node == NULL\n"); |
| 972 | return(-1); |
| 973 | } |
| 974 | tmp = cur->node->properties; |
| 975 | if (tmp == cur) { |
| 976 | cur->node->properties = cur->next; |
| 977 | xmlFreeProp(cur); |
| 978 | return(0); |
| 979 | } |
| 980 | while (tmp != NULL) { |
| 981 | if (tmp->next == cur) { |
| 982 | tmp->next = cur->next; |
| 983 | xmlFreeProp(cur); |
| 984 | return(0); |
| 985 | } |
| 986 | tmp = tmp->next; |
| 987 | } |
| 988 | fprintf(stderr, "xmlRemoveProp : attribute not owned by its node\n"); |
| 989 | return(-1); |
| 990 | } |
| 991 | |
| 992 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 993 | * xmlNewPI: |
| 994 | * @name: the processing instruction name |
| 995 | * @content: the PI content |
| 996 | * |
| 997 | * Creation of a processing instruction element. |
| 998 | * Returns a pointer to the new node object. |
| 999 | */ |
| 1000 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1001 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1002 | xmlNodePtr cur; |
| 1003 | |
| 1004 | if (name == NULL) { |
| 1005 | fprintf(stderr, "xmlNewPI : name == NULL\n"); |
| 1006 | return(NULL); |
| 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * Allocate a new node and fill the fields. |
| 1011 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1012 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1013 | if (cur == NULL) { |
| 1014 | fprintf(stderr, "xmlNewPI : malloc failed\n"); |
| 1015 | return(NULL); |
| 1016 | } |
| 1017 | |
| 1018 | cur->type = XML_PI_NODE; |
| 1019 | cur->doc = NULL; |
| 1020 | cur->parent = NULL; |
| 1021 | cur->next = NULL; |
| 1022 | cur->prev = NULL; |
| 1023 | cur->childs = NULL; |
| 1024 | cur->last = NULL; |
| 1025 | cur->properties = NULL; |
| 1026 | cur->name = xmlStrdup(name); |
| 1027 | cur->ns = NULL; |
| 1028 | cur->nsDef = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1029 | if (content != NULL) { |
| 1030 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1031 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1032 | #else |
| 1033 | cur->content = xmlBufferCreateSize(0); |
| 1034 | xmlBufferSetAllocationScheme(cur->content, |
| 1035 | xmlGetBufferAllocationScheme()); |
| 1036 | xmlBufferAdd(cur->content, content, -1); |
| 1037 | #endif |
| 1038 | } else |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1039 | cur->content = NULL; |
| 1040 | #ifndef XML_WITHOUT_CORBA |
| 1041 | cur->_private = NULL; |
| 1042 | cur->vepv = NULL; |
| 1043 | #endif |
| 1044 | return(cur); |
| 1045 | } |
| 1046 | |
| 1047 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1048 | * xmlNewNode: |
| 1049 | * @ns: namespace if any |
| 1050 | * @name: the node name |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1051 | * |
| 1052 | * Creation of a new node element. @ns and @content are optionnal (NULL). |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1053 | * If content is non NULL, a child list containing the TEXTs and |
| 1054 | * ENTITY_REFs node will be created. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1055 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1056 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1057 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1058 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1059 | xmlNodePtr cur; |
| 1060 | |
| 1061 | if (name == NULL) { |
| 1062 | fprintf(stderr, "xmlNewNode : name == NULL\n"); |
| 1063 | return(NULL); |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * Allocate a new node and fill the fields. |
| 1068 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1069 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1070 | if (cur == NULL) { |
| 1071 | fprintf(stderr, "xmlNewNode : malloc failed\n"); |
| 1072 | return(NULL); |
| 1073 | } |
| 1074 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1075 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1076 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1077 | cur->parent = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1078 | cur->next = NULL; |
| 1079 | cur->prev = NULL; |
| 1080 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1081 | cur->last = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1082 | cur->properties = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1083 | cur->name = xmlStrdup(name); |
| 1084 | cur->ns = ns; |
| 1085 | cur->nsDef = NULL; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1086 | cur->content = NULL; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 1087 | #ifndef XML_WITHOUT_CORBA |
Daniel Veillard | 27fb075 | 1998-10-17 06:47:46 +0000 | [diff] [blame] | 1088 | cur->_private = NULL; |
| 1089 | cur->vepv = NULL; |
| 1090 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1091 | return(cur); |
| 1092 | } |
| 1093 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1094 | /** |
| 1095 | * xmlNewDocNode: |
| 1096 | * @doc: the document |
| 1097 | * @ns: namespace if any |
| 1098 | * @name: the node name |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1099 | * @content: the XML text content if any |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1100 | * |
| 1101 | * Creation of a new node element within a document. @ns and @content |
| 1102 | * are optionnal (NULL). |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1103 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 1104 | * references, but XML special chars need to be escaped first by using |
| 1105 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 1106 | * need entities support. |
| 1107 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1108 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1109 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1110 | xmlNodePtr |
| 1111 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1112 | const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1113 | xmlNodePtr cur; |
| 1114 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1115 | cur = xmlNewNode(ns, name); |
| 1116 | if (cur != NULL) { |
| 1117 | cur->doc = doc; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1118 | if (content != NULL) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1119 | cur->childs = xmlStringGetNodeList(doc, content); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1120 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1121 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1122 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1123 | return(cur); |
| 1124 | } |
| 1125 | |
| 1126 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1127 | /** |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1128 | * xmlNewDocRawNode: |
| 1129 | * @doc: the document |
| 1130 | * @ns: namespace if any |
| 1131 | * @name: the node name |
| 1132 | * @content: the text content if any |
| 1133 | * |
| 1134 | * Creation of a new node element within a document. @ns and @content |
| 1135 | * are optionnal (NULL). |
| 1136 | * |
| 1137 | * Returns a pointer to the new node object. |
| 1138 | */ |
| 1139 | xmlNodePtr |
| 1140 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 1141 | const xmlChar *name, const xmlChar *content) { |
| 1142 | xmlNodePtr cur; |
| 1143 | |
| 1144 | cur = xmlNewNode(ns, name); |
| 1145 | if (cur != NULL) { |
| 1146 | cur->doc = doc; |
| 1147 | if (content != NULL) { |
| 1148 | cur->childs = xmlNewDocText(doc, content); |
| 1149 | UPDATE_LAST_CHILD(cur) |
| 1150 | } |
| 1151 | } |
| 1152 | return(cur); |
| 1153 | } |
| 1154 | |
| 1155 | |
| 1156 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1157 | * xmlNewText: |
| 1158 | * @content: the text content |
| 1159 | * |
| 1160 | * Creation of a new text node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1161 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1162 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1163 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1164 | xmlNewText(const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1165 | xmlNodePtr cur; |
| 1166 | |
| 1167 | /* |
| 1168 | * Allocate a new node and fill the fields. |
| 1169 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1170 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1171 | if (cur == NULL) { |
| 1172 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 1173 | return(NULL); |
| 1174 | } |
| 1175 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1176 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1177 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1178 | cur->parent = NULL; |
| 1179 | cur->next = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1180 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1181 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1182 | cur->last = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1183 | cur->properties = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1184 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1185 | cur->name = xmlStrdup(xmlStringText); |
| 1186 | cur->ns = NULL; |
| 1187 | cur->nsDef = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1188 | if (content != NULL) { |
| 1189 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1190 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1191 | #else |
| 1192 | cur->content = xmlBufferCreateSize(0); |
| 1193 | xmlBufferSetAllocationScheme(cur->content, |
| 1194 | xmlGetBufferAllocationScheme()); |
| 1195 | xmlBufferAdd(cur->content, content, -1); |
| 1196 | #endif |
| 1197 | } else |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1198 | cur->content = NULL; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 1199 | #ifndef XML_WITHOUT_CORBA |
| 1200 | cur->_private = NULL; |
| 1201 | cur->vepv = NULL; |
| 1202 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1203 | return(cur); |
| 1204 | } |
| 1205 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1206 | /** |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1207 | * xmlNewTextChild: |
| 1208 | * @parent: the parent node |
| 1209 | * @ns: a namespace if any |
| 1210 | * @name: the name of the child |
| 1211 | * @content: the text content of the child if any. |
| 1212 | * |
| 1213 | * Creation of a new child element, added at the end of @parent childs list. |
| 1214 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1215 | * a child TEXT node will be created containing the string content. |
| 1216 | * |
| 1217 | * Returns a pointer to the new node object. |
| 1218 | */ |
| 1219 | xmlNodePtr |
| 1220 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 1221 | const xmlChar *name, const xmlChar *content) { |
| 1222 | xmlNodePtr cur, prev; |
| 1223 | |
| 1224 | if (parent == NULL) { |
| 1225 | fprintf(stderr, "xmlNewTextChild : parent == NULL\n"); |
| 1226 | return(NULL); |
| 1227 | } |
| 1228 | |
| 1229 | if (name == NULL) { |
| 1230 | fprintf(stderr, "xmlNewTextChild : name == NULL\n"); |
| 1231 | return(NULL); |
| 1232 | } |
| 1233 | |
| 1234 | /* |
| 1235 | * Allocate a new node |
| 1236 | */ |
| 1237 | if (ns == NULL) |
| 1238 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 1239 | else |
| 1240 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 1241 | if (cur == NULL) return(NULL); |
| 1242 | |
| 1243 | /* |
| 1244 | * add the new element at the end of the childs list. |
| 1245 | */ |
| 1246 | cur->type = XML_ELEMENT_NODE; |
| 1247 | cur->parent = parent; |
| 1248 | cur->doc = parent->doc; |
| 1249 | if (parent->childs == NULL) { |
| 1250 | parent->childs = cur; |
| 1251 | parent->last = cur; |
| 1252 | } else { |
| 1253 | prev = parent->last; |
| 1254 | prev->next = cur; |
| 1255 | cur->prev = prev; |
| 1256 | parent->last = cur; |
| 1257 | } |
| 1258 | |
| 1259 | return(cur); |
| 1260 | } |
| 1261 | |
| 1262 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1263 | * xmlNewReference: |
| 1264 | * @doc: the document |
| 1265 | * @name: the reference name, or the reference string with & and ; |
| 1266 | * |
| 1267 | * Creation of a new reference node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1268 | * Returns a pointer to the new node object. |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1269 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1270 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1271 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1272 | xmlNodePtr cur; |
| 1273 | xmlEntityPtr ent; |
| 1274 | |
| 1275 | /* |
| 1276 | * Allocate a new node and fill the fields. |
| 1277 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1278 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1279 | if (cur == NULL) { |
| 1280 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 1281 | return(NULL); |
| 1282 | } |
| 1283 | |
| 1284 | cur->type = XML_ENTITY_REF_NODE; |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 1285 | cur->doc = doc; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1286 | cur->parent = NULL; |
| 1287 | cur->next = NULL; |
| 1288 | cur->prev = NULL; |
| 1289 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1290 | cur->last = NULL; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1291 | cur->properties = NULL; |
| 1292 | if (name[0] == '&') { |
| 1293 | int len; |
| 1294 | name++; |
| 1295 | len = xmlStrlen(name); |
| 1296 | if (name[len - 1] == ';') |
| 1297 | cur->name = xmlStrndup(name, len - 1); |
| 1298 | else |
| 1299 | cur->name = xmlStrndup(name, len); |
| 1300 | } else |
| 1301 | cur->name = xmlStrdup(name); |
| 1302 | cur->ns = NULL; |
| 1303 | cur->nsDef = NULL; |
| 1304 | |
| 1305 | ent = xmlGetDocEntity(doc, cur->name); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1306 | if (ent != NULL) { |
| 1307 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1308 | cur->content = ent->content; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1309 | #else |
| 1310 | /* |
| 1311 | * CJN 11.18.99 this might be a problem, since the xmlBuffer gets |
| 1312 | * a copy of this pointer. Let's hope we don't manipulate it |
| 1313 | * later |
| 1314 | */ |
| 1315 | cur->content = xmlBufferCreateSize(0); |
| 1316 | xmlBufferSetAllocationScheme(cur->content, |
| 1317 | xmlGetBufferAllocationScheme()); |
| 1318 | if (ent->content != NULL) |
| 1319 | xmlBufferAdd(cur->content, ent->content, -1); |
| 1320 | #endif |
| 1321 | } else |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1322 | cur->content = NULL; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 1323 | #ifndef XML_WITHOUT_CORBA |
| 1324 | cur->_private = NULL; |
| 1325 | cur->vepv = NULL; |
| 1326 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1327 | return(cur); |
| 1328 | } |
| 1329 | |
| 1330 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1331 | * xmlNewDocText: |
| 1332 | * @doc: the document |
| 1333 | * @content: the text content |
| 1334 | * |
| 1335 | * Creation of a new text node within a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1336 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1337 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1338 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1339 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1340 | xmlNodePtr cur; |
| 1341 | |
| 1342 | cur = xmlNewText(content); |
| 1343 | if (cur != NULL) cur->doc = doc; |
| 1344 | return(cur); |
| 1345 | } |
| 1346 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1347 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1348 | * xmlNewTextLen: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1349 | * @content: the text content |
| 1350 | * @len: the text len. |
| 1351 | * |
| 1352 | * 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] | 1353 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1354 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1355 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1356 | xmlNewTextLen(const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1357 | xmlNodePtr cur; |
| 1358 | |
| 1359 | /* |
| 1360 | * Allocate a new node and fill the fields. |
| 1361 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1362 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1363 | if (cur == NULL) { |
| 1364 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 1365 | return(NULL); |
| 1366 | } |
| 1367 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1368 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1369 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1370 | cur->parent = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1371 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1372 | cur->next = NULL; |
| 1373 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1374 | cur->last = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1375 | cur->properties = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1376 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1377 | cur->name = xmlStrdup(xmlStringText); |
| 1378 | cur->ns = NULL; |
| 1379 | cur->nsDef = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1380 | if (content != NULL) { |
| 1381 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1382 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1383 | #else |
| 1384 | cur->content = xmlBufferCreateSize(len); |
| 1385 | xmlBufferSetAllocationScheme(cur->content, |
| 1386 | xmlGetBufferAllocationScheme()); |
| 1387 | xmlBufferAdd(cur->content, content, len); |
| 1388 | #endif |
| 1389 | } else |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1390 | cur->content = NULL; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 1391 | #ifndef XML_WITHOUT_CORBA |
| 1392 | cur->_private = NULL; |
| 1393 | cur->vepv = NULL; |
| 1394 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1395 | return(cur); |
| 1396 | } |
| 1397 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1398 | /** |
| 1399 | * xmlNewDocTextLen: |
| 1400 | * @doc: the document |
| 1401 | * @content: the text content |
| 1402 | * @len: the text len. |
| 1403 | * |
| 1404 | * Creation of a new text node with an extra content lenght parameter. The |
| 1405 | * text node pertain to a given document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1406 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1407 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1408 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1409 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1410 | xmlNodePtr cur; |
| 1411 | |
| 1412 | cur = xmlNewTextLen(content, len); |
| 1413 | if (cur != NULL) cur->doc = doc; |
| 1414 | return(cur); |
| 1415 | } |
| 1416 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1417 | /** |
| 1418 | * xmlNewComment: |
| 1419 | * @content: the comment content |
| 1420 | * |
| 1421 | * Creation of a new node containing a comment. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1422 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1423 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1424 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1425 | xmlNewComment(const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1426 | xmlNodePtr cur; |
| 1427 | |
| 1428 | /* |
| 1429 | * Allocate a new node and fill the fields. |
| 1430 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1431 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1432 | if (cur == NULL) { |
| 1433 | fprintf(stderr, "xmlNewComment : malloc failed\n"); |
| 1434 | return(NULL); |
| 1435 | } |
| 1436 | |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1437 | cur->type = XML_COMMENT_NODE; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1438 | cur->doc = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1439 | cur->parent = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1440 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1441 | cur->next = NULL; |
| 1442 | cur->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1443 | cur->last = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1444 | cur->properties = NULL; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1445 | cur->type = XML_COMMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1446 | cur->name = xmlStrdup(xmlStringText); |
| 1447 | cur->ns = NULL; |
| 1448 | cur->nsDef = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1449 | if (content != NULL) { |
| 1450 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1451 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1452 | #else |
| 1453 | cur->content = xmlBufferCreateSize(0); |
| 1454 | xmlBufferSetAllocationScheme(cur->content, |
| 1455 | xmlGetBufferAllocationScheme()); |
| 1456 | xmlBufferAdd(cur->content, content, -1); |
| 1457 | #endif |
| 1458 | } else |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1459 | cur->content = NULL; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 1460 | #ifndef XML_WITHOUT_CORBA |
| 1461 | cur->_private = NULL; |
| 1462 | cur->vepv = NULL; |
| 1463 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1464 | return(cur); |
| 1465 | } |
| 1466 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1467 | /** |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1468 | * xmlNewCDataBlock: |
| 1469 | * @doc: the document |
| 1470 | * @content: the CData block content content |
| 1471 | * @len: the length of the block |
| 1472 | * |
| 1473 | * Creation of a new node containing a CData block. |
| 1474 | * Returns a pointer to the new node object. |
| 1475 | */ |
| 1476 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1477 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1478 | xmlNodePtr cur; |
| 1479 | |
| 1480 | /* |
| 1481 | * Allocate a new node and fill the fields. |
| 1482 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1483 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1484 | if (cur == NULL) { |
| 1485 | fprintf(stderr, "xmlNewCDataBlock : malloc failed\n"); |
| 1486 | return(NULL); |
| 1487 | } |
| 1488 | |
| 1489 | cur->type = XML_CDATA_SECTION_NODE; |
| 1490 | cur->doc = NULL; |
| 1491 | cur->parent = NULL; |
| 1492 | cur->prev = NULL; |
| 1493 | cur->next = NULL; |
| 1494 | cur->childs = NULL; |
| 1495 | cur->last = NULL; |
| 1496 | cur->properties = NULL; |
| 1497 | cur->name = xmlStrdup(xmlStringText); |
| 1498 | cur->ns = NULL; |
| 1499 | cur->nsDef = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1500 | if (content != NULL) { |
| 1501 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1502 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1503 | #else |
| 1504 | cur->content = xmlBufferCreateSize(len); |
| 1505 | xmlBufferSetAllocationScheme(cur->content, |
| 1506 | xmlGetBufferAllocationScheme()); |
| 1507 | xmlBufferAdd(cur->content, content, len); |
| 1508 | #endif |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1509 | } else |
| 1510 | cur->content = NULL; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 1511 | #ifndef XML_WITHOUT_CORBA |
| 1512 | cur->_private = NULL; |
| 1513 | cur->vepv = NULL; |
| 1514 | #endif |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1515 | return(cur); |
| 1516 | } |
| 1517 | |
| 1518 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1519 | * xmlNewDocComment: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1520 | * @doc: the document |
| 1521 | * @content: the comment content |
| 1522 | * |
| 1523 | * Creation of a new node containing a commentwithin a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1524 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1525 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1526 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1527 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1528 | xmlNodePtr cur; |
| 1529 | |
| 1530 | cur = xmlNewComment(content); |
| 1531 | if (cur != NULL) cur->doc = doc; |
| 1532 | return(cur); |
| 1533 | } |
| 1534 | |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1535 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1536 | /** |
| 1537 | * xmlNewChild: |
| 1538 | * @parent: the parent node |
| 1539 | * @ns: a namespace if any |
| 1540 | * @name: the name of the child |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1541 | * @content: the XML content of the child if any. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1542 | * |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1543 | * 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] | 1544 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1545 | * a child list containing the TEXTs and ENTITY_REFs node will be created. |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1546 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 1547 | * references, but XML special chars need to be escaped first by using |
| 1548 | * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities |
| 1549 | * support is not needed. |
| 1550 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1551 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1552 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1553 | xmlNodePtr |
| 1554 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1555 | const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1556 | xmlNodePtr cur, prev; |
| 1557 | |
| 1558 | if (parent == NULL) { |
| 1559 | fprintf(stderr, "xmlNewChild : parent == NULL\n"); |
| 1560 | return(NULL); |
| 1561 | } |
| 1562 | |
| 1563 | if (name == NULL) { |
| 1564 | fprintf(stderr, "xmlNewChild : name == NULL\n"); |
| 1565 | return(NULL); |
| 1566 | } |
| 1567 | |
| 1568 | /* |
| 1569 | * Allocate a new node |
| 1570 | */ |
| 1571 | if (ns == NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1572 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1573 | else |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1574 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1575 | if (cur == NULL) return(NULL); |
| 1576 | |
| 1577 | /* |
| 1578 | * add the new element at the end of the childs list. |
| 1579 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1580 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1581 | cur->parent = parent; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1582 | cur->doc = parent->doc; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1583 | if (parent->childs == NULL) { |
| 1584 | parent->childs = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1585 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1586 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1587 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1588 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1589 | cur->prev = prev; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1590 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | return(cur); |
| 1594 | } |
| 1595 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1596 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1597 | * xmlAddNextSibling: |
| 1598 | * @cur: the child node |
| 1599 | * @elem: the new node |
| 1600 | * |
| 1601 | * Add a new element @elem as the next siblings of @cur |
| 1602 | * If the new element was already inserted in a document it is |
| 1603 | * first unlinked from its existing context. |
| 1604 | * |
| 1605 | * Returns the new element or NULL in case of error. |
| 1606 | */ |
| 1607 | xmlNodePtr |
| 1608 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1609 | if (cur == NULL) { |
| 1610 | fprintf(stderr, "xmlAddNextSibling : cur == NULL\n"); |
| 1611 | return(NULL); |
| 1612 | } |
| 1613 | if (elem == NULL) { |
| 1614 | fprintf(stderr, "xmlAddNextSibling : elem == NULL\n"); |
| 1615 | return(NULL); |
| 1616 | } |
| 1617 | |
| 1618 | xmlUnlinkNode(elem); |
| 1619 | elem->doc = cur->doc; |
| 1620 | elem->parent = cur->parent; |
| 1621 | elem->next = cur; |
| 1622 | elem->prev = cur->prev; |
| 1623 | cur->prev = elem; |
| 1624 | if (elem->prev != NULL) |
| 1625 | elem->prev->next = elem; |
| 1626 | if ((elem->parent != NULL) && (elem->parent->childs == cur)) |
| 1627 | elem->parent->childs = elem; |
| 1628 | return(elem); |
| 1629 | } |
| 1630 | |
| 1631 | /** |
| 1632 | * xmlAddPrevSibling: |
| 1633 | * @cur: the child node |
| 1634 | * @elem: the new node |
| 1635 | * |
| 1636 | * Add a new element @elem as the previous siblings of @cur |
| 1637 | * If the new element was already inserted in a document it is |
| 1638 | * first unlinked from its existing context. |
| 1639 | * |
| 1640 | * Returns the new element or NULL in case of error. |
| 1641 | */ |
| 1642 | xmlNodePtr |
| 1643 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1644 | if (cur == NULL) { |
| 1645 | fprintf(stderr, "xmlAddPrevSibling : cur == NULL\n"); |
| 1646 | return(NULL); |
| 1647 | } |
| 1648 | if (elem == NULL) { |
| 1649 | fprintf(stderr, "xmlAddPrevSibling : elem == NULL\n"); |
| 1650 | return(NULL); |
| 1651 | } |
| 1652 | |
| 1653 | xmlUnlinkNode(elem); |
| 1654 | elem->doc = cur->doc; |
| 1655 | elem->parent = cur->parent; |
| 1656 | elem->prev = cur; |
| 1657 | elem->next = cur->next; |
| 1658 | cur->next = elem; |
| 1659 | if (elem->next != NULL) |
| 1660 | elem->next->prev = elem; |
| 1661 | if ((elem->parent != NULL) && (elem->parent->last == cur)) |
| 1662 | elem->parent->last = elem; |
| 1663 | return(elem); |
| 1664 | } |
| 1665 | |
| 1666 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1667 | * xmlAddSibling: |
| 1668 | * @cur: the child node |
| 1669 | * @elem: the new node |
| 1670 | * |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1671 | * Add a new element @elem to the list of siblings of @cur |
| 1672 | * If the new element was already inserted in a document it is |
| 1673 | * first unlinked from its existing context. |
| 1674 | * |
| 1675 | * Returns the new element or NULL in case of error. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1676 | */ |
| 1677 | xmlNodePtr |
| 1678 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1679 | xmlNodePtr parent; |
| 1680 | |
| 1681 | if (cur == NULL) { |
| 1682 | fprintf(stderr, "xmlAddSibling : cur == NULL\n"); |
| 1683 | return(NULL); |
| 1684 | } |
| 1685 | |
| 1686 | if (elem == NULL) { |
| 1687 | fprintf(stderr, "xmlAddSibling : elem == NULL\n"); |
| 1688 | return(NULL); |
| 1689 | } |
| 1690 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1691 | /* |
| 1692 | * Constant time is we can rely on the ->parent->last to find |
| 1693 | * the last sibling. |
| 1694 | */ |
| 1695 | if ((cur->parent != NULL) && |
| 1696 | (cur->parent->childs != NULL) && |
| 1697 | (cur->parent->last != NULL) && |
| 1698 | (cur->parent->last->next == NULL)) { |
| 1699 | cur = cur->parent->last; |
| 1700 | } else { |
| 1701 | while (cur->next != NULL) cur = cur->next; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1704 | xmlUnlinkNode(elem); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1705 | if (elem->doc == NULL) |
| 1706 | elem->doc = cur->doc; /* the parent may not be linked to a doc ! */ |
| 1707 | |
| 1708 | parent = cur->parent; |
| 1709 | elem->prev = cur; |
| 1710 | elem->next = NULL; |
| 1711 | elem->parent = parent; |
| 1712 | cur->next = elem; |
| 1713 | if (parent != NULL) |
| 1714 | parent->last = elem; |
| 1715 | |
| 1716 | return(elem); |
| 1717 | } |
| 1718 | |
| 1719 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1720 | * xmlAddChild: |
| 1721 | * @parent: the parent node |
| 1722 | * @cur: the child node |
| 1723 | * |
| 1724 | * 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] | 1725 | * Returns the child or NULL in case of error. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1726 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1727 | xmlNodePtr |
| 1728 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1729 | xmlNodePtr prev; |
| 1730 | |
| 1731 | if (parent == NULL) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 1732 | fprintf(stderr, "xmlAddChild : parent == NULL\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1733 | return(NULL); |
| 1734 | } |
| 1735 | |
| 1736 | if (cur == NULL) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 1737 | fprintf(stderr, "xmlAddChild : child == NULL\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1738 | return(NULL); |
| 1739 | } |
| 1740 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1741 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 1742 | (cur->doc != parent->doc)) { |
| 1743 | fprintf(stderr, "Elements moved to a different document\n"); |
| 1744 | } |
| 1745 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1746 | /* |
| 1747 | * add the new element at the end of the childs list. |
| 1748 | */ |
| 1749 | cur->parent = parent; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1750 | 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] | 1751 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1752 | /* |
| 1753 | * Handle the case where parent->content != NULL, in that case it will |
| 1754 | * create a intermediate TEXT node. |
| 1755 | */ |
| 1756 | if (parent->content != NULL) { |
| 1757 | xmlNodePtr text; |
| 1758 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1759 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1760 | text = xmlNewDocText(parent->doc, parent->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1761 | #else |
| 1762 | text = xmlNewDocText(parent->doc, xmlBufferContent(parent->content)); |
| 1763 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1764 | if (text != NULL) { |
| 1765 | text->next = parent->childs; |
| 1766 | if (text->next != NULL) |
| 1767 | text->next->prev = text; |
| 1768 | parent->childs = text; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1769 | UPDATE_LAST_CHILD(parent) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1770 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1771 | xmlFree(parent->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1772 | #else |
| 1773 | xmlBufferFree(parent->content); |
| 1774 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1775 | parent->content = NULL; |
| 1776 | } |
| 1777 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1778 | if (parent->childs == NULL) { |
| 1779 | parent->childs = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1780 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1781 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1782 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1783 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1784 | cur->prev = prev; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1785 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | return(cur); |
| 1789 | } |
| 1790 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1791 | /** |
| 1792 | * xmlGetLastChild: |
| 1793 | * @parent: the parent node |
| 1794 | * |
| 1795 | * Search the last child of a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1796 | * Returns the last child or NULL if none. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1797 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1798 | xmlNodePtr |
| 1799 | xmlGetLastChild(xmlNodePtr parent) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1800 | if (parent == NULL) { |
| 1801 | fprintf(stderr, "xmlGetLastChild : parent == NULL\n"); |
| 1802 | return(NULL); |
| 1803 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1804 | return(parent->last); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1807 | /** |
| 1808 | * xmlFreeNodeList: |
| 1809 | * @cur: the first node in the list |
| 1810 | * |
| 1811 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 1812 | * the childs are freed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1813 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1814 | void |
| 1815 | xmlFreeNodeList(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1816 | xmlNodePtr next; |
| 1817 | if (cur == NULL) { |
| 1818 | fprintf(stderr, "xmlFreeNodeList : node == NULL\n"); |
| 1819 | return; |
| 1820 | } |
| 1821 | while (cur != NULL) { |
| 1822 | next = cur->next; |
| 1823 | xmlFreeNode(cur); |
| 1824 | cur = next; |
| 1825 | } |
| 1826 | } |
| 1827 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1828 | /** |
| 1829 | * xmlFreeNode: |
| 1830 | * @cur: the node |
| 1831 | * |
| 1832 | * 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] | 1833 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1834 | void |
| 1835 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1836 | if (cur == NULL) { |
| 1837 | fprintf(stderr, "xmlFreeNode : node == NULL\n"); |
| 1838 | return; |
| 1839 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1840 | cur->doc = NULL; |
| 1841 | cur->parent = NULL; |
| 1842 | cur->next = NULL; |
| 1843 | cur->prev = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1844 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1845 | if (cur->properties != NULL) xmlFreePropList(cur->properties); |
| 1846 | if (cur->type != XML_ENTITY_REF_NODE) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1847 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1848 | if (cur->content != NULL) xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1849 | #else |
| 1850 | if (cur->content != NULL) xmlBufferFree(cur->content); |
| 1851 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1852 | if (cur->name != NULL) xmlFree((char *) cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1853 | if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef); |
| 1854 | memset(cur, -1, sizeof(xmlNode)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1855 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1858 | /** |
| 1859 | * xmlUnlinkNode: |
| 1860 | * @cur: the node |
| 1861 | * |
| 1862 | * Unlink a node from it's current context, the node is not freed |
| 1863 | */ |
| 1864 | void |
| 1865 | xmlUnlinkNode(xmlNodePtr cur) { |
| 1866 | if (cur == NULL) { |
| 1867 | fprintf(stderr, "xmlUnlinkNode : node == NULL\n"); |
| 1868 | return; |
| 1869 | } |
| 1870 | if ((cur->parent != NULL) && (cur->parent->childs == cur)) |
| 1871 | cur->parent->childs = cur->next; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1872 | if ((cur->parent != NULL) && (cur->parent->last == cur)) |
| 1873 | cur->parent->last = cur->prev; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 1874 | if (cur->next != NULL) |
| 1875 | cur->next->prev = cur->prev; |
| 1876 | if (cur->prev != NULL) |
| 1877 | cur->prev->next = cur->next; |
| 1878 | cur->next = cur->prev = NULL; |
| 1879 | cur->parent = NULL; |
| 1880 | } |
| 1881 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1882 | /** |
| 1883 | * xmlReplaceNode: |
| 1884 | * @old: the old node |
| 1885 | * @cur: the node |
| 1886 | * |
| 1887 | * Unlink the old node from it's current context, prune the new one |
| 1888 | * at the same place. If cur was already inserted in a document it is |
| 1889 | * first unlinked from its existing context. |
| 1890 | * |
| 1891 | * Returns the old node |
| 1892 | */ |
| 1893 | xmlNodePtr |
| 1894 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
| 1895 | if (old == NULL) { |
| 1896 | fprintf(stderr, "xmlReplaceNode : old == NULL\n"); |
| 1897 | return(NULL); |
| 1898 | } |
| 1899 | if (cur == NULL) { |
| 1900 | xmlUnlinkNode(old); |
| 1901 | return(old); |
| 1902 | } |
| 1903 | xmlUnlinkNode(cur); |
| 1904 | cur->doc = old->doc; |
| 1905 | cur->parent = old->parent; |
| 1906 | cur->next = old->next; |
| 1907 | if (cur->next != NULL) |
| 1908 | cur->next->prev = cur; |
| 1909 | cur->prev = old->prev; |
| 1910 | if (cur->prev != NULL) |
| 1911 | cur->prev->next = cur; |
| 1912 | if (cur->parent != NULL) { |
| 1913 | if (cur->parent->childs == old) |
| 1914 | cur->parent->childs = cur; |
| 1915 | if (cur->parent->last == old) |
| 1916 | cur->parent->last = cur; |
| 1917 | } |
| 1918 | old->next = old->prev = NULL; |
| 1919 | old->parent = NULL; |
| 1920 | return(old); |
| 1921 | } |
| 1922 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1923 | /************************************************************************ |
| 1924 | * * |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1925 | * Copy operations * |
| 1926 | * * |
| 1927 | ************************************************************************/ |
| 1928 | |
| 1929 | /** |
| 1930 | * xmlCopyNamespace: |
| 1931 | * @cur: the namespace |
| 1932 | * |
| 1933 | * Do a copy of the namespace. |
| 1934 | * |
| 1935 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 1936 | */ |
| 1937 | xmlNsPtr |
| 1938 | xmlCopyNamespace(xmlNsPtr cur) { |
| 1939 | xmlNsPtr ret; |
| 1940 | |
| 1941 | if (cur == NULL) return(NULL); |
| 1942 | switch (cur->type) { |
| 1943 | case XML_GLOBAL_NAMESPACE: |
| 1944 | ret = xmlNewGlobalNs(NULL, cur->href, cur->prefix); |
| 1945 | break; |
| 1946 | case XML_LOCAL_NAMESPACE: |
| 1947 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 1948 | break; |
| 1949 | default: |
| 1950 | fprintf(stderr, "xmlCopyNamespace: unknown type %d\n", cur->type); |
| 1951 | return(NULL); |
| 1952 | } |
| 1953 | return(ret); |
| 1954 | } |
| 1955 | |
| 1956 | /** |
| 1957 | * xmlCopyNamespaceList: |
| 1958 | * @cur: the first namespace |
| 1959 | * |
| 1960 | * Do a copy of an namespace list. |
| 1961 | * |
| 1962 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 1963 | */ |
| 1964 | xmlNsPtr |
| 1965 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 1966 | xmlNsPtr ret = NULL; |
| 1967 | xmlNsPtr p = NULL,q; |
| 1968 | |
| 1969 | while (cur != NULL) { |
| 1970 | q = xmlCopyNamespace(cur); |
| 1971 | if (p == NULL) { |
| 1972 | ret = p = q; |
| 1973 | } else { |
| 1974 | p->next = q; |
| 1975 | p = q; |
| 1976 | } |
| 1977 | cur = cur->next; |
| 1978 | } |
| 1979 | return(ret); |
| 1980 | } |
| 1981 | |
| 1982 | /** |
| 1983 | * xmlCopyProp: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1984 | * @target: the element where the attribute will be grafted |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1985 | * @cur: the attribute |
| 1986 | * |
| 1987 | * Do a copy of the attribute. |
| 1988 | * |
| 1989 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 1990 | */ |
| 1991 | xmlAttrPtr |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1992 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 1993 | xmlAttrPtr ret; |
| 1994 | |
| 1995 | if (cur == NULL) return(NULL); |
| 1996 | if (cur->val != NULL) |
| 1997 | ret = xmlNewDocProp(cur->val->doc, cur->name, NULL); |
| 1998 | else |
| 1999 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 2000 | if (ret == NULL) return(NULL); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2001 | |
| 2002 | if ((cur->ns != NULL) && (target != NULL)) { |
| 2003 | xmlNsPtr ns; |
| 2004 | |
| 2005 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 2006 | ret->ns = ns; |
| 2007 | } else |
| 2008 | ret->ns = NULL; |
| 2009 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2010 | if (cur->val != NULL) |
| 2011 | ret->val = xmlCopyNodeList(cur->val); |
| 2012 | return(ret); |
| 2013 | } |
| 2014 | |
| 2015 | /** |
| 2016 | * xmlCopyPropList: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2017 | * @target: the element where the attributes will be grafted |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2018 | * @cur: the first attribute |
| 2019 | * |
| 2020 | * Do a copy of an attribute list. |
| 2021 | * |
| 2022 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 2023 | */ |
| 2024 | xmlAttrPtr |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2025 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2026 | xmlAttrPtr ret = NULL; |
| 2027 | xmlAttrPtr p = NULL,q; |
| 2028 | |
| 2029 | while (cur != NULL) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2030 | q = xmlCopyProp(target, cur); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2031 | if (p == NULL) { |
| 2032 | ret = p = q; |
| 2033 | } else { |
| 2034 | p->next = q; |
| 2035 | p = q; |
| 2036 | } |
| 2037 | cur = cur->next; |
| 2038 | } |
| 2039 | return(ret); |
| 2040 | } |
| 2041 | |
| 2042 | /* |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 2043 | * NOTE abeut the CopyNode operations ! |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2044 | * |
| 2045 | * They are splitted into external and internal parts for one |
| 2046 | * tricky reason: namespaces. Doing a direct copy of a node |
| 2047 | * say RPM:Copyright without changing the namespace pointer to |
| 2048 | * something else can produce stale links. One way to do it is |
| 2049 | * to keep a reference counter but this doesn't work as soon |
| 2050 | * as one move the element or the subtree out of the scope of |
| 2051 | * the existing namespace. The actual solution seems to add |
| 2052 | * a copy of the namespace at the top of the copied tree if |
| 2053 | * not available in the subtree. |
| 2054 | * Hence two functions, the public front-end call the inner ones |
| 2055 | */ |
| 2056 | |
| 2057 | static xmlNodePtr |
| 2058 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
| 2059 | |
| 2060 | static xmlNodePtr |
| 2061 | xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
| 2062 | int recursive) { |
| 2063 | xmlNodePtr ret; |
| 2064 | |
| 2065 | if (node == NULL) return(NULL); |
| 2066 | /* |
| 2067 | * Allocate a new node and fill the fields. |
| 2068 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2069 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2070 | if (ret == NULL) { |
| 2071 | fprintf(stderr, "xmlStaticCopyNode : malloc failed\n"); |
| 2072 | return(NULL); |
| 2073 | } |
| 2074 | |
| 2075 | ret->type = node->type; |
| 2076 | ret->doc = doc; |
| 2077 | ret->parent = parent; |
| 2078 | ret->next = NULL; |
| 2079 | ret->prev = NULL; |
| 2080 | ret->childs = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2081 | ret->last = NULL; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2082 | ret->properties = NULL; |
| 2083 | if (node->name != NULL) |
| 2084 | ret->name = xmlStrdup(node->name); |
| 2085 | else |
| 2086 | ret->name = NULL; |
| 2087 | ret->ns = NULL; |
| 2088 | ret->nsDef = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2089 | if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) { |
| 2090 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2091 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2092 | #else |
| 2093 | ret->content = xmlBufferCreateSize(xmlBufferLength(node->content)); |
| 2094 | xmlBufferSetAllocationScheme(ret->content, |
| 2095 | xmlGetBufferAllocationScheme()); |
| 2096 | xmlBufferAdd(ret->content, |
| 2097 | xmlBufferContent(node->content), |
| 2098 | xmlBufferLength(node->content)); |
| 2099 | #endif |
| 2100 | } else |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2101 | ret->content = NULL; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 2102 | #ifndef XML_WITHOUT_CORBA |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2103 | ret->_private = NULL; |
| 2104 | ret->vepv = NULL; |
| 2105 | #endif |
| 2106 | if (parent != NULL) |
| 2107 | xmlAddChild(parent, ret); |
| 2108 | |
| 2109 | if (!recursive) return(ret); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2110 | if (node->nsDef != NULL) |
| 2111 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 2112 | |
| 2113 | if (node->ns != NULL) { |
| 2114 | xmlNsPtr ns; |
| 2115 | |
| 2116 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 2117 | if (ns == NULL) { |
| 2118 | /* |
| 2119 | * Humm, we are copying an element whose namespace is defined |
| 2120 | * out of the new tree scope. Search it in the original tree |
| 2121 | * and add it at the top of the new tree |
| 2122 | */ |
| 2123 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 2124 | if (ns != NULL) { |
| 2125 | xmlNodePtr root = ret; |
| 2126 | |
| 2127 | while (root->parent != NULL) root = root->parent; |
| 2128 | xmlNewNs(root, ns->href, ns->prefix); |
| 2129 | } |
| 2130 | } else { |
| 2131 | /* |
| 2132 | * reference the existing namespace definition in our own tree. |
| 2133 | */ |
| 2134 | ret->ns = ns; |
| 2135 | } |
| 2136 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2137 | if (node->properties != NULL) |
| 2138 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2139 | if (node->childs != NULL) |
| 2140 | ret->childs = xmlStaticCopyNodeList(node->childs, doc, ret); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2141 | UPDATE_LAST_CHILD(ret) |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2142 | return(ret); |
| 2143 | } |
| 2144 | |
| 2145 | static xmlNodePtr |
| 2146 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 2147 | xmlNodePtr ret = NULL; |
| 2148 | xmlNodePtr p = NULL,q; |
| 2149 | |
| 2150 | while (node != NULL) { |
| 2151 | q = xmlStaticCopyNode(node, doc, parent, 1); |
| 2152 | if (parent == NULL) { |
| 2153 | if (ret == NULL) ret = q; |
| 2154 | } else { |
| 2155 | if (ret == NULL) { |
| 2156 | q->prev = NULL; |
| 2157 | ret = p = q; |
| 2158 | } else { |
| 2159 | p->next = q; |
| 2160 | q->prev = p; |
| 2161 | p = q; |
| 2162 | } |
| 2163 | } |
| 2164 | node = node->next; |
| 2165 | } |
| 2166 | return(ret); |
| 2167 | } |
| 2168 | |
| 2169 | /** |
| 2170 | * xmlCopyNode: |
| 2171 | * @node: the node |
| 2172 | * @recursive: if 1 do a recursive copy. |
| 2173 | * |
| 2174 | * Do a copy of the node. |
| 2175 | * |
| 2176 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 2177 | */ |
| 2178 | xmlNodePtr |
| 2179 | xmlCopyNode(xmlNodePtr node, int recursive) { |
| 2180 | xmlNodePtr ret; |
| 2181 | |
| 2182 | ret = xmlStaticCopyNode(node, NULL, NULL, recursive); |
| 2183 | return(ret); |
| 2184 | } |
| 2185 | |
| 2186 | /** |
| 2187 | * xmlCopyNodeList: |
| 2188 | * @node: the first node in the list. |
| 2189 | * |
| 2190 | * Do a recursive copy of the node list. |
| 2191 | * |
| 2192 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 2193 | */ |
| 2194 | xmlNodePtr xmlCopyNodeList(xmlNodePtr node) { |
| 2195 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 2196 | return(ret); |
| 2197 | } |
| 2198 | |
| 2199 | /** |
| 2200 | * xmlCopyElement: |
| 2201 | * @elem: the element |
| 2202 | * |
| 2203 | * Do a copy of the element definition. |
| 2204 | * |
| 2205 | * Returns: a new xmlElementPtr, or NULL in case of error. |
| 2206 | xmlElementPtr |
| 2207 | xmlCopyElement(xmlElementPtr elem) { |
| 2208 | xmlElementPtr ret; |
| 2209 | |
| 2210 | if (elem == NULL) return(NULL); |
| 2211 | ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content); |
| 2212 | if (ret == NULL) return(NULL); |
| 2213 | if (!recursive) return(ret); |
| 2214 | if (elem->properties != NULL) |
| 2215 | ret->properties = xmlCopyPropList(elem->properties); |
| 2216 | |
| 2217 | if (elem->nsDef != NULL) |
| 2218 | ret->nsDef = xmlCopyNamespaceList(elem->nsDef); |
| 2219 | if (elem->childs != NULL) |
| 2220 | ret->childs = xmlCopyElementList(elem->childs); |
| 2221 | return(ret); |
| 2222 | } |
| 2223 | */ |
| 2224 | |
| 2225 | /** |
| 2226 | * xmlCopyDtd: |
| 2227 | * @dtd: the dtd |
| 2228 | * |
| 2229 | * Do a copy of the dtd. |
| 2230 | * |
| 2231 | * Returns: a new xmlDtdPtr, or NULL in case of error. |
| 2232 | */ |
| 2233 | xmlDtdPtr |
| 2234 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 2235 | xmlDtdPtr ret; |
| 2236 | |
| 2237 | if (dtd == NULL) return(NULL); |
| 2238 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 2239 | if (ret == NULL) return(NULL); |
| 2240 | if (dtd->entities != NULL) |
| 2241 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 2242 | (xmlEntitiesTablePtr) dtd->entities); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2243 | if (dtd->notations != NULL) |
| 2244 | ret->notations = (void *) xmlCopyNotationTable( |
| 2245 | (xmlNotationTablePtr) dtd->notations); |
| 2246 | if (dtd->elements != NULL) |
| 2247 | ret->elements = (void *) xmlCopyElementTable( |
| 2248 | (xmlElementTablePtr) dtd->elements); |
| 2249 | if (dtd->attributes != NULL) |
| 2250 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 2251 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2252 | return(ret); |
| 2253 | } |
| 2254 | |
| 2255 | /** |
| 2256 | * xmlCopyDoc: |
| 2257 | * @doc: the document |
| 2258 | * @recursive: if 1 do a recursive copy. |
| 2259 | * |
| 2260 | * Do a copy of the document info. If recursive, the content tree will |
| 2261 | * be copied too as well as Dtd, namespaces and entities. |
| 2262 | * |
| 2263 | * Returns: a new xmlDocPtr, or NULL in case of error. |
| 2264 | */ |
| 2265 | xmlDocPtr |
| 2266 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 2267 | xmlDocPtr ret; |
| 2268 | |
| 2269 | if (doc == NULL) return(NULL); |
| 2270 | ret = xmlNewDoc(doc->version); |
| 2271 | if (ret == NULL) return(NULL); |
| 2272 | if (doc->name != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2273 | ret->name = xmlMemStrdup(doc->name); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2274 | if (doc->encoding != NULL) |
| 2275 | ret->encoding = xmlStrdup(doc->encoding); |
| 2276 | ret->compression = doc->compression; |
| 2277 | ret->standalone = doc->standalone; |
| 2278 | if (!recursive) return(ret); |
| 2279 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2280 | if (doc->intSubset != NULL) |
| 2281 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2282 | if (doc->oldNs != NULL) |
| 2283 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 2284 | if (doc->root != NULL) |
| 2285 | ret->root = xmlStaticCopyNodeList(doc->root, ret, NULL); |
| 2286 | return(ret); |
| 2287 | } |
| 2288 | |
| 2289 | /************************************************************************ |
| 2290 | * * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2291 | * Content access functions * |
| 2292 | * * |
| 2293 | ************************************************************************/ |
| 2294 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2295 | /** |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 2296 | * xmlDocGetRootElement: |
| 2297 | * @doc: the document |
| 2298 | * |
| 2299 | * Get the root element of the document (doc->root is a list |
| 2300 | * containing possibly comments, PIs, etc ...). |
| 2301 | * |
| 2302 | * Returns the xmlNodePtr for the root or NULL |
| 2303 | */ |
| 2304 | xmlNodePtr |
| 2305 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 2306 | xmlNodePtr ret; |
| 2307 | |
| 2308 | if (doc == NULL) return(NULL); |
| 2309 | ret = doc->root; |
| 2310 | while (ret != NULL) { |
| 2311 | if (ret->type == XML_ELEMENT_NODE) |
| 2312 | return(ret); |
| 2313 | ret = ret->next; |
| 2314 | } |
| 2315 | return(ret); |
| 2316 | } |
| 2317 | |
| 2318 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2319 | * xmlDocSetRootElement: |
| 2320 | * @doc: the document |
| 2321 | * @root: the new document root element |
| 2322 | * |
| 2323 | * Set the root element of the document (doc->root is a list |
| 2324 | * containing possibly comments, PIs, etc ...). |
| 2325 | * |
| 2326 | * Returns the old root element if any was found |
| 2327 | */ |
| 2328 | xmlNodePtr |
| 2329 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 2330 | xmlNodePtr old = NULL; |
| 2331 | |
| 2332 | if (doc == NULL) return(NULL); |
| 2333 | old = doc->root; |
| 2334 | while (old != NULL) { |
| 2335 | if (old->type == XML_ELEMENT_NODE) |
| 2336 | break; |
| 2337 | old = old->next; |
| 2338 | } |
| 2339 | if (old == NULL) { |
| 2340 | if (doc->root == NULL) { |
| 2341 | doc->root = root; |
| 2342 | } else { |
| 2343 | xmlAddSibling(doc->root, root); |
| 2344 | } |
| 2345 | } else { |
| 2346 | xmlReplaceNode(old, root); |
| 2347 | } |
| 2348 | return(old); |
| 2349 | } |
| 2350 | |
| 2351 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2352 | * xmlNodeSetLang: |
| 2353 | * @cur: the node being changed |
| 2354 | * @lang: the langage description |
| 2355 | * |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2356 | * Set the language of a node, i.e. the values of the xml:lang |
| 2357 | * attribute. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2358 | */ |
| 2359 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2360 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2361 | if (cur == NULL) return; |
| 2362 | switch(cur->type) { |
| 2363 | case XML_TEXT_NODE: |
| 2364 | case XML_CDATA_SECTION_NODE: |
| 2365 | case XML_COMMENT_NODE: |
| 2366 | case XML_DOCUMENT_NODE: |
| 2367 | case XML_DOCUMENT_TYPE_NODE: |
| 2368 | case XML_DOCUMENT_FRAG_NODE: |
| 2369 | case XML_NOTATION_NODE: |
| 2370 | case XML_HTML_DOCUMENT_NODE: |
| 2371 | return; |
| 2372 | case XML_ELEMENT_NODE: |
| 2373 | case XML_ATTRIBUTE_NODE: |
| 2374 | case XML_PI_NODE: |
| 2375 | case XML_ENTITY_REF_NODE: |
| 2376 | case XML_ENTITY_NODE: |
| 2377 | break; |
| 2378 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2379 | xmlSetProp(cur, BAD_CAST "xml:lang", lang); |
| 2380 | } |
| 2381 | |
| 2382 | /** |
| 2383 | * xmlNodeGetLang: |
| 2384 | * @cur: the node being checked |
| 2385 | * |
| 2386 | * Searches the language of a node, i.e. the values of the xml:lang |
| 2387 | * attribute or the one carried by the nearest ancestor. |
| 2388 | * |
| 2389 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2390 | * It's up to the caller to free the memory. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2391 | */ |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2392 | xmlChar * |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2393 | xmlNodeGetLang(xmlNodePtr cur) { |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2394 | xmlChar *lang; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2395 | |
| 2396 | while (cur != NULL) { |
| 2397 | lang = xmlGetProp(cur, BAD_CAST "xml:lang"); |
| 2398 | if (lang != NULL) |
| 2399 | return(lang); |
| 2400 | cur = cur->parent; |
| 2401 | } |
| 2402 | return(NULL); |
| 2403 | } |
| 2404 | |
| 2405 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2406 | * xmlNodeSetName: |
| 2407 | * @cur: the node being changed |
| 2408 | * @name: the new tag name |
| 2409 | * |
| 2410 | * Searches the language of a node, i.e. the values of the xml:lang |
| 2411 | * attribute or the one carried by the nearest ancestor. |
| 2412 | */ |
| 2413 | void |
| 2414 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
| 2415 | if (cur == NULL) return; |
| 2416 | if (name == NULL) return; |
| 2417 | switch(cur->type) { |
| 2418 | case XML_TEXT_NODE: |
| 2419 | case XML_CDATA_SECTION_NODE: |
| 2420 | case XML_COMMENT_NODE: |
| 2421 | case XML_DOCUMENT_NODE: |
| 2422 | case XML_DOCUMENT_TYPE_NODE: |
| 2423 | case XML_DOCUMENT_FRAG_NODE: |
| 2424 | case XML_NOTATION_NODE: |
| 2425 | case XML_HTML_DOCUMENT_NODE: |
| 2426 | return; |
| 2427 | case XML_ELEMENT_NODE: |
| 2428 | case XML_ATTRIBUTE_NODE: |
| 2429 | case XML_PI_NODE: |
| 2430 | case XML_ENTITY_REF_NODE: |
| 2431 | case XML_ENTITY_NODE: |
| 2432 | break; |
| 2433 | } |
| 2434 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 2435 | cur->name = xmlStrdup(name); |
| 2436 | } |
| 2437 | |
| 2438 | /** |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 2439 | * xmlNodeGetBase: |
| 2440 | * @doc: the document the node pertains to |
| 2441 | * @cur: the node being checked |
| 2442 | * |
| 2443 | * Searches for the BASE URL. The code should work on both XML |
| 2444 | * and HTML document even if base mechanisms are completely different. |
| 2445 | * |
| 2446 | * Returns a pointer to the base URL, or NULL if not found |
| 2447 | * It's up to the caller to free the memory. |
| 2448 | */ |
| 2449 | xmlChar * |
| 2450 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
| 2451 | xmlChar *base; |
| 2452 | |
| 2453 | if ((cur == NULL) && (doc == NULL)) |
| 2454 | return(NULL); |
| 2455 | if (doc == NULL) doc = cur->doc; |
| 2456 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 2457 | cur = doc->root; |
| 2458 | while ((cur != NULL) && (cur->name != NULL)) { |
| 2459 | if (cur->type != XML_ELEMENT_NODE) { |
| 2460 | cur = cur->next; |
| 2461 | continue; |
| 2462 | } |
| 2463 | if ((!xmlStrcmp(cur->name, BAD_CAST "html")) || |
| 2464 | (!xmlStrcmp(cur->name, BAD_CAST "HTML"))) { |
| 2465 | cur = cur->childs; |
| 2466 | continue; |
| 2467 | } |
| 2468 | if ((!xmlStrcmp(cur->name, BAD_CAST "head")) || |
| 2469 | (!xmlStrcmp(cur->name, BAD_CAST "HEAD"))) { |
| 2470 | cur = cur->childs; |
| 2471 | continue; |
| 2472 | } |
| 2473 | if ((!xmlStrcmp(cur->name, BAD_CAST "base")) || |
| 2474 | (!xmlStrcmp(cur->name, BAD_CAST "BASE"))) { |
| 2475 | base = xmlGetProp(cur, BAD_CAST "href"); |
| 2476 | if (base != NULL) return(base); |
| 2477 | return(xmlGetProp(cur, BAD_CAST "HREF")); |
| 2478 | } |
| 2479 | } |
| 2480 | return(NULL); |
| 2481 | } |
| 2482 | while (cur != NULL) { |
| 2483 | base = xmlGetProp(cur, BAD_CAST "xml:base"); |
| 2484 | if (base != NULL) |
| 2485 | return(base); |
| 2486 | cur = cur->parent; |
| 2487 | } |
| 2488 | return(NULL); |
| 2489 | } |
| 2490 | |
| 2491 | /** |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2492 | * xmlNodeGetContent: |
| 2493 | * @cur: the node being read |
| 2494 | * |
| 2495 | * Read the value of a node, this can be either the text carried |
| 2496 | * directly by this node if it's a TEXT node or the aggregate string |
| 2497 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 2498 | * Entity references are substitued. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2499 | * Returns a new xmlChar * or NULL if no content is available. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2500 | * It's up to the caller to free the memory. |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2501 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2502 | xmlChar * |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2503 | xmlNodeGetContent(xmlNodePtr cur) { |
| 2504 | if (cur == NULL) return(NULL); |
| 2505 | switch (cur->type) { |
| 2506 | case XML_DOCUMENT_FRAG_NODE: |
| 2507 | case XML_ELEMENT_NODE: |
| 2508 | return(xmlNodeListGetString(cur->doc, cur->childs, 1)); |
| 2509 | break; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2510 | case XML_ATTRIBUTE_NODE: { |
| 2511 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 2512 | if (attr->node != NULL) |
| 2513 | return(xmlNodeListGetString(attr->node->doc, attr->val, 1)); |
| 2514 | else |
| 2515 | return(xmlNodeListGetString(NULL, attr->val, 1)); |
| 2516 | break; |
| 2517 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 2518 | case XML_COMMENT_NODE: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2519 | case XML_PI_NODE: |
| 2520 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2521 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2522 | return(xmlStrdup(cur->content)); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2523 | #else |
| 2524 | return(xmlStrdup(xmlBufferContent(cur->content))); |
| 2525 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2526 | return(NULL); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2527 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 2528 | /* |
| 2529 | * Locate the entity, and get it's content |
| 2530 | * @@@ |
| 2531 | */ |
| 2532 | return(NULL); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2533 | case XML_ENTITY_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2534 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 2535 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2536 | case XML_DOCUMENT_TYPE_NODE: |
| 2537 | case XML_NOTATION_NODE: |
| 2538 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2539 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2540 | case XML_TEXT_NODE: |
| 2541 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2542 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2543 | return(xmlStrdup(cur->content)); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2544 | #else |
| 2545 | return(xmlStrdup(xmlBufferContent(cur->content))); |
| 2546 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2547 | return(NULL); |
| 2548 | } |
| 2549 | return(NULL); |
| 2550 | } |
| 2551 | |
| 2552 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2553 | * xmlNodeSetContent: |
| 2554 | * @cur: the node being modified |
| 2555 | * @content: the new value of the content |
| 2556 | * |
| 2557 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2558 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2559 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2560 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2561 | if (cur == NULL) { |
| 2562 | fprintf(stderr, "xmlNodeSetContent : node == NULL\n"); |
| 2563 | return; |
| 2564 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2565 | switch (cur->type) { |
| 2566 | case XML_DOCUMENT_FRAG_NODE: |
| 2567 | case XML_ELEMENT_NODE: |
| 2568 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2569 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2570 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2571 | #else |
| 2572 | xmlBufferFree(cur->content); |
| 2573 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2574 | cur->content = NULL; |
| 2575 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2576 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2577 | cur->childs = xmlStringGetNodeList(cur->doc, content); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2578 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2579 | break; |
| 2580 | case XML_ATTRIBUTE_NODE: |
| 2581 | break; |
| 2582 | case XML_TEXT_NODE: |
| 2583 | case XML_CDATA_SECTION_NODE: |
| 2584 | case XML_ENTITY_REF_NODE: |
| 2585 | case XML_ENTITY_NODE: |
| 2586 | case XML_PI_NODE: |
| 2587 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2588 | if (cur->content != NULL) { |
| 2589 | #ifndef XML_USE_BUFFER_CONTENT |
| 2590 | xmlFree(cur->content); |
| 2591 | #else |
| 2592 | xmlBufferFree(cur->content); |
| 2593 | #endif |
| 2594 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2595 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2596 | cur->last = cur->childs = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2597 | if (content != NULL) { |
| 2598 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2599 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2600 | #else |
| 2601 | cur->content = xmlBufferCreateSize(0); |
| 2602 | xmlBufferSetAllocationScheme(cur->content, |
| 2603 | xmlGetBufferAllocationScheme()); |
| 2604 | xmlBufferAdd(cur->content, content, -1); |
| 2605 | #endif |
| 2606 | } else |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2607 | cur->content = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2608 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2609 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 2610 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2611 | case XML_DOCUMENT_TYPE_NODE: |
| 2612 | break; |
| 2613 | case XML_NOTATION_NODE: |
| 2614 | break; |
| 2615 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2618 | /** |
| 2619 | * xmlNodeSetContentLen: |
| 2620 | * @cur: the node being modified |
| 2621 | * @content: the new value of the content |
| 2622 | * @len: the size of @content |
| 2623 | * |
| 2624 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2625 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2626 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2627 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2628 | if (cur == NULL) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2629 | fprintf(stderr, "xmlNodeSetContentLen : node == NULL\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2630 | return; |
| 2631 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2632 | switch (cur->type) { |
| 2633 | case XML_DOCUMENT_FRAG_NODE: |
| 2634 | case XML_ELEMENT_NODE: |
| 2635 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2636 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2637 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2638 | #else |
| 2639 | xmlBufferFree(cur->content); |
| 2640 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2641 | cur->content = NULL; |
| 2642 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2643 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2644 | cur->childs = xmlStringLenGetNodeList(cur->doc, content, len); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2645 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2646 | break; |
| 2647 | case XML_ATTRIBUTE_NODE: |
| 2648 | break; |
| 2649 | case XML_TEXT_NODE: |
| 2650 | case XML_CDATA_SECTION_NODE: |
| 2651 | case XML_ENTITY_REF_NODE: |
| 2652 | case XML_ENTITY_NODE: |
| 2653 | case XML_PI_NODE: |
| 2654 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2655 | case XML_NOTATION_NODE: |
| 2656 | if (cur->content != NULL) { |
| 2657 | #ifndef XML_USE_BUFFER_CONTENT |
| 2658 | xmlFree(cur->content); |
| 2659 | #else |
| 2660 | xmlBufferFree(cur->content); |
| 2661 | #endif |
| 2662 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2663 | if (cur->childs != NULL) xmlFreeNodeList(cur->childs); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2664 | cur->childs = cur->last = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2665 | if (content != NULL) { |
| 2666 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2667 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2668 | #else |
| 2669 | cur->content = xmlBufferCreateSize(len); |
| 2670 | xmlBufferSetAllocationScheme(cur->content, |
| 2671 | xmlGetBufferAllocationScheme()); |
| 2672 | xmlBufferAdd(cur->content, content, len); |
| 2673 | #endif |
| 2674 | } else |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2675 | cur->content = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2676 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2677 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 2678 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2679 | case XML_DOCUMENT_TYPE_NODE: |
| 2680 | break; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2681 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2684 | /** |
| 2685 | * xmlNodeAddContentLen: |
| 2686 | * @cur: the node being modified |
| 2687 | * @content: extra content |
| 2688 | * @len: the size of @content |
| 2689 | * |
| 2690 | * Append the extra substring to the node content. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2691 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2692 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2693 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2694 | if (cur == NULL) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2695 | fprintf(stderr, "xmlNodeAddContentLen : node == NULL\n"); |
| 2696 | return; |
| 2697 | } |
| 2698 | if (len <= 0) return; |
| 2699 | switch (cur->type) { |
| 2700 | case XML_DOCUMENT_FRAG_NODE: |
| 2701 | case XML_ELEMENT_NODE: { |
| 2702 | xmlNodePtr last = NULL, new; |
| 2703 | |
| 2704 | if (cur->childs != NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2705 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2706 | } else { |
| 2707 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2708 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2709 | cur->childs = xmlStringGetNodeList(cur->doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2710 | #else |
| 2711 | cur->childs = xmlStringGetNodeList(cur->doc, |
| 2712 | xmlBufferContent(cur->content)); |
| 2713 | #endif |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2714 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2715 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2716 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2717 | #else |
| 2718 | xmlBufferFree(cur->content); |
| 2719 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2720 | cur->content = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2721 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2722 | } |
| 2723 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2724 | new = xmlNewTextLen(content, len); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2725 | if (new != NULL) { |
| 2726 | xmlAddChild(cur, new); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2727 | if ((last != NULL) && (last->next == new)) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2728 | xmlTextMerge(last, new); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2729 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2730 | } |
| 2731 | break; |
| 2732 | } |
| 2733 | case XML_ATTRIBUTE_NODE: |
| 2734 | break; |
| 2735 | case XML_TEXT_NODE: |
| 2736 | case XML_CDATA_SECTION_NODE: |
| 2737 | case XML_ENTITY_REF_NODE: |
| 2738 | case XML_ENTITY_NODE: |
| 2739 | case XML_PI_NODE: |
| 2740 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2741 | case XML_NOTATION_NODE: |
| 2742 | if (content != NULL) { |
| 2743 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2744 | cur->content = xmlStrncat(cur->content, content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2745 | #else |
| 2746 | xmlBufferAdd(cur->content, content, len); |
| 2747 | #endif |
| 2748 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2749 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 2750 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2751 | case XML_DOCUMENT_TYPE_NODE: |
| 2752 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | /** |
| 2757 | * xmlNodeAddContent: |
| 2758 | * @cur: the node being modified |
| 2759 | * @content: extra content |
| 2760 | * |
| 2761 | * Append the extra substring to the node content. |
| 2762 | */ |
| 2763 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2764 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2765 | int len; |
| 2766 | |
| 2767 | if (cur == NULL) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2768 | fprintf(stderr, "xmlNodeAddContent : node == NULL\n"); |
| 2769 | return; |
| 2770 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2771 | if (content == NULL) return; |
| 2772 | len = xmlStrlen(content); |
| 2773 | xmlNodeAddContentLen(cur, content, len); |
| 2774 | } |
| 2775 | |
| 2776 | /** |
| 2777 | * xmlTextMerge: |
| 2778 | * @first: the first text node |
| 2779 | * @second: the second text node being merged |
| 2780 | * |
| 2781 | * Merge two text nodes into one |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2782 | * Returns the first text node augmented |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2783 | */ |
| 2784 | xmlNodePtr |
| 2785 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 2786 | if (first == NULL) return(second); |
| 2787 | if (second == NULL) return(first); |
| 2788 | if (first->type != XML_TEXT_NODE) return(first); |
| 2789 | if (second->type != XML_TEXT_NODE) return(first); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2790 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2791 | xmlNodeAddContent(first, second->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2792 | #else |
| 2793 | xmlNodeAddContent(first, xmlBufferContent(second->content)); |
| 2794 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2795 | xmlUnlinkNode(second); |
| 2796 | xmlFreeNode(second); |
| 2797 | return(first); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2800 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2801 | * xmlGetNsList: |
| 2802 | * @doc: the document |
| 2803 | * @node: the current node |
| 2804 | * |
| 2805 | * Search all the namespace applying to a given element. |
| 2806 | * Returns an NULL terminated array of all the xmlNsPtr found |
| 2807 | * that need to be freed by the caller or NULL if no |
| 2808 | * namespace if defined |
| 2809 | */ |
| 2810 | xmlNsPtr * |
| 2811 | xmlGetNsList(xmlDocPtr doc, xmlNodePtr node) { |
| 2812 | xmlNsPtr cur; |
| 2813 | xmlNsPtr *ret = NULL; |
| 2814 | int nbns = 0; |
| 2815 | int maxns = 10; |
| 2816 | int i; |
| 2817 | |
| 2818 | while (node != NULL) { |
| 2819 | cur = node->nsDef; |
| 2820 | while (cur != NULL) { |
| 2821 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2822 | ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2823 | if (ret == NULL) { |
| 2824 | fprintf(stderr, "xmlGetNsList : out of memory!\n"); |
| 2825 | return(NULL); |
| 2826 | } |
| 2827 | ret[nbns] = NULL; |
| 2828 | } |
| 2829 | for (i = 0;i < nbns;i++) { |
| 2830 | if ((cur->prefix == ret[i]->prefix) || |
| 2831 | (!xmlStrcmp(cur->prefix, ret[i]->prefix))) break; |
| 2832 | } |
| 2833 | if (i >= nbns) { |
| 2834 | if (nbns >= maxns) { |
| 2835 | maxns *= 2; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2836 | ret = (xmlNsPtr *) xmlRealloc(ret, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2837 | (maxns + 1) * sizeof(xmlNsPtr)); |
| 2838 | if (ret == NULL) { |
| 2839 | fprintf(stderr, "xmlGetNsList : realloc failed!\n"); |
| 2840 | return(NULL); |
| 2841 | } |
| 2842 | } |
| 2843 | ret[nbns++] = cur; |
| 2844 | ret[nbns] = NULL; |
| 2845 | } |
| 2846 | |
| 2847 | cur = cur->next; |
| 2848 | } |
| 2849 | node = node->parent; |
| 2850 | } |
| 2851 | return(ret); |
| 2852 | } |
| 2853 | |
| 2854 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2855 | * xmlSearchNs: |
| 2856 | * @doc: the document |
| 2857 | * @node: the current node |
| 2858 | * @nameSpace: the namespace string |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2859 | * |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2860 | * Search a Ns registered under a given name space for a document. |
| 2861 | * recurse on the parents until it finds the defined namespace |
| 2862 | * or return NULL otherwise. |
| 2863 | * @nameSpace can be NULL, this is a search for the default namespace. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2864 | * Returns the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2865 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2866 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2867 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2868 | xmlNsPtr cur; |
| 2869 | |
Daniel Veillard | 62ba71e | 1999-12-16 17:52:19 +0000 | [diff] [blame] | 2870 | if (node == NULL) return(NULL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2871 | while (node != NULL) { |
| 2872 | cur = node->nsDef; |
| 2873 | while (cur != NULL) { |
| 2874 | if ((cur->prefix == NULL) && (nameSpace == NULL)) |
| 2875 | return(cur); |
| 2876 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 2877 | (!xmlStrcmp(cur->prefix, nameSpace))) |
| 2878 | return(cur); |
| 2879 | cur = cur->next; |
| 2880 | } |
| 2881 | node = node->parent; |
| 2882 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2883 | return(NULL); |
| 2884 | } |
| 2885 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2886 | /** |
| 2887 | * xmlSearchNsByHref: |
| 2888 | * @doc: the document |
| 2889 | * @node: the current node |
| 2890 | * @href: the namespace value |
| 2891 | * |
| 2892 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 2893 | * the defined namespace or return NULL otherwise. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2894 | * Returns the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2895 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2896 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2897 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2898 | xmlNsPtr cur; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 2899 | xmlNodePtr orig = node; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2900 | |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 2901 | if ((node == NULL) || (href == NULL)) return(NULL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2902 | while (node != NULL) { |
| 2903 | cur = node->nsDef; |
| 2904 | while (cur != NULL) { |
| 2905 | if ((cur->href != NULL) && (href != NULL) && |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 2906 | (!xmlStrcmp(cur->href, href))) { |
| 2907 | /* |
| 2908 | * Check that the prefix is not shadowed between orig and node |
| 2909 | */ |
| 2910 | xmlNodePtr check = orig; |
| 2911 | xmlNsPtr tst; |
| 2912 | |
| 2913 | while (check != node) { |
| 2914 | tst = check->nsDef; |
| 2915 | while (tst != NULL) { |
| 2916 | if ((tst->prefix == NULL) && (cur->prefix == NULL)) |
| 2917 | goto shadowed; |
| 2918 | if ((tst->prefix != NULL) && (cur->prefix != NULL) && |
| 2919 | (!xmlStrcmp(tst->prefix, cur->prefix))) |
| 2920 | goto shadowed; |
| 2921 | tst = tst->next; |
| 2922 | } |
| 2923 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2924 | return(cur); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 2925 | } |
| 2926 | shadowed: |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2927 | cur = cur->next; |
| 2928 | } |
| 2929 | node = node->parent; |
| 2930 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 2931 | return(NULL); |
| 2932 | } |
| 2933 | |
| 2934 | /** |
| 2935 | * xmlNewReconciliedNs |
| 2936 | * @doc: the document |
| 2937 | * @tree: a node expected to hold the new namespace |
| 2938 | * @ns: the original namespace |
| 2939 | * |
| 2940 | * This function tries to locate a namespace definition in a tree |
| 2941 | * ancestors, or create a new namespace definition node similar to |
| 2942 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 2943 | * null (default namespace) or reused within the subtree defined by |
| 2944 | * @tree or on one of its ancestors then a new prefix is generated. |
| 2945 | * Returns the (new) namespace definition or NULL in case of error |
| 2946 | */ |
| 2947 | xmlNsPtr |
| 2948 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 2949 | xmlNsPtr def; |
| 2950 | xmlChar prefix[50]; |
| 2951 | int counter = 1; |
| 2952 | |
| 2953 | if (tree == NULL) { |
| 2954 | #ifdef DEBUG_TREE |
| 2955 | fprintf(stderr, "xmlNewReconciliedNs : tree == NULL\n"); |
| 2956 | #endif |
| 2957 | return(NULL); |
| 2958 | } |
| 2959 | if (ns == NULL) { |
| 2960 | #ifdef DEBUG_TREE |
| 2961 | fprintf(stderr, "xmlNewReconciliedNs : ns == NULL\n"); |
| 2962 | #endif |
| 2963 | return(NULL); |
| 2964 | } |
| 2965 | /* |
| 2966 | * Search an existing namespace definition inherited. |
| 2967 | */ |
| 2968 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 2969 | if (def != NULL) |
| 2970 | return(def); |
| 2971 | |
| 2972 | /* |
| 2973 | * Find a close prefix which is not already in use. |
| 2974 | * Let's strip namespace prefixes longer than 20 chars ! |
| 2975 | */ |
| 2976 | sprintf((char *) prefix, "%.20s", ns->prefix); |
| 2977 | def = xmlSearchNs(doc, tree, prefix); |
| 2978 | while (def != NULL) { |
| 2979 | if (counter > 1000) return(NULL); |
| 2980 | sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++); |
| 2981 | def = xmlSearchNs(doc, tree, prefix); |
| 2982 | } |
| 2983 | |
| 2984 | /* |
| 2985 | * Ok, now we are ready to create a new one. |
| 2986 | */ |
| 2987 | def = xmlNewNs(tree, ns->href, prefix); |
| 2988 | return(def); |
| 2989 | } |
| 2990 | |
| 2991 | /** |
| 2992 | * xmlReconciliateNs |
| 2993 | * @doc: the document |
| 2994 | * @tree: a node defining the subtree to reconciliate |
| 2995 | * |
| 2996 | * This function checks that all the namespaces declared within the given |
| 2997 | * tree are properly declared. This is needed for example after Copy or Cut |
| 2998 | * and then paste operations. The subtree may still hold pointers to |
| 2999 | * namespace declarations outside the subtree or invalid/masked. As much |
| 3000 | * as possible the function try tu reuse the existing namespaces found in |
| 3001 | * the new environment. If not possible the new namespaces are redeclared |
| 3002 | * on @tree at the top of the given subtree. |
| 3003 | * Returns the number of namespace declarations created or -1 in case of error. |
| 3004 | */ |
| 3005 | int |
| 3006 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 3007 | xmlNsPtr *oldNs = NULL; |
| 3008 | xmlNsPtr *newNs = NULL; |
| 3009 | int sizeCache = 0; |
| 3010 | int nbCache = 0; |
| 3011 | |
| 3012 | xmlNsPtr n; |
| 3013 | xmlNodePtr node = tree; |
| 3014 | xmlAttrPtr attr; |
| 3015 | int ret = 0, i; |
| 3016 | |
| 3017 | while (node != NULL) { |
| 3018 | /* |
| 3019 | * Reconciliate the node namespace |
| 3020 | */ |
| 3021 | if (node->ns != NULL) { |
| 3022 | /* |
| 3023 | * initialize the cache if needed |
| 3024 | */ |
| 3025 | if (sizeCache == 0) { |
| 3026 | sizeCache = 10; |
| 3027 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3028 | sizeof(xmlNsPtr)); |
| 3029 | if (oldNs == NULL) { |
| 3030 | fprintf(stderr, "xmlReconciliateNs : memory pbm\n"); |
| 3031 | return(-1); |
| 3032 | } |
| 3033 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3034 | sizeof(xmlNsPtr)); |
| 3035 | if (newNs == NULL) { |
| 3036 | fprintf(stderr, "xmlReconciliateNs : memory pbm\n"); |
| 3037 | xmlFree(oldNs); |
| 3038 | return(-1); |
| 3039 | } |
| 3040 | } |
| 3041 | for (i = 0;i < nbCache;i++) { |
| 3042 | if (oldNs[i] == node->ns) { |
| 3043 | node->ns = newNs[i]; |
| 3044 | break; |
| 3045 | } |
| 3046 | } |
| 3047 | if (i == nbCache) { |
| 3048 | /* |
| 3049 | * Ok we need to recreate a new namespace definition |
| 3050 | */ |
| 3051 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 3052 | if (n != NULL) { /* :-( what if else ??? */ |
| 3053 | /* |
| 3054 | * check if we need to grow the cache buffers. |
| 3055 | */ |
| 3056 | if (sizeCache <= nbCache) { |
| 3057 | sizeCache *= 2; |
| 3058 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 3059 | sizeof(xmlNsPtr)); |
| 3060 | if (oldNs == NULL) { |
| 3061 | fprintf(stderr, "xmlReconciliateNs : memory pbm\n"); |
| 3062 | xmlFree(newNs); |
| 3063 | return(-1); |
| 3064 | } |
| 3065 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 3066 | sizeof(xmlNsPtr)); |
| 3067 | if (newNs == NULL) { |
| 3068 | fprintf(stderr, "xmlReconciliateNs : memory pbm\n"); |
| 3069 | xmlFree(oldNs); |
| 3070 | return(-1); |
| 3071 | } |
| 3072 | } |
| 3073 | newNs[nbCache] = n; |
| 3074 | oldNs[nbCache++] = node->ns; |
| 3075 | node->ns = n; |
| 3076 | } |
| 3077 | } |
| 3078 | } |
| 3079 | /* |
| 3080 | * now check for namespace hold by attributes on the node. |
| 3081 | */ |
| 3082 | attr = node->properties; |
| 3083 | while (attr != NULL) { |
| 3084 | if (attr->ns != NULL) { |
| 3085 | /* |
| 3086 | * initialize the cache if needed |
| 3087 | */ |
| 3088 | if (sizeCache == 0) { |
| 3089 | sizeCache = 10; |
| 3090 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3091 | sizeof(xmlNsPtr)); |
| 3092 | if (oldNs == NULL) { |
| 3093 | fprintf(stderr, "xmlReconciliateNs : memory pbm\n"); |
| 3094 | return(-1); |
| 3095 | } |
| 3096 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3097 | sizeof(xmlNsPtr)); |
| 3098 | if (newNs == NULL) { |
| 3099 | fprintf(stderr, "xmlReconciliateNs : memory pbm\n"); |
| 3100 | xmlFree(oldNs); |
| 3101 | return(-1); |
| 3102 | } |
| 3103 | } |
| 3104 | for (i = 0;i < nbCache;i++) { |
| 3105 | if (oldNs[i] == attr->ns) { |
| 3106 | node->ns = newNs[i]; |
| 3107 | break; |
| 3108 | } |
| 3109 | } |
| 3110 | if (i == nbCache) { |
| 3111 | /* |
| 3112 | * Ok we need to recreate a new namespace definition |
| 3113 | */ |
| 3114 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 3115 | if (n != NULL) { /* :-( what if else ??? */ |
| 3116 | /* |
| 3117 | * check if we need to grow the cache buffers. |
| 3118 | */ |
| 3119 | if (sizeCache <= nbCache) { |
| 3120 | sizeCache *= 2; |
| 3121 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 3122 | sizeof(xmlNsPtr)); |
| 3123 | if (oldNs == NULL) { |
| 3124 | fprintf(stderr, |
| 3125 | "xmlReconciliateNs : memory pbm\n"); |
| 3126 | xmlFree(newNs); |
| 3127 | return(-1); |
| 3128 | } |
| 3129 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 3130 | sizeof(xmlNsPtr)); |
| 3131 | if (newNs == NULL) { |
| 3132 | fprintf(stderr, |
| 3133 | "xmlReconciliateNs : memory pbm\n"); |
| 3134 | xmlFree(oldNs); |
| 3135 | return(-1); |
| 3136 | } |
| 3137 | } |
| 3138 | newNs[nbCache] = n; |
| 3139 | oldNs[nbCache++] = attr->ns; |
| 3140 | attr->ns = n; |
| 3141 | } |
| 3142 | } |
| 3143 | } |
| 3144 | attr = attr->next; |
| 3145 | } |
| 3146 | |
| 3147 | /* |
| 3148 | * Browse the full subtree, deep first |
| 3149 | */ |
| 3150 | if (node->childs != NULL) { |
| 3151 | /* deep first */ |
| 3152 | node = node->childs; |
| 3153 | } else if ((node != tree) && (node->next != NULL)) { |
| 3154 | /* then siblings */ |
| 3155 | node = node->next; |
| 3156 | } else if (node != tree) { |
| 3157 | /* go up to parents->next if needed */ |
| 3158 | while (node != tree) { |
| 3159 | if (node->parent != NULL) |
| 3160 | node = node->parent; |
| 3161 | if ((node != tree) && (node->next != NULL)) { |
| 3162 | node = node->next; |
| 3163 | break; |
| 3164 | } |
| 3165 | if (node->parent == NULL) { |
| 3166 | node = NULL; |
| 3167 | break; |
| 3168 | } |
| 3169 | } |
| 3170 | /* exit condition */ |
| 3171 | if (node == tree) |
| 3172 | node = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3173 | } |
| 3174 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3175 | return(ret); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3178 | /** |
| 3179 | * xmlGetProp: |
| 3180 | * @node: the node |
| 3181 | * @name: the attribute name |
| 3182 | * |
| 3183 | * Search and get the value of an attribute associated to a node |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3184 | * This does the entity substitution. |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3185 | * This function looks in DTD attribute declaration for #FIXED or |
| 3186 | * default declaration values unless DTD use has been turned off. |
| 3187 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3188 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 3189 | * It's up to the caller to free the memory. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3190 | */ |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 3191 | xmlChar * |
| 3192 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3193 | xmlAttrPtr prop; |
| 3194 | xmlDocPtr doc; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3195 | |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3196 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 3197 | /* |
| 3198 | * Check on the properties attached to the node |
| 3199 | */ |
| 3200 | prop = node->properties; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3201 | while (prop != NULL) { |
Daniel Veillard | 6817893 | 1999-02-08 18:34:36 +0000 | [diff] [blame] | 3202 | if (!xmlStrcmp(prop->name, name)) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3203 | xmlChar *ret; |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame] | 3204 | |
| 3205 | ret = xmlNodeListGetString(node->doc, prop->val, 1); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3206 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame] | 3207 | return(ret); |
Daniel Veillard | 6817893 | 1999-02-08 18:34:36 +0000 | [diff] [blame] | 3208 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3209 | prop = prop->next; |
| 3210 | } |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3211 | if (!xmlCheckDTD) return(NULL); |
| 3212 | |
| 3213 | /* |
| 3214 | * Check if there is a default declaration in the internal |
| 3215 | * or external subsets |
| 3216 | */ |
| 3217 | doc = node->doc; |
| 3218 | if (doc != NULL) { |
| 3219 | xmlAttributePtr attrDecl; |
| 3220 | if (doc->intSubset != NULL) { |
| 3221 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 3222 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3223 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 3224 | return(xmlStrdup(attrDecl->defaultValue)); |
| 3225 | } |
| 3226 | } |
| 3227 | return(NULL); |
| 3228 | } |
| 3229 | |
| 3230 | /** |
| 3231 | * xmlGetNsProp: |
| 3232 | * @node: the node |
| 3233 | * @name: the attribute name |
| 3234 | * @namespace: the URI of the namespace |
| 3235 | * |
| 3236 | * Search and get the value of an attribute associated to a node |
| 3237 | * This attribute has to be anchored in the namespace specified. |
| 3238 | * This does the entity substitution. |
| 3239 | * This function looks in DTD attribute declaration for #FIXED or |
| 3240 | * default declaration values unless DTD use has been turned off. |
| 3241 | * |
| 3242 | * Returns the attribute value or NULL if not found. |
| 3243 | * It's up to the caller to free the memory. |
| 3244 | */ |
| 3245 | xmlChar * |
| 3246 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) { |
| 3247 | xmlAttrPtr prop = node->properties; |
| 3248 | xmlDocPtr doc; |
| 3249 | xmlNsPtr ns; |
| 3250 | |
| 3251 | if (namespace == NULL) |
| 3252 | return(xmlGetProp(node, name)); |
| 3253 | while (prop != NULL) { |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 3254 | /* |
| 3255 | * One need to have |
| 3256 | * - same attribute names |
| 3257 | * - and the attribute carrying that namespace |
| 3258 | * or |
| 3259 | * no namespace on the attribute and the element carrying it |
| 3260 | */ |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3261 | if ((!xmlStrcmp(prop->name, name)) && |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 3262 | (((prop->ns == NULL) && (node->ns != NULL) && |
| 3263 | (!xmlStrcmp(node->ns->href, namespace))) || |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 3264 | ((prop->ns != NULL) && (!xmlStrcmp(prop->ns->href, namespace))))) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3265 | xmlChar *ret; |
| 3266 | |
| 3267 | ret = xmlNodeListGetString(node->doc, prop->val, 1); |
| 3268 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 3269 | return(ret); |
| 3270 | } |
| 3271 | prop = prop->next; |
| 3272 | } |
| 3273 | if (!xmlCheckDTD) return(NULL); |
| 3274 | |
| 3275 | /* |
| 3276 | * Check if there is a default declaration in the internal |
| 3277 | * or external subsets |
| 3278 | */ |
| 3279 | doc = node->doc; |
| 3280 | if (doc != NULL) { |
| 3281 | xmlAttributePtr attrDecl; |
| 3282 | if (doc->intSubset != NULL) { |
| 3283 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 3284 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3285 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 3286 | |
| 3287 | if (attrDecl->prefix != NULL) { |
| 3288 | /* |
| 3289 | * The DTD declaration only allows a prefix search |
| 3290 | */ |
| 3291 | ns = xmlSearchNs(doc, node, attrDecl->prefix); |
| 3292 | if ((ns != NULL) && (!xmlStrcmp(ns->href, namespace))) |
| 3293 | return(xmlStrdup(attrDecl->defaultValue)); |
| 3294 | } |
| 3295 | } |
| 3296 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3297 | return(NULL); |
| 3298 | } |
| 3299 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3300 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3301 | * xmlSetProp: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3302 | * @node: the node |
| 3303 | * @name: the attribute name |
| 3304 | * @value: the attribute value |
| 3305 | * |
| 3306 | * Set (or reset) an attribute carried by a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3307 | * Returns the attribute pointer. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3308 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3309 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3310 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3311 | xmlAttrPtr prop = node->properties; |
| 3312 | |
| 3313 | while (prop != NULL) { |
| 3314 | if (!xmlStrcmp(prop->name, name)) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3315 | if (prop->val != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3316 | xmlFreeNodeList(prop->val); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3317 | prop->val = NULL; |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 3318 | if (value != NULL) { |
| 3319 | xmlChar *buffer; |
| 3320 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 3321 | prop->val = xmlStringGetNodeList(node->doc, buffer); |
| 3322 | xmlFree(buffer); |
| 3323 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3324 | return(prop); |
| 3325 | } |
| 3326 | prop = prop->next; |
| 3327 | } |
| 3328 | prop = xmlNewProp(node, name, value); |
| 3329 | return(prop); |
| 3330 | } |
| 3331 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3332 | /** |
| 3333 | * xmlNodeIsText: |
| 3334 | * @node: the node |
| 3335 | * |
| 3336 | * Is this node a Text node ? |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3337 | * Returns 1 yes, 0 no |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3338 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3339 | int |
| 3340 | xmlNodeIsText(xmlNodePtr node) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3341 | if (node == NULL) return(0); |
| 3342 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 3343 | if (node->type == XML_TEXT_NODE) return(1); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3344 | return(0); |
| 3345 | } |
| 3346 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3347 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3348 | * xmlTextConcat: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3349 | * @node: the node |
| 3350 | * @content: the content |
| 3351 | * @len: @content lenght |
| 3352 | * |
| 3353 | * Concat the given string at the end of the existing node content |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3354 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3355 | |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3356 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3357 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3358 | if (node == NULL) return; |
| 3359 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3360 | if ((node->type != XML_TEXT_NODE) && |
| 3361 | (node->type != XML_CDATA_SECTION_NODE)) { |
| 3362 | fprintf(stderr, "xmlTextConcat: node is not text nor cdata\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3363 | return; |
| 3364 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3365 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3366 | node->content = xmlStrncat(node->content, content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3367 | #else |
| 3368 | xmlBufferAdd(node->content, content, len); |
| 3369 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | /************************************************************************ |
| 3373 | * * |
| 3374 | * Output : to a FILE or in memory * |
| 3375 | * * |
| 3376 | ************************************************************************/ |
| 3377 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3378 | #define BASE_BUFFER_SIZE 4000 |
| 3379 | |
| 3380 | /** |
| 3381 | * xmlBufferCreate: |
| 3382 | * |
| 3383 | * routine to create an XML buffer. |
| 3384 | * returns the new structure. |
| 3385 | */ |
| 3386 | xmlBufferPtr |
| 3387 | xmlBufferCreate(void) { |
| 3388 | xmlBufferPtr ret; |
| 3389 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3390 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3391 | if (ret == NULL) { |
| 3392 | fprintf(stderr, "xmlBufferCreate : out of memory!\n"); |
| 3393 | return(NULL); |
| 3394 | } |
| 3395 | ret->use = 0; |
| 3396 | ret->size = BASE_BUFFER_SIZE; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3397 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3398 | ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3399 | if (ret->content == NULL) { |
| 3400 | fprintf(stderr, "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3401 | xmlFree(ret); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3402 | return(NULL); |
| 3403 | } |
| 3404 | ret->content[0] = 0; |
| 3405 | return(ret); |
| 3406 | } |
| 3407 | |
| 3408 | /** |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3409 | * xmlBufferCreateSize: |
| 3410 | * @size: initial size of buffer |
| 3411 | * |
| 3412 | * routine to create an XML buffer. |
| 3413 | * returns the new structure. |
| 3414 | */ |
| 3415 | xmlBufferPtr |
| 3416 | xmlBufferCreateSize(size_t size) { |
| 3417 | xmlBufferPtr ret; |
| 3418 | |
| 3419 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 3420 | if (ret == NULL) { |
| 3421 | fprintf(stderr, "xmlBufferCreate : out of memory!\n"); |
| 3422 | return(NULL); |
| 3423 | } |
| 3424 | ret->use = 0; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3425 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3426 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3427 | if (ret->size){ |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3428 | ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar)); |
| 3429 | if (ret->content == NULL) { |
| 3430 | fprintf(stderr, "xmlBufferCreate : out of memory!\n"); |
| 3431 | xmlFree(ret); |
| 3432 | return(NULL); |
| 3433 | } |
| 3434 | ret->content[0] = 0; |
| 3435 | } else |
| 3436 | ret->content = NULL; |
| 3437 | return(ret); |
| 3438 | } |
| 3439 | |
| 3440 | /** |
| 3441 | * xmlBufferAllocationScheme: |
| 3442 | * @buf: the buffer to free |
| 3443 | * @scheme: allocation scheme to use |
| 3444 | * |
| 3445 | * Sets the allocation scheme for this buffer |
| 3446 | */ |
| 3447 | void |
| 3448 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
| 3449 | xmlBufferAllocationScheme scheme) { |
| 3450 | if (buf == NULL) { |
| 3451 | fprintf(stderr, "xmlBufferSetAllocationScheme: buf == NULL\n"); |
| 3452 | return; |
| 3453 | } |
| 3454 | |
| 3455 | buf->alloc = scheme; |
| 3456 | } |
| 3457 | |
| 3458 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3459 | * xmlBufferFree: |
| 3460 | * @buf: the buffer to free |
| 3461 | * |
| 3462 | * Frees an XML buffer. |
| 3463 | */ |
| 3464 | void |
| 3465 | xmlBufferFree(xmlBufferPtr buf) { |
| 3466 | if (buf == NULL) { |
| 3467 | fprintf(stderr, "xmlBufferFree: buf == NULL\n"); |
| 3468 | return; |
| 3469 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3470 | if (buf->content != NULL) { |
| 3471 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3472 | memset(buf->content, -1, BASE_BUFFER_SIZE); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3473 | #else |
| 3474 | memset(buf->content, -1, buf->size); |
| 3475 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3476 | xmlFree(buf->content); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3477 | } |
| 3478 | memset(buf, -1, sizeof(xmlBuffer)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3479 | xmlFree(buf); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3480 | } |
| 3481 | |
| 3482 | /** |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3483 | * xmlBufferEmpty: |
| 3484 | * @buf: the buffer |
| 3485 | * |
| 3486 | * empty a buffer. |
| 3487 | */ |
| 3488 | void |
| 3489 | xmlBufferEmpty(xmlBufferPtr buf) { |
| 3490 | buf->use = 0; |
| 3491 | memset(buf->content, -1, buf->size);/* just for debug */ |
| 3492 | } |
| 3493 | |
| 3494 | /** |
| 3495 | * xmlBufferShrink: |
| 3496 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3497 | * @len: the number of xmlChar to remove |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3498 | * |
| 3499 | * Remove the beginning of an XML buffer. |
| 3500 | * |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3501 | * Returns the number of xmlChar removed, or -1 in case of failure. |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3502 | */ |
| 3503 | int |
| 3504 | xmlBufferShrink(xmlBufferPtr buf, int len) { |
| 3505 | if (len == 0) return(0); |
| 3506 | if (len > buf->use) return(-1); |
| 3507 | |
| 3508 | buf->use -= len; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3509 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3510 | |
| 3511 | buf->content[buf->use] = 0; |
| 3512 | return(len); |
| 3513 | } |
| 3514 | |
| 3515 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3516 | * xmlBufferDump: |
| 3517 | * @file: the file output |
| 3518 | * @buf: the buffer to dump |
| 3519 | * |
| 3520 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3521 | * Returns the number of xmlChar written |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3522 | */ |
| 3523 | int |
| 3524 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 3525 | int ret; |
| 3526 | |
| 3527 | if (buf == NULL) { |
| 3528 | fprintf(stderr, "xmlBufferDump: buf == NULL\n"); |
| 3529 | return(0); |
| 3530 | } |
| 3531 | if (buf->content == NULL) { |
| 3532 | fprintf(stderr, "xmlBufferDump: buf->content == NULL\n"); |
| 3533 | return(0); |
| 3534 | } |
| 3535 | if (file == NULL) file = stdout; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3536 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3537 | return(ret); |
| 3538 | } |
| 3539 | |
| 3540 | /** |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3541 | * xmlBufferContent: |
| 3542 | * @buf: the buffer to resize |
| 3543 | * |
| 3544 | * Returns the internal content |
| 3545 | */ |
| 3546 | |
| 3547 | const xmlChar* |
| 3548 | xmlBufferContent(const xmlBufferPtr buf) |
| 3549 | { |
| 3550 | if(!buf) |
| 3551 | return NULL; |
| 3552 | |
| 3553 | return buf->content; |
| 3554 | } |
| 3555 | |
| 3556 | /** |
| 3557 | * xmlBufferLength: |
| 3558 | * @buf: the buffer |
| 3559 | * |
| 3560 | * Returns the length of data in the internal content |
| 3561 | */ |
| 3562 | |
| 3563 | int |
| 3564 | xmlBufferLength(const xmlBufferPtr buf) |
| 3565 | { |
| 3566 | if(!buf) |
| 3567 | return 0; |
| 3568 | |
| 3569 | return buf->use; |
| 3570 | } |
| 3571 | |
| 3572 | /** |
| 3573 | * xmlBufferResize: |
| 3574 | * @buf: the buffer to resize |
| 3575 | * @len: the desired size |
| 3576 | * |
| 3577 | * Resize a buffer to accomodate minimum size of <len>. |
| 3578 | * |
| 3579 | * Returns 0 in case of problems, 1 otherwise |
| 3580 | */ |
| 3581 | int |
| 3582 | xmlBufferResize(xmlBufferPtr buf, int size) |
| 3583 | { |
| 3584 | int newSize = (buf->size ? buf->size*2 : size);/*take care of empty case*/ |
| 3585 | xmlChar* rebuf = NULL; |
| 3586 | |
| 3587 | /* Don't resize if we don't have to */ |
| 3588 | if(size < buf->size) |
| 3589 | return 1; |
| 3590 | |
| 3591 | /* figure out new size */ |
| 3592 | switch(buf->alloc){ |
| 3593 | case XML_BUFFER_ALLOC_DOUBLEIT: |
| 3594 | while(size > newSize) newSize *= 2; |
| 3595 | break; |
| 3596 | case XML_BUFFER_ALLOC_EXACT: |
| 3597 | newSize = size+10; |
| 3598 | break; |
| 3599 | default: |
| 3600 | newSize = size+10; |
| 3601 | break; |
| 3602 | } |
| 3603 | |
| 3604 | if (buf->content == NULL) |
| 3605 | rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar)); |
| 3606 | else |
| 3607 | rebuf = (xmlChar *) xmlRealloc(buf->content, |
| 3608 | newSize * sizeof(xmlChar)); |
| 3609 | if (rebuf == NULL) { |
| 3610 | fprintf(stderr, "xmlBufferAdd : out of memory!\n"); |
| 3611 | return 0; |
| 3612 | } |
| 3613 | buf->content = rebuf; |
| 3614 | buf->size = newSize; |
| 3615 | |
| 3616 | return 1; |
| 3617 | } |
| 3618 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3619 | * xmlBufferAdd: |
| 3620 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3621 | * @str: the xmlChar string |
| 3622 | * @len: the number of xmlChar to add |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3623 | * |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3624 | * Add a string range to an XML buffer. if len == -1, the lenght of |
| 3625 | * str is recomputed. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3626 | */ |
| 3627 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3628 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3629 | int l, needSize; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3630 | |
| 3631 | if (str == NULL) { |
| 3632 | fprintf(stderr, "xmlBufferAdd: str == NULL\n"); |
| 3633 | return; |
| 3634 | } |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3635 | if (len < -1) { |
| 3636 | fprintf(stderr, "xmlBufferAdd: len < 0\n"); |
| 3637 | return; |
| 3638 | } |
| 3639 | if (len == 0) return; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3640 | |
| 3641 | /* CJN What's this for??? */ |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3642 | if (len < 0) |
| 3643 | l = xmlStrlen(str); |
| 3644 | else |
| 3645 | for (l = 0;l < len;l++) |
| 3646 | if (str[l] == 0) break; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3647 | if (l < len){ len = l; printf("xmlBufferAdd bad length\n"); } |
| 3648 | |
| 3649 | /* CJN 11.18.99 okay, now I'm using the length */ |
| 3650 | if(len == -1) len = l; |
| 3651 | |
| 3652 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3653 | if (len <= 0) return; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3654 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3655 | needSize = buf->use + len + 2; |
| 3656 | if(needSize > buf->size){ |
| 3657 | if(!xmlBufferResize(buf, needSize)){ |
| 3658 | fprintf(stderr, "xmlBufferAdd : out of memory!\n"); |
| 3659 | return; |
| 3660 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3661 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3662 | |
| 3663 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3664 | buf->use += len; |
| 3665 | buf->content[buf->use] = 0; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
| 3668 | /** |
| 3669 | * xmlBufferCat: |
| 3670 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3671 | * @str: the xmlChar string |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3672 | * |
| 3673 | * Append a zero terminated string to an XML buffer. |
| 3674 | */ |
| 3675 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3676 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3677 | if (str != NULL) |
| 3678 | xmlBufferAdd(buf, str, -1); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | /** |
| 3682 | * xmlBufferCCat: |
| 3683 | * @buf: the buffer to dump |
| 3684 | * @str: the C char string |
| 3685 | * |
| 3686 | * Append a zero terminated C string to an XML buffer. |
| 3687 | */ |
| 3688 | void |
| 3689 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 3690 | const char *cur; |
| 3691 | |
| 3692 | if (str == NULL) { |
| 3693 | fprintf(stderr, "xmlBufferAdd: str == NULL\n"); |
| 3694 | return; |
| 3695 | } |
| 3696 | for (cur = str;*cur != 0;cur++) { |
| 3697 | if (buf->use + 10 >= buf->size) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3698 | if(!xmlBufferResize(buf, buf->use+10)){ |
| 3699 | fprintf(stderr, "xmlBufferCCat : out of memory!\n"); |
| 3700 | return; |
| 3701 | } |
| 3702 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3703 | buf->content[buf->use++] = *cur; |
| 3704 | } |
| 3705 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3706 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3707 | /** |
| 3708 | * xmlBufferWriteCHAR: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3709 | * @buf: the XML buffer |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3710 | * @string: the string to add |
| 3711 | * |
| 3712 | * routine which manage and grows an output buffer. This one add |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3713 | * xmlChars at the end of the buffer. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3714 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3715 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3716 | xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3717 | xmlBufferCat(buf, string); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3720 | /** |
| 3721 | * xmlBufferWriteChar: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3722 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3723 | * @string: the string to add |
| 3724 | * |
| 3725 | * routine which manage and grows an output buffer. This one add |
| 3726 | * C chars at the end of the array. |
| 3727 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3728 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3729 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
| 3730 | xmlBufferCCat(buf, string); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3733 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3734 | /** |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3735 | * xmlBufferWriteQuotedString: |
| 3736 | * @buf: the XML buffer output |
| 3737 | * @string: the string to add |
| 3738 | * |
| 3739 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3740 | * a quoted or double quoted xmlChar string, checking first if it holds |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3741 | * quote or double-quotes internally |
| 3742 | */ |
| 3743 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3744 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3745 | if (xmlStrchr(string, '"')) { |
| 3746 | if (xmlStrchr(string, '\'')) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3747 | fprintf(stderr, |
| 3748 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 3749 | } |
| 3750 | xmlBufferCCat(buf, "'"); |
| 3751 | xmlBufferCat(buf, string); |
| 3752 | xmlBufferCCat(buf, "'"); |
| 3753 | } else { |
| 3754 | xmlBufferCCat(buf, "\""); |
| 3755 | xmlBufferCat(buf, string); |
| 3756 | xmlBufferCCat(buf, "\""); |
| 3757 | } |
| 3758 | } |
| 3759 | |
| 3760 | |
| 3761 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3762 | * xmlGlobalNsDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3763 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3764 | * @cur: a namespace |
| 3765 | * |
| 3766 | * Dump a global Namespace, this is the old version based on PIs. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3767 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3768 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3769 | xmlGlobalNsDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3770 | if (cur == NULL) { |
| 3771 | fprintf(stderr, "xmlGlobalNsDump : Ns == NULL\n"); |
| 3772 | return; |
| 3773 | } |
| 3774 | if (cur->type == XML_GLOBAL_NAMESPACE) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3775 | xmlBufferWriteChar(buf, "<?namespace"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3776 | if (cur->href != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3777 | xmlBufferWriteChar(buf, " href="); |
| 3778 | xmlBufferWriteQuotedString(buf, cur->href); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3779 | } |
| 3780 | if (cur->prefix != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3781 | xmlBufferWriteChar(buf, " AS="); |
| 3782 | xmlBufferWriteQuotedString(buf, cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3783 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3784 | xmlBufferWriteChar(buf, "?>\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3785 | } |
| 3786 | } |
| 3787 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3788 | /** |
| 3789 | * xmlGlobalNsListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3790 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3791 | * @cur: the first namespace |
| 3792 | * |
| 3793 | * 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] | 3794 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3795 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3796 | xmlGlobalNsListDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3797 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3798 | xmlGlobalNsDump(buf, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3799 | cur = cur->next; |
| 3800 | } |
| 3801 | } |
| 3802 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3803 | /** |
| 3804 | * xmlNsDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3805 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3806 | * @cur: a namespace |
| 3807 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3808 | * Dump a local Namespace definition. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3809 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3810 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3811 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3812 | xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3813 | if (cur == NULL) { |
| 3814 | fprintf(stderr, "xmlNsDump : Ns == NULL\n"); |
| 3815 | return; |
| 3816 | } |
| 3817 | if (cur->type == XML_LOCAL_NAMESPACE) { |
| 3818 | /* Within the context of an element attributes */ |
| 3819 | if (cur->prefix != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3820 | xmlBufferWriteChar(buf, " xmlns:"); |
| 3821 | xmlBufferWriteCHAR(buf, cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3822 | } else |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3823 | xmlBufferWriteChar(buf, " xmlns"); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3824 | xmlBufferWriteChar(buf, "="); |
| 3825 | xmlBufferWriteQuotedString(buf, cur->href); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3826 | } |
| 3827 | } |
| 3828 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3829 | /** |
| 3830 | * xmlNsListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3831 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3832 | * @cur: the first namespace |
| 3833 | * |
| 3834 | * Dump a list of local Namespace definitions. |
| 3835 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3836 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3837 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3838 | xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3839 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3840 | xmlNsDump(buf, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3841 | cur = cur->next; |
| 3842 | } |
| 3843 | } |
| 3844 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3845 | /** |
| 3846 | * xmlDtdDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3847 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3848 | * @doc: the document |
| 3849 | * |
| 3850 | * Dump the XML document DTD, if any. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3851 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3852 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3853 | xmlDtdDump(xmlBufferPtr buf, xmlDocPtr doc) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3854 | xmlDtdPtr cur = doc->intSubset; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3855 | |
| 3856 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3857 | fprintf(stderr, "xmlDtdDump : no internal subset\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3858 | return; |
| 3859 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3860 | xmlBufferWriteChar(buf, "<!DOCTYPE "); |
| 3861 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3862 | if (cur->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3863 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 3864 | xmlBufferWriteQuotedString(buf, cur->ExternalID); |
| 3865 | xmlBufferWriteChar(buf, " "); |
| 3866 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3867 | } else if (cur->SystemID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3868 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 3869 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3870 | } |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3871 | if ((cur->entities == NULL) && (cur->elements == NULL) && |
| 3872 | (cur->attributes == NULL) && (cur->notations == NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3873 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3874 | return; |
| 3875 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3876 | xmlBufferWriteChar(buf, " [\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3877 | if (cur->entities != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3878 | xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) cur->entities); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3879 | if (cur->notations != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3880 | xmlDumpNotationTable(buf, (xmlNotationTablePtr) cur->notations); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 3881 | if (cur->elements != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3882 | xmlDumpElementTable(buf, (xmlElementTablePtr) cur->elements); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3883 | if (cur->attributes != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3884 | xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) cur->attributes); |
| 3885 | xmlBufferWriteChar(buf, "]"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3886 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3887 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3890 | /** |
| 3891 | * xmlAttrDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3892 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3893 | * @doc: the document |
| 3894 | * @cur: the attribute pointer |
| 3895 | * |
| 3896 | * Dump an XML attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3897 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3898 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3899 | xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3900 | xmlChar *value; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3901 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3902 | if (cur == NULL) { |
| 3903 | fprintf(stderr, "xmlAttrDump : property == NULL\n"); |
| 3904 | return; |
| 3905 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3906 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3907 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 3908 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 3909 | xmlBufferWriteChar(buf, ":"); |
| 3910 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3911 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3912 | value = xmlNodeListGetString(doc, cur->val, 0); |
| 3913 | if (value) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3914 | xmlBufferWriteChar(buf, "="); |
| 3915 | xmlBufferWriteQuotedString(buf, value); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3916 | xmlFree(value); |
Daniel Veillard | 726c7e3 | 1999-02-08 15:13:10 +0000 | [diff] [blame] | 3917 | } else { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3918 | xmlBufferWriteChar(buf, "=\"\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3919 | } |
| 3920 | } |
| 3921 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3922 | /** |
| 3923 | * xmlAttrListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3924 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3925 | * @doc: the document |
| 3926 | * @cur: the first attribute pointer |
| 3927 | * |
| 3928 | * Dump a list of XML attributes |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3929 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3930 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3931 | xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3932 | if (cur == NULL) { |
| 3933 | fprintf(stderr, "xmlAttrListDump : property == NULL\n"); |
| 3934 | return; |
| 3935 | } |
| 3936 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3937 | xmlAttrDump(buf, doc, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3938 | cur = cur->next; |
| 3939 | } |
| 3940 | } |
| 3941 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3942 | |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3943 | static void |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 3944 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 3945 | int format); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3946 | void |
| 3947 | htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur); |
| 3948 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3949 | /** |
| 3950 | * xmlNodeListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3951 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3952 | * @doc: the document |
| 3953 | * @cur: the first node |
| 3954 | * @level: the imbrication level for indenting |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 3955 | * @format: is formatting allowed |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3956 | * |
| 3957 | * Dump an XML node list, recursive behaviour,children are printed too. |
| 3958 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3959 | static void |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 3960 | xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 3961 | int format) { |
| 3962 | int i; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3963 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3964 | if (cur == NULL) { |
| 3965 | fprintf(stderr, "xmlNodeListDump : node == NULL\n"); |
| 3966 | return; |
| 3967 | } |
| 3968 | while (cur != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 3969 | if ((format) && (xmlIndentTreeOutput) && |
| 3970 | (cur->type == XML_ELEMENT_NODE)) |
| 3971 | for (i = 0;i < level;i++) |
| 3972 | xmlBufferWriteChar(buf, " "); |
| 3973 | xmlNodeDump(buf, doc, cur, level, format); |
| 3974 | if (format) { |
| 3975 | xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3976 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3977 | cur = cur->next; |
| 3978 | } |
| 3979 | } |
| 3980 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3981 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3982 | * xmlNodeDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 3983 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3984 | * @doc: the document |
| 3985 | * @cur: the current node |
| 3986 | * @level: the imbrication level for indenting |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 3987 | * @format: is formatting allowed |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3988 | * |
| 3989 | * Dump an XML node, recursive behaviour,children are printed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3990 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3991 | static void |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 3992 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 3993 | int format) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3994 | int i; |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 3995 | xmlNodePtr tmp; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3996 | |
| 3997 | if (cur == NULL) { |
| 3998 | fprintf(stderr, "xmlNodeDump : node == NULL\n"); |
| 3999 | return; |
| 4000 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 4001 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4002 | if (cur->content != NULL) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4003 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4004 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4005 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4006 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4007 | #else |
| 4008 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 4009 | xmlBufferContent(cur->content)); |
| 4010 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4011 | if (buffer != NULL) { |
| 4012 | xmlBufferWriteCHAR(buf, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4013 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4014 | } |
| 4015 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4016 | return; |
| 4017 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4018 | if (cur->type == XML_PI_NODE) { |
| 4019 | if (cur->content != NULL) { |
| 4020 | xmlBufferWriteChar(buf, "<?"); |
| 4021 | xmlBufferWriteCHAR(buf, cur->name); |
| 4022 | if (cur->content != NULL) { |
| 4023 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4024 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4025 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4026 | #else |
| 4027 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 4028 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4029 | } |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4030 | xmlBufferWriteChar(buf, "?>"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4031 | } |
| 4032 | return; |
| 4033 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 4034 | if (cur->type == XML_COMMENT_NODE) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4035 | if (cur->content != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4036 | xmlBufferWriteChar(buf, "<!--"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4037 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4038 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4039 | #else |
| 4040 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 4041 | #endif |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4042 | xmlBufferWriteChar(buf, "-->"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4043 | } |
| 4044 | return; |
| 4045 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4046 | if (cur->type == XML_ENTITY_REF_NODE) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4047 | xmlBufferWriteChar(buf, "&"); |
| 4048 | xmlBufferWriteCHAR(buf, cur->name); |
| 4049 | xmlBufferWriteChar(buf, ";"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4050 | return; |
| 4051 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 4052 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 4053 | xmlBufferWriteChar(buf, "<![CDATA["); |
| 4054 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4055 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 4056 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4057 | #else |
| 4058 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 4059 | #endif |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 4060 | xmlBufferWriteChar(buf, "]]>"); |
| 4061 | return; |
| 4062 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4063 | |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4064 | if (format == 1) { |
| 4065 | tmp = cur->childs; |
| 4066 | while (tmp != NULL) { |
| 4067 | if ((tmp->type == XML_TEXT_NODE) || |
| 4068 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 4069 | format = 0; |
| 4070 | break; |
| 4071 | } |
| 4072 | tmp = tmp->next; |
| 4073 | } |
| 4074 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4075 | xmlBufferWriteChar(buf, "<"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4076 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4077 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 4078 | xmlBufferWriteChar(buf, ":"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4081 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4082 | if (cur->nsDef) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4083 | xmlNsListDump(buf, cur->nsDef); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4084 | if (cur->properties != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4085 | xmlAttrListDump(buf, doc, cur->properties); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4086 | |
| 4087 | if ((cur->content == NULL) && (cur->childs == NULL)) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4088 | xmlBufferWriteChar(buf, "/>"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4089 | return; |
| 4090 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4091 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4092 | if (cur->content != NULL) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4093 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4094 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4095 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4096 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4097 | #else |
| 4098 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 4099 | xmlBufferContent(cur->content)); |
| 4100 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4101 | if (buffer != NULL) { |
| 4102 | xmlBufferWriteCHAR(buf, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4103 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4104 | } |
| 4105 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4106 | if (cur->childs != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4107 | if (format) xmlBufferWriteChar(buf, "\n"); |
| 4108 | xmlNodeListDump(buf, doc, cur->childs, level + 1, format); |
| 4109 | if ((xmlIndentTreeOutput) && (format)) |
| 4110 | for (i = 0;i < level;i++) |
| 4111 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4112 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4113 | xmlBufferWriteChar(buf, "</"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4114 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4115 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 4116 | xmlBufferWriteChar(buf, ":"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4119 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4120 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4121 | } |
| 4122 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4123 | /** |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4124 | * xmlElemDump: |
| 4125 | * @buf: the XML buffer output |
| 4126 | * @doc: the document |
| 4127 | * @cur: the current node |
| 4128 | * |
| 4129 | * Dump an XML/HTML node, recursive behaviour,children are printed too. |
| 4130 | */ |
| 4131 | void |
| 4132 | xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) { |
| 4133 | xmlBufferPtr buf; |
| 4134 | |
| 4135 | if (cur == NULL) { |
| 4136 | #ifdef DEBUG_TREE |
| 4137 | fprintf(stderr, "xmlElemDump : cur == NULL\n"); |
| 4138 | #endif |
| 4139 | return; |
| 4140 | } |
| 4141 | if (doc == NULL) { |
| 4142 | #ifdef DEBUG_TREE |
| 4143 | fprintf(stderr, "xmlElemDump : doc == NULL\n"); |
| 4144 | #endif |
| 4145 | } |
| 4146 | buf = xmlBufferCreate(); |
| 4147 | if (buf == NULL) return; |
| 4148 | if ((doc != NULL) && |
| 4149 | (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 4150 | htmlNodeDump(buf, doc, cur); |
| 4151 | } else |
| 4152 | xmlNodeDump(buf, doc, cur, 0, 1); |
| 4153 | xmlBufferDump(f, buf); |
| 4154 | xmlBufferFree(buf); |
| 4155 | } |
| 4156 | |
| 4157 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4158 | * xmlDocContentDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4159 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4160 | * @cur: the document |
| 4161 | * |
| 4162 | * Dump an XML document. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4163 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4164 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4165 | xmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur) { |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 4166 | xmlBufferWriteChar(buf, "<?xml version="); |
| 4167 | if (cur->version != NULL) |
| 4168 | xmlBufferWriteQuotedString(buf, cur->version); |
| 4169 | else |
| 4170 | xmlBufferWriteChar(buf, "\"1.0\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4171 | if (cur->encoding != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4172 | xmlBufferWriteChar(buf, " encoding="); |
| 4173 | xmlBufferWriteQuotedString(buf, cur->encoding); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4174 | } |
| 4175 | switch (cur->standalone) { |
| 4176 | case 0: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4177 | xmlBufferWriteChar(buf, " standalone=\"no\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4178 | break; |
| 4179 | case 1: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4180 | xmlBufferWriteChar(buf, " standalone=\"yes\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4181 | break; |
| 4182 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4183 | xmlBufferWriteChar(buf, "?>\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 4184 | if (cur->intSubset != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4185 | xmlDtdDump(buf, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4186 | if (cur->root != NULL) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4187 | xmlNodePtr child = cur->root; |
| 4188 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4189 | /* global namespace definitions, the old way */ |
| 4190 | if (oldXMLWDcompatibility) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4191 | xmlGlobalNsListDump(buf, cur->oldNs); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4192 | else |
| 4193 | xmlUpgradeOldNs(cur); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4194 | |
| 4195 | while (child != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4196 | xmlNodeDump(buf, cur, child, 0, 1); |
| 4197 | xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4198 | child = child->next; |
| 4199 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4200 | } |
| 4201 | } |
| 4202 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4203 | /** |
| 4204 | * xmlDocDumpMemory: |
| 4205 | * @cur: the document |
| 4206 | * @mem: OUT: the memory pointer |
| 4207 | * @size: OUT: the memory lenght |
| 4208 | * |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4209 | * Dump an XML document in memory and return the xmlChar * and it's size. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4210 | * It's up to the caller to free the memory. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4211 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4212 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4213 | xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4214 | xmlBufferPtr buf; |
| 4215 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4216 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 4217 | #ifdef DEBUG_TREE |
| 4218 | fprintf(stderr, "xmlDocDumpMemory : document == NULL\n"); |
| 4219 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4220 | *mem = NULL; |
| 4221 | *size = 0; |
| 4222 | return; |
| 4223 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4224 | buf = xmlBufferCreate(); |
| 4225 | if (buf == NULL) { |
| 4226 | *mem = NULL; |
| 4227 | *size = 0; |
| 4228 | return; |
| 4229 | } |
| 4230 | xmlDocContentDump(buf, cur); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 4231 | *mem = xmlStrndup(buf->content, buf->use); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4232 | *size = buf->use; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 4233 | xmlBufferFree(buf); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4234 | } |
| 4235 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4236 | /** |
| 4237 | * xmlGetDocCompressMode: |
| 4238 | * @doc: the document |
| 4239 | * |
| 4240 | * get the compression ratio for a document, ZLIB based |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 4241 | * Returns 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4242 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4243 | int |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4244 | xmlGetDocCompressMode (xmlDocPtr doc) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 4245 | if (doc == NULL) return(-1); |
| 4246 | return(doc->compression); |
| 4247 | } |
| 4248 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4249 | /** |
| 4250 | * xmlSetDocCompressMode: |
| 4251 | * @doc: the document |
| 4252 | * @mode: the compression ratio |
| 4253 | * |
| 4254 | * set the compression ratio for a document, ZLIB based |
| 4255 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 4256 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4257 | void |
| 4258 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 4259 | if (doc == NULL) return; |
| 4260 | if (mode < 0) doc->compression = 0; |
| 4261 | else if (mode > 9) doc->compression = 9; |
| 4262 | else doc->compression = mode; |
| 4263 | } |
| 4264 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4265 | /** |
| 4266 | * xmlGetCompressMode: |
| 4267 | * |
| 4268 | * get the default compression mode used, ZLIB based. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 4269 | * Returns 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 4270 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4271 | int |
| 4272 | xmlGetCompressMode(void) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4273 | return(xmlCompressMode); |
| 4274 | } |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4275 | |
| 4276 | /** |
| 4277 | * xmlSetCompressMode: |
| 4278 | * @mode: the compression ratio |
| 4279 | * |
| 4280 | * set the default compression mode used, ZLIB based |
| 4281 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 4282 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4283 | void |
| 4284 | xmlSetCompressMode(int mode) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4285 | if (mode < 0) xmlCompressMode = 0; |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 4286 | else if (mode > 9) xmlCompressMode = 9; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4287 | else xmlCompressMode = mode; |
| 4288 | } |
| 4289 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4290 | /** |
| 4291 | * xmlDocDump: |
| 4292 | * @f: the FILE* |
| 4293 | * @cur: the document |
| 4294 | * |
| 4295 | * Dump an XML document to an open FILE. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4296 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4297 | void |
| 4298 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4299 | xmlBufferPtr buf; |
| 4300 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4301 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 4302 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4303 | fprintf(stderr, "xmlDocDump : document == NULL\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 4304 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4305 | return; |
| 4306 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4307 | buf = xmlBufferCreate(); |
| 4308 | if (buf == NULL) return; |
| 4309 | xmlDocContentDump(buf, cur); |
| 4310 | xmlBufferDump(f, buf); |
| 4311 | xmlBufferFree(buf); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4312 | } |
| 4313 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4314 | /** |
| 4315 | * xmlSaveFile: |
| 4316 | * @filename: the filename |
| 4317 | * @cur: the document |
| 4318 | * |
| 4319 | * Dump an XML document to a file. Will use compression if |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 4320 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 4321 | * used. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4322 | * returns: the number of file written or -1 in case of failure. |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4323 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4324 | int |
| 4325 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4326 | xmlBufferPtr buf; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4327 | #ifdef HAVE_ZLIB_H |
| 4328 | gzFile zoutput = NULL; |
| 4329 | char mode[15]; |
| 4330 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4331 | FILE *output = NULL; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4332 | int ret; |
| 4333 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4334 | /* |
| 4335 | * save the content to a temp buffer. |
| 4336 | */ |
| 4337 | buf = xmlBufferCreate(); |
| 4338 | if (buf == NULL) return(0); |
| 4339 | xmlDocContentDump(buf, cur); |
| 4340 | |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4341 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 4342 | if (cur->compression < 0) cur->compression = xmlCompressMode; |
Daniel Veillard | dc3dd9d | 1998-09-24 19:25:54 +0000 | [diff] [blame] | 4343 | if ((cur->compression > 0) && (cur->compression <= 9)) { |
| 4344 | sprintf(mode, "w%d", cur->compression); |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 4345 | if (!strcmp(filename, "-")) |
| 4346 | zoutput = gzdopen(1, mode); |
| 4347 | else |
| 4348 | zoutput = gzopen(filename, mode); |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4349 | } |
| 4350 | if (zoutput == NULL) { |
| 4351 | #endif |
| 4352 | output = fopen(filename, "w"); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4353 | if (output == NULL) { |
| 4354 | xmlBufferFree(buf); |
| 4355 | return(-1); |
| 4356 | } |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4357 | #ifdef HAVE_ZLIB_H |
| 4358 | } |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4359 | |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4360 | if (zoutput != NULL) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4361 | ret = gzwrite(zoutput, buf->content, sizeof(xmlChar) * buf->use); |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4362 | gzclose(zoutput); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4363 | } else { |
| 4364 | #endif |
| 4365 | ret = xmlBufferDump(output, buf); |
| 4366 | fclose(output); |
| 4367 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4368 | } |
| 4369 | #endif |
Manish Vachharajani | 5e60f5a | 1999-05-29 03:04:30 +0000 | [diff] [blame] | 4370 | xmlBufferFree(buf); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4371 | return(ret * sizeof(xmlChar)); |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 4372 | } |
| 4373 | |