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