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