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 | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 28 | #include <libxml/xmlmemory.h> |
| 29 | #include <libxml/tree.h> |
| 30 | #include <libxml/parser.h> |
| 31 | #include <libxml/entities.h> |
| 32 | #include <libxml/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 | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 850 | * xmlNodeListGetRawString: |
| 851 | * @doc: the document |
| 852 | * @list: a Node list |
| 853 | * @inLine: should we replace entity contents or show their external form |
| 854 | * |
| 855 | * Returns the string equivalent to the text contained in the Node list |
| 856 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 857 | * this function doesn't do any character encoding handling. |
| 858 | * |
| 859 | * Returns a pointer to the string copy, the calller must free it. |
| 860 | */ |
| 861 | xmlChar * |
| 862 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) { |
| 863 | xmlNodePtr node = list; |
| 864 | xmlChar *ret = NULL; |
| 865 | xmlEntityPtr ent; |
| 866 | |
| 867 | if (list == NULL) return(NULL); |
| 868 | |
| 869 | while (node != NULL) { |
| 870 | if (node->type == XML_TEXT_NODE) { |
| 871 | if (inLine) { |
| 872 | #ifndef XML_USE_BUFFER_CONTENT |
| 873 | ret = xmlStrcat(ret, node->content); |
| 874 | #else |
| 875 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 876 | #endif |
| 877 | } else { |
| 878 | xmlChar *buffer; |
| 879 | |
| 880 | #ifndef XML_USE_BUFFER_CONTENT |
| 881 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 882 | #else |
| 883 | buffer = xmlEncodeSpecialChars(doc, |
| 884 | xmlBufferContent(node->content)); |
| 885 | #endif |
| 886 | if (buffer != NULL) { |
| 887 | ret = xmlStrcat(ret, buffer); |
| 888 | xmlFree(buffer); |
| 889 | } |
| 890 | } |
| 891 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 892 | if (inLine) { |
| 893 | ent = xmlGetDocEntity(doc, node->name); |
| 894 | if (ent != NULL) |
| 895 | ret = xmlStrcat(ret, ent->content); |
| 896 | else { |
| 897 | #ifndef XML_USE_BUFFER_CONTENT |
| 898 | ret = xmlStrcat(ret, node->content); |
| 899 | #else |
| 900 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 901 | #endif |
| 902 | } |
| 903 | } else { |
| 904 | xmlChar buf[2]; |
| 905 | buf[0] = '&'; buf[1] = 0; |
| 906 | ret = xmlStrncat(ret, buf, 1); |
| 907 | ret = xmlStrcat(ret, node->name); |
| 908 | buf[0] = ';'; buf[1] = 0; |
| 909 | ret = xmlStrncat(ret, buf, 1); |
| 910 | } |
| 911 | } |
| 912 | #if 0 |
| 913 | else { |
| 914 | fprintf(stderr, "xmlGetNodeListString : invalide node type %d\n", |
| 915 | node->type); |
| 916 | } |
| 917 | #endif |
| 918 | node = node->next; |
| 919 | } |
| 920 | return(ret); |
| 921 | } |
| 922 | |
| 923 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 924 | * xmlNewProp: |
| 925 | * @node: the holding node |
| 926 | * @name: the name of the attribute |
| 927 | * @value: the value of the attribute |
| 928 | * |
| 929 | * Create a new property carried by a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 930 | * Returns a pointer to the attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 931 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 932 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 933 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 934 | xmlAttrPtr cur; |
| 935 | |
| 936 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 937 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 938 | fprintf(stderr, "xmlNewProp : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 939 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 940 | return(NULL); |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * Allocate a new property and fill the fields. |
| 945 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 946 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 947 | if (cur == NULL) { |
| 948 | fprintf(stderr, "xmlNewProp : malloc failed\n"); |
| 949 | return(NULL); |
| 950 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 951 | memset(cur, 0, sizeof(xmlAttr)); |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 952 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 953 | |
| 954 | cur->parent = node; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 955 | cur->name = xmlStrdup(name); |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 956 | if (value != NULL) { |
| 957 | xmlChar *buffer; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 958 | xmlNodePtr tmp; |
| 959 | |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 960 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 961 | cur->children = xmlStringGetNodeList(node->doc, buffer); |
| 962 | tmp = cur->children; |
| 963 | while (tmp != NULL) { |
| 964 | tmp->parent = (xmlNodePtr) cur; |
| 965 | if (tmp->next == NULL) |
| 966 | cur->last = tmp; |
| 967 | tmp = tmp->next; |
| 968 | } |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 969 | xmlFree(buffer); |
| 970 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 971 | |
| 972 | /* |
| 973 | * Add it at the end to preserve parsing order ... |
| 974 | */ |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 975 | if (node != NULL) { |
| 976 | if (node->properties == NULL) { |
| 977 | node->properties = cur; |
| 978 | } else { |
| 979 | xmlAttrPtr prev = node->properties; |
| 980 | |
| 981 | while (prev->next != NULL) prev = prev->next; |
| 982 | prev->next = cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 983 | cur->prev = prev; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | return(cur); |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * xmlNewNsProp: |
| 991 | * @node: the holding node |
| 992 | * @ns: the namespace |
| 993 | * @name: the name of the attribute |
| 994 | * @value: the value of the attribute |
| 995 | * |
| 996 | * Create a new property tagged with a namespace and carried by a node. |
| 997 | * Returns a pointer to the attribute |
| 998 | */ |
| 999 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1000 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 1001 | const xmlChar *value) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1002 | xmlAttrPtr cur; |
| 1003 | |
| 1004 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1005 | #ifdef DEBUG_TREE |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1006 | fprintf(stderr, "xmlNewProp : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1007 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1008 | return(NULL); |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * Allocate a new property and fill the fields. |
| 1013 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1014 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1015 | if (cur == NULL) { |
| 1016 | fprintf(stderr, "xmlNewProp : malloc failed\n"); |
| 1017 | return(NULL); |
| 1018 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1019 | memset(cur, 0, sizeof(xmlAttr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1020 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1021 | |
| 1022 | cur->parent = node; |
| 1023 | if (node != NULL) |
| 1024 | cur->doc = node->doc; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1025 | cur->ns = ns; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1026 | cur->name = xmlStrdup(name); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1027 | if (value != NULL) { |
| 1028 | xmlChar *buffer; |
| 1029 | xmlNodePtr tmp; |
| 1030 | |
| 1031 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 1032 | cur->children = xmlStringGetNodeList(node->doc, buffer); |
| 1033 | tmp = cur->children; |
| 1034 | while (tmp != NULL) { |
| 1035 | tmp->parent = (xmlNodePtr) cur; |
| 1036 | if (tmp->next == NULL) |
| 1037 | cur->last = tmp; |
| 1038 | tmp = tmp->next; |
| 1039 | } |
| 1040 | xmlFree(buffer); |
| 1041 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1042 | |
| 1043 | /* |
| 1044 | * Add it at the end to preserve parsing order ... |
| 1045 | */ |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1046 | if (node != NULL) { |
| 1047 | if (node->properties == NULL) { |
| 1048 | node->properties = cur; |
| 1049 | } else { |
| 1050 | xmlAttrPtr prev = node->properties; |
| 1051 | |
| 1052 | while (prev->next != NULL) prev = prev->next; |
| 1053 | prev->next = cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1054 | cur->prev = prev; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | return(cur); |
| 1058 | } |
| 1059 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1060 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1061 | * xmlNewDocProp: |
| 1062 | * @doc: the document |
| 1063 | * @name: the name of the attribute |
| 1064 | * @value: the value of the attribute |
| 1065 | * |
| 1066 | * Create a new property carried by a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1067 | * Returns a pointer to the attribute |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1068 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1069 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1070 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1071 | xmlAttrPtr cur; |
| 1072 | |
| 1073 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1074 | #ifdef DEBUG_TREE |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1075 | fprintf(stderr, "xmlNewProp : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1076 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1077 | return(NULL); |
| 1078 | } |
| 1079 | |
| 1080 | /* |
| 1081 | * Allocate a new property and fill the fields. |
| 1082 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1083 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1084 | if (cur == NULL) { |
| 1085 | fprintf(stderr, "xmlNewProp : malloc failed\n"); |
| 1086 | return(NULL); |
| 1087 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1088 | memset(cur, 0, sizeof(xmlAttr)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1089 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1090 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1091 | cur->name = xmlStrdup(name); |
| 1092 | cur->doc = doc; |
| 1093 | if (value != NULL) |
| 1094 | cur->children = xmlStringGetNodeList(doc, value); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1095 | return(cur); |
| 1096 | } |
| 1097 | |
| 1098 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1099 | * xmlFreePropList: |
| 1100 | * @cur: the first property in the list |
| 1101 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1102 | * 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] | 1103 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1104 | void |
| 1105 | xmlFreePropList(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1106 | xmlAttrPtr next; |
| 1107 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1108 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1109 | fprintf(stderr, "xmlFreePropList : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1110 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1111 | return; |
| 1112 | } |
| 1113 | while (cur != NULL) { |
| 1114 | next = cur->next; |
| 1115 | xmlFreeProp(cur); |
| 1116 | cur = next; |
| 1117 | } |
| 1118 | } |
| 1119 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1120 | /** |
| 1121 | * xmlFreeProp: |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1122 | * @cur: an attribute |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1123 | * |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1124 | * Free one attribute, all the content is freed too |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1125 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1126 | void |
| 1127 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1128 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1129 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1130 | fprintf(stderr, "xmlFreeProp : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1131 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1132 | return; |
| 1133 | } |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 1134 | /* Check for ID removal -> leading to invalid references ! */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1135 | if ((cur->parent != NULL) && |
| 1136 | (xmlIsID(cur->parent->doc, cur->parent, cur))) |
| 1137 | xmlRemoveID(cur->parent->doc, cur); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1138 | if (cur->name != NULL) xmlFree((char *) cur->name); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1139 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1140 | memset(cur, -1, sizeof(xmlAttr)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1141 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1144 | /** |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1145 | * xmlRemoveProp: |
| 1146 | * @cur: an attribute |
| 1147 | * |
| 1148 | * Unlink and free one attribute, all the content is freed too |
| 1149 | * Note this doesn't work for namespace definition attributes |
| 1150 | * |
| 1151 | * Returns 0 if success and -1 in case of error. |
| 1152 | */ |
| 1153 | int |
| 1154 | xmlRemoveProp(xmlAttrPtr cur) { |
| 1155 | xmlAttrPtr tmp; |
| 1156 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1157 | #ifdef DEBUG_TREE |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1158 | fprintf(stderr, "xmlRemoveProp : cur == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1159 | #endif |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1160 | return(-1); |
| 1161 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1162 | if (cur->parent == NULL) { |
| 1163 | #ifdef DEBUG_TREE |
| 1164 | fprintf(stderr, "xmlRemoveProp : cur->parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1165 | #endif |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1166 | return(-1); |
| 1167 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1168 | tmp = cur->parent->properties; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1169 | if (tmp == cur) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1170 | cur->parent->properties = cur->next; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1171 | xmlFreeProp(cur); |
| 1172 | return(0); |
| 1173 | } |
| 1174 | while (tmp != NULL) { |
| 1175 | if (tmp->next == cur) { |
| 1176 | tmp->next = cur->next; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1177 | if (tmp->next != NULL) |
| 1178 | tmp->next->prev = tmp; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1179 | xmlFreeProp(cur); |
| 1180 | return(0); |
| 1181 | } |
| 1182 | tmp = tmp->next; |
| 1183 | } |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1184 | #ifdef DEBUG_TREE |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1185 | fprintf(stderr, "xmlRemoveProp : attribute not owned by its node\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1186 | #endif |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1187 | return(-1); |
| 1188 | } |
| 1189 | |
| 1190 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1191 | * xmlNewPI: |
| 1192 | * @name: the processing instruction name |
| 1193 | * @content: the PI content |
| 1194 | * |
| 1195 | * Creation of a processing instruction element. |
| 1196 | * Returns a pointer to the new node object. |
| 1197 | */ |
| 1198 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1199 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1200 | xmlNodePtr cur; |
| 1201 | |
| 1202 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1203 | #ifdef DEBUG_TREE |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1204 | fprintf(stderr, "xmlNewPI : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1205 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1206 | return(NULL); |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * Allocate a new node and fill the fields. |
| 1211 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1212 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1213 | if (cur == NULL) { |
| 1214 | fprintf(stderr, "xmlNewPI : malloc failed\n"); |
| 1215 | return(NULL); |
| 1216 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1217 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1218 | cur->type = XML_PI_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1219 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1220 | cur->name = xmlStrdup(name); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1221 | if (content != NULL) { |
| 1222 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1223 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1224 | #else |
| 1225 | cur->content = xmlBufferCreateSize(0); |
| 1226 | xmlBufferSetAllocationScheme(cur->content, |
| 1227 | xmlGetBufferAllocationScheme()); |
| 1228 | xmlBufferAdd(cur->content, content, -1); |
| 1229 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1230 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1231 | return(cur); |
| 1232 | } |
| 1233 | |
| 1234 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1235 | * xmlNewNode: |
| 1236 | * @ns: namespace if any |
| 1237 | * @name: the node name |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1238 | * |
| 1239 | * Creation of a new node element. @ns and @content are optionnal (NULL). |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1240 | * If content is non NULL, a child list containing the TEXTs and |
| 1241 | * ENTITY_REFs node will be created. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1242 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1243 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1244 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1245 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1246 | xmlNodePtr cur; |
| 1247 | |
| 1248 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1249 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1250 | fprintf(stderr, "xmlNewNode : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1251 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1252 | return(NULL); |
| 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * Allocate a new node and fill the fields. |
| 1257 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1258 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1259 | if (cur == NULL) { |
| 1260 | fprintf(stderr, "xmlNewNode : malloc failed\n"); |
| 1261 | return(NULL); |
| 1262 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1263 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1264 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1265 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1266 | cur->name = xmlStrdup(name); |
| 1267 | cur->ns = ns; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1268 | return(cur); |
| 1269 | } |
| 1270 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1271 | /** |
| 1272 | * xmlNewDocNode: |
| 1273 | * @doc: the document |
| 1274 | * @ns: namespace if any |
| 1275 | * @name: the node name |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1276 | * @content: the XML text content if any |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1277 | * |
| 1278 | * Creation of a new node element within a document. @ns and @content |
| 1279 | * are optionnal (NULL). |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1280 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 1281 | * references, but XML special chars need to be escaped first by using |
| 1282 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 1283 | * need entities support. |
| 1284 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1285 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1286 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1287 | xmlNodePtr |
| 1288 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1289 | const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1290 | xmlNodePtr cur; |
| 1291 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1292 | cur = xmlNewNode(ns, name); |
| 1293 | if (cur != NULL) { |
| 1294 | cur->doc = doc; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1295 | if (content != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1296 | cur->children = xmlStringGetNodeList(doc, content); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1297 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1298 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1299 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1300 | return(cur); |
| 1301 | } |
| 1302 | |
| 1303 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1304 | /** |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1305 | * xmlNewDocRawNode: |
| 1306 | * @doc: the document |
| 1307 | * @ns: namespace if any |
| 1308 | * @name: the node name |
| 1309 | * @content: the text content if any |
| 1310 | * |
| 1311 | * Creation of a new node element within a document. @ns and @content |
| 1312 | * are optionnal (NULL). |
| 1313 | * |
| 1314 | * Returns a pointer to the new node object. |
| 1315 | */ |
| 1316 | xmlNodePtr |
| 1317 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 1318 | const xmlChar *name, const xmlChar *content) { |
| 1319 | xmlNodePtr cur; |
| 1320 | |
| 1321 | cur = xmlNewNode(ns, name); |
| 1322 | if (cur != NULL) { |
| 1323 | cur->doc = doc; |
| 1324 | if (content != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1325 | cur->children = xmlNewDocText(doc, content); |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1326 | UPDATE_LAST_CHILD(cur) |
| 1327 | } |
| 1328 | } |
| 1329 | return(cur); |
| 1330 | } |
| 1331 | |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1332 | /** |
| 1333 | * xmlNewDocFragment: |
| 1334 | * @doc: the document owning the fragment |
| 1335 | * |
| 1336 | * Creation of a new Fragment node. |
| 1337 | * Returns a pointer to the new node object. |
| 1338 | */ |
| 1339 | xmlNodePtr |
| 1340 | xmlNewDocFragment(xmlDocPtr doc) { |
| 1341 | xmlNodePtr cur; |
| 1342 | |
| 1343 | /* |
| 1344 | * Allocate a new DocumentFragment node and fill the fields. |
| 1345 | */ |
| 1346 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1347 | if (cur == NULL) { |
| 1348 | fprintf(stderr, "xmlNewDocFragment : malloc failed\n"); |
| 1349 | return(NULL); |
| 1350 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1351 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1352 | cur->type = XML_DOCUMENT_FRAG_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1353 | |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1354 | cur->doc = doc; |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1355 | return(cur); |
| 1356 | } |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1357 | |
| 1358 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1359 | * xmlNewText: |
| 1360 | * @content: the text content |
| 1361 | * |
| 1362 | * Creation of a new text node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1363 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1364 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1365 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1366 | xmlNewText(const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1367 | xmlNodePtr cur; |
| 1368 | |
| 1369 | /* |
| 1370 | * Allocate a new node and fill the fields. |
| 1371 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1372 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1373 | if (cur == NULL) { |
| 1374 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 1375 | return(NULL); |
| 1376 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1377 | memset(cur, 0, sizeof(xmlNode)); |
| 1378 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1379 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1380 | cur->name = xmlStrdup(xmlStringText); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1381 | if (content != NULL) { |
| 1382 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1383 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1384 | #else |
| 1385 | cur->content = xmlBufferCreateSize(0); |
| 1386 | xmlBufferSetAllocationScheme(cur->content, |
| 1387 | xmlGetBufferAllocationScheme()); |
| 1388 | xmlBufferAdd(cur->content, content, -1); |
| 1389 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1390 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1391 | return(cur); |
| 1392 | } |
| 1393 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1394 | /** |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1395 | * xmlNewTextChild: |
| 1396 | * @parent: the parent node |
| 1397 | * @ns: a namespace if any |
| 1398 | * @name: the name of the child |
| 1399 | * @content: the text content of the child if any. |
| 1400 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1401 | * 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] | 1402 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1403 | * a child TEXT node will be created containing the string content. |
| 1404 | * |
| 1405 | * Returns a pointer to the new node object. |
| 1406 | */ |
| 1407 | xmlNodePtr |
| 1408 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 1409 | const xmlChar *name, const xmlChar *content) { |
| 1410 | xmlNodePtr cur, prev; |
| 1411 | |
| 1412 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1413 | #ifdef DEBUG_TREE |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1414 | fprintf(stderr, "xmlNewTextChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1415 | #endif |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1416 | return(NULL); |
| 1417 | } |
| 1418 | |
| 1419 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1420 | #ifdef DEBUG_TREE |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1421 | fprintf(stderr, "xmlNewTextChild : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1422 | #endif |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1423 | return(NULL); |
| 1424 | } |
| 1425 | |
| 1426 | /* |
| 1427 | * Allocate a new node |
| 1428 | */ |
| 1429 | if (ns == NULL) |
| 1430 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 1431 | else |
| 1432 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 1433 | if (cur == NULL) return(NULL); |
| 1434 | |
| 1435 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1436 | * add the new element at the end of the children list. |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1437 | */ |
| 1438 | cur->type = XML_ELEMENT_NODE; |
| 1439 | cur->parent = parent; |
| 1440 | cur->doc = parent->doc; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1441 | if (parent->children == NULL) { |
| 1442 | parent->children = cur; |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1443 | parent->last = cur; |
| 1444 | } else { |
| 1445 | prev = parent->last; |
| 1446 | prev->next = cur; |
| 1447 | cur->prev = prev; |
| 1448 | parent->last = cur; |
| 1449 | } |
| 1450 | |
| 1451 | return(cur); |
| 1452 | } |
| 1453 | |
| 1454 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1455 | * xmlNewCharRef: |
| 1456 | * @doc: the document |
| 1457 | * @name: the char ref string, starting with # or "&# ... ;" |
| 1458 | * |
| 1459 | * Creation of a new character reference node. |
| 1460 | * Returns a pointer to the new node object. |
| 1461 | */ |
| 1462 | xmlNodePtr |
| 1463 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 1464 | xmlNodePtr cur; |
| 1465 | |
| 1466 | /* |
| 1467 | * Allocate a new node and fill the fields. |
| 1468 | */ |
| 1469 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1470 | if (cur == NULL) { |
| 1471 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 1472 | return(NULL); |
| 1473 | } |
| 1474 | memset(cur, 0, sizeof(xmlNode)); |
| 1475 | cur->type = XML_ENTITY_REF_NODE; |
| 1476 | |
| 1477 | cur->doc = doc; |
| 1478 | if (name[0] == '&') { |
| 1479 | int len; |
| 1480 | name++; |
| 1481 | len = xmlStrlen(name); |
| 1482 | if (name[len - 1] == ';') |
| 1483 | cur->name = xmlStrndup(name, len - 1); |
| 1484 | else |
| 1485 | cur->name = xmlStrndup(name, len); |
| 1486 | } else |
| 1487 | cur->name = xmlStrdup(name); |
| 1488 | return(cur); |
| 1489 | } |
| 1490 | |
| 1491 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1492 | * xmlNewReference: |
| 1493 | * @doc: the document |
| 1494 | * @name: the reference name, or the reference string with & and ; |
| 1495 | * |
| 1496 | * Creation of a new reference node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1497 | * Returns a pointer to the new node object. |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +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 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1501 | xmlNodePtr cur; |
| 1502 | xmlEntityPtr ent; |
| 1503 | |
| 1504 | /* |
| 1505 | * Allocate a new node and fill the fields. |
| 1506 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1507 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1508 | if (cur == NULL) { |
| 1509 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 1510 | return(NULL); |
| 1511 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1512 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1513 | cur->type = XML_ENTITY_REF_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1514 | |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 1515 | cur->doc = doc; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1516 | if (name[0] == '&') { |
| 1517 | int len; |
| 1518 | name++; |
| 1519 | len = xmlStrlen(name); |
| 1520 | if (name[len - 1] == ';') |
| 1521 | cur->name = xmlStrndup(name, len - 1); |
| 1522 | else |
| 1523 | cur->name = xmlStrndup(name, len); |
| 1524 | } else |
| 1525 | cur->name = xmlStrdup(name); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1526 | |
| 1527 | ent = xmlGetDocEntity(doc, cur->name); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1528 | if (ent != NULL) { |
| 1529 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1530 | cur->content = ent->content; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1531 | #else |
| 1532 | /* |
| 1533 | * CJN 11.18.99 this might be a problem, since the xmlBuffer gets |
| 1534 | * a copy of this pointer. Let's hope we don't manipulate it |
| 1535 | * later |
| 1536 | */ |
| 1537 | cur->content = xmlBufferCreateSize(0); |
| 1538 | xmlBufferSetAllocationScheme(cur->content, |
| 1539 | xmlGetBufferAllocationScheme()); |
| 1540 | if (ent->content != NULL) |
| 1541 | xmlBufferAdd(cur->content, ent->content, -1); |
| 1542 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1543 | cur->children = (xmlNodePtr) ent; |
| 1544 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1545 | return(cur); |
| 1546 | } |
| 1547 | |
| 1548 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1549 | * xmlNewDocText: |
| 1550 | * @doc: the document |
| 1551 | * @content: the text content |
| 1552 | * |
| 1553 | * Creation of a new text node within a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1554 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1555 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1556 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1557 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1558 | xmlNodePtr cur; |
| 1559 | |
| 1560 | cur = xmlNewText(content); |
| 1561 | if (cur != NULL) cur->doc = doc; |
| 1562 | return(cur); |
| 1563 | } |
| 1564 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1565 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1566 | * xmlNewTextLen: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1567 | * @content: the text content |
| 1568 | * @len: the text len. |
| 1569 | * |
| 1570 | * 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] | 1571 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1572 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1573 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1574 | xmlNewTextLen(const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1575 | xmlNodePtr cur; |
| 1576 | |
| 1577 | /* |
| 1578 | * Allocate a new node and fill the fields. |
| 1579 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1580 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1581 | if (cur == NULL) { |
| 1582 | fprintf(stderr, "xmlNewText : malloc failed\n"); |
| 1583 | return(NULL); |
| 1584 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1585 | memset(cur, 0, sizeof(xmlNode)); |
| 1586 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1587 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1588 | cur->name = xmlStrdup(xmlStringText); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1589 | if (content != NULL) { |
| 1590 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1591 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1592 | #else |
| 1593 | cur->content = xmlBufferCreateSize(len); |
| 1594 | xmlBufferSetAllocationScheme(cur->content, |
| 1595 | xmlGetBufferAllocationScheme()); |
| 1596 | xmlBufferAdd(cur->content, content, len); |
| 1597 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1598 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1599 | return(cur); |
| 1600 | } |
| 1601 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1602 | /** |
| 1603 | * xmlNewDocTextLen: |
| 1604 | * @doc: the document |
| 1605 | * @content: the text content |
| 1606 | * @len: the text len. |
| 1607 | * |
| 1608 | * Creation of a new text node with an extra content lenght parameter. The |
| 1609 | * text node pertain to a given document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1610 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1611 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1612 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1613 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1614 | xmlNodePtr cur; |
| 1615 | |
| 1616 | cur = xmlNewTextLen(content, len); |
| 1617 | if (cur != NULL) cur->doc = doc; |
| 1618 | return(cur); |
| 1619 | } |
| 1620 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1621 | /** |
| 1622 | * xmlNewComment: |
| 1623 | * @content: the comment content |
| 1624 | * |
| 1625 | * Creation of a new node containing a comment. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1626 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +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 | xmlNewComment(const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1630 | xmlNodePtr cur; |
| 1631 | |
| 1632 | /* |
| 1633 | * Allocate a new node and fill the fields. |
| 1634 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1635 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1636 | if (cur == NULL) { |
| 1637 | fprintf(stderr, "xmlNewComment : malloc failed\n"); |
| 1638 | return(NULL); |
| 1639 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1640 | memset(cur, 0, sizeof(xmlNode)); |
| 1641 | cur->type = XML_COMMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1642 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1643 | cur->name = xmlStrdup(xmlStringComment); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1644 | if (content != NULL) { |
| 1645 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1646 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1647 | #else |
| 1648 | cur->content = xmlBufferCreateSize(0); |
| 1649 | xmlBufferSetAllocationScheme(cur->content, |
| 1650 | xmlGetBufferAllocationScheme()); |
| 1651 | xmlBufferAdd(cur->content, content, -1); |
| 1652 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1653 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1654 | return(cur); |
| 1655 | } |
| 1656 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1657 | /** |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1658 | * xmlNewCDataBlock: |
| 1659 | * @doc: the document |
| 1660 | * @content: the CData block content content |
| 1661 | * @len: the length of the block |
| 1662 | * |
| 1663 | * Creation of a new node containing a CData block. |
| 1664 | * Returns a pointer to the new node object. |
| 1665 | */ |
| 1666 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1667 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1668 | xmlNodePtr cur; |
| 1669 | |
| 1670 | /* |
| 1671 | * Allocate a new node and fill the fields. |
| 1672 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1673 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1674 | if (cur == NULL) { |
| 1675 | fprintf(stderr, "xmlNewCDataBlock : malloc failed\n"); |
| 1676 | return(NULL); |
| 1677 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1678 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1679 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1680 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1681 | if (content != NULL) { |
| 1682 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1683 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1684 | #else |
| 1685 | cur->content = xmlBufferCreateSize(len); |
| 1686 | xmlBufferSetAllocationScheme(cur->content, |
| 1687 | xmlGetBufferAllocationScheme()); |
| 1688 | xmlBufferAdd(cur->content, content, len); |
| 1689 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1690 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1691 | return(cur); |
| 1692 | } |
| 1693 | |
| 1694 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1695 | * xmlNewDocComment: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1696 | * @doc: the document |
| 1697 | * @content: the comment content |
| 1698 | * |
| 1699 | * Creation of a new node containing a commentwithin a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1700 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1701 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1702 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1703 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1704 | xmlNodePtr cur; |
| 1705 | |
| 1706 | cur = xmlNewComment(content); |
| 1707 | if (cur != NULL) cur->doc = doc; |
| 1708 | return(cur); |
| 1709 | } |
| 1710 | |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1711 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1712 | /** |
| 1713 | * xmlNewChild: |
| 1714 | * @parent: the parent node |
| 1715 | * @ns: a namespace if any |
| 1716 | * @name: the name of the child |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1717 | * @content: the XML content of the child if any. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1718 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1719 | * 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] | 1720 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1721 | * 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] | 1722 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 1723 | * references, but XML special chars need to be escaped first by using |
| 1724 | * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities |
| 1725 | * support is not needed. |
| 1726 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1727 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1728 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1729 | xmlNodePtr |
| 1730 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1731 | const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1732 | xmlNodePtr cur, prev; |
| 1733 | |
| 1734 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1735 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1736 | fprintf(stderr, "xmlNewChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1737 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1738 | return(NULL); |
| 1739 | } |
| 1740 | |
| 1741 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1742 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1743 | fprintf(stderr, "xmlNewChild : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1744 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1745 | return(NULL); |
| 1746 | } |
| 1747 | |
| 1748 | /* |
| 1749 | * Allocate a new node |
| 1750 | */ |
| 1751 | if (ns == NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1752 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1753 | else |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1754 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1755 | if (cur == NULL) return(NULL); |
| 1756 | |
| 1757 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1758 | * add the new element at the end of the children list. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1759 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1760 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1761 | cur->parent = parent; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1762 | cur->doc = parent->doc; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1763 | if (parent->children == NULL) { |
| 1764 | parent->children = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1765 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1766 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1767 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1768 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1769 | cur->prev = prev; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1770 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | return(cur); |
| 1774 | } |
| 1775 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1776 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1777 | * xmlAddNextSibling: |
| 1778 | * @cur: the child node |
| 1779 | * @elem: the new node |
| 1780 | * |
| 1781 | * Add a new element @elem as the next siblings of @cur |
| 1782 | * If the new element was already inserted in a document it is |
| 1783 | * first unlinked from its existing context. |
| 1784 | * |
| 1785 | * Returns the new element or NULL in case of error. |
| 1786 | */ |
| 1787 | xmlNodePtr |
| 1788 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1789 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1790 | #ifdef DEBUG_TREE |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1791 | fprintf(stderr, "xmlAddNextSibling : cur == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1792 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1793 | return(NULL); |
| 1794 | } |
| 1795 | if (elem == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1796 | #ifdef DEBUG_TREE |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1797 | fprintf(stderr, "xmlAddNextSibling : elem == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1798 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1799 | return(NULL); |
| 1800 | } |
| 1801 | |
| 1802 | xmlUnlinkNode(elem); |
| 1803 | elem->doc = cur->doc; |
| 1804 | elem->parent = cur->parent; |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 1805 | elem->prev = cur; |
| 1806 | elem->next = cur->next; |
| 1807 | cur->next = elem; |
| 1808 | if (elem->next != NULL) |
| 1809 | elem->next->prev = elem; |
| 1810 | if ((elem->parent != NULL) && (elem->parent->last == cur)) |
| 1811 | elem->parent->last = elem; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1812 | return(elem); |
| 1813 | } |
| 1814 | |
| 1815 | /** |
| 1816 | * xmlAddPrevSibling: |
| 1817 | * @cur: the child node |
| 1818 | * @elem: the new node |
| 1819 | * |
| 1820 | * Add a new element @elem as the previous siblings of @cur |
| 1821 | * If the new element was already inserted in a document it is |
| 1822 | * first unlinked from its existing context. |
| 1823 | * |
| 1824 | * Returns the new element or NULL in case of error. |
| 1825 | */ |
| 1826 | xmlNodePtr |
| 1827 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1828 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1829 | #ifdef DEBUG_TREE |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1830 | fprintf(stderr, "xmlAddPrevSibling : cur == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1831 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1832 | return(NULL); |
| 1833 | } |
| 1834 | if (elem == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1835 | #ifdef DEBUG_TREE |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1836 | fprintf(stderr, "xmlAddPrevSibling : elem == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1837 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1838 | return(NULL); |
| 1839 | } |
| 1840 | |
| 1841 | xmlUnlinkNode(elem); |
| 1842 | elem->doc = cur->doc; |
| 1843 | elem->parent = cur->parent; |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 1844 | elem->next = cur; |
| 1845 | elem->prev = cur->prev; |
| 1846 | cur->prev = elem; |
| 1847 | if (elem->prev != NULL) |
| 1848 | elem->prev->next = elem; |
| 1849 | if ((elem->parent != NULL) && (elem->parent->children == cur)) |
| 1850 | elem->parent->children = elem; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1851 | return(elem); |
| 1852 | } |
| 1853 | |
| 1854 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1855 | * xmlAddSibling: |
| 1856 | * @cur: the child node |
| 1857 | * @elem: the new node |
| 1858 | * |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1859 | * Add a new element @elem to the list of siblings of @cur |
| 1860 | * If the new element was already inserted in a document it is |
| 1861 | * first unlinked from its existing context. |
| 1862 | * |
| 1863 | * Returns the new element or NULL in case of error. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1864 | */ |
| 1865 | xmlNodePtr |
| 1866 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1867 | xmlNodePtr parent; |
| 1868 | |
| 1869 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1870 | #ifdef DEBUG_TREE |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1871 | fprintf(stderr, "xmlAddSibling : cur == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1872 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1873 | return(NULL); |
| 1874 | } |
| 1875 | |
| 1876 | if (elem == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1877 | #ifdef DEBUG_TREE |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1878 | fprintf(stderr, "xmlAddSibling : elem == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1879 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1880 | return(NULL); |
| 1881 | } |
| 1882 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1883 | /* |
| 1884 | * Constant time is we can rely on the ->parent->last to find |
| 1885 | * the last sibling. |
| 1886 | */ |
| 1887 | if ((cur->parent != NULL) && |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1888 | (cur->parent->children != NULL) && |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1889 | (cur->parent->last != NULL) && |
| 1890 | (cur->parent->last->next == NULL)) { |
| 1891 | cur = cur->parent->last; |
| 1892 | } else { |
| 1893 | while (cur->next != NULL) cur = cur->next; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1896 | xmlUnlinkNode(elem); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1897 | if (elem->doc == NULL) |
| 1898 | elem->doc = cur->doc; /* the parent may not be linked to a doc ! */ |
| 1899 | |
| 1900 | parent = cur->parent; |
| 1901 | elem->prev = cur; |
| 1902 | elem->next = NULL; |
| 1903 | elem->parent = parent; |
| 1904 | cur->next = elem; |
| 1905 | if (parent != NULL) |
| 1906 | parent->last = elem; |
| 1907 | |
| 1908 | return(elem); |
| 1909 | } |
| 1910 | |
| 1911 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1912 | * xmlAddChild: |
| 1913 | * @parent: the parent node |
| 1914 | * @cur: the child node |
| 1915 | * |
| 1916 | * 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] | 1917 | * Returns the child or NULL in case of error. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1918 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1919 | xmlNodePtr |
| 1920 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1921 | xmlNodePtr prev; |
| 1922 | |
| 1923 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1924 | #ifdef DEBUG_TREE |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 1925 | fprintf(stderr, "xmlAddChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1926 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1927 | return(NULL); |
| 1928 | } |
| 1929 | |
| 1930 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1931 | #ifdef DEBUG_TREE |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 1932 | fprintf(stderr, "xmlAddChild : child == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1933 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1934 | return(NULL); |
| 1935 | } |
| 1936 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1937 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 1938 | (cur->doc != parent->doc)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1939 | #ifdef DEBUG_TREE |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1940 | fprintf(stderr, "Elements moved to a different document\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1941 | #endif |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1944 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1945 | * add the new element at the end of the children list. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1946 | */ |
| 1947 | cur->parent = parent; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1948 | 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] | 1949 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1950 | /* |
| 1951 | * Handle the case where parent->content != NULL, in that case it will |
| 1952 | * create a intermediate TEXT node. |
| 1953 | */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1954 | if (((parent->type == XML_ELEMENT_NODE) || (parent->type == XML_TEXT_NODE)) && |
| 1955 | (parent->content != NULL)) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1956 | xmlNodePtr text; |
| 1957 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1958 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1959 | text = xmlNewDocText(parent->doc, parent->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1960 | #else |
| 1961 | text = xmlNewDocText(parent->doc, xmlBufferContent(parent->content)); |
| 1962 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1963 | if (text != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1964 | text->next = parent->children; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1965 | if (text->next != NULL) |
| 1966 | text->next->prev = text; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1967 | parent->children = text; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1968 | UPDATE_LAST_CHILD(parent) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1969 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1970 | xmlFree(parent->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1971 | #else |
| 1972 | xmlBufferFree(parent->content); |
| 1973 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1974 | parent->content = NULL; |
| 1975 | } |
| 1976 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1977 | if (parent->children == NULL) { |
| 1978 | parent->children = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1979 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1980 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1981 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1982 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1983 | cur->prev = prev; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1984 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | return(cur); |
| 1988 | } |
| 1989 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1990 | /** |
| 1991 | * xmlGetLastChild: |
| 1992 | * @parent: the parent node |
| 1993 | * |
| 1994 | * Search the last child of a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1995 | * Returns the last child or NULL if none. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1996 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1997 | xmlNodePtr |
| 1998 | xmlGetLastChild(xmlNodePtr parent) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1999 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2000 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2001 | fprintf(stderr, "xmlGetLastChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2002 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2003 | return(NULL); |
| 2004 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2005 | return(parent->last); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2008 | /** |
| 2009 | * xmlFreeNodeList: |
| 2010 | * @cur: the first node in the list |
| 2011 | * |
| 2012 | * 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] | 2013 | * the children are freed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2014 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2015 | void |
| 2016 | xmlFreeNodeList(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2017 | xmlNodePtr next; |
| 2018 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2019 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2020 | fprintf(stderr, "xmlFreeNodeList : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2021 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2022 | return; |
| 2023 | } |
| 2024 | while (cur != NULL) { |
| 2025 | next = cur->next; |
| 2026 | xmlFreeNode(cur); |
| 2027 | cur = next; |
| 2028 | } |
| 2029 | } |
| 2030 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2031 | /** |
| 2032 | * xmlFreeNode: |
| 2033 | * @cur: the node |
| 2034 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2035 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 2036 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2037 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2038 | void |
| 2039 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2040 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2041 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2042 | fprintf(stderr, "xmlFreeNode : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2043 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2044 | return; |
| 2045 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2046 | if (cur->type == XML_DTD_NODE) |
| 2047 | return; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2048 | cur->doc = NULL; |
| 2049 | cur->parent = NULL; |
| 2050 | cur->next = NULL; |
| 2051 | cur->prev = NULL; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2052 | if ((cur->children != NULL) && |
| 2053 | (cur->type != XML_ENTITY_REF_NODE)) |
| 2054 | xmlFreeNodeList(cur->children); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2055 | if (cur->properties != NULL) xmlFreePropList(cur->properties); |
| 2056 | if (cur->type != XML_ENTITY_REF_NODE) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2057 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2058 | if (cur->content != NULL) xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2059 | #else |
| 2060 | if (cur->content != NULL) xmlBufferFree(cur->content); |
| 2061 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2062 | if (cur->name != NULL) xmlFree((char *) cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2063 | if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef); |
| 2064 | memset(cur, -1, sizeof(xmlNode)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2065 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2068 | /** |
| 2069 | * xmlUnlinkNode: |
| 2070 | * @cur: the node |
| 2071 | * |
| 2072 | * Unlink a node from it's current context, the node is not freed |
| 2073 | */ |
| 2074 | void |
| 2075 | xmlUnlinkNode(xmlNodePtr cur) { |
| 2076 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2077 | #ifdef DEBUG_TREE |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2078 | fprintf(stderr, "xmlUnlinkNode : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2079 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2080 | return; |
| 2081 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2082 | if ((cur->parent != NULL) && (cur->parent->children == cur)) |
| 2083 | cur->parent->children = cur->next; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2084 | if ((cur->parent != NULL) && (cur->parent->last == cur)) |
| 2085 | cur->parent->last = cur->prev; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2086 | if (cur->next != NULL) |
| 2087 | cur->next->prev = cur->prev; |
| 2088 | if (cur->prev != NULL) |
| 2089 | cur->prev->next = cur->next; |
| 2090 | cur->next = cur->prev = NULL; |
| 2091 | cur->parent = NULL; |
| 2092 | } |
| 2093 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2094 | /** |
| 2095 | * xmlReplaceNode: |
| 2096 | * @old: the old node |
| 2097 | * @cur: the node |
| 2098 | * |
| 2099 | * Unlink the old node from it's current context, prune the new one |
| 2100 | * at the same place. If cur was already inserted in a document it is |
| 2101 | * first unlinked from its existing context. |
| 2102 | * |
| 2103 | * Returns the old node |
| 2104 | */ |
| 2105 | xmlNodePtr |
| 2106 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
| 2107 | if (old == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2108 | #ifdef DEBUG_TREE |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2109 | fprintf(stderr, "xmlReplaceNode : old == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2110 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2111 | return(NULL); |
| 2112 | } |
| 2113 | if (cur == NULL) { |
| 2114 | xmlUnlinkNode(old); |
| 2115 | return(old); |
| 2116 | } |
| 2117 | xmlUnlinkNode(cur); |
| 2118 | cur->doc = old->doc; |
| 2119 | cur->parent = old->parent; |
| 2120 | cur->next = old->next; |
| 2121 | if (cur->next != NULL) |
| 2122 | cur->next->prev = cur; |
| 2123 | cur->prev = old->prev; |
| 2124 | if (cur->prev != NULL) |
| 2125 | cur->prev->next = cur; |
| 2126 | if (cur->parent != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2127 | if (cur->parent->children == old) |
| 2128 | cur->parent->children = cur; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2129 | if (cur->parent->last == old) |
| 2130 | cur->parent->last = cur; |
| 2131 | } |
| 2132 | old->next = old->prev = NULL; |
| 2133 | old->parent = NULL; |
| 2134 | return(old); |
| 2135 | } |
| 2136 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2137 | /************************************************************************ |
| 2138 | * * |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2139 | * Copy operations * |
| 2140 | * * |
| 2141 | ************************************************************************/ |
| 2142 | |
| 2143 | /** |
| 2144 | * xmlCopyNamespace: |
| 2145 | * @cur: the namespace |
| 2146 | * |
| 2147 | * Do a copy of the namespace. |
| 2148 | * |
| 2149 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 2150 | */ |
| 2151 | xmlNsPtr |
| 2152 | xmlCopyNamespace(xmlNsPtr cur) { |
| 2153 | xmlNsPtr ret; |
| 2154 | |
| 2155 | if (cur == NULL) return(NULL); |
| 2156 | switch (cur->type) { |
| 2157 | case XML_GLOBAL_NAMESPACE: |
| 2158 | ret = xmlNewGlobalNs(NULL, cur->href, cur->prefix); |
| 2159 | break; |
| 2160 | case XML_LOCAL_NAMESPACE: |
| 2161 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 2162 | break; |
| 2163 | default: |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2164 | #ifdef DEBUG_TREE |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2165 | fprintf(stderr, "xmlCopyNamespace: unknown type %d\n", cur->type); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2166 | #endif |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2167 | return(NULL); |
| 2168 | } |
| 2169 | return(ret); |
| 2170 | } |
| 2171 | |
| 2172 | /** |
| 2173 | * xmlCopyNamespaceList: |
| 2174 | * @cur: the first namespace |
| 2175 | * |
| 2176 | * Do a copy of an namespace list. |
| 2177 | * |
| 2178 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 2179 | */ |
| 2180 | xmlNsPtr |
| 2181 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 2182 | xmlNsPtr ret = NULL; |
| 2183 | xmlNsPtr p = NULL,q; |
| 2184 | |
| 2185 | while (cur != NULL) { |
| 2186 | q = xmlCopyNamespace(cur); |
| 2187 | if (p == NULL) { |
| 2188 | ret = p = q; |
| 2189 | } else { |
| 2190 | p->next = q; |
| 2191 | p = q; |
| 2192 | } |
| 2193 | cur = cur->next; |
| 2194 | } |
| 2195 | return(ret); |
| 2196 | } |
| 2197 | |
| 2198 | /** |
| 2199 | * xmlCopyProp: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2200 | * @target: the element where the attribute will be grafted |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2201 | * @cur: the attribute |
| 2202 | * |
| 2203 | * Do a copy of the attribute. |
| 2204 | * |
| 2205 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 2206 | */ |
| 2207 | xmlAttrPtr |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2208 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2209 | xmlAttrPtr ret; |
| 2210 | |
| 2211 | if (cur == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2212 | if (cur->parent != NULL) |
| 2213 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 2214 | else if (cur->children != NULL) |
| 2215 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2216 | else |
| 2217 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 2218 | if (ret == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2219 | ret->parent = target; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2220 | |
| 2221 | if ((cur->ns != NULL) && (target != NULL)) { |
| 2222 | xmlNsPtr ns; |
| 2223 | |
| 2224 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 2225 | ret->ns = ns; |
| 2226 | } else |
| 2227 | ret->ns = NULL; |
| 2228 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2229 | if (cur->children != NULL) |
| 2230 | ret->children = xmlCopyNodeList(cur->children); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2231 | return(ret); |
| 2232 | } |
| 2233 | |
| 2234 | /** |
| 2235 | * xmlCopyPropList: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2236 | * @target: the element where the attributes will be grafted |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2237 | * @cur: the first attribute |
| 2238 | * |
| 2239 | * Do a copy of an attribute list. |
| 2240 | * |
| 2241 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 2242 | */ |
| 2243 | xmlAttrPtr |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2244 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2245 | xmlAttrPtr ret = NULL; |
| 2246 | xmlAttrPtr p = NULL,q; |
| 2247 | |
| 2248 | while (cur != NULL) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2249 | q = xmlCopyProp(target, cur); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2250 | if (p == NULL) { |
| 2251 | ret = p = q; |
| 2252 | } else { |
| 2253 | p->next = q; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2254 | q->prev = p; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2255 | p = q; |
| 2256 | } |
| 2257 | cur = cur->next; |
| 2258 | } |
| 2259 | return(ret); |
| 2260 | } |
| 2261 | |
| 2262 | /* |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 2263 | * NOTE abeut the CopyNode operations ! |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2264 | * |
| 2265 | * They are splitted into external and internal parts for one |
| 2266 | * tricky reason: namespaces. Doing a direct copy of a node |
| 2267 | * say RPM:Copyright without changing the namespace pointer to |
| 2268 | * something else can produce stale links. One way to do it is |
| 2269 | * to keep a reference counter but this doesn't work as soon |
| 2270 | * as one move the element or the subtree out of the scope of |
| 2271 | * the existing namespace. The actual solution seems to add |
| 2272 | * a copy of the namespace at the top of the copied tree if |
| 2273 | * not available in the subtree. |
| 2274 | * Hence two functions, the public front-end call the inner ones |
| 2275 | */ |
| 2276 | |
| 2277 | static xmlNodePtr |
| 2278 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
| 2279 | |
| 2280 | static xmlNodePtr |
| 2281 | xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
| 2282 | int recursive) { |
| 2283 | xmlNodePtr ret; |
| 2284 | |
| 2285 | if (node == NULL) return(NULL); |
| 2286 | /* |
| 2287 | * Allocate a new node and fill the fields. |
| 2288 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2289 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2290 | if (ret == NULL) { |
| 2291 | fprintf(stderr, "xmlStaticCopyNode : malloc failed\n"); |
| 2292 | return(NULL); |
| 2293 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2294 | memset(ret, 0, sizeof(xmlNode)); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2295 | ret->type = node->type; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2296 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2297 | ret->doc = doc; |
| 2298 | ret->parent = parent; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2299 | if (node->name != NULL) |
| 2300 | ret->name = xmlStrdup(node->name); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2301 | if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) { |
| 2302 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2303 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2304 | #else |
| 2305 | ret->content = xmlBufferCreateSize(xmlBufferLength(node->content)); |
| 2306 | xmlBufferSetAllocationScheme(ret->content, |
| 2307 | xmlGetBufferAllocationScheme()); |
| 2308 | xmlBufferAdd(ret->content, |
| 2309 | xmlBufferContent(node->content), |
| 2310 | xmlBufferLength(node->content)); |
| 2311 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2312 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2313 | if (parent != NULL) |
| 2314 | xmlAddChild(parent, ret); |
| 2315 | |
| 2316 | if (!recursive) return(ret); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2317 | if (node->nsDef != NULL) |
| 2318 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 2319 | |
| 2320 | if (node->ns != NULL) { |
| 2321 | xmlNsPtr ns; |
| 2322 | |
| 2323 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 2324 | if (ns == NULL) { |
| 2325 | /* |
| 2326 | * Humm, we are copying an element whose namespace is defined |
| 2327 | * out of the new tree scope. Search it in the original tree |
| 2328 | * and add it at the top of the new tree |
| 2329 | */ |
| 2330 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 2331 | if (ns != NULL) { |
| 2332 | xmlNodePtr root = ret; |
| 2333 | |
| 2334 | while (root->parent != NULL) root = root->parent; |
| 2335 | xmlNewNs(root, ns->href, ns->prefix); |
| 2336 | } |
| 2337 | } else { |
| 2338 | /* |
| 2339 | * reference the existing namespace definition in our own tree. |
| 2340 | */ |
| 2341 | ret->ns = ns; |
| 2342 | } |
| 2343 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2344 | if (node->properties != NULL) |
| 2345 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2346 | if (node->children != NULL) |
| 2347 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2348 | UPDATE_LAST_CHILD(ret) |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2349 | return(ret); |
| 2350 | } |
| 2351 | |
| 2352 | static xmlNodePtr |
| 2353 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 2354 | xmlNodePtr ret = NULL; |
| 2355 | xmlNodePtr p = NULL,q; |
| 2356 | |
| 2357 | while (node != NULL) { |
| 2358 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 2359 | if (ret == NULL) { |
| 2360 | q->prev = NULL; |
| 2361 | ret = p = q; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2362 | } else { |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 2363 | p->next = q; |
| 2364 | q->prev = p; |
| 2365 | p = q; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2366 | } |
| 2367 | node = node->next; |
| 2368 | } |
| 2369 | return(ret); |
| 2370 | } |
| 2371 | |
| 2372 | /** |
| 2373 | * xmlCopyNode: |
| 2374 | * @node: the node |
| 2375 | * @recursive: if 1 do a recursive copy. |
| 2376 | * |
| 2377 | * Do a copy of the node. |
| 2378 | * |
| 2379 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 2380 | */ |
| 2381 | xmlNodePtr |
| 2382 | xmlCopyNode(xmlNodePtr node, int recursive) { |
| 2383 | xmlNodePtr ret; |
| 2384 | |
| 2385 | ret = xmlStaticCopyNode(node, NULL, NULL, recursive); |
| 2386 | return(ret); |
| 2387 | } |
| 2388 | |
| 2389 | /** |
| 2390 | * xmlCopyNodeList: |
| 2391 | * @node: the first node in the list. |
| 2392 | * |
| 2393 | * Do a recursive copy of the node list. |
| 2394 | * |
| 2395 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 2396 | */ |
| 2397 | xmlNodePtr xmlCopyNodeList(xmlNodePtr node) { |
| 2398 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 2399 | return(ret); |
| 2400 | } |
| 2401 | |
| 2402 | /** |
| 2403 | * xmlCopyElement: |
| 2404 | * @elem: the element |
| 2405 | * |
| 2406 | * Do a copy of the element definition. |
| 2407 | * |
| 2408 | * Returns: a new xmlElementPtr, or NULL in case of error. |
| 2409 | xmlElementPtr |
| 2410 | xmlCopyElement(xmlElementPtr elem) { |
| 2411 | xmlElementPtr ret; |
| 2412 | |
| 2413 | if (elem == NULL) return(NULL); |
| 2414 | ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content); |
| 2415 | if (ret == NULL) return(NULL); |
| 2416 | if (!recursive) return(ret); |
| 2417 | if (elem->properties != NULL) |
| 2418 | ret->properties = xmlCopyPropList(elem->properties); |
| 2419 | |
| 2420 | if (elem->nsDef != NULL) |
| 2421 | ret->nsDef = xmlCopyNamespaceList(elem->nsDef); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2422 | if (elem->children != NULL) |
| 2423 | ret->children = xmlCopyElementList(elem->children); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2424 | return(ret); |
| 2425 | } |
| 2426 | */ |
| 2427 | |
| 2428 | /** |
| 2429 | * xmlCopyDtd: |
| 2430 | * @dtd: the dtd |
| 2431 | * |
| 2432 | * Do a copy of the dtd. |
| 2433 | * |
| 2434 | * Returns: a new xmlDtdPtr, or NULL in case of error. |
| 2435 | */ |
| 2436 | xmlDtdPtr |
| 2437 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 2438 | xmlDtdPtr ret; |
| 2439 | |
| 2440 | if (dtd == NULL) return(NULL); |
| 2441 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 2442 | if (ret == NULL) return(NULL); |
| 2443 | if (dtd->entities != NULL) |
| 2444 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 2445 | (xmlEntitiesTablePtr) dtd->entities); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2446 | if (dtd->notations != NULL) |
| 2447 | ret->notations = (void *) xmlCopyNotationTable( |
| 2448 | (xmlNotationTablePtr) dtd->notations); |
| 2449 | if (dtd->elements != NULL) |
| 2450 | ret->elements = (void *) xmlCopyElementTable( |
| 2451 | (xmlElementTablePtr) dtd->elements); |
| 2452 | if (dtd->attributes != NULL) |
| 2453 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 2454 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2455 | return(ret); |
| 2456 | } |
| 2457 | |
| 2458 | /** |
| 2459 | * xmlCopyDoc: |
| 2460 | * @doc: the document |
| 2461 | * @recursive: if 1 do a recursive copy. |
| 2462 | * |
| 2463 | * Do a copy of the document info. If recursive, the content tree will |
| 2464 | * be copied too as well as Dtd, namespaces and entities. |
| 2465 | * |
| 2466 | * Returns: a new xmlDocPtr, or NULL in case of error. |
| 2467 | */ |
| 2468 | xmlDocPtr |
| 2469 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 2470 | xmlDocPtr ret; |
| 2471 | |
| 2472 | if (doc == NULL) return(NULL); |
| 2473 | ret = xmlNewDoc(doc->version); |
| 2474 | if (ret == NULL) return(NULL); |
| 2475 | if (doc->name != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2476 | ret->name = xmlMemStrdup(doc->name); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2477 | if (doc->encoding != NULL) |
| 2478 | ret->encoding = xmlStrdup(doc->encoding); |
| 2479 | ret->compression = doc->compression; |
| 2480 | ret->standalone = doc->standalone; |
| 2481 | if (!recursive) return(ret); |
| 2482 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2483 | if (doc->intSubset != NULL) |
| 2484 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2485 | if (doc->oldNs != NULL) |
| 2486 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2487 | if (doc->children != NULL) |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 2488 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 2489 | (xmlNodePtr)ret); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2490 | return(ret); |
| 2491 | } |
| 2492 | |
| 2493 | /************************************************************************ |
| 2494 | * * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2495 | * Content access functions * |
| 2496 | * * |
| 2497 | ************************************************************************/ |
| 2498 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2499 | /** |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 2500 | * xmlDocGetRootElement: |
| 2501 | * @doc: the document |
| 2502 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2503 | * Get the root element of the document (doc->children is a list |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 2504 | * containing possibly comments, PIs, etc ...). |
| 2505 | * |
| 2506 | * Returns the xmlNodePtr for the root or NULL |
| 2507 | */ |
| 2508 | xmlNodePtr |
| 2509 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 2510 | xmlNodePtr ret; |
| 2511 | |
| 2512 | if (doc == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2513 | ret = doc->children; |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 2514 | while (ret != NULL) { |
| 2515 | if (ret->type == XML_ELEMENT_NODE) |
| 2516 | return(ret); |
| 2517 | ret = ret->next; |
| 2518 | } |
| 2519 | return(ret); |
| 2520 | } |
| 2521 | |
| 2522 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2523 | * xmlDocSetRootElement: |
| 2524 | * @doc: the document |
| 2525 | * @root: the new document root element |
| 2526 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2527 | * Set the root element of the document (doc->children is a list |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2528 | * containing possibly comments, PIs, etc ...). |
| 2529 | * |
| 2530 | * Returns the old root element if any was found |
| 2531 | */ |
| 2532 | xmlNodePtr |
| 2533 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 2534 | xmlNodePtr old = NULL; |
| 2535 | |
| 2536 | if (doc == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2537 | old = doc->children; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2538 | while (old != NULL) { |
| 2539 | if (old->type == XML_ELEMENT_NODE) |
| 2540 | break; |
| 2541 | old = old->next; |
| 2542 | } |
| 2543 | if (old == NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2544 | if (doc->children == NULL) { |
| 2545 | doc->children = root; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2546 | } else { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2547 | xmlAddSibling(doc->children, root); |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2548 | } |
| 2549 | } else { |
| 2550 | xmlReplaceNode(old, root); |
| 2551 | } |
| 2552 | return(old); |
| 2553 | } |
| 2554 | |
| 2555 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2556 | * xmlNodeSetLang: |
| 2557 | * @cur: the node being changed |
| 2558 | * @lang: the langage description |
| 2559 | * |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2560 | * Set the language of a node, i.e. the values of the xml:lang |
| 2561 | * attribute. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2562 | */ |
| 2563 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2564 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2565 | if (cur == NULL) return; |
| 2566 | switch(cur->type) { |
| 2567 | case XML_TEXT_NODE: |
| 2568 | case XML_CDATA_SECTION_NODE: |
| 2569 | case XML_COMMENT_NODE: |
| 2570 | case XML_DOCUMENT_NODE: |
| 2571 | case XML_DOCUMENT_TYPE_NODE: |
| 2572 | case XML_DOCUMENT_FRAG_NODE: |
| 2573 | case XML_NOTATION_NODE: |
| 2574 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2575 | case XML_DTD_NODE: |
| 2576 | case XML_ELEMENT_DECL: |
| 2577 | case XML_ATTRIBUTE_DECL: |
| 2578 | case XML_ENTITY_DECL: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2579 | return; |
| 2580 | case XML_ELEMENT_NODE: |
| 2581 | case XML_ATTRIBUTE_NODE: |
| 2582 | case XML_PI_NODE: |
| 2583 | case XML_ENTITY_REF_NODE: |
| 2584 | case XML_ENTITY_NODE: |
| 2585 | break; |
| 2586 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2587 | xmlSetProp(cur, BAD_CAST "xml:lang", lang); |
| 2588 | } |
| 2589 | |
| 2590 | /** |
| 2591 | * xmlNodeGetLang: |
| 2592 | * @cur: the node being checked |
| 2593 | * |
| 2594 | * Searches the language of a node, i.e. the values of the xml:lang |
| 2595 | * attribute or the one carried by the nearest ancestor. |
| 2596 | * |
| 2597 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2598 | * It's up to the caller to free the memory. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2599 | */ |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2600 | xmlChar * |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2601 | xmlNodeGetLang(xmlNodePtr cur) { |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2602 | xmlChar *lang; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2603 | |
| 2604 | while (cur != NULL) { |
| 2605 | lang = xmlGetProp(cur, BAD_CAST "xml:lang"); |
| 2606 | if (lang != NULL) |
| 2607 | return(lang); |
| 2608 | cur = cur->parent; |
| 2609 | } |
| 2610 | return(NULL); |
| 2611 | } |
| 2612 | |
| 2613 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2614 | * xmlNodeGetSpacePreserve: |
| 2615 | * @cur: the node being checked |
| 2616 | * |
| 2617 | * Searches the language of a node, i.e. the values of the xml:space |
| 2618 | * attribute or the one carried by the nearest ancestor. |
| 2619 | * |
| 2620 | * Returns -1 if xml:space is not inheried, 0 if "default", 1 if "preserve" |
| 2621 | */ |
| 2622 | int |
| 2623 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 2624 | xmlChar *space; |
| 2625 | |
| 2626 | while (cur != NULL) { |
| 2627 | space = xmlGetProp(cur, BAD_CAST "xml:space"); |
| 2628 | if (space != NULL) { |
| 2629 | if (!xmlStrcmp(space, BAD_CAST "preserve")) { |
| 2630 | xmlFree(space); |
| 2631 | return(1); |
| 2632 | } |
| 2633 | if (!xmlStrcmp(space, BAD_CAST "default")) { |
| 2634 | xmlFree(space); |
| 2635 | return(0); |
| 2636 | } |
| 2637 | xmlFree(space); |
| 2638 | } |
| 2639 | cur = cur->parent; |
| 2640 | } |
| 2641 | return(-1); |
| 2642 | } |
| 2643 | |
| 2644 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2645 | * xmlNodeSetName: |
| 2646 | * @cur: the node being changed |
| 2647 | * @name: the new tag name |
| 2648 | * |
| 2649 | * Searches the language of a node, i.e. the values of the xml:lang |
| 2650 | * attribute or the one carried by the nearest ancestor. |
| 2651 | */ |
| 2652 | void |
| 2653 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
| 2654 | if (cur == NULL) return; |
| 2655 | if (name == NULL) return; |
| 2656 | switch(cur->type) { |
| 2657 | case XML_TEXT_NODE: |
| 2658 | case XML_CDATA_SECTION_NODE: |
| 2659 | case XML_COMMENT_NODE: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2660 | case XML_DOCUMENT_TYPE_NODE: |
| 2661 | case XML_DOCUMENT_FRAG_NODE: |
| 2662 | case XML_NOTATION_NODE: |
| 2663 | case XML_HTML_DOCUMENT_NODE: |
| 2664 | return; |
| 2665 | case XML_ELEMENT_NODE: |
| 2666 | case XML_ATTRIBUTE_NODE: |
| 2667 | case XML_PI_NODE: |
| 2668 | case XML_ENTITY_REF_NODE: |
| 2669 | case XML_ENTITY_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2670 | case XML_DTD_NODE: |
| 2671 | case XML_DOCUMENT_NODE: |
| 2672 | case XML_ELEMENT_DECL: |
| 2673 | case XML_ATTRIBUTE_DECL: |
| 2674 | case XML_ENTITY_DECL: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2675 | break; |
| 2676 | } |
| 2677 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 2678 | cur->name = xmlStrdup(name); |
| 2679 | } |
| 2680 | |
| 2681 | /** |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 2682 | * xmlNodeGetBase: |
| 2683 | * @doc: the document the node pertains to |
| 2684 | * @cur: the node being checked |
| 2685 | * |
| 2686 | * Searches for the BASE URL. The code should work on both XML |
| 2687 | * and HTML document even if base mechanisms are completely different. |
| 2688 | * |
| 2689 | * Returns a pointer to the base URL, or NULL if not found |
| 2690 | * It's up to the caller to free the memory. |
| 2691 | */ |
| 2692 | xmlChar * |
| 2693 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
| 2694 | xmlChar *base; |
| 2695 | |
| 2696 | if ((cur == NULL) && (doc == NULL)) |
| 2697 | return(NULL); |
| 2698 | if (doc == NULL) doc = cur->doc; |
| 2699 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2700 | cur = doc->children; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 2701 | while ((cur != NULL) && (cur->name != NULL)) { |
| 2702 | if (cur->type != XML_ELEMENT_NODE) { |
| 2703 | cur = cur->next; |
| 2704 | continue; |
| 2705 | } |
| 2706 | if ((!xmlStrcmp(cur->name, BAD_CAST "html")) || |
| 2707 | (!xmlStrcmp(cur->name, BAD_CAST "HTML"))) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2708 | cur = cur->children; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 2709 | continue; |
| 2710 | } |
| 2711 | if ((!xmlStrcmp(cur->name, BAD_CAST "head")) || |
| 2712 | (!xmlStrcmp(cur->name, BAD_CAST "HEAD"))) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2713 | cur = cur->children; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 2714 | continue; |
| 2715 | } |
| 2716 | if ((!xmlStrcmp(cur->name, BAD_CAST "base")) || |
| 2717 | (!xmlStrcmp(cur->name, BAD_CAST "BASE"))) { |
| 2718 | base = xmlGetProp(cur, BAD_CAST "href"); |
| 2719 | if (base != NULL) return(base); |
| 2720 | return(xmlGetProp(cur, BAD_CAST "HREF")); |
| 2721 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2722 | cur = cur->next; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 2723 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2724 | if ((doc != NULL) && (doc->URL != NULL)) |
| 2725 | return(xmlStrdup(doc->URL)); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 2726 | return(NULL); |
| 2727 | } |
| 2728 | while (cur != NULL) { |
| 2729 | base = xmlGetProp(cur, BAD_CAST "xml:base"); |
| 2730 | if (base != NULL) |
| 2731 | return(base); |
| 2732 | cur = cur->parent; |
| 2733 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2734 | if ((doc != NULL) && (doc->URL != NULL)) |
| 2735 | return(xmlStrdup(doc->URL)); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 2736 | return(NULL); |
| 2737 | } |
| 2738 | |
| 2739 | /** |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2740 | * xmlNodeGetContent: |
| 2741 | * @cur: the node being read |
| 2742 | * |
| 2743 | * Read the value of a node, this can be either the text carried |
| 2744 | * directly by this node if it's a TEXT node or the aggregate string |
| 2745 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 2746 | * Entity references are substitued. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2747 | * Returns a new xmlChar * or NULL if no content is available. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 2748 | * It's up to the caller to free the memory. |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2749 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2750 | xmlChar * |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2751 | xmlNodeGetContent(xmlNodePtr cur) { |
| 2752 | if (cur == NULL) return(NULL); |
| 2753 | switch (cur->type) { |
| 2754 | case XML_DOCUMENT_FRAG_NODE: |
| 2755 | case XML_ELEMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2756 | return(xmlNodeListGetString(cur->doc, cur->children, 1)); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2757 | break; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2758 | case XML_ATTRIBUTE_NODE: { |
| 2759 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2760 | if (attr->parent != NULL) |
| 2761 | return(xmlNodeListGetString(attr->parent->doc, attr->children, 1)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2762 | else |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2763 | return(xmlNodeListGetString(NULL, attr->children, 1)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2764 | break; |
| 2765 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 2766 | case XML_COMMENT_NODE: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2767 | case XML_PI_NODE: |
| 2768 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2769 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2770 | return(xmlStrdup(cur->content)); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2771 | #else |
| 2772 | return(xmlStrdup(xmlBufferContent(cur->content))); |
| 2773 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2774 | return(NULL); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2775 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 2776 | /* |
| 2777 | * Locate the entity, and get it's content |
| 2778 | * @@@ |
| 2779 | */ |
| 2780 | return(NULL); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2781 | case XML_ENTITY_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2782 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 2783 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2784 | case XML_DOCUMENT_TYPE_NODE: |
| 2785 | case XML_NOTATION_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2786 | case XML_DTD_NODE: |
| 2787 | return(NULL); |
| 2788 | case XML_ELEMENT_DECL: |
| 2789 | /* TODO !!! */ |
| 2790 | return(NULL); |
| 2791 | case XML_ATTRIBUTE_DECL: |
| 2792 | /* TODO !!! */ |
| 2793 | return(NULL); |
| 2794 | case XML_ENTITY_DECL: |
| 2795 | /* TODO !!! */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2796 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2797 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2798 | case XML_TEXT_NODE: |
| 2799 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2800 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2801 | return(xmlStrdup(cur->content)); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2802 | #else |
| 2803 | return(xmlStrdup(xmlBufferContent(cur->content))); |
| 2804 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2805 | return(NULL); |
| 2806 | } |
| 2807 | return(NULL); |
| 2808 | } |
| 2809 | |
| 2810 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2811 | * xmlNodeSetContent: |
| 2812 | * @cur: the node being modified |
| 2813 | * @content: the new value of the content |
| 2814 | * |
| 2815 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2816 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2817 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2818 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2819 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2820 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2821 | fprintf(stderr, "xmlNodeSetContent : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2822 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2823 | return; |
| 2824 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2825 | switch (cur->type) { |
| 2826 | case XML_DOCUMENT_FRAG_NODE: |
| 2827 | case XML_ELEMENT_NODE: |
| 2828 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2829 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2830 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2831 | #else |
| 2832 | xmlBufferFree(cur->content); |
| 2833 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2834 | cur->content = NULL; |
| 2835 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2836 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 2837 | cur->children = xmlStringGetNodeList(cur->doc, content); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2838 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2839 | break; |
| 2840 | case XML_ATTRIBUTE_NODE: |
| 2841 | break; |
| 2842 | case XML_TEXT_NODE: |
| 2843 | case XML_CDATA_SECTION_NODE: |
| 2844 | case XML_ENTITY_REF_NODE: |
| 2845 | case XML_ENTITY_NODE: |
| 2846 | case XML_PI_NODE: |
| 2847 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2848 | if (cur->content != NULL) { |
| 2849 | #ifndef XML_USE_BUFFER_CONTENT |
| 2850 | xmlFree(cur->content); |
| 2851 | #else |
| 2852 | xmlBufferFree(cur->content); |
| 2853 | #endif |
| 2854 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2855 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 2856 | cur->last = cur->children = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2857 | if (content != NULL) { |
| 2858 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2859 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2860 | #else |
| 2861 | cur->content = xmlBufferCreateSize(0); |
| 2862 | xmlBufferSetAllocationScheme(cur->content, |
| 2863 | xmlGetBufferAllocationScheme()); |
| 2864 | xmlBufferAdd(cur->content, content, -1); |
| 2865 | #endif |
| 2866 | } else |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2867 | cur->content = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2868 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2869 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 2870 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2871 | case XML_DOCUMENT_TYPE_NODE: |
| 2872 | break; |
| 2873 | case XML_NOTATION_NODE: |
| 2874 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2875 | case XML_DTD_NODE: |
| 2876 | break; |
| 2877 | case XML_ELEMENT_DECL: |
| 2878 | /* TODO !!! */ |
| 2879 | break; |
| 2880 | case XML_ATTRIBUTE_DECL: |
| 2881 | /* TODO !!! */ |
| 2882 | break; |
| 2883 | case XML_ENTITY_DECL: |
| 2884 | /* TODO !!! */ |
| 2885 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2886 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2889 | /** |
| 2890 | * xmlNodeSetContentLen: |
| 2891 | * @cur: the node being modified |
| 2892 | * @content: the new value of the content |
| 2893 | * @len: the size of @content |
| 2894 | * |
| 2895 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2896 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2897 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2898 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2899 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2900 | #ifdef DEBUG_TREE |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2901 | fprintf(stderr, "xmlNodeSetContentLen : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2902 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2903 | return; |
| 2904 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2905 | switch (cur->type) { |
| 2906 | case XML_DOCUMENT_FRAG_NODE: |
| 2907 | case XML_ELEMENT_NODE: |
| 2908 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2909 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2910 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2911 | #else |
| 2912 | xmlBufferFree(cur->content); |
| 2913 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2914 | cur->content = NULL; |
| 2915 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2916 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 2917 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2918 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2919 | break; |
| 2920 | case XML_ATTRIBUTE_NODE: |
| 2921 | break; |
| 2922 | case XML_TEXT_NODE: |
| 2923 | case XML_CDATA_SECTION_NODE: |
| 2924 | case XML_ENTITY_REF_NODE: |
| 2925 | case XML_ENTITY_NODE: |
| 2926 | case XML_PI_NODE: |
| 2927 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2928 | case XML_NOTATION_NODE: |
| 2929 | if (cur->content != NULL) { |
| 2930 | #ifndef XML_USE_BUFFER_CONTENT |
| 2931 | xmlFree(cur->content); |
| 2932 | #else |
| 2933 | xmlBufferFree(cur->content); |
| 2934 | #endif |
| 2935 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2936 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 2937 | cur->children = cur->last = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2938 | if (content != NULL) { |
| 2939 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2940 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2941 | #else |
| 2942 | cur->content = xmlBufferCreateSize(len); |
| 2943 | xmlBufferSetAllocationScheme(cur->content, |
| 2944 | xmlGetBufferAllocationScheme()); |
| 2945 | xmlBufferAdd(cur->content, content, len); |
| 2946 | #endif |
| 2947 | } else |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2948 | cur->content = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2949 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2950 | case XML_DOCUMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2951 | case XML_DTD_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 2952 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2953 | case XML_DOCUMENT_TYPE_NODE: |
| 2954 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2955 | case XML_ELEMENT_DECL: |
| 2956 | /* TODO !!! */ |
| 2957 | break; |
| 2958 | case XML_ATTRIBUTE_DECL: |
| 2959 | /* TODO !!! */ |
| 2960 | break; |
| 2961 | case XML_ENTITY_DECL: |
| 2962 | /* TODO !!! */ |
| 2963 | break; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2964 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2967 | /** |
| 2968 | * xmlNodeAddContentLen: |
| 2969 | * @cur: the node being modified |
| 2970 | * @content: extra content |
| 2971 | * @len: the size of @content |
| 2972 | * |
| 2973 | * Append the extra substring to the node content. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2974 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2975 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2976 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2977 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2978 | #ifdef DEBUG_TREE |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2979 | fprintf(stderr, "xmlNodeAddContentLen : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2980 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2981 | return; |
| 2982 | } |
| 2983 | if (len <= 0) return; |
| 2984 | switch (cur->type) { |
| 2985 | case XML_DOCUMENT_FRAG_NODE: |
| 2986 | case XML_ELEMENT_NODE: { |
| 2987 | xmlNodePtr last = NULL, new; |
| 2988 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2989 | if (cur->children != NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2990 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2991 | } else { |
| 2992 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2993 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2994 | cur->children = xmlStringGetNodeList(cur->doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2995 | #else |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2996 | cur->children = xmlStringGetNodeList(cur->doc, |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2997 | xmlBufferContent(cur->content)); |
| 2998 | #endif |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2999 | UPDATE_LAST_CHILD(cur) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3000 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3001 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3002 | #else |
| 3003 | xmlBufferFree(cur->content); |
| 3004 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3005 | cur->content = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3006 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3007 | } |
| 3008 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3009 | new = xmlNewTextLen(content, len); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3010 | if (new != NULL) { |
| 3011 | xmlAddChild(cur, new); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3012 | if ((last != NULL) && (last->next == new)) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3013 | xmlTextMerge(last, new); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3014 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3015 | } |
| 3016 | break; |
| 3017 | } |
| 3018 | case XML_ATTRIBUTE_NODE: |
| 3019 | break; |
| 3020 | case XML_TEXT_NODE: |
| 3021 | case XML_CDATA_SECTION_NODE: |
| 3022 | case XML_ENTITY_REF_NODE: |
| 3023 | case XML_ENTITY_NODE: |
| 3024 | case XML_PI_NODE: |
| 3025 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3026 | case XML_NOTATION_NODE: |
| 3027 | if (content != NULL) { |
| 3028 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3029 | cur->content = xmlStrncat(cur->content, content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3030 | #else |
| 3031 | xmlBufferAdd(cur->content, content, len); |
| 3032 | #endif |
| 3033 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3034 | case XML_DOCUMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3035 | case XML_DTD_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3036 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3037 | case XML_DOCUMENT_TYPE_NODE: |
| 3038 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3039 | case XML_ELEMENT_DECL: |
| 3040 | case XML_ATTRIBUTE_DECL: |
| 3041 | case XML_ENTITY_DECL: |
| 3042 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3043 | } |
| 3044 | } |
| 3045 | |
| 3046 | /** |
| 3047 | * xmlNodeAddContent: |
| 3048 | * @cur: the node being modified |
| 3049 | * @content: extra content |
| 3050 | * |
| 3051 | * Append the extra substring to the node content. |
| 3052 | */ |
| 3053 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3054 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3055 | int len; |
| 3056 | |
| 3057 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3058 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3059 | fprintf(stderr, "xmlNodeAddContent : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3060 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3061 | return; |
| 3062 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3063 | if (content == NULL) return; |
| 3064 | len = xmlStrlen(content); |
| 3065 | xmlNodeAddContentLen(cur, content, len); |
| 3066 | } |
| 3067 | |
| 3068 | /** |
| 3069 | * xmlTextMerge: |
| 3070 | * @first: the first text node |
| 3071 | * @second: the second text node being merged |
| 3072 | * |
| 3073 | * Merge two text nodes into one |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3074 | * Returns the first text node augmented |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3075 | */ |
| 3076 | xmlNodePtr |
| 3077 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 3078 | if (first == NULL) return(second); |
| 3079 | if (second == NULL) return(first); |
| 3080 | if (first->type != XML_TEXT_NODE) return(first); |
| 3081 | if (second->type != XML_TEXT_NODE) return(first); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3082 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3083 | xmlNodeAddContent(first, second->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3084 | #else |
| 3085 | xmlNodeAddContent(first, xmlBufferContent(second->content)); |
| 3086 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3087 | xmlUnlinkNode(second); |
| 3088 | xmlFreeNode(second); |
| 3089 | return(first); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3092 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3093 | * xmlGetNsList: |
| 3094 | * @doc: the document |
| 3095 | * @node: the current node |
| 3096 | * |
| 3097 | * Search all the namespace applying to a given element. |
| 3098 | * Returns an NULL terminated array of all the xmlNsPtr found |
| 3099 | * that need to be freed by the caller or NULL if no |
| 3100 | * namespace if defined |
| 3101 | */ |
| 3102 | xmlNsPtr * |
| 3103 | xmlGetNsList(xmlDocPtr doc, xmlNodePtr node) { |
| 3104 | xmlNsPtr cur; |
| 3105 | xmlNsPtr *ret = NULL; |
| 3106 | int nbns = 0; |
| 3107 | int maxns = 10; |
| 3108 | int i; |
| 3109 | |
| 3110 | while (node != NULL) { |
| 3111 | cur = node->nsDef; |
| 3112 | while (cur != NULL) { |
| 3113 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3114 | ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3115 | if (ret == NULL) { |
| 3116 | fprintf(stderr, "xmlGetNsList : out of memory!\n"); |
| 3117 | return(NULL); |
| 3118 | } |
| 3119 | ret[nbns] = NULL; |
| 3120 | } |
| 3121 | for (i = 0;i < nbns;i++) { |
| 3122 | if ((cur->prefix == ret[i]->prefix) || |
| 3123 | (!xmlStrcmp(cur->prefix, ret[i]->prefix))) break; |
| 3124 | } |
| 3125 | if (i >= nbns) { |
| 3126 | if (nbns >= maxns) { |
| 3127 | maxns *= 2; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3128 | ret = (xmlNsPtr *) xmlRealloc(ret, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3129 | (maxns + 1) * sizeof(xmlNsPtr)); |
| 3130 | if (ret == NULL) { |
| 3131 | fprintf(stderr, "xmlGetNsList : realloc failed!\n"); |
| 3132 | return(NULL); |
| 3133 | } |
| 3134 | } |
| 3135 | ret[nbns++] = cur; |
| 3136 | ret[nbns] = NULL; |
| 3137 | } |
| 3138 | |
| 3139 | cur = cur->next; |
| 3140 | } |
| 3141 | node = node->parent; |
| 3142 | } |
| 3143 | return(ret); |
| 3144 | } |
| 3145 | |
| 3146 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3147 | * xmlSearchNs: |
| 3148 | * @doc: the document |
| 3149 | * @node: the current node |
| 3150 | * @nameSpace: the namespace string |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3151 | * |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3152 | * Search a Ns registered under a given name space for a document. |
| 3153 | * recurse on the parents until it finds the defined namespace |
| 3154 | * or return NULL otherwise. |
| 3155 | * @nameSpace can be NULL, this is a search for the default namespace. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3156 | * Returns the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3157 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3158 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3159 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3160 | xmlNsPtr cur; |
| 3161 | |
Daniel Veillard | 62ba71e | 1999-12-16 17:52:19 +0000 | [diff] [blame] | 3162 | if (node == NULL) return(NULL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3163 | while (node != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3164 | if (node->type == XML_ELEMENT_NODE) { |
| 3165 | cur = node->nsDef; |
| 3166 | while (cur != NULL) { |
| 3167 | if ((cur->prefix == NULL) && (nameSpace == NULL)) |
| 3168 | return(cur); |
| 3169 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 3170 | (!xmlStrcmp(cur->prefix, nameSpace))) |
| 3171 | return(cur); |
| 3172 | cur = cur->next; |
| 3173 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3174 | } |
| 3175 | node = node->parent; |
| 3176 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3177 | return(NULL); |
| 3178 | } |
| 3179 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3180 | /** |
| 3181 | * xmlSearchNsByHref: |
| 3182 | * @doc: the document |
| 3183 | * @node: the current node |
| 3184 | * @href: the namespace value |
| 3185 | * |
| 3186 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 3187 | * the defined namespace or return NULL otherwise. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3188 | * Returns the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3189 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3190 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3191 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3192 | xmlNsPtr cur; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3193 | xmlNodePtr orig = node; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3194 | |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3195 | if ((node == NULL) || (href == NULL)) return(NULL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3196 | while (node != NULL) { |
| 3197 | cur = node->nsDef; |
| 3198 | while (cur != NULL) { |
| 3199 | if ((cur->href != NULL) && (href != NULL) && |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3200 | (!xmlStrcmp(cur->href, href))) { |
| 3201 | /* |
| 3202 | * Check that the prefix is not shadowed between orig and node |
| 3203 | */ |
| 3204 | xmlNodePtr check = orig; |
| 3205 | xmlNsPtr tst; |
| 3206 | |
| 3207 | while (check != node) { |
| 3208 | tst = check->nsDef; |
| 3209 | while (tst != NULL) { |
| 3210 | if ((tst->prefix == NULL) && (cur->prefix == NULL)) |
| 3211 | goto shadowed; |
| 3212 | if ((tst->prefix != NULL) && (cur->prefix != NULL) && |
| 3213 | (!xmlStrcmp(tst->prefix, cur->prefix))) |
| 3214 | goto shadowed; |
| 3215 | tst = tst->next; |
| 3216 | } |
| 3217 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3218 | return(cur); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3219 | } |
| 3220 | shadowed: |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3221 | cur = cur->next; |
| 3222 | } |
| 3223 | node = node->parent; |
| 3224 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3225 | return(NULL); |
| 3226 | } |
| 3227 | |
| 3228 | /** |
| 3229 | * xmlNewReconciliedNs |
| 3230 | * @doc: the document |
| 3231 | * @tree: a node expected to hold the new namespace |
| 3232 | * @ns: the original namespace |
| 3233 | * |
| 3234 | * This function tries to locate a namespace definition in a tree |
| 3235 | * ancestors, or create a new namespace definition node similar to |
| 3236 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 3237 | * null (default namespace) or reused within the subtree defined by |
| 3238 | * @tree or on one of its ancestors then a new prefix is generated. |
| 3239 | * Returns the (new) namespace definition or NULL in case of error |
| 3240 | */ |
| 3241 | xmlNsPtr |
| 3242 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 3243 | xmlNsPtr def; |
| 3244 | xmlChar prefix[50]; |
| 3245 | int counter = 1; |
| 3246 | |
| 3247 | if (tree == NULL) { |
| 3248 | #ifdef DEBUG_TREE |
| 3249 | fprintf(stderr, "xmlNewReconciliedNs : tree == NULL\n"); |
| 3250 | #endif |
| 3251 | return(NULL); |
| 3252 | } |
| 3253 | if (ns == NULL) { |
| 3254 | #ifdef DEBUG_TREE |
| 3255 | fprintf(stderr, "xmlNewReconciliedNs : ns == NULL\n"); |
| 3256 | #endif |
| 3257 | return(NULL); |
| 3258 | } |
| 3259 | /* |
| 3260 | * Search an existing namespace definition inherited. |
| 3261 | */ |
| 3262 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 3263 | if (def != NULL) |
| 3264 | return(def); |
| 3265 | |
| 3266 | /* |
| 3267 | * Find a close prefix which is not already in use. |
| 3268 | * Let's strip namespace prefixes longer than 20 chars ! |
| 3269 | */ |
| 3270 | sprintf((char *) prefix, "%.20s", ns->prefix); |
| 3271 | def = xmlSearchNs(doc, tree, prefix); |
| 3272 | while (def != NULL) { |
| 3273 | if (counter > 1000) return(NULL); |
| 3274 | sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++); |
| 3275 | def = xmlSearchNs(doc, tree, prefix); |
| 3276 | } |
| 3277 | |
| 3278 | /* |
| 3279 | * Ok, now we are ready to create a new one. |
| 3280 | */ |
| 3281 | def = xmlNewNs(tree, ns->href, prefix); |
| 3282 | return(def); |
| 3283 | } |
| 3284 | |
| 3285 | /** |
| 3286 | * xmlReconciliateNs |
| 3287 | * @doc: the document |
| 3288 | * @tree: a node defining the subtree to reconciliate |
| 3289 | * |
| 3290 | * This function checks that all the namespaces declared within the given |
| 3291 | * tree are properly declared. This is needed for example after Copy or Cut |
| 3292 | * and then paste operations. The subtree may still hold pointers to |
| 3293 | * namespace declarations outside the subtree or invalid/masked. As much |
| 3294 | * as possible the function try tu reuse the existing namespaces found in |
| 3295 | * the new environment. If not possible the new namespaces are redeclared |
| 3296 | * on @tree at the top of the given subtree. |
| 3297 | * Returns the number of namespace declarations created or -1 in case of error. |
| 3298 | */ |
| 3299 | int |
| 3300 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 3301 | xmlNsPtr *oldNs = NULL; |
| 3302 | xmlNsPtr *newNs = NULL; |
| 3303 | int sizeCache = 0; |
| 3304 | int nbCache = 0; |
| 3305 | |
| 3306 | xmlNsPtr n; |
| 3307 | xmlNodePtr node = tree; |
| 3308 | xmlAttrPtr attr; |
| 3309 | int ret = 0, i; |
| 3310 | |
| 3311 | while (node != NULL) { |
| 3312 | /* |
| 3313 | * Reconciliate the node namespace |
| 3314 | */ |
| 3315 | if (node->ns != NULL) { |
| 3316 | /* |
| 3317 | * initialize the cache if needed |
| 3318 | */ |
| 3319 | if (sizeCache == 0) { |
| 3320 | sizeCache = 10; |
| 3321 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3322 | sizeof(xmlNsPtr)); |
| 3323 | if (oldNs == NULL) { |
| 3324 | fprintf(stderr, "xmlReconciliateNs : memory pbm\n"); |
| 3325 | return(-1); |
| 3326 | } |
| 3327 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3328 | sizeof(xmlNsPtr)); |
| 3329 | if (newNs == NULL) { |
| 3330 | fprintf(stderr, "xmlReconciliateNs : memory pbm\n"); |
| 3331 | xmlFree(oldNs); |
| 3332 | return(-1); |
| 3333 | } |
| 3334 | } |
| 3335 | for (i = 0;i < nbCache;i++) { |
| 3336 | if (oldNs[i] == node->ns) { |
| 3337 | node->ns = newNs[i]; |
| 3338 | break; |
| 3339 | } |
| 3340 | } |
| 3341 | if (i == nbCache) { |
| 3342 | /* |
| 3343 | * Ok we need to recreate a new namespace definition |
| 3344 | */ |
| 3345 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 3346 | if (n != NULL) { /* :-( what if else ??? */ |
| 3347 | /* |
| 3348 | * check if we need to grow the cache buffers. |
| 3349 | */ |
| 3350 | if (sizeCache <= nbCache) { |
| 3351 | sizeCache *= 2; |
| 3352 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 3353 | sizeof(xmlNsPtr)); |
| 3354 | if (oldNs == NULL) { |
| 3355 | fprintf(stderr, "xmlReconciliateNs : memory pbm\n"); |
| 3356 | xmlFree(newNs); |
| 3357 | return(-1); |
| 3358 | } |
| 3359 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 3360 | sizeof(xmlNsPtr)); |
| 3361 | if (newNs == NULL) { |
| 3362 | fprintf(stderr, "xmlReconciliateNs : memory pbm\n"); |
| 3363 | xmlFree(oldNs); |
| 3364 | return(-1); |
| 3365 | } |
| 3366 | } |
| 3367 | newNs[nbCache] = n; |
| 3368 | oldNs[nbCache++] = node->ns; |
| 3369 | node->ns = n; |
| 3370 | } |
| 3371 | } |
| 3372 | } |
| 3373 | /* |
| 3374 | * now check for namespace hold by attributes on the node. |
| 3375 | */ |
| 3376 | attr = node->properties; |
| 3377 | while (attr != NULL) { |
| 3378 | if (attr->ns != NULL) { |
| 3379 | /* |
| 3380 | * initialize the cache if needed |
| 3381 | */ |
| 3382 | if (sizeCache == 0) { |
| 3383 | sizeCache = 10; |
| 3384 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3385 | sizeof(xmlNsPtr)); |
| 3386 | if (oldNs == NULL) { |
| 3387 | fprintf(stderr, "xmlReconciliateNs : memory pbm\n"); |
| 3388 | return(-1); |
| 3389 | } |
| 3390 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3391 | sizeof(xmlNsPtr)); |
| 3392 | if (newNs == NULL) { |
| 3393 | fprintf(stderr, "xmlReconciliateNs : memory pbm\n"); |
| 3394 | xmlFree(oldNs); |
| 3395 | return(-1); |
| 3396 | } |
| 3397 | } |
| 3398 | for (i = 0;i < nbCache;i++) { |
| 3399 | if (oldNs[i] == attr->ns) { |
| 3400 | node->ns = newNs[i]; |
| 3401 | break; |
| 3402 | } |
| 3403 | } |
| 3404 | if (i == nbCache) { |
| 3405 | /* |
| 3406 | * Ok we need to recreate a new namespace definition |
| 3407 | */ |
| 3408 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 3409 | if (n != NULL) { /* :-( what if else ??? */ |
| 3410 | /* |
| 3411 | * check if we need to grow the cache buffers. |
| 3412 | */ |
| 3413 | if (sizeCache <= nbCache) { |
| 3414 | sizeCache *= 2; |
| 3415 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 3416 | sizeof(xmlNsPtr)); |
| 3417 | if (oldNs == NULL) { |
| 3418 | fprintf(stderr, |
| 3419 | "xmlReconciliateNs : memory pbm\n"); |
| 3420 | xmlFree(newNs); |
| 3421 | return(-1); |
| 3422 | } |
| 3423 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 3424 | sizeof(xmlNsPtr)); |
| 3425 | if (newNs == NULL) { |
| 3426 | fprintf(stderr, |
| 3427 | "xmlReconciliateNs : memory pbm\n"); |
| 3428 | xmlFree(oldNs); |
| 3429 | return(-1); |
| 3430 | } |
| 3431 | } |
| 3432 | newNs[nbCache] = n; |
| 3433 | oldNs[nbCache++] = attr->ns; |
| 3434 | attr->ns = n; |
| 3435 | } |
| 3436 | } |
| 3437 | } |
| 3438 | attr = attr->next; |
| 3439 | } |
| 3440 | |
| 3441 | /* |
| 3442 | * Browse the full subtree, deep first |
| 3443 | */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3444 | if (node->children != NULL) { |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3445 | /* deep first */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3446 | node = node->children; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3447 | } else if ((node != tree) && (node->next != NULL)) { |
| 3448 | /* then siblings */ |
| 3449 | node = node->next; |
| 3450 | } else if (node != tree) { |
| 3451 | /* go up to parents->next if needed */ |
| 3452 | while (node != tree) { |
| 3453 | if (node->parent != NULL) |
| 3454 | node = node->parent; |
| 3455 | if ((node != tree) && (node->next != NULL)) { |
| 3456 | node = node->next; |
| 3457 | break; |
| 3458 | } |
| 3459 | if (node->parent == NULL) { |
| 3460 | node = NULL; |
| 3461 | break; |
| 3462 | } |
| 3463 | } |
| 3464 | /* exit condition */ |
| 3465 | if (node == tree) |
| 3466 | node = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3467 | } |
| 3468 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3469 | return(ret); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3472 | /** |
| 3473 | * xmlGetProp: |
| 3474 | * @node: the node |
| 3475 | * @name: the attribute name |
| 3476 | * |
| 3477 | * Search and get the value of an attribute associated to a node |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3478 | * This does the entity substitution. |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3479 | * This function looks in DTD attribute declaration for #FIXED or |
| 3480 | * default declaration values unless DTD use has been turned off. |
| 3481 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3482 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 3483 | * It's up to the caller to free the memory. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3484 | */ |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 3485 | xmlChar * |
| 3486 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3487 | xmlAttrPtr prop; |
| 3488 | xmlDocPtr doc; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3489 | |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3490 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 3491 | /* |
| 3492 | * Check on the properties attached to the node |
| 3493 | */ |
| 3494 | prop = node->properties; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3495 | while (prop != NULL) { |
Daniel Veillard | 6817893 | 1999-02-08 18:34:36 +0000 | [diff] [blame] | 3496 | if (!xmlStrcmp(prop->name, name)) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3497 | xmlChar *ret; |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame] | 3498 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3499 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3500 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame] | 3501 | return(ret); |
Daniel Veillard | 6817893 | 1999-02-08 18:34:36 +0000 | [diff] [blame] | 3502 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3503 | prop = prop->next; |
| 3504 | } |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3505 | if (!xmlCheckDTD) return(NULL); |
| 3506 | |
| 3507 | /* |
| 3508 | * Check if there is a default declaration in the internal |
| 3509 | * or external subsets |
| 3510 | */ |
| 3511 | doc = node->doc; |
| 3512 | if (doc != NULL) { |
| 3513 | xmlAttributePtr attrDecl; |
| 3514 | if (doc->intSubset != NULL) { |
| 3515 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 3516 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3517 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | f967b90 | 2000-01-17 16:06:10 +0000 | [diff] [blame] | 3518 | if (attrDecl != NULL) |
| 3519 | return(xmlStrdup(attrDecl->defaultValue)); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3520 | } |
| 3521 | } |
| 3522 | return(NULL); |
| 3523 | } |
| 3524 | |
| 3525 | /** |
| 3526 | * xmlGetNsProp: |
| 3527 | * @node: the node |
| 3528 | * @name: the attribute name |
| 3529 | * @namespace: the URI of the namespace |
| 3530 | * |
| 3531 | * Search and get the value of an attribute associated to a node |
| 3532 | * This attribute has to be anchored in the namespace specified. |
| 3533 | * This does the entity substitution. |
| 3534 | * This function looks in DTD attribute declaration for #FIXED or |
| 3535 | * default declaration values unless DTD use has been turned off. |
| 3536 | * |
| 3537 | * Returns the attribute value or NULL if not found. |
| 3538 | * It's up to the caller to free the memory. |
| 3539 | */ |
| 3540 | xmlChar * |
| 3541 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) { |
| 3542 | xmlAttrPtr prop = node->properties; |
| 3543 | xmlDocPtr doc; |
| 3544 | xmlNsPtr ns; |
| 3545 | |
| 3546 | if (namespace == NULL) |
| 3547 | return(xmlGetProp(node, name)); |
| 3548 | while (prop != NULL) { |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 3549 | /* |
| 3550 | * One need to have |
| 3551 | * - same attribute names |
| 3552 | * - and the attribute carrying that namespace |
| 3553 | * or |
| 3554 | * no namespace on the attribute and the element carrying it |
| 3555 | */ |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3556 | if ((!xmlStrcmp(prop->name, name)) && |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 3557 | (((prop->ns == NULL) && (node->ns != NULL) && |
| 3558 | (!xmlStrcmp(node->ns->href, namespace))) || |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 3559 | ((prop->ns != NULL) && (!xmlStrcmp(prop->ns->href, namespace))))) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3560 | xmlChar *ret; |
| 3561 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3562 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3563 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 3564 | return(ret); |
| 3565 | } |
| 3566 | prop = prop->next; |
| 3567 | } |
| 3568 | if (!xmlCheckDTD) return(NULL); |
| 3569 | |
| 3570 | /* |
| 3571 | * Check if there is a default declaration in the internal |
| 3572 | * or external subsets |
| 3573 | */ |
| 3574 | doc = node->doc; |
| 3575 | if (doc != NULL) { |
| 3576 | xmlAttributePtr attrDecl; |
| 3577 | if (doc->intSubset != NULL) { |
| 3578 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 3579 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3580 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 3581 | |
| 3582 | if (attrDecl->prefix != NULL) { |
| 3583 | /* |
| 3584 | * The DTD declaration only allows a prefix search |
| 3585 | */ |
| 3586 | ns = xmlSearchNs(doc, node, attrDecl->prefix); |
| 3587 | if ((ns != NULL) && (!xmlStrcmp(ns->href, namespace))) |
| 3588 | return(xmlStrdup(attrDecl->defaultValue)); |
| 3589 | } |
| 3590 | } |
| 3591 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3592 | return(NULL); |
| 3593 | } |
| 3594 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3595 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3596 | * xmlSetProp: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3597 | * @node: the node |
| 3598 | * @name: the attribute name |
| 3599 | * @value: the attribute value |
| 3600 | * |
| 3601 | * Set (or reset) an attribute carried by a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3602 | * Returns the attribute pointer. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3603 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3604 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3605 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3606 | xmlAttrPtr prop = node->properties; |
| 3607 | |
| 3608 | while (prop != NULL) { |
| 3609 | if (!xmlStrcmp(prop->name, name)) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3610 | if (prop->children != NULL) |
| 3611 | xmlFreeNodeList(prop->children); |
| 3612 | prop->children = NULL; |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 3613 | if (value != NULL) { |
| 3614 | xmlChar *buffer; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3615 | xmlNodePtr tmp; |
| 3616 | |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 3617 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3618 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 3619 | tmp = prop->children; |
| 3620 | while (tmp != NULL) { |
| 3621 | tmp->parent = (xmlNodePtr) prop; |
| 3622 | if (tmp->next == NULL) |
| 3623 | prop->last = tmp; |
| 3624 | tmp = tmp->next; |
| 3625 | } |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 3626 | xmlFree(buffer); |
| 3627 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3628 | return(prop); |
| 3629 | } |
| 3630 | prop = prop->next; |
| 3631 | } |
| 3632 | prop = xmlNewProp(node, name, value); |
| 3633 | return(prop); |
| 3634 | } |
| 3635 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3636 | /** |
| 3637 | * xmlNodeIsText: |
| 3638 | * @node: the node |
| 3639 | * |
| 3640 | * Is this node a Text node ? |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3641 | * Returns 1 yes, 0 no |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3642 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3643 | int |
| 3644 | xmlNodeIsText(xmlNodePtr node) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3645 | if (node == NULL) return(0); |
| 3646 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 3647 | if (node->type == XML_TEXT_NODE) return(1); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3648 | return(0); |
| 3649 | } |
| 3650 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3651 | /** |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 3652 | * xmlIsBlankNode: |
| 3653 | * @node: the node |
| 3654 | * |
| 3655 | * Is this node a Text node ? |
| 3656 | * Returns 1 yes, 0 no |
| 3657 | */ |
| 3658 | int |
| 3659 | xmlIsBlankNode(xmlNodePtr node) { |
| 3660 | xmlChar *cur; |
| 3661 | if (node == NULL) return(0); |
| 3662 | |
| 3663 | if (node->type != XML_TEXT_NODE) return(0); |
| 3664 | if (node->content == NULL) return(0); |
| 3665 | cur = node->content; |
| 3666 | while (*cur != 0) { |
| 3667 | if (!IS_BLANK(*cur)) return(0); |
| 3668 | cur++; |
| 3669 | } |
| 3670 | |
| 3671 | return(1); |
| 3672 | } |
| 3673 | |
| 3674 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3675 | * xmlTextConcat: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3676 | * @node: the node |
| 3677 | * @content: the content |
| 3678 | * @len: @content lenght |
| 3679 | * |
| 3680 | * Concat the given string at the end of the existing node content |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3681 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3682 | |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3683 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3684 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3685 | if (node == NULL) return; |
| 3686 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3687 | if ((node->type != XML_TEXT_NODE) && |
| 3688 | (node->type != XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3689 | #ifdef DEBUG_TREE |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3690 | fprintf(stderr, "xmlTextConcat: node is not text nor cdata\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3691 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3692 | return; |
| 3693 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3694 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3695 | node->content = xmlStrncat(node->content, content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3696 | #else |
| 3697 | xmlBufferAdd(node->content, content, len); |
| 3698 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | /************************************************************************ |
| 3702 | * * |
| 3703 | * Output : to a FILE or in memory * |
| 3704 | * * |
| 3705 | ************************************************************************/ |
| 3706 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3707 | #define BASE_BUFFER_SIZE 4000 |
| 3708 | |
| 3709 | /** |
| 3710 | * xmlBufferCreate: |
| 3711 | * |
| 3712 | * routine to create an XML buffer. |
| 3713 | * returns the new structure. |
| 3714 | */ |
| 3715 | xmlBufferPtr |
| 3716 | xmlBufferCreate(void) { |
| 3717 | xmlBufferPtr ret; |
| 3718 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3719 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3720 | if (ret == NULL) { |
| 3721 | fprintf(stderr, "xmlBufferCreate : out of memory!\n"); |
| 3722 | return(NULL); |
| 3723 | } |
| 3724 | ret->use = 0; |
| 3725 | ret->size = BASE_BUFFER_SIZE; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3726 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3727 | ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3728 | if (ret->content == NULL) { |
| 3729 | fprintf(stderr, "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3730 | xmlFree(ret); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3731 | return(NULL); |
| 3732 | } |
| 3733 | ret->content[0] = 0; |
| 3734 | return(ret); |
| 3735 | } |
| 3736 | |
| 3737 | /** |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3738 | * xmlBufferCreateSize: |
| 3739 | * @size: initial size of buffer |
| 3740 | * |
| 3741 | * routine to create an XML buffer. |
| 3742 | * returns the new structure. |
| 3743 | */ |
| 3744 | xmlBufferPtr |
| 3745 | xmlBufferCreateSize(size_t size) { |
| 3746 | xmlBufferPtr ret; |
| 3747 | |
| 3748 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 3749 | if (ret == NULL) { |
| 3750 | fprintf(stderr, "xmlBufferCreate : out of memory!\n"); |
| 3751 | return(NULL); |
| 3752 | } |
| 3753 | ret->use = 0; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3754 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3755 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3756 | if (ret->size){ |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3757 | ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar)); |
| 3758 | if (ret->content == NULL) { |
| 3759 | fprintf(stderr, "xmlBufferCreate : out of memory!\n"); |
| 3760 | xmlFree(ret); |
| 3761 | return(NULL); |
| 3762 | } |
| 3763 | ret->content[0] = 0; |
| 3764 | } else |
| 3765 | ret->content = NULL; |
| 3766 | return(ret); |
| 3767 | } |
| 3768 | |
| 3769 | /** |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 3770 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3771 | * @buf: the buffer to free |
| 3772 | * @scheme: allocation scheme to use |
| 3773 | * |
| 3774 | * Sets the allocation scheme for this buffer |
| 3775 | */ |
| 3776 | void |
| 3777 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
| 3778 | xmlBufferAllocationScheme scheme) { |
| 3779 | if (buf == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3780 | #ifdef DEBUG_BUFFER |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3781 | fprintf(stderr, "xmlBufferSetAllocationScheme: buf == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3782 | #endif |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3783 | return; |
| 3784 | } |
| 3785 | |
| 3786 | buf->alloc = scheme; |
| 3787 | } |
| 3788 | |
| 3789 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3790 | * xmlBufferFree: |
| 3791 | * @buf: the buffer to free |
| 3792 | * |
| 3793 | * Frees an XML buffer. |
| 3794 | */ |
| 3795 | void |
| 3796 | xmlBufferFree(xmlBufferPtr buf) { |
| 3797 | if (buf == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3798 | #ifdef DEBUG_BUFFER |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3799 | fprintf(stderr, "xmlBufferFree: buf == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3800 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3801 | return; |
| 3802 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3803 | if (buf->content != NULL) { |
| 3804 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3805 | memset(buf->content, -1, BASE_BUFFER_SIZE); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3806 | #else |
| 3807 | memset(buf->content, -1, buf->size); |
| 3808 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3809 | xmlFree(buf->content); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3810 | } |
| 3811 | memset(buf, -1, sizeof(xmlBuffer)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3812 | xmlFree(buf); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
| 3815 | /** |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3816 | * xmlBufferEmpty: |
| 3817 | * @buf: the buffer |
| 3818 | * |
| 3819 | * empty a buffer. |
| 3820 | */ |
| 3821 | void |
| 3822 | xmlBufferEmpty(xmlBufferPtr buf) { |
| 3823 | buf->use = 0; |
| 3824 | memset(buf->content, -1, buf->size);/* just for debug */ |
| 3825 | } |
| 3826 | |
| 3827 | /** |
| 3828 | * xmlBufferShrink: |
| 3829 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3830 | * @len: the number of xmlChar to remove |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3831 | * |
| 3832 | * Remove the beginning of an XML buffer. |
| 3833 | * |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3834 | * Returns the number of xmlChar removed, or -1 in case of failure. |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3835 | */ |
| 3836 | int |
| 3837 | xmlBufferShrink(xmlBufferPtr buf, int len) { |
| 3838 | if (len == 0) return(0); |
| 3839 | if (len > buf->use) return(-1); |
| 3840 | |
| 3841 | buf->use -= len; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3842 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3843 | |
| 3844 | buf->content[buf->use] = 0; |
| 3845 | return(len); |
| 3846 | } |
| 3847 | |
| 3848 | /** |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 3849 | * xmlBufferGrow: |
| 3850 | * @buf: the buffer |
| 3851 | * @len: the minimum free sie to allocate |
| 3852 | * |
| 3853 | * Grow the available space of an XML buffer. |
| 3854 | * |
| 3855 | * Returns the new available space or -1 in case of error |
| 3856 | */ |
| 3857 | int |
| 3858 | xmlBufferGrow(xmlBufferPtr buf, int len) { |
| 3859 | int size; |
| 3860 | xmlChar *newbuf; |
| 3861 | |
| 3862 | if (len <= buf->use) return(0); |
| 3863 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 3864 | size = buf->use + len + 100; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 3865 | |
| 3866 | newbuf = xmlRealloc(buf->content, size); |
| 3867 | if (newbuf == NULL) return(-1); |
| 3868 | buf->content = newbuf; |
| 3869 | buf->size = size; |
| 3870 | return(buf->size - buf->use); |
| 3871 | } |
| 3872 | |
| 3873 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3874 | * xmlBufferDump: |
| 3875 | * @file: the file output |
| 3876 | * @buf: the buffer to dump |
| 3877 | * |
| 3878 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3879 | * Returns the number of xmlChar written |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3880 | */ |
| 3881 | int |
| 3882 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 3883 | int ret; |
| 3884 | |
| 3885 | if (buf == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3886 | #ifdef DEBUG_BUFFER |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3887 | fprintf(stderr, "xmlBufferDump: buf == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3888 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3889 | return(0); |
| 3890 | } |
| 3891 | if (buf->content == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3892 | #ifdef DEBUG_BUFFER |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3893 | fprintf(stderr, "xmlBufferDump: buf->content == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3894 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3895 | return(0); |
| 3896 | } |
| 3897 | if (file == NULL) file = stdout; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3898 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3899 | return(ret); |
| 3900 | } |
| 3901 | |
| 3902 | /** |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3903 | * xmlBufferContent: |
| 3904 | * @buf: the buffer to resize |
| 3905 | * |
| 3906 | * Returns the internal content |
| 3907 | */ |
| 3908 | |
| 3909 | const xmlChar* |
| 3910 | xmlBufferContent(const xmlBufferPtr buf) |
| 3911 | { |
| 3912 | if(!buf) |
| 3913 | return NULL; |
| 3914 | |
| 3915 | return buf->content; |
| 3916 | } |
| 3917 | |
| 3918 | /** |
| 3919 | * xmlBufferLength: |
| 3920 | * @buf: the buffer |
| 3921 | * |
| 3922 | * Returns the length of data in the internal content |
| 3923 | */ |
| 3924 | |
| 3925 | int |
| 3926 | xmlBufferLength(const xmlBufferPtr buf) |
| 3927 | { |
| 3928 | if(!buf) |
| 3929 | return 0; |
| 3930 | |
| 3931 | return buf->use; |
| 3932 | } |
| 3933 | |
| 3934 | /** |
| 3935 | * xmlBufferResize: |
| 3936 | * @buf: the buffer to resize |
| 3937 | * @len: the desired size |
| 3938 | * |
| 3939 | * Resize a buffer to accomodate minimum size of <len>. |
| 3940 | * |
| 3941 | * Returns 0 in case of problems, 1 otherwise |
| 3942 | */ |
| 3943 | int |
| 3944 | xmlBufferResize(xmlBufferPtr buf, int size) |
| 3945 | { |
| 3946 | int newSize = (buf->size ? buf->size*2 : size);/*take care of empty case*/ |
| 3947 | xmlChar* rebuf = NULL; |
| 3948 | |
| 3949 | /* Don't resize if we don't have to */ |
| 3950 | if(size < buf->size) |
| 3951 | return 1; |
| 3952 | |
| 3953 | /* figure out new size */ |
| 3954 | switch(buf->alloc){ |
| 3955 | case XML_BUFFER_ALLOC_DOUBLEIT: |
| 3956 | while(size > newSize) newSize *= 2; |
| 3957 | break; |
| 3958 | case XML_BUFFER_ALLOC_EXACT: |
| 3959 | newSize = size+10; |
| 3960 | break; |
| 3961 | default: |
| 3962 | newSize = size+10; |
| 3963 | break; |
| 3964 | } |
| 3965 | |
| 3966 | if (buf->content == NULL) |
| 3967 | rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar)); |
| 3968 | else |
| 3969 | rebuf = (xmlChar *) xmlRealloc(buf->content, |
| 3970 | newSize * sizeof(xmlChar)); |
| 3971 | if (rebuf == NULL) { |
| 3972 | fprintf(stderr, "xmlBufferAdd : out of memory!\n"); |
| 3973 | return 0; |
| 3974 | } |
| 3975 | buf->content = rebuf; |
| 3976 | buf->size = newSize; |
| 3977 | |
| 3978 | return 1; |
| 3979 | } |
| 3980 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3981 | * xmlBufferAdd: |
| 3982 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3983 | * @str: the xmlChar string |
| 3984 | * @len: the number of xmlChar to add |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3985 | * |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3986 | * Add a string range to an XML buffer. if len == -1, the lenght of |
| 3987 | * str is recomputed. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3988 | */ |
| 3989 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3990 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3991 | int needSize; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3992 | |
| 3993 | if (str == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3994 | #ifdef DEBUG_BUFFER |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3995 | fprintf(stderr, "xmlBufferAdd: str == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3996 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3997 | return; |
| 3998 | } |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3999 | if (len < -1) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4000 | #ifdef DEBUG_BUFFER |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4001 | fprintf(stderr, "xmlBufferAdd: len < 0\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4002 | #endif |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4003 | return; |
| 4004 | } |
| 4005 | if (len == 0) return; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4006 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4007 | if (len < 0) |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4008 | len = xmlStrlen(str); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4009 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4010 | if (len <= 0) return; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4011 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4012 | needSize = buf->use + len + 2; |
| 4013 | if(needSize > buf->size){ |
| 4014 | if(!xmlBufferResize(buf, needSize)){ |
| 4015 | fprintf(stderr, "xmlBufferAdd : out of memory!\n"); |
| 4016 | return; |
| 4017 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4018 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4019 | |
| 4020 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4021 | buf->use += len; |
| 4022 | buf->content[buf->use] = 0; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
| 4025 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4026 | * xmlBufferAddHead: |
| 4027 | * @buf: the buffer |
| 4028 | * @str: the xmlChar string |
| 4029 | * @len: the number of xmlChar to add |
| 4030 | * |
| 4031 | * Add a string range to the beginning of an XML buffer. |
| 4032 | * if len == -1, the lenght of @str is recomputed. |
| 4033 | */ |
| 4034 | void |
| 4035 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 4036 | int needSize; |
| 4037 | |
| 4038 | if (str == NULL) { |
| 4039 | #ifdef DEBUG_BUFFER |
| 4040 | fprintf(stderr, "xmlBufferAdd: str == NULL\n"); |
| 4041 | #endif |
| 4042 | return; |
| 4043 | } |
| 4044 | if (len < -1) { |
| 4045 | #ifdef DEBUG_BUFFER |
| 4046 | fprintf(stderr, "xmlBufferAdd: len < 0\n"); |
| 4047 | #endif |
| 4048 | return; |
| 4049 | } |
| 4050 | if (len == 0) return; |
| 4051 | |
| 4052 | if (len < 0) |
| 4053 | len = xmlStrlen(str); |
| 4054 | |
| 4055 | if (len <= 0) return; |
| 4056 | |
| 4057 | needSize = buf->use + len + 2; |
| 4058 | if(needSize > buf->size){ |
| 4059 | if(!xmlBufferResize(buf, needSize)){ |
| 4060 | fprintf(stderr, "xmlBufferAddHead : out of memory!\n"); |
| 4061 | return; |
| 4062 | } |
| 4063 | } |
| 4064 | |
| 4065 | memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar)); |
| 4066 | memmove(&buf->content[0], str, len * sizeof(xmlChar)); |
| 4067 | buf->use += len; |
| 4068 | buf->content[buf->use] = 0; |
| 4069 | } |
| 4070 | |
| 4071 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4072 | * xmlBufferCat: |
| 4073 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4074 | * @str: the xmlChar string |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4075 | * |
| 4076 | * Append a zero terminated string to an XML buffer. |
| 4077 | */ |
| 4078 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4079 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4080 | if (str != NULL) |
| 4081 | xmlBufferAdd(buf, str, -1); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | /** |
| 4085 | * xmlBufferCCat: |
| 4086 | * @buf: the buffer to dump |
| 4087 | * @str: the C char string |
| 4088 | * |
| 4089 | * Append a zero terminated C string to an XML buffer. |
| 4090 | */ |
| 4091 | void |
| 4092 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 4093 | const char *cur; |
| 4094 | |
| 4095 | if (str == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4096 | #ifdef DEBUG_BUFFER |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4097 | fprintf(stderr, "xmlBufferAdd: str == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4098 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4099 | return; |
| 4100 | } |
| 4101 | for (cur = str;*cur != 0;cur++) { |
| 4102 | if (buf->use + 10 >= buf->size) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4103 | if(!xmlBufferResize(buf, buf->use+10)){ |
| 4104 | fprintf(stderr, "xmlBufferCCat : out of memory!\n"); |
| 4105 | return; |
| 4106 | } |
| 4107 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4108 | buf->content[buf->use++] = *cur; |
| 4109 | } |
| 4110 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4111 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4112 | /** |
| 4113 | * xmlBufferWriteCHAR: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4114 | * @buf: the XML buffer |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4115 | * @string: the string to add |
| 4116 | * |
| 4117 | * routine which manage and grows an output buffer. This one add |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4118 | * xmlChars at the end of the buffer. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4119 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4120 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4121 | xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4122 | xmlBufferCat(buf, string); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4123 | } |
| 4124 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4125 | /** |
| 4126 | * xmlBufferWriteChar: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4127 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4128 | * @string: the string to add |
| 4129 | * |
| 4130 | * routine which manage and grows an output buffer. This one add |
| 4131 | * C chars at the end of the array. |
| 4132 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4133 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4134 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
| 4135 | xmlBufferCCat(buf, string); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4136 | } |
| 4137 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4138 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4139 | /** |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4140 | * xmlBufferWriteQuotedString: |
| 4141 | * @buf: the XML buffer output |
| 4142 | * @string: the string to add |
| 4143 | * |
| 4144 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4145 | * a quoted or double quoted xmlChar string, checking first if it holds |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4146 | * quote or double-quotes internally |
| 4147 | */ |
| 4148 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4149 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4150 | if (xmlStrchr(string, '"')) { |
| 4151 | if (xmlStrchr(string, '\'')) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4152 | #ifdef DEBUG_BUFFER |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4153 | fprintf(stderr, |
| 4154 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4155 | #endif |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4156 | } |
| 4157 | xmlBufferCCat(buf, "'"); |
| 4158 | xmlBufferCat(buf, string); |
| 4159 | xmlBufferCCat(buf, "'"); |
| 4160 | } else { |
| 4161 | xmlBufferCCat(buf, "\""); |
| 4162 | xmlBufferCat(buf, string); |
| 4163 | xmlBufferCCat(buf, "\""); |
| 4164 | } |
| 4165 | } |
| 4166 | |
| 4167 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4168 | /************************************************************************ |
| 4169 | * * |
| 4170 | * Dumping XML tree content to a simple buffer * |
| 4171 | * * |
| 4172 | ************************************************************************/ |
| 4173 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4174 | static void |
| 4175 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4176 | int format); |
| 4177 | static void |
| 4178 | xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4179 | int format); |
| 4180 | void |
| 4181 | htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur); |
| 4182 | |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4183 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4184 | * xmlGlobalNsDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4185 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4186 | * @cur: a namespace |
| 4187 | * |
| 4188 | * Dump a global Namespace, this is the old version based on PIs. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4189 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4190 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4191 | xmlGlobalNsDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4192 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4193 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4194 | fprintf(stderr, "xmlGlobalNsDump : Ns == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4195 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4196 | return; |
| 4197 | } |
| 4198 | if (cur->type == XML_GLOBAL_NAMESPACE) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4199 | xmlBufferWriteChar(buf, "<?namespace"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4200 | if (cur->href != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4201 | xmlBufferWriteChar(buf, " href="); |
| 4202 | xmlBufferWriteQuotedString(buf, cur->href); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4203 | } |
| 4204 | if (cur->prefix != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4205 | xmlBufferWriteChar(buf, " AS="); |
| 4206 | xmlBufferWriteQuotedString(buf, cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4207 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4208 | xmlBufferWriteChar(buf, "?>\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4209 | } |
| 4210 | } |
| 4211 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4212 | /** |
| 4213 | * xmlGlobalNsListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4214 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4215 | * @cur: the first namespace |
| 4216 | * |
| 4217 | * 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] | 4218 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4219 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4220 | xmlGlobalNsListDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4221 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4222 | xmlGlobalNsDump(buf, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4223 | cur = cur->next; |
| 4224 | } |
| 4225 | } |
| 4226 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4227 | /** |
| 4228 | * xmlNsDump: |
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 | * @cur: a namespace |
| 4231 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4232 | * Dump a local Namespace definition. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4233 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4234 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4235 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4236 | xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4237 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4238 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4239 | fprintf(stderr, "xmlNsDump : Ns == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4240 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4241 | return; |
| 4242 | } |
| 4243 | if (cur->type == XML_LOCAL_NAMESPACE) { |
| 4244 | /* Within the context of an element attributes */ |
| 4245 | if (cur->prefix != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4246 | xmlBufferWriteChar(buf, " xmlns:"); |
| 4247 | xmlBufferWriteCHAR(buf, cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4248 | } else |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4249 | xmlBufferWriteChar(buf, " xmlns"); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4250 | xmlBufferWriteChar(buf, "="); |
| 4251 | xmlBufferWriteQuotedString(buf, cur->href); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4252 | } |
| 4253 | } |
| 4254 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4255 | /** |
| 4256 | * xmlNsListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4257 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4258 | * @cur: the first namespace |
| 4259 | * |
| 4260 | * Dump a list of local Namespace definitions. |
| 4261 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4262 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4263 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4264 | xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4265 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4266 | xmlNsDump(buf, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4267 | cur = cur->next; |
| 4268 | } |
| 4269 | } |
| 4270 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4271 | /** |
| 4272 | * xmlDtdDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4273 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4274 | * @doc: the document |
| 4275 | * |
| 4276 | * Dump the XML document DTD, if any. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4277 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4278 | static void |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4279 | xmlDtdDump(xmlBufferPtr buf, xmlDtdPtr dtd) { |
| 4280 | if (dtd == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4281 | #ifdef DEBUG_TREE |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 4282 | fprintf(stderr, "xmlDtdDump : no internal subset\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4283 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4284 | return; |
| 4285 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4286 | xmlBufferWriteChar(buf, "<!DOCTYPE "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4287 | xmlBufferWriteCHAR(buf, dtd->name); |
| 4288 | if (dtd->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4289 | xmlBufferWriteChar(buf, " PUBLIC "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4290 | xmlBufferWriteQuotedString(buf, dtd->ExternalID); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4291 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4292 | xmlBufferWriteQuotedString(buf, dtd->SystemID); |
| 4293 | } else if (dtd->SystemID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4294 | xmlBufferWriteChar(buf, " SYSTEM "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4295 | xmlBufferWriteQuotedString(buf, dtd->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4296 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4297 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
| 4298 | (dtd->attributes == NULL) && (dtd->notations == NULL)) { |
| 4299 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4300 | return; |
| 4301 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4302 | xmlBufferWriteChar(buf, " [\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4303 | xmlNodeListDump(buf, dtd->doc, dtd->children, -1, 0); |
| 4304 | #if 0 |
| 4305 | if (dtd->entities != NULL) |
| 4306 | xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) dtd->entities); |
| 4307 | if (dtd->notations != NULL) |
| 4308 | xmlDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations); |
| 4309 | if (dtd->elements != NULL) |
| 4310 | xmlDumpElementTable(buf, (xmlElementTablePtr) dtd->elements); |
| 4311 | if (dtd->attributes != NULL) |
| 4312 | xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) dtd->attributes); |
| 4313 | #endif |
| 4314 | xmlBufferWriteChar(buf, "]>"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4317 | /** |
| 4318 | * xmlAttrDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4319 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4320 | * @doc: the document |
| 4321 | * @cur: the attribute pointer |
| 4322 | * |
| 4323 | * Dump an XML attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4324 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4325 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4326 | xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4327 | xmlChar *value; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4328 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4329 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4330 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4331 | fprintf(stderr, "xmlAttrDump : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4332 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4333 | return; |
| 4334 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4335 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4336 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 4337 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 4338 | xmlBufferWriteChar(buf, ":"); |
| 4339 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4340 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4341 | value = xmlNodeListGetString(doc, cur->children, 0); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4342 | if (value) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4343 | xmlBufferWriteChar(buf, "="); |
| 4344 | xmlBufferWriteQuotedString(buf, value); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4345 | xmlFree(value); |
Daniel Veillard | 726c7e3 | 1999-02-08 15:13:10 +0000 | [diff] [blame] | 4346 | } else { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4347 | xmlBufferWriteChar(buf, "=\"\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4348 | } |
| 4349 | } |
| 4350 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4351 | /** |
| 4352 | * xmlAttrListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4353 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4354 | * @doc: the document |
| 4355 | * @cur: the first attribute pointer |
| 4356 | * |
| 4357 | * Dump a list of XML attributes |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4358 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4359 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4360 | xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4361 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4362 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4363 | fprintf(stderr, "xmlAttrListDump : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4364 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4365 | return; |
| 4366 | } |
| 4367 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4368 | xmlAttrDump(buf, doc, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4369 | cur = cur->next; |
| 4370 | } |
| 4371 | } |
| 4372 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4373 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4374 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4375 | /** |
| 4376 | * xmlNodeListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4377 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4378 | * @doc: the document |
| 4379 | * @cur: the first node |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4380 | * @level: the imbrication level for indenting |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4381 | * @format: is formatting allowed |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4382 | * |
| 4383 | * Dump an XML node list, recursive behaviour,children are printed too. |
| 4384 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4385 | static void |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4386 | xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4387 | int format) { |
| 4388 | int i; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4389 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4390 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4391 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4392 | fprintf(stderr, "xmlNodeListDump : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4393 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4394 | return; |
| 4395 | } |
| 4396 | while (cur != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4397 | if ((format) && (xmlIndentTreeOutput) && |
| 4398 | (cur->type == XML_ELEMENT_NODE)) |
| 4399 | for (i = 0;i < level;i++) |
| 4400 | xmlBufferWriteChar(buf, " "); |
| 4401 | xmlNodeDump(buf, doc, cur, level, format); |
| 4402 | if (format) { |
| 4403 | xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4404 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4405 | cur = cur->next; |
| 4406 | } |
| 4407 | } |
| 4408 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4409 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4410 | * xmlNodeDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4411 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4412 | * @doc: the document |
| 4413 | * @cur: the current node |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4414 | * @level: the imbrication level for indenting |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4415 | * @format: is formatting allowed |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4416 | * |
| 4417 | * Dump an XML node, recursive behaviour,children are printed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4418 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4419 | static void |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4420 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4421 | int format) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4422 | int i; |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4423 | xmlNodePtr tmp; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4424 | |
| 4425 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4426 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4427 | fprintf(stderr, "xmlNodeDump : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4428 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4429 | return; |
| 4430 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4431 | if (cur->type == XML_DTD_NODE) { |
| 4432 | xmlDtdDump(buf, (xmlDtdPtr) cur); |
| 4433 | return; |
| 4434 | } |
| 4435 | if (cur->type == XML_ELEMENT_DECL) { |
| 4436 | xmlDumpElementDecl(buf, (xmlElementPtr) cur); |
| 4437 | return; |
| 4438 | } |
| 4439 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 4440 | xmlDumpAttributeDecl(buf, (xmlAttributePtr) cur); |
| 4441 | return; |
| 4442 | } |
| 4443 | if (cur->type == XML_ENTITY_DECL) { |
| 4444 | xmlDumpEntityDecl(buf, (xmlEntityPtr) cur); |
| 4445 | return; |
| 4446 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 4447 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4448 | if (cur->content != NULL) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4449 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4450 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4451 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4452 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4453 | #else |
| 4454 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 4455 | xmlBufferContent(cur->content)); |
| 4456 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4457 | if (buffer != NULL) { |
| 4458 | xmlBufferWriteCHAR(buf, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4459 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4460 | } |
| 4461 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4462 | return; |
| 4463 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4464 | if (cur->type == XML_PI_NODE) { |
| 4465 | if (cur->content != NULL) { |
| 4466 | xmlBufferWriteChar(buf, "<?"); |
| 4467 | xmlBufferWriteCHAR(buf, cur->name); |
| 4468 | if (cur->content != NULL) { |
| 4469 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4470 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4471 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4472 | #else |
| 4473 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 4474 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4475 | } |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4476 | xmlBufferWriteChar(buf, "?>"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4477 | } else { |
| 4478 | xmlBufferWriteChar(buf, "<?"); |
| 4479 | xmlBufferWriteCHAR(buf, cur->name); |
| 4480 | xmlBufferWriteChar(buf, "?>"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4481 | } |
| 4482 | return; |
| 4483 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 4484 | if (cur->type == XML_COMMENT_NODE) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4485 | if (cur->content != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4486 | xmlBufferWriteChar(buf, "<!--"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4487 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4488 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4489 | #else |
| 4490 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 4491 | #endif |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4492 | xmlBufferWriteChar(buf, "-->"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4493 | } |
| 4494 | return; |
| 4495 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4496 | if (cur->type == XML_ENTITY_REF_NODE) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4497 | xmlBufferWriteChar(buf, "&"); |
| 4498 | xmlBufferWriteCHAR(buf, cur->name); |
| 4499 | xmlBufferWriteChar(buf, ";"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4500 | return; |
| 4501 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 4502 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 4503 | xmlBufferWriteChar(buf, "<![CDATA["); |
| 4504 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4505 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 4506 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4507 | #else |
| 4508 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 4509 | #endif |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 4510 | xmlBufferWriteChar(buf, "]]>"); |
| 4511 | return; |
| 4512 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4513 | |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4514 | if (format == 1) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4515 | tmp = cur->children; |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4516 | while (tmp != NULL) { |
| 4517 | if ((tmp->type == XML_TEXT_NODE) || |
| 4518 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 4519 | format = 0; |
| 4520 | break; |
| 4521 | } |
| 4522 | tmp = tmp->next; |
| 4523 | } |
| 4524 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4525 | xmlBufferWriteChar(buf, "<"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4526 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4527 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 4528 | xmlBufferWriteChar(buf, ":"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4531 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4532 | if (cur->nsDef) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4533 | xmlNsListDump(buf, cur->nsDef); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4534 | if (cur->properties != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4535 | xmlAttrListDump(buf, doc, cur->properties); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4536 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4537 | if ((cur->content == NULL) && (cur->children == NULL) && |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 4538 | (!xmlSaveNoEmptyTags)) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4539 | xmlBufferWriteChar(buf, "/>"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4540 | return; |
| 4541 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4542 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4543 | if (cur->content != NULL) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4544 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4545 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4546 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4547 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4548 | #else |
| 4549 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 4550 | xmlBufferContent(cur->content)); |
| 4551 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4552 | if (buffer != NULL) { |
| 4553 | xmlBufferWriteCHAR(buf, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4554 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4555 | } |
| 4556 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4557 | if (cur->children != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4558 | if (format) xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4559 | xmlNodeListDump(buf, doc, cur->children, |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4560 | (level >= 0?level+1:-1), format); |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4561 | if ((xmlIndentTreeOutput) && (format)) |
| 4562 | for (i = 0;i < level;i++) |
| 4563 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4564 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4565 | xmlBufferWriteChar(buf, "</"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4566 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4567 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 4568 | xmlBufferWriteChar(buf, ":"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4571 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4572 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4575 | /** |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4576 | * xmlElemDump: |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 4577 | * @f: the FILE * for the output |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4578 | * @doc: the document |
| 4579 | * @cur: the current node |
| 4580 | * |
| 4581 | * Dump an XML/HTML node, recursive behaviour,children are printed too. |
| 4582 | */ |
| 4583 | void |
| 4584 | xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) { |
| 4585 | xmlBufferPtr buf; |
| 4586 | |
| 4587 | if (cur == NULL) { |
| 4588 | #ifdef DEBUG_TREE |
| 4589 | fprintf(stderr, "xmlElemDump : cur == NULL\n"); |
| 4590 | #endif |
| 4591 | return; |
| 4592 | } |
| 4593 | if (doc == NULL) { |
| 4594 | #ifdef DEBUG_TREE |
| 4595 | fprintf(stderr, "xmlElemDump : doc == NULL\n"); |
| 4596 | #endif |
| 4597 | } |
| 4598 | buf = xmlBufferCreate(); |
| 4599 | if (buf == NULL) return; |
| 4600 | if ((doc != NULL) && |
| 4601 | (doc->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 4602 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4603 | htmlNodeDump(buf, doc, cur); |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 4604 | #else |
| 4605 | printf("HTML support not compiled in\n"); |
| 4606 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4607 | } else |
| 4608 | xmlNodeDump(buf, doc, cur, 0, 1); |
| 4609 | xmlBufferDump(f, buf); |
| 4610 | xmlBufferFree(buf); |
| 4611 | } |
| 4612 | |
| 4613 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4614 | * xmlDocContentDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4615 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4616 | * @cur: the document |
| 4617 | * |
| 4618 | * Dump an XML document. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4619 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4620 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4621 | xmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur) { |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 4622 | xmlBufferWriteChar(buf, "<?xml version="); |
| 4623 | if (cur->version != NULL) |
| 4624 | xmlBufferWriteQuotedString(buf, cur->version); |
| 4625 | else |
| 4626 | xmlBufferWriteChar(buf, "\"1.0\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4627 | if (cur->encoding != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4628 | xmlBufferWriteChar(buf, " encoding="); |
| 4629 | xmlBufferWriteQuotedString(buf, cur->encoding); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4630 | } |
| 4631 | switch (cur->standalone) { |
| 4632 | case 0: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4633 | xmlBufferWriteChar(buf, " standalone=\"no\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4634 | break; |
| 4635 | case 1: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4636 | xmlBufferWriteChar(buf, " standalone=\"yes\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4637 | break; |
| 4638 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4639 | xmlBufferWriteChar(buf, "?>\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4640 | if (cur->children != NULL) { |
| 4641 | xmlNodePtr child = cur->children; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4642 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4643 | /* global namespace definitions, the old way */ |
| 4644 | if (oldXMLWDcompatibility) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4645 | xmlGlobalNsListDump(buf, cur->oldNs); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4646 | else |
| 4647 | xmlUpgradeOldNs(cur); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4648 | |
| 4649 | while (child != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4650 | xmlNodeDump(buf, cur, child, 0, 1); |
| 4651 | xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4652 | child = child->next; |
| 4653 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4654 | } |
| 4655 | } |
| 4656 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4657 | /************************************************************************ |
| 4658 | * * |
| 4659 | * Dumping XML tree content to an I/O output buffer * |
| 4660 | * * |
| 4661 | ************************************************************************/ |
| 4662 | |
| 4663 | static void |
| 4664 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 4665 | int level, int format, const char *encoding); |
| 4666 | static void |
| 4667 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 4668 | int level, int format, const char *encoding); |
| 4669 | /** |
| 4670 | * xmlGlobalNsDumpOutput: |
| 4671 | * @buf: the XML buffer output |
| 4672 | * @cur: a namespace |
| 4673 | * |
| 4674 | * Dump a global Namespace, this is the old version based on PIs. |
| 4675 | */ |
| 4676 | static void |
| 4677 | xmlGlobalNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 4678 | if (cur == NULL) { |
| 4679 | #ifdef DEBUG_TREE |
| 4680 | fprintf(stderr, "xmlGlobalNsDump : Ns == NULL\n"); |
| 4681 | #endif |
| 4682 | return; |
| 4683 | } |
| 4684 | if (cur->type == XML_GLOBAL_NAMESPACE) { |
| 4685 | xmlOutputBufferWriteString(buf, "<?namespace"); |
| 4686 | if (cur->href != NULL) { |
| 4687 | xmlOutputBufferWriteString(buf, " href="); |
| 4688 | xmlBufferWriteQuotedString(buf->buffer, cur->href); |
| 4689 | } |
| 4690 | if (cur->prefix != NULL) { |
| 4691 | xmlOutputBufferWriteString(buf, " AS="); |
| 4692 | xmlBufferWriteQuotedString(buf->buffer, cur->prefix); |
| 4693 | } |
| 4694 | xmlOutputBufferWriteString(buf, "?>\n"); |
| 4695 | } |
| 4696 | } |
| 4697 | |
| 4698 | /** |
| 4699 | * xmlGlobalNsListDumpOutput: |
| 4700 | * @buf: the XML buffer output |
| 4701 | * @cur: the first namespace |
| 4702 | * |
| 4703 | * Dump a list of global Namespace, this is the old version based on PIs. |
| 4704 | */ |
| 4705 | static void |
| 4706 | xmlGlobalNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 4707 | while (cur != NULL) { |
| 4708 | xmlGlobalNsDumpOutput(buf, cur); |
| 4709 | cur = cur->next; |
| 4710 | } |
| 4711 | } |
| 4712 | |
| 4713 | /** |
| 4714 | * xmlNsDumpOutput: |
| 4715 | * @buf: the XML buffer output |
| 4716 | * @cur: a namespace |
| 4717 | * |
| 4718 | * Dump a local Namespace definition. |
| 4719 | * Should be called in the context of attributes dumps. |
| 4720 | */ |
| 4721 | static void |
| 4722 | xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 4723 | if (cur == NULL) { |
| 4724 | #ifdef DEBUG_TREE |
| 4725 | fprintf(stderr, "xmlNsDump : Ns == NULL\n"); |
| 4726 | #endif |
| 4727 | return; |
| 4728 | } |
| 4729 | if (cur->type == XML_LOCAL_NAMESPACE) { |
| 4730 | /* Within the context of an element attributes */ |
| 4731 | if (cur->prefix != NULL) { |
| 4732 | xmlOutputBufferWriteString(buf, " xmlns:"); |
| 4733 | xmlOutputBufferWriteString(buf, (const char *)cur->prefix); |
| 4734 | } else |
| 4735 | xmlOutputBufferWriteString(buf, " xmlns"); |
| 4736 | xmlOutputBufferWriteString(buf, "="); |
| 4737 | xmlBufferWriteQuotedString(buf->buffer, cur->href); |
| 4738 | } |
| 4739 | } |
| 4740 | |
| 4741 | /** |
| 4742 | * xmlNsListDumpOutput: |
| 4743 | * @buf: the XML buffer output |
| 4744 | * @cur: the first namespace |
| 4745 | * |
| 4746 | * Dump a list of local Namespace definitions. |
| 4747 | * Should be called in the context of attributes dumps. |
| 4748 | */ |
| 4749 | static void |
| 4750 | xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 4751 | while (cur != NULL) { |
| 4752 | xmlNsDumpOutput(buf, cur); |
| 4753 | cur = cur->next; |
| 4754 | } |
| 4755 | } |
| 4756 | |
| 4757 | /** |
| 4758 | * xmlDtdDumpOutput: |
| 4759 | * @buf: the XML buffer output |
| 4760 | * @doc: the document |
| 4761 | * @encoding: an optional encoding string |
| 4762 | * |
| 4763 | * Dump the XML document DTD, if any. |
| 4764 | */ |
| 4765 | static void |
| 4766 | xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) { |
| 4767 | if (dtd == NULL) { |
| 4768 | #ifdef DEBUG_TREE |
| 4769 | fprintf(stderr, "xmlDtdDump : no internal subset\n"); |
| 4770 | #endif |
| 4771 | return; |
| 4772 | } |
| 4773 | xmlOutputBufferWriteString(buf, "<!DOCTYPE "); |
| 4774 | xmlOutputBufferWriteString(buf, (const char *)dtd->name); |
| 4775 | if (dtd->ExternalID != NULL) { |
| 4776 | xmlOutputBufferWriteString(buf, " PUBLIC "); |
| 4777 | xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID); |
| 4778 | xmlOutputBufferWriteString(buf, " "); |
| 4779 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 4780 | } else if (dtd->SystemID != NULL) { |
| 4781 | xmlOutputBufferWriteString(buf, " SYSTEM "); |
| 4782 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 4783 | } |
| 4784 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
| 4785 | (dtd->attributes == NULL) && (dtd->notations == NULL)) { |
| 4786 | xmlOutputBufferWriteString(buf, ">"); |
| 4787 | return; |
| 4788 | } |
| 4789 | xmlOutputBufferWriteString(buf, " [\n"); |
| 4790 | xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding); |
| 4791 | #if 0 |
| 4792 | if (dtd->entities != NULL) |
| 4793 | xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) dtd->entities); |
| 4794 | if (dtd->notations != NULL) |
| 4795 | xmlDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations); |
| 4796 | if (dtd->elements != NULL) |
| 4797 | xmlDumpElementTable(buf, (xmlElementTablePtr) dtd->elements); |
| 4798 | if (dtd->attributes != NULL) |
| 4799 | xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) dtd->attributes); |
| 4800 | #endif |
| 4801 | xmlOutputBufferWriteString(buf, "]>"); |
| 4802 | } |
| 4803 | |
| 4804 | /** |
| 4805 | * xmlAttrDumpOutput: |
| 4806 | * @buf: the XML buffer output |
| 4807 | * @doc: the document |
| 4808 | * @cur: the attribute pointer |
| 4809 | * @encoding: an optional encoding string |
| 4810 | * |
| 4811 | * Dump an XML attribute |
| 4812 | */ |
| 4813 | static void |
| 4814 | xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, |
| 4815 | const char *encoding) { |
| 4816 | xmlChar *value; |
| 4817 | |
| 4818 | if (cur == NULL) { |
| 4819 | #ifdef DEBUG_TREE |
| 4820 | fprintf(stderr, "xmlAttrDump : property == NULL\n"); |
| 4821 | #endif |
| 4822 | return; |
| 4823 | } |
| 4824 | xmlOutputBufferWriteString(buf, " "); |
| 4825 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 4826 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 4827 | xmlOutputBufferWriteString(buf, ":"); |
| 4828 | } |
| 4829 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 4830 | value = xmlNodeListGetString(doc, cur->children, 0); |
| 4831 | if (value) { |
| 4832 | xmlOutputBufferWriteString(buf, "="); |
| 4833 | xmlBufferWriteQuotedString(buf->buffer, value); |
| 4834 | xmlFree(value); |
| 4835 | } else { |
| 4836 | xmlOutputBufferWriteString(buf, "=\"\""); |
| 4837 | } |
| 4838 | } |
| 4839 | |
| 4840 | /** |
| 4841 | * xmlAttrListDumpOutput: |
| 4842 | * @buf: the XML buffer output |
| 4843 | * @doc: the document |
| 4844 | * @cur: the first attribute pointer |
| 4845 | * @encoding: an optional encoding string |
| 4846 | * |
| 4847 | * Dump a list of XML attributes |
| 4848 | */ |
| 4849 | static void |
| 4850 | xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 4851 | xmlAttrPtr cur, const char *encoding) { |
| 4852 | if (cur == NULL) { |
| 4853 | #ifdef DEBUG_TREE |
| 4854 | fprintf(stderr, "xmlAttrListDump : property == NULL\n"); |
| 4855 | #endif |
| 4856 | return; |
| 4857 | } |
| 4858 | while (cur != NULL) { |
| 4859 | xmlAttrDumpOutput(buf, doc, cur, encoding); |
| 4860 | cur = cur->next; |
| 4861 | } |
| 4862 | } |
| 4863 | |
| 4864 | |
| 4865 | |
| 4866 | /** |
| 4867 | * xmlNodeListDumpOutput: |
| 4868 | * @buf: the XML buffer output |
| 4869 | * @doc: the document |
| 4870 | * @cur: the first node |
| 4871 | * @level: the imbrication level for indenting |
| 4872 | * @format: is formatting allowed |
| 4873 | * @encoding: an optional encoding string |
| 4874 | * |
| 4875 | * Dump an XML node list, recursive behaviour,children are printed too. |
| 4876 | */ |
| 4877 | static void |
| 4878 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 4879 | xmlNodePtr cur, int level, int format, const char *encoding) { |
| 4880 | int i; |
| 4881 | |
| 4882 | if (cur == NULL) { |
| 4883 | #ifdef DEBUG_TREE |
| 4884 | fprintf(stderr, "xmlNodeListDump : node == NULL\n"); |
| 4885 | #endif |
| 4886 | return; |
| 4887 | } |
| 4888 | while (cur != NULL) { |
| 4889 | if ((format) && (xmlIndentTreeOutput) && |
| 4890 | (cur->type == XML_ELEMENT_NODE)) |
| 4891 | for (i = 0;i < level;i++) |
| 4892 | xmlOutputBufferWriteString(buf, " "); |
| 4893 | xmlNodeDumpOutput(buf, doc, cur, level, format, encoding); |
| 4894 | if (format) { |
| 4895 | xmlOutputBufferWriteString(buf, "\n"); |
| 4896 | } |
| 4897 | cur = cur->next; |
| 4898 | } |
| 4899 | } |
| 4900 | |
| 4901 | /** |
| 4902 | * xmlNodeDumpOutput: |
| 4903 | * @buf: the XML buffer output |
| 4904 | * @doc: the document |
| 4905 | * @cur: the current node |
| 4906 | * @level: the imbrication level for indenting |
| 4907 | * @format: is formatting allowed |
| 4908 | * @encoding: an optional encoding string |
| 4909 | * |
| 4910 | * Dump an XML node, recursive behaviour,children are printed too. |
| 4911 | */ |
| 4912 | static void |
| 4913 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 4914 | int level, int format, const char *encoding) { |
| 4915 | int i; |
| 4916 | xmlNodePtr tmp; |
| 4917 | |
| 4918 | if (cur == NULL) { |
| 4919 | #ifdef DEBUG_TREE |
| 4920 | fprintf(stderr, "xmlNodeDump : node == NULL\n"); |
| 4921 | #endif |
| 4922 | return; |
| 4923 | } |
| 4924 | if (cur->type == XML_DTD_NODE) { |
| 4925 | xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding); |
| 4926 | return; |
| 4927 | } |
| 4928 | if (cur->type == XML_ELEMENT_DECL) { |
| 4929 | xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
| 4930 | return; |
| 4931 | } |
| 4932 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 4933 | xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
| 4934 | return; |
| 4935 | } |
| 4936 | if (cur->type == XML_ENTITY_DECL) { |
| 4937 | xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
| 4938 | return; |
| 4939 | } |
| 4940 | if (cur->type == XML_TEXT_NODE) { |
| 4941 | if (cur->content != NULL) { |
| 4942 | xmlChar *buffer; |
| 4943 | |
| 4944 | #ifndef XML_USE_BUFFER_CONTENT |
| 4945 | if (encoding == NULL) |
| 4946 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 4947 | else |
| 4948 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 4949 | #else |
| 4950 | if (encoding == NULL) |
| 4951 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 4952 | xmlBufferContent(cur->content)); |
| 4953 | else |
| 4954 | buffer = xmlEncodeSpecialChars(doc, |
| 4955 | xmlBufferContent(cur->content)); |
| 4956 | #endif |
| 4957 | if (buffer != NULL) { |
| 4958 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 4959 | xmlFree(buffer); |
| 4960 | } |
| 4961 | } |
| 4962 | return; |
| 4963 | } |
| 4964 | if (cur->type == XML_PI_NODE) { |
| 4965 | if (cur->content != NULL) { |
| 4966 | xmlOutputBufferWriteString(buf, "<?"); |
| 4967 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 4968 | if (cur->content != NULL) { |
| 4969 | xmlOutputBufferWriteString(buf, " "); |
| 4970 | #ifndef XML_USE_BUFFER_CONTENT |
| 4971 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 4972 | #else |
| 4973 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 4974 | #endif |
| 4975 | } |
| 4976 | xmlOutputBufferWriteString(buf, "?>"); |
| 4977 | } else { |
| 4978 | xmlOutputBufferWriteString(buf, "<?"); |
| 4979 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 4980 | xmlOutputBufferWriteString(buf, "?>"); |
| 4981 | } |
| 4982 | return; |
| 4983 | } |
| 4984 | if (cur->type == XML_COMMENT_NODE) { |
| 4985 | if (cur->content != NULL) { |
| 4986 | xmlOutputBufferWriteString(buf, "<!--"); |
| 4987 | #ifndef XML_USE_BUFFER_CONTENT |
| 4988 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 4989 | #else |
| 4990 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 4991 | #endif |
| 4992 | xmlOutputBufferWriteString(buf, "-->"); |
| 4993 | } |
| 4994 | return; |
| 4995 | } |
| 4996 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 4997 | xmlOutputBufferWriteString(buf, "&"); |
| 4998 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 4999 | xmlOutputBufferWriteString(buf, ";"); |
| 5000 | return; |
| 5001 | } |
| 5002 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 5003 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 5004 | if (cur->content != NULL) |
| 5005 | #ifndef XML_USE_BUFFER_CONTENT |
| 5006 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 5007 | #else |
| 5008 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 5009 | #endif |
| 5010 | xmlOutputBufferWriteString(buf, "]]>"); |
| 5011 | return; |
| 5012 | } |
| 5013 | |
| 5014 | if (format == 1) { |
| 5015 | tmp = cur->children; |
| 5016 | while (tmp != NULL) { |
| 5017 | if ((tmp->type == XML_TEXT_NODE) || |
| 5018 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 5019 | format = 0; |
| 5020 | break; |
| 5021 | } |
| 5022 | tmp = tmp->next; |
| 5023 | } |
| 5024 | } |
| 5025 | xmlOutputBufferWriteString(buf, "<"); |
| 5026 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5027 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 5028 | xmlOutputBufferWriteString(buf, ":"); |
| 5029 | } |
| 5030 | |
| 5031 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5032 | if (cur->nsDef) |
| 5033 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 5034 | if (cur->properties != NULL) |
| 5035 | xmlAttrListDumpOutput(buf, doc, cur->properties, encoding); |
| 5036 | |
| 5037 | if ((cur->content == NULL) && (cur->children == NULL) && |
| 5038 | (!xmlSaveNoEmptyTags)) { |
| 5039 | xmlOutputBufferWriteString(buf, "/>"); |
| 5040 | return; |
| 5041 | } |
| 5042 | xmlOutputBufferWriteString(buf, ">"); |
| 5043 | if (cur->content != NULL) { |
| 5044 | xmlChar *buffer; |
| 5045 | |
| 5046 | #ifndef XML_USE_BUFFER_CONTENT |
| 5047 | if (encoding == NULL) |
| 5048 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 5049 | else |
| 5050 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 5051 | #else |
| 5052 | if (encoding == NULL) |
| 5053 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5054 | xmlBufferContent(cur->content)); |
| 5055 | else |
| 5056 | buffer = xmlEncodeSpecialChars(doc, |
| 5057 | xmlBufferContent(cur->content)); |
| 5058 | #endif |
| 5059 | if (buffer != NULL) { |
| 5060 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 5061 | xmlFree(buffer); |
| 5062 | } |
| 5063 | } |
| 5064 | if (cur->children != NULL) { |
| 5065 | if (format) xmlOutputBufferWriteString(buf, "\n"); |
| 5066 | xmlNodeListDumpOutput(buf, doc, cur->children, |
| 5067 | (level >= 0?level+1:-1), format, encoding); |
| 5068 | if ((xmlIndentTreeOutput) && (format)) |
| 5069 | for (i = 0;i < level;i++) |
| 5070 | xmlOutputBufferWriteString(buf, " "); |
| 5071 | } |
| 5072 | xmlOutputBufferWriteString(buf, "</"); |
| 5073 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5074 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 5075 | xmlOutputBufferWriteString(buf, ":"); |
| 5076 | } |
| 5077 | |
| 5078 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5079 | xmlOutputBufferWriteString(buf, ">"); |
| 5080 | } |
| 5081 | |
| 5082 | /** |
| 5083 | * xmlDocContentDumpOutput: |
| 5084 | * @buf: the XML buffer output |
| 5085 | * @cur: the document |
| 5086 | * @encoding: an optional encoding string |
| 5087 | * |
| 5088 | * Dump an XML document. |
| 5089 | */ |
| 5090 | static void |
| 5091 | xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur, |
| 5092 | const char *encoding) { |
| 5093 | xmlOutputBufferWriteString(buf, "<?xml version="); |
| 5094 | if (cur->version != NULL) |
| 5095 | xmlBufferWriteQuotedString(buf->buffer, cur->version); |
| 5096 | else |
| 5097 | xmlOutputBufferWriteString(buf, "\"1.0\""); |
| 5098 | if (encoding == NULL) { |
| 5099 | if (cur->encoding != NULL) |
| 5100 | encoding = (const char *) cur->encoding; |
| 5101 | else if (cur->charset != XML_CHAR_ENCODING_UTF8) |
| 5102 | encoding = xmlGetCharEncodingName(cur->charset); |
| 5103 | } |
| 5104 | if (encoding != NULL) { |
| 5105 | xmlOutputBufferWriteString(buf, " encoding="); |
| 5106 | xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding); |
| 5107 | } |
| 5108 | switch (cur->standalone) { |
| 5109 | case 0: |
| 5110 | xmlOutputBufferWriteString(buf, " standalone=\"no\""); |
| 5111 | break; |
| 5112 | case 1: |
| 5113 | xmlOutputBufferWriteString(buf, " standalone=\"yes\""); |
| 5114 | break; |
| 5115 | } |
| 5116 | xmlOutputBufferWriteString(buf, "?>\n"); |
| 5117 | if (cur->children != NULL) { |
| 5118 | xmlNodePtr child = cur->children; |
| 5119 | |
| 5120 | /* global namespace definitions, the old way */ |
| 5121 | if (oldXMLWDcompatibility) |
| 5122 | xmlGlobalNsListDumpOutput(buf, cur->oldNs); |
| 5123 | else |
| 5124 | xmlUpgradeOldNs(cur); |
| 5125 | |
| 5126 | while (child != NULL) { |
| 5127 | xmlNodeDumpOutput(buf, cur, child, 0, 1, encoding); |
| 5128 | xmlOutputBufferWriteString(buf, "\n"); |
| 5129 | child = child->next; |
| 5130 | } |
| 5131 | } |
| 5132 | } |
| 5133 | |
| 5134 | /************************************************************************ |
| 5135 | * * |
| 5136 | * Saving functions front-ends * |
| 5137 | * * |
| 5138 | ************************************************************************/ |
| 5139 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5140 | /** |
| 5141 | * xmlDocDumpMemory: |
| 5142 | * @cur: the document |
| 5143 | * @mem: OUT: the memory pointer |
| 5144 | * @size: OUT: the memory lenght |
| 5145 | * |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5146 | * 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] | 5147 | * It's up to the caller to free the memory. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5148 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5149 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5150 | xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5151 | xmlBufferPtr buf; |
| 5152 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5153 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 5154 | #ifdef DEBUG_TREE |
| 5155 | fprintf(stderr, "xmlDocDumpMemory : document == NULL\n"); |
| 5156 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5157 | *mem = NULL; |
| 5158 | *size = 0; |
| 5159 | return; |
| 5160 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5161 | buf = xmlBufferCreate(); |
| 5162 | if (buf == NULL) { |
| 5163 | *mem = NULL; |
| 5164 | *size = 0; |
| 5165 | return; |
| 5166 | } |
| 5167 | xmlDocContentDump(buf, cur); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 5168 | *mem = xmlStrndup(buf->content, buf->use); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5169 | *size = buf->use; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 5170 | xmlBufferFree(buf); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5173 | /** |
| 5174 | * xmlGetDocCompressMode: |
| 5175 | * @doc: the document |
| 5176 | * |
| 5177 | * get the compression ratio for a document, ZLIB based |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 5178 | * Returns 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5179 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5180 | int |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5181 | xmlGetDocCompressMode (xmlDocPtr doc) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5182 | if (doc == NULL) return(-1); |
| 5183 | return(doc->compression); |
| 5184 | } |
| 5185 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5186 | /** |
| 5187 | * xmlSetDocCompressMode: |
| 5188 | * @doc: the document |
| 5189 | * @mode: the compression ratio |
| 5190 | * |
| 5191 | * set the compression ratio for a document, ZLIB based |
| 5192 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 5193 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5194 | void |
| 5195 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5196 | if (doc == NULL) return; |
| 5197 | if (mode < 0) doc->compression = 0; |
| 5198 | else if (mode > 9) doc->compression = 9; |
| 5199 | else doc->compression = mode; |
| 5200 | } |
| 5201 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5202 | /** |
| 5203 | * xmlGetCompressMode: |
| 5204 | * |
| 5205 | * get the default compression mode used, ZLIB based. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 5206 | * Returns 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5207 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5208 | int |
| 5209 | xmlGetCompressMode(void) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5210 | return(xmlCompressMode); |
| 5211 | } |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5212 | |
| 5213 | /** |
| 5214 | * xmlSetCompressMode: |
| 5215 | * @mode: the compression ratio |
| 5216 | * |
| 5217 | * set the default compression mode used, ZLIB based |
| 5218 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 5219 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5220 | void |
| 5221 | xmlSetCompressMode(int mode) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5222 | if (mode < 0) xmlCompressMode = 0; |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5223 | else if (mode > 9) xmlCompressMode = 9; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5224 | else xmlCompressMode = mode; |
| 5225 | } |
| 5226 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5227 | #if 0 |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5228 | /** |
| 5229 | * xmlDocDump: |
| 5230 | * @f: the FILE* |
| 5231 | * @cur: the document |
| 5232 | * |
| 5233 | * Dump an XML document to an open FILE. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5234 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5235 | void |
| 5236 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5237 | xmlBufferPtr buf; |
| 5238 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5239 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 5240 | #ifdef DEBUG_TREE |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5241 | fprintf(stderr, "xmlDocDump : document == NULL\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 5242 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5243 | return; |
| 5244 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5245 | buf = xmlBufferCreate(); |
| 5246 | if (buf == NULL) return; |
| 5247 | xmlDocContentDump(buf, cur); |
| 5248 | xmlBufferDump(f, buf); |
| 5249 | xmlBufferFree(buf); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5250 | } |
| 5251 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5252 | /** |
| 5253 | * xmlSaveFile: |
| 5254 | * @filename: the filename |
| 5255 | * @cur: the document |
| 5256 | * |
| 5257 | * Dump an XML document to a file. Will use compression if |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 5258 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 5259 | * used. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5260 | * returns: the number of file written or -1 in case of failure. |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5261 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5262 | int |
| 5263 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5264 | xmlBufferPtr buf; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5265 | #ifdef HAVE_ZLIB_H |
| 5266 | gzFile zoutput = NULL; |
| 5267 | char mode[15]; |
| 5268 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 5269 | FILE *output = NULL; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5270 | int ret; |
| 5271 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5272 | /* |
| 5273 | * save the content to a temp buffer. |
| 5274 | */ |
| 5275 | buf = xmlBufferCreate(); |
| 5276 | if (buf == NULL) return(0); |
| 5277 | xmlDocContentDump(buf, cur); |
| 5278 | |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5279 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 5280 | if (cur->compression < 0) cur->compression = xmlCompressMode; |
Daniel Veillard | dc3dd9d | 1998-09-24 19:25:54 +0000 | [diff] [blame] | 5281 | if ((cur->compression > 0) && (cur->compression <= 9)) { |
| 5282 | sprintf(mode, "w%d", cur->compression); |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 5283 | if (!strcmp(filename, "-")) |
| 5284 | zoutput = gzdopen(1, mode); |
| 5285 | else |
| 5286 | zoutput = gzopen(filename, mode); |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5287 | } |
| 5288 | if (zoutput == NULL) { |
| 5289 | #endif |
| 5290 | output = fopen(filename, "w"); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 5291 | if (output == NULL) { |
| 5292 | xmlBufferFree(buf); |
| 5293 | return(-1); |
| 5294 | } |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5295 | #ifdef HAVE_ZLIB_H |
| 5296 | } |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5297 | |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5298 | if (zoutput != NULL) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5299 | ret = gzwrite(zoutput, buf->content, sizeof(xmlChar) * buf->use); |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5300 | gzclose(zoutput); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5301 | } else { |
| 5302 | #endif |
| 5303 | ret = xmlBufferDump(output, buf); |
| 5304 | fclose(output); |
| 5305 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5306 | } |
| 5307 | #endif |
Manish Vachharajani | 5e60f5a | 1999-05-29 03:04:30 +0000 | [diff] [blame] | 5308 | xmlBufferFree(buf); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5309 | return(ret * sizeof(xmlChar)); |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5310 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5311 | #else |
| 5312 | /** |
| 5313 | * xmlDocDump: |
| 5314 | * @f: the FILE* |
| 5315 | * @cur: the document |
| 5316 | * |
| 5317 | * Dump an XML document to an open FILE. |
| 5318 | * |
| 5319 | * returns: the number of file written or -1 in case of failure. |
| 5320 | */ |
| 5321 | int |
| 5322 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
| 5323 | xmlOutputBufferPtr buf; |
| 5324 | int ret; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5325 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5326 | if (cur == NULL) { |
| 5327 | #ifdef DEBUG_TREE |
| 5328 | fprintf(stderr, "xmlDocDump : document == NULL\n"); |
| 5329 | #endif |
| 5330 | return(-1); |
| 5331 | } |
| 5332 | buf = xmlOutputBufferCreateFile(f, NULL); |
| 5333 | if (buf == NULL) return(-1); |
| 5334 | xmlDocContentDumpOutput(buf, cur, NULL); |
| 5335 | |
| 5336 | ret = xmlOutputBufferClose(buf); |
| 5337 | return(ret); |
| 5338 | } |
| 5339 | |
| 5340 | /** |
| 5341 | * xmlSaveFile: |
| 5342 | * @filename: the filename (or URL) |
| 5343 | * @cur: the document |
| 5344 | * |
| 5345 | * Dump an XML document to a file. Will use compression if |
| 5346 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 5347 | * used. |
| 5348 | * returns: the number of file written or -1 in case of failure. |
| 5349 | */ |
| 5350 | int |
| 5351 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
| 5352 | xmlOutputBufferPtr buf; |
| 5353 | int ret; |
| 5354 | |
| 5355 | /* |
| 5356 | * save the content to a temp buffer. |
| 5357 | */ |
| 5358 | #ifdef HAVE_ZLIB_H |
| 5359 | if (cur->compression < 0) cur->compression = xmlCompressMode; |
| 5360 | #endif |
| 5361 | buf = xmlOutputBufferCreateFilename(filename, NULL, cur->compression); |
| 5362 | if (buf == NULL) return(0); |
| 5363 | |
| 5364 | xmlDocContentDumpOutput(buf, cur, NULL); |
| 5365 | |
| 5366 | ret = xmlOutputBufferClose(buf); |
| 5367 | return(ret); |
| 5368 | } |
| 5369 | |
| 5370 | /** |
| 5371 | * xmlSaveFileTo: |
| 5372 | * @buf: an output I/O buffer |
| 5373 | * @cur: the document |
| 5374 | * @encoding: the encoding if any assuming the i/O layer handles the trancoding |
| 5375 | * |
| 5376 | * Dump an XML document to an I/O buffer. |
| 5377 | * |
| 5378 | * returns: the number of file written or -1 in case of failure. |
| 5379 | */ |
| 5380 | int |
| 5381 | xmlSaveFileTo(xmlOutputBuffer *buf, xmlDocPtr cur, const char *encoding) { |
| 5382 | int ret; |
| 5383 | |
| 5384 | if (buf == NULL) return(0); |
| 5385 | xmlDocContentDumpOutput(buf, cur, encoding); |
| 5386 | ret = xmlOutputBufferClose(buf); |
| 5387 | return(ret); |
| 5388 | } |
| 5389 | |
| 5390 | /** |
| 5391 | * xmlSaveFileEnc: |
| 5392 | * @filename: the filename (or URL) |
| 5393 | * @cur: the document |
| 5394 | * @encoding: the name of an encoding (or NULL) |
| 5395 | * |
| 5396 | * Dump an XML document, converting it to the given encoding |
| 5397 | * |
| 5398 | * returns: the number of file written or -1 in case of failure. |
| 5399 | */ |
| 5400 | int |
| 5401 | xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) { |
| 5402 | xmlOutputBufferPtr buf; |
| 5403 | xmlCharEncodingHandlerPtr handler = NULL; |
| 5404 | int ret; |
| 5405 | |
| 5406 | if (encoding != NULL) { |
| 5407 | xmlCharEncoding enc; |
| 5408 | |
| 5409 | enc = xmlParseCharEncoding(encoding); |
| 5410 | if (enc != cur->charset) { |
| 5411 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
| 5412 | /* |
| 5413 | * Not supported yet |
| 5414 | */ |
| 5415 | return(-1); |
| 5416 | } |
| 5417 | |
| 5418 | handler = xmlFindCharEncodingHandler(encoding); |
| 5419 | if (handler == NULL) |
| 5420 | return(-1); |
| 5421 | } |
| 5422 | } |
| 5423 | |
| 5424 | /* |
| 5425 | * save the content to a temp buffer. |
| 5426 | */ |
| 5427 | buf = xmlOutputBufferCreateFilename(filename, handler, 0); |
| 5428 | if (buf == NULL) return(0); |
| 5429 | |
| 5430 | xmlDocContentDumpOutput(buf, cur, encoding); |
| 5431 | |
| 5432 | ret = xmlOutputBufferClose(buf); |
| 5433 | return(ret); |
| 5434 | } |
| 5435 | #endif |