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