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